From a8cbbee3925d832b9ee7c278d0f07789008110e2 Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 25 Jun 2020 17:19:32 -0700 Subject: [PATCH] fix integer and field gadgets --- compiler/src/constraints/comparator.rs | 2 +- compiler/src/constraints/integer.rs | 12 ++++++++++-- compiler/src/field/mod.rs | 4 ++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/compiler/src/constraints/comparator.rs b/compiler/src/constraints/comparator.rs index a22c6ede4d..34b75ead66 100644 --- a/compiler/src/constraints/comparator.rs +++ b/compiler/src/constraints/comparator.rs @@ -14,7 +14,7 @@ where Self: EvaluateLtGadget, { fn greater_than>(&self, cs: CS, other: &Self) -> Result { - other.less_than(cs, other) + other.less_than(cs, self) } fn less_than_or_equal>(&self, cs: CS, other: &Self) -> Result { diff --git a/compiler/src/constraints/integer.rs b/compiler/src/constraints/integer.rs index aa8b66991a..4c7331e2bf 100644 --- a/compiler/src/constraints/integer.rs +++ b/compiler/src/constraints/integer.rs @@ -458,9 +458,17 @@ impl EvaluateLtGadget for Integer { .zip(other.to_bits_le().iter().rev()) .enumerate() { - let is_less = Boolean::and(&mut cs, self_bit, &other_bit.not())?; + // is_greater = a & !b + // only true when a > b + let is_greater = Boolean::and(cs.ns(|| format!("a and not b [{}]", i)), self_bit, &other_bit.not())?; - if is_less.eq(&Boolean::constant(true)) { + // is_less = !a & b + // only true when a < b + let is_less = Boolean::and(cs.ns(|| format!("not a and b [{}]", i)), &self_bit.not(), other_bit)?; + + if is_greater.get_value().unwrap() { + return Ok(is_greater.not()); + } else if is_less.get_value().unwrap() { return Ok(is_less); } else if i == self.to_bits_le().len() - 1 { return Ok(is_less); diff --git a/compiler/src/field/mod.rs b/compiler/src/field/mod.rs index 6db0f95a24..69a894e52f 100644 --- a/compiler/src/field/mod.rs +++ b/compiler/src/field/mod.rs @@ -224,9 +224,9 @@ impl EvaluateLtGadget for FieldType { }) } (FieldType::Allocated(first), FieldType::Allocated(second)) => { - let bool_option = first.value.and_then(|a| second.value.map(|b| a.eq(&b))); + let bool_option = first.value.and_then(|a| second.value.map(|b| a.lt(&b))); - Boolean::alloc(&mut cs.ns(|| "evaluate_equal"), || { + Boolean::alloc(&mut cs.ns(|| "less than"), || { bool_option.ok_or(SynthesisError::AssignmentMissing) }) }