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))
|
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>>(
|
pub(crate) fn bool_from_input<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
name: String,
|
name: String,
|
||||||
@ -27,7 +42,7 @@ pub(crate) fn bool_from_input<F: Field + PrimeField, G: GroupType<F>, CS: Constr
|
|||||||
span: Span,
|
span: Span,
|
||||||
) -> Result<ConstrainedValue<F, G>, BooleanError> {
|
) -> Result<ConstrainedValue<F, G>, BooleanError> {
|
||||||
// Check that the input value is the correct type
|
// Check that the input value is the correct type
|
||||||
let bool_value = match input_value {
|
let option = match input_value {
|
||||||
Some(input) => {
|
Some(input) => {
|
||||||
if let InputValue::Boolean(bool) = input {
|
if let InputValue::Boolean(bool) = input {
|
||||||
Some(bool)
|
Some(bool)
|
||||||
@ -38,12 +53,7 @@ pub(crate) fn bool_from_input<F: Field + PrimeField, G: GroupType<F>, CS: Constr
|
|||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let boolean_name = format!("{}: bool", name);
|
let number = allocate_bool(cs, name, option, span)?;
|
||||||
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))?;
|
|
||||||
|
|
||||||
Ok(ConstrainedValue::Boolean(number))
|
Ok(ConstrainedValue::Boolean(number))
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,21 @@ use snarkos_models::{
|
|||||||
gadgets::{r1cs::ConstraintSystem, utilities::alloc::AllocGadget},
|
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>>(
|
pub(crate) fn field_from_input<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
name: String,
|
name: String,
|
||||||
@ -16,7 +31,7 @@ pub(crate) fn field_from_input<F: Field + PrimeField, G: GroupType<F>, CS: Const
|
|||||||
span: Span,
|
span: Span,
|
||||||
) -> Result<ConstrainedValue<F, G>, FieldError> {
|
) -> Result<ConstrainedValue<F, G>, FieldError> {
|
||||||
// Check that the parameter value is the correct type
|
// Check that the parameter value is the correct type
|
||||||
let field_option = match input_value {
|
let option = match input_value {
|
||||||
Some(input) => {
|
Some(input) => {
|
||||||
if let InputValue::Field(string) = input {
|
if let InputValue::Field(string) = input {
|
||||||
Some(string)
|
Some(string)
|
||||||
@ -27,12 +42,7 @@ pub(crate) fn field_from_input<F: Field + PrimeField, G: GroupType<F>, CS: Const
|
|||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let field_name = format!("{}: field", name);
|
let field = allocate_field(cs, name, option, span)?;
|
||||||
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))?;
|
|
||||||
|
|
||||||
Ok(ConstrainedValue::Field(field_value))
|
Ok(ConstrainedValue::Field(field))
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,21 @@ use snarkos_models::{
|
|||||||
gadgets::r1cs::ConstraintSystem,
|
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>>(
|
pub(crate) fn group_from_input<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
name: String,
|
name: String,
|
||||||
@ -14,7 +29,7 @@ pub(crate) fn group_from_input<F: Field + PrimeField, G: GroupType<F>, CS: Const
|
|||||||
span: Span,
|
span: Span,
|
||||||
) -> Result<ConstrainedValue<F, G>, GroupError> {
|
) -> Result<ConstrainedValue<F, G>, GroupError> {
|
||||||
// Check that the parameter value is the correct type
|
// Check that the parameter value is the correct type
|
||||||
let group_option = match input_value {
|
let option = match input_value {
|
||||||
Some(input) => {
|
Some(input) => {
|
||||||
if let InputValue::Group(string) = input {
|
if let InputValue::Group(string) = input {
|
||||||
Some(string)
|
Some(string)
|
||||||
@ -25,12 +40,7 @@ pub(crate) fn group_from_input<F: Field + PrimeField, G: GroupType<F>, CS: Const
|
|||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let group_name = format!("{}: group", name);
|
let group = allocate_group(cs, name, option, span)?;
|
||||||
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))?;
|
|
||||||
|
|
||||||
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) => {
|
ConstrainedValue::Array(old) => {
|
||||||
new_value.resolve_type(&vec![old[index].to_type()], span.clone())?;
|
new_value.resolve_type(&vec![old[index].to_type()], span.clone())?;
|
||||||
|
|
||||||
let selected_value =
|
let mut unique_namespace =
|
||||||
ConstrainedValue::conditionally_select(cs, &condition, &new_value, &old[index]).map_err(
|
cs.ns(|| format!("select {} {}:{}", new_value.to_string(), span.line, span.start));
|
||||||
|_| StatementError::select_fail(new_value.to_string(), old[index].to_string(), span),
|
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;
|
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)),
|
_ => return Err(StatementError::array_assign_range(span)),
|
||||||
};
|
};
|
||||||
let selected_array = ConstrainedValue::conditionally_select(cs, &condition, &new_array, old_array)
|
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))?;
|
.map_err(|_| StatementError::select_fail(new_array.to_string(), old_array.to_string(), span))?;
|
||||||
|
|
||||||
*old_array = selected_array;
|
*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())?;
|
new_value.resolve_type(&vec![object.1.to_type()], span.clone())?;
|
||||||
|
|
||||||
let selected_value =
|
let mut unique_namespace =
|
||||||
ConstrainedValue::conditionally_select(cs, &condition, &new_value, &object.1).map_err(
|
cs.ns(|| format!("select {} {}:{}", new_value.to_string(), span.line, span.start));
|
||||||
|_| StatementError::select_fail(new_value.to_string(), object.1.to_string(), span),
|
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();
|
object.1 = selected_value.to_owned();
|
||||||
}
|
}
|
||||||
@ -185,8 +203,13 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
|||||||
Assignee::Identifier(_identifier) => {
|
Assignee::Identifier(_identifier) => {
|
||||||
let condition = indicator.unwrap_or(Boolean::Constant(true));
|
let condition = indicator.unwrap_or(Boolean::Constant(true));
|
||||||
let old_value = self.get_mutable_assignee(variable_name.clone(), span.clone())?;
|
let old_value = self.get_mutable_assignee(variable_name.clone(), span.clone())?;
|
||||||
|
|
||||||
new_value.resolve_type(&vec![old_value.to_type()], 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)
|
|
||||||
|
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))?;
|
.map_err(|_| StatementError::select_fail(new_value.to_string(), old_value.to_string(), span))?;
|
||||||
|
|
||||||
*old_value = selected_value;
|
*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)),
|
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(
|
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,
|
&outer_indicator,
|
||||||
&inner_indicator,
|
&inner_indicator,
|
||||||
)?;
|
)?;
|
||||||
@ -408,13 +431,14 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
|||||||
return_types.clone(),
|
return_types.clone(),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
// Execute branch 2
|
// Determine nested branch 2 selection
|
||||||
let branch_2_indicator = Boolean::and(
|
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,
|
&outer_indicator,
|
||||||
&inner_indicator.not(),
|
&inner_indicator.not(),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
// Execute branch 2
|
||||||
match statement.next {
|
match statement.next {
|
||||||
Some(next) => match next {
|
Some(next) => match next {
|
||||||
ConditionalNestedOrEndStatement::Nested(nested) => self.enforce_conditional_statement(
|
ConditionalNestedOrEndStatement::Nested(nested) => self.enforce_conditional_statement(
|
||||||
@ -435,7 +459,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
|||||||
return_types,
|
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,
|
stop: Integer,
|
||||||
statements: Vec<Statement>,
|
statements: Vec<Statement>,
|
||||||
return_types: Vec<Type>,
|
return_types: Vec<Type>,
|
||||||
_span: Span,
|
span: Span,
|
||||||
) -> Result<Option<ConstrainedValue<F, G>>, StatementError> {
|
) -> Result<Option<ConstrainedValue<F, G>>, StatementError> {
|
||||||
let mut res = None;
|
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))),
|
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
|
// Evaluate statements and possibly return early
|
||||||
if let Some(early_return) = self.evaluate_branch(
|
if let Some(early_return) = self.evaluate_branch(
|
||||||
cs,
|
&mut unique_namespace,
|
||||||
file_scope.clone(),
|
file_scope.clone(),
|
||||||
function_scope.clone(),
|
function_scope.clone(),
|
||||||
indicator,
|
indicator,
|
||||||
@ -491,7 +515,16 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
|||||||
span: Span,
|
span: Span,
|
||||||
) -> Result<(), StatementError> {
|
) -> Result<(), StatementError> {
|
||||||
let condition = indicator.unwrap_or(Boolean::Constant(true));
|
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))?)
|
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.
|
//! 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 leo_types::{Circuit, Function, Identifier, Integer, Span, Type};
|
||||||
|
|
||||||
use snarkos_errors::gadgets::SynthesisError;
|
use snarkos_errors::gadgets::SynthesisError;
|
||||||
@ -8,7 +16,7 @@ use snarkos_models::{
|
|||||||
curves::{Field, PrimeField},
|
curves::{Field, PrimeField},
|
||||||
gadgets::{
|
gadgets::{
|
||||||
r1cs::ConstraintSystem,
|
r1cs::ConstraintSystem,
|
||||||
utilities::{alloc::AllocGadget, boolean::Boolean, eq::ConditionalEqGadget, select::CondSelectGadget},
|
utilities::{boolean::Boolean, eq::ConditionalEqGadget, select::CondSelectGadget},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
@ -104,25 +112,28 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedValue<F, G> {
|
|||||||
// allocated values
|
// allocated values
|
||||||
ConstrainedValue::Boolean(boolean) => {
|
ConstrainedValue::Boolean(boolean) => {
|
||||||
let option = boolean.get_value();
|
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) => {
|
ConstrainedValue::Integer(integer) => {
|
||||||
let integer_type = integer.get_type();
|
let integer_type = integer.get_type();
|
||||||
let option = integer.get_value();
|
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)?;
|
*integer = Integer::allocate_type(&mut cs, integer_type, name, option, span)?;
|
||||||
}
|
}
|
||||||
ConstrainedValue::Field(field) => {
|
ConstrainedValue::Field(field) => {
|
||||||
let option = field.get_value().map(|v| format!("{}", v));
|
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) => {
|
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
|
// value wrappers
|
||||||
ConstrainedValue::Array(array) => {
|
ConstrainedValue::Array(array) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user