Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 86e85f1

Browse files
authored
AllocConstantSize hint (#415)
* Uint256_div_mod hint for hintprocessor * Clippy * Clippy fmt * Fix merge error * Update Cargo.toml newline * Remove duplicate function * Add AllocConstantSize hint * fix * clippy fmt * Remove duplicate definition
1 parent 573c40e commit 86e85f1

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

crates/cairo-1-hint-processor/src/hint_processor.rs

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ use cairo_rs::{
1212
};
1313
use felt::Felt252;
1414
use num_integer::Integer;
15-
use num_traits::identities::Zero;
15+
use num_traits::{identities::Zero, ToPrimitive};
1616
use std::{collections::HashMap, ops::Mul};
1717

1818
/// HintProcessor for Cairo 1 compiler hints.
1919
struct Cairo1HintProcessor {}
2020

21+
/// Execution scope for constant memory allocation.
22+
struct MemoryExecScope {
23+
/// The first free address in the segment.
24+
next_address: Relocatable,
25+
}
26+
2127
fn cell_ref_to_relocatable(cell_ref: &CellRef, vm: &VirtualMachine) -> Relocatable {
2228
let base = match cell_ref.register {
2329
Register::AP => vm.get_ap(),
@@ -101,21 +107,6 @@ impl Cairo1HintProcessor {
101107
.map_err(HintError::from)
102108
}
103109

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-
119110
fn test_less_than_or_equal(
120111
&self,
121112
vm: &mut VirtualMachine,
@@ -262,6 +253,39 @@ impl Cairo1HintProcessor {
262253

263254
Ok(())
264255
}
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+
}
265289
}
266290

267291
impl HintProcessor for Cairo1HintProcessor {
@@ -281,7 +305,13 @@ impl HintProcessor for Cairo1HintProcessor {
281305
let hint = hint_data.downcast_ref::<Hint>().unwrap();
282306
match hint {
283307
Hint::AllocSegment { dst } => self.alloc_segment(vm, dst),
308+
Hint::AllocConstantSize { size, dst } => {
309+
self.alloc_constant_size(vm, exec_scopes, size, dst)
310+
}
284311
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+
}
285315
Hint::SquareRoot { value, dst } => self.square_root(vm, value, dst),
286316
Hint::TestLessThanOrEqual { lhs, rhs, dst } => {
287317
self.test_less_than_or_equal(vm, lhs, rhs, dst)

0 commit comments

Comments
 (0)