@@ -29,6 +29,7 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString, Registers: Re
29
29
message : message_vector. to_heap_vector ( ) ,
30
30
output : result_array. to_heap_array ( ) ,
31
31
} ) ;
32
+ deallocate_converted_vector ( brillig_context, message, message_vector, bb_func) ;
32
33
} else {
33
34
unreachable ! ( "ICE: SHA256 expects one array argument and one array result" )
34
35
}
@@ -42,6 +43,7 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString, Registers: Re
42
43
message : message_vector. to_heap_vector ( ) ,
43
44
output : result_array. to_heap_array ( ) ,
44
45
} ) ;
46
+ deallocate_converted_vector ( brillig_context, message, message_vector, bb_func) ;
45
47
} else {
46
48
unreachable ! ( "ICE: Blake2s expects one array argument and one array result" )
47
49
}
@@ -55,6 +57,7 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString, Registers: Re
55
57
message : message_vector. to_heap_vector ( ) ,
56
58
output : result_array. to_heap_array ( ) ,
57
59
} ) ;
60
+ deallocate_converted_vector ( brillig_context, message, message_vector, bb_func) ;
58
61
} else {
59
62
unreachable ! ( "ICE: Blake3 expects one array argument and one array result" )
60
63
}
@@ -78,6 +81,7 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString, Registers: Re
78
81
output : result_array. to_heap_array ( ) ,
79
82
} ) ;
80
83
brillig_context. deallocate_single_addr ( message_size_as_usize) ;
84
+ deallocate_converted_vector ( brillig_context, message, message_vector, bb_func) ;
81
85
} else {
82
86
unreachable ! ( "ICE: Keccak256 expects message, message size and result array" )
83
87
}
@@ -92,6 +96,7 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString, Registers: Re
92
96
message : state_vector. to_heap_vector ( ) ,
93
97
output : result_array. to_heap_array ( ) ,
94
98
} ) ;
99
+ deallocate_converted_vector ( brillig_context, message, state_vector, bb_func) ;
95
100
} else {
96
101
unreachable ! ( "ICE: Keccakf1600 expects one array argument and one array result" )
97
102
}
@@ -111,6 +116,7 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString, Registers: Re
111
116
signature : signature. to_heap_array ( ) ,
112
117
result : result_register. address ,
113
118
} ) ;
119
+ deallocate_converted_vector ( brillig_context, message, message_hash_vector, bb_func) ;
114
120
} else {
115
121
unreachable ! (
116
122
"ICE: EcdsaSecp256k1 expects four array arguments and one register result"
@@ -132,6 +138,7 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString, Registers: Re
132
138
signature : signature. to_heap_array ( ) ,
133
139
result : result_register. address ,
134
140
} ) ;
141
+ deallocate_converted_vector ( brillig_context, message, message_hash_vector, bb_func) ;
135
142
} else {
136
143
unreachable ! (
137
144
"ICE: EcdsaSecp256r1 expects four array arguments and one register result"
@@ -151,6 +158,7 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString, Registers: Re
151
158
domain_separator : domain_separator. address ,
152
159
output : result_array. to_heap_array ( ) ,
153
160
} ) ;
161
+ deallocate_converted_vector ( brillig_context, message, message_vector, bb_func) ;
154
162
} else {
155
163
unreachable ! ( "ICE: Pedersen expects one array argument, a register for the domain separator, and one array result" )
156
164
}
@@ -167,6 +175,7 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString, Registers: Re
167
175
domain_separator : domain_separator. address ,
168
176
output : result. address ,
169
177
} ) ;
178
+ deallocate_converted_vector ( brillig_context, message, message_vector, bb_func) ;
170
179
} else {
171
180
unreachable ! ( "ICE: Pedersen hash expects one array argument, a register for the domain separator, and one register result" )
172
181
}
@@ -178,14 +187,16 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString, Registers: Re
178
187
) = ( function_arguments, function_results)
179
188
{
180
189
let message_hash = convert_array_or_vector ( brillig_context, message, bb_func) ;
181
- let signature = brillig_context. array_to_vector_instruction ( signature) ;
190
+ let signature_vector = brillig_context. array_to_vector_instruction ( signature) ;
182
191
brillig_context. black_box_op_instruction ( BlackBoxOp :: SchnorrVerify {
183
192
public_key_x : public_key_x. address ,
184
193
public_key_y : public_key_y. address ,
185
194
message : message_hash. to_heap_vector ( ) ,
186
- signature : signature . to_heap_vector ( ) ,
195
+ signature : signature_vector . to_heap_vector ( ) ,
187
196
result : result_register. address ,
188
197
} ) ;
198
+ deallocate_converted_vector ( brillig_context, message, message_hash, bb_func) ;
199
+ brillig_context. deallocate_register ( signature_vector. size ) ;
189
200
} else {
190
201
unreachable ! ( "ICE: Schnorr verify expects two registers for the public key, an array for signature, an array for the message hash and one result register" )
191
202
}
@@ -194,13 +205,15 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString, Registers: Re
194
205
if let ( [ points, scalars] , [ BrilligVariable :: BrilligArray ( outputs) ] ) =
195
206
( function_arguments, function_results)
196
207
{
197
- let points = convert_array_or_vector ( brillig_context, points, bb_func) ;
198
- let scalars = convert_array_or_vector ( brillig_context, scalars, bb_func) ;
208
+ let points_vector = convert_array_or_vector ( brillig_context, points, bb_func) ;
209
+ let scalars_vector = convert_array_or_vector ( brillig_context, scalars, bb_func) ;
199
210
brillig_context. black_box_op_instruction ( BlackBoxOp :: MultiScalarMul {
200
- points : points . to_heap_vector ( ) ,
201
- scalars : scalars . to_heap_vector ( ) ,
211
+ points : points_vector . to_heap_vector ( ) ,
212
+ scalars : scalars_vector . to_heap_vector ( ) ,
202
213
outputs : outputs. to_heap_array ( ) ,
203
214
} ) ;
215
+ deallocate_converted_vector ( brillig_context, points, points_vector, bb_func) ;
216
+ deallocate_converted_vector ( brillig_context, scalars, scalars_vector, bb_func) ;
204
217
} else {
205
218
unreachable ! (
206
219
"ICE: MultiScalarMul expects two register arguments and one array result"
@@ -319,6 +332,8 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString, Registers: Re
319
332
modulus : modulus_vector. to_heap_vector ( ) ,
320
333
output : output. address ,
321
334
} ) ;
335
+ deallocate_converted_vector ( brillig_context, inputs, inputs_vector, bb_func) ;
336
+ deallocate_converted_vector ( brillig_context, modulus, modulus_vector, bb_func) ;
322
337
} else {
323
338
unreachable ! (
324
339
"ICE: BigIntFromLeBytes expects a register and an array as arguments and two result registers"
@@ -336,6 +351,7 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString, Registers: Re
336
351
input : input. address ,
337
352
output : output. to_heap_vector ( ) ,
338
353
} ) ;
354
+ deallocate_converted_vector ( brillig_context, result_array, output, bb_func) ;
339
355
} else {
340
356
unreachable ! (
341
357
"ICE: BigIntToLeBytes expects two register arguments and one array result"
@@ -354,6 +370,7 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString, Registers: Re
354
370
output : result_array. to_heap_array ( ) ,
355
371
len : state_len. address ,
356
372
} ) ;
373
+ deallocate_converted_vector ( brillig_context, message, message_vector, bb_func) ;
357
374
} else {
358
375
unreachable ! ( "ICE: Poseidon2Permutation expects one array argument, a length and one array result" )
359
376
}
@@ -369,6 +386,8 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString, Registers: Re
369
386
hash_values : hash_vector. to_heap_vector ( ) ,
370
387
output : result_array. to_heap_array ( ) ,
371
388
} ) ;
389
+ deallocate_converted_vector ( brillig_context, message, message_vector, bb_func) ;
390
+ deallocate_converted_vector ( brillig_context, hash_values, hash_vector, bb_func) ;
372
391
} else {
373
392
unreachable ! ( "ICE: Sha256Compression expects two array argument, one array result" )
374
393
}
@@ -379,18 +398,19 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString, Registers: Re
379
398
[ BrilligVariable :: SingleAddr ( out_len) , outputs] ,
380
399
) = ( function_arguments, function_results)
381
400
{
382
- let inputs = convert_array_or_vector ( brillig_context, inputs, bb_func) ;
383
- let outputs = convert_array_or_vector ( brillig_context, outputs, bb_func) ;
384
- let output_vec = outputs. to_heap_vector ( ) ;
401
+ let inputs_vector = convert_array_or_vector ( brillig_context, inputs, bb_func) ;
402
+ let outputs_vector = convert_array_or_vector ( brillig_context, outputs, bb_func) ;
385
403
brillig_context. black_box_op_instruction ( BlackBoxOp :: AES128Encrypt {
386
- inputs : inputs . to_heap_vector ( ) ,
404
+ inputs : inputs_vector . to_heap_vector ( ) ,
387
405
iv : iv. to_heap_array ( ) ,
388
406
key : key. to_heap_array ( ) ,
389
- outputs : output_vec ,
407
+ outputs : outputs_vector . to_heap_vector ( ) ,
390
408
} ) ;
391
- brillig_context. mov_instruction ( out_len. address , output_vec . size ) ;
409
+ brillig_context. mov_instruction ( out_len. address , outputs_vector . size ) ;
392
410
// Returns slice, so we need to allocate memory for it after the fact
393
- brillig_context. increase_free_memory_pointer_instruction ( output_vec. size ) ;
411
+ brillig_context. increase_free_memory_pointer_instruction ( outputs_vector. size ) ;
412
+ deallocate_converted_vector ( brillig_context, inputs, inputs_vector, bb_func) ;
413
+ deallocate_converted_vector ( brillig_context, outputs, outputs_vector, bb_func) ;
394
414
} else {
395
415
unreachable ! ( "ICE: AES128Encrypt expects three array arguments, one array result" )
396
416
}
@@ -413,3 +433,24 @@ fn convert_array_or_vector<F: AcirField + DebugToString, Registers: RegisterAllo
413
433
) ,
414
434
}
415
435
}
436
+
437
+ /// Deallocates any new register allocated by the function above.
438
+ /// Concretely, the only allocated register between array and vector is the size register if the array was converted to a vector.
439
+ fn deallocate_converted_vector < F : AcirField + DebugToString , Registers : RegisterAllocator > (
440
+ brillig_context : & mut BrilligContext < F , Registers > ,
441
+ original_array_or_vector : & BrilligVariable ,
442
+ converted_vector : BrilligVector ,
443
+ bb_func : & BlackBoxFunc ,
444
+ ) {
445
+ match original_array_or_vector {
446
+ BrilligVariable :: BrilligArray ( _) => {
447
+ brillig_context. deallocate_register ( converted_vector. size ) ;
448
+ }
449
+ BrilligVariable :: BrilligVector ( _) => { }
450
+ _ => unreachable ! (
451
+ "ICE: {} expected an array or a vector, but got {:?}" ,
452
+ bb_func. name( ) ,
453
+ original_array_or_vector
454
+ ) ,
455
+ }
456
+ }
0 commit comments