Runs rustfmt on the repo
jump to
@@ -13,9 +13,9 @@
#[derive(Derivative)] #[derivative(Clone(bound = "G: Group, W: PedersenWindow, ConstraintF: Field"))] pub struct PedersenCommitmentGadgetParameters<G: Group, W: PedersenWindow, ConstraintF: Field> { - params: PedersenParameters<G>, + params: PedersenParameters<G>, #[doc(hidden)] - _group: PhantomData<G>, + _group: PhantomData<G>, #[doc(hidden)] _engine: PhantomData<ConstraintF>, #[doc(hidden)]
@@ -122,8 +122,10 @@ assert_eq!(generators.len(), W::WINDOW_SIZE);
} assert_eq!(CHUNK_SIZE, 3); - // Compute sum of h_i^{sum of (1-2*c_{i,j,2})*(1+c_{i,j,0}+2*c_{i,j,1})*2^{4*(j-1)} for all j in segment} for all i. - // Described in section 5.4.1.7 in the Zcash protocol specification. + // Compute sum of h_i^{sum of + // (1-2*c_{i,j,2})*(1+c_{i,j,0}+2*c_{i,j,1})*2^{4*(j-1)} for all j in segment} + // for all i. Described in section 5.4.1.7 in the Zcash protocol + // specification. let result = padded_input .par_chunks(W::WINDOW_SIZE * CHUNK_SIZE) .zip(¶meters.generators)
@@ -26,11 +26,11 @@ }
pub struct PedersenCRHGadget<G: Group, ConstraintF: Field, GG: GroupGadget<G, ConstraintF>> { #[doc(hideen)] - _group: PhantomData<*const G>, + _group: PhantomData<*const G>, #[doc(hideen)] _group_gadget: PhantomData<*const GG>, #[doc(hideen)] - _engine: PhantomData<ConstraintF>, + _engine: PhantomData<ConstraintF>, } impl<ConstraintF, G, GG, W> FixedLengthCRHGadget<PedersenCRH<G, W>, ConstraintF>
@@ -200,7 +200,7 @@ let empty_hash = hash_empty::<P::H>(&self.parameters)?;
// Check that the given index corresponds to the correct leaf. if leaf_hash != self.tree[tree_index] { - return Err(MerkleTreeError::IncorrectLeafIndex(tree_index).into()) + return Err(MerkleTreeError::IncorrectLeafIndex(tree_index).into()); } // Iterate from the leaf up to the root, storing all intermediate hash values.@@ -230,7 +230,7 @@ }
} end_timer!(prove_time); if path.len() != (Self::HEIGHT - 1) as usize { - return Err(MerkleTreeError::IncorrectPathLength(path.len()).into()) + return Err(MerkleTreeError::IncorrectPathLength(path.len()).into()); } else { Ok(MerkleTreePath { path }) }@@ -371,8 +371,7 @@ use crate::{
crh::{pedersen::*, *}, merkle_tree::*, }; - use algebra::curves::jubjub::JubJubAffine as JubJub; - use algebra::Zero; + use algebra::{curves::jubjub::JubJubAffine as JubJub, Zero}; use rand::SeedableRng; use rand_xorshift::XorShiftRng;
@@ -23,9 +23,9 @@ C: ConstraintSynthesizer<E::Fr>,
V: ToConstraintField<E::Fr> + ?Sized, > { #[doc(hidden)] - _engine: PhantomData<E>, + _engine: PhantomData<E>, #[doc(hidden)] - _circuit: PhantomData<C>, + _circuit: PhantomData<C>, #[doc(hidden)] _verifier_input: PhantomData<V>, }
@@ -23,9 +23,9 @@ C: ConstraintSynthesizer<E::Fr>,
V: ToConstraintField<E::Fr> + ?Sized, > { #[doc(hidden)] - _engine: PhantomData<E>, + _engine: PhantomData<E>, #[doc(hidden)] - _circuit: PhantomData<C>, + _circuit: PhantomData<C>, #[doc(hidden)] _verifier_input: PhantomData<V>, }
@@ -59,8 +59,7 @@
#[test] fn test_gm17() { use crate::nizk::{gm17::Gm17, NIZK}; - use algebra::{curves::bls12_381::Bls12_381, fields::bls12_381::Fr}; - use algebra::One; + use algebra::{curves::bls12_381::Bls12_381, fields::bls12_381::Fr, One}; use r1cs_core::{ConstraintSynthesizer, ConstraintSystem, SynthesisError}; #[derive(Copy, Clone)]
@@ -38,18 +38,18 @@ )]
pub struct SchnorrSigGadgetPk<G: Group, ConstraintF: Field, GG: GroupGadget<G, ConstraintF>> { pub_key: GG, #[doc(hidden)] - _group: PhantomData<*const G>, + _group: PhantomData<*const G>, #[doc(hidden)] _engine: PhantomData<*const ConstraintF>, } pub struct SchnorrRandomizePkGadget<G: Group, ConstraintF: Field, GG: GroupGadget<G, ConstraintF>> { #[doc(hidden)] - _group: PhantomData<*const G>, + _group: PhantomData<*const G>, #[doc(hidden)] _group_gadget: PhantomData<*const GG>, #[doc(hidden)] - _engine: PhantomData<*const ConstraintF>, + _engine: PhantomData<*const ConstraintF>, } impl<G, GG, D, ConstraintF> SigRandomizePkGadget<SchnorrSignature<G, D>, ConstraintF>
@@ -3,10 +3,9 @@ use algebra::{
bytes::ToBytes, fields::{Field, PrimeField}, groups::Group, - to_bytes, ToConstraintField, UniformRand, + to_bytes, One, ToConstraintField, UniformRand, Zero, }; use digest::Digest; -use algebra::{One, Zero}; use rand::Rng; use std::{ hash::Hash,
@@ -1,7 +1,7 @@
use algebra::{BitIterator, Field, FpParameters, PrimeField}; use crate::{prelude::*, Assignment}; -use r1cs_core::{ConstraintSystem, LinearCombination, SynthesisError, Variable, ConstraintVar}; +use r1cs_core::{ConstraintSystem, ConstraintVar, LinearCombination, SynthesisError, Variable}; use std::borrow::Borrow; /// Represents a variable in the constraint system which is guaranteed@@ -336,11 +336,14 @@ second: (Option<bool>, impl Into<ConstraintVar<F>>),
) -> Result<AllocatedBit, SynthesisError> { let mut result_val = None; let result_var = cs.alloc( - || "cond_select_result", + || "cond_select_result", || { - result_val = cond.get_value().and_then(|c| if c { first.0 } else { second.0 }); + result_val = cond + .get_value() + .and_then(|c| if c { first.0 } else { second.0 }); result_val.get().map(|v| F::from(v as u8)) - })?; + }, + )?; let first_var = first.1.into(); let second_var = second.1.into();@@ -358,8 +361,10 @@ |lc| (&first_var - &second_var) + lc,
|lc| ConstraintVar::from(result_var) - &second_var + lc, ); - Ok(AllocatedBit { value: result_val, variable: result_var }) - + Ok(AllocatedBit { + value: result_val, + variable: result_var, + }) } /// This is a boolean value which may be either a constant or@@ -800,31 +805,23 @@ match cond {
Boolean::Constant(true) => Ok(first.clone()), Boolean::Constant(false) => Ok(second.clone()), cond @ Boolean::Not(_) => Self::conditionally_select(cs, &cond.not(), second, first), - cond @ Boolean::Is(_) => { - match (first, second) { - (x, &Boolean::Constant(false)) => { - Boolean::and(cs.ns(|| "and"), cond, x).into() - }, - (&Boolean::Constant(false), x) => { - Boolean::and(cs.ns(|| "and"), &cond.not(), x) - }, - (&Boolean::Constant(true), x) => { - Boolean::or(cs.ns(|| "or"), cond, x).into() - }, - (x, &Boolean::Constant(true)) => { - Boolean::or(cs.ns(|| "or"), &cond.not(), x) - }, - (a @ Boolean::Is(_), b @ Boolean::Is(_)) - | (a @ Boolean::Not(_), b @ Boolean::Not(_)) - | (a @ Boolean::Is(_), b @ Boolean::Not(_)) - | (a @ Boolean::Not(_), b @ Boolean::Is(_)) => { - let a_lc = a.lc(CS::one(), ConstraintF::one()); - let b_lc = b.lc(CS::one(), ConstraintF::one()); - Ok(cond_select_helper(cs, cond, (a.get_value(), a_lc), (b.get_value(), b_lc))?.into()) - }, - } - - } + cond @ Boolean::Is(_) => match (first, second) { + (x, &Boolean::Constant(false)) => Boolean::and(cs.ns(|| "and"), cond, x).into(), + (&Boolean::Constant(false), x) => Boolean::and(cs.ns(|| "and"), &cond.not(), x), + (&Boolean::Constant(true), x) => Boolean::or(cs.ns(|| "or"), cond, x).into(), + (x, &Boolean::Constant(true)) => Boolean::or(cs.ns(|| "or"), &cond.not(), x), + (a @ Boolean::Is(_), b @ Boolean::Is(_)) + | (a @ Boolean::Not(_), b @ Boolean::Not(_)) + | (a @ Boolean::Is(_), b @ Boolean::Not(_)) + | (a @ Boolean::Not(_), b @ Boolean::Is(_)) => { + let a_lc = a.lc(CS::one(), ConstraintF::one()); + let b_lc = b.lc(CS::one(), ConstraintF::one()); + Ok( + cond_select_helper(cs, cond, (a.get_value(), a_lc), (b.get_value(), b_lc))? + .into(), + ) + }, + }, } }@@ -837,8 +834,7 @@ #[cfg(test)]
mod test { use super::{AllocatedBit, Boolean}; use crate::{prelude::*, test_constraint_system::TestConstraintSystem}; - use algebra::{fields::bls12_381::Fr, BitIterator, Field, PrimeField, UniformRand}; - use algebra::{One, Zero}; + use algebra::{fields::bls12_381::Fr, BitIterator, Field, One, PrimeField, UniformRand, Zero}; use r1cs_core::ConstraintSystem; use rand::SeedableRng; use rand_xorshift::XorShiftRng;@@ -1408,10 +1404,12 @@ OperandType::AllocatedFalse => {
Boolean::from(AllocatedBit::alloc(cs, || Ok(false)).unwrap()) }, OperandType::NegatedAllocatedTrue => { - Boolean::from(AllocatedBit::alloc(cs, || Ok(true)).unwrap()).not() + Boolean::from(AllocatedBit::alloc(cs, || Ok(true)).unwrap()) + .not() }, OperandType::NegatedAllocatedFalse => { - Boolean::from(AllocatedBit::alloc(cs, || Ok(false)).unwrap()).not() + Boolean::from(AllocatedBit::alloc(cs, || Ok(false)).unwrap()) + .not() }, } };@@ -1432,7 +1430,14 @@ condition,
first_operand, second_operand, ); - assert_eq!(c.get_value(), if cond.get_value().unwrap() { a.get_value() } else { b.get_value() }); + assert_eq!( + c.get_value(), + if cond.get_value().unwrap() { + a.get_value() + } else { + b.get_value() + } + ); assert!(<Boolean as CondSelectGadget<Fr>>::cost() >= after - before); } }
@@ -344,8 +344,7 @@ #[cfg(test)]
mod test { use super::UInt32; use crate::{bits::boolean::Boolean, test_constraint_system::TestConstraintSystem}; - use algebra::fields::bls12_381::Fr; - use algebra::{One, Zero}; + use algebra::{fields::bls12_381::Fr, One, Zero}; use r1cs_core::ConstraintSystem; use rand::{Rng, SeedableRng}; use rand_xorshift::XorShiftRng;
@@ -6,9 +6,8 @@ fp12_2over3over2::{Fp12, Fp12Parameters},
fp6_3over2::{Fp6, Fp6Parameters}, Fp2Parameters, }, - BitIterator, Field, PrimeField, + BitIterator, Field, One, PrimeField, }; -use algebra::One; use std::{borrow::Borrow, marker::PhantomData}; use crate::{prelude::*, Assignment};@@ -32,8 +31,8 @@ where
P: Fp12Parameters, <P::Fp6Params as Fp6Parameters>::Fp2Params: Fp2Parameters<Fp = ConstraintF>, { - pub c0: Fp6Gadget<P, ConstraintF>, - pub c1: Fp6Gadget<P, ConstraintF>, + pub c0: Fp6Gadget<P, ConstraintF>, + pub c1: Fp6Gadget<P, ConstraintF>, #[derivative(Debug = "ignore")] _params: PhantomData<P>, }
@@ -11,8 +11,8 @@ #[derive(Derivative)]
#[derivative(Debug(bound = "P: Fp2Parameters, ConstraintF: PrimeField"))] #[must_use] pub struct Fp2Gadget<P: Fp2Parameters<Fp = ConstraintF>, ConstraintF: PrimeField> { - pub c0: FpGadget<ConstraintF>, - pub c1: FpGadget<ConstraintF>, + pub c0: FpGadget<ConstraintF>, + pub c1: FpGadget<ConstraintF>, #[derivative(Debug = "ignore")] _params: PhantomData<P>, }@@ -75,10 +75,7 @@ }
#[inline] fn get_variable(&self) -> Self::Variable { - ( - self.c0.get_variable(), - self.c1.get_variable(), - ) + (self.c0.get_variable(), self.c1.get_variable()) } #[inline]
@@ -21,9 +21,9 @@ where
P: Fp6Parameters, P::Fp2Params: Fp2Parameters<Fp = ConstraintF>, { - pub c0: Fp2Gadget<P, ConstraintF>, - pub c1: Fp2Gadget<P, ConstraintF>, - pub c2: Fp2Gadget<P, ConstraintF>, + pub c0: Fp2Gadget<P, ConstraintF>, + pub c1: Fp2Gadget<P, ConstraintF>, + pub c2: Fp2Gadget<P, ConstraintF>, #[derivative(Debug = "ignore")] _params: PhantomData<P>, }
@@ -1,9 +1,8 @@
use algebra::{ curves::bls12::{Bls12Parameters, G1Prepared, TwistType}, fields::Field, - BitIterator, ProjectiveCurve, + BitIterator, One, ProjectiveCurve, }; -use algebra::One; use r1cs_core::{ConstraintSystem, SynthesisError}; use crate::{
@@ -3,9 +3,8 @@ curves::{
short_weierstrass_jacobian::{GroupAffine as SWAffine, GroupProjective as SWProjective}, SWModelParameters, }, - AffineCurve, BitIterator, Field, PrimeField, ProjectiveCurve, + AffineCurve, BitIterator, Field, One, PrimeField, ProjectiveCurve, Zero, }; -use algebra::{One, Zero}; use r1cs_core::{ConstraintSystem, SynthesisError}; use std::{borrow::Borrow, marker::PhantomData, ops::Neg};@@ -21,11 +20,11 @@ P: SWModelParameters,
ConstraintF: Field, F: FieldGadget<P::BaseField, ConstraintF>, > { - pub x: F, - pub y: F, + pub x: F, + pub y: F, pub infinity: Boolean, - _params: PhantomData<P>, - _engine: PhantomData<ConstraintF>, + _params: PhantomData<P>, + _engine: PhantomData<ConstraintF>, } impl<P: SWModelParameters, ConstraintF: Field, F: FieldGadget<P::BaseField, ConstraintF>>@@ -99,7 +98,11 @@ type Variable = (F::Variable, F::Variable);
#[inline] fn get_value(&self) -> Option<Self::Value> { - match (self.x.get_value(), self.y.get_value(), self.infinity.get_value()) { + match ( + self.x.get_value(), + self.y.get_value(), + self.infinity.get_value(), + ) { (Some(x), Some(y), Some(infinity)) => { Some(SWAffine::new(x, y, infinity).into_projective()) },@@ -341,14 +344,19 @@ second: &Self,
) -> Result<Self, SynthesisError> { let x = F::conditionally_select(&mut cs.ns(|| "x"), cond, &first.x, &second.x)?; let y = F::conditionally_select(&mut cs.ns(|| "y"), cond, &first.y, &second.y)?; - let infinity = Boolean::conditionally_select(&mut cs.ns(|| "infinity"), cond, &first.infinity, &second.infinity)?; + let infinity = Boolean::conditionally_select( + &mut cs.ns(|| "infinity"), + cond, + &first.infinity, + &second.infinity, + )?; Ok(Self::new(x, y, infinity)) } fn cost() -> usize { - 2 * <F as CondSelectGadget<ConstraintF>>::cost() + - <Boolean as CondSelectGadget<ConstraintF>>::cost() + 2 * <F as CondSelectGadget<ConstraintF>>::cost() + + <Boolean as CondSelectGadget<ConstraintF>>::cost() } }@@ -558,7 +566,7 @@ where
FN: FnOnce() -> Result<T, SynthesisError>, T: Borrow<SWProjective<P>>, { - // When allocating the input we assume that the verifier has performed + // When allocating the input we assume that the verifier has performed // any on curve checks already. let (x, y, infinity) = match value_gen() { Ok(ge) => {
@@ -3,9 +3,8 @@ curves::{
twisted_edwards_extended::GroupAffine as TEAffine, MontgomeryModelParameters, TEModelParameters, }, - BitIterator, Field, + BitIterator, Field, One, Zero, }; -use algebra::{One, Zero}; use r1cs_core::{ConstraintSystem, SynthesisError};@@ -28,8 +27,8 @@ P: TEModelParameters,
ConstraintF: Field, F: FieldGadget<P::BaseField, ConstraintF>, > { - pub x: F, - pub y: F, + pub x: F, + pub y: F, #[derivative(Debug = "ignore")] _params: PhantomData<P>, #[derivative(Debug = "ignore")]@@ -62,8 +61,8 @@ GroupAffine::zero()
} else if p.x == P::BaseField::zero() { GroupAffine::new(P::BaseField::zero(), P::BaseField::zero()) } else { - let u = (P::BaseField::one() + &p.y) - * &(P::BaseField::one() - &p.y).inverse().unwrap(); + let u = + (P::BaseField::one() + &p.y) * &(P::BaseField::one() - &p.y).inverse().unwrap(); let v = u * &p.x.inverse().unwrap(); GroupAffine::new(u, v) };@@ -200,8 +199,8 @@ P: TEModelParameters,
ConstraintF: Field, F: FieldGadget<P::BaseField, ConstraintF>, > { - pub x: F, - pub y: F, + pub x: F, + pub y: F, #[derivative(Debug = "ignore")] _params: PhantomData<P>, #[derivative(Debug = "ignore")]
@@ -71,6 +71,6 @@ }
impl<T> Assignment<T> for Option<T> { fn get(self) -> Result<T, r1cs_core::SynthesisError> { - self.ok_or_else(|| { r1cs_core::SynthesisError::AssignmentMissing }) + self.ok_or_else(|| r1cs_core::SynthesisError::AssignmentMissing) } }
@@ -59,8 +59,7 @@ #[cfg(test)]
mod test { // use rand; use crate::test_constraint_system::TestConstraintSystem; - use algebra::{BitIterator, Field}; - use algebra::One; + use algebra::{BitIterator, Field, One}; use r1cs_core::ConstraintSystem; #[test]@@ -107,23 +106,17 @@ let sa_prep_g = G1PreparedGadget::from_affine(&mut cs.ns(|| "sa_prep"), &sa_g).unwrap();
let sb_prep_g = G2PreparedGadget::from_affine(&mut cs.ns(|| "sb_prep"), &sb_g).unwrap(); let (ans1_g, ans1_n) = { - let ans_g = PairingGadget::pairing( - cs.ns(|| "pair(sa, b)"), - sa_prep_g, - b_prep_g.clone(), - ) - .unwrap(); + let ans_g = + PairingGadget::pairing(cs.ns(|| "pair(sa, b)"), sa_prep_g, b_prep_g.clone()) + .unwrap(); let ans_n = Bls12_377::pairing(sa, b); (ans_g, ans_n) }; let (ans2_g, ans2_n) = { - let ans_g = PairingGadget::pairing( - cs.ns(|| "pair(a, sb)"), - a_prep_g.clone(), - sb_prep_g, - ) - .unwrap(); + let ans_g = + PairingGadget::pairing(cs.ns(|| "pair(a, sb)"), a_prep_g.clone(), sb_prep_g) + .unwrap(); let ans_n = Bls12_377::pairing(a, sb); (ans_g, ans_n) };@@ -134,8 +127,7 @@ .map(Boolean::constant)
.collect::<Vec<_>>(); let mut ans_g = - PairingGadget::pairing(cs.ns(|| "pair(a, b)"), a_prep_g, b_prep_g) - .unwrap(); + PairingGadget::pairing(cs.ns(|| "pair(a, b)"), a_prep_g, b_prep_g).unwrap(); let mut ans_n = Bls12_377::pairing(a, b); ans_n = ans_n.pow(s.into_repr()); ans_g = ans_g.pow(cs.ns(|| "pow"), &s_iter).unwrap();