1
1
use crate :: {
2
2
ssa:: {
3
3
acir_gen:: {
4
- acir_mem:: AcirMem , constraints, internal_var_cache:: InternalVarCache , operations,
5
- InternalVar ,
4
+ constraints, internal_var_cache:: InternalVarCache , operations, Acir , InternalVar ,
6
5
} ,
7
6
context:: SsaContext ,
8
7
node:: { self , BinaryOp , Node , ObjectType } ,
@@ -29,8 +28,7 @@ fn get_predicate(
29
28
pub ( crate ) fn evaluate (
30
29
binary : & node:: Binary ,
31
30
res_type : ObjectType ,
32
- var_cache : & mut InternalVarCache ,
33
- memory_map : & mut AcirMem ,
31
+ acir_gen : & mut Acir ,
34
32
evaluator : & mut Evaluator ,
35
33
ctx : & SsaContext ,
36
34
) -> Option < InternalVar > {
@@ -43,17 +41,17 @@ pub(crate) fn evaluate(
43
41
44
42
let binary_output = match & binary. operator {
45
43
BinaryOp :: Add | BinaryOp :: SafeAdd => {
46
- let l_c = var_cache. get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
47
- let r_c = var_cache. get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
44
+ let l_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
45
+ let r_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
48
46
InternalVar :: from ( constraints:: add (
49
47
l_c. expression ( ) ,
50
48
FieldElement :: one ( ) ,
51
49
r_c. expression ( ) ,
52
50
) )
53
51
} ,
54
52
BinaryOp :: Sub { max_rhs_value } | BinaryOp :: SafeSub { max_rhs_value } => {
55
- let l_c = var_cache. get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
56
- let r_c = var_cache. get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
53
+ let l_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
54
+ let r_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
57
55
if res_type == ObjectType :: NativeField {
58
56
InternalVar :: from ( constraints:: subtract (
59
57
l_c. expression ( ) ,
@@ -92,18 +90,18 @@ pub(crate) fn evaluate(
92
90
}
93
91
}
94
92
BinaryOp :: Mul | BinaryOp :: SafeMul => {
95
- let l_c = var_cache. get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
96
- let r_c = var_cache. get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
93
+ let l_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
94
+ let r_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
97
95
InternalVar :: from ( constraints:: mul_with_witness (
98
96
evaluator,
99
97
l_c. expression ( ) ,
100
98
r_c. expression ( ) ,
101
99
) )
102
100
} ,
103
101
BinaryOp :: Udiv ( _) => {
104
- let l_c = var_cache. get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
105
- let r_c = var_cache. get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
106
- let predicate = get_predicate ( var_cache, binary, evaluator, ctx) ;
102
+ let l_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
103
+ let r_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
104
+ let predicate = get_predicate ( & mut acir_gen . var_cache , binary, evaluator, ctx) ;
107
105
let ( q_wit, _) = constraints:: evaluate_udiv (
108
106
l_c. expression ( ) ,
109
107
r_c. expression ( ) ,
@@ -114,16 +112,16 @@ pub(crate) fn evaluate(
114
112
InternalVar :: from ( q_wit)
115
113
}
116
114
BinaryOp :: Sdiv ( _) => {
117
- let l_c = var_cache. get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
118
- let r_c = var_cache. get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
115
+ let l_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
116
+ let r_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
119
117
InternalVar :: from (
120
118
constraints:: evaluate_sdiv ( l_c. expression ( ) , r_c. expression ( ) , evaluator) . 0 ,
121
119
)
122
120
} ,
123
121
BinaryOp :: Urem ( _) => {
124
- let l_c = var_cache. get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
125
- let r_c = var_cache. get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
126
- let predicate = get_predicate ( var_cache, binary, evaluator, ctx) ;
122
+ let l_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
123
+ let r_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
124
+ let predicate = get_predicate ( & mut acir_gen . var_cache , binary, evaluator, ctx) ;
127
125
let ( _, r_wit) = constraints:: evaluate_udiv (
128
126
l_c. expression ( ) ,
129
127
r_c. expression ( ) ,
@@ -134,16 +132,16 @@ pub(crate) fn evaluate(
134
132
InternalVar :: from ( r_wit)
135
133
}
136
134
BinaryOp :: Srem ( _) => {
137
- let l_c = var_cache. get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
138
- let r_c = var_cache. get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
135
+ let l_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
136
+ let r_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
139
137
InternalVar :: from (
140
138
// TODO: we should use variable naming here instead of .1
141
139
constraints:: evaluate_sdiv ( l_c. expression ( ) , r_c. expression ( ) , evaluator) . 1 ,
142
140
) } ,
143
141
BinaryOp :: Div ( _) => {
144
- let l_c = var_cache. get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
145
- let mut r_c = var_cache. get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
146
- let predicate = get_predicate ( var_cache, binary, evaluator, ctx) . expression ( ) . clone ( ) ;
142
+ let l_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
143
+ let r_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
144
+ let predicate = get_predicate ( & mut acir_gen . var_cache , binary, evaluator, ctx) . expression ( ) . clone ( ) ;
147
145
if let Some ( r_value) = r_c. to_const ( ) {
148
146
if r_value. is_zero ( ) {
149
147
panic ! ( "Panic - division by zero" ) ;
@@ -152,7 +150,7 @@ pub(crate) fn evaluate(
152
150
}
153
151
} else {
154
152
//TODO avoid creating witnesses here.
155
- let x_witness = r_c . get_or_compute_witness ( evaluator , false ) . expect ( "unexpected constant expression" ) ;
153
+ let x_witness = acir_gen . var_cache . get_or_compute_witness ( r_c , evaluator ) . expect ( "unexpected constant expression" ) ;
156
154
let inverse = Expression :: from ( & constraints:: evaluate_inverse (
157
155
x_witness, & predicate, evaluator,
158
156
) ) ;
@@ -164,20 +162,20 @@ pub(crate) fn evaluate(
164
162
}
165
163
}
166
164
BinaryOp :: Eq => {
167
- let l_c = var_cache. get_or_compute_internal_var ( binary. lhs , evaluator, ctx) ;
168
- let r_c = var_cache. get_or_compute_internal_var ( binary. rhs , evaluator, ctx) ;
165
+ let l_c = acir_gen . var_cache . get_or_compute_internal_var ( binary. lhs , evaluator, ctx) ;
166
+ let r_c = acir_gen . var_cache . get_or_compute_internal_var ( binary. rhs , evaluator, ctx) ;
169
167
InternalVar :: from (
170
- operations:: cmp:: evaluate_eq ( memory_map , binary. lhs , binary. rhs , l_c, r_c, ctx, evaluator) ,
168
+ operations:: cmp:: evaluate_eq ( acir_gen , binary. lhs , binary. rhs , l_c, r_c, ctx, evaluator) ,
171
169
) } ,
172
170
BinaryOp :: Ne => {
173
- let l_c = var_cache. get_or_compute_internal_var ( binary. lhs , evaluator, ctx) ;
174
- let r_c = var_cache. get_or_compute_internal_var ( binary. rhs , evaluator, ctx) ;
171
+ let l_c = acir_gen . var_cache . get_or_compute_internal_var ( binary. lhs , evaluator, ctx) ;
172
+ let r_c = acir_gen . var_cache . get_or_compute_internal_var ( binary. rhs , evaluator, ctx) ;
175
173
InternalVar :: from (
176
- operations:: cmp:: evaluate_neq ( memory_map , binary. lhs , binary. rhs , l_c, r_c, ctx, evaluator) ,
174
+ operations:: cmp:: evaluate_neq ( acir_gen , binary. lhs , binary. rhs , l_c, r_c, ctx, evaluator) ,
177
175
) } ,
178
176
BinaryOp :: Ult => {
179
- let l_c = var_cache. get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
180
- let r_c = var_cache. get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
177
+ let l_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
178
+ let r_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
181
179
let size = ctx[ binary. lhs ] . get_type ( ) . bits ( ) ;
182
180
constraints:: evaluate_cmp (
183
181
l_c. expression ( ) ,
@@ -189,8 +187,8 @@ pub(crate) fn evaluate(
189
187
. into ( )
190
188
}
191
189
BinaryOp :: Ule => {
192
- let l_c = var_cache. get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
193
- let r_c = var_cache. get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
190
+ let l_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
191
+ let r_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
194
192
let size = ctx[ binary. lhs ] . get_type ( ) . bits ( ) ;
195
193
let e = constraints:: evaluate_cmp (
196
194
r_c. expression ( ) ,
@@ -202,15 +200,15 @@ pub(crate) fn evaluate(
202
200
constraints:: subtract ( & Expression :: one ( ) , FieldElement :: one ( ) , & e) . into ( )
203
201
}
204
202
BinaryOp :: Slt => {
205
- let l_c = var_cache. get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
206
- let r_c = var_cache. get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
203
+ let l_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
204
+ let r_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
207
205
let s = ctx[ binary. lhs ] . get_type ( ) . bits ( ) ;
208
206
constraints:: evaluate_cmp ( l_c. expression ( ) , r_c. expression ( ) , s, true , evaluator)
209
207
. into ( )
210
208
}
211
209
BinaryOp :: Sle => {
212
- let l_c = var_cache. get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
213
- let r_c = var_cache. get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
210
+ let l_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
211
+ let r_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
214
212
let s = ctx[ binary. lhs ] . get_type ( ) . bits ( ) ;
215
213
let e = constraints:: evaluate_cmp (
216
214
r_c. expression ( ) ,
@@ -230,13 +228,13 @@ pub(crate) fn evaluate(
230
228
)
231
229
}
232
230
BinaryOp :: And | BinaryOp :: Or | BinaryOp :: Xor => {
233
- let l_c = var_cache. get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
234
- let r_c = var_cache. get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
231
+ let l_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. lhs , evaluator, ctx) ;
232
+ let r_c = acir_gen . var_cache . get_or_compute_internal_var_unwrap ( binary. rhs , evaluator, ctx) ;
235
233
let bit_size = res_type. bits ( ) ;
236
234
let opcode = binary. operator . clone ( ) ;
237
235
let bitwise_result = match operations:: bitwise:: simplify_bitwise ( & l_c, & r_c, bit_size, & opcode) {
238
236
Some ( simplified_internal_var) => simplified_internal_var. expression ( ) . clone ( ) ,
239
- None => operations:: bitwise:: evaluate_bitwise ( l_c, r_c, bit_size, evaluator, opcode) ,
237
+ None => operations:: bitwise:: evaluate_bitwise ( l_c, r_c, bit_size, evaluator, & mut acir_gen . var_cache , ctx , opcode) ,
240
238
} ;
241
239
InternalVar :: from ( bitwise_result)
242
240
}
0 commit comments