Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add decoder hints #106

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cairo_vm_hints/src/hint_processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ impl CustomHintProcessor {
hints.insert(lib::contract_bootloader::scopes::ENTER_SCOPE_SYSCALL_HANDLER.into(), lib::contract_bootloader::scopes::enter_scope_syscall_handler);
hints.insert(lib::contract_bootloader::syscall_handler::SYSCALL_HANDLER_CREATE.into(), lib::contract_bootloader::syscall_handler::syscall_handler_create);
hints.insert(lib::contract_bootloader::syscall_handler::SYSCALL_HANDLER_SET_SYSCALL_PTR.into(), lib::contract_bootloader::syscall_handler::syscall_handler_set_syscall_ptr);
hints.insert(lib::decoder::evm::has_type_prefix::HINT_HAS_TYPE_PREFIX.into(), lib::decoder::evm::has_type_prefix::hint_has_type_prefix);
hints.insert(lib::decoder::evm::is_byzantium::HINT_IS_BYZANTIUM.into(), lib::decoder::evm::is_byzantium::hint_is_byzantium);
hints.insert(lib::decoder::evm::v_is_encoded::HINT_V_IS_ENCODED.into(), lib::decoder::evm::v_is_encoded::hint_v_is_encoded);
hints.insert(lib::print::PROGRAM_HASH.into(), lib::print::program_hash);
hints.insert(lib::segments::SEGMENTS_ADD.into(), lib::segments::segments_add);
hints.insert(lib::segments::SEGMENTS_ADD_EVM_MEMORIZER_SEGMENT_INDEX.into(), lib::segments::segments_add_evm_memorizer_segment_index);
Expand Down
43 changes: 43 additions & 0 deletions cairo_vm_hints/src/hints/lib/decoder/evm/has_type_prefix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use cairo_vm::{
hint_processor::builtin_hint_processor::{
builtin_hint_processor_definition::HintProcessorData,
hint_utils::{get_integer_from_var_name, insert_value_from_var_name},
},
types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable},
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
Felt252,
};
use std::collections::HashMap;

const FELT_4: Felt252 = Felt252::from_hex_unchecked("0x04");

pub const HINT_HAS_TYPE_PREFIX: &str =
"ids.has_type_prefix = 1 if 0x0 < ids.first_byte < 0x04 else 0";

pub fn hint_has_type_prefix(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let first_byte = get_integer_from_var_name(
"first_byte",
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
)?;

let insert = if Felt252::ZERO < first_byte && first_byte < FELT_4 {
Felt252::ZERO
} else {
Felt252::ONE
};

insert_value_from_var_name(
"has_type_prefix",
MaybeRelocatable::Int(insert),
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
)
}
42 changes: 42 additions & 0 deletions cairo_vm_hints/src/hints/lib/decoder/evm/is_byzantium.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use cairo_vm::{
hint_processor::builtin_hint_processor::{
builtin_hint_processor_definition::HintProcessorData,
hint_utils::{get_integer_from_var_name, insert_value_from_var_name},
},
types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable},
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
Felt252,
};
use std::{cmp::Ordering, collections::HashMap};

const BYZANTIUM_START_BLOCK_NUMBER: Felt252 = Felt252::from_hex_unchecked("0x42AE50");

pub const HINT_IS_BYZANTIUM: &str =
"ids.is_byzantium = 1 if ids.block_number >= ids.chain_info.byzantium else 0";

pub fn hint_is_byzantium(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let insert = match get_integer_from_var_name(
"block_number",
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
)?
.cmp(&BYZANTIUM_START_BLOCK_NUMBER)
{
Ordering::Less => Felt252::ZERO,
Ordering::Equal | Ordering::Greater => Felt252::ONE,
};

insert_value_from_var_name(
"is_byzantium",
MaybeRelocatable::Int(insert),
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
)
}
3 changes: 3 additions & 0 deletions cairo_vm_hints/src/hints/lib/decoder/evm/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod has_type_prefix;
pub mod is_byzantium;
pub mod v_is_encoded;
43 changes: 43 additions & 0 deletions cairo_vm_hints/src/hints/lib/decoder/evm/v_is_encoded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use cairo_vm::{
hint_processor::builtin_hint_processor::{
builtin_hint_processor_definition::HintProcessorData,
hint_utils::{get_relocatable_from_var_name, insert_value_from_var_name},
},
types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable},
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
Felt252,
};
use std::{cmp::Ordering, collections::HashMap};

const FELT_127: Felt252 = Felt252::from_hex_unchecked("0x7f");

pub const HINT_V_IS_ENCODED: &str = "ids.v_is_encoded = 1 if ids.v.low > 0x7f else 0";

pub fn hint_v_is_encoded(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let v_ptr =
get_relocatable_from_var_name("v", vm, &hint_data.ids_data, &hint_data.ap_tracking)?;

let v = vm
.get_continuous_range(v_ptr, 2)?
.into_iter()
.map(|v| v.get_int().unwrap())
.collect::<Vec<Felt252>>();

let insert = match v[0].cmp(&FELT_127) {
Ordering::Less | Ordering::Equal => Felt252::ZERO,
Ordering::Greater => Felt252::ONE,
};

insert_value_from_var_name(
"v_is_encoded",
MaybeRelocatable::Int(insert),
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
)
}
1 change: 1 addition & 0 deletions cairo_vm_hints/src/hints/lib/decoder/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod evm;
1 change: 1 addition & 0 deletions cairo_vm_hints/src/hints/lib/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod contract_bootloader;
pub mod decoder;
pub mod print;
pub mod segments;
Loading