This commit is contained in:
Kobi Gurkan
2020-03-23 21:47:40 +02:00
committed by Pratyush Mishra
parent 7ed38fe4a6
commit b0f266de1e
2 changed files with 43 additions and 14 deletions

View File

@@ -5,10 +5,7 @@ use crate::{
}; };
use algebra::PrimeField; use algebra::PrimeField;
use r1cs_core::{ConstraintSystem, SynthesisError}; use r1cs_core::{ConstraintSystem, SynthesisError};
use std::{ use std::{cmp::Ordering, marker::PhantomData};
cmp::Ordering,
marker::PhantomData
};
pub struct CmpGadget<ConstraintF: PrimeField> { pub struct CmpGadget<ConstraintF: PrimeField> {
constraint_field_type: PhantomData<ConstraintF>, constraint_field_type: PhantomData<ConstraintF>,
@@ -25,9 +22,17 @@ impl<ConstraintF: PrimeField> CmpGadget<ConstraintF> {
let left; let left;
let right; let right;
match ordering { match ordering {
Ordering::Less => { left = a; right = b; } Ordering::Less => {
Ordering::Greater => {left = b; right = a; } left = a;
Ordering::Equal => { return Err(SynthesisError::Unsatisfiable); } right = b;
},
Ordering::Greater => {
left = b;
right = a;
},
Ordering::Equal => {
return Err(SynthesisError::Unsatisfiable);
},
}; };
let right_for_check = if should_also_check_equality { let right_for_check = if should_also_check_equality {
right.add_constant(cs.ns(|| "plus one"), &ConstraintF::one())? right.add_constant(cs.ns(|| "plus one"), &ConstraintF::one())?
@@ -41,7 +46,7 @@ impl<ConstraintF: PrimeField> CmpGadget<ConstraintF> {
fn check_smaller_than_mod_minus_one_div_two<CS: ConstraintSystem<ConstraintF>>( fn check_smaller_than_mod_minus_one_div_two<CS: ConstraintSystem<ConstraintF>>(
mut cs: CS, mut cs: CS,
a: &FpGadget<ConstraintF>, a: &FpGadget<ConstraintF>,
) -> Result<(), SynthesisError> { ) -> Result<(), SynthesisError> {
let a_bits = a.to_bits(cs.ns(|| "a to bits"))?; let a_bits = a.to_bits(cs.ns(|| "a to bits"))?;
Boolean::enforce_smaller_or_equal_than::<_, _, ConstraintF, _>( Boolean::enforce_smaller_or_equal_than::<_, _, ConstraintF, _>(
cs.ns(|| "enforce smaller than modulus minus one div two"), cs.ns(|| "enforce smaller than modulus minus one div two"),
@@ -60,7 +65,13 @@ impl<ConstraintF: PrimeField> CmpGadget<ConstraintF> {
ordering: Ordering, ordering: Ordering,
should_also_check_equality: bool, should_also_check_equality: bool,
) -> Result<(), SynthesisError> { ) -> Result<(), SynthesisError> {
let (left, right) = Self::process_cmp_inputs(cs.ns(|| "process cmp inputs"), a, b, ordering, should_also_check_equality)?; let (left, right) = Self::process_cmp_inputs(
cs.ns(|| "process cmp inputs"),
a,
b,
ordering,
should_also_check_equality,
)?;
Self::enforce_smaller_than_unchecked(cs.ns(|| "enforce smaller than"), &left, &right) Self::enforce_smaller_than_unchecked(cs.ns(|| "enforce smaller than"), &left, &right)
} }
@@ -72,7 +83,13 @@ impl<ConstraintF: PrimeField> CmpGadget<ConstraintF> {
ordering: Ordering, ordering: Ordering,
should_also_check_equality: bool, should_also_check_equality: bool,
) -> Result<(), SynthesisError> { ) -> Result<(), SynthesisError> {
let (left, right) = Self::process_cmp_inputs(cs.ns(|| "process cmp inputs"), a, b, ordering, should_also_check_equality)?; let (left, right) = Self::process_cmp_inputs(
cs.ns(|| "process cmp inputs"),
a,
b,
ordering,
should_also_check_equality,
)?;
Self::enforce_smaller_than(cs.ns(|| "enforce smaller than"), &left, &right) Self::enforce_smaller_than(cs.ns(|| "enforce smaller than"), &left, &right)
} }
@@ -84,7 +101,13 @@ impl<ConstraintF: PrimeField> CmpGadget<ConstraintF> {
ordering: Ordering, ordering: Ordering,
should_also_check_equality: bool, should_also_check_equality: bool,
) -> Result<Boolean, SynthesisError> { ) -> Result<Boolean, SynthesisError> {
let (left, right) = Self::process_cmp_inputs(cs.ns(|| "process cmp inputs"), a, b, ordering, should_also_check_equality)?; let (left, right) = Self::process_cmp_inputs(
cs.ns(|| "process cmp inputs"),
a,
b,
ordering,
should_also_check_equality,
)?;
Self::is_smaller_than(cs.ns(|| "enforce smaller than"), &left, &right) Self::is_smaller_than(cs.ns(|| "enforce smaller than"), &left, &right)
} }
@@ -96,7 +119,13 @@ impl<ConstraintF: PrimeField> CmpGadget<ConstraintF> {
ordering: Ordering, ordering: Ordering,
should_also_check_equality: bool, should_also_check_equality: bool,
) -> Result<Boolean, SynthesisError> { ) -> Result<Boolean, SynthesisError> {
let (left, right) = Self::process_cmp_inputs(cs.ns(|| "process cmp inputs"), a, b, ordering, should_also_check_equality)?; let (left, right) = Self::process_cmp_inputs(
cs.ns(|| "process cmp inputs"),
a,
b,
ordering,
should_also_check_equality,
)?;
Self::is_smaller_than_unchecked(cs.ns(|| "enforce smaller than"), &left, &right) Self::is_smaller_than_unchecked(cs.ns(|| "enforce smaller than"), &left, &right)
} }

View File

@@ -60,21 +60,21 @@ pub use instantiated::jubjub;
pub mod pairing; pub mod pairing;
pub mod alloc; pub mod alloc;
pub mod cmp;
pub mod eq; pub mod eq;
pub mod select; pub mod select;
pub mod cmp;
pub mod prelude { pub mod prelude {
pub use crate::{ pub use crate::{
alloc::*, alloc::*,
bits::{boolean::Boolean, uint32::UInt32, uint8::UInt8, ToBitsGadget, ToBytesGadget}, bits::{boolean::Boolean, uint32::UInt32, uint8::UInt8, ToBitsGadget, ToBytesGadget},
cmp::CmpGadget,
eq::*, eq::*,
fields::FieldGadget, fields::FieldGadget,
groups::GroupGadget, groups::GroupGadget,
instantiated::*, instantiated::*,
pairing::PairingGadget, pairing::PairingGadget,
select::*, select::*,
cmp::CmpGadget,
}; };
} }