From 5f67df02d1fa3110d542b20b92a081363066a23c Mon Sep 17 00:00:00 2001 From: zac-williamson Date: Fri, 27 Sep 2024 14:42:03 -0700 Subject: [PATCH] fixed errors --- src/lib.nr | 9 +++------ src/scalar_field.nr | 43 ++++++++++++++++++++++--------------------- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/lib.nr b/src/lib.nr index c0dd2d7..59e0e8f 100644 --- a/src/lib.nr +++ b/src/lib.nr @@ -294,7 +294,7 @@ impl BigCurve where CurveParams: Curve let conditional_select = |a: bool, b: bool, predicate: bool| ((a as Field - b as Field) * predicate as Field + b as Field) as bool; - let mut infinity_out = conditional_select(true, false, evaluate_group_operation_predicate); + let mut infinity_out = conditional_select(false, true, evaluate_group_operation_predicate); infinity_out = conditional_select(true, infinity_out, infinity_predicate); infinity_out = conditional_select(self.is_infinity, infinity_out, output_is_lhs); infinity_out = conditional_select(other.is_infinity, infinity_out, output_is_rhs); @@ -396,7 +396,7 @@ impl BigCurve where CurveParams: Curve let conditional_select = |a: bool, b: bool, predicate: bool| ((a as Field - b as Field) * predicate as Field + b as Field) as bool; - let mut infinity_out = conditional_select(true, false, evaluate_group_operation_predicate); + let mut infinity_out = conditional_select(false, true, evaluate_group_operation_predicate); infinity_out = conditional_select(true, infinity_out, infinity_predicate); infinity_out = conditional_select(self.is_infinity, infinity_out, output_is_lhs); infinity_out = conditional_select(other.is_infinity, infinity_out, output_is_rhs); @@ -630,10 +630,7 @@ impl BigCurve where CurveParams: Curve accumulator } - unconstrained fn get_mul_transcript( - P: Self, - scalar: ScalarField - ) -> [AffineTranscript; 326] { + unconstrained fn get_mul_transcript(P: Self, scalar: ScalarField) -> [AffineTranscript; 326] { CurveJ::from(P).mul(scalar).1.as_array() } diff --git a/src/scalar_field.nr b/src/scalar_field.nr index 0119927..1951f2b 100644 --- a/src/scalar_field.nr +++ b/src/scalar_field.nr @@ -24,7 +24,6 @@ struct ScalarField { // 1, 2, 3, 4 unconstrained fn get_wnaf_slices(x: Field) -> ([u8; N], bool) { - let mut result: [u8; N] = [0; N]; let mut nibbles = x.to_le_radix(16, N); @@ -44,7 +43,6 @@ unconstrained fn get_wnaf_slices(x: Field) -> ([u8; N], bool) { } unconstrained fn get_wnaf_slices2(x: [Field; M]) -> ([u8; N], bool) { - let mut result: [u8; N] = [0; N]; let mut nibbles: [[u8; 30]; M] = [[0; 30]; M]; for i in 0..M { @@ -80,7 +78,6 @@ unconstrained fn from_wnaf_slices(x: [u8; 64], skew: bool) -> Field { result } - unconstrained fn get_modulus_slices() -> (Field, Field) { let bytes = std::field::modulus_be_bytes(); let num_bytes = (std::field::modulus_num_bits() / 8) + ((std::field::modulus_num_bits() % 8 != 0) as u64); @@ -184,43 +181,44 @@ impl std::convert::Into for ScalarField { impl ScalarField { fn zero() -> Self { - let mut result: Self = ScalarField{ base4_slices: [0; N], skew: true }; - - result.base4_slices[N-1] = 8; // 8 = numeric value of 1, 0 = numeric value of -15 + let mut result: Self = ScalarField { base4_slices: [0; N], skew: true }; + + result.base4_slices[0] = 8; // 8 = numeric value of 1, 0 = numeric value of -15 result } fn conditional_select(lhs: Self, rhs: Self, predicate: bool) -> Self { let mut result = rhs; - if (predicate) - { + if (predicate) { result = lhs; } result } // Note: I can't propagate ModulusBits or NumLimbs from a generic that satisfies BigNumTrait due to bugs, so we have to pass NumLimbs and Params in directly. disgusting! - fn from_bignum(x: BigNum) -> Self where Params: BigNumParamsTrait + RuntimeBigNumParamsTrait { + fn from_bignum(x: BigNum) -> Self where Params: BigNumParamsTrait + RuntimeBigNumParamsTrait { x.validate_in_field(); let mut (slices, skew): ([u8; N], bool) = get_wnaf_slices2(x.limbs); // TODO: NONE OF THIS IS CONSTRAINED YET. FIX! - Self{ base4_slices: slices, skew } + Self { base4_slices: slices, skew } } - fn into_bignum(self) -> BigNum where BigNum: BigNumTrait { + fn into_bignum(self) -> BigNum where BigNum: BigNumTrait { let mut result = BigNum::new(); let mut count: u64 = 0; { let mut acc = 0; - let mut last_bits = (result.modulus_bits() % 120) as u64; - if (last_bits == 0) { last_bits = 120; } - let mut last_nibbles = (last_bits / 4) + (last_bits % 4 != 0) as u64; - for _ in 0..last_nibbles { - acc = acc * 16; - acc = acc + (self.base4_slices[count] as Field) * 2 - 15; - count = count + 1; - } + let mut last_bits = (result.modulus_bits() % 120) as u64; + if (last_bits == 0) { + last_bits = 120; + } + let mut last_nibbles = (last_bits / 4) + (last_bits % 4 != 0) as u64; + for _ in 0..last_nibbles { + acc = acc * 16; + acc = acc + (self.base4_slices[count] as Field) * 2 - 15; + count = count + 1; + } result.set_limb(result.num_limbs() - 1, acc); } for i in 1..result.num_limbs() { @@ -232,14 +230,17 @@ impl ScalarField { } if acc.lt(0x1000000000000000000000000000000) == false { acc += 0x1000000000000000000000000000000; - result.set_limb(result.num_limbs() - i, result.get_limb((result.num_limbs() - i) as u64) - 1); + result.set_limb( + result.num_limbs() - i, + result.get_limb((result.num_limbs() - i) as u64) - 1 + ); } result.set_limb(result.num_limbs() - 1 - i, acc); } result.set_limb(0, result.get_limb(0) - self.skew as Field); result } - + fn new() -> Self { Self { base4_slices: [0; N], skew: false } }