Runs rustfmt on the repo

This commit is contained in:
François Garillot
2020-01-23 07:19:48 -08:00
committed by Pratyush Mishra
parent a3e1cd6cf2
commit b26867f267
19 changed files with 107 additions and 110 deletions

View File

@@ -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<F: PrimeField, CS: ConstraintSystem<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 @@ fn cond_select_helper<F: PrimeField, CS: ConstraintSystem<F>>(
|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<ConstraintF: PrimeField> CondSelectGadget<ConstraintF> 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<ConstraintF: PrimeField> CondSelectGadget<ConstraintF> 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!(<Boolean as CondSelectGadget<Fr>>::cost() >= after - before);
}
}

View File

@@ -344,8 +344,7 @@ impl<ConstraintF: Field> ConditionalEqGadget<ConstraintF> 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;

View File

@@ -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,
<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>,
}

View File

@@ -11,8 +11,8 @@ use crate::{fields::fp::FpGadget, prelude::*, Assignment};
#[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 @@ impl<P: Fp2Parameters<Fp = ConstraintF>, ConstraintF: PrimeField> FieldGadget<Fp
#[inline]
fn get_variable(&self) -> Self::Variable {
(
self.c0.get_variable(),
self.c1.get_variable(),
)
(self.c0.get_variable(), self.c1.get_variable())
}
#[inline]

View File

@@ -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>,
}

View File

@@ -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::{

View File

@@ -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<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 @@ where
#[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 @@ where
) -> 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) => {

View File

@@ -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<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 @@ 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<P::BaseField, ConstraintF>,
> {
pub x: F,
pub y: F,
pub x: F,
pub y: F,
#[derivative(Debug = "ignore")]
_params: PhantomData<P>,
#[derivative(Debug = "ignore")]

View File

@@ -71,6 +71,6 @@ pub trait Assignment<T> {
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)
}
}

View File

@@ -59,8 +59,7 @@ pub trait PairingGadget<PairingE: PairingEngine, ConstraintF: Field> {
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::<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();