-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
cairo_vm_hints/src/hints/lib/decoder/evm/has_type_prefix.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
)), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
)), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
)), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod evm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |