Skip to content

Commit

Permalink
Add decoder hints
Browse files Browse the repository at this point in the history
  • Loading branch information
codyx committed Nov 27, 2024
1 parent 3f0c8ab commit a6db720
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 0 deletions.
73 changes: 73 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,73 @@
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{
get_integer_from_var_name, insert_value_from_var_name,
};
use cairo_vm::types::exec_scope::ExecutionScopes;
use cairo_vm::types::relocatable::MaybeRelocatable;
use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine};
use cairo_vm::Felt252;
use std::cmp::Ordering;
use std::collections::HashMap;

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

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,
)?;
match first_byte.cmp(&Felt252::ZERO) {
Ordering::Less | Ordering::Equal => {
insert_value_from_var_name(
"has_type_prefix",
MaybeRelocatable::Int(Felt252::ZERO),
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
)?;
}
Ordering::Greater => match first_byte.cmp(&Felt252::from_hex_unchecked("0x04")) {
Ordering::Less => {
insert_value_from_var_name(
"has_type_prefix",
MaybeRelocatable::Int(Felt252::ONE),
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
)?;
}
Ordering::Equal | Ordering::Greater => {
insert_value_from_var_name(
"has_type_prefix",
MaybeRelocatable::Int(Felt252::ZERO),
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
)?;
}
},
};

Ok(())
}

pub fn run_hint(
vm: &mut VirtualMachine,
exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
match hint_data.code.as_str() {
HINT_HAS_TYPE_PREFIX => hint_has_type_prefix(vm, exec_scope, hint_data, constants),
_ => Err(HintError::UnknownHint(
hint_data.code.to_string().into_boxed_str(),
)),
}
}
62 changes: 62 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,62 @@
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{
get_integer_from_var_name, insert_value_from_var_name,
};
use cairo_vm::types::exec_scope::ExecutionScopes;
use cairo_vm::types::relocatable::MaybeRelocatable;
use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine};
use cairo_vm::Felt252;
use std::cmp::Ordering;
use std::collections::HashMap;

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

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

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

Ok(())
}

pub fn run_hint(
vm: &mut VirtualMachine,
exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
match hint_data.code.as_str() {
HINT_IS_BYZANTIUM => hint_is_byzantium(vm, exec_scope, hint_data, constants),
_ => Err(HintError::UnknownHint(
hint_data.code.to_string().into_boxed_str(),
)),
}
}
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;
54 changes: 54 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,54 @@
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{
get_integer_from_var_name, insert_value_from_var_name,
};
use cairo_vm::types::exec_scope::ExecutionScopes;
use cairo_vm::types::relocatable::MaybeRelocatable;
use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine};
use cairo_vm::Felt252;
use std::cmp::Ordering;
use std::collections::HashMap;

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

fn hint_v_is_encoded(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
match get_integer_from_var_name("v", vm, &hint_data.ids_data, &hint_data.ap_tracking)?
.cmp(&Felt252::from_hex_unchecked("0x7f"))
{
Ordering::Less | Ordering::Equal => insert_value_from_var_name(
"v_is_encoded",
MaybeRelocatable::Int(Felt252::ZERO),
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
)?,
Ordering::Greater => insert_value_from_var_name(
"v_is_encoded",
MaybeRelocatable::Int(Felt252::ONE),
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
)?,
};

Ok(())
}

pub fn run_hint(
vm: &mut VirtualMachine,
exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
match hint_data.code.as_str() {
HINT_V_IS_ENCODED => hint_v_is_encoded(vm, exec_scope, hint_data, constants),
_ => Err(HintError::UnknownHint(
hint_data.code.to_string().into_boxed_str(),
)),
}
}
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;

0 comments on commit a6db720

Please sign in to comment.