fix integer and field gadgets

This commit is contained in:
collin 2020-06-25 17:19:32 -07:00
parent 7233e55dc1
commit a8cbbee392
3 changed files with 13 additions and 5 deletions

View File

@ -14,7 +14,7 @@ where
Self: EvaluateLtGadget<F>, Self: EvaluateLtGadget<F>,
{ {
fn greater_than<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError> { fn greater_than<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError> {
other.less_than(cs, other) other.less_than(cs, self)
} }
fn less_than_or_equal<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError> { fn less_than_or_equal<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError> {

View File

@ -458,9 +458,17 @@ impl<F: Field + PrimeField> EvaluateLtGadget<F> for Integer {
.zip(other.to_bits_le().iter().rev()) .zip(other.to_bits_le().iter().rev())
.enumerate() .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); return Ok(is_less);
} else if i == self.to_bits_le().len() - 1 { } else if i == self.to_bits_le().len() - 1 {
return Ok(is_less); return Ok(is_less);

View File

@ -224,9 +224,9 @@ impl<F: Field + PrimeField> EvaluateLtGadget<F> for FieldType<F> {
}) })
} }
(FieldType::Allocated(first), FieldType::Allocated(second)) => { (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) bool_option.ok_or(SynthesisError::AssignmentMissing)
}) })
} }