diff --git a/compiler/src/expression/relational/ge.rs b/compiler/src/expression/relational/ge.rs index a7913f11c7..0f72215bda 100644 --- a/compiler/src/expression/relational/ge.rs +++ b/compiler/src/expression/relational/ge.rs @@ -18,9 +18,9 @@ use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType}; use leo_asg::Span; -use leo_gadgets::bits::ComparatorGadget; use snarkvm_fields::PrimeField; +use snarkvm_gadgets::utilities::bits::ComparatorGadget; use snarkvm_r1cs::ConstraintSystem; pub fn evaluate_ge<'a, F: PrimeField, G: GroupType, CS: ConstraintSystem>( diff --git a/compiler/src/expression/relational/gt.rs b/compiler/src/expression/relational/gt.rs index bc1deccf95..eda1055be7 100644 --- a/compiler/src/expression/relational/gt.rs +++ b/compiler/src/expression/relational/gt.rs @@ -18,9 +18,9 @@ use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType}; use leo_asg::Span; -use leo_gadgets::bits::ComparatorGadget; use snarkvm_fields::PrimeField; +use snarkvm_gadgets::utilities::bits::ComparatorGadget; use snarkvm_r1cs::ConstraintSystem; pub fn evaluate_gt<'a, F: PrimeField, G: GroupType, CS: ConstraintSystem>( diff --git a/compiler/src/expression/relational/le.rs b/compiler/src/expression/relational/le.rs index 3dd23855c5..36e63d41df 100644 --- a/compiler/src/expression/relational/le.rs +++ b/compiler/src/expression/relational/le.rs @@ -18,9 +18,9 @@ use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType}; use leo_asg::Span; -use leo_gadgets::bits::ComparatorGadget; use snarkvm_fields::PrimeField; +use snarkvm_gadgets::utilities::bits::ComparatorGadget; use snarkvm_r1cs::ConstraintSystem; pub fn evaluate_le<'a, F: PrimeField, G: GroupType, CS: ConstraintSystem>( diff --git a/compiler/src/expression/relational/lt.rs b/compiler/src/expression/relational/lt.rs index 0bbd4a4219..72fd5c5fdc 100644 --- a/compiler/src/expression/relational/lt.rs +++ b/compiler/src/expression/relational/lt.rs @@ -18,9 +18,9 @@ use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType}; use leo_asg::Span; -use leo_gadgets::bits::comparator::EvaluateLtGadget; use snarkvm_fields::PrimeField; +use snarkvm_gadgets::utilities::bits::EvaluateLtGadget; use snarkvm_r1cs::ConstraintSystem; pub fn evaluate_lt<'a, F: PrimeField, G: GroupType, CS: ConstraintSystem>( diff --git a/compiler/src/value/integer/integer.rs b/compiler/src/value/integer/integer.rs index b4c8b2b7ce..a2affe09b6 100644 --- a/compiler/src/value/integer/integer.rs +++ b/compiler/src/value/integer/integer.rs @@ -18,15 +18,13 @@ use crate::{errors::IntegerError, IntegerTrait}; use leo_asg::{ConstInt, IntegerType, Span}; use leo_ast::InputValue; -use leo_gadgets::{ - bits::comparator::{ComparatorGadget, EvaluateLtGadget}, - signed_integer::*, -}; +use leo_gadgets::signed_integer::*; use snarkvm_fields::{Field, PrimeField}; use snarkvm_gadgets::traits::utilities::{ alloc::AllocGadget, arithmetic::{Add, Div, Mul, Neg, Pow, Sub}, + bits::comparator::{ComparatorGadget, EvaluateLtGadget}, boolean::Boolean, eq::{ConditionalEqGadget, EqGadget, EvaluateEqGadget}, select::CondSelectGadget, diff --git a/gadgets/src/bits/comparator.rs b/gadgets/src/bits/comparator.rs index ef0a6fc5ad..40cbec43b0 100644 --- a/gadgets/src/bits/comparator.rs +++ b/gadgets/src/bits/comparator.rs @@ -1,93 +1,93 @@ -// Copyright (C) 2019-2021 Aleo Systems Inc. -// This file is part of the Leo library. - -// 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 -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// The Leo library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with the Leo library. If not, see . - -use snarkvm_fields::{Field, PrimeField}; -use snarkvm_gadgets::traits::utilities::{ - boolean::Boolean, - select::CondSelectGadget, - uint::{UInt128, UInt16, UInt32, UInt64, UInt8}, -}; -use snarkvm_r1cs::{ConstraintSystem, SynthesisError}; - -pub trait EvaluateLtGadget { - fn less_than>(&self, cs: CS, other: &Self) -> Result; -} - -// implementing `EvaluateLtGadget` will implement `ComparatorGadget` -pub trait ComparatorGadget -where - Self: EvaluateLtGadget, -{ - fn greater_than>(&self, cs: CS, other: &Self) -> Result { - other.less_than(cs, self) - } - - fn less_than_or_equal>(&self, cs: CS, other: &Self) -> Result { - let is_gt = self.greater_than(cs, other)?; - Ok(is_gt.not()) - } - - fn greater_than_or_equal>(&self, cs: CS, other: &Self) -> Result { - other.less_than_or_equal(cs, self) - } -} - -macro_rules! uint_cmp_impl { - ($($gadget: ident),*) => ($( - /* Bitwise less than comparison of two unsigned integers */ - impl EvaluateLtGadget for $gadget { - fn less_than>(&self, mut cs: CS, other: &Self) -> Result { - - let mut result = Boolean::constant(true); - let mut all_equal = Boolean::constant(true); - - // msb -> lsb - for (i, (a, b)) in self - .bits - .iter() - .rev() - .zip(other.bits.iter().rev()) - .enumerate() - { - // a == 0 & b == 1 - let less = Boolean::and(cs.ns(|| format!("not a and b [{}]", i)), &a.not(), b)?; - - // a == b = !(a ^ b) - let not_equal = Boolean::xor(cs.ns(|| format!("a XOR b [{}]", i)), a, b)?; - let equal = not_equal.not(); - - // evaluate a <= b - 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 - result = Boolean::conditionally_select(cs.ns(|| format!("select bit [{}]", i)), &all_equal, &less_or_equal, &result)?; - - // keep track of equal bits - 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())?; - - Ok(result) - } - } - - /* Bitwise comparison of two unsigned integers */ - impl ComparatorGadget for $gadget {} - )*) -} - -uint_cmp_impl!(UInt8, UInt16, UInt32, UInt64, UInt128); +// // Copyright (C) 2019-2021 Aleo Systems Inc. +// // This file is part of the Leo library. +// +// // 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 +// // the Free Software Foundation, either version 3 of the License, or +// // (at your option) any later version. +// +// // The Leo library is distributed in the hope that it will be useful, +// // but WITHOUT ANY WARRANTY; without even the implied warranty of +// // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// // GNU General Public License for more details. +// +// // You should have received a copy of the GNU General Public License +// // along with the Leo library. If not, see . +// +// use snarkvm_fields::{Field, PrimeField}; +// use snarkvm_gadgets::traits::utilities::{ +// boolean::Boolean, +// select::CondSelectGadget, +// uint::{UInt128, UInt16, UInt32, UInt64, UInt8}, +// }; +// use snarkvm_r1cs::{ConstraintSystem, SynthesisError}; +// +// pub trait EvaluateLtGadget { +// fn less_than>(&self, cs: CS, other: &Self) -> Result; +// } +// +// // implementing `EvaluateLtGadget` will implement `ComparatorGadget` +// pub trait ComparatorGadget +// where +// Self: EvaluateLtGadget, +// { +// fn greater_than>(&self, cs: CS, other: &Self) -> Result { +// other.less_than(cs, self) +// } +// +// fn less_than_or_equal>(&self, cs: CS, other: &Self) -> Result { +// let is_gt = self.greater_than(cs, other)?; +// Ok(is_gt.not()) +// } +// +// fn greater_than_or_equal>(&self, cs: CS, other: &Self) -> Result { +// other.less_than_or_equal(cs, self) +// } +// } +// +// macro_rules! uint_cmp_impl { +// ($($gadget: ident),*) => ($( +// /* Bitwise less than comparison of two unsigned integers */ +// impl EvaluateLtGadget for $gadget { +// fn less_than>(&self, mut cs: CS, other: &Self) -> Result { +// +// let mut result = Boolean::constant(true); +// let mut all_equal = Boolean::constant(true); +// +// // msb -> lsb +// for (i, (a, b)) in self +// .bits +// .iter() +// .rev() +// .zip(other.bits.iter().rev()) +// .enumerate() +// { +// // a == 0 & b == 1 +// let less = Boolean::and(cs.ns(|| format!("not a and b [{}]", i)), &a.not(), b)?; +// +// // a == b = !(a ^ b) +// let not_equal = Boolean::xor(cs.ns(|| format!("a XOR b [{}]", i)), a, b)?; +// let equal = not_equal.not(); +// +// // evaluate a <= b +// 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 +// result = Boolean::conditionally_select(cs.ns(|| format!("select bit [{}]", i)), &all_equal, &less_or_equal, &result)?; +// +// // keep track of equal bits +// 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())?; +// +// Ok(result) +// } +// } +// +// /* Bitwise comparison of two unsigned integers */ +// impl ComparatorGadget for $gadget {} +// )*) +// } +// +// uint_cmp_impl!(UInt8, UInt16, UInt32, UInt64, UInt128); diff --git a/gadgets/src/bits/mod.rs b/gadgets/src/bits/mod.rs index e4842090db..19d137f833 100644 --- a/gadgets/src/bits/mod.rs +++ b/gadgets/src/bits/mod.rs @@ -18,8 +18,8 @@ pub mod adder; pub use self::adder::*; -pub mod comparator; -pub use self::comparator::*; +// pub mod comparator; +// pub use self::comparator::*; pub mod rca; pub use self::rca::*; diff --git a/gadgets/src/signed_integer/arithmetic/div.rs b/gadgets/src/signed_integer/arithmetic/div.rs index 7048ff1df1..656583a643 100644 --- a/gadgets/src/signed_integer/arithmetic/div.rs +++ b/gadgets/src/signed_integer/arithmetic/div.rs @@ -14,11 +14,12 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -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_gadgets::traits::utilities::{ alloc::AllocGadget, arithmetic::{Add, Div, Neg, Sub}, + bits::ComparatorGadget, boolean::{AllocatedBit, Boolean}, eq::EvaluateEqGadget, select::CondSelectGadget, diff --git a/gadgets/src/signed_integer/relational/cmp.rs b/gadgets/src/signed_integer/relational/cmp.rs index 07b2537748..26cfcaea7c 100644 --- a/gadgets/src/signed_integer/relational/cmp.rs +++ b/gadgets/src/signed_integer/relational/cmp.rs @@ -14,17 +14,14 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{ - bits::{ComparatorGadget, EvaluateLtGadget}, - Int128, - Int16, - Int32, - Int64, - Int8, -}; +use crate::{Int128, Int16, Int32, Int64, Int8}; 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 std::cmp::Ordering;