From a6db7204b043ad0151b3bf52694a85e0a3874e9d Mon Sep 17 00:00:00 2001 From: codyx Date: Wed, 27 Nov 2024 18:25:44 +0400 Subject: [PATCH 1/3] Add decoder hints --- .../hints/lib/decoder/evm/has_type_prefix.rs | 73 +++++++++++++++++++ .../src/hints/lib/decoder/evm/is_byzantium.rs | 62 ++++++++++++++++ .../src/hints/lib/decoder/evm/mod.rs | 3 + .../src/hints/lib/decoder/evm/v_is_encoded.rs | 54 ++++++++++++++ cairo_vm_hints/src/hints/lib/decoder/mod.rs | 1 + cairo_vm_hints/src/hints/lib/mod.rs | 1 + 6 files changed, 194 insertions(+) create mode 100644 cairo_vm_hints/src/hints/lib/decoder/evm/has_type_prefix.rs create mode 100644 cairo_vm_hints/src/hints/lib/decoder/evm/is_byzantium.rs create mode 100644 cairo_vm_hints/src/hints/lib/decoder/evm/mod.rs create mode 100644 cairo_vm_hints/src/hints/lib/decoder/evm/v_is_encoded.rs create mode 100644 cairo_vm_hints/src/hints/lib/decoder/mod.rs diff --git a/cairo_vm_hints/src/hints/lib/decoder/evm/has_type_prefix.rs b/cairo_vm_hints/src/hints/lib/decoder/evm/has_type_prefix.rs new file mode 100644 index 00000000..8c8f9eda --- /dev/null +++ b/cairo_vm_hints/src/hints/lib/decoder/evm/has_type_prefix.rs @@ -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, +) -> 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, +) -> 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(), + )), + } +} diff --git a/cairo_vm_hints/src/hints/lib/decoder/evm/is_byzantium.rs b/cairo_vm_hints/src/hints/lib/decoder/evm/is_byzantium.rs new file mode 100644 index 00000000..b9263639 --- /dev/null +++ b/cairo_vm_hints/src/hints/lib/decoder/evm/is_byzantium.rs @@ -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, +) -> 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, +) -> 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(), + )), + } +} diff --git a/cairo_vm_hints/src/hints/lib/decoder/evm/mod.rs b/cairo_vm_hints/src/hints/lib/decoder/evm/mod.rs new file mode 100644 index 00000000..d109e2a0 --- /dev/null +++ b/cairo_vm_hints/src/hints/lib/decoder/evm/mod.rs @@ -0,0 +1,3 @@ +pub mod has_type_prefix; +pub mod is_byzantium; +pub mod v_is_encoded; diff --git a/cairo_vm_hints/src/hints/lib/decoder/evm/v_is_encoded.rs b/cairo_vm_hints/src/hints/lib/decoder/evm/v_is_encoded.rs new file mode 100644 index 00000000..44a52651 --- /dev/null +++ b/cairo_vm_hints/src/hints/lib/decoder/evm/v_is_encoded.rs @@ -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, +) -> 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, +) -> 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(), + )), + } +} diff --git a/cairo_vm_hints/src/hints/lib/decoder/mod.rs b/cairo_vm_hints/src/hints/lib/decoder/mod.rs new file mode 100644 index 00000000..c469d0c8 --- /dev/null +++ b/cairo_vm_hints/src/hints/lib/decoder/mod.rs @@ -0,0 +1 @@ +pub mod evm; diff --git a/cairo_vm_hints/src/hints/lib/mod.rs b/cairo_vm_hints/src/hints/lib/mod.rs index 6c6ec1aa..585bd722 100644 --- a/cairo_vm_hints/src/hints/lib/mod.rs +++ b/cairo_vm_hints/src/hints/lib/mod.rs @@ -1,3 +1,4 @@ pub mod contract_bootloader; +pub mod decoder; pub mod print; pub mod segments; From 018c19c8b4440e4245d815fb40aff5771307f600 Mon Sep 17 00:00:00 2001 From: codyx Date: Wed, 27 Nov 2024 18:40:36 +0400 Subject: [PATCH 2/3] Insert decoder hints into hint processor --- cairo_vm_hints/src/hint_processor/mod.rs | 3 +++ cairo_vm_hints/src/hints/lib/decoder/evm/has_type_prefix.rs | 5 +++-- cairo_vm_hints/src/hints/lib/decoder/evm/is_byzantium.rs | 4 ++-- cairo_vm_hints/src/hints/lib/decoder/evm/v_is_encoded.rs | 4 ++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cairo_vm_hints/src/hint_processor/mod.rs b/cairo_vm_hints/src/hint_processor/mod.rs index 9407766f..755a4821 100644 --- a/cairo_vm_hints/src/hint_processor/mod.rs +++ b/cairo_vm_hints/src/hint_processor/mod.rs @@ -85,6 +85,9 @@ impl CustomHintProcessor { hints.insert(lib::segments::SEGMENTS_ADD_EVM_MEMORIZER_OFFSET.into(), lib::segments::segments_add_evm_memorizer_offset); hints.insert(lib::segments::SEGMENTS_ADD_EVM_STARKNET_MEMORIZER_INDEX.into(), lib::segments::segments_add_evm_starknet_memorizer_index); hints.insert(lib::segments::SEGMENTS_ADD_STARKNET_MEMORIZER_OFFSET.into(), lib::segments::segments_add_starknet_memorizer_offset); + 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 } diff --git a/cairo_vm_hints/src/hints/lib/decoder/evm/has_type_prefix.rs b/cairo_vm_hints/src/hints/lib/decoder/evm/has_type_prefix.rs index 8c8f9eda..31b3365a 100644 --- a/cairo_vm_hints/src/hints/lib/decoder/evm/has_type_prefix.rs +++ b/cairo_vm_hints/src/hints/lib/decoder/evm/has_type_prefix.rs @@ -9,9 +9,10 @@ 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"; +pub const HINT_HAS_TYPE_PREFIX: &str = + "ids.has_type_prefix = 1 if 0x0 < ids.first_byte < 0x04 else 0"; -fn hint_has_type_prefix( +pub fn hint_has_type_prefix( vm: &mut VirtualMachine, _exec_scope: &mut ExecutionScopes, hint_data: &HintProcessorData, diff --git a/cairo_vm_hints/src/hints/lib/decoder/evm/is_byzantium.rs b/cairo_vm_hints/src/hints/lib/decoder/evm/is_byzantium.rs index b9263639..dd7d5a0d 100644 --- a/cairo_vm_hints/src/hints/lib/decoder/evm/is_byzantium.rs +++ b/cairo_vm_hints/src/hints/lib/decoder/evm/is_byzantium.rs @@ -9,12 +9,12 @@ use cairo_vm::Felt252; use std::cmp::Ordering; use std::collections::HashMap; -const HINT_IS_BYZANTIUM: &str = +pub 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( +pub fn hint_is_byzantium( vm: &mut VirtualMachine, _exec_scope: &mut ExecutionScopes, hint_data: &HintProcessorData, diff --git a/cairo_vm_hints/src/hints/lib/decoder/evm/v_is_encoded.rs b/cairo_vm_hints/src/hints/lib/decoder/evm/v_is_encoded.rs index 44a52651..43caa0cf 100644 --- a/cairo_vm_hints/src/hints/lib/decoder/evm/v_is_encoded.rs +++ b/cairo_vm_hints/src/hints/lib/decoder/evm/v_is_encoded.rs @@ -9,9 +9,9 @@ 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"; +pub const HINT_V_IS_ENCODED: &str = "ids.v_is_encoded = 1 if ids.v.low > 0x7f else 0"; -fn hint_v_is_encoded( +pub fn hint_v_is_encoded( vm: &mut VirtualMachine, _exec_scope: &mut ExecutionScopes, hint_data: &HintProcessorData, From 45b64e5d7299be5d56f3dbd82c381a20932a466b Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Wed, 27 Nov 2024 18:30:59 +0100 Subject: [PATCH 3/3] review - fixes & reorgs --- cairo_vm_hints/src/hint_processor/mod.rs | 6 +- .../hints/lib/decoder/evm/has_type_prefix.rs | 75 ++++++------------- .../src/hints/lib/decoder/evm/is_byzantium.rs | 62 ++++++--------- .../src/hints/lib/decoder/evm/v_is_encoded.rs | 71 ++++++++---------- 4 files changed, 76 insertions(+), 138 deletions(-) diff --git a/cairo_vm_hints/src/hint_processor/mod.rs b/cairo_vm_hints/src/hint_processor/mod.rs index 755a4821..a72eea0d 100644 --- a/cairo_vm_hints/src/hint_processor/mod.rs +++ b/cairo_vm_hints/src/hint_processor/mod.rs @@ -79,15 +79,15 @@ 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); hints.insert(lib::segments::SEGMENTS_ADD_EVM_MEMORIZER_OFFSET.into(), lib::segments::segments_add_evm_memorizer_offset); hints.insert(lib::segments::SEGMENTS_ADD_EVM_STARKNET_MEMORIZER_INDEX.into(), lib::segments::segments_add_evm_starknet_memorizer_index); hints.insert(lib::segments::SEGMENTS_ADD_STARKNET_MEMORIZER_OFFSET.into(), lib::segments::segments_add_starknet_memorizer_offset); - 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 } diff --git a/cairo_vm_hints/src/hints/lib/decoder/evm/has_type_prefix.rs b/cairo_vm_hints/src/hints/lib/decoder/evm/has_type_prefix.rs index 31b3365a..d21c37d6 100644 --- a/cairo_vm_hints/src/hints/lib/decoder/evm/has_type_prefix.rs +++ b/cairo_vm_hints/src/hints/lib/decoder/evm/has_type_prefix.rs @@ -1,14 +1,16 @@ -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::{ + 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 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 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"; @@ -24,51 +26,18 @@ pub fn hint_has_type_prefix( &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(()) -} + let insert = if Felt252::ZERO < first_byte && first_byte < FELT_4 { + Felt252::ZERO + } else { + Felt252::ONE + }; -pub fn run_hint( - vm: &mut VirtualMachine, - exec_scope: &mut ExecutionScopes, - hint_data: &HintProcessorData, - constants: &HashMap, -) -> 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(), - )), - } + insert_value_from_var_name( + "has_type_prefix", + MaybeRelocatable::Int(insert), + vm, + &hint_data.ids_data, + &hint_data.ap_tracking, + ) } diff --git a/cairo_vm_hints/src/hints/lib/decoder/evm/is_byzantium.rs b/cairo_vm_hints/src/hints/lib/decoder/evm/is_byzantium.rs index dd7d5a0d..fd82609f 100644 --- a/cairo_vm_hints/src/hints/lib/decoder/evm/is_byzantium.rs +++ b/cairo_vm_hints/src/hints/lib/decoder/evm/is_byzantium.rs @@ -1,26 +1,26 @@ -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::{ + 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 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; +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"; -const BYZANTIUM_START_BLOCK_NUMBER: Felt252 = Felt252::from_hex_unchecked("0x42AE50"); - pub fn hint_is_byzantium( vm: &mut VirtualMachine, _exec_scope: &mut ExecutionScopes, hint_data: &HintProcessorData, _constants: &HashMap, ) -> Result<(), HintError> { - match get_integer_from_var_name( + let insert = match get_integer_from_var_name( "block_number", vm, &hint_data.ids_data, @@ -28,35 +28,15 @@ pub fn hint_is_byzantium( )? .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, - )?, + Ordering::Less => Felt252::ZERO, + Ordering::Equal | Ordering::Greater => Felt252::ONE, }; - Ok(()) -} - -pub fn run_hint( - vm: &mut VirtualMachine, - exec_scope: &mut ExecutionScopes, - hint_data: &HintProcessorData, - constants: &HashMap, -) -> 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(), - )), - } + insert_value_from_var_name( + "is_byzantium", + MaybeRelocatable::Int(insert), + vm, + &hint_data.ids_data, + &hint_data.ap_tracking, + ) } diff --git a/cairo_vm_hints/src/hints/lib/decoder/evm/v_is_encoded.rs b/cairo_vm_hints/src/hints/lib/decoder/evm/v_is_encoded.rs index 43caa0cf..f7ed9c7c 100644 --- a/cairo_vm_hints/src/hints/lib/decoder/evm/v_is_encoded.rs +++ b/cairo_vm_hints/src/hints/lib/decoder/evm/v_is_encoded.rs @@ -1,13 +1,15 @@ -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::{ + 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 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; +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"; @@ -17,38 +19,25 @@ pub fn hint_v_is_encoded( hint_data: &HintProcessorData, _constants: &HashMap, ) -> 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, - )?, - }; + let v_ptr = + get_relocatable_from_var_name("v", vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - Ok(()) -} + let v = vm + .get_continuous_range(v_ptr, 2)? + .into_iter() + .map(|v| v.get_int().unwrap()) + .collect::>(); -pub fn run_hint( - vm: &mut VirtualMachine, - exec_scope: &mut ExecutionScopes, - hint_data: &HintProcessorData, - constants: &HashMap, -) -> 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(), - )), - } + 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, + ) }