Optimise conditionally_reverse.

Use a single constraint instead of two.

Fixes #25.
This commit is contained in:
Jason Davies 2018-02-11 20:02:18 +00:00
parent c091e274ee
commit 26dbdb450b
No known key found for this signature in database
GPG Key ID: BAE3938318C90D61
1 changed files with 11 additions and 13 deletions

View File

@ -303,7 +303,7 @@ impl<E: Engine> AllocatedNum<E> {
) -> Result<(Self, Self), SynthesisError>
where CS: ConstraintSystem<E>
{
let c = Self::alloc(
let r = Self::alloc(
cs.namespace(|| "conditional reversal result 1"),
|| {
if *condition.get_value().get()? {
@ -314,14 +314,7 @@ impl<E: Engine> AllocatedNum<E> {
}
)?;
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<E: Engine> AllocatedNum<E> {
}
)?;
// (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<CS>(