mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-27 12:17:35 +03:00
move comparator into leo-gadgets. impl cmp for i types
This commit is contained in:
parent
a1f2366fe8
commit
eb5ab1fbe1
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -580,6 +580,7 @@ dependencies = [
|
||||
"bincode",
|
||||
"hex",
|
||||
"leo-ast",
|
||||
"leo-gadgets",
|
||||
"leo-inputs",
|
||||
"leo-types",
|
||||
"log",
|
||||
|
@ -6,8 +6,9 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
leo-ast = { path = "../ast", version = "0.1.0" }
|
||||
leo-types = { path = "../types", version = "0.1.0" }
|
||||
leo-gadgets = { path = "../gadgets", version = "0.1.0" }
|
||||
leo-inputs = { path = "../leo-inputs", version = "0.1.0" }
|
||||
leo-types = { path = "../types", version = "0.1.0" }
|
||||
|
||||
snarkos-curves = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", rev = "c7a56d9", default-features = false }
|
||||
snarkos-dpc = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", rev = "c7a56d9", default-features = false }
|
||||
|
@ -1,6 +1,7 @@
|
||||
//! Enforces a relational `>=` operator in a resolved Leo program.
|
||||
|
||||
use crate::{comparator::ComparatorGadget, errors::ExpressionError, value::ConstrainedValue, GroupType};
|
||||
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
|
||||
use leo_gadgets::binary::ComparatorGadget;
|
||||
use leo_types::Span;
|
||||
|
||||
use snarkos_models::{
|
||||
|
@ -1,6 +1,7 @@
|
||||
//! Enforces a relational `>` operator in a resolved Leo program.
|
||||
|
||||
use crate::{comparator::ComparatorGadget, errors::ExpressionError, value::ConstrainedValue, GroupType};
|
||||
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
|
||||
use leo_gadgets::binary::ComparatorGadget;
|
||||
use leo_types::Span;
|
||||
|
||||
use snarkos_models::{
|
||||
|
@ -1,6 +1,7 @@
|
||||
//! Enforces a relational `<=` operator in a resolved Leo program.
|
||||
|
||||
use crate::{comparator::ComparatorGadget, errors::ExpressionError, value::ConstrainedValue, GroupType};
|
||||
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
|
||||
use leo_gadgets::binary::ComparatorGadget;
|
||||
use leo_types::Span;
|
||||
|
||||
use snarkos_models::{
|
||||
|
@ -1,6 +1,7 @@
|
||||
//! Enforces a relational `<` operator in a resolved Leo program.
|
||||
|
||||
use crate::{comparator::EvaluateLtGadget, errors::ExpressionError, value::ConstrainedValue, GroupType};
|
||||
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
|
||||
use leo_gadgets::binary::comparator::EvaluateLtGadget;
|
||||
use leo_types::Span;
|
||||
|
||||
use snarkos_models::{
|
||||
|
@ -1,9 +1,7 @@
|
||||
//! A data type that represents a field value
|
||||
|
||||
use crate::{
|
||||
comparator::{ComparatorGadget, EvaluateLtGadget},
|
||||
errors::FieldError,
|
||||
};
|
||||
use crate::errors::FieldError;
|
||||
use leo_gadgets::binary::{ComparatorGadget, EvaluateLtGadget};
|
||||
use leo_types::Span;
|
||||
|
||||
use snarkos_errors::gadgets::SynthesisError;
|
||||
|
@ -1,8 +1,6 @@
|
||||
//! Conversion of integer declarations to constraints in Leo.
|
||||
use crate::{
|
||||
comparator::{ComparatorGadget, EvaluateLtGadget},
|
||||
errors::IntegerError,
|
||||
};
|
||||
use crate::errors::IntegerError;
|
||||
use leo_gadgets::binary::comparator::{ComparatorGadget, EvaluateLtGadget};
|
||||
use leo_types::{InputValue, IntegerType, Span};
|
||||
|
||||
use snarkos_errors::gadgets::SynthesisError;
|
||||
|
@ -5,8 +5,6 @@ pub use self::address::*;
|
||||
|
||||
pub mod boolean;
|
||||
|
||||
pub(crate) mod comparator;
|
||||
|
||||
pub mod field;
|
||||
pub use self::field::*;
|
||||
|
||||
|
@ -2,5 +2,8 @@
|
||||
pub mod adder;
|
||||
pub use self::adder::*;
|
||||
|
||||
pub mod comparator;
|
||||
pub use self::comparator::*;
|
||||
|
||||
pub mod rca;
|
||||
pub use self::rca::*;
|
||||
|
70
gadgets/src/signed_integer/relational/lt.rs
Normal file
70
gadgets/src/signed_integer/relational/lt.rs
Normal file
@ -0,0 +1,70 @@
|
||||
use crate::{
|
||||
binary::{ComparatorGadget, EvaluateLtGadget},
|
||||
Int128,
|
||||
Int16,
|
||||
Int32,
|
||||
Int64,
|
||||
Int8,
|
||||
};
|
||||
|
||||
use snarkos_errors::gadgets::SynthesisError;
|
||||
use snarkos_models::{
|
||||
curves::PrimeField,
|
||||
gadgets::{r1cs::ConstraintSystem, utilities::boolean::Boolean},
|
||||
};
|
||||
|
||||
macro_rules! cmp_gadget_impl {
|
||||
($($gadget: ident)*) => ($(
|
||||
impl<F: PrimeField> EvaluateLtGadget<F> for $gadget {
|
||||
fn less_than<CS: ConstraintSystem<F>>(
|
||||
&self,
|
||||
mut cs: CS,
|
||||
other: &Self
|
||||
) -> Result<Boolean, SynthesisError> {
|
||||
|
||||
for (i, (a, b)) in self.bits
|
||||
.iter()
|
||||
.rev()
|
||||
.zip(other.bits.iter().rev())
|
||||
.enumerate()
|
||||
{
|
||||
let is_greater = if i == 0 {
|
||||
// Check sign bit
|
||||
// is_greater = !a_msb & b_msb
|
||||
// only true when a > b
|
||||
Boolean::and(cs.ns(|| format!("not a and b [{}]", i)), &a.not(), b)?
|
||||
} else {
|
||||
// is_greater = a & !b
|
||||
// only true when a > b
|
||||
Boolean::and(cs.ns(|| format!("a and not b [{}]", i)), a, &b.not())?
|
||||
};
|
||||
|
||||
let is_less = if i == 0 {
|
||||
// Check sign bit
|
||||
// is_less = a_msb & ! b_msb
|
||||
// only true when a < b
|
||||
Boolean::and(cs.ns(|| format!("a and not b [{}]", i)), a, &b.not())?
|
||||
} else {
|
||||
// is_less = !a & b
|
||||
// only true when a < b
|
||||
Boolean::and(cs.ns(|| format!("not a and b [{}]", i)), &a.not(), b)?
|
||||
};
|
||||
|
||||
if is_greater.get_value().unwrap() {
|
||||
return Ok(is_greater.not());
|
||||
} else if is_less.get_value().unwrap() {
|
||||
return Ok(is_less);
|
||||
} else if i == self.bits.len() - 1 {
|
||||
return Ok(is_less);
|
||||
}
|
||||
}
|
||||
|
||||
Err(SynthesisError::Unsatisfiable)
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: PrimeField> ComparatorGadget<F> for $gadget {}
|
||||
)*)
|
||||
}
|
||||
|
||||
cmp_gadget_impl!(Int8 Int16 Int32 Int64 Int128);
|
@ -1,2 +1,6 @@
|
||||
#[macro_use]
|
||||
pub mod eq;
|
||||
pub use self::eq::*;
|
||||
|
||||
pub mod lt;
|
||||
pub use self::lt::*;
|
||||
|
Loading…
Reference in New Issue
Block a user