diff --git a/src/circuits.rs b/src/circuits.rs index bbb62393..7b6b991c 100644 --- a/src/circuits.rs +++ b/src/circuits.rs @@ -7,3 +7,4 @@ pub use werewolf::*; pub mod bit_decomposition; pub mod enforce_smaller_or_eq_than; pub mod equality_zero; +pub mod smaller_than; diff --git a/src/circuits/smaller_than.rs b/src/circuits/smaller_than.rs new file mode 100644 index 00000000..b3a59699 --- /dev/null +++ b/src/circuits/smaller_than.rs @@ -0,0 +1,40 @@ +use std::cmp::Ordering; + +use ark_ff::PrimeField; +use ark_r1cs_std::alloc::AllocVar; +use ark_r1cs_std::fields::fp::FpVar; +use ark_relations::r1cs::{ConstraintSynthesizer, ConstraintSystemRef, SynthesisError}; +use mpc_algebra::malicious_majority::MpcField; +use mpc_algebra::MpcFpVar; + +type Fr = ark_bls12_377::Fr; +type MFr = MpcField; + +pub struct LessThanCircuit { + pub a: F, + pub b: F, + pub cmp: Ordering, + pub check_eq: bool, +} + +impl ConstraintSynthesizer for LessThanCircuit { + fn generate_constraints(self, cs: ConstraintSystemRef) -> Result<(), SynthesisError> { + let a_var = MpcFpVar::new_witness(cs.clone(), || Ok(self.a))?; + let b_var = MpcFpVar::new_witness(cs, || Ok(self.b))?; + + let _res = MpcFpVar::is_cmp(&a_var, &b_var, self.cmp, self.check_eq).unwrap(); + + Ok(()) + } +} + +impl ConstraintSynthesizer for LessThanCircuit { + fn generate_constraints(self, cs: ConstraintSystemRef) -> Result<(), SynthesisError> { + let a_var = FpVar::new_witness(cs.clone(), || Ok(self.a))?; + let b_var = FpVar::new_witness(cs, || Ok(self.b))?; + + let _res = FpVar::is_cmp(&a_var, &b_var, self.cmp, self.check_eq).unwrap(); + + Ok(()) + } +}