@@ -12,12 +12,18 @@ use cairo_rs::{
12
12
} ;
13
13
use felt:: Felt252 ;
14
14
use num_integer:: Integer ;
15
- use num_traits:: identities:: Zero ;
15
+ use num_traits:: { identities:: Zero , ToPrimitive } ;
16
16
use std:: { collections:: HashMap , ops:: Mul } ;
17
17
18
18
/// HintProcessor for Cairo 1 compiler hints.
19
19
struct Cairo1HintProcessor { }
20
20
21
+ /// Execution scope for constant memory allocation.
22
+ struct MemoryExecScope {
23
+ /// The first free address in the segment.
24
+ next_address : Relocatable ,
25
+ }
26
+
21
27
fn cell_ref_to_relocatable ( cell_ref : & CellRef , vm : & VirtualMachine ) -> Relocatable {
22
28
let base = match cell_ref. register {
23
29
Register :: AP => vm. get_ap ( ) ,
@@ -101,21 +107,6 @@ impl Cairo1HintProcessor {
101
107
. map_err ( HintError :: from)
102
108
}
103
109
104
- fn square_root (
105
- & self ,
106
- vm : & mut VirtualMachine ,
107
- value : & ResOperand ,
108
- dst : & CellRef ,
109
- ) -> Result < ( ) , HintError > {
110
- let value = res_operand_get_val ( vm, value) ?;
111
- let result = value. sqrt ( ) ;
112
- vm. insert_value (
113
- cell_ref_to_relocatable ( dst, vm) ,
114
- MaybeRelocatable :: from ( result) ,
115
- )
116
- . map_err ( HintError :: from)
117
- }
118
-
119
110
fn test_less_than_or_equal (
120
111
& self ,
121
112
vm : & mut VirtualMachine ,
@@ -262,6 +253,39 @@ impl Cairo1HintProcessor {
262
253
263
254
Ok ( ( ) )
264
255
}
256
+
257
+ fn alloc_constant_size (
258
+ & self ,
259
+ vm : & mut VirtualMachine ,
260
+ exec_scopes : & mut ExecutionScopes ,
261
+ size : & ResOperand ,
262
+ dst : & CellRef ,
263
+ ) -> Result < ( ) , HintError > {
264
+ let object_size = res_operand_get_val ( vm, size) ?
265
+ . to_usize ( )
266
+ . expect ( "Object size too large." ) ;
267
+ let memory_exec_scope =
268
+ match exec_scopes. get_mut_ref :: < MemoryExecScope > ( "memory_exec_scope" ) {
269
+ Ok ( memory_exec_scope) => memory_exec_scope,
270
+ Err ( _) => {
271
+ exec_scopes. assign_or_update_variable (
272
+ "memory_exec_scope" ,
273
+ Box :: new ( MemoryExecScope {
274
+ next_address : vm. add_memory_segment ( ) ,
275
+ } ) ,
276
+ ) ;
277
+ exec_scopes. get_mut_ref :: < MemoryExecScope > ( "memory_exec_scope" ) ?
278
+ }
279
+ } ;
280
+
281
+ vm. insert_value (
282
+ cell_ref_to_relocatable ( dst, vm) ,
283
+ memory_exec_scope. next_address ,
284
+ ) ?;
285
+
286
+ memory_exec_scope. next_address . offset += object_size;
287
+ Ok ( ( ) )
288
+ }
265
289
}
266
290
267
291
impl HintProcessor for Cairo1HintProcessor {
@@ -281,7 +305,13 @@ impl HintProcessor for Cairo1HintProcessor {
281
305
let hint = hint_data. downcast_ref :: < Hint > ( ) . unwrap ( ) ;
282
306
match hint {
283
307
Hint :: AllocSegment { dst } => self . alloc_segment ( vm, dst) ,
308
+ Hint :: AllocConstantSize { size, dst } => {
309
+ self . alloc_constant_size ( vm, exec_scopes, size, dst)
310
+ }
284
311
Hint :: TestLessThan { lhs, rhs, dst } => self . test_less_than ( vm, lhs, rhs, dst) ,
312
+ Hint :: TestLessThanOrEqual { lhs, rhs, dst } => {
313
+ self . test_less_than_or_equal ( vm, lhs, rhs, dst)
314
+ }
285
315
Hint :: SquareRoot { value, dst } => self . square_root ( vm, value, dst) ,
286
316
Hint :: TestLessThanOrEqual { lhs, rhs, dst } => {
287
317
self . test_less_than_or_equal ( vm, lhs, rhs, dst)
0 commit comments