Skip to content

Commit

Permalink
feat: add res_operand_get_val_maybe to handle mayberelocatables
Browse files Browse the repository at this point in the history
  • Loading branch information
HermanObst committed Sep 3, 2024
1 parent 0a4a949 commit 78b4689
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
9 changes: 9 additions & 0 deletions cairo-recompile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
set -e

DIR="/Users/hermanobst/starkware/snos/tests/integration/contracts/blockifier_contracts/feature_contracts/cairo1"

for cairo_file in ${DIR}/*.cairo; do
sierra_filename=$(basename $cairo_file .cairo).sierra
sierra_file="$DIR/compiled/$sierra_filename"
cargo run --release --bin starknet-compile -- --allow-warnings --single-file "${cairo_file}" "${sierra_file}" --replace-ids
done
17 changes: 16 additions & 1 deletion vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Cairo1HintProcessor {
self.test_less_than_or_equal(vm, lhs, rhs, dst)
}
Hint::Core(CoreHintBase::Core(CoreHint::TestLessThanOrEqualAddress { lhs, rhs, dst })) => {
self.test_less_than_or_equal(vm, lhs, rhs, dst)
self.test_less_than_or_equal_address(vm, lhs, rhs, dst)
}
Hint::Core(CoreHintBase::Deprecated(DeprecatedHint::Felt252DictRead {
dict_ptr,
Expand Down Expand Up @@ -346,6 +346,21 @@ impl Cairo1HintProcessor {
.map_err(HintError::from)
}

fn test_less_than_or_equal_address(
&self,
vm: &mut VirtualMachine,
lhs: &ResOperand,
rhs: &ResOperand,
dst: &CellRef,
) -> Result<(), HintError> {
let lhs_value = res_operand_get_val_maybe(vm, lhs)?;
let rhs_value = res_operand_get_val_maybe(vm, rhs)?;
let result = Felt252::from((lhs_value <= rhs_value) as u8);

vm.insert_value(cell_ref_to_relocatable(dst, vm)?, result)
.map_err(HintError::from)
}

fn assert_le_find_small_arcs(
&self,
vm: &mut VirtualMachine,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,49 @@ pub(crate) fn get_val(
}
}

/// Fetches the maybe relocatable value of a pointer described by the value at `cell` plus an offset
/// from the vm.
fn get_double_deref_maybe(
vm: &VirtualMachine,
cell: &CellRef,
offset: &Felt252,
) -> Result<MaybeRelocatable, VirtualMachineError> {
let relocatable = get_ptr(vm, cell, offset)?;
vm.get_maybe(&relocatable).ok_or_else(|| {
VirtualMachineError::InvalidMemoryValueTemporaryAddress(Box::new(relocatable))
})
}

/// Fetches the maybe relocatable value of `res_operand` from the vm.
pub(crate) fn res_operand_get_val_maybe(
vm: &VirtualMachine,
res_operand: &ResOperand,
) -> Result<MaybeRelocatable, VirtualMachineError> {
match res_operand {
ResOperand::Deref(cell) => get_mayberelocatable(vm, cell),
ResOperand::DoubleDeref(cell, offset) => {
get_double_deref_maybe(vm, cell, &(*offset).into())
}
ResOperand::Immediate(x) => Ok(Felt252::from(x.value.clone()).into()),
ResOperand::BinOp(op) => {
let a = get_mayberelocatable(vm, &op.a)?;
let b = match &op.b {
DerefOrImmediate::Deref(cell) => get_cell_val(vm, cell)?,
DerefOrImmediate::Immediate(x) => Felt252::from(x.value.clone()),
};
Ok(match op.op {
Operation::Add => a.add_int(&b)?,
Operation::Mul => match a {
MaybeRelocatable::RelocatableValue(_) => {
panic!("mul not implemented for relocatable values")
}
MaybeRelocatable::Int(a) => (a * b).into(),
},
})
}
}
}

pub(crate) fn cell_ref_to_relocatable(
cell_ref: &CellRef,
vm: &VirtualMachine,
Expand Down

0 comments on commit 78b4689

Please sign in to comment.