mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-28 09:02:58 +03:00
add unique namespaces to statement gadget calls
This commit is contained in:
parent
9f27d22486
commit
cff3be9d01
@ -20,6 +20,21 @@ pub(crate) fn new_bool_constant(string: String, span: Span) -> Result<Boolean, B
|
||||
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,
|
||||
@ -27,7 +42,7 @@ pub(crate) fn bool_from_input<F: Field + PrimeField, G: GroupType<F>, CS: Constr
|
||||
span: Span,
|
||||
) -> Result<ConstrainedValue<F, G>, BooleanError> {
|
||||
// Check that the input value is the correct type
|
||||
let bool_value = match input_value {
|
||||
let option = match input_value {
|
||||
Some(input) => {
|
||||
if let InputValue::Boolean(bool) = input {
|
||||
Some(bool)
|
||||
@ -38,12 +53,7 @@ pub(crate) fn bool_from_input<F: Field + PrimeField, G: GroupType<F>, CS: Constr
|
||||
None => None,
|
||||
};
|
||||
|
||||
let boolean_name = format!("{}: bool", name);
|
||||
let boolean_name_unique = format!("`{}` {}:{}", boolean_name, span.line, span.start);
|
||||
let number = Boolean::alloc(cs.ns(|| boolean_name_unique), || {
|
||||
bool_value.ok_or(SynthesisError::AssignmentMissing)
|
||||
})
|
||||
.map_err(|_| BooleanError::missing_boolean(boolean_name, span))?;
|
||||
let number = allocate_bool(cs, name, option, span)?;
|
||||
|
||||
Ok(ConstrainedValue::Boolean(number))
|
||||
}
|
||||
|
@ -9,6 +9,21 @@ use snarkos_models::{
|
||||
gadgets::{r1cs::ConstraintSystem, utilities::alloc::AllocGadget},
|
||||
};
|
||||
|
||||
pub(crate) fn allocate_field<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
cs: &mut CS,
|
||||
name: String,
|
||||
option: Option<String>,
|
||||
span: Span,
|
||||
) -> Result<FieldType<F>, FieldError> {
|
||||
let field_name = format!("{}: field", name);
|
||||
let field_name_unique = format!("`{}` {}:{}", field_name, span.line, span.start);
|
||||
|
||||
FieldType::alloc(cs.ns(|| field_name_unique), || {
|
||||
option.ok_or(SynthesisError::AssignmentMissing)
|
||||
})
|
||||
.map_err(|_| FieldError::missing_field(field_name, span))
|
||||
}
|
||||
|
||||
pub(crate) fn field_from_input<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
|
||||
cs: &mut CS,
|
||||
name: String,
|
||||
@ -16,7 +31,7 @@ pub(crate) fn field_from_input<F: Field + PrimeField, G: GroupType<F>, CS: Const
|
||||
span: Span,
|
||||
) -> Result<ConstrainedValue<F, G>, FieldError> {
|
||||
// Check that the parameter value is the correct type
|
||||
let field_option = match input_value {
|
||||
let option = match input_value {
|
||||
Some(input) => {
|
||||
if let InputValue::Field(string) = input {
|
||||
Some(string)
|
||||
@ -27,12 +42,7 @@ pub(crate) fn field_from_input<F: Field + PrimeField, G: GroupType<F>, CS: Const
|
||||
None => None,
|
||||
};
|
||||
|
||||
let field_name = format!("{}: field", name);
|
||||
let field_name_unique = format!("`{}` {}:{}", field_name, span.line, span.start);
|
||||
let field_value = FieldType::alloc(cs.ns(|| field_name_unique), || {
|
||||
field_option.ok_or(SynthesisError::AssignmentMissing)
|
||||
})
|
||||
.map_err(|_| FieldError::missing_field(field_name, span))?;
|
||||
let field = allocate_field(cs, name, option, span)?;
|
||||
|
||||
Ok(ConstrainedValue::Field(field_value))
|
||||
Ok(ConstrainedValue::Field(field))
|
||||
}
|
||||
|
@ -7,6 +7,21 @@ use snarkos_models::{
|
||||
gadgets::r1cs::ConstraintSystem,
|
||||
};
|
||||
|
||||
pub(crate) fn allocate_group<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
|
||||
cs: &mut CS,
|
||||
name: String,
|
||||
option: Option<String>,
|
||||
span: Span,
|
||||
) -> Result<G, GroupError> {
|
||||
let group_name = format!("{}: group", name);
|
||||
let group_name_unique = format!("`{}` {}:{}", group_name, span.line, span.start);
|
||||
|
||||
G::alloc(cs.ns(|| group_name_unique), || {
|
||||
option.ok_or(SynthesisError::AssignmentMissing)
|
||||
})
|
||||
.map_err(|_| GroupError::missing_group(group_name, span))
|
||||
}
|
||||
|
||||
pub(crate) fn group_from_input<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
|
||||
cs: &mut CS,
|
||||
name: String,
|
||||
@ -14,7 +29,7 @@ pub(crate) fn group_from_input<F: Field + PrimeField, G: GroupType<F>, CS: Const
|
||||
span: Span,
|
||||
) -> Result<ConstrainedValue<F, G>, GroupError> {
|
||||
// Check that the parameter value is the correct type
|
||||
let group_option = match input_value {
|
||||
let option = match input_value {
|
||||
Some(input) => {
|
||||
if let InputValue::Group(string) = input {
|
||||
Some(string)
|
||||
@ -25,12 +40,7 @@ pub(crate) fn group_from_input<F: Field + PrimeField, G: GroupType<F>, CS: Const
|
||||
None => None,
|
||||
};
|
||||
|
||||
let group_name = format!("{}: group", name);
|
||||
let group_name_unique = format!("`{}` {}:{}", group_name, span.line, span.start);
|
||||
let group_value = G::alloc(cs.ns(|| group_name_unique), || {
|
||||
group_option.ok_or(SynthesisError::AssignmentMissing)
|
||||
})
|
||||
.map_err(|_| GroupError::missing_group(group_name, span))?;
|
||||
let group = allocate_group(cs, name, option, span)?;
|
||||
|
||||
Ok(ConstrainedValue::Group(group_value))
|
||||
Ok(ConstrainedValue::Group(group))
|
||||
}
|
||||
|
@ -76,10 +76,17 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
ConstrainedValue::Array(old) => {
|
||||
new_value.resolve_type(&vec![old[index].to_type()], span.clone())?;
|
||||
|
||||
let selected_value =
|
||||
ConstrainedValue::conditionally_select(cs, &condition, &new_value, &old[index]).map_err(
|
||||
|_| StatementError::select_fail(new_value.to_string(), old[index].to_string(), span),
|
||||
)?;
|
||||
let mut unique_namespace =
|
||||
cs.ns(|| format!("select {} {}:{}", new_value.to_string(), span.line, span.start));
|
||||
let selected_value = ConstrainedValue::conditionally_select(
|
||||
&mut unique_namespace,
|
||||
&condition,
|
||||
&new_value,
|
||||
&old[index],
|
||||
)
|
||||
.map_err(|_| {
|
||||
StatementError::select_fail(new_value.to_string(), old[index].to_string(), span)
|
||||
})?;
|
||||
|
||||
old[index] = selected_value;
|
||||
}
|
||||
@ -107,8 +114,12 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
}
|
||||
_ => return Err(StatementError::array_assign_range(span)),
|
||||
};
|
||||
let selected_array = ConstrainedValue::conditionally_select(cs, &condition, &new_array, old_array)
|
||||
.map_err(|_| StatementError::select_fail(new_array.to_string(), old_array.to_string(), span))?;
|
||||
let mut unique_namespace =
|
||||
cs.ns(|| format!("select {} {}:{}", new_array.to_string(), span.line, span.start));
|
||||
let selected_array =
|
||||
ConstrainedValue::conditionally_select(&mut unique_namespace, &condition, &new_array, old_array)
|
||||
.map_err(|_| StatementError::select_fail(new_array.to_string(), old_array.to_string(), span))?;
|
||||
|
||||
*old_array = selected_array;
|
||||
}
|
||||
}
|
||||
@ -146,10 +157,17 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
_ => {
|
||||
new_value.resolve_type(&vec![object.1.to_type()], span.clone())?;
|
||||
|
||||
let selected_value =
|
||||
ConstrainedValue::conditionally_select(cs, &condition, &new_value, &object.1).map_err(
|
||||
|_| StatementError::select_fail(new_value.to_string(), object.1.to_string(), span),
|
||||
)?;
|
||||
let mut unique_namespace =
|
||||
cs.ns(|| format!("select {} {}:{}", new_value.to_string(), span.line, span.start));
|
||||
let selected_value = ConstrainedValue::conditionally_select(
|
||||
&mut unique_namespace,
|
||||
&condition,
|
||||
&new_value,
|
||||
&object.1,
|
||||
)
|
||||
.map_err(|_| {
|
||||
StatementError::select_fail(new_value.to_string(), object.1.to_string(), span)
|
||||
})?;
|
||||
|
||||
object.1 = selected_value.to_owned();
|
||||
}
|
||||
@ -185,9 +203,14 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
Assignee::Identifier(_identifier) => {
|
||||
let condition = indicator.unwrap_or(Boolean::Constant(true));
|
||||
let old_value = self.get_mutable_assignee(variable_name.clone(), span.clone())?;
|
||||
|
||||
new_value.resolve_type(&vec![old_value.to_type()], span.clone())?;
|
||||
let selected_value = ConstrainedValue::conditionally_select(cs, &condition, &new_value, old_value)
|
||||
.map_err(|_| StatementError::select_fail(new_value.to_string(), old_value.to_string(), span))?;
|
||||
|
||||
let mut unique_namespace =
|
||||
cs.ns(|| format!("select {} {}:{}", new_value.to_string(), span.line, span.start));
|
||||
let selected_value =
|
||||
ConstrainedValue::conditionally_select(&mut unique_namespace, &condition, &new_value, old_value)
|
||||
.map_err(|_| StatementError::select_fail(new_value.to_string(), old_value.to_string(), span))?;
|
||||
|
||||
*old_value = selected_value;
|
||||
|
||||
@ -391,9 +414,9 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
value => return Err(StatementError::conditional_boolean(value.to_string(), span)),
|
||||
};
|
||||
|
||||
// Determine nested branch selection
|
||||
// Determine nested branch 1 selection
|
||||
let branch_1_indicator = Boolean::and(
|
||||
&mut cs.ns(|| format!("statement branch 1 indicator {}", statement_string)),
|
||||
&mut cs.ns(|| format!("branch indicator 1 {} {}:{}", statement_string, span.line, span.start)),
|
||||
&outer_indicator,
|
||||
&inner_indicator,
|
||||
)?;
|
||||
@ -408,13 +431,14 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
return_types.clone(),
|
||||
)?;
|
||||
|
||||
// Execute branch 2
|
||||
// Determine nested branch 2 selection
|
||||
let branch_2_indicator = Boolean::and(
|
||||
&mut cs.ns(|| format!("statement branch 2 indicator {}", statement_string)),
|
||||
&mut cs.ns(|| format!("branch indicator 2 {} {}:{}", statement_string, span.line, span.start)),
|
||||
&outer_indicator,
|
||||
&inner_indicator.not(),
|
||||
)?;
|
||||
|
||||
// Execute branch 2
|
||||
match statement.next {
|
||||
Some(next) => match next {
|
||||
ConditionalNestedOrEndStatement::Nested(nested) => self.enforce_conditional_statement(
|
||||
@ -435,7 +459,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
return_types,
|
||||
),
|
||||
},
|
||||
None => Ok(None), // this is an if with no else, have to pass statements.conditional down to next statements somehow
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
@ -450,7 +474,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
stop: Integer,
|
||||
statements: Vec<Statement>,
|
||||
return_types: Vec<Type>,
|
||||
_span: Span,
|
||||
span: Span,
|
||||
) -> Result<Option<ConstrainedValue<F, G>>, StatementError> {
|
||||
let mut res = None;
|
||||
|
||||
@ -463,11 +487,11 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
ConstrainedValue::Integer(Integer::U32(UInt32::constant(i as u32))),
|
||||
);
|
||||
|
||||
cs.ns(|| format!("loop {} = {}", index.to_string(), i));
|
||||
let mut unique_namespace = cs.ns(|| format!("for loop iteration {} {}:{}", i, span.line, span.start));
|
||||
|
||||
// Evaluate statements and possibly return early
|
||||
if let Some(early_return) = self.evaluate_branch(
|
||||
cs,
|
||||
&mut unique_namespace,
|
||||
file_scope.clone(),
|
||||
function_scope.clone(),
|
||||
indicator,
|
||||
@ -491,7 +515,16 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
span: Span,
|
||||
) -> Result<(), StatementError> {
|
||||
let condition = indicator.unwrap_or(Boolean::Constant(true));
|
||||
let result = left.conditional_enforce_equal(cs, right, &condition);
|
||||
let unique_namespace = cs.ns(|| {
|
||||
format!(
|
||||
"assert {} == {} {}:{}",
|
||||
left.to_string(),
|
||||
right.to_string(),
|
||||
span.line,
|
||||
span.start
|
||||
)
|
||||
});
|
||||
let result = left.conditional_enforce_equal(unique_namespace, right, &condition);
|
||||
|
||||
Ok(result.map_err(|_| StatementError::assertion_failed(left.to_string(), right.to_string(), span))?)
|
||||
}
|
||||
|
@ -1,6 +1,14 @@
|
||||
//! The in memory stored value for a defined name in a resolved Leo program.
|
||||
|
||||
use crate::{errors::ValueError, new_bool_constant, FieldType, GroupType};
|
||||
use crate::{
|
||||
allocate_bool,
|
||||
allocate_field,
|
||||
allocate_group,
|
||||
errors::ValueError,
|
||||
new_bool_constant,
|
||||
FieldType,
|
||||
GroupType,
|
||||
};
|
||||
use leo_types::{Circuit, Function, Identifier, Integer, Span, Type};
|
||||
|
||||
use snarkos_errors::gadgets::SynthesisError;
|
||||
@ -8,7 +16,7 @@ use snarkos_models::{
|
||||
curves::{Field, PrimeField},
|
||||
gadgets::{
|
||||
r1cs::ConstraintSystem,
|
||||
utilities::{alloc::AllocGadget, boolean::Boolean, eq::ConditionalEqGadget, select::CondSelectGadget},
|
||||
utilities::{boolean::Boolean, eq::ConditionalEqGadget, select::CondSelectGadget},
|
||||
},
|
||||
};
|
||||
use std::fmt;
|
||||
@ -104,25 +112,28 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedValue<F, G> {
|
||||
// allocated values
|
||||
ConstrainedValue::Boolean(boolean) => {
|
||||
let option = boolean.get_value();
|
||||
let name = option.map(|b| b.to_string()).unwrap_or(format!("[allocated]"));
|
||||
|
||||
*boolean = Boolean::alloc(cs, || option.ok_or(SynthesisError::AssignmentMissing))?;
|
||||
*boolean = allocate_bool(&mut cs, name, option, span)?;
|
||||
}
|
||||
ConstrainedValue::Integer(integer) => {
|
||||
let integer_type = integer.get_type();
|
||||
let option = integer.get_value();
|
||||
let name = format!("clone {}", integer);
|
||||
let name = option.map(|n| n.to_string()).unwrap_or(format!("[allocated]"));
|
||||
|
||||
*integer = Integer::allocate_type(&mut cs, integer_type, name, option, span)?;
|
||||
}
|
||||
ConstrainedValue::Field(field) => {
|
||||
let option = field.get_value().map(|v| format!("{}", v));
|
||||
let name = option.clone().map(|f| f.to_string()).unwrap_or(format!("[allocated]"));
|
||||
|
||||
*field = FieldType::alloc(cs, || option.ok_or(SynthesisError::AssignmentMissing))?;
|
||||
*field = allocate_field(&mut cs, name, option, span)?;
|
||||
}
|
||||
ConstrainedValue::Group(group) => {
|
||||
let string = format!("{}", group); // may need to implement u256 -> decimal formatting
|
||||
let name = format!("{}", group); // may need to implement u256 -> decimal formatting
|
||||
let option = Some(name.clone());
|
||||
|
||||
*group = G::alloc(cs, || Ok(string))?;
|
||||
*group = allocate_group(&mut cs, name, option, span)?;
|
||||
}
|
||||
// value wrappers
|
||||
ConstrainedValue::Array(array) => {
|
||||
|
Loading…
Reference in New Issue
Block a user