diff --git a/src/circuit/num.rs b/src/circuit/num.rs index aca7562..4c7ab0e 100644 --- a/src/circuit/num.rs +++ b/src/circuit/num.rs @@ -303,7 +303,7 @@ impl AllocatedNum { ) -> Result<(Self, Self), SynthesisError> where CS: ConstraintSystem { - let c = Self::alloc( + let r = Self::alloc( cs.namespace(|| "conditional reversal result 1"), || { if *condition.get_value().get()? { @@ -314,14 +314,7 @@ impl AllocatedNum { } )?; - cs.enforce( - || "first conditional reversal", - |lc| lc + a.variable - b.variable, - |_| condition.lc(CS::one(), E::Fr::one()), - |lc| lc + a.variable - c.variable - ); - - let d = Self::alloc( + let s = Self::alloc( cs.namespace(|| "conditional reversal result 2"), || { if *condition.get_value().get()? { @@ -332,14 +325,19 @@ impl AllocatedNum { } )?; + // (1-c)(a) + (c)(b) = r + // (1-c)(b) + (c)(a) = s + // a - ca + cb - r = b - cb + ca - s + // c(2b - 2a) = (r - s) + (b - a) + cs.enforce( - || "second conditional reversal", - |lc| lc + b.variable - a.variable, + || "conditional reversal", + |lc| lc + b.variable + b.variable - a.variable - a.variable, |_| condition.lc(CS::one(), E::Fr::one()), - |lc| lc + b.variable - d.variable + |lc| lc + r.variable - s.variable + b.variable - a.variable ); - Ok((c, d)) + Ok((r, s)) } pub fn conditionally_negate(