Skip to content

Commit

Permalink
fixed errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zac-williamson committed Sep 27, 2024
1 parent 6630f9e commit 5f67df0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
9 changes: 3 additions & 6 deletions src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ impl<BigNum, CurveParams> BigCurve<BigNum, CurveParams> 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);
Expand Down Expand Up @@ -396,7 +396,7 @@ impl<BigNum, CurveParams> BigCurve<BigNum, CurveParams> 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);
Expand Down Expand Up @@ -630,10 +630,7 @@ impl<BigNum, CurveParams> BigCurve<BigNum, CurveParams> where CurveParams: Curve
accumulator
}

unconstrained fn get_mul_transcript<let NScalarSlices: u32>(
P: Self,
scalar: ScalarField<NScalarSlices>
) -> [AffineTranscript<BigNum>; 326] {
unconstrained fn get_mul_transcript<let NScalarSlices: u32>(P: Self, scalar: ScalarField<NScalarSlices>) -> [AffineTranscript<BigNum>; 326] {
CurveJ::from(P).mul(scalar).1.as_array()
}

Expand Down
43 changes: 22 additions & 21 deletions src/scalar_field.nr
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ struct ScalarField<let N: u32> {

// 1, 2, 3, 4
unconstrained fn get_wnaf_slices<let N: u32>(x: Field) -> ([u8; N], bool) {

let mut result: [u8; N] = [0; N];
let mut nibbles = x.to_le_radix(16, N);

Expand All @@ -44,7 +43,6 @@ unconstrained fn get_wnaf_slices<let N: u32>(x: Field) -> ([u8; N], bool) {
}

unconstrained fn get_wnaf_slices2<let N: u32, let M: u32>(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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -184,43 +181,44 @@ impl<let N: u32> std::convert::Into<Field> for ScalarField<N> {
impl<let N: u32> ScalarField<N> {

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<let NumLimbs: u32, Params>(x: BigNum<NumLimbs, Params>) -> Self where Params: BigNumParamsTrait<NumLimbs> + RuntimeBigNumParamsTrait<NumLimbs> {
fn from_bignum<let NumLimbs: u32, Params>(x: BigNum<NumLimbs, Params>) -> Self where Params: BigNumParamsTrait<NumLimbs> + RuntimeBigNumParamsTrait<NumLimbs> {
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<BigNum>(self) -> BigNum where BigNum: BigNumTrait {
fn into_bignum<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() {
Expand All @@ -232,14 +230,17 @@ impl<let N: u32> ScalarField<N> {
}
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 }
}
Expand Down

0 comments on commit 5f67df0

Please sign in to comment.