Refactor to handle changes to ConstraintSystem API (no more Variable associated type, one is now static method).

This commit is contained in:
Sean Bowe
2018-02-16 08:45:18 -07:00
parent 814cfef769
commit 2f95a9094a
9 changed files with 199 additions and 171 deletions
+27 -29
View File
@@ -8,7 +8,8 @@ use pairing::{
use bellman::{
SynthesisError,
ConstraintSystem,
LinearCombination
LinearCombination,
Variable
};
use super::{
@@ -20,12 +21,12 @@ use super::boolean::{
AllocatedBit
};
pub struct AllocatedNum<E: Engine, Var> {
pub struct AllocatedNum<E: Engine> {
value: Option<E::Fr>,
variable: Var
variable: Variable
}
impl<Var: Copy, E: Engine> Clone for AllocatedNum<E, Var> {
impl<E: Engine> Clone for AllocatedNum<E> {
fn clone(&self) -> Self {
AllocatedNum {
value: self.value,
@@ -34,12 +35,12 @@ impl<Var: Copy, E: Engine> Clone for AllocatedNum<E, Var> {
}
}
impl<E: Engine, Var: Copy> AllocatedNum<E, Var> {
impl<E: Engine> AllocatedNum<E> {
pub fn alloc<CS, F>(
mut cs: CS,
value: F,
) -> Result<Self, SynthesisError>
where CS: ConstraintSystem<E, Variable=Var>,
where CS: ConstraintSystem<E>,
F: FnOnce() -> Result<E::Fr, SynthesisError>
{
let mut new_value = None;
@@ -60,8 +61,8 @@ impl<E: Engine, Var: Copy> AllocatedNum<E, Var> {
pub fn into_bits_strict<CS>(
&self,
mut cs: CS
) -> Result<Vec<Boolean<Var>>, SynthesisError>
where CS: ConstraintSystem<E, Variable=Var>
) -> Result<Vec<Boolean>, SynthesisError>
where CS: ConstraintSystem<E>
{
let bits = self.into_bits(&mut cs)?;
Boolean::enforce_in_field::<_, _, E::Fr>(&mut cs, &bits)?;
@@ -72,8 +73,8 @@ impl<E: Engine, Var: Copy> AllocatedNum<E, Var> {
pub fn into_bits<CS>(
&self,
mut cs: CS
) -> Result<Vec<Boolean<Var>>, SynthesisError>
where CS: ConstraintSystem<E, Variable=Var>
) -> Result<Vec<Boolean>, SynthesisError>
where CS: ConstraintSystem<E>
{
let bit_values = match self.value {
Some(value) => {
@@ -132,16 +133,16 @@ impl<E: Engine, Var: Copy> AllocatedNum<E, Var> {
pub fn from_bits_strict<CS>(
mut cs: CS,
bits: &[Boolean<Var>]
bits: &[Boolean]
) -> Result<Self, SynthesisError>
where CS: ConstraintSystem<E, Variable=Var>
where CS: ConstraintSystem<E>
{
assert_eq!(bits.len(), E::Fr::NUM_BITS as usize);
Boolean::enforce_in_field::<_, _, E::Fr>(&mut cs, bits)?;
let one = cs.one();
let mut lc = LinearCombination::<Var, E>::zero();
let one = CS::one();
let mut lc = LinearCombination::<E>::zero();
let mut coeff = E::Fr::one();
let mut value = Some(E::Fr::zero());
for bit in bits.iter().rev() {
@@ -204,7 +205,7 @@ impl<E: Engine, Var: Copy> AllocatedNum<E, Var> {
mut cs: CS,
other: &Self
) -> Result<Self, SynthesisError>
where CS: ConstraintSystem<E, Variable=Var>
where CS: ConstraintSystem<E>
{
let mut value = None;
@@ -235,7 +236,7 @@ impl<E: Engine, Var: Copy> AllocatedNum<E, Var> {
&self,
mut cs: CS
) -> Result<Self, SynthesisError>
where CS: ConstraintSystem<E, Variable=Var>
where CS: ConstraintSystem<E>
{
let mut value = None;
@@ -266,7 +267,7 @@ impl<E: Engine, Var: Copy> AllocatedNum<E, Var> {
&self,
mut cs: CS
) -> Result<(), SynthesisError>
where CS: ConstraintSystem<E, Variable=Var>
where CS: ConstraintSystem<E>
{
let inv = cs.alloc(|| "ephemeral inverse", || {
let tmp = *self.value.get()?;
@@ -281,12 +282,11 @@ impl<E: Engine, Var: Copy> AllocatedNum<E, Var> {
// Constrain a * inv = 1, which is only valid
// iff a has a multiplicative inverse, untrue
// for zero.
let one = cs.one();
cs.enforce(
|| "nonzero assertion constraint",
|lc| lc + self.variable,
|lc| lc + inv,
|lc| lc + one
|lc| lc + CS::one()
);
Ok(())
@@ -299,9 +299,9 @@ impl<E: Engine, Var: Copy> AllocatedNum<E, Var> {
mut cs: CS,
a: &Self,
b: &Self,
condition: &Boolean<Var>
condition: &Boolean
) -> Result<(Self, Self), SynthesisError>
where CS: ConstraintSystem<E, Variable=Var>
where CS: ConstraintSystem<E>
{
let c = Self::alloc(
cs.namespace(|| "conditional reversal result 1"),
@@ -314,11 +314,10 @@ impl<E: Engine, Var: Copy> AllocatedNum<E, Var> {
}
)?;
let one = cs.one();
cs.enforce(
|| "first conditional reversal",
|lc| lc + a.variable - b.variable,
|_| condition.lc(one, E::Fr::one()),
|_| condition.lc(CS::one(), E::Fr::one()),
|lc| lc + a.variable - c.variable
);
@@ -336,7 +335,7 @@ impl<E: Engine, Var: Copy> AllocatedNum<E, Var> {
cs.enforce(
|| "second conditional reversal",
|lc| lc + b.variable - a.variable,
|_| condition.lc(one, E::Fr::one()),
|_| condition.lc(CS::one(), E::Fr::one()),
|lc| lc + b.variable - d.variable
);
@@ -346,9 +345,9 @@ impl<E: Engine, Var: Copy> AllocatedNum<E, Var> {
pub fn conditionally_negate<CS>(
&self,
mut cs: CS,
condition: &Boolean<Var>
condition: &Boolean
) -> Result<Self, SynthesisError>
where CS: ConstraintSystem<E, Variable=Var>
where CS: ConstraintSystem<E>
{
let r = Self::alloc(
cs.namespace(|| "conditional negation result"),
@@ -365,11 +364,10 @@ impl<E: Engine, Var: Copy> AllocatedNum<E, Var> {
// x - 2cx = r
// (2x) * (c) = x - r
let one = cs.one();
cs.enforce(
|| "conditional negation",
|lc| lc + self.variable + self.variable,
|_| condition.lc(one, E::Fr::one()),
|_| condition.lc(CS::one(), E::Fr::one()),
|lc| lc + self.variable - r.variable
);
@@ -380,7 +378,7 @@ impl<E: Engine, Var: Copy> AllocatedNum<E, Var> {
self.value
}
pub fn get_variable(&self) -> Var {
pub fn get_variable(&self) -> Variable {
self.variable
}
}