Prepare Zexe for recursion (#241)

Co-authored-by: Pratyush Mishra <pratyushmishra@berkeley.edu>
This commit is contained in:
Weikeng Chen
2020-07-20 15:42:25 -07:00
committed by GitHub
parent 36305e7247
commit 61c70ed644
20 changed files with 1071 additions and 15 deletions

View File

@@ -852,6 +852,63 @@ impl<ConstraintF: Field> ToBytesGadget<ConstraintF> for Boolean {
}
}
impl<ConstraintF: PrimeField> ToConstraintFieldGadget<ConstraintF> for Boolean {
fn to_constraint_field<CS: ConstraintSystem<ConstraintF>>(
&self,
mut cs: CS,
) -> Result<Vec<FpGadget<ConstraintF>>, SynthesisError> {
let gadget = match self {
Boolean::Constant(cond) => {
if *cond {
FpGadget::one(&mut cs.ns(|| "one"))?
} else {
FpGadget::zero(&mut cs.ns(|| "zero"))?
}
}
Boolean::Is(allocated_bit) => {
let value = match self.get_value() {
None => None,
Some(bool) => {
if bool {
Some(ConstraintF::one())
} else {
Some(ConstraintF::zero())
}
}
};
FpGadget::<ConstraintF> {
value,
variable: ConstraintVar::Var(allocated_bit.get_variable()),
}
}
Boolean::Not(allocated_bit) => {
let value = match self.get_value() {
None => None,
Some(bool) => {
if bool {
Some(ConstraintF::zero())
} else {
Some(ConstraintF::one())
}
}
};
let mut lc = LinearCombination::<ConstraintF>::zero();
lc += (ConstraintF::one(), CS::one());
lc += (-ConstraintF::one(), allocated_bit.get_variable());
FpGadget::<ConstraintF> {
value,
variable: ConstraintVar::LC(lc),
}
}
};
Ok(vec![gadget])
}
}
impl<ConstraintF: PrimeField> CondSelectGadget<ConstraintF> for Boolean {
fn conditionally_select<CS>(
mut cs: CS,

View File

@@ -14,7 +14,7 @@ use crate::{
pub struct UInt32 {
// Least significant bit_gadget first
bits: Vec<Boolean>,
value: Option<u32>,
pub value: Option<u32>,
}
impl UInt32 {