make expression module methods public

This commit is contained in:
collin 2020-07-07 23:32:49 -07:00
parent 6de9e67282
commit ada42fdcf4
15 changed files with 326 additions and 376 deletions

View File

@ -1,6 +1,6 @@
//! Methods to enforce arithmetic addition in a compiled Leo program.
use crate::{errors::ExpressionError, program::ConstrainedProgram, value::ConstrainedValue, GroupType};
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
use leo_types::Span;
use snarkos_models::{
@ -8,37 +8,33 @@ use snarkos_models::{
gadgets::r1cs::ConstraintSystem,
};
impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
/// Enforce numerical operations
pub fn enforce_add_expression<CS: ConstraintSystem<F>>(
&mut self,
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
match (left, right) {
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
Ok(ConstrainedValue::Integer(num_1.add(cs, num_2, span)?))
}
(ConstrainedValue::Field(field_1), ConstrainedValue::Field(field_2)) => {
Ok(ConstrainedValue::Field(field_1.add(cs, &field_2, span)?))
}
(ConstrainedValue::Group(point_1), ConstrainedValue::Group(point_2)) => {
Ok(ConstrainedValue::Group(point_1.add(cs, &point_2, span)?))
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?;
self.enforce_add_expression(cs, val_1, val_2, span)
}
(val_1, ConstrainedValue::Unresolved(string)) => {
let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?;
self.enforce_add_expression(cs, val_1, val_2, span)
}
(val_1, val_2) => Err(ExpressionError::incompatible_types(
format!("{} + {}", val_1, val_2),
span,
)),
pub fn enforce_add_expression<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
match (left, right) {
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
Ok(ConstrainedValue::Integer(num_1.add(cs, num_2, span)?))
}
(ConstrainedValue::Field(field_1), ConstrainedValue::Field(field_2)) => {
Ok(ConstrainedValue::Field(field_1.add(cs, &field_2, span)?))
}
(ConstrainedValue::Group(point_1), ConstrainedValue::Group(point_2)) => {
Ok(ConstrainedValue::Group(point_1.add(cs, &point_2, span)?))
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?;
enforce_add_expression(cs, val_1, val_2, span)
}
(val_1, ConstrainedValue::Unresolved(string)) => {
let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?;
enforce_add_expression(cs, val_1, val_2, span)
}
(val_1, val_2) => Err(ExpressionError::incompatible_types(
format!("{} + {}", val_1, val_2),
span,
)),
}
}

View File

@ -1,6 +1,6 @@
//! Methods to enforce arithmetic division in a compiled Leo program.
use crate::{errors::ExpressionError, program::ConstrainedProgram, value::ConstrainedValue, GroupType};
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
use leo_types::Span;
use snarkos_models::{
@ -8,35 +8,32 @@ use snarkos_models::{
gadgets::r1cs::ConstraintSystem,
};
impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
pub fn enforce_div_expression<CS: ConstraintSystem<F>>(
&mut self,
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
match (left, right) {
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
Ok(ConstrainedValue::Integer(num_1.div(cs, num_2, span)?))
}
(ConstrainedValue::Field(field_1), ConstrainedValue::Field(field_2)) => {
Ok(ConstrainedValue::Field(field_1.div(cs, &field_2, span)?))
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?;
self.enforce_div_expression(cs, val_1, val_2, span)
}
(val_1, ConstrainedValue::Unresolved(string)) => {
let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?;
self.enforce_div_expression(cs, val_1, val_2, span)
}
(val_1, val_2) => {
return Err(ExpressionError::incompatible_types(
format!("{} / {}", val_1, val_2,),
span,
));
}
pub fn enforce_div_expression<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
match (left, right) {
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
Ok(ConstrainedValue::Integer(num_1.div(cs, num_2, span)?))
}
(ConstrainedValue::Field(field_1), ConstrainedValue::Field(field_2)) => {
Ok(ConstrainedValue::Field(field_1.div(cs, &field_2, span)?))
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?;
enforce_div_expression(cs, val_1, val_2, span)
}
(val_1, ConstrainedValue::Unresolved(string)) => {
let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?;
enforce_div_expression(cs, val_1, val_2, span)
}
(val_1, val_2) => {
return Err(ExpressionError::incompatible_types(
format!("{} / {}", val_1, val_2,),
span,
));
}
}
}

View File

@ -1,6 +1,6 @@
//! Methods to enforce arithmetic multiplication in a compiled Leo program.
use crate::{errors::ExpressionError, program::ConstrainedProgram, value::ConstrainedValue, GroupType};
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
use leo_types::Span;
use snarkos_models::{
@ -8,35 +8,32 @@ use snarkos_models::{
gadgets::r1cs::ConstraintSystem,
};
impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
pub fn enforce_mul_expression<CS: ConstraintSystem<F>>(
&mut self,
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
match (left, right) {
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
Ok(ConstrainedValue::Integer(num_1.mul(cs, num_2, span)?))
}
(ConstrainedValue::Field(field_1), ConstrainedValue::Field(field_2)) => {
Ok(ConstrainedValue::Field(field_1.mul(cs, &field_2, span)?))
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?;
self.enforce_mul_expression(cs, val_1, val_2, span)
}
(val_1, ConstrainedValue::Unresolved(string)) => {
let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?;
self.enforce_mul_expression(cs, val_1, val_2, span)
}
(val_1, val_2) => {
return Err(ExpressionError::incompatible_types(
format!("{} * {}", val_1, val_2),
span,
));
}
pub fn enforce_mul_expression<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
match (left, right) {
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
Ok(ConstrainedValue::Integer(num_1.mul(cs, num_2, span)?))
}
(ConstrainedValue::Field(field_1), ConstrainedValue::Field(field_2)) => {
Ok(ConstrainedValue::Field(field_1.mul(cs, &field_2, span)?))
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?;
enforce_mul_expression(cs, val_1, val_2, span)
}
(val_1, ConstrainedValue::Unresolved(string)) => {
let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?;
enforce_mul_expression(cs, val_1, val_2, span)
}
(val_1, val_2) => {
return Err(ExpressionError::incompatible_types(
format!("{} * {}", val_1, val_2),
span,
));
}
}
}

View File

@ -1,6 +1,6 @@
//! Methods to enforce arithmetic exponentiation in a compiled Leo program.
use crate::{errors::ExpressionError, program::ConstrainedProgram, value::ConstrainedValue, GroupType};
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
use leo_types::Span;
use snarkos_models::{
@ -8,30 +8,27 @@ use snarkos_models::{
gadgets::r1cs::ConstraintSystem,
};
impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
pub fn enforce_pow_expression<CS: ConstraintSystem<F>>(
&mut self,
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
match (left, right) {
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
Ok(ConstrainedValue::Integer(num_1.pow(cs, num_2, span)?))
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?;
self.enforce_pow_expression(cs, val_1, val_2, span)
}
(val_1, ConstrainedValue::Unresolved(string)) => {
let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?;
self.enforce_pow_expression(cs, val_1, val_2, span)
}
(val_1, val_2) => Err(ExpressionError::incompatible_types(
format!("{} ** {}", val_1, val_2,),
span,
)),
pub fn enforce_pow_expression<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
match (left, right) {
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
Ok(ConstrainedValue::Integer(num_1.pow(cs, num_2, span)?))
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?;
enforce_pow_expression(cs, val_1, val_2, span)
}
(val_1, ConstrainedValue::Unresolved(string)) => {
let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?;
enforce_pow_expression(cs, val_1, val_2, span)
}
(val_1, val_2) => Err(ExpressionError::incompatible_types(
format!("{} ** {}", val_1, val_2,),
span,
)),
}
}

View File

@ -1,6 +1,6 @@
//! Methods to enforce arithmetic subtraction in a compiled Leo program.
use crate::{errors::ExpressionError, program::ConstrainedProgram, value::ConstrainedValue, GroupType};
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
use leo_types::Span;
use snarkos_models::{
@ -8,36 +8,33 @@ use snarkos_models::{
gadgets::r1cs::ConstraintSystem,
};
impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
pub fn enforce_sub_expression<CS: ConstraintSystem<F>>(
&mut self,
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
match (left, right) {
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
Ok(ConstrainedValue::Integer(num_1.sub(cs, num_2, span)?))
}
(ConstrainedValue::Field(field_1), ConstrainedValue::Field(field_2)) => {
Ok(ConstrainedValue::Field(field_1.sub(cs, &field_2, span)?))
}
(ConstrainedValue::Group(point_1), ConstrainedValue::Group(point_2)) => {
Ok(ConstrainedValue::Group(point_1.sub(cs, &point_2, span)?))
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?;
self.enforce_sub_expression(cs, val_1, val_2, span)
}
(val_1, ConstrainedValue::Unresolved(string)) => {
let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?;
self.enforce_sub_expression(cs, val_1, val_2, span)
}
(val_1, val_2) => Err(ExpressionError::incompatible_types(
format!("{} - {}", val_1, val_2),
span,
)),
pub fn enforce_sub_expression<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
match (left, right) {
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
Ok(ConstrainedValue::Integer(num_1.sub(cs, num_2, span)?))
}
(ConstrainedValue::Field(field_1), ConstrainedValue::Field(field_2)) => {
Ok(ConstrainedValue::Field(field_1.sub(cs, &field_2, span)?))
}
(ConstrainedValue::Group(point_1), ConstrainedValue::Group(point_2)) => {
Ok(ConstrainedValue::Group(point_1.sub(cs, &point_2, span)?))
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?;
enforce_sub_expression(cs, val_1, val_2, span)
}
(val_1, ConstrainedValue::Unresolved(string)) => {
let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?;
enforce_sub_expression(cs, val_1, val_2, span)
}
(val_1, val_2) => Err(ExpressionError::incompatible_types(
format!("{} - {}", val_1, val_2),
span,
)),
}
}

View File

@ -1,9 +1,11 @@
//! Methods to enforce constraints on expressions in a compiled Leo program.
use crate::{
arithmetic::*,
errors::ExpressionError,
logical::{and::enforce_and, not::evaluate_not, or::enforce_or},
logical::*,
program::{new_scope, ConstrainedProgram},
relational::*,
value::{boolean::input::new_bool_constant, ConstrainedCircuitMember, ConstrainedValue},
Address,
FieldType,
@ -586,7 +588,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
span.clone(),
)?;
self.enforce_add_expression(cs, resolved_left, resolved_right, span)
enforce_add_expression(cs, resolved_left, resolved_right, span)
}
Expression::Sub(left, right, span) => {
let (resolved_left, resolved_right) = self.enforce_binary_expression(
@ -599,7 +601,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
span.clone(),
)?;
self.enforce_sub_expression(cs, resolved_left, resolved_right, span)
enforce_sub_expression(cs, resolved_left, resolved_right, span)
}
Expression::Mul(left, right, span) => {
let (resolved_left, resolved_right) = self.enforce_binary_expression(
@ -612,7 +614,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
span.clone(),
)?;
self.enforce_mul_expression(cs, resolved_left, resolved_right, span)
enforce_mul_expression(cs, resolved_left, resolved_right, span)
}
Expression::Div(left, right, span) => {
let (resolved_left, resolved_right) = self.enforce_binary_expression(
@ -625,7 +627,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
span.clone(),
)?;
self.enforce_div_expression(cs, resolved_left, resolved_right, span)
enforce_div_expression(cs, resolved_left, resolved_right, span)
}
Expression::Pow(left, right, span) => {
let (resolved_left, resolved_right) = self.enforce_binary_expression(
@ -638,7 +640,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
span.clone(),
)?;
self.enforce_pow_expression(cs, resolved_left, resolved_right, span)
enforce_pow_expression(cs, resolved_left, resolved_right, span)
}
// Boolean operations
@ -683,7 +685,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
span.clone(),
)?;
Ok(self.evaluate_eq_expression(cs, resolved_left, resolved_right, span)?)
Ok(evaluate_eq_expression(cs, resolved_left, resolved_right, span)?)
}
Expression::Ge(left, right, span) => {
let (resolved_left, resolved_right) = self.enforce_binary_expression(
@ -696,7 +698,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
span.clone(),
)?;
Ok(self.evaluate_ge_expression(cs, resolved_left, resolved_right, span)?)
Ok(evaluate_ge_expression(cs, resolved_left, resolved_right, span)?)
}
Expression::Gt(left, right, span) => {
let (resolved_left, resolved_right) = self.enforce_binary_expression(
@ -709,7 +711,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
span.clone(),
)?;
Ok(self.evaluate_gt_expression(cs, resolved_left, resolved_right, span)?)
Ok(evaluate_gt_expression(cs, resolved_left, resolved_right, span)?)
}
Expression::Le(left, right, span) => {
let (resolved_left, resolved_right) = self.enforce_binary_expression(
@ -722,7 +724,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
span.clone(),
)?;
Ok(self.evaluate_le_expression(cs, resolved_left, resolved_right, span)?)
Ok(evaluate_le_expression(cs, resolved_left, resolved_right, span)?)
}
Expression::Lt(left, right, span) => {
let (resolved_left, resolved_right) = self.enforce_binary_expression(
@ -735,7 +737,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
span.clone(),
)?;
Ok(self.evaluate_lt_expression(cs, resolved_left, resolved_right, span)?)
Ok(evaluate_lt_expression(cs, resolved_left, resolved_right, span)?)
}
// Conditionals

View File

@ -8,7 +8,7 @@ use snarkos_models::{
gadgets::{r1cs::ConstraintSystem, utilities::boolean::Boolean},
};
pub(crate) fn enforce_and<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
pub fn enforce_and<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,

View File

@ -1,5 +1,10 @@
//! Methods to enforce logical expressions in a compiled Leo program.
pub mod and;
pub use self::and::*;
pub mod not;
pub use self::not::*;
pub mod or;
pub use self::or::*;

View File

@ -5,7 +5,7 @@ use leo_types::Span;
use snarkos_models::curves::{Field, PrimeField};
pub(crate) fn evaluate_not<F: Field + PrimeField, G: GroupType<F>>(
pub fn evaluate_not<F: Field + PrimeField, G: GroupType<F>>(
value: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, BooleanError> {

View File

@ -8,7 +8,7 @@ use snarkos_models::{
gadgets::{r1cs::ConstraintSystem, utilities::boolean::Boolean},
};
pub(crate) fn enforce_or<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
pub fn enforce_or<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,

View File

@ -1,6 +1,6 @@
//! Methods to enforce constraints on `==` comparison expressions in a compiled Leo program.
use crate::{errors::ExpressionError, program::ConstrainedProgram, value::ConstrainedValue, GroupType};
use crate::{errors::ExpressionError, value::ConstrainedValue, GroupType};
use leo_types::Span;
use snarkos_models::{
@ -8,51 +8,46 @@ use snarkos_models::{
gadgets::{r1cs::ConstraintSystem, utilities::eq::EvaluateEqGadget},
};
impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
/// Evaluate Boolean operations
pub fn evaluate_eq_expression<CS: ConstraintSystem<F>>(
&mut self,
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
let mut unique_namespace = cs.ns(|| format!("evaluate {} == {} {}:{}", left, right, span.line, span.start));
let constraint_result = match (left, right) {
(ConstrainedValue::Address(address_1), ConstrainedValue::Address(address_2)) => {
address_1.evaluate_equal(unique_namespace, &address_2)
}
(ConstrainedValue::Boolean(bool_1), ConstrainedValue::Boolean(bool_2)) => {
bool_1.evaluate_equal(unique_namespace, &bool_2)
}
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
num_1.evaluate_equal(unique_namespace, &num_2)
}
(ConstrainedValue::Field(field_1), ConstrainedValue::Field(field_2)) => {
field_1.evaluate_equal(unique_namespace, &field_2)
}
(ConstrainedValue::Group(point_1), ConstrainedValue::Group(point_2)) => {
point_1.evaluate_equal(unique_namespace, &point_2)
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?;
return self.evaluate_eq_expression(&mut unique_namespace, val_1, val_2, span);
}
(val_1, ConstrainedValue::Unresolved(string)) => {
let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?;
return self.evaluate_eq_expression(&mut unique_namespace, val_1, val_2, span);
}
(val_1, val_2) => {
return Err(ExpressionError::incompatible_types(
format!("{} == {}", val_1, val_2,),
span,
));
}
};
pub fn evaluate_eq_expression<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
let mut unique_namespace = cs.ns(|| format!("evaluate {} == {} {}:{}", left, right, span.line, span.start));
let constraint_result = match (left, right) {
(ConstrainedValue::Address(address_1), ConstrainedValue::Address(address_2)) => {
address_1.evaluate_equal(unique_namespace, &address_2)
}
(ConstrainedValue::Boolean(bool_1), ConstrainedValue::Boolean(bool_2)) => {
bool_1.evaluate_equal(unique_namespace, &bool_2)
}
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
num_1.evaluate_equal(unique_namespace, &num_2)
}
(ConstrainedValue::Field(field_1), ConstrainedValue::Field(field_2)) => {
field_1.evaluate_equal(unique_namespace, &field_2)
}
(ConstrainedValue::Group(point_1), ConstrainedValue::Group(point_2)) => {
point_1.evaluate_equal(unique_namespace, &point_2)
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?;
return evaluate_eq_expression(&mut unique_namespace, val_1, val_2, span);
}
(val_1, ConstrainedValue::Unresolved(string)) => {
let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?;
return evaluate_eq_expression(&mut unique_namespace, val_1, val_2, span);
}
(val_1, val_2) => {
return Err(ExpressionError::incompatible_types(
format!("{} == {}", val_1, val_2,),
span,
));
}
};
let boolean =
constraint_result.map_err(|e| ExpressionError::cannot_enforce(format!("evaluate equal"), e, span))?;
let boolean = constraint_result.map_err(|e| ExpressionError::cannot_enforce(format!("evaluate equal"), e, span))?;
Ok(ConstrainedValue::Boolean(boolean))
}
Ok(ConstrainedValue::Boolean(boolean))
}

View File

@ -1,12 +1,6 @@
//! Methods to enforce constraints on `>=` comparison expressions in a compiled Leo program.
use crate::{
comparator::ComparatorGadget,
errors::ExpressionError,
program::ConstrainedProgram,
value::ConstrainedValue,
GroupType,
};
use crate::{comparator::ComparatorGadget, errors::ExpressionError, value::ConstrainedValue, GroupType};
use leo_types::Span;
use snarkos_models::{
@ -14,41 +8,38 @@ use snarkos_models::{
gadgets::r1cs::ConstraintSystem,
};
impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
pub fn evaluate_ge_expression<CS: ConstraintSystem<F>>(
&mut self,
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
let mut unique_namespace = cs.ns(|| format!("evaluate {} >= {} {}:{}", left, right, span.line, span.start));
let constraint_result = match (left, right) {
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
num_1.greater_than_or_equal(unique_namespace, &num_2)
}
(ConstrainedValue::Field(field_1), ConstrainedValue::Field(field_2)) => {
field_1.greater_than_or_equal(unique_namespace, &field_2)
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?;
return self.evaluate_ge_expression(&mut unique_namespace, val_1, val_2, span);
}
(val_1, ConstrainedValue::Unresolved(string)) => {
let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?;
return self.evaluate_ge_expression(&mut unique_namespace, val_1, val_2, span);
}
(val_1, val_2) => {
return Err(ExpressionError::incompatible_types(
format!("{} >= {}", val_1, val_2),
span,
));
}
};
pub fn evaluate_ge_expression<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
let mut unique_namespace = cs.ns(|| format!("evaluate {} >= {} {}:{}", left, right, span.line, span.start));
let constraint_result = match (left, right) {
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
num_1.greater_than_or_equal(unique_namespace, &num_2)
}
(ConstrainedValue::Field(field_1), ConstrainedValue::Field(field_2)) => {
field_1.greater_than_or_equal(unique_namespace, &field_2)
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?;
return evaluate_ge_expression(&mut unique_namespace, val_1, val_2, span);
}
(val_1, ConstrainedValue::Unresolved(string)) => {
let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?;
return evaluate_ge_expression(&mut unique_namespace, val_1, val_2, span);
}
(val_1, val_2) => {
return Err(ExpressionError::incompatible_types(
format!("{} >= {}", val_1, val_2),
span,
));
}
};
let boolean = constraint_result
.map_err(|e| ExpressionError::cannot_enforce(format!("evaluate greater than or equal"), e, span))?;
let boolean = constraint_result
.map_err(|e| ExpressionError::cannot_enforce(format!("evaluate greater than or equal"), e, span))?;
Ok(ConstrainedValue::Boolean(boolean))
}
Ok(ConstrainedValue::Boolean(boolean))
}

View File

@ -1,12 +1,6 @@
//! Methods to enforce constraints on `>` comparison expressions in a compiled Leo program.
use crate::{
comparator::ComparatorGadget,
errors::ExpressionError,
program::ConstrainedProgram,
value::ConstrainedValue,
GroupType,
};
use crate::{comparator::ComparatorGadget, errors::ExpressionError, value::ConstrainedValue, GroupType};
use leo_types::Span;
use snarkos_models::{
@ -14,41 +8,38 @@ use snarkos_models::{
gadgets::r1cs::ConstraintSystem,
};
impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
pub fn evaluate_gt_expression<CS: ConstraintSystem<F>>(
&mut self,
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
let mut unique_namespace = cs.ns(|| format!("evaluate {} > {} {}:{}", left, right, span.line, span.start));
let constraint_result = match (left, right) {
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
num_1.greater_than(unique_namespace, &num_2)
}
(ConstrainedValue::Field(field_1), ConstrainedValue::Field(field_2)) => {
field_1.greater_than(unique_namespace, &field_2)
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?;
return self.evaluate_gt_expression(&mut unique_namespace, val_1, val_2, span);
}
(val_1, ConstrainedValue::Unresolved(string)) => {
let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?;
return self.evaluate_gt_expression(&mut unique_namespace, val_1, val_2, span);
}
(val_1, val_2) => {
return Err(ExpressionError::incompatible_types(
format!("{} > {}", val_1, val_2),
span,
));
}
};
pub fn evaluate_gt_expression<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
let mut unique_namespace = cs.ns(|| format!("evaluate {} > {} {}:{}", left, right, span.line, span.start));
let constraint_result = match (left, right) {
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
num_1.greater_than(unique_namespace, &num_2)
}
(ConstrainedValue::Field(field_1), ConstrainedValue::Field(field_2)) => {
field_1.greater_than(unique_namespace, &field_2)
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?;
return evaluate_gt_expression(&mut unique_namespace, val_1, val_2, span);
}
(val_1, ConstrainedValue::Unresolved(string)) => {
let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?;
return evaluate_gt_expression(&mut unique_namespace, val_1, val_2, span);
}
(val_1, val_2) => {
return Err(ExpressionError::incompatible_types(
format!("{} > {}", val_1, val_2),
span,
));
}
};
let boolean = constraint_result
.map_err(|e| ExpressionError::cannot_enforce(format!("evaluate greater than"), e, span))?;
let boolean =
constraint_result.map_err(|e| ExpressionError::cannot_enforce(format!("evaluate greater than"), e, span))?;
Ok(ConstrainedValue::Boolean(boolean))
}
Ok(ConstrainedValue::Boolean(boolean))
}

View File

@ -1,12 +1,6 @@
//! Methods to enforce constraints on `<=` comparison expressions in a compiled Leo program.
use crate::{
comparator::ComparatorGadget,
errors::ExpressionError,
program::ConstrainedProgram,
value::ConstrainedValue,
GroupType,
};
use crate::{comparator::ComparatorGadget, errors::ExpressionError, value::ConstrainedValue, GroupType};
use leo_types::Span;
use snarkos_models::{
@ -14,41 +8,38 @@ use snarkos_models::{
gadgets::r1cs::ConstraintSystem,
};
impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
pub fn evaluate_le_expression<CS: ConstraintSystem<F>>(
&mut self,
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
let mut unique_namespace = cs.ns(|| format!("evaluate {} <= {} {}:{}", left, right, span.line, span.start));
let constraint_result = match (left, right) {
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
num_1.less_than_or_equal(unique_namespace, &num_2)
}
(ConstrainedValue::Field(field_1), ConstrainedValue::Field(field_2)) => {
field_1.less_than_or_equal(unique_namespace, &field_2)
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?;
return self.evaluate_le_expression(&mut unique_namespace, val_1, val_2, span);
}
(val_1, ConstrainedValue::Unresolved(string)) => {
let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?;
return self.evaluate_le_expression(&mut unique_namespace, val_1, val_2, span);
}
(val_1, val_2) => {
return Err(ExpressionError::incompatible_types(
format!("{} <= {}", val_1, val_2),
span,
));
}
};
pub fn evaluate_le_expression<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
let mut unique_namespace = cs.ns(|| format!("evaluate {} <= {} {}:{}", left, right, span.line, span.start));
let constraint_result = match (left, right) {
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
num_1.less_than_or_equal(unique_namespace, &num_2)
}
(ConstrainedValue::Field(field_1), ConstrainedValue::Field(field_2)) => {
field_1.less_than_or_equal(unique_namespace, &field_2)
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?;
return evaluate_le_expression(&mut unique_namespace, val_1, val_2, span);
}
(val_1, ConstrainedValue::Unresolved(string)) => {
let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?;
return evaluate_le_expression(&mut unique_namespace, val_1, val_2, span);
}
(val_1, val_2) => {
return Err(ExpressionError::incompatible_types(
format!("{} <= {}", val_1, val_2),
span,
));
}
};
let boolean = constraint_result
.map_err(|e| ExpressionError::cannot_enforce(format!("evaluate less than or equal"), e, span))?;
let boolean = constraint_result
.map_err(|e| ExpressionError::cannot_enforce(format!("evaluate less than or equal"), e, span))?;
Ok(ConstrainedValue::Boolean(boolean))
}
Ok(ConstrainedValue::Boolean(boolean))
}

View File

@ -1,12 +1,6 @@
//! Methods to enforce constraints on `<` comparison expressions in a compiled Leo program.
use crate::{
comparator::EvaluateLtGadget,
errors::ExpressionError,
program::ConstrainedProgram,
value::ConstrainedValue,
GroupType,
};
use crate::{comparator::EvaluateLtGadget, errors::ExpressionError, value::ConstrainedValue, GroupType};
use leo_types::Span;
use snarkos_models::{
@ -14,41 +8,38 @@ use snarkos_models::{
gadgets::r1cs::ConstraintSystem,
};
impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
pub fn evaluate_lt_expression<CS: ConstraintSystem<F>>(
&mut self,
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
let mut unique_namespace = cs.ns(|| format!("evaluate {} < {} {}:{}", left, right, span.line, span.start));
let constraint_result = match (left, right) {
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
num_1.less_than(unique_namespace, &num_2)
}
(ConstrainedValue::Field(field_1), ConstrainedValue::Field(field_2)) => {
field_1.less_than(unique_namespace, &field_2)
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?;
return self.evaluate_lt_expression(&mut unique_namespace, val_1, val_2, span);
}
(val_1, ConstrainedValue::Unresolved(string)) => {
let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?;
return self.evaluate_lt_expression(&mut unique_namespace, val_1, val_2, span);
}
(val_1, val_2) => {
return Err(ExpressionError::incompatible_types(
format!("{} < {}", val_1, val_2),
span,
));
}
};
pub fn evaluate_lt_expression<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
cs: &mut CS,
left: ConstrainedValue<F, G>,
right: ConstrainedValue<F, G>,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
let mut unique_namespace = cs.ns(|| format!("evaluate {} < {} {}:{}", left, right, span.line, span.start));
let constraint_result = match (left, right) {
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
num_1.less_than(unique_namespace, &num_2)
}
(ConstrainedValue::Field(field_1), ConstrainedValue::Field(field_2)) => {
field_1.less_than(unique_namespace, &field_2)
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?;
return evaluate_lt_expression(&mut unique_namespace, val_1, val_2, span);
}
(val_1, ConstrainedValue::Unresolved(string)) => {
let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?;
return evaluate_lt_expression(&mut unique_namespace, val_1, val_2, span);
}
(val_1, val_2) => {
return Err(ExpressionError::incompatible_types(
format!("{} < {}", val_1, val_2),
span,
));
}
};
let boolean =
constraint_result.map_err(|e| ExpressionError::cannot_enforce(format!("evaluate less than"), e, span))?;
let boolean =
constraint_result.map_err(|e| ExpressionError::cannot_enforce(format!("evaluate less than"), e, span))?;
Ok(ConstrainedValue::Boolean(boolean))
}
Ok(ConstrainedValue::Boolean(boolean))
}