Optimise conditionally_reverse.
Use a single constraint instead of two. Fixes #25.
This commit is contained in:
parent
c091e274ee
commit
26dbdb450b
|
@ -303,7 +303,7 @@ impl<E: Engine> AllocatedNum<E> {
|
||||||
) -> Result<(Self, Self), SynthesisError>
|
) -> Result<(Self, Self), SynthesisError>
|
||||||
where CS: ConstraintSystem<E>
|
where CS: ConstraintSystem<E>
|
||||||
{
|
{
|
||||||
let c = Self::alloc(
|
let r = Self::alloc(
|
||||||
cs.namespace(|| "conditional reversal result 1"),
|
cs.namespace(|| "conditional reversal result 1"),
|
||||||
|| {
|
|| {
|
||||||
if *condition.get_value().get()? {
|
if *condition.get_value().get()? {
|
||||||
|
@ -314,14 +314,7 @@ impl<E: Engine> AllocatedNum<E> {
|
||||||
}
|
}
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
cs.enforce(
|
let s = Self::alloc(
|
||||||
|| "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(
|
|
||||||
cs.namespace(|| "conditional reversal result 2"),
|
cs.namespace(|| "conditional reversal result 2"),
|
||||||
|| {
|
|| {
|
||||||
if *condition.get_value().get()? {
|
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(
|
cs.enforce(
|
||||||
|| "second conditional reversal",
|
|| "conditional reversal",
|
||||||
|lc| lc + b.variable - a.variable,
|
|lc| lc + b.variable + b.variable - a.variable - a.variable,
|
||||||
|_| condition.lc(CS::one(), E::Fr::one()),
|
|_| 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>(
|
pub fn conditionally_negate<CS>(
|
||||||
|
|
Loading…
Reference in New Issue