Skip to content

Commit

Permalink
Merge pull request AleoNet#217 from AleoHQ/feat/fft
Browse files Browse the repository at this point in the history
Adds traits FftField and FftParameters for all fields
  • Loading branch information
howardwu authored Jun 28, 2021
2 parents 4932465 + d40d954 commit c2d080c
Show file tree
Hide file tree
Showing 124 changed files with 2,880 additions and 1,912 deletions.
28 changes: 14 additions & 14 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:

rust-stable:
docker:
- image: cimg/rust:1.51.0
- image: cimg/rust:1.53.0
resource_class: 2xlarge
steps:
- checkout
Expand Down Expand Up @@ -93,7 +93,7 @@ jobs:

snarkvm-algorithms:
docker:
- image: cimg/rust:1.51.0
- image: cimg/rust:1.53.0
resource_class: xlarge
steps:
- checkout
Expand All @@ -108,7 +108,7 @@ jobs:

snarkvm-curves:
docker:
- image: cimg/rust:1.51.0
- image: cimg/rust:1.53.0
resource_class: xlarge
steps:
- checkout
Expand All @@ -123,7 +123,7 @@ jobs:

snarkvm-derives:
docker:
- image: cimg/rust:1.51.0
- image: cimg/rust:1.53.0
resource_class: xlarge
steps:
- checkout
Expand All @@ -138,7 +138,7 @@ jobs:

snarkvm-dpc:
docker:
- image: cimg/rust:1.51.0
- image: cimg/rust:1.53.0
resource_class: xlarge
steps:
- checkout
Expand All @@ -153,7 +153,7 @@ jobs:

snarkvm-fields:
docker:
- image: cimg/rust:1.51.0
- image: cimg/rust:1.53.0
resource_class: xlarge
steps:
- checkout
Expand All @@ -168,7 +168,7 @@ jobs:

snarkvm-gadgets:
docker:
- image: cimg/rust:1.51.0
- image: cimg/rust:1.53.0
resource_class: xlarge
steps:
- checkout
Expand All @@ -183,7 +183,7 @@ jobs:

snarkvm-marlin:
docker:
- image: cimg/rust:1.51.0
- image: cimg/rust:1.53.0
resource_class: xlarge
steps:
- checkout
Expand All @@ -198,7 +198,7 @@ jobs:

snarkvm-parameters:
docker:
- image: cimg/rust:1.51.0
- image: cimg/rust:1.53.0
resource_class: xlarge
steps:
- checkout
Expand All @@ -213,7 +213,7 @@ jobs:

snarkvm-polycommit:
docker:
- image: cimg/rust:1.51.0
- image: cimg/rust:1.53.0
resource_class: xlarge
steps:
- checkout
Expand All @@ -228,7 +228,7 @@ jobs:

snarkvm-posw:
docker:
- image: cimg/rust:1.51.0
- image: cimg/rust:1.53.0
resource_class: xlarge
steps:
- checkout
Expand All @@ -243,7 +243,7 @@ jobs:

snarkvm-profiler:
docker:
- image: cimg/rust:1.51.0
- image: cimg/rust:1.53.0
resource_class: xlarge
steps:
- checkout
Expand All @@ -258,7 +258,7 @@ jobs:

snarkvm-r1cs:
docker:
- image: cimg/rust:1.51.0
- image: cimg/rust:1.53.0
resource_class: xlarge
steps:
- checkout
Expand All @@ -273,7 +273,7 @@ jobs:

snarkvm-utilities:
docker:
- image: cimg/rust:1.51.0
- image: cimg/rust:1.53.0
resource_class: xlarge
steps:
- checkout
Expand Down
2 changes: 1 addition & 1 deletion algorithms/benches/snark/gm17.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<F: Field> ConstraintSynthesizer<F> for Benchmark<F> {
let new_entry = {
let (input_1_val, input_1_var) = variables[i];
let (input_2_val, input_2_var) = variables[i + 1];
let result_val = input_1_val.and_then(|input_1| input_2_val.map(|input_2| input_1 * &input_2));
let result_val = input_1_val.and_then(|input_1| input_2_val.map(|input_2| input_1 * input_2));
let result_var = cs.alloc(
|| format!("result_{}", i),
|| result_val.ok_or(SynthesisError::AssignmentMissing),
Expand Down
4 changes: 2 additions & 2 deletions algorithms/examples/snark/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl<F: Field> ConstraintSynthesizer<F> for Benchmark<F> {

for i in 0..self.num_constraints - 1 {
if i % 2 != 0 {
let c_val = a_val * &b_val;
let c_val = a_val * b_val;
let c_var = cs.alloc(|| format!("{}", i), || Ok(c_val))?;

cs.enforce(
Expand All @@ -63,7 +63,7 @@ impl<F: Field> ConstraintSynthesizer<F> for Benchmark<F> {
b_val = c_val;
b_var = c_var;
} else {
let c_val = a_val + &b_val;
let c_val = a_val + b_val;
let c_var = cs.alloc(|| format!("{}", i), || Ok(c_val))?;

cs.enforce(
Expand Down
4 changes: 2 additions & 2 deletions algorithms/src/crh/bowe_hopwood_pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ impl<G: Group, S: PedersenSize> CRH for BoweHopwoodPedersenCRH<G, S> {
&generator
[(chunk_bits[0] as usize) | (chunk_bits[1] as usize) << 1 | (chunk_bits[2] as usize) << 2]
})
.fold(G::zero(), |a, b| a + &b)
.fold(G::zero(), |a, b| a + b)
})
.fold(G::zero(), |a, b| a + &b);
.fold(G::zero(), |a, b| a + b);

end_timer!(eval_time);

Expand Down
2 changes: 1 addition & 1 deletion algorithms/src/crh/bowe_hopwood_pedersen_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<G: Group> BoweHopwoodPedersenCRHParameters<G> {
encoded += g;
}
if (i & 0x02) != 0 {
encoded += &g.double();
encoded += g.double();
}
if (i & 0x04) != 0 {
encoded = encoded.neg();
Expand Down
2 changes: 1 addition & 1 deletion algorithms/src/crh/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl<G: Group, S: PedersenSize> CRH for PedersenCRH<G, S> {
}
encoded
})
.fold(G::zero(), |a, b| a + &b);
.fold(G::zero(), |a, b| a + b);

Ok(result)
}
Expand Down
88 changes: 44 additions & 44 deletions algorithms/src/encoding/elligator2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl<P: MontgomeryModelParameters + TEModelParameters, G: Group + ProjectiveCurv

// Compute the parameters for the alternate Montgomery form: v^2 == u^3 + A * u^2 + B * u.
let (a, b) = {
let a = Self::A * &Self::B.inverse().unwrap();
let b = P::BaseField::one() * &Self::B.square().inverse().unwrap();
let a = Self::A * Self::B.inverse().unwrap();
let b = P::BaseField::one() * Self::B.square().inverse().unwrap();
(a, b)
};

Expand All @@ -65,45 +65,45 @@ impl<P: MontgomeryModelParameters + TEModelParameters, G: Group + ProjectiveCurv
let u = Self::D;

// Let ur2 = u * r^2;
let ur2 = r.square() * &u;
let ur2 = r.square() * u;

{
// Verify u is a quadratic nonresidue.
#[cfg(debug_assertions)]
assert!(u.legendre().is_qnr());

// Verify 1 + ur^2 != 0.
assert_ne!(P::BaseField::one() + &ur2, P::BaseField::zero());
assert_ne!(P::BaseField::one() + ur2, P::BaseField::zero());

// Verify A^2 * ur^2 != B(1 + ur^2)^2.
let a2 = a.square();
assert_ne!(a2 * &ur2, (P::BaseField::one() + &ur2).square() * &b);
assert_ne!(a2 * ur2, (P::BaseField::one() + ur2).square() * b);
}

// Let v = -A / (1 + ur^2).
let v = (P::BaseField::one() + &ur2).inverse().unwrap() * &(-a);
let v = (P::BaseField::one() + ur2).inverse().unwrap() * (-a);

// Let e = legendre(v^3 + Av^2 + Bv).
let v2 = v.square();
let v3 = v2 * &v;
let av2 = a * &v2;
let bv = b * &v;
let e = (v3 + &(av2 + &bv)).legendre();
let v3 = v2 * v;
let av2 = a * v2;
let bv = b * v;
let e = (v3 + (av2 + bv)).legendre();

// Let x = ev - ((1 - e) * A/2).
let two = P::BaseField::one().double();
let x = match e {
LegendreSymbol::Zero => -(a * &two.inverse().unwrap()),
LegendreSymbol::Zero => -(a * two.inverse().unwrap()),
LegendreSymbol::QuadraticResidue => v,
LegendreSymbol::QuadraticNonResidue => (-v) - &a,
LegendreSymbol::QuadraticNonResidue => (-v) - a,
};

// Let y = -e * sqrt(x^3 + Ax^2 + Bx).
let x2 = x.square();
let x3 = x2 * &x;
let ax2 = a * &x2;
let bx = b * &x;
let value = (x3 + &(ax2 + &bx)).sqrt().unwrap();
let x3 = x2 * x;
let ax2 = a * x2;
let bx = b * x;
let value = (x3 + (ax2 + bx)).sqrt().unwrap();
let y = match e {
LegendreSymbol::Zero => P::BaseField::zero(),
LegendreSymbol::QuadraticResidue => -value,
Expand All @@ -118,35 +118,35 @@ impl<P: MontgomeryModelParameters + TEModelParameters, G: Group + ProjectiveCurv
// Enforce v^2 == u^3 + A * u^2 + B * u
let v2 = v.square();
let u2 = u.square();
let u3 = u2 * &u;
assert_eq!(v2, u3 + &(a * &u2) + &(b * &u));
let u3 = u2 * u;
assert_eq!(v2, u3 + (a * u2) + (b * u));
}

// Convert the alternate Montgomery element (u, v) to Montgomery element (s, t).
let (s, t) = {
let s = u * &Self::B;
let t = v * &Self::B;
let s = u * Self::B;
let t = v * Self::B;

// Ensure (s, t) is a valid Montgomery element
#[cfg(debug_assertions)]
{
// Enforce B * t^2 == s^3 + A * s^2 + s
let t2 = t.square();
let s2 = s.square();
let s3 = s2 * &s;
assert_eq!(Self::B * &t2, s3 + &(Self::A * &s2) + &s);
let s3 = s2 * s;
assert_eq!(Self::B * t2, s3 + (Self::A * s2) + s);
}

(s, t)
};

// Convert the Montgomery element (s, t) to the twisted Edwards element (x, y).
let (x, y) = {
let x = s * &t.inverse().unwrap();
let x = s * t.inverse().unwrap();

let numerator = s - &P::BaseField::one();
let denominator = s + &P::BaseField::one();
let y = numerator * &denominator.inverse().unwrap();
let numerator = s - P::BaseField::one();
let denominator = s + P::BaseField::one();
let y = numerator * denominator.inverse().unwrap();

(x, y)
};
Expand All @@ -169,39 +169,39 @@ impl<P: MontgomeryModelParameters + TEModelParameters, G: Group + ProjectiveCurv

// Compute the parameters for the alternate Montgomery form: v^2 == u^3 + A * u^2 + B * u.
let (a, b) = {
let a = Self::A * &Self::B.inverse().unwrap();
let b = P::BaseField::one() * &Self::B.square().inverse().unwrap();
let a = Self::A * Self::B.inverse().unwrap();
let b = P::BaseField::one() * Self::B.square().inverse().unwrap();
(a, b)
};

// Convert the twisted Edwards element (x, y) to the alternate Montgomery element (u, v)
let (u_reconstructed, v_reconstructed) = {
let numerator = P::BaseField::one() + &y;
let denominator = P::BaseField::one() - &y;
let numerator = P::BaseField::one() + y;
let denominator = P::BaseField::one() - y;

let u = numerator * &(denominator.inverse().unwrap());
let v = numerator * &((denominator * &x).inverse().unwrap());
let u = numerator * (denominator.inverse().unwrap());
let v = numerator * ((denominator * x).inverse().unwrap());

// Ensure (u, v) is a valid Montgomery element
#[cfg(debug_assertions)]
{
// Enforce B * v^2 == u^3 + A * u^2 + u
let v2 = v.square();
let u2 = u.square();
let u3 = u2 * &u;
assert_eq!(Self::B * &v2, u3 + &(Self::A * &u2) + &u);
let u3 = u2 * u;
assert_eq!(Self::B * v2, u3 + (Self::A * u2) + u);
}

let u = u * &Self::B.inverse().unwrap();
let v = v * &Self::B.inverse().unwrap();
let u = u * Self::B.inverse().unwrap();
let v = v * Self::B.inverse().unwrap();

// Ensure (u, v) is a valid alternate Montgomery element.
{
// Enforce v^2 == u^3 + A * u^2 + B * u
let v2 = v.square();
let u2 = u.square();
let u3 = u2 * &u;
assert_eq!(v2, u3 + &(a * &u2) + &(b * &u));
let u3 = u2 * u;
assert_eq!(v2, u3 + (a * u2) + (b * u));
}

(u, v)
Expand All @@ -227,21 +227,21 @@ impl<P: MontgomeryModelParameters + TEModelParameters, G: Group + ProjectiveCurv
}

// Verify -ux(x + A) is a residue.
assert_eq!((-(u * &x) * &(x + &a)).legendre(), LegendreSymbol::QuadraticResidue);
assert_eq!((-(u * x) * (x + a)).legendre(), LegendreSymbol::QuadraticResidue);
}

let exists_in_sqrt_fq2 = v_reconstructed.square().sqrt().unwrap() == v_reconstructed;

let element = if exists_in_sqrt_fq2 {
// Let value = sqrt(-x / ((x + A) * u)).
let numerator = -x;
let denominator = (x + &a) * &u;
(numerator * &denominator.inverse().unwrap()).sqrt().unwrap()
let denominator = (x + a) * u;
(numerator * denominator.inverse().unwrap()).sqrt().unwrap()
} else {
// Let value2 = sqrt(-(x + A) / ux)).
let numerator = -x - &a;
let denominator = x * &u;
(numerator * &denominator.inverse().unwrap()).sqrt().unwrap()
let numerator = -x - a;
let denominator = x * u;
(numerator * denominator.inverse().unwrap()).sqrt().unwrap()
};

let element = if sign_high {
Expand Down
Loading

0 comments on commit c2d080c

Please sign in to comment.