create boolean module

This commit is contained in:
collin 2020-07-07 19:26:01 -07:00
parent e98c344f76
commit 933775fe95
6 changed files with 73 additions and 56 deletions

View File

@ -5,7 +5,10 @@ use crate::{
constraints::{new_scope, ConstrainedProgram},
errors::ExpressionError,
value::{
boolean::{enforce_and, enforce_or, evaluate_not, new_bool_constant},
boolean::{
input::new_bool_constant,
operation::{enforce_and, enforce_or, evaluate_not},
},
ConstrainedCircuitMember,
ConstrainedValue,
},

View File

@ -5,7 +5,7 @@ use crate::{
address::Address,
constraints::{field::field_from_input, group::group_from_input, new_scope, ConstrainedProgram},
errors::{FunctionError, StatementError},
value::{boolean::bool_from_input, ConstrainedValue},
value::{boolean::input::bool_from_input, ConstrainedValue},
GroupType,
Integer,
};

View File

@ -0,0 +1,59 @@
//! Methods to enforce constraints on an input boolean value in a resolved Leo program.
use crate::{errors::BooleanError, value::ConstrainedValue, GroupType};
use leo_types::{InputValue, Span};
use snarkos_errors::gadgets::SynthesisError;
use snarkos_models::{
curves::{Field, PrimeField},
gadgets::{
r1cs::ConstraintSystem,
utilities::{alloc::AllocGadget, boolean::Boolean},
},
};
pub(crate) fn new_bool_constant(string: String, span: Span) -> Result<Boolean, BooleanError> {
let boolean = string
.parse::<bool>()
.map_err(|_| BooleanError::invalid_boolean(string, span))?;
Ok(Boolean::constant(boolean))
}
pub(crate) fn allocate_bool<F: Field + PrimeField, CS: ConstraintSystem<F>>(
cs: &mut CS,
name: String,
option: Option<bool>,
span: Span,
) -> Result<Boolean, BooleanError> {
let boolean_name = format!("{}: bool", name);
let boolean_name_unique = format!("`{}` {}:{}", boolean_name, span.line, span.start);
Boolean::alloc(cs.ns(|| boolean_name_unique), || {
option.ok_or(SynthesisError::AssignmentMissing)
})
.map_err(|_| BooleanError::missing_boolean(boolean_name, span))
}
pub(crate) fn bool_from_input<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
cs: &mut CS,
name: String,
input_value: Option<InputValue>,
span: Span,
) -> Result<ConstrainedValue<F, G>, BooleanError> {
// Check that the input value is the correct type
let option = match input_value {
Some(input) => {
if let InputValue::Boolean(bool) = input {
Some(bool)
} else {
return Err(BooleanError::invalid_boolean(name, span));
}
}
None => None,
};
let number = allocate_bool(cs, name, option, span)?;
Ok(ConstrainedValue::Boolean(number))
}

View File

@ -0,0 +1,5 @@
//! Methods to enforce constraints on booleans in a resolved Leo program.
pub mod operation;
pub mod input;

View File

@ -1,63 +1,13 @@
//! Methods to enforce constraints on booleans in a resolved Leo program.
//! Methods to enforce constraints on boolean operations in a resolved Leo program.
use crate::{errors::BooleanError, value::ConstrainedValue, GroupType};
use leo_types::{InputValue, Span};
use leo_types::Span;
use snarkos_errors::gadgets::SynthesisError;
use snarkos_models::{
curves::{Field, PrimeField},
gadgets::{
r1cs::ConstraintSystem,
utilities::{alloc::AllocGadget, boolean::Boolean},
},
gadgets::{r1cs::ConstraintSystem, utilities::boolean::Boolean},
};
pub(crate) fn new_bool_constant(string: String, span: Span) -> Result<Boolean, BooleanError> {
let boolean = string
.parse::<bool>()
.map_err(|_| BooleanError::invalid_boolean(string, span))?;
Ok(Boolean::constant(boolean))
}
pub(crate) fn allocate_bool<F: Field + PrimeField, CS: ConstraintSystem<F>>(
cs: &mut CS,
name: String,
option: Option<bool>,
span: Span,
) -> Result<Boolean, BooleanError> {
let boolean_name = format!("{}: bool", name);
let boolean_name_unique = format!("`{}` {}:{}", boolean_name, span.line, span.start);
Boolean::alloc(cs.ns(|| boolean_name_unique), || {
option.ok_or(SynthesisError::AssignmentMissing)
})
.map_err(|_| BooleanError::missing_boolean(boolean_name, span))
}
pub(crate) fn bool_from_input<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
cs: &mut CS,
name: String,
input_value: Option<InputValue>,
span: Span,
) -> Result<ConstrainedValue<F, G>, BooleanError> {
// Check that the input value is the correct type
let option = match input_value {
Some(input) => {
if let InputValue::Boolean(bool) = input {
Some(bool)
} else {
return Err(BooleanError::invalid_boolean(name, span));
}
}
None => None,
};
let number = allocate_bool(cs, name, option, span)?;
Ok(ConstrainedValue::Boolean(number))
}
pub(crate) fn evaluate_not<F: Field + PrimeField, G: GroupType<F>>(
value: ConstrainedValue<F, G>,
span: Span,

View File

@ -1,7 +1,7 @@
//! The in memory stored value for a defined name in a resolved Leo program.
use crate::{
boolean::{allocate_bool, new_bool_constant},
boolean::input::{allocate_bool, new_bool_constant},
errors::{ExpressionError, FieldError, ValueError},
is_in_scope,
new_scope,