Browse Source

Runs rustfmt on the repo

master
François Garillot 4 years ago
committed by Pratyush Mishra
parent
commit
b26867f267
19 changed files with 107 additions and 110 deletions
  1. +2
    -2
      crypto-primitives/src/commitment/pedersen/constraints.rs
  2. +4
    -2
      crypto-primitives/src/crh/bowe_hopwood/mod.rs
  3. +2
    -2
      crypto-primitives/src/crh/pedersen/constraints.rs
  4. +3
    -4
      crypto-primitives/src/merkle_tree/mod.rs
  5. +2
    -2
      crypto-primitives/src/nizk/gm17/mod.rs
  6. +2
    -2
      crypto-primitives/src/nizk/groth16/mod.rs
  7. +1
    -2
      crypto-primitives/src/nizk/mod.rs
  8. +3
    -3
      crypto-primitives/src/signature/schnorr/constraints.rs
  9. +1
    -2
      crypto-primitives/src/signature/schnorr/mod.rs
  10. +41
    -36
      r1cs-std/src/bits/boolean.rs
  11. +1
    -2
      r1cs-std/src/bits/uint32.rs
  12. +3
    -4
      r1cs-std/src/fields/fp12.rs
  13. +3
    -6
      r1cs-std/src/fields/fp2.rs
  14. +3
    -3
      r1cs-std/src/fields/fp6_3over2.rs
  15. +1
    -2
      r1cs-std/src/groups/curves/short_weierstrass/bls12/mod.rs
  16. +19
    -11
      r1cs-std/src/groups/curves/short_weierstrass/mod.rs
  17. +7
    -8
      r1cs-std/src/groups/curves/twisted_edwards/mod.rs
  18. +1
    -1
      r1cs-std/src/lib.rs
  19. +8
    -16
      r1cs-std/src/pairing/mod.rs

+ 2
- 2
crypto-primitives/src/commitment/pedersen/constraints.rs

@ -13,9 +13,9 @@ use std::{borrow::Borrow, marker::PhantomData};
#[derive(Derivative)] #[derive(Derivative)]
#[derivative(Clone(bound = "G: Group, W: PedersenWindow, ConstraintF: Field"))] #[derivative(Clone(bound = "G: Group, W: PedersenWindow, ConstraintF: Field"))]
pub struct PedersenCommitmentGadgetParameters<G: Group, W: PedersenWindow, ConstraintF: Field> { pub struct PedersenCommitmentGadgetParameters<G: Group, W: PedersenWindow, ConstraintF: Field> {
params: PedersenParameters<G>,
params: PedersenParameters<G>,
#[doc(hidden)] #[doc(hidden)]
_group: PhantomData<G>,
_group: PhantomData<G>,
#[doc(hidden)] #[doc(hidden)]
_engine: PhantomData<ConstraintF>, _engine: PhantomData<ConstraintF>,
#[doc(hidden)] #[doc(hidden)]

+ 4
- 2
crypto-primitives/src/crh/bowe_hopwood/mod.rs

@ -122,8 +122,10 @@ impl FixedLengthCRH for BoweHopwoodPedersenCRH
} }
assert_eq!(CHUNK_SIZE, 3); 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 let result = padded_input
.par_chunks(W::WINDOW_SIZE * CHUNK_SIZE) .par_chunks(W::WINDOW_SIZE * CHUNK_SIZE)
.zip(&parameters.generators) .zip(&parameters.generators)

+ 2
- 2
crypto-primitives/src/crh/pedersen/constraints.rs

@ -26,11 +26,11 @@ pub struct PedersenCRHGadgetParameters<
pub struct PedersenCRHGadget<G: Group, ConstraintF: Field, GG: GroupGadget<G, ConstraintF>> { pub struct PedersenCRHGadget<G: Group, ConstraintF: Field, GG: GroupGadget<G, ConstraintF>> {
#[doc(hideen)] #[doc(hideen)]
_group: PhantomData<*const G>,
_group: PhantomData<*const G>,
#[doc(hideen)] #[doc(hideen)]
_group_gadget: PhantomData<*const GG>, _group_gadget: PhantomData<*const GG>,
#[doc(hideen)] #[doc(hideen)]
_engine: PhantomData<ConstraintF>,
_engine: PhantomData<ConstraintF>,
} }
impl<ConstraintF, G, GG, W> FixedLengthCRHGadget<PedersenCRH<G, W>, ConstraintF> impl<ConstraintF, G, GG, W> FixedLengthCRHGadget<PedersenCRH<G, W>, ConstraintF>

+ 3
- 4
crypto-primitives/src/merkle_tree/mod.rs

@ -200,7 +200,7 @@ impl MerkleHashTree

{

// Check that the given index corresponds to the correct leaf. // Check that the given index corresponds to the correct leaf.
if leaf_hash != self.tree[tree_index] { 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. // Iterate from the leaf up to the root, storing all intermediate hash values.
@ -230,7 +230,7 @@ impl MerkleHashTree

{

} }
end_timer!(prove_time); end_timer!(prove_time);
if path.len() != (Self::HEIGHT - 1) as usize { if path.len() != (Self::HEIGHT - 1) as usize {
return Err(MerkleTreeError::IncorrectPathLength(path.len()).into())
return Err(MerkleTreeError::IncorrectPathLength(path.len()).into());
} else { } else {
Ok(MerkleTreePath { path }) Ok(MerkleTreePath { path })
} }
@ -371,8 +371,7 @@ mod test {
crh::{pedersen::*, *}, crh::{pedersen::*, *},
merkle_tree::*, 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::SeedableRng;
use rand_xorshift::XorShiftRng; use rand_xorshift::XorShiftRng;

+ 2
- 2
crypto-primitives/src/nizk/gm17/mod.rs

@ -23,9 +23,9 @@ pub struct Gm17<
V: ToConstraintField<E::Fr> + ?Sized, V: ToConstraintField<E::Fr> + ?Sized,
> { > {
#[doc(hidden)] #[doc(hidden)]
_engine: PhantomData<E>,
_engine: PhantomData<E>,
#[doc(hidden)] #[doc(hidden)]
_circuit: PhantomData<C>,
_circuit: PhantomData<C>,
#[doc(hidden)] #[doc(hidden)]
_verifier_input: PhantomData<V>, _verifier_input: PhantomData<V>,
} }

+ 2
- 2
crypto-primitives/src/nizk/groth16/mod.rs

@ -23,9 +23,9 @@ pub struct Groth16<
V: ToConstraintField<E::Fr> + ?Sized, V: ToConstraintField<E::Fr> + ?Sized,
> { > {
#[doc(hidden)] #[doc(hidden)]
_engine: PhantomData<E>,
_engine: PhantomData<E>,
#[doc(hidden)] #[doc(hidden)]
_circuit: PhantomData<C>,
_circuit: PhantomData<C>,
#[doc(hidden)] #[doc(hidden)]
_verifier_input: PhantomData<V>, _verifier_input: PhantomData<V>,
} }

+ 1
- 2
crypto-primitives/src/nizk/mod.rs

@ -59,8 +59,7 @@ mod test {
#[test] #[test]
fn test_gm17() { fn test_gm17() {
use crate::nizk::{gm17::Gm17, NIZK}; 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}; use r1cs_core::{ConstraintSynthesizer, ConstraintSystem, SynthesisError};
#[derive(Copy, Clone)] #[derive(Copy, Clone)]

+ 3
- 3
crypto-primitives/src/signature/schnorr/constraints.rs

@ -38,18 +38,18 @@ impl> Clone
pub struct SchnorrSigGadgetPk<G: Group, ConstraintF: Field, GG: GroupGadget<G, ConstraintF>> { pub struct SchnorrSigGadgetPk<G: Group, ConstraintF: Field, GG: GroupGadget<G, ConstraintF>> {
pub_key: GG, pub_key: GG,
#[doc(hidden)] #[doc(hidden)]
_group: PhantomData<*const G>,
_group: PhantomData<*const G>,
#[doc(hidden)] #[doc(hidden)]
_engine: PhantomData<*const ConstraintF>, _engine: PhantomData<*const ConstraintF>,
} }
pub struct SchnorrRandomizePkGadget<G: Group, ConstraintF: Field, GG: GroupGadget<G, ConstraintF>> { pub struct SchnorrRandomizePkGadget<G: Group, ConstraintF: Field, GG: GroupGadget<G, ConstraintF>> {
#[doc(hidden)] #[doc(hidden)]
_group: PhantomData<*const G>,
_group: PhantomData<*const G>,
#[doc(hidden)] #[doc(hidden)]
_group_gadget: PhantomData<*const GG>, _group_gadget: PhantomData<*const GG>,
#[doc(hidden)] #[doc(hidden)]
_engine: PhantomData<*const ConstraintF>,
_engine: PhantomData<*const ConstraintF>,
} }
impl<G, GG, D, ConstraintF> SigRandomizePkGadget<SchnorrSignature<G, D>, ConstraintF> impl<G, GG, D, ConstraintF> SigRandomizePkGadget<SchnorrSignature<G, D>, ConstraintF>

+ 1
- 2
crypto-primitives/src/signature/schnorr/mod.rs

@ -3,10 +3,9 @@ use algebra::{
bytes::ToBytes, bytes::ToBytes,
fields::{Field, PrimeField}, fields::{Field, PrimeField},
groups::Group, groups::Group,
to_bytes, ToConstraintField, UniformRand,
to_bytes, One, ToConstraintField, UniformRand, Zero,
}; };
use digest::Digest; use digest::Digest;
use algebra::{One, Zero};
use rand::Rng; use rand::Rng;
use std::{ use std::{
hash::Hash, hash::Hash,

+ 41
- 36
r1cs-std/src/bits/boolean.rs

@ -1,7 +1,7 @@
use algebra::{BitIterator, Field, FpParameters, PrimeField}; use algebra::{BitIterator, Field, FpParameters, PrimeField};
use crate::{prelude::*, Assignment}; use crate::{prelude::*, Assignment};
use r1cs_core::{ConstraintSystem, LinearCombination, SynthesisError, Variable, ConstraintVar};
use r1cs_core::{ConstraintSystem, ConstraintVar, LinearCombination, SynthesisError, Variable};
use std::borrow::Borrow; use std::borrow::Borrow;
/// Represents a variable in the constraint system which is guaranteed /// Represents a variable in the constraint system which is guaranteed
@ -336,11 +336,14 @@ fn cond_select_helper>(
) -> Result<AllocatedBit, SynthesisError> { ) -> Result<AllocatedBit, SynthesisError> {
let mut result_val = None; let mut result_val = None;
let result_var = cs.alloc( 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)) result_val.get().map(|v| F::from(v as u8))
})?;
},
)?;
let first_var = first.1.into(); let first_var = first.1.into();
let second_var = second.1.into(); let second_var = second.1.into();
@ -358,8 +361,10 @@ fn cond_select_helper>(
|lc| ConstraintVar::from(result_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 /// 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(true) => Ok(first.clone()),
Boolean::Constant(false) => Ok(second.clone()), Boolean::Constant(false) => Ok(second.clone()),
cond @ Boolean::Not(_) => Self::conditionally_select(cs, &cond.not(), second, first), 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 { mod test {
use super::{AllocatedBit, Boolean}; use super::{AllocatedBit, Boolean};
use crate::{prelude::*, test_constraint_system::TestConstraintSystem}; 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 r1cs_core::ConstraintSystem;
use rand::SeedableRng; use rand::SeedableRng;
use rand_xorshift::XorShiftRng; use rand_xorshift::XorShiftRng;
@ -1408,10 +1404,12 @@ mod test {
Boolean::from(AllocatedBit::alloc(cs, || Ok(false)).unwrap()) Boolean::from(AllocatedBit::alloc(cs, || Ok(false)).unwrap())
}, },
OperandType::NegatedAllocatedTrue => { OperandType::NegatedAllocatedTrue => {
Boolean::from(AllocatedBit::alloc(cs, || Ok(true)).unwrap()).not()
Boolean::from(AllocatedBit::alloc(cs, || Ok(true)).unwrap())
.not()
}, },
OperandType::NegatedAllocatedFalse => { 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, first_operand,
second_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); assert!(<Boolean as CondSelectGadget<Fr>>::cost() >= after - before);
} }
} }

+ 1
- 2
r1cs-std/src/bits/uint32.rs

@ -344,8 +344,7 @@ impl ConditionalEqGadget for UInt32 {
mod test { mod test {
use super::UInt32; use super::UInt32;
use crate::{bits::boolean::Boolean, test_constraint_system::TestConstraintSystem}; 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 r1cs_core::ConstraintSystem;
use rand::{Rng, SeedableRng}; use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng; use rand_xorshift::XorShiftRng;

+ 3
- 4
r1cs-std/src/fields/fp12.rs

@ -6,9 +6,8 @@ use algebra::{
fp6_3over2::{Fp6, Fp6Parameters}, fp6_3over2::{Fp6, Fp6Parameters},
Fp2Parameters, Fp2Parameters,
}, },
BitIterator, Field, PrimeField,
BitIterator, Field, One, PrimeField,
}; };
use algebra::One;
use std::{borrow::Borrow, marker::PhantomData}; use std::{borrow::Borrow, marker::PhantomData};
use crate::{prelude::*, Assignment}; use crate::{prelude::*, Assignment};
@ -32,8 +31,8 @@ where
P: Fp12Parameters, P: Fp12Parameters,
<P::Fp6Params as Fp6Parameters>::Fp2Params: Fp2Parameters<Fp = ConstraintF>, <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")] #[derivative(Debug = "ignore")]
_params: PhantomData<P>, _params: PhantomData<P>,
} }

+ 3
- 6
r1cs-std/src/fields/fp2.rs

@ -11,8 +11,8 @@ use crate::{fields::fp::FpGadget, prelude::*, Assignment};
#[derivative(Debug(bound = "P: Fp2Parameters, ConstraintF: PrimeField"))] #[derivative(Debug(bound = "P: Fp2Parameters, ConstraintF: PrimeField"))]
#[must_use] #[must_use]
pub struct Fp2Gadget<P: Fp2Parameters<Fp = ConstraintF>, ConstraintF: PrimeField> { 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")] #[derivative(Debug = "ignore")]
_params: PhantomData<P>, _params: PhantomData<P>,
} }
@ -75,10 +75,7 @@ impl, ConstraintF: PrimeField> FieldGadget
#[inline] #[inline]
fn get_variable(&self) -> Self::Variable { fn get_variable(&self) -> Self::Variable {
(
self.c0.get_variable(),
self.c1.get_variable(),
)
(self.c0.get_variable(), self.c1.get_variable())
} }
#[inline] #[inline]

+ 3
- 3
r1cs-std/src/fields/fp6_3over2.rs

@ -21,9 +21,9 @@ where
P: Fp6Parameters, P: Fp6Parameters,
P::Fp2Params: Fp2Parameters<Fp = ConstraintF>, 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")] #[derivative(Debug = "ignore")]
_params: PhantomData<P>, _params: PhantomData<P>,
} }

+ 1
- 2
r1cs-std/src/groups/curves/short_weierstrass/bls12/mod.rs

@ -1,9 +1,8 @@
use algebra::{ use algebra::{
curves::bls12::{Bls12Parameters, G1Prepared, TwistType}, curves::bls12::{Bls12Parameters, G1Prepared, TwistType},
fields::Field, fields::Field,
BitIterator, ProjectiveCurve,
BitIterator, One, ProjectiveCurve,
}; };
use algebra::One;
use r1cs_core::{ConstraintSystem, SynthesisError}; use r1cs_core::{ConstraintSystem, SynthesisError};
use crate::{ use crate::{

+ 19
- 11
r1cs-std/src/groups/curves/short_weierstrass/mod.rs

@ -3,9 +3,8 @@ use algebra::{
short_weierstrass_jacobian::{GroupAffine as SWAffine, GroupProjective as SWProjective}, short_weierstrass_jacobian::{GroupAffine as SWAffine, GroupProjective as SWProjective},
SWModelParameters, SWModelParameters,
}, },
AffineCurve, BitIterator, Field, PrimeField, ProjectiveCurve,
AffineCurve, BitIterator, Field, One, PrimeField, ProjectiveCurve, Zero,
}; };
use algebra::{One, Zero};
use r1cs_core::{ConstraintSystem, SynthesisError}; use r1cs_core::{ConstraintSystem, SynthesisError};
use std::{borrow::Borrow, marker::PhantomData, ops::Neg}; use std::{borrow::Borrow, marker::PhantomData, ops::Neg};
@ -21,11 +20,11 @@ pub struct AffineGadget<
ConstraintF: Field, ConstraintF: Field,
F: FieldGadget<P::BaseField, ConstraintF>, F: FieldGadget<P::BaseField, ConstraintF>,
> { > {
pub x: F,
pub y: F,
pub x: F,
pub y: F,
pub infinity: Boolean, 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>> impl<P: SWModelParameters, ConstraintF: Field, F: FieldGadget<P::BaseField, ConstraintF>>
@ -99,7 +98,11 @@ where
#[inline] #[inline]
fn get_value(&self) -> Option<Self::Value> { 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(x), Some(y), Some(infinity)) => {
Some(SWAffine::new(x, y, infinity).into_projective()) Some(SWAffine::new(x, y, infinity).into_projective())
}, },
@ -341,14 +344,19 @@ where
) -> Result<Self, SynthesisError> { ) -> Result<Self, SynthesisError> {
let x = F::conditionally_select(&mut cs.ns(|| "x"), cond, &first.x, &second.x)?; 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 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)) Ok(Self::new(x, y, infinity))
} }
fn cost() -> usize { 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>, FN: FnOnce() -> Result<T, SynthesisError>,
T: Borrow<SWProjective<P>>, 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. // any on curve checks already.
let (x, y, infinity) = match value_gen() { let (x, y, infinity) = match value_gen() {
Ok(ge) => { Ok(ge) => {

+ 7
- 8
r1cs-std/src/groups/curves/twisted_edwards/mod.rs

@ -3,9 +3,8 @@ use algebra::{
twisted_edwards_extended::GroupAffine as TEAffine, MontgomeryModelParameters, twisted_edwards_extended::GroupAffine as TEAffine, MontgomeryModelParameters,
TEModelParameters, TEModelParameters,
}, },
BitIterator, Field,
BitIterator, Field, One, Zero,
}; };
use algebra::{One, Zero};
use r1cs_core::{ConstraintSystem, SynthesisError}; use r1cs_core::{ConstraintSystem, SynthesisError};
@ -28,8 +27,8 @@ pub struct MontgomeryAffineGadget<
ConstraintF: Field, ConstraintF: Field,
F: FieldGadget<P::BaseField, ConstraintF>, F: FieldGadget<P::BaseField, ConstraintF>,
> { > {
pub x: F,
pub y: F,
pub x: F,
pub y: F,
#[derivative(Debug = "ignore")] #[derivative(Debug = "ignore")]
_params: PhantomData<P>, _params: PhantomData<P>,
#[derivative(Debug = "ignore")] #[derivative(Debug = "ignore")]
@ -62,8 +61,8 @@ mod montgomery_affine_impl {
} else if p.x == P::BaseField::zero() { } else if p.x == P::BaseField::zero() {
GroupAffine::new(P::BaseField::zero(), P::BaseField::zero()) GroupAffine::new(P::BaseField::zero(), P::BaseField::zero())
} else { } 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(); let v = u * &p.x.inverse().unwrap();
GroupAffine::new(u, v) GroupAffine::new(u, v)
}; };
@ -200,8 +199,8 @@ pub struct AffineGadget<
ConstraintF: Field, ConstraintF: Field,
F: FieldGadget<P::BaseField, ConstraintF>, F: FieldGadget<P::BaseField, ConstraintF>,
> { > {
pub x: F,
pub y: F,
pub x: F,
pub y: F,
#[derivative(Debug = "ignore")] #[derivative(Debug = "ignore")]
_params: PhantomData<P>, _params: PhantomData<P>,
#[derivative(Debug = "ignore")] #[derivative(Debug = "ignore")]

+ 1
- 1
r1cs-std/src/lib.rs

@ -71,6 +71,6 @@ pub trait Assignment {
impl<T> Assignment<T> for Option<T> { impl<T> Assignment<T> for Option<T> {
fn get(self) -> Result<T, r1cs_core::SynthesisError> { fn get(self) -> Result<T, r1cs_core::SynthesisError> {
self.ok_or_else(|| { r1cs_core::SynthesisError::AssignmentMissing })
self.ok_or_else(|| r1cs_core::SynthesisError::AssignmentMissing)
} }
} }

+ 8
- 16
r1cs-std/src/pairing/mod.rs

@ -59,8 +59,7 @@ pub trait PairingGadget {
mod test { mod test {
// use rand; // use rand;
use crate::test_constraint_system::TestConstraintSystem; use crate::test_constraint_system::TestConstraintSystem;
use algebra::{BitIterator, Field};
use algebra::One;
use algebra::{BitIterator, Field, One};
use r1cs_core::ConstraintSystem; use r1cs_core::ConstraintSystem;
#[test] #[test]
@ -107,23 +106,17 @@ mod test {
let sb_prep_g = G2PreparedGadget::from_affine(&mut cs.ns(|| "sb_prep"), &sb_g).unwrap(); let sb_prep_g = G2PreparedGadget::from_affine(&mut cs.ns(|| "sb_prep"), &sb_g).unwrap();
let (ans1_g, ans1_n) = { 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); let ans_n = Bls12_377::pairing(sa, b);
(ans_g, ans_n) (ans_g, ans_n)
}; };
let (ans2_g, ans2_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); let ans_n = Bls12_377::pairing(a, sb);
(ans_g, ans_n) (ans_g, ans_n)
}; };
@ -134,8 +127,7 @@ mod test {
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let mut ans_g = 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); let mut ans_n = Bls12_377::pairing(a, b);
ans_n = ans_n.pow(s.into_repr()); ans_n = ans_n.pow(s.into_repr());
ans_g = ans_g.pow(cs.ns(|| "pow"), &s_iter).unwrap(); ans_g = ans_g.pow(cs.ns(|| "pow"), &s_iter).unwrap();

Loading…
Cancel
Save