add evaluate eq gadget for i types

This commit is contained in:
collin 2020-07-14 17:37:04 -07:00
parent 6c0a5ed872
commit a1f2366fe8
5 changed files with 45 additions and 16 deletions

View File

@ -1,13 +0,0 @@
use crate::errors::IntegerError;
use snarkos_models::{
curves::PrimeField,
gadgets::{r1cs::ConstraintSystem, utilities::boolean::Boolean},
};
///
pub trait Cmp<Rhs = Self>
where
Self: std::marker::Sized,
{
}

View File

@ -2,9 +2,6 @@
pub mod add; pub mod add;
pub use self::add::*; pub use self::add::*;
pub mod cmp;
pub use self::cmp::*;
pub mod div; pub mod div;
pub use self::div::*; pub use self::div::*;

View File

@ -6,5 +6,8 @@ pub use self::arithmetic::*;
pub mod int_impl; pub mod int_impl;
pub use self::int_impl::*; pub use self::int_impl::*;
pub mod relational;
pub use self::relational::*;
pub mod utilities; pub mod utilities;
pub use self::utilities::*; pub use self::utilities::*;

View File

@ -0,0 +1,40 @@
use crate::{Int, Int128, Int16, Int32, Int64, Int8};
use snarkos_errors::gadgets::SynthesisError;
use snarkos_models::{
curves::PrimeField,
gadgets::{
r1cs::ConstraintSystem,
utilities::{boolean::Boolean, eq::EvaluateEqGadget},
},
};
macro_rules! eq_gadget_impl {
($($gadget: ident)*) => ($(
impl<F: PrimeField> EvaluateEqGadget<F> for $gadget {
fn evaluate_equal<CS: ConstraintSystem<F>>(
&self,
mut cs: CS,
other: &Self
) -> Result<Boolean, SynthesisError> {
let mut result = Boolean::constant(true);
for (i, (a, b)) in self.bits.iter().zip(&other.bits).enumerate() {
let equal = a.evaluate_equal(
&mut cs.ns(|| format!("{} evaluate equality for {}-th bit", <$gadget as Int>::SIZE, i)),
b,
)?;
result = Boolean::and(
&mut cs.ns(|| format!("{} and result for {}-th bit", <$gadget as Int>::SIZE, i)),
&equal,
&result,
)?;
}
Ok(result)
}
}
)*)
}
eq_gadget_impl!(Int8 Int16 Int32 Int64 Int128);

View File

@ -0,0 +1,2 @@
pub mod eq;
pub use self::eq::*;