mirror of
https://github.com/AleoHQ/leo.git
synced 2024-12-27 03:21:49 +03:00
refactor to use snarkvm evaluatelt and comparator gadgets
This commit is contained in:
parent
e7745bad80
commit
14063f723b
compiler/src
gadgets/src
@ -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<F>, CS: ConstraintSystem<F>>(
|
||||
|
@ -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<F>, CS: ConstraintSystem<F>>(
|
||||
|
@ -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<F>, CS: ConstraintSystem<F>>(
|
||||
|
@ -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<F>, CS: ConstraintSystem<F>>(
|
||||
|
@ -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,
|
||||
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
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<F: Field> {
|
||||
fn less_than<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError>;
|
||||
}
|
||||
|
||||
// implementing `EvaluateLtGadget` will implement `ComparatorGadget`
|
||||
pub trait ComparatorGadget<F: Field>
|
||||
where
|
||||
Self: EvaluateLtGadget<F>,
|
||||
{
|
||||
fn greater_than<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError> {
|
||||
other.less_than(cs, self)
|
||||
}
|
||||
|
||||
fn less_than_or_equal<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError> {
|
||||
let is_gt = self.greater_than(cs, other)?;
|
||||
Ok(is_gt.not())
|
||||
}
|
||||
|
||||
fn greater_than_or_equal<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError> {
|
||||
other.less_than_or_equal(cs, self)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! uint_cmp_impl {
|
||||
($($gadget: ident),*) => ($(
|
||||
/* Bitwise less than comparison of two unsigned integers */
|
||||
impl<F: PrimeField> EvaluateLtGadget<F> for $gadget {
|
||||
fn less_than<CS: ConstraintSystem<F>>(&self, mut cs: CS, other: &Self) -> Result<Boolean, SynthesisError> {
|
||||
|
||||
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<F: PrimeField> ComparatorGadget<F> 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 <https://www.gnu.org/licenses/>.
|
||||
//
|
||||
// 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<F: Field> {
|
||||
// fn less_than<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError>;
|
||||
// }
|
||||
//
|
||||
// // implementing `EvaluateLtGadget` will implement `ComparatorGadget`
|
||||
// pub trait ComparatorGadget<F: Field>
|
||||
// where
|
||||
// Self: EvaluateLtGadget<F>,
|
||||
// {
|
||||
// fn greater_than<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError> {
|
||||
// other.less_than(cs, self)
|
||||
// }
|
||||
//
|
||||
// fn less_than_or_equal<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError> {
|
||||
// let is_gt = self.greater_than(cs, other)?;
|
||||
// Ok(is_gt.not())
|
||||
// }
|
||||
//
|
||||
// fn greater_than_or_equal<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError> {
|
||||
// other.less_than_or_equal(cs, self)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// macro_rules! uint_cmp_impl {
|
||||
// ($($gadget: ident),*) => ($(
|
||||
// /* Bitwise less than comparison of two unsigned integers */
|
||||
// impl<F: PrimeField> EvaluateLtGadget<F> for $gadget {
|
||||
// fn less_than<CS: ConstraintSystem<F>>(&self, mut cs: CS, other: &Self) -> Result<Boolean, SynthesisError> {
|
||||
//
|
||||
// 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<F: PrimeField> ComparatorGadget<F> for $gadget {}
|
||||
// )*)
|
||||
// }
|
||||
//
|
||||
// uint_cmp_impl!(UInt8, UInt16, UInt32, UInt64, UInt128);
|
||||
|
@ -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::*;
|
||||
|
@ -14,11 +14,12 @@
|
||||
// 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/>.
|
||||
|
||||
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,
|
||||
|
@ -14,17 +14,14 @@
|
||||
// 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/>.
|
||||
|
||||
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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user