From a1f2366fe8be24908a3ed3de1df1184912c8a25f Mon Sep 17 00:00:00 2001 From: collin Date: Tue, 14 Jul 2020 17:37:04 -0700 Subject: [PATCH] add evaluate eq gadget for i types --- gadgets/src/signed_integer/arithmetic/cmp.rs | 13 ------- gadgets/src/signed_integer/arithmetic/mod.rs | 3 -- gadgets/src/signed_integer/mod.rs | 3 ++ gadgets/src/signed_integer/relational/eq.rs | 40 ++++++++++++++++++++ gadgets/src/signed_integer/relational/mod.rs | 2 + 5 files changed, 45 insertions(+), 16 deletions(-) delete mode 100644 gadgets/src/signed_integer/arithmetic/cmp.rs create mode 100644 gadgets/src/signed_integer/relational/eq.rs create mode 100644 gadgets/src/signed_integer/relational/mod.rs diff --git a/gadgets/src/signed_integer/arithmetic/cmp.rs b/gadgets/src/signed_integer/arithmetic/cmp.rs deleted file mode 100644 index d198a88596..0000000000 --- a/gadgets/src/signed_integer/arithmetic/cmp.rs +++ /dev/null @@ -1,13 +0,0 @@ -use crate::errors::IntegerError; - -use snarkos_models::{ - curves::PrimeField, - gadgets::{r1cs::ConstraintSystem, utilities::boolean::Boolean}, -}; - -/// -pub trait Cmp -where - Self: std::marker::Sized, -{ -} diff --git a/gadgets/src/signed_integer/arithmetic/mod.rs b/gadgets/src/signed_integer/arithmetic/mod.rs index 6cd0f3bc66..ae4970b62e 100644 --- a/gadgets/src/signed_integer/arithmetic/mod.rs +++ b/gadgets/src/signed_integer/arithmetic/mod.rs @@ -2,9 +2,6 @@ pub mod add; pub use self::add::*; -pub mod cmp; -pub use self::cmp::*; - pub mod div; pub use self::div::*; diff --git a/gadgets/src/signed_integer/mod.rs b/gadgets/src/signed_integer/mod.rs index 53c20da0b5..99a2192182 100644 --- a/gadgets/src/signed_integer/mod.rs +++ b/gadgets/src/signed_integer/mod.rs @@ -6,5 +6,8 @@ pub use self::arithmetic::*; pub mod int_impl; pub use self::int_impl::*; +pub mod relational; +pub use self::relational::*; + pub mod utilities; pub use self::utilities::*; diff --git a/gadgets/src/signed_integer/relational/eq.rs b/gadgets/src/signed_integer/relational/eq.rs new file mode 100644 index 0000000000..68deca71ed --- /dev/null +++ b/gadgets/src/signed_integer/relational/eq.rs @@ -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 EvaluateEqGadget for $gadget { + fn evaluate_equal>( + &self, + mut cs: CS, + other: &Self + ) -> Result { + 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); diff --git a/gadgets/src/signed_integer/relational/mod.rs b/gadgets/src/signed_integer/relational/mod.rs new file mode 100644 index 0000000000..9d43347649 --- /dev/null +++ b/gadgets/src/signed_integer/relational/mod.rs @@ -0,0 +1,2 @@ +pub mod eq; +pub use self::eq::*;