use crate::{
curves::{Field, PrimeField},
gadgets::{
r1cs::ConstraintSystem,
utilities::{
boolean::Boolean,
select::CondSelectGadget,
uint::{UInt128, UInt16, UInt32, UInt64, UInt8},
},
},
};
use snarkvm_errors::gadgets::SynthesisError;
pub trait EvaluateLtGadget<F: Field> {
fn less_than<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Boolean, SynthesisError>;
}
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),*) => ($(
impl<F: Field + 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);
for (i, (a, b)) in self
.bits
.iter()
.rev()
.zip(other.bits.iter().rev())
.enumerate()
{
let less = Boolean::and(cs.ns(|| format!("not a and b [{}]", i)), &a.not(), b)?;
let not_equal = Boolean::xor(cs.ns(|| format!("a XOR b [{}]", i)), a, b)?;
let equal = not_equal.not();
let less_or_equal = Boolean::or(cs.ns(|| format!("less or equal [{}]", i)), &less, &equal)?;
result = Boolean::conditionally_select(cs.ns(|| format!("select bit [{}]", i)), &all_equal, &less_or_equal, &result)?;
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)
}
}
impl<F: Field + PrimeField> ComparatorGadget<F> for $gadget {}
)*)
}
uint_cmp_impl!(UInt8, UInt16, UInt32, UInt64, UInt128);