refactor to use snarkvm evaluatelt and comparator gadgets

This commit is contained in:
collin 2021-03-11 16:44:46 -08:00
parent e7745bad80
commit 14063f723b
9 changed files with 109 additions and 113 deletions

View File

@ -18,9 +18,9 @@
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType}; use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
use leo_asg::Span; use leo_asg::Span;
use leo_gadgets::bits::ComparatorGadget;
use snarkvm_fields::PrimeField; use snarkvm_fields::PrimeField;
use snarkvm_gadgets::utilities::bits::ComparatorGadget;
use snarkvm_r1cs::ConstraintSystem; use snarkvm_r1cs::ConstraintSystem;
pub fn evaluate_ge<'a, F: PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>( pub fn evaluate_ge<'a, F: PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(

View File

@ -18,9 +18,9 @@
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType}; use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
use leo_asg::Span; use leo_asg::Span;
use leo_gadgets::bits::ComparatorGadget;
use snarkvm_fields::PrimeField; use snarkvm_fields::PrimeField;
use snarkvm_gadgets::utilities::bits::ComparatorGadget;
use snarkvm_r1cs::ConstraintSystem; use snarkvm_r1cs::ConstraintSystem;
pub fn evaluate_gt<'a, F: PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>( pub fn evaluate_gt<'a, F: PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(

View File

@ -18,9 +18,9 @@
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType}; use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
use leo_asg::Span; use leo_asg::Span;
use leo_gadgets::bits::ComparatorGadget;
use snarkvm_fields::PrimeField; use snarkvm_fields::PrimeField;
use snarkvm_gadgets::utilities::bits::ComparatorGadget;
use snarkvm_r1cs::ConstraintSystem; use snarkvm_r1cs::ConstraintSystem;
pub fn evaluate_le<'a, F: PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>( pub fn evaluate_le<'a, F: PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(

View File

@ -18,9 +18,9 @@
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType}; use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
use leo_asg::Span; use leo_asg::Span;
use leo_gadgets::bits::comparator::EvaluateLtGadget;
use snarkvm_fields::PrimeField; use snarkvm_fields::PrimeField;
use snarkvm_gadgets::utilities::bits::EvaluateLtGadget;
use snarkvm_r1cs::ConstraintSystem; use snarkvm_r1cs::ConstraintSystem;
pub fn evaluate_lt<'a, F: PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>( pub fn evaluate_lt<'a, F: PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(

View File

@ -18,15 +18,13 @@
use crate::{errors::IntegerError, IntegerTrait}; use crate::{errors::IntegerError, IntegerTrait};
use leo_asg::{ConstInt, IntegerType, Span}; use leo_asg::{ConstInt, IntegerType, Span};
use leo_ast::InputValue; use leo_ast::InputValue;
use leo_gadgets::{ use leo_gadgets::signed_integer::*;
bits::comparator::{ComparatorGadget, EvaluateLtGadget},
signed_integer::*,
};
use snarkvm_fields::{Field, PrimeField}; use snarkvm_fields::{Field, PrimeField};
use snarkvm_gadgets::traits::utilities::{ use snarkvm_gadgets::traits::utilities::{
alloc::AllocGadget, alloc::AllocGadget,
arithmetic::{Add, Div, Mul, Neg, Pow, Sub}, arithmetic::{Add, Div, Mul, Neg, Pow, Sub},
bits::comparator::{ComparatorGadget, EvaluateLtGadget},
boolean::Boolean, boolean::Boolean,
eq::{ConditionalEqGadget, EqGadget, EvaluateEqGadget}, eq::{ConditionalEqGadget, EqGadget, EvaluateEqGadget},
select::CondSelectGadget, select::CondSelectGadget,

View File

@ -1,93 +1,93 @@
// Copyright (C) 2019-2021 Aleo Systems Inc. // // Copyright (C) 2019-2021 Aleo Systems Inc.
// This file is part of the Leo library. // // This file is part of the Leo library.
//
// The Leo library is free software: you can redistribute it and/or modify // // The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // // (at your option) any later version.
//
// The Leo library is distributed in the hope that it will be useful, // // The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // // GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License // // You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
//
use snarkvm_fields::{Field, PrimeField}; // use snarkvm_fields::{Field, PrimeField};
use snarkvm_gadgets::traits::utilities::{ // use snarkvm_gadgets::traits::utilities::{
boolean::Boolean, // boolean::Boolean,
select::CondSelectGadget, // select::CondSelectGadget,
uint::{UInt128, UInt16, UInt32, UInt64, UInt8}, // uint::{UInt128, UInt16, UInt32, UInt64, UInt8},
}; // };
use snarkvm_r1cs::{ConstraintSystem, SynthesisError}; // use snarkvm_r1cs::{ConstraintSystem, SynthesisError};
//
pub trait EvaluateLtGadget<F: Field> { // pub trait EvaluateLtGadget<F: Field> {
fn less_than<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError>; // fn less_than<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError>;
} // }
//
// implementing `EvaluateLtGadget` will implement `ComparatorGadget` // // implementing `EvaluateLtGadget` will implement `ComparatorGadget`
pub trait ComparatorGadget<F: Field> // pub trait ComparatorGadget<F: Field>
where // 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, self) // 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> {
let is_gt = self.greater_than(cs, other)?; // let is_gt = self.greater_than(cs, other)?;
Ok(is_gt.not()) // Ok(is_gt.not())
} // }
//
fn greater_than_or_equal<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError> { // fn greater_than_or_equal<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError> {
other.less_than_or_equal(cs, self) // other.less_than_or_equal(cs, self)
} // }
} // }
//
macro_rules! uint_cmp_impl { // macro_rules! uint_cmp_impl {
($($gadget: ident),*) => ($( // ($($gadget: ident),*) => ($(
/* Bitwise less than comparison of two unsigned integers */ // /* Bitwise less than comparison of two unsigned integers */
impl<F: PrimeField> EvaluateLtGadget<F> for $gadget { // impl<F: PrimeField> EvaluateLtGadget<F> for $gadget {
fn less_than<CS: ConstraintSystem<F>>(&self, mut cs: CS, other: &Self) -> Result<Boolean, SynthesisError> { // fn less_than<CS: ConstraintSystem<F>>(&self, mut cs: CS, other: &Self) -> Result<Boolean, SynthesisError> {
//
let mut result = Boolean::constant(true); // let mut result = Boolean::constant(true);
let mut all_equal = Boolean::constant(true); // let mut all_equal = Boolean::constant(true);
//
// msb -> lsb // // msb -> lsb
for (i, (a, b)) in self // for (i, (a, b)) in self
.bits // .bits
.iter() // .iter()
.rev() // .rev()
.zip(other.bits.iter().rev()) // .zip(other.bits.iter().rev())
.enumerate() // .enumerate()
{ // {
// a == 0 & b == 1 // // a == 0 & b == 1
let less = Boolean::and(cs.ns(|| format!("not a and b [{}]", i)), &a.not(), b)?; // let less = Boolean::and(cs.ns(|| format!("not a and b [{}]", i)), &a.not(), b)?;
//
// a == b = !(a ^ b) // // a == b = !(a ^ b)
let not_equal = Boolean::xor(cs.ns(|| format!("a XOR b [{}]", i)), a, b)?; // let not_equal = Boolean::xor(cs.ns(|| format!("a XOR b [{}]", i)), a, b)?;
let equal = not_equal.not(); // let equal = not_equal.not();
//
// evaluate a <= b // // evaluate a <= b
let less_or_equal = Boolean::or(cs.ns(|| format!("less or equal [{}]", i)), &less, &equal)?; // let less_or_equal = Boolean::or(cs.ns(|| format!("less or equal [{}]", i)), &less, &equal)?;
//
// select the current result if it is the first bit difference // // select the current result if it is the first bit difference
result = Boolean::conditionally_select(cs.ns(|| format!("select bit [{}]", i)), &all_equal, &less_or_equal, &result)?; // result = Boolean::conditionally_select(cs.ns(|| format!("select bit [{}]", i)), &all_equal, &less_or_equal, &result)?;
//
// keep track of equal bits // // keep track of equal bits
all_equal = Boolean::and(cs.ns(|| format!("accumulate equal [{}]", i)), &all_equal, &equal)?; // all_equal = Boolean::and(cs.ns(|| format!("accumulate equal [{}]", i)), &all_equal, &equal)?;
} // }
//
result = Boolean::and(cs.ns(|| format!("false if all equal")), &result, &all_equal.not())?; // result = Boolean::and(cs.ns(|| format!("false if all equal")), &result, &all_equal.not())?;
//
Ok(result) // Ok(result)
} // }
} // }
//
/* Bitwise comparison of two unsigned integers */ // /* Bitwise comparison of two unsigned integers */
impl<F: PrimeField> ComparatorGadget<F> for $gadget {} // impl<F: PrimeField> ComparatorGadget<F> for $gadget {}
)*) // )*)
} // }
//
uint_cmp_impl!(UInt8, UInt16, UInt32, UInt64, UInt128); // uint_cmp_impl!(UInt8, UInt16, UInt32, UInt64, UInt128);

View File

@ -18,8 +18,8 @@
pub mod adder; pub mod adder;
pub use self::adder::*; pub use self::adder::*;
pub mod comparator; // pub mod comparator;
pub use self::comparator::*; // pub use self::comparator::*;
pub mod rca; pub mod rca;
pub use self::rca::*; pub use self::rca::*;

View File

@ -14,11 +14,12 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{bits::ComparatorGadget, errors::SignedIntegerError, Int, Int128, Int16, Int32, Int64, Int8}; use crate::{errors::SignedIntegerError, Int, Int128, Int16, Int32, Int64, Int8};
use snarkvm_fields::PrimeField; use snarkvm_fields::PrimeField;
use snarkvm_gadgets::traits::utilities::{ use snarkvm_gadgets::traits::utilities::{
alloc::AllocGadget, alloc::AllocGadget,
arithmetic::{Add, Div, Neg, Sub}, arithmetic::{Add, Div, Neg, Sub},
bits::ComparatorGadget,
boolean::{AllocatedBit, Boolean}, boolean::{AllocatedBit, Boolean},
eq::EvaluateEqGadget, eq::EvaluateEqGadget,
select::CondSelectGadget, select::CondSelectGadget,

View File

@ -14,17 +14,14 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{ use crate::{Int128, Int16, Int32, Int64, Int8};
bits::{ComparatorGadget, EvaluateLtGadget},
Int128,
Int16,
Int32,
Int64,
Int8,
};
use snarkvm_fields::PrimeField; use snarkvm_fields::PrimeField;
use snarkvm_gadgets::traits::utilities::{boolean::Boolean, select::CondSelectGadget}; use snarkvm_gadgets::traits::utilities::{
bits::comparator::{ComparatorGadget, EvaluateLtGadget},
boolean::Boolean,
select::CondSelectGadget,
};
use snarkvm_r1cs::{ConstraintSystem, SynthesisError}; use snarkvm_r1cs::{ConstraintSystem, SynthesisError};
use std::cmp::Ordering; use std::cmp::Ordering;