1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use crate::{
errors::StatementError,
program::ConstrainedProgram,
value::ConstrainedValue,
GroupType,
IndicatorAndConstrainedValue,
StatementResult,
};
use leo_ast::{ConditionalNestedOrEndStatement, ConditionalStatement, Span, Type};
use snarkos_models::{
curves::{Field, PrimeField},
gadgets::{r1cs::ConstraintSystem, utilities::boolean::Boolean},
};
fn indicator_to_string(indicator: &Boolean) -> String {
indicator
.get_value()
.map(|b| b.to_string())
.unwrap_or_else(|| "[input]".to_string())
}
impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
#[allow(clippy::too_many_arguments)]
pub fn enforce_conditional_statement<CS: ConstraintSystem<F>>(
&mut self,
cs: &mut CS,
file_scope: &str,
function_scope: &str,
indicator: &Boolean,
statement: ConditionalStatement,
return_type: Option<Type>,
declared_circuit_reference: &str,
mut_self: bool,
span: &Span,
) -> StatementResult<Vec<IndicatorAndConstrainedValue<F, G>>> {
let statement_string = statement.to_string();
let outer_indicator = indicator;
let inner_indicator = match self.enforce_expression(
cs,
file_scope,
function_scope,
Some(Type::Boolean),
statement.condition.clone(),
)? {
ConstrainedValue::Boolean(resolved) => resolved,
value => return Err(StatementError::conditional_boolean(value.to_string(), span.to_owned())),
};
let outer_indicator_string = indicator_to_string(outer_indicator);
let inner_indicator_string = indicator_to_string(&inner_indicator);
let branch_1_name = format!(
"branch indicator 1 {} && {}",
outer_indicator_string, inner_indicator_string
);
let branch_1_indicator = Boolean::and(
&mut cs.ns(|| format!("branch 1 {} {}:{}", statement_string, span.line, span.start)),
outer_indicator,
&inner_indicator,
)
.map_err(|_| StatementError::indicator_calculation(branch_1_name, span.to_owned()))?;
let mut results = vec![];
let mut branch_1_result = self.evaluate_block(
cs,
file_scope,
function_scope,
&branch_1_indicator,
statement.block,
return_type.clone(),
declared_circuit_reference,
mut_self,
)?;
results.append(&mut branch_1_result);
let inner_indicator = inner_indicator.not();
let inner_indicator_string = indicator_to_string(&inner_indicator);
let branch_2_name = format!(
"branch indicator 2 {} && {}",
outer_indicator_string, inner_indicator_string
);
let branch_2_indicator = Boolean::and(
&mut cs.ns(|| format!("branch 2 {} {}:{}", statement_string, span.line, span.start)),
&outer_indicator,
&inner_indicator,
)
.map_err(|_| StatementError::indicator_calculation(branch_2_name, span.to_owned()))?;
let mut branch_2_result = match statement.next {
Some(next) => match next {
ConditionalNestedOrEndStatement::Nested(nested) => self.enforce_conditional_statement(
cs,
file_scope,
function_scope,
&branch_2_indicator,
*nested,
return_type,
declared_circuit_reference,
mut_self,
span,
)?,
ConditionalNestedOrEndStatement::End(block) => self.evaluate_block(
cs,
file_scope,
function_scope,
&branch_2_indicator,
block,
return_type,
declared_circuit_reference,
mut_self,
)?,
},
None => vec![],
};
results.append(&mut branch_2_result);
Ok(results)
}
}