Skip to content

Commit

Permalink
Add big match
Browse files Browse the repository at this point in the history
  • Loading branch information
fmkra committed Oct 25, 2024
1 parent 4d8df02 commit 633bd7e
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 97 deletions.
62 changes: 1 addition & 61 deletions cairo_vm_hints/src/hint_processor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::hints::lib;
use crate::hints::tests;
use crate::hints::run_hint;
use cairo_vm::{
hint_processor::{
builtin_hint_processor::builtin_hint_processor_definition::{
Expand Down Expand Up @@ -28,65 +27,6 @@ impl CustomHintProcessor {
}
}

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() {
tests::print::HINT_PRINT_BREAKLINE => {
tests::print::hint_print_breakline(vm, exec_scope, hint_data, constants)
}
tests::print::HINT_PRINT_PASS => {
tests::print::hint_print_pass(vm, exec_scope, hint_data, constants)
}
tests::mmr_size_generate::HINT_GENERATE_SEQUENTIAL => {
tests::mmr_size_generate::hint_generate_sequential(vm, exec_scope, hint_data, constants)
}
tests::mmr_size_generate::HINT_GENERATE_RANDOM => {
tests::mmr_size_generate::hint_generate_random(vm, exec_scope, hint_data, constants)
}
lib::bit_length::HINT_BIT_LENGTH => {
lib::bit_length::hint_bit_length(vm, exec_scope, hint_data, constants)
}
tests::dw_hack::HINT_BIT_LENGTH_ASSIGN_140 => {
tests::dw_hack::hint_bit_length_assign_140(vm, exec_scope, hint_data, constants)
}
tests::dw_hack::HINT_BIT_LENGTH_ASSIGN_2500 => {
tests::dw_hack::hint_bit_length_assign_2500(vm, exec_scope, hint_data, constants)
}
tests::dw_hack::HINT_BIT_LENGTH_ASSIGN_NEGATIVE_ONE => {
tests::dw_hack::hint_bit_length_assign_negative_one(
vm, exec_scope, hint_data, constants,
)
}
tests::dw_hack::HINT_PRINT_NS => {
tests::dw_hack::hint_print_ns(vm, exec_scope, hint_data, constants)
}
tests::encode_packed_256::HINT_GENERATE_TEST_VECTOR => {
tests::encode_packed_256::hint_generate_test_vector(
vm, exec_scope, hint_data, constants,
)
}
lib::block_header::HINT_RLP_BIGINT_SIZE => {
lib::block_header::hint_rlp_bigint_size(vm, exec_scope, hint_data, constants)
}
lib::mmr::bit_length::MMR_BIT_LENGTH => {
lib::mmr::bit_length::mmr_bit_length(vm, exec_scope, hint_data, constants)
}
tests::construct_mmr::TEST_CONSTRUCT_MMR => {
tests::construct_mmr::test_construct_mmr(vm, exec_scope, hint_data, constants)
}
lib::mmr::left_child::MMR_LEFT_CHILD => {
lib::mmr::left_child::mmr_left_child(vm, exec_scope, hint_data, constants)
}
_ => Err(HintError::UnknownHint(
hint_data.code.to_string().into_boxed_str(),
)),
}
}

impl HintProcessorLogic for CustomHintProcessor {
fn execute_hint(
&mut self,
Expand Down
18 changes: 16 additions & 2 deletions cairo_vm_hints/src/hints/lib/bit_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine};
use cairo_vm::Felt252;
use std::collections::HashMap;

pub const HINT_BIT_LENGTH: &str = "ids.bit_length = ids.x.bit_length()";
const HINT_BIT_LENGTH: &str = "ids.bit_length = ids.x.bit_length()";

pub fn hint_bit_length(
fn hint_bit_length(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
Expand All @@ -27,3 +27,17 @@ pub fn hint_bit_length(

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_BIT_LENGTH => hint_bit_length(vm, exec_scope, hint_data, constants),
_ => Err(HintError::UnknownHint(
hint_data.code.to_string().into_boxed_str(),
)),
}
}
18 changes: 16 additions & 2 deletions cairo_vm_hints/src/hints/lib/block_header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use cairo_vm::Felt252;
use std::cmp::Ordering;
use std::collections::HashMap;

pub const HINT_RLP_BIGINT_SIZE: &str = "memory[ap] = 1 if ids.byte <= 127 else 0";
const HINT_RLP_BIGINT_SIZE: &str = "memory[ap] = 1 if ids.byte <= 127 else 0";

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

pub fn hint_rlp_bigint_size(
fn hint_rlp_bigint_size(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
Expand All @@ -30,3 +30,17 @@ pub fn hint_rlp_bigint_size(

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_RLP_BIGINT_SIZE => hint_rlp_bigint_size(vm, exec_scope, hint_data, constants),
_ => Err(HintError::UnknownHint(
hint_data.code.to_string().into_boxed_str(),
)),
}
}
31 changes: 29 additions & 2 deletions cairo_vm_hints/src/hints/lib/mmr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
pub mod bit_length;
pub mod left_child;
use cairo_vm::{
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData,
types::exec_scope::ExecutionScopes,
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
Felt252,
};
use std::collections::HashMap;

mod bit_length;
mod left_child;

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() {
bit_length::MMR_BIT_LENGTH => {
bit_length::mmr_bit_length(vm, exec_scope, hint_data, constants)
}
left_child::MMR_LEFT_CHILD => {
left_child::mmr_left_child(vm, exec_scope, hint_data, constants)
}
_ => Err(HintError::UnknownHint(
hint_data.code.to_string().into_boxed_str(),
)),
}
}
45 changes: 39 additions & 6 deletions cairo_vm_hints/src/hints/lib/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,40 @@
pub mod bit_length;
use cairo_vm::{
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData,
types::exec_scope::ExecutionScopes,
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
Felt252,
};
use std::collections::HashMap;

pub mod block_header;
pub mod mmr;
pub mod mpt;
pub mod rlp_little;
pub mod utils;
mod bit_length;
mod block_header;
mod mmr;
mod mpt;
mod rlp_little;
mod utils;

pub fn run_hint(
vm: &mut VirtualMachine,
exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let hints = [
bit_length::run_hint,
block_header::run_hint,
mmr::run_hint,
mpt::run_hint,
rlp_little::run_hint,
utils::run_hint,
];

for hint in hints.iter() {
let res = hint(vm, exec_scope, hint_data, constants);
if !matches!(res, Err(HintError::UnknownHint(_))) {
return res;
}
}
Err(HintError::UnknownHint(
hint_data.code.to_string().into_boxed_str(),
))
}
33 changes: 25 additions & 8 deletions cairo_vm_hints/src/hints/lib/mpt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine};
use cairo_vm::Felt252;
use std::collections::HashMap;

pub const HINT_LONG_SHORT_LIST: &str = "from tools.py.hints import is_short_list, is_long_list\nif is_short_list(ids.list_prefix):\n ids.long_short_list = 0\nelif is_long_list(ids.list_prefix):\n ids.long_short_list = 1\nelse:\n raise ValueError(f\"Invalid list prefix: {hex(ids.list_prefix)}. Not a recognized list type.\")";
const HINT_LONG_SHORT_LIST: &str = "from tools.py.hints import is_short_list, is_long_list\nif is_short_list(ids.list_prefix):\n ids.long_short_list = 0\nelif is_long_list(ids.list_prefix):\n ids.long_short_list = 1\nelse:\n raise ValueError(f\"Invalid list prefix: {hex(ids.list_prefix)}. Not a recognized list type.\")";

pub fn hint_long_short_list(
fn hint_long_short_list(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
Expand Down Expand Up @@ -82,9 +82,9 @@ pub fn hint_long_short_list(
}
}

pub const HINT_FIRST_ITEM_TYPE: &str = "from tools.py.hints import is_single_byte, is_short_string\nif is_single_byte(ids.first_item_prefix):\n ids.first_item_type = 0\nelif is_short_string(ids.first_item_prefix):\n ids.first_item_type = 1\nelse:\n raise ValueError(f\"Unsupported first item prefix: {hex(ids.first_item_prefix)}.\")";
const HINT_FIRST_ITEM_TYPE: &str = "from tools.py.hints import is_single_byte, is_short_string\nif is_single_byte(ids.first_item_prefix):\n ids.first_item_type = 0\nelif is_short_string(ids.first_item_prefix):\n ids.first_item_type = 1\nelse:\n raise ValueError(f\"Unsupported first item prefix: {hex(ids.first_item_prefix)}.\")";

pub fn hint_first_item_type(
fn hint_first_item_type(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
Expand Down Expand Up @@ -120,9 +120,9 @@ pub fn hint_first_item_type(
}
}

pub const HINT_SECOND_ITEM_TYPE: &str = "from tools.py.hints import is_single_byte, is_short_string, is_long_string\nif is_single_byte(ids.second_item_prefix):\n ids.second_item_type = 0\nelif is_short_string(ids.second_item_prefix):\n ids.second_item_type = 1\nelif is_long_string(ids.second_item_prefix):\n ids.second_item_type = 2\nelse:\n raise ValueError(f\"Unsupported second item prefix: {hex(ids.second_item_prefix)}.\")";
const HINT_SECOND_ITEM_TYPE: &str = "from tools.py.hints import is_single_byte, is_short_string, is_long_string\nif is_single_byte(ids.second_item_prefix):\n ids.second_item_type = 0\nelif is_short_string(ids.second_item_prefix):\n ids.second_item_type = 1\nelif is_long_string(ids.second_item_prefix):\n ids.second_item_type = 2\nelse:\n raise ValueError(f\"Unsupported second item prefix: {hex(ids.second_item_prefix)}.\")";

pub fn hint_second_item_type(
fn hint_second_item_type(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
Expand Down Expand Up @@ -165,9 +165,9 @@ pub fn hint_second_item_type(
}
}

pub const HINT_ITEM_TYPE: &str = "from tools.py.hints import is_single_byte, is_short_string\nif is_single_byte(ids.item_prefix):\n ids.item_type = 0\nelif is_short_string(ids.item_prefix):\n ids.item_type = 1\nelse:\n raise ValueError(f\"Unsupported item prefix: {hex(ids.item_prefix)} for a branch node. Should be single byte or short string only.\")";
const HINT_ITEM_TYPE: &str = "from tools.py.hints import is_single_byte, is_short_string\nif is_single_byte(ids.item_prefix):\n ids.item_type = 0\nelif is_short_string(ids.item_prefix):\n ids.item_type = 1\nelse:\n raise ValueError(f\"Unsupported item prefix: {hex(ids.item_prefix)} for a branch node. Should be single byte or short string only.\")";

pub fn hint_item_type(
fn hint_item_type(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
Expand Down Expand Up @@ -202,3 +202,20 @@ pub fn hint_item_type(
)))),
}
}

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_LONG_SHORT_LIST => hint_long_short_list(vm, exec_scope, hint_data, constants),
HINT_FIRST_ITEM_TYPE => hint_first_item_type(vm, exec_scope, hint_data, constants),
HINT_SECOND_ITEM_TYPE => hint_second_item_type(vm, exec_scope, hint_data, constants),
HINT_ITEM_TYPE => hint_item_type(vm, exec_scope, hint_data, constants),
_ => Err(HintError::UnknownHint(
hint_data.code.to_string().into_boxed_str(),
)),
}
}
54 changes: 50 additions & 4 deletions cairo_vm_hints/src/hints/lib/rlp_little/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,50 @@
pub mod assert;
pub mod divmod;
pub mod leading_zeros;
pub mod nibbles;
use std::collections::HashMap;

use cairo_vm::{
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData,
types::exec_scope::ExecutionScopes,
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
Felt252,
};

mod assert;
mod divmod;
mod leading_zeros;
mod nibbles;

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() {
assert::HINT_EXPECTED_LEADING_ZEROES => {
assert::hint_expected_leading_zeroes(vm, exec_scope, hint_data, constants)
}
assert::HINT_EXPECTED_NIBBLE => {
assert::hint_expected_nibble(vm, exec_scope, hint_data, constants)
}
divmod::HINT_POW_CUT => divmod::hint_pow_cut(vm, exec_scope, hint_data, constants),
leading_zeros::HINT_EXPECTED_LEADING_ZEROES => {
leading_zeros::hint_expected_leading_zeroes(vm, exec_scope, hint_data, constants)
}
leading_zeros::HINT_EXPECTED_NIBBLE => {
leading_zeros::hint_expected_nibble(vm, exec_scope, hint_data, constants)
}
nibbles::HINT_IS_ZERO => nibbles::hint_is_zero(vm, exec_scope, hint_data, constants),
nibbles::HINT_NIBBLE_FROM_LOW => {
nibbles::hint_nibble_from_low(vm, exec_scope, hint_data, constants)
}
nibbles::HINT_NEEDS_NEXT_WORD => {
nibbles::hint_needs_next_word(vm, exec_scope, hint_data, constants)
}
nibbles::HINT_NEEDS_NEXT_WORD_ENDING => {
nibbles::hint_needs_next_word_ending(vm, exec_scope, hint_data, constants)
}
nibbles::HINT_WORDS_LOOP => nibbles::hint_words_loop(vm, exec_scope, hint_data, constants),
_ => Err(HintError::UnknownHint(
hint_data.code.to_string().into_boxed_str(),
)),
}
}
51 changes: 46 additions & 5 deletions cairo_vm_hints/src/hints/lib/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,46 @@
pub mod assert;
pub mod carry;
pub mod divmod;
pub mod trailing_zeroes;
pub mod write;
use std::collections::HashMap;

use cairo_vm::{
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData,
types::exec_scope::ExecutionScopes,
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
Felt252,
};

mod assert;
mod carry;
mod divmod;
mod trailing_zeroes;
mod write;

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() {
assert::HINT_ASSERT_INTEGER_DIV32 => {
assert::hint_assert_integer_div32(vm, exec_scope, hint_data, constants)
}
assert::HINT_ASSERT_INTEGER_DIV => {
assert::hint_assert_integer_div(vm, exec_scope, hint_data, constants)
}
carry::HINT_CARRY => carry::hint_carry(vm, exec_scope, hint_data, constants),
divmod::HINT_VALUE_DIV32 => divmod::hint_value_div32(vm, exec_scope, hint_data, constants),
divmod::HINT_VALUE_8 => divmod::hint_value_8(vm, exec_scope, hint_data, constants),
divmod::HINT_VALUE_DIV => divmod::hint_value_div(vm, exec_scope, hint_data, constants),
trailing_zeroes::HINT_TRAILING_ZEROES_BYTES => {
trailing_zeroes::hint_trailing_zeroes_bytes(vm, exec_scope, hint_data, constants)
}
write::HINT_WRITE_2 => write::hint_write_2(vm, exec_scope, hint_data, constants),
write::HINT_WRITE_3 => write::hint_write_3(vm, exec_scope, hint_data, constants),
write::HINT_WRITE_4 => write::hint_write_4(vm, exec_scope, hint_data, constants),
write::HINT_WRITE_5 => write::hint_write_5(vm, exec_scope, hint_data, constants),
write::HINT_WRITE_6 => write::hint_write_6(vm, exec_scope, hint_data, constants),
write::HINT_WRITE_7 => write::hint_write_7(vm, exec_scope, hint_data, constants),
_ => Err(HintError::UnknownHint(
hint_data.code.to_string().into_boxed_str(),
)),
}
}
Loading

0 comments on commit 633bd7e

Please sign in to comment.