diff --git a/crypto-primitives/src/commitment/pedersen/constraints.rs b/crypto-primitives/src/commitment/pedersen/constraints.rs index 6d7219c..55f17cf 100644 --- a/crypto-primitives/src/commitment/pedersen/constraints.rs +++ b/crypto-primitives/src/commitment/pedersen/constraints.rs @@ -13,9 +13,9 @@ use std::{borrow::Borrow, marker::PhantomData}; #[derive(Derivative)] #[derivative(Clone(bound = "G: Group, W: PedersenWindow, ConstraintF: Field"))] pub struct PedersenCommitmentGadgetParameters { - params: PedersenParameters, + params: PedersenParameters, #[doc(hidden)] - _group: PhantomData, + _group: PhantomData, #[doc(hidden)] _engine: PhantomData, #[doc(hidden)] diff --git a/crypto-primitives/src/crh/bowe_hopwood/mod.rs b/crypto-primitives/src/crh/bowe_hopwood/mod.rs index 95e04e5..f2addcd 100644 --- a/crypto-primitives/src/crh/bowe_hopwood/mod.rs +++ b/crypto-primitives/src/crh/bowe_hopwood/mod.rs @@ -122,8 +122,10 @@ impl FixedLengthCRH for BoweHopwoodPedersenCRH> { #[doc(hideen)] - _group: PhantomData<*const G>, + _group: PhantomData<*const G>, #[doc(hideen)] _group_gadget: PhantomData<*const GG>, #[doc(hideen)] - _engine: PhantomData, + _engine: PhantomData, } impl FixedLengthCRHGadget, ConstraintF> diff --git a/crypto-primitives/src/merkle_tree/mod.rs b/crypto-primitives/src/merkle_tree/mod.rs index 27b248e..90c22f0 100644 --- a/crypto-primitives/src/merkle_tree/mod.rs +++ b/crypto-primitives/src/merkle_tree/mod.rs @@ -200,7 +200,7 @@ impl MerkleHashTree

{ // 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 @@ impl MerkleHashTree

{ } 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 @@ mod test { 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; diff --git a/crypto-primitives/src/nizk/gm17/mod.rs b/crypto-primitives/src/nizk/gm17/mod.rs index 26790d0..881cb24 100644 --- a/crypto-primitives/src/nizk/gm17/mod.rs +++ b/crypto-primitives/src/nizk/gm17/mod.rs @@ -23,9 +23,9 @@ pub struct Gm17< V: ToConstraintField + ?Sized, > { #[doc(hidden)] - _engine: PhantomData, + _engine: PhantomData, #[doc(hidden)] - _circuit: PhantomData, + _circuit: PhantomData, #[doc(hidden)] _verifier_input: PhantomData, } diff --git a/crypto-primitives/src/nizk/groth16/mod.rs b/crypto-primitives/src/nizk/groth16/mod.rs index cec4ca2..8e38a87 100644 --- a/crypto-primitives/src/nizk/groth16/mod.rs +++ b/crypto-primitives/src/nizk/groth16/mod.rs @@ -23,9 +23,9 @@ pub struct Groth16< V: ToConstraintField + ?Sized, > { #[doc(hidden)] - _engine: PhantomData, + _engine: PhantomData, #[doc(hidden)] - _circuit: PhantomData, + _circuit: PhantomData, #[doc(hidden)] _verifier_input: PhantomData, } diff --git a/crypto-primitives/src/nizk/mod.rs b/crypto-primitives/src/nizk/mod.rs index fc40af5..1e8cae4 100644 --- a/crypto-primitives/src/nizk/mod.rs +++ b/crypto-primitives/src/nizk/mod.rs @@ -59,8 +59,7 @@ mod test { #[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)] diff --git a/crypto-primitives/src/signature/schnorr/constraints.rs b/crypto-primitives/src/signature/schnorr/constraints.rs index 59830f9..e8d31f2 100644 --- a/crypto-primitives/src/signature/schnorr/constraints.rs +++ b/crypto-primitives/src/signature/schnorr/constraints.rs @@ -38,18 +38,18 @@ impl> Clone pub struct SchnorrSigGadgetPk> { pub_key: GG, #[doc(hidden)] - _group: PhantomData<*const G>, + _group: PhantomData<*const G>, #[doc(hidden)] _engine: PhantomData<*const ConstraintF>, } pub struct SchnorrRandomizePkGadget> { #[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 SigRandomizePkGadget, ConstraintF> diff --git a/crypto-primitives/src/signature/schnorr/mod.rs b/crypto-primitives/src/signature/schnorr/mod.rs index 2356f20..8f45692 100644 --- a/crypto-primitives/src/signature/schnorr/mod.rs +++ b/crypto-primitives/src/signature/schnorr/mod.rs @@ -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, diff --git a/r1cs-std/src/bits/boolean.rs b/r1cs-std/src/bits/boolean.rs index 37613f1..3a8a820 100644 --- a/r1cs-std/src/bits/boolean.rs +++ b/r1cs-std/src/bits/boolean.rs @@ -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 @@ fn cond_select_helper>( ) -> Result { 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 @@ fn cond_select_helper>( |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 @@ impl CondSelectGadget for Boolean { 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 @@ impl CondSelectGadget for Boolean { 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 @@ mod test { 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 @@ mod test { 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!(>::cost() >= after - before); } } diff --git a/r1cs-std/src/bits/uint32.rs b/r1cs-std/src/bits/uint32.rs index 719352f..4f1fb2c 100644 --- a/r1cs-std/src/bits/uint32.rs +++ b/r1cs-std/src/bits/uint32.rs @@ -344,8 +344,7 @@ impl ConditionalEqGadget for UInt32 { 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; diff --git a/r1cs-std/src/fields/fp12.rs b/r1cs-std/src/fields/fp12.rs index 7f8cf80..ffe89a6 100644 --- a/r1cs-std/src/fields/fp12.rs +++ b/r1cs-std/src/fields/fp12.rs @@ -6,9 +6,8 @@ use algebra::{ 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, ::Fp2Params: Fp2Parameters, { - pub c0: Fp6Gadget, - pub c1: Fp6Gadget, + pub c0: Fp6Gadget, + pub c1: Fp6Gadget, #[derivative(Debug = "ignore")] _params: PhantomData

, } diff --git a/r1cs-std/src/fields/fp2.rs b/r1cs-std/src/fields/fp2.rs index a5154d7..4b4cb15 100644 --- a/r1cs-std/src/fields/fp2.rs +++ b/r1cs-std/src/fields/fp2.rs @@ -11,8 +11,8 @@ use crate::{fields::fp::FpGadget, prelude::*, Assignment}; #[derivative(Debug(bound = "P: Fp2Parameters, ConstraintF: PrimeField"))] #[must_use] pub struct Fp2Gadget, ConstraintF: PrimeField> { - pub c0: FpGadget, - pub c1: FpGadget, + pub c0: FpGadget, + pub c1: FpGadget, #[derivative(Debug = "ignore")] _params: PhantomData

, } @@ -75,10 +75,7 @@ impl, ConstraintF: PrimeField> FieldGadget Self::Variable { - ( - self.c0.get_variable(), - self.c1.get_variable(), - ) + (self.c0.get_variable(), self.c1.get_variable()) } #[inline] diff --git a/r1cs-std/src/fields/fp6_3over2.rs b/r1cs-std/src/fields/fp6_3over2.rs index 722a806..3a6f93d 100644 --- a/r1cs-std/src/fields/fp6_3over2.rs +++ b/r1cs-std/src/fields/fp6_3over2.rs @@ -21,9 +21,9 @@ where P: Fp6Parameters, P::Fp2Params: Fp2Parameters, { - pub c0: Fp2Gadget, - pub c1: Fp2Gadget, - pub c2: Fp2Gadget, + pub c0: Fp2Gadget, + pub c1: Fp2Gadget, + pub c2: Fp2Gadget, #[derivative(Debug = "ignore")] _params: PhantomData

, } diff --git a/r1cs-std/src/groups/curves/short_weierstrass/bls12/mod.rs b/r1cs-std/src/groups/curves/short_weierstrass/bls12/mod.rs index 6c7d383..eb7b184 100644 --- a/r1cs-std/src/groups/curves/short_weierstrass/bls12/mod.rs +++ b/r1cs-std/src/groups/curves/short_weierstrass/bls12/mod.rs @@ -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::{ diff --git a/r1cs-std/src/groups/curves/short_weierstrass/mod.rs b/r1cs-std/src/groups/curves/short_weierstrass/mod.rs index 8653bf3..69163a9 100644 --- a/r1cs-std/src/groups/curves/short_weierstrass/mod.rs +++ b/r1cs-std/src/groups/curves/short_weierstrass/mod.rs @@ -3,9 +3,8 @@ use algebra::{ 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 @@ pub struct AffineGadget< ConstraintF: Field, F: FieldGadget, > { - pub x: F, - pub y: F, + pub x: F, + pub y: F, pub infinity: Boolean, - _params: PhantomData

, - _engine: PhantomData, + _params: PhantomData

, + _engine: PhantomData, } impl> @@ -99,7 +98,11 @@ where #[inline] fn get_value(&self) -> Option { - 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 @@ where ) -> Result { 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 * >::cost() + - >::cost() + 2 * >::cost() + + >::cost() } } @@ -558,7 +566,7 @@ where FN: FnOnce() -> Result, T: Borrow>, { - // 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) => { diff --git a/r1cs-std/src/groups/curves/twisted_edwards/mod.rs b/r1cs-std/src/groups/curves/twisted_edwards/mod.rs index d5c3751..0a495d3 100644 --- a/r1cs-std/src/groups/curves/twisted_edwards/mod.rs +++ b/r1cs-std/src/groups/curves/twisted_edwards/mod.rs @@ -3,9 +3,8 @@ use algebra::{ 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 @@ pub struct MontgomeryAffineGadget< ConstraintF: Field, F: FieldGadget, > { - pub x: F, - pub y: F, + pub x: F, + pub y: F, #[derivative(Debug = "ignore")] _params: PhantomData

, #[derivative(Debug = "ignore")] @@ -62,8 +61,8 @@ mod montgomery_affine_impl { } 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 @@ pub struct AffineGadget< ConstraintF: Field, F: FieldGadget, > { - pub x: F, - pub y: F, + pub x: F, + pub y: F, #[derivative(Debug = "ignore")] _params: PhantomData

, #[derivative(Debug = "ignore")] diff --git a/r1cs-std/src/lib.rs b/r1cs-std/src/lib.rs index 775aca0..8a159bd 100644 --- a/r1cs-std/src/lib.rs +++ b/r1cs-std/src/lib.rs @@ -71,6 +71,6 @@ pub trait Assignment { impl Assignment for Option { fn get(self) -> Result { - self.ok_or_else(|| { r1cs_core::SynthesisError::AssignmentMissing }) + self.ok_or_else(|| r1cs_core::SynthesisError::AssignmentMissing) } } diff --git a/r1cs-std/src/pairing/mod.rs b/r1cs-std/src/pairing/mod.rs index 48d943d..5ff8473 100644 --- a/r1cs-std/src/pairing/mod.rs +++ b/r1cs-std/src/pairing/mod.rs @@ -59,8 +59,7 @@ pub trait PairingGadget { 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 @@ mod test { 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 @@ mod test { .collect::>(); 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();