diff --git a/CHANGELOG.md b/CHANGELOG.md index ada7158afb..2c268f9a6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* refactor: Move hint code match from execute_hint to compile_hint [#2224](https://github.com/lambdaclass/cairo-vm/pull/2224) + * chore: Pin generic-array version to 0.14.7 or lower. [#2227](https://github.com/lambdaclass/cairo-vm/pull/2227) * fix: Added `cairo_1_test_contracts` and `cairo_2_test_contracts` as dependencies for `test-extensive_hints` target [#2201](https://github.com/lambdaclass/cairo-vm/pull/2201) diff --git a/Makefile b/Makefile index 8cae4aa86e..58898a7739 100644 --- a/Makefile +++ b/Makefile @@ -305,6 +305,9 @@ benchmark-action: cairo_bench_programs iai-benchmark-action: cairo_bench_programs cargo bench --bench iai_benchmark +cairo0-benchmark: cairo_bench_programs + cargo bench --bench cairo0_benchmark --features cairo-0-secp-hints + flamegraph: cargo flamegraph --root --bench criterion_benchmark -- --bench diff --git a/README.md b/README.md index d701bad50a..9457aa692b 100644 --- a/README.md +++ b/README.md @@ -324,6 +324,12 @@ Run only the `iai_benchmark` benchmark suite with cargo: cargo bench --bench iai_benchmark ``` +Run only the `cairo0_benchmark` benchmark suite with cargo: + +```bash +cargo bench --bench cairo0_benchmark --features cairo-0-secp-hints +``` + Benchmark the `cairo-vm` in a hyper-threaded environment with the [`examples/hyper_threading/ crate`](examples/hyper_threading/) ```bash make hyper-threading-benchmarks diff --git a/bench/cairo0_benchmark.rs b/bench/cairo0_benchmark.rs new file mode 100644 index 0000000000..4d86811599 --- /dev/null +++ b/bench/cairo0_benchmark.rs @@ -0,0 +1,43 @@ +use cairo_vm::{ + cairo_run::{cairo_run, CairoRunConfig}, + hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, + types::layout_name::LayoutName, +}; +use criterion::{criterion_group, criterion_main, Criterion}; + +fn repeat_hint_cairo0(c: &mut Criterion) { + let program = include_bytes!("../cairo_programs/benchmarks/repeated_hint_cairo0.json"); + let mut hint_processor = BuiltinHintProcessor::new_empty(); + c.bench_function("repeat hint cairo 0", |b| { + b.iter(|| { + let _ = cairo_run( + program, + &CairoRunConfig { + layout: LayoutName::all_cairo, + ..Default::default() + }, + &mut hint_processor, + ); + }); + }); +} + +fn repeat_hint(c: &mut Criterion) { + let program = include_bytes!("../cairo_programs/benchmarks/repeated_hint.json"); + let mut hint_processor = BuiltinHintProcessor::new_empty(); + c.bench_function("repeat hint", |b| { + b.iter(|| { + let _ = cairo_run( + program, + &CairoRunConfig { + layout: LayoutName::all_cairo, + ..Default::default() + }, + &mut hint_processor, + ); + }); + }); +} + +criterion_group!(runner, repeat_hint_cairo0, repeat_hint); +criterion_main!(runner); diff --git a/cairo-vm-cli/Cargo.toml b/cairo-vm-cli/Cargo.toml index 3e51185835..065f371755 100644 --- a/cairo-vm-cli/Cargo.toml +++ b/cairo-vm-cli/Cargo.toml @@ -8,7 +8,7 @@ readme.workspace = true keywords.workspace = true [dependencies] -cairo-vm = { workspace = true, features = ["std", "clap"] } +cairo-vm = { workspace = true, features = ["std", "clap", "test_utils"] } cairo-vm-tracer = { workspace = true, optional = true } clap = { version = "4.3.10", features = ["derive"] } mimalloc = { version = "0.1.37", default-features = false, optional = true } diff --git a/cairo_programs/benchmarks/repeated_hint.cairo b/cairo_programs/benchmarks/repeated_hint.cairo new file mode 100644 index 0000000000..f462838b2a --- /dev/null +++ b/cairo_programs/benchmarks/repeated_hint.cairo @@ -0,0 +1,51 @@ +%builtins range_check + +from starkware.cairo.common.uint256 import Uint256 + +const P_low = 201385395114098847380338600778089168199; +const P_high = 64323764613183177041862057485226039389; + +struct Uint512 { + d0: felt, + d1: felt, + d2: felt, + d3: felt, +} + +func inv_mod_p_uint512{range_check_ptr}(x: Uint512) -> Uint256 { + alloc_locals; + local x_inverse_mod_p: Uint256; + local p: Uint256 = Uint256(P_low, P_high); + // To whitelist + %{ + def pack_512(u, num_bits_shift: int) -> int: + limbs = (u.d0, u.d1, u.d2, u.d3) + return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs)) + + x = pack_512(ids.x, num_bits_shift = 128) + p = ids.p.low + (ids.p.high << 128) + x_inverse_mod_p = pow(x,-1, p) + + x_inverse_mod_p_split = (x_inverse_mod_p & ((1 << 128) - 1), x_inverse_mod_p >> 128) + + ids.x_inverse_mod_p.low = x_inverse_mod_p_split[0] + ids.x_inverse_mod_p.high = x_inverse_mod_p_split[1] + %} + + return x_inverse_mod_p; +} + +func recursive_hint{range_check_ptr: felt}(n: felt) { + if (n == 100000) { + return (); + } + let x = Uint512(101, 2, 15, 61); + let y = inv_mod_p_uint512(x); + + return recursive_hint(n + 1); +} + +func main{range_check_ptr: felt}() { + recursive_hint(0); + return (); +} diff --git a/cairo_programs/benchmarks/repeated_hint_cairo0.cairo b/cairo_programs/benchmarks/repeated_hint_cairo0.cairo new file mode 100644 index 0000000000..3764c70f95 --- /dev/null +++ b/cairo_programs/benchmarks/repeated_hint_cairo0.cairo @@ -0,0 +1,52 @@ +%builtins range_check + +from starkware.cairo.common.secp256r1.ec import ( + EcPoint, +) +from starkware.cairo.common.secp256r1.bigint import nondet_bigint3 +from starkware.cairo.common.cairo_secp.bigint3 import BigInt3 + +func main{range_check_ptr: felt}() { + recursive_hint(0); + + return (); +} + +func recursive_hint{range_check_ptr}(n: felt) { + if (n == 100000) { + return (); + } + + let y = BigInt3(-1,-2,-3); + let z = try_get_point_from_x_prime(y, 0); + + return recursive_hint(n + 1); +} + +func try_get_point_from_x_prime{range_check_ptr}(x: BigInt3, v: felt) -> BigInt3 { + %{ + from starkware.cairo.common.cairo_secp.secp_utils import SECP256R1, pack + from starkware.python.math_utils import y_squared_from_x + + y_square_int = y_squared_from_x( + x=pack(ids.x, PRIME), + alpha=SECP256R1.alpha, + beta=SECP256R1.beta, + field_prime=SECP256R1.prime, + ) + + # Note that (y_square_int ** ((SECP256R1.prime + 1) / 4)) ** 2 = + # = y_square_int ** ((SECP256R1.prime + 1) / 2) = + # = y_square_int ** ((SECP256R1.prime - 1) / 2 + 1) = + # = y_square_int * y_square_int ** ((SECP256R1.prime - 1) / 2) = y_square_int * {+/-}1. + y = pow(y_square_int, (SECP256R1.prime + 1) // 4, SECP256R1.prime) + + # We need to decide whether to take y or prime - y. + if ids.v % 2 == y % 2: + value = y + else: + value = (-y) % SECP256R1.prime + %} + let (y: BigInt3) = nondet_bigint3(); + return y; +} diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 84cda21268..f937ede272 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -104,6 +104,11 @@ path = "../bench/criterion_benchmark.rs" name = "criterion_benchmark" harness = false +[[bench]] +path = "../bench/cairo0_benchmark.rs" +name = "cairo0_benchmark" +harness = false + [[example]] name = "custom_hint" path = "../examples/custom_hint/src/main.rs" diff --git a/vm/src/hint_processor/builtin_hint_processor/bigint.rs b/vm/src/hint_processor/builtin_hint_processor/bigint.rs index 0c14aab3cd..8bc049305d 100644 --- a/vm/src/hint_processor/builtin_hint_processor/bigint.rs +++ b/vm/src/hint_processor/builtin_hint_processor/bigint.rs @@ -35,6 +35,7 @@ pub fn bigint_pack_div_mod_hint( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let p: BigInt = BigInt3::from_var_name("P", vm, ids_data, ap_tracking)?.pack86(); @@ -76,6 +77,7 @@ pub fn bigint_safe_div_hint( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let res = exec_scopes.get::("res")?; let y = exec_scopes.get::("y")?; @@ -103,6 +105,7 @@ mod test { use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData; use crate::hint_processor::builtin_hint_processor::hint_code; use crate::hint_processor::hint_processor_definition::{HintProcessorLogic, HintReference}; + use crate::serde::deserialize_program::ApTracking; use crate::types::exec_scope::ExecutionScopes; use crate::utils::test_utils::*; use assert_matches::assert_matches; diff --git a/vm/src/hint_processor/builtin_hint_processor/blake2s_utils.rs b/vm/src/hint_processor/builtin_hint_processor/blake2s_utils.rs index 8dfcedf030..f9c4102bf1 100644 --- a/vm/src/hint_processor/builtin_hint_processor/blake2s_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/blake2s_utils.rs @@ -2,6 +2,7 @@ use crate::hint_processor::hint_processor_utils::felt_to_usize; use crate::stdlib::{borrow::Cow, collections::HashMap, prelude::*}; use crate::types::errors::math_errors::MathError; +use crate::types::exec_scope::ExecutionScopes; use crate::Felt252; use crate::{ hint_processor::{ @@ -67,8 +68,10 @@ fn compute_blake2s_func(vm: &mut VirtualMachine, output_ptr: Relocatable) -> Res */ pub fn compute_blake2s( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let output = get_ptr_from_var_name("output", vm, ids_data, ap_tracking)?; compute_blake2s_func(vm, output) @@ -98,8 +101,10 @@ pub fn compute_blake2s( */ pub fn finalize_blake2s( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { const N_PACKED_INSTANCES: usize = 7; let blake2s_ptr_end = get_ptr_from_var_name("blake2s_ptr_end", vm, ids_data, ap_tracking)?; @@ -146,8 +151,10 @@ pub fn finalize_blake2s( */ pub fn finalize_blake2s_v3( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { const N_PACKED_INSTANCES: usize = 7; let blake2s_ptr_end = get_ptr_from_var_name("blake2s_ptr_end", vm, ids_data, ap_tracking)?; @@ -178,8 +185,10 @@ pub fn finalize_blake2s_v3( */ pub fn blake2s_add_uint256( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { //Get variables from ids let data_ptr = get_ptr_from_var_name("data", vm, ids_data, ap_tracking)?; @@ -214,8 +223,10 @@ pub fn blake2s_add_uint256( */ pub fn blake2s_add_uint256_bigend( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { //Get variables from ids let data_ptr = get_ptr_from_var_name("data", vm, ids_data, ap_tracking)?; @@ -250,8 +261,10 @@ memory[ap] = (ids.end != ids.packed_values) and (memory[ids.packed_values] < 2** */ pub fn is_less_than_63_bits_and_not_end( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let end = get_ptr_from_var_name("end", vm, ids_data, ap_tracking)?; let packed_values = get_ptr_from_var_name("packed_values", vm, ids_data, ap_tracking)?; @@ -282,8 +295,10 @@ for i in range(ids.packed_values_len): */ pub fn blake2s_unpack_felts( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let packed_values_len = get_integer_from_var_name("packed_values_len", vm, ids_data, ap_tracking)?; @@ -346,8 +361,10 @@ Note: This hint belongs to the blake2s lib in cario_examples */ pub fn example_blake2s_compress( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let blake2s_start = get_ptr_from_var_name("blake2s_start", vm, ids_data, ap_tracking)?; let output = get_ptr_from_var_name("output", vm, ids_data, ap_tracking)?; diff --git a/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs b/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs index 029b9dfa3e..8c5b131df2 100644 --- a/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs +++ b/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs @@ -1,116 +1,128 @@ -use super::blake2s_utils::example_blake2s_compress; -use super::{ - blake2s_utils::{blake2s_unpack_felts, finalize_blake2s_v3, is_less_than_63_bits_and_not_end}, - ec_recover::{ - ec_recover_divmod_n_packed, ec_recover_product_div_m, ec_recover_product_mod, - ec_recover_sub_a_b, - }, - excess_balance::excess_balance_hint, - field_arithmetic::{u256_get_square_root, u384_get_square_root, uint384_div}, - mod_circuit::{run_p_mod_circuit, run_p_mod_circuit_with_large_batch_size}, - secp::{ - ec_utils::{ - compute_doubling_slope_external_consts, compute_slope_and_assing_secp_p, - ec_double_assign_new_y, ec_mul_inner, ec_negate_embedded_secp_p, - ec_negate_import_secp_p, square_slope_minus_xs, - }, - secp_utils::{ALPHA, ALPHA_V2, SECP_P, SECP_P_V2}, - }, - uint384::sub_reduced_a_and_reduced_b, - vrf::{ - fq::{inv_mod_p_uint256, uint512_unsigned_div_rem}, - inv_mod_p_uint512::inv_mod_p_uint512, - pack::*, - }, +use crate::hint_processor::builtin_hint_processor::bigint::{ + bigint_pack_div_mod_hint, bigint_safe_div_hint, }; -use crate::Felt252; -use crate::{ - hint_processor::builtin_hint_processor::secp::secp_utils::{SECP256R1_ALPHA, SECP256R1_P}, - utils::CAIRO_PRIME, +use crate::hint_processor::builtin_hint_processor::blake2s_utils::{ + blake2s_add_uint256, blake2s_add_uint256_bigend, blake2s_unpack_felts, compute_blake2s, + example_blake2s_compress, finalize_blake2s, finalize_blake2s_v3, + is_less_than_63_bits_and_not_end, +}; +use crate::hint_processor::builtin_hint_processor::cairo_keccak::keccak_hints::{ + block_permutation_v1, block_permutation_v2, cairo_keccak_finalize_v1, cairo_keccak_finalize_v2, + cairo_keccak_is_full_word, compare_bytes_in_word_nondet, + compare_keccak_full_rate_in_bytes_nondet, keccak_write_args, +}; +use crate::hint_processor::builtin_hint_processor::dict_hint_utils::{ + default_dict_new, dict_new, dict_read, dict_squash_copy_dict, dict_squash_update_ptr, + dict_update, dict_write, +}; +use crate::hint_processor::builtin_hint_processor::ec_recover::{ + ec_recover_divmod_n_packed, ec_recover_product_div_m, ec_recover_product_mod, + ec_recover_sub_a_b, +}; +use crate::hint_processor::builtin_hint_processor::ec_utils::{ + chained_ec_op_random_ec_point_hint, random_ec_point_hint, recover_y_hint, +}; +use crate::hint_processor::builtin_hint_processor::excess_balance::excess_balance_hint; +use crate::hint_processor::builtin_hint_processor::field_arithmetic::{ + u256_get_square_root, u384_get_square_root, uint384_div, +}; +use crate::hint_processor::builtin_hint_processor::find_element_hint::{ + find_element, search_sorted_lower, +}; +use crate::hint_processor::builtin_hint_processor::garaga::get_felt_bitlenght; +use crate::hint_processor::builtin_hint_processor::keccak_utils::{ + split_input_12_wrapper, split_input_15_wrapper, split_input_3_wrapper, split_input_6_wrapper, + split_input_9_wrapper, split_n_bytes, split_output_0_wrapper, split_output_1_wrapper, + split_output_mid_low_high, unsafe_keccak, unsafe_keccak_finalize, +}; +use crate::hint_processor::builtin_hint_processor::memcpy_hint_utils::enter_scope; +use crate::hint_processor::builtin_hint_processor::memset_utils::{ + memset_step_loop_copying_wrapper, memset_step_loop_wrapper, +}; +use crate::hint_processor::builtin_hint_processor::mod_circuit::{ + run_p_mod_circuit, run_p_mod_circuit_with_large_batch_size, +}; +use crate::hint_processor::builtin_hint_processor::poseidon_utils::{ + elements_over_ten_wrapper, elements_over_two_wrapper, n_greater_than_10, n_greater_than_2, +}; +use crate::hint_processor::builtin_hint_processor::pow_utils::pow; +#[cfg(feature = "test_utils")] +use crate::hint_processor::builtin_hint_processor::print::{print_array, print_dict, print_felt}; +use crate::hint_processor::builtin_hint_processor::secp::bigint_utils::{ + bigint_to_uint256, hi_max_bitlen, nondet_bigint3, +}; +use crate::hint_processor::builtin_hint_processor::secp::ec_utils::{ + compute_doubling_slope_external_consts, compute_doubling_slope_v1_wrapper, + compute_doubling_slope_v2_wrapper, compute_doubling_slope_v3_wrapper, + compute_doubling_slope_v4_wrapper, compute_doubling_slope_v5_wrapper, + compute_slope_and_assing_secp_p_v2_wrapper, compute_slope_and_assing_secp_p_whitelist_wrapper, + compute_slope_and_assing_secp_p_wrapper, compute_slope_v1_wrapper, compute_slope_v2_wrapper, + di_bit, ec_double_assign_new_x_v1_wrapper, ec_double_assign_new_x_v2_wrapper, + ec_double_assign_new_x_v3_wrapper, ec_double_assign_new_x_v4_wrapper, ec_double_assign_new_y, + ec_mul_inner, ec_negate_embedded_secp_p, ec_negate_import_secp_p, + fast_ec_add_assign_new_x_v2_wrapper, fast_ec_add_assign_new_x_v3_wrapper, + fast_ec_add_assign_new_x_wrapper, fast_ec_add_assign_new_y, import_secp256r1_alpha, + import_secp256r1_n, import_secp256r1_p, quad_bit, square_slope_minus_xs, +}; +use crate::hint_processor::builtin_hint_processor::secp::field_utils::{ + is_zero_assign_scope_variables, is_zero_assign_scope_variables_external_const, is_zero_nondet, + is_zero_pack, is_zero_pack_external_secp, reduce_v1, reduce_v2, + verify_zero_with_external_const, verify_zero_wrapper, verify_zero_wrapper_v2, +}; +use crate::hint_processor::builtin_hint_processor::secp::signature::{ + div_mod_n_packed_divmod, div_mod_n_packed_external_n, div_mod_n_safe_div_plus_one_wrapper, + div_mod_n_safe_div_wrapper, div_mod_n_safe_div_xs_wrapper, get_point_from_x, + pack_modn_div_modn, +}; +use crate::hint_processor::builtin_hint_processor::segments::{relocate_segment, temporary_array}; +use crate::hint_processor::builtin_hint_processor::set::set_add; +use crate::hint_processor::builtin_hint_processor::sha256_utils::{ + sha256_finalize, sha256_input, sha256_main_arbitrary_input_length, + sha256_main_constant_input_length, +}; +use crate::hint_processor::builtin_hint_processor::signature::verify_ecdsa_signature; +#[cfg(feature = "test_utils")] +use crate::hint_processor::builtin_hint_processor::skip_next_instruction::skip_next_instruction; +use crate::hint_processor::builtin_hint_processor::squash_dict_utils::{ + squash_dict, squash_dict_inner_assert_len_keys, squash_dict_inner_check_access_index, + squash_dict_inner_continue_loop, squash_dict_inner_first_iteration, + squash_dict_inner_len_assert, squash_dict_inner_next_key, squash_dict_inner_skip_loop, + squash_dict_inner_used_accesses_assert, +}; +use crate::hint_processor::builtin_hint_processor::uint256_utils::{ + split_64, uint128_add, uint256_add_low_wrapper, uint256_add_wrapper, + uint256_expanded_unsigned_div_rem, uint256_mul_div_mod, uint256_signed_nn, + uint256_sqrt_felt_wrapper, uint256_sqrt_wrapper, uint256_sub, uint256_unsigned_div_rem, +}; +use crate::hint_processor::builtin_hint_processor::uint384::{ + add_no_uint384_check, sub_reduced_a_and_reduced_b, uint384_signed_nn, uint384_split_128, + uint384_sqrt, uint384_unsigned_div_rem, }; +use crate::hint_processor::builtin_hint_processor::uint384_extension::unsigned_div_rem_uint768_by_uint384; +use crate::hint_processor::builtin_hint_processor::usort::{ + usort_body, usort_enter_scope, verify_multiplicity_assert, verify_multiplicity_body, + verify_usort, +}; +use crate::hint_processor::builtin_hint_processor::vrf::fq::{ + inv_mod_p_uint256, uint512_unsigned_div_rem, +}; +use crate::hint_processor::builtin_hint_processor::vrf::inv_mod_p_uint512::inv_mod_p_uint512; +use crate::hint_processor::builtin_hint_processor::vrf::pack::{ + ed25519_is_zero_assign_scope_vars, ed25519_is_zero_pack, ed25519_reduce, +}; +use crate::hint_processor::hint_processor_definition::get_ids_data; +use crate::{any_box, Felt252}; use crate::{ - hint_processor::{ - builtin_hint_processor::secp::ec_utils::{ - ec_double_assign_new_x, ec_double_assign_new_x_v2, - }, - hint_processor_definition::HintProcessorLogic, - }, + hint_processor::hint_processor_definition::HintProcessorLogic, vm::runners::cairo_runner::{ResourceTracker, RunResources}, }; use crate::{ hint_processor::{ builtin_hint_processor::{ - bigint::{bigint_pack_div_mod_hint, bigint_safe_div_hint}, - blake2s_utils::{ - blake2s_add_uint256, blake2s_add_uint256_bigend, compute_blake2s, finalize_blake2s, - }, - cairo_keccak::keccak_hints::{ - block_permutation_v1, block_permutation_v2, cairo_keccak_finalize_v1, - cairo_keccak_finalize_v2, cairo_keccak_is_full_word, compare_bytes_in_word_nondet, - compare_keccak_full_rate_in_bytes_nondet, keccak_write_args, - }, - dict_hint_utils::{ - default_dict_new, dict_new, dict_read, dict_squash_copy_dict, - dict_squash_update_ptr, dict_update, dict_write, - }, - ec_utils::{chained_ec_op_random_ec_point_hint, random_ec_point_hint, recover_y_hint}, - find_element_hint::{find_element, search_sorted_lower}, - garaga::get_felt_bitlenght, hint_code, - keccak_utils::{ - split_input, split_n_bytes, split_output, split_output_mid_low_high, unsafe_keccak, - unsafe_keccak_finalize, - }, math_utils::*, - memcpy_hint_utils::{add_segment, enter_scope, exit_scope, memcpy_enter_scope}, - memset_utils::{memset_enter_scope, memset_step_loop}, - poseidon_utils::{elements_over_x, n_greater_than_10, n_greater_than_2}, - pow_utils::pow, - secp::{ - bigint_utils::{bigint_to_uint256, hi_max_bitlen, nondet_bigint3}, - ec_utils::{ - compute_doubling_slope, compute_slope, di_bit, fast_ec_add_assign_new_x, - fast_ec_add_assign_new_y, import_secp256r1_alpha, import_secp256r1_n, - import_secp256r1_p, quad_bit, - }, - field_utils::{ - is_zero_assign_scope_variables, is_zero_assign_scope_variables_external_const, - is_zero_nondet, is_zero_pack, is_zero_pack_external_secp, reduce_v1, reduce_v2, - verify_zero, verify_zero_with_external_const, - }, - signature::{ - div_mod_n_packed_divmod, div_mod_n_packed_external_n, div_mod_n_safe_div, - get_point_from_x, pack_modn_div_modn, - }, - }, - segments::{relocate_segment, temporary_array}, - set::set_add, - sha256_utils::{ - sha256_finalize, sha256_input, sha256_main_arbitrary_input_length, - sha256_main_constant_input_length, - }, - signature::verify_ecdsa_signature, - squash_dict_utils::{ - squash_dict, squash_dict_inner_assert_len_keys, - squash_dict_inner_check_access_index, squash_dict_inner_continue_loop, - squash_dict_inner_first_iteration, squash_dict_inner_len_assert, - squash_dict_inner_next_key, squash_dict_inner_skip_loop, - squash_dict_inner_used_accesses_assert, - }, - uint256_utils::{ - split_64, uint128_add, uint256_add, uint256_expanded_unsigned_div_rem, - uint256_mul_div_mod, uint256_signed_nn, uint256_sqrt, uint256_sub, - uint256_unsigned_div_rem, - }, - uint384::{ - add_no_uint384_check, uint384_signed_nn, uint384_split_128, uint384_sqrt, - uint384_unsigned_div_rem, - }, - uint384_extension::unsigned_div_rem_uint768_by_uint384, - usort::{ - usort_body, usort_enter_scope, verify_multiplicity_assert, - verify_multiplicity_body, verify_usort, - }, + memcpy_hint_utils::{add_segment, exit_scope, memcpy_enter_scope}, + memset_utils::memset_enter_scope, }, hint_processor_definition::HintReference, }, @@ -122,11 +134,16 @@ use crate::{ #[cfg(feature = "cairo-0-secp-hints")] use crate::hint_processor::builtin_hint_processor::secp::cairo0_hints; -#[cfg(feature = "test_utils")] -use crate::hint_processor::builtin_hint_processor::{ - print::{print_array, print_dict, print_felt}, - skip_next_instruction::skip_next_instruction, -}; + +type HintFuncPointer = Option< + fn( + &mut VirtualMachine, + &mut ExecutionScopes, + &HashMap, + &ApTracking, + &HashMap, + ) -> Result<(), HintError>, +>; pub struct HintProcessorData { pub code: String, @@ -134,6 +151,7 @@ pub struct HintProcessorData { pub ids_data: HashMap, pub accessible_scopes: Vec, pub constants: Rc>, + pub f: HintFuncPointer, } impl HintProcessorData { @@ -144,6 +162,7 @@ impl HintProcessorData { ids_data, accessible_scopes: vec![], constants: Default::default(), + f: Default::default(), } } } @@ -206,832 +225,323 @@ impl HintProcessorLogic for BuiltinHintProcessor { constants, ); } - match &*hint_data.code { - hint_code::ADD_SEGMENT => add_segment(vm), - hint_code::IS_NN => is_nn(vm, &hint_data.ids_data, &hint_data.ap_tracking), - hint_code::IS_NN_OUT_OF_RANGE => { - is_nn_out_of_range(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::ASSERT_LE_FELT => assert_le_felt( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, + + let hint_func = match hint_data.f { + Some(f) => f, + None => { + return Err(HintError::UnknownHint( + // TODO: Check if we want to continue to return this error. If not, keep in mind that SimplifiedOsHintProcessor depends on this error (check execute_hint_extensive) + hint_data.code.to_string().into_boxed_str(), + )); + } + }; + + hint_func( + vm, + exec_scopes, + &hint_data.ids_data, + &hint_data.ap_tracking, + constants, + ) + } + + fn compile_hint( + &self, + //Block of hint code as String + hint_code: &str, + //Ap Tracking Data corresponding to the Hint + ap_tracking_data: &ApTracking, + //Map from variable name to reference id number + //(may contain other variables aside from those used by the hint) + reference_ids: &HashMap, + //List of all references (key corresponds to element of the previous dictionary) + references: &[HintReference], + // List of accessible scopes in the hint + accessible_scopes: &[String], + // Identifiers stored in the hint's program. + constants: Rc>, + ) -> Result, crate::vm::errors::vm_errors::VirtualMachineError> { + let ids_data = get_ids_data(reference_ids, references)?; + + if self.extra_hints.contains_key(hint_code) { + // TODO: This is to handle the extra_hints. Handle this case nicely + return Ok(any_box!(HintProcessorData { + code: hint_code.to_string(), + ap_tracking: ap_tracking_data.clone(), + ids_data, + accessible_scopes: accessible_scopes.to_vec(), constants, - ), - hint_code::ASSERT_LE_FELT_EXCLUDED_2 => assert_le_felt_excluded_2(exec_scopes), - hint_code::ASSERT_LE_FELT_EXCLUDED_1 => assert_le_felt_excluded_1(vm, exec_scopes), - hint_code::ASSERT_LE_FELT_EXCLUDED_0 => assert_le_felt_excluded_0(vm, exec_scopes), - hint_code::IS_LE_FELT => is_le_felt(vm, &hint_data.ids_data, &hint_data.ap_tracking), - hint_code::ASSERT_250_BITS => { - assert_250_bit(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants) - } - hint_code::IS_250_BITS => is_250_bits(vm, &hint_data.ids_data, &hint_data.ap_tracking), - hint_code::IS_ADDR_BOUNDED => { - is_addr_bounded(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants) - } - hint_code::IS_POSITIVE => is_positive(vm, &hint_data.ids_data, &hint_data.ap_tracking), - hint_code::SPLIT_INT_ASSERT_RANGE => { - split_int_assert_range(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::SPLIT_INT => split_int(vm, &hint_data.ids_data, &hint_data.ap_tracking), - hint_code::ASSERT_NOT_EQUAL => { - assert_not_equal(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::ASSERT_NN => assert_nn(vm, &hint_data.ids_data, &hint_data.ap_tracking), - hint_code::SQRT => sqrt(vm, &hint_data.ids_data, &hint_data.ap_tracking), - hint_code::ASSERT_NOT_ZERO => { - assert_not_zero(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::IS_QUAD_RESIDUE => { - is_quad_residue(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::VM_EXIT_SCOPE => exit_scope(exec_scopes), - hint_code::MEMCPY_ENTER_SCOPE => { - memcpy_enter_scope(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::MEMSET_ENTER_SCOPE => { - memset_enter_scope(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::MEMCPY_CONTINUE_COPYING => memset_step_loop( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - "continue_copying", - ), - hint_code::MEMSET_CONTINUE_LOOP => memset_step_loop( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - "continue_loop", - ), - hint_code::SPLIT_FELT => { - split_felt(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants) - } - hint_code::UNSIGNED_DIV_REM => { - unsigned_div_rem(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::SIGNED_DIV_REM => { - signed_div_rem(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::ASSERT_LT_FELT => { - assert_lt_felt(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::FIND_ELEMENT => { - find_element(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::SEARCH_SORTED_LOWER => { - search_sorted_lower(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::POW => pow(vm, &hint_data.ids_data, &hint_data.ap_tracking), - hint_code::SET_ADD => set_add(vm, &hint_data.ids_data, &hint_data.ap_tracking), - hint_code::DICT_NEW => dict_new(vm, exec_scopes), - hint_code::DICT_READ => { - dict_read(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::DICT_WRITE => { - dict_write(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::DEFAULT_DICT_NEW => { - default_dict_new(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::SQUASH_DICT_INNER_FIRST_ITERATION => squash_dict_inner_first_iteration( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - ), - hint_code::USORT_ENTER_SCOPE => usort_enter_scope(exec_scopes), - hint_code::USORT_BODY => { - usort_body(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::USORT_VERIFY => { - verify_usort(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::USORT_VERIFY_MULTIPLICITY_ASSERT => verify_multiplicity_assert(exec_scopes), - hint_code::USORT_VERIFY_MULTIPLICITY_BODY => verify_multiplicity_body( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - ), - hint_code::BLAKE2S_COMPUTE => { - compute_blake2s(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::VERIFY_ZERO_V1 | hint_code::VERIFY_ZERO_V2 => verify_zero( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - &SECP_P, - ), - hint_code::VERIFY_ZERO_V3 => verify_zero( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - &SECP_P_V2, - ), - hint_code::VERIFY_ZERO_EXTERNAL_SECP => verify_zero_with_external_const( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - ), - hint_code::NONDET_BIGINT3_V1 | hint_code::NONDET_BIGINT3_V2 => { - nondet_bigint3(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::REDUCE_V1 => { - reduce_v1(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::REDUCE_V2 => { - reduce_v2(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::REDUCE_ED25519 => { - ed25519_reduce(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::BLAKE2S_FINALIZE | hint_code::BLAKE2S_FINALIZE_V2 => { - finalize_blake2s(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::BLAKE2S_FINALIZE_V3 => { - finalize_blake2s_v3(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::BLAKE2S_ADD_UINT256 => { - blake2s_add_uint256(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::BLAKE2S_ADD_UINT256_BIGEND => { - blake2s_add_uint256_bigend(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::IS_LESS_THAN_63_BITS_AND_NOT_END => { - is_less_than_63_bits_and_not_end(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::BLAKE2S_UNPACK_FELTS => { - blake2s_unpack_felts(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::UNSAFE_KECCAK => { - unsafe_keccak(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::UNSAFE_KECCAK_FINALIZE => { - unsafe_keccak_finalize(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::SQUASH_DICT_INNER_SKIP_LOOP => squash_dict_inner_skip_loop( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - ), - hint_code::SQUASH_DICT_INNER_CHECK_ACCESS_INDEX => { - squash_dict_inner_check_access_index( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - ) - } - hint_code::SQUASH_DICT_INNER_CONTINUE_LOOP => squash_dict_inner_continue_loop( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - ), - hint_code::SQUASH_DICT_INNER_ASSERT_LEN_KEYS => { - squash_dict_inner_assert_len_keys(exec_scopes) - } - hint_code::SQUASH_DICT_INNER_LEN_ASSERT => squash_dict_inner_len_assert(exec_scopes), + f: None + })); + } + + let f = match hint_code { + hint_code::ADD_SEGMENT => add_segment, + hint_code::IS_NN => is_nn, + hint_code::IS_NN_OUT_OF_RANGE => is_nn_out_of_range, + hint_code::ASSERT_LE_FELT => assert_le_felt, + hint_code::ASSERT_LE_FELT_EXCLUDED_2 => assert_le_felt_excluded_2, + hint_code::ASSERT_LE_FELT_EXCLUDED_1 => assert_le_felt_excluded_1, + hint_code::ASSERT_LE_FELT_EXCLUDED_0 => assert_le_felt_excluded_0, + hint_code::IS_LE_FELT => is_le_felt, + hint_code::ASSERT_250_BITS => assert_250_bit, + hint_code::IS_250_BITS => is_250_bits, + hint_code::IS_ADDR_BOUNDED => is_addr_bounded, + hint_code::IS_POSITIVE => is_positive, + hint_code::SPLIT_INT_ASSERT_RANGE => split_int_assert_range, + hint_code::SPLIT_INT => split_int, + hint_code::ASSERT_NOT_EQUAL => assert_not_equal, + hint_code::ASSERT_NN => assert_nn, + hint_code::SQRT => sqrt, + hint_code::ASSERT_NOT_ZERO => assert_not_zero, + hint_code::IS_QUAD_RESIDUE => is_quad_residue, + hint_code::VM_EXIT_SCOPE => exit_scope, + hint_code::MEMCPY_ENTER_SCOPE => memcpy_enter_scope, + hint_code::MEMSET_ENTER_SCOPE => memset_enter_scope, + hint_code::MEMCPY_CONTINUE_COPYING => memset_step_loop_copying_wrapper, + hint_code::MEMSET_CONTINUE_LOOP => memset_step_loop_wrapper, + hint_code::SPLIT_FELT => split_felt, + hint_code::UNSIGNED_DIV_REM => unsigned_div_rem, + hint_code::SIGNED_DIV_REM => signed_div_rem, + hint_code::ASSERT_LT_FELT => assert_lt_felt, + hint_code::FIND_ELEMENT => find_element, + hint_code::SEARCH_SORTED_LOWER => search_sorted_lower, + hint_code::POW => pow, + hint_code::SET_ADD => set_add, + hint_code::DICT_NEW => dict_new, + hint_code::DICT_READ => dict_read, + hint_code::DICT_WRITE => dict_write, + hint_code::DEFAULT_DICT_NEW => default_dict_new, + hint_code::SQUASH_DICT_INNER_FIRST_ITERATION => squash_dict_inner_first_iteration, + hint_code::USORT_ENTER_SCOPE => usort_enter_scope, + hint_code::USORT_BODY => usort_body, + hint_code::USORT_VERIFY => verify_usort, + hint_code::USORT_VERIFY_MULTIPLICITY_ASSERT => verify_multiplicity_assert, + hint_code::USORT_VERIFY_MULTIPLICITY_BODY => verify_multiplicity_body, + hint_code::BLAKE2S_COMPUTE => compute_blake2s, + hint_code::VERIFY_ZERO_V1 | hint_code::VERIFY_ZERO_V2 => verify_zero_wrapper, + hint_code::VERIFY_ZERO_V3 => verify_zero_wrapper_v2, + hint_code::VERIFY_ZERO_EXTERNAL_SECP => verify_zero_with_external_const, + hint_code::NONDET_BIGINT3_V1 | hint_code::NONDET_BIGINT3_V2 => nondet_bigint3, + hint_code::REDUCE_V1 => reduce_v1, + hint_code::REDUCE_V2 => reduce_v2, + hint_code::REDUCE_ED25519 => ed25519_reduce, + hint_code::BLAKE2S_FINALIZE | hint_code::BLAKE2S_FINALIZE_V2 => finalize_blake2s, + hint_code::BLAKE2S_FINALIZE_V3 => finalize_blake2s_v3, + hint_code::BLAKE2S_ADD_UINT256 => blake2s_add_uint256, + hint_code::BLAKE2S_ADD_UINT256_BIGEND => blake2s_add_uint256_bigend, + hint_code::IS_LESS_THAN_63_BITS_AND_NOT_END => is_less_than_63_bits_and_not_end, + hint_code::BLAKE2S_UNPACK_FELTS => blake2s_unpack_felts, + hint_code::UNSAFE_KECCAK => unsafe_keccak, + hint_code::UNSAFE_KECCAK_FINALIZE => unsafe_keccak_finalize, + hint_code::SQUASH_DICT_INNER_SKIP_LOOP => squash_dict_inner_skip_loop, + hint_code::SQUASH_DICT_INNER_CHECK_ACCESS_INDEX => squash_dict_inner_check_access_index, + hint_code::SQUASH_DICT_INNER_CONTINUE_LOOP => squash_dict_inner_continue_loop, + hint_code::SQUASH_DICT_INNER_ASSERT_LEN_KEYS => squash_dict_inner_assert_len_keys, + hint_code::SQUASH_DICT_INNER_LEN_ASSERT => squash_dict_inner_len_assert, hint_code::SQUASH_DICT_INNER_USED_ACCESSES_ASSERT => { - squash_dict_inner_used_accesses_assert( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - ) - } - hint_code::SQUASH_DICT_INNER_NEXT_KEY => squash_dict_inner_next_key( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - ), - hint_code::SQUASH_DICT => { - squash_dict(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::VM_ENTER_SCOPE => enter_scope(exec_scopes), - hint_code::DICT_UPDATE => { - dict_update(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::DICT_SQUASH_COPY_DICT => { - dict_squash_copy_dict(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::DICT_SQUASH_UPDATE_PTR => { - dict_squash_update_ptr(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::UINT256_ADD => { - uint256_add(vm, &hint_data.ids_data, &hint_data.ap_tracking, false) - } - hint_code::UINT256_ADD_LOW => { - uint256_add(vm, &hint_data.ids_data, &hint_data.ap_tracking, true) - } - hint_code::UINT128_ADD => uint128_add(vm, &hint_data.ids_data, &hint_data.ap_tracking), - hint_code::UINT256_SUB => uint256_sub(vm, &hint_data.ids_data, &hint_data.ap_tracking), - hint_code::SPLIT_64 => split_64(vm, &hint_data.ids_data, &hint_data.ap_tracking), - hint_code::UINT256_SQRT => { - uint256_sqrt(vm, &hint_data.ids_data, &hint_data.ap_tracking, false) - } - hint_code::UINT256_SQRT_FELT => { - uint256_sqrt(vm, &hint_data.ids_data, &hint_data.ap_tracking, true) - } - hint_code::UINT256_SIGNED_NN => { - uint256_signed_nn(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::UINT256_UNSIGNED_DIV_REM => { - uint256_unsigned_div_rem(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::UINT256_EXPANDED_UNSIGNED_DIV_REM => { - uint256_expanded_unsigned_div_rem(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::BIGINT_TO_UINT256 => { - bigint_to_uint256(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants) - } - hint_code::IS_ZERO_PACK_V1 | hint_code::IS_ZERO_PACK_V2 => { - is_zero_pack(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::IS_ZERO_NONDET | hint_code::IS_ZERO_INT => is_zero_nondet(vm, exec_scopes), + squash_dict_inner_used_accesses_assert + } + hint_code::SQUASH_DICT_INNER_NEXT_KEY => squash_dict_inner_next_key, + hint_code::SQUASH_DICT => squash_dict, + hint_code::VM_ENTER_SCOPE => enter_scope, + hint_code::DICT_UPDATE => dict_update, + hint_code::DICT_SQUASH_COPY_DICT => dict_squash_copy_dict, + hint_code::DICT_SQUASH_UPDATE_PTR => dict_squash_update_ptr, + hint_code::UINT256_ADD => uint256_add_wrapper, + hint_code::UINT256_ADD_LOW => uint256_add_low_wrapper, + hint_code::UINT128_ADD => uint128_add, + hint_code::UINT256_SUB => uint256_sub, + hint_code::SPLIT_64 => split_64, + hint_code::UINT256_SQRT => uint256_sqrt_wrapper, + hint_code::UINT256_SQRT_FELT => uint256_sqrt_felt_wrapper, + hint_code::UINT256_SIGNED_NN => uint256_signed_nn, + hint_code::UINT256_UNSIGNED_DIV_REM => uint256_unsigned_div_rem, + hint_code::UINT256_EXPANDED_UNSIGNED_DIV_REM => uint256_expanded_unsigned_div_rem, + hint_code::BIGINT_TO_UINT256 => bigint_to_uint256, + hint_code::IS_ZERO_PACK_V1 | hint_code::IS_ZERO_PACK_V2 => is_zero_pack, + hint_code::IS_ZERO_NONDET | hint_code::IS_ZERO_INT => is_zero_nondet, hint_code::IS_ZERO_PACK_EXTERNAL_SECP_V1 | hint_code::IS_ZERO_PACK_EXTERNAL_SECP_V2 => { - is_zero_pack_external_secp( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - ) - } - hint_code::IS_ZERO_PACK_ED25519 => { - ed25519_is_zero_pack(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) + is_zero_pack_external_secp } - hint_code::IS_ZERO_ASSIGN_SCOPE_VARS => is_zero_assign_scope_variables(exec_scopes), + hint_code::IS_ZERO_PACK_ED25519 => ed25519_is_zero_pack, + hint_code::IS_ZERO_ASSIGN_SCOPE_VARS => is_zero_assign_scope_variables, hint_code::IS_ZERO_ASSIGN_SCOPE_VARS_EXTERNAL_SECP => { - is_zero_assign_scope_variables_external_const(exec_scopes) - } - hint_code::IS_ZERO_ASSIGN_SCOPE_VARS_ED25519 => { - ed25519_is_zero_assign_scope_vars(exec_scopes) - } - hint_code::DIV_MOD_N_PACKED_DIVMOD_V1 => div_mod_n_packed_divmod( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - ), - hint_code::GET_FELT_BIT_LENGTH => { - get_felt_bitlenght(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::BIGINT_PACK_DIV_MOD => bigint_pack_div_mod_hint( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - ), - hint_code::BIGINT_SAFE_DIV => { - bigint_safe_div_hint(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::DIV_MOD_N_PACKED_DIVMOD_EXTERNAL_N => div_mod_n_packed_external_n( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - ), - hint_code::DIV_MOD_N_SAFE_DIV => div_mod_n_safe_div(exec_scopes, "a", "b", 0), - hint_code::DIV_MOD_N_SAFE_DIV_PLUS_ONE => div_mod_n_safe_div(exec_scopes, "a", "b", 1), - hint_code::GET_POINT_FROM_X => get_point_from_x( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - ), - hint_code::EC_NEGATE => ec_negate_import_secp_p( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - ), - hint_code::EC_NEGATE_EMBEDDED_SECP => ec_negate_embedded_secp_p( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - ), - hint_code::EC_DOUBLE_SLOPE_V1 => compute_doubling_slope( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - "point", - &CAIRO_PRIME, - &SECP_P, - &ALPHA, - ), - hint_code::EC_DOUBLE_SLOPE_V2 => compute_doubling_slope( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - "point", - &CAIRO_PRIME, - &SECP_P_V2, - &ALPHA_V2, - ), - hint_code::EC_DOUBLE_SLOPE_V3 => compute_doubling_slope( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - "pt", - &CAIRO_PRIME, - &SECP_P, - &ALPHA, - ), - hint_code::EC_DOUBLE_SLOPE_V4 => compute_doubling_slope( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - "point", - SECP256R1_P.magnitude(), - &SECP256R1_P, - &SECP256R1_ALPHA, - ), - hint_code::EC_DOUBLE_SLOPE_V5 => compute_doubling_slope( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - "point", - &CAIRO_PRIME, - &SECP256R1_P, - &SECP256R1_ALPHA, - ), - hint_code::EC_DOUBLE_SLOPE_EXTERNAL_CONSTS => compute_doubling_slope_external_consts( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - ), - hint_code::COMPUTE_SLOPE_V1 => compute_slope_and_assing_secp_p( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - "point0", - "point1", - &SECP_P, - ), - hint_code::SQUARE_SLOPE_X_MOD_P => { - square_slope_minus_xs(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::COMPUTE_SLOPE_V2 => compute_slope_and_assing_secp_p( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - "point0", - "point1", - &SECP_P_V2, - ), - hint_code::COMPUTE_SLOPE_SECP256R1_V1 => compute_slope( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - "point0", - "point1", - "SECP_P", - ), - hint_code::COMPUTE_SLOPE_SECP256R1_V2 => compute_slope( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - "point0", - "point1", - "SECP256R1_P", - ), - hint_code::IMPORT_SECP256R1_P => import_secp256r1_p(exec_scopes), - hint_code::COMPUTE_SLOPE_WHITELIST => compute_slope_and_assing_secp_p( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - "pt0", - "pt1", - &SECP_P, - ), - hint_code::EC_DOUBLE_ASSIGN_NEW_X_V1 => ec_double_assign_new_x( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - &SECP_P, - "point", - ), - hint_code::EC_DOUBLE_ASSIGN_NEW_X_V2 => ec_double_assign_new_x_v2( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - "point", - ), - hint_code::EC_DOUBLE_ASSIGN_NEW_X_V3 => ec_double_assign_new_x( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - &SECP_P_V2, - "point", - ), - hint_code::EC_DOUBLE_ASSIGN_NEW_X_V4 => ec_double_assign_new_x( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - &SECP_P, - "pt", - ), - hint_code::EC_DOUBLE_ASSIGN_NEW_Y => ec_double_assign_new_y(exec_scopes), - hint_code::KECCAK_WRITE_ARGS => { - keccak_write_args(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::COMPARE_BYTES_IN_WORD_NONDET => compare_bytes_in_word_nondet( - vm, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - ), - hint_code::SHA256_MAIN_CONSTANT_INPUT_LENGTH => sha256_main_constant_input_length( - vm, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - ), - hint_code::SHA256_MAIN_ARBITRARY_INPUT_LENGTH => sha256_main_arbitrary_input_length( - vm, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - ), - hint_code::SHA256_INPUT => { - sha256_input(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::SHA256_FINALIZE => { - sha256_finalize(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::CAIRO_KECCAK_INPUT_IS_FULL_WORD => { - cairo_keccak_is_full_word(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } + is_zero_assign_scope_variables_external_const + } + hint_code::IS_ZERO_ASSIGN_SCOPE_VARS_ED25519 => ed25519_is_zero_assign_scope_vars, + hint_code::DIV_MOD_N_PACKED_DIVMOD_V1 => div_mod_n_packed_divmod, + hint_code::GET_FELT_BIT_LENGTH => get_felt_bitlenght, + hint_code::BIGINT_PACK_DIV_MOD => bigint_pack_div_mod_hint, + hint_code::BIGINT_SAFE_DIV => bigint_safe_div_hint, + hint_code::DIV_MOD_N_PACKED_DIVMOD_EXTERNAL_N => div_mod_n_packed_external_n, + hint_code::DIV_MOD_N_SAFE_DIV => div_mod_n_safe_div_wrapper, + hint_code::DIV_MOD_N_SAFE_DIV_PLUS_ONE => div_mod_n_safe_div_plus_one_wrapper, + hint_code::GET_POINT_FROM_X => get_point_from_x, + hint_code::EC_NEGATE => ec_negate_import_secp_p, + hint_code::EC_NEGATE_EMBEDDED_SECP => ec_negate_embedded_secp_p, + hint_code::EC_DOUBLE_SLOPE_V1 => compute_doubling_slope_v1_wrapper, + hint_code::EC_DOUBLE_SLOPE_V2 => compute_doubling_slope_v2_wrapper, + hint_code::EC_DOUBLE_SLOPE_V3 => compute_doubling_slope_v3_wrapper, + hint_code::EC_DOUBLE_SLOPE_V4 => compute_doubling_slope_v4_wrapper, + hint_code::EC_DOUBLE_SLOPE_V5 => compute_doubling_slope_v5_wrapper, + hint_code::EC_DOUBLE_SLOPE_EXTERNAL_CONSTS => compute_doubling_slope_external_consts, + hint_code::COMPUTE_SLOPE_V1 => compute_slope_and_assing_secp_p_wrapper, + hint_code::SQUARE_SLOPE_X_MOD_P => square_slope_minus_xs, + hint_code::COMPUTE_SLOPE_V2 => compute_slope_and_assing_secp_p_v2_wrapper, + hint_code::COMPUTE_SLOPE_SECP256R1_V1 => compute_slope_v1_wrapper, + hint_code::COMPUTE_SLOPE_SECP256R1_V2 => compute_slope_v2_wrapper, + hint_code::IMPORT_SECP256R1_P => import_secp256r1_p, + hint_code::COMPUTE_SLOPE_WHITELIST => compute_slope_and_assing_secp_p_whitelist_wrapper, + hint_code::EC_DOUBLE_ASSIGN_NEW_X_V1 => ec_double_assign_new_x_v1_wrapper, + hint_code::EC_DOUBLE_ASSIGN_NEW_X_V2 => ec_double_assign_new_x_v2_wrapper, + hint_code::EC_DOUBLE_ASSIGN_NEW_X_V3 => ec_double_assign_new_x_v3_wrapper, + hint_code::EC_DOUBLE_ASSIGN_NEW_X_V4 => ec_double_assign_new_x_v4_wrapper, + hint_code::EC_DOUBLE_ASSIGN_NEW_Y => ec_double_assign_new_y, + hint_code::KECCAK_WRITE_ARGS => keccak_write_args, + hint_code::COMPARE_BYTES_IN_WORD_NONDET => compare_bytes_in_word_nondet, + hint_code::SHA256_MAIN_CONSTANT_INPUT_LENGTH => sha256_main_constant_input_length, + hint_code::SHA256_MAIN_ARBITRARY_INPUT_LENGTH => sha256_main_arbitrary_input_length, + hint_code::SHA256_INPUT => sha256_input, + hint_code::SHA256_FINALIZE => sha256_finalize, + hint_code::CAIRO_KECCAK_INPUT_IS_FULL_WORD => cairo_keccak_is_full_word, hint_code::COMPARE_KECCAK_FULL_RATE_IN_BYTES_NONDET => { - compare_keccak_full_rate_in_bytes_nondet( - vm, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - ) + compare_keccak_full_rate_in_bytes_nondet } hint_code::BLOCK_PERMUTATION | hint_code::BLOCK_PERMUTATION_WHITELIST_V1 => { - block_permutation_v1(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants) - } - hint_code::BLOCK_PERMUTATION_WHITELIST_V2 => { - block_permutation_v2(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants) - } - hint_code::CAIRO_KECCAK_FINALIZE_V1 => { - cairo_keccak_finalize_v1(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants) - } - hint_code::CAIRO_KECCAK_FINALIZE_V2 => { - cairo_keccak_finalize_v2(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants) - } - hint_code::FAST_EC_ADD_ASSIGN_NEW_X => fast_ec_add_assign_new_x( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - &SECP_P, - "point0", - "point1", - ), - hint_code::FAST_EC_ADD_ASSIGN_NEW_X_V2 => fast_ec_add_assign_new_x( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - &SECP_P_V2, - "point0", - "point1", - ), - hint_code::FAST_EC_ADD_ASSIGN_NEW_X_V3 => fast_ec_add_assign_new_x( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - &SECP_P, - "pt0", - "pt1", - ), - hint_code::FAST_EC_ADD_ASSIGN_NEW_Y => fast_ec_add_assign_new_y(exec_scopes), - hint_code::EC_MUL_INNER => { - ec_mul_inner(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::RELOCATE_SEGMENT => { - relocate_segment(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::TEMPORARY_ARRAY => { - temporary_array(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::VERIFY_ECDSA_SIGNATURE => { - verify_ecdsa_signature(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::SPLIT_OUTPUT_0 => { - split_output(vm, &hint_data.ids_data, &hint_data.ap_tracking, 0) - } - hint_code::SPLIT_OUTPUT_1 => { - split_output(vm, &hint_data.ids_data, &hint_data.ap_tracking, 1) - } - hint_code::SPLIT_INPUT_3 => { - split_input(vm, &hint_data.ids_data, &hint_data.ap_tracking, 3, 1) - } - hint_code::SPLIT_INPUT_6 => { - split_input(vm, &hint_data.ids_data, &hint_data.ap_tracking, 6, 2) - } - hint_code::SPLIT_INPUT_9 => { - split_input(vm, &hint_data.ids_data, &hint_data.ap_tracking, 9, 3) - } - hint_code::SPLIT_INPUT_12 => { - split_input(vm, &hint_data.ids_data, &hint_data.ap_tracking, 12, 4) - } - hint_code::SPLIT_INPUT_15 => { - split_input(vm, &hint_data.ids_data, &hint_data.ap_tracking, 15, 5) - } - hint_code::SPLIT_N_BYTES => { - split_n_bytes(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants) - } - hint_code::SPLIT_OUTPUT_MID_LOW_HIGH => { - split_output_mid_low_high(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::NONDET_N_GREATER_THAN_10 => { - n_greater_than_10(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::NONDET_N_GREATER_THAN_2 => { - n_greater_than_2(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::NONDET_ELEMENTS_OVER_TEN => { - elements_over_x(vm, &hint_data.ids_data, &hint_data.ap_tracking, 10) - } - hint_code::NONDET_ELEMENTS_OVER_TWO => { - elements_over_x(vm, &hint_data.ids_data, &hint_data.ap_tracking, 2) - } - hint_code::RANDOM_EC_POINT => { - random_ec_point_hint(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::CHAINED_EC_OP_RANDOM_EC_POINT => { - chained_ec_op_random_ec_point_hint(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::RECOVER_Y => recover_y_hint(vm, &hint_data.ids_data, &hint_data.ap_tracking), - hint_code::PACK_MODN_DIV_MODN => { - pack_modn_div_modn(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::XS_SAFE_DIV => div_mod_n_safe_div(exec_scopes, "x", "s", 0), - hint_code::UINT384_UNSIGNED_DIV_REM => { - uint384_unsigned_div_rem(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::UINT384_SPLIT_128 => { - uint384_split_128(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::ADD_NO_UINT384_CHECK => { - add_no_uint384_check(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants) - } - hint_code::UINT384_SQRT => { - uint384_sqrt(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } + block_permutation_v1 + } + hint_code::BLOCK_PERMUTATION_WHITELIST_V2 => block_permutation_v2, + hint_code::CAIRO_KECCAK_FINALIZE_V1 => cairo_keccak_finalize_v1, + hint_code::CAIRO_KECCAK_FINALIZE_V2 => cairo_keccak_finalize_v2, + hint_code::FAST_EC_ADD_ASSIGN_NEW_X => fast_ec_add_assign_new_x_wrapper, + hint_code::FAST_EC_ADD_ASSIGN_NEW_X_V2 => fast_ec_add_assign_new_x_v2_wrapper, + hint_code::FAST_EC_ADD_ASSIGN_NEW_X_V3 => fast_ec_add_assign_new_x_v3_wrapper, + hint_code::FAST_EC_ADD_ASSIGN_NEW_Y => fast_ec_add_assign_new_y, + hint_code::EC_MUL_INNER => ec_mul_inner, + hint_code::RELOCATE_SEGMENT => relocate_segment, + hint_code::TEMPORARY_ARRAY => temporary_array, + hint_code::VERIFY_ECDSA_SIGNATURE => verify_ecdsa_signature, + hint_code::SPLIT_OUTPUT_0 => split_output_0_wrapper, + hint_code::SPLIT_OUTPUT_1 => split_output_1_wrapper, + hint_code::SPLIT_INPUT_3 => split_input_3_wrapper, + hint_code::SPLIT_INPUT_6 => split_input_6_wrapper, + hint_code::SPLIT_INPUT_9 => split_input_9_wrapper, + hint_code::SPLIT_INPUT_12 => split_input_12_wrapper, + hint_code::SPLIT_INPUT_15 => split_input_15_wrapper, + hint_code::SPLIT_N_BYTES => split_n_bytes, + hint_code::SPLIT_OUTPUT_MID_LOW_HIGH => split_output_mid_low_high, + hint_code::NONDET_N_GREATER_THAN_10 => n_greater_than_10, + hint_code::NONDET_N_GREATER_THAN_2 => n_greater_than_2, + hint_code::NONDET_ELEMENTS_OVER_TEN => elements_over_ten_wrapper, + hint_code::NONDET_ELEMENTS_OVER_TWO => elements_over_two_wrapper, + hint_code::RANDOM_EC_POINT => random_ec_point_hint, + hint_code::CHAINED_EC_OP_RANDOM_EC_POINT => chained_ec_op_random_ec_point_hint, + hint_code::RECOVER_Y => recover_y_hint, + hint_code::PACK_MODN_DIV_MODN => pack_modn_div_modn, + hint_code::XS_SAFE_DIV => div_mod_n_safe_div_xs_wrapper, + hint_code::UINT384_UNSIGNED_DIV_REM => uint384_unsigned_div_rem, + hint_code::UINT384_SPLIT_128 => uint384_split_128, + hint_code::ADD_NO_UINT384_CHECK => add_no_uint384_check, + hint_code::UINT384_SQRT => uint384_sqrt, hint_code::UNSIGNED_DIV_REM_UINT768_BY_UINT384 | hint_code::UNSIGNED_DIV_REM_UINT768_BY_UINT384_STRIPPED => { - unsigned_div_rem_uint768_by_uint384(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::SUB_REDUCED_A_AND_REDUCED_B => { - sub_reduced_a_and_reduced_b(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::UINT384_GET_SQUARE_ROOT => { - u384_get_square_root(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::UINT256_GET_SQUARE_ROOT => { - u256_get_square_root(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::UINT384_SIGNED_NN => { - uint384_signed_nn(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::UINT384_DIV => uint384_div(vm, &hint_data.ids_data, &hint_data.ap_tracking), - hint_code::UINT256_MUL_DIV_MOD => { - uint256_mul_div_mod(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::IMPORT_SECP256R1_ALPHA => import_secp256r1_alpha(exec_scopes), - hint_code::IMPORT_SECP256R1_N => import_secp256r1_n(exec_scopes), - hint_code::UINT512_UNSIGNED_DIV_REM => { - uint512_unsigned_div_rem(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::HI_MAX_BITLEN => { - hi_max_bitlen(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::QUAD_BIT => quad_bit(vm, &hint_data.ids_data, &hint_data.ap_tracking), - hint_code::INV_MOD_P_UINT256 => { - inv_mod_p_uint256(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::INV_MOD_P_UINT512 => { - inv_mod_p_uint512(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::DI_BIT => di_bit(vm, &hint_data.ids_data, &hint_data.ap_tracking), - hint_code::EXAMPLE_BLAKE2S_COMPRESS => { - example_blake2s_compress(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::EC_RECOVER_DIV_MOD_N_PACKED => ec_recover_divmod_n_packed( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - ), - hint_code::EC_RECOVER_SUB_A_B => { - ec_recover_sub_a_b(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::A_B_BITAND_1 => { - a_b_bitand_1(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::ASSERT_LE_FELT_V_0_6 => { - assert_le_felt_v_0_6(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::ASSERT_LE_FELT_V_0_8 => { - assert_le_felt_v_0_8(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::EC_RECOVER_PRODUCT_MOD => { - ec_recover_product_mod(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::EC_RECOVER_PRODUCT_DIV_M => ec_recover_product_div_m(exec_scopes), - hint_code::SPLIT_XX => split_xx(vm, &hint_data.ids_data, &hint_data.ap_tracking), - hint_code::RUN_P_CIRCUIT => { - run_p_mod_circuit(vm, &hint_data.ids_data, &hint_data.ap_tracking) - } + unsigned_div_rem_uint768_by_uint384 + } + hint_code::SUB_REDUCED_A_AND_REDUCED_B => sub_reduced_a_and_reduced_b, + hint_code::UINT384_GET_SQUARE_ROOT => u384_get_square_root, + hint_code::UINT256_GET_SQUARE_ROOT => u256_get_square_root, + hint_code::UINT384_SIGNED_NN => uint384_signed_nn, + hint_code::UINT384_DIV => uint384_div, + hint_code::UINT256_MUL_DIV_MOD => uint256_mul_div_mod, + hint_code::IMPORT_SECP256R1_ALPHA => import_secp256r1_alpha, + hint_code::IMPORT_SECP256R1_N => import_secp256r1_n, + hint_code::UINT512_UNSIGNED_DIV_REM => uint512_unsigned_div_rem, + hint_code::HI_MAX_BITLEN => hi_max_bitlen, + hint_code::QUAD_BIT => quad_bit, + hint_code::INV_MOD_P_UINT256 => inv_mod_p_uint256, + hint_code::INV_MOD_P_UINT512 => inv_mod_p_uint512, + hint_code::DI_BIT => di_bit, + hint_code::EXAMPLE_BLAKE2S_COMPRESS => example_blake2s_compress, + hint_code::EC_RECOVER_DIV_MOD_N_PACKED => ec_recover_divmod_n_packed, + hint_code::EC_RECOVER_SUB_A_B => ec_recover_sub_a_b, + hint_code::A_B_BITAND_1 => a_b_bitand_1, + hint_code::ASSERT_LE_FELT_V_0_6 => assert_le_felt_v_0_6, + hint_code::ASSERT_LE_FELT_V_0_8 => assert_le_felt_v_0_8, + hint_code::EC_RECOVER_PRODUCT_MOD => ec_recover_product_mod, + hint_code::EC_RECOVER_PRODUCT_DIV_M => ec_recover_product_div_m, + hint_code::SPLIT_XX => split_xx, + hint_code::RUN_P_CIRCUIT => run_p_mod_circuit, hint_code::RUN_P_CIRCUIT_WITH_LARGE_BATCH_SIZE => { - run_p_mod_circuit_with_large_batch_size( - vm, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - ) + run_p_mod_circuit_with_large_batch_size } #[cfg(feature = "test_utils")] - hint_code::SKIP_NEXT_INSTRUCTION => skip_next_instruction(vm), + hint_code::SKIP_NEXT_INSTRUCTION => skip_next_instruction, #[cfg(feature = "test_utils")] - hint_code::PRINT_FELT => print_felt(vm, &hint_data.ids_data, &hint_data.ap_tracking), + hint_code::PRINT_FELT => print_felt, #[cfg(feature = "test_utils")] - hint_code::PRINT_ARR => print_array(vm, &hint_data.ids_data, &hint_data.ap_tracking), + hint_code::PRINT_ARR => print_array, #[cfg(feature = "test_utils")] - hint_code::PRINT_DICT => { - print_dict(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) - } - hint_code::EXCESS_BALANCE => excess_balance_hint( - vm, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - exec_scopes, - ), + hint_code::PRINT_DICT => print_dict, + hint_code::EXCESS_BALANCE => excess_balance_hint, #[cfg(feature = "cairo-0-secp-hints")] - cairo0_hints::COMPUTE_Q_MOD_PRIME => cairo0_hints::compute_q_mod_prime( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - ), + cairo0_hints::COMPUTE_Q_MOD_PRIME => cairo0_hints::compute_q_mod_prime, #[cfg(feature = "cairo-0-secp-hints")] - cairo0_hints::COMPUTE_IDS_HIGH_LOW => cairo0_hints::compute_ids_high_low( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - ), + cairo0_hints::COMPUTE_IDS_HIGH_LOW => cairo0_hints::compute_ids_high_low, #[cfg(feature = "cairo-0-secp-hints")] - cairo0_hints::SECP_DOUBLE_ASSIGN_NEW_X => cairo0_hints::secp_double_assign_new_x( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - SECP256R1_P.magnitude(), - ), + cairo0_hints::SECP_DOUBLE_ASSIGN_NEW_X => { + cairo0_hints::secp_double_assign_new_x_wrapper + } #[cfg(feature = "cairo-0-secp-hints")] - cairo0_hints::SECP_DOUBLE_ASSIGN_NEW_X_V2 => cairo0_hints::secp_double_assign_new_x( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - &CAIRO_PRIME, - ), + cairo0_hints::SECP_DOUBLE_ASSIGN_NEW_X_V2 => { + cairo0_hints::secp_double_assign_new_x_v2_wrapper + } #[cfg(feature = "cairo-0-secp-hints")] - cairo0_hints::FAST_SECP_ADD_ASSIGN_NEW_Y => cairo0_hints::fast_secp_add_assign_new_y( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - ), + cairo0_hints::FAST_SECP_ADD_ASSIGN_NEW_Y => cairo0_hints::fast_secp_add_assign_new_y, #[cfg(feature = "cairo-0-secp-hints")] - cairo0_hints::COMPUTE_VALUE_DIV_MOD => cairo0_hints::compute_value_div_mod( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - ), + cairo0_hints::COMPUTE_VALUE_DIV_MOD => cairo0_hints::compute_value_div_mod, #[cfg(feature = "cairo-0-secp-hints")] - cairo0_hints::GENERATE_NIBBLES => cairo0_hints::generate_nibbles( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - ), - + cairo0_hints::GENERATE_NIBBLES => cairo0_hints::generate_nibbles, #[cfg(feature = "cairo-0-secp-hints")] - cairo0_hints::WRITE_NIBBLES_TO_MEM => cairo0_hints::write_nibbles_to_mem( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - ), + cairo0_hints::WRITE_NIBBLES_TO_MEM => cairo0_hints::write_nibbles_to_mem, #[cfg(feature = "cairo-0-secp-hints")] - cairo0_hints::IS_ON_CURVE_2 => cairo0_hints::is_on_curve_2( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - ), + cairo0_hints::IS_ON_CURVE_2 => cairo0_hints::is_on_curve_2, #[cfg(feature = "cairo-0-secp-hints")] - cairo0_hints::SECP_R1_GET_POINT_FROM_X => cairo0_hints::r1_get_point_from_x( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - SECP256R1_P.magnitude(), - ), - + cairo0_hints::SECP_R1_GET_POINT_FROM_X => cairo0_hints::r1_get_point_from_x_wrapper, #[cfg(feature = "cairo-0-secp-hints")] - cairo0_hints::SECP_R1_GET_POINT_FROM_X_V2 => cairo0_hints::r1_get_point_from_x( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - &CAIRO_PRIME, - ), - + cairo0_hints::SECP_R1_GET_POINT_FROM_X_V2 => { + cairo0_hints::r1_get_point_from_x_v2_wrapper + } #[cfg(feature = "cairo-0-secp-hints")] - cairo0_hints::SECP_REDUCE => cairo0_hints::reduce_value( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - ), + cairo0_hints::SECP_REDUCE => cairo0_hints::reduce_value, #[cfg(feature = "cairo-0-secp-hints")] - cairo0_hints::SECP_REDUCE_X => cairo0_hints::reduce_x( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - ), + cairo0_hints::SECP_REDUCE_X => cairo0_hints::reduce_x, #[cfg(feature = "cairo-0-data-availability-hints")] - super::kzg_da::WRITE_DIVMOD_SEGMENT => super::kzg_da::write_div_mod_segment( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, - constants, - ), + super::kzg_da::WRITE_DIVMOD_SEGMENT => super::kzg_da::write_div_mod_segment, #[cfg(feature = "test_utils")] super::simulated_builtins::GET_SIMULATED_BUILTIN_BASE => { - super::simulated_builtins::get_simulated_builtin_base( - vm, - exec_scopes, - &hint_data.ids_data, - &hint_data.ap_tracking, + super::simulated_builtins::get_simulated_builtin_base + } + _code => { + return Ok(any_box!(HintProcessorData { + code: hint_code.to_string(), + ap_tracking: ap_tracking_data.clone(), + ids_data, + accessible_scopes: accessible_scopes.to_vec(), constants, - ) + f: None + })) } + }; - code => Err(HintError::UnknownHint(code.to_string().into_boxed_str())), - } + Ok(any_box!(HintProcessorData { + code: hint_code.to_string(), + ap_tracking: ap_tracking_data.clone(), + ids_data, + accessible_scopes: accessible_scopes.to_vec(), + constants, + f: Some(f) + })) } } diff --git a/vm/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs b/vm/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs index 202cba60a0..1fbf501128 100644 --- a/vm/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs +++ b/vm/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs @@ -1,9 +1,3 @@ -use crate::stdlib::{ - borrow::{Cow, ToOwned}, - boxed::Box, - collections::HashMap, - prelude::*, -}; use crate::{ hint_processor::{ builtin_hint_processor::hint_utils::{ @@ -21,6 +15,15 @@ use crate::{ }, Felt252, }; +use crate::{ + stdlib::{ + borrow::{Cow, ToOwned}, + boxed::Box, + collections::HashMap, + prelude::*, + }, + types::exec_scope::ExecutionScopes, +}; use num_traits::ToPrimitive; // Constants in package "starkware.cairo.common.cairo_keccak.keccak". @@ -47,8 +50,10 @@ Implements hint: */ pub fn keccak_write_args( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let inputs_ptr = get_ptr_from_var_name("inputs", vm, ids_data, ap_tracking)?; @@ -78,6 +83,7 @@ Implements hint: */ pub fn compare_bytes_in_word_nondet( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, constants: &HashMap, @@ -107,6 +113,7 @@ Implements hint: */ pub fn compare_keccak_full_rate_in_bytes_nondet( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, constants: &HashMap, @@ -145,6 +152,7 @@ Implements hints: */ pub(crate) fn block_permutation_v1( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, constants: &HashMap, @@ -189,8 +197,10 @@ Implements hint: */ pub(crate) fn cairo_keccak_is_full_word( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let n_bytes = get_integer_from_var_name("n_bytes", vm, ids_data, ap_tracking)? .to_usize() @@ -212,6 +222,7 @@ Implements hint: */ pub(crate) fn block_permutation_v2( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, constants: &HashMap, @@ -309,6 +320,7 @@ fn cairo_keccak_finalize( */ pub(crate) fn cairo_keccak_finalize_v1( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, constants: &HashMap, @@ -330,6 +342,7 @@ pub(crate) fn cairo_keccak_finalize_v1( */ pub(crate) fn cairo_keccak_finalize_v2( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, constants: &HashMap, diff --git a/vm/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs b/vm/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs index 8e8ae9271b..b941085c90 100644 --- a/vm/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs @@ -1,7 +1,7 @@ use crate::stdlib::{ any::Any, boxed::Box, cell::RefCell, collections::HashMap, prelude::*, rc::Rc, }; - +use crate::Felt252; use crate::{ types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable}, vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, @@ -49,6 +49,9 @@ is not available pub fn dict_new( vm: &mut VirtualMachine, exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { //Get initial dictionary from scope (defined by an earlier hint) let initial_dict = copy_initial_dict(exec_scopes).ok_or(HintError::NoInitialDict)?; @@ -79,6 +82,7 @@ pub fn default_dict_new( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { //Check that ids contains the reference id for each variable used by the hint let default_value = @@ -109,6 +113,7 @@ pub fn dict_read( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let key = get_maybe_relocatable_from_var_name("key", vm, ids_data, ap_tracking)?; let dict_ptr = get_ptr_from_var_name("dict_ptr", vm, ids_data, ap_tracking)?; @@ -131,6 +136,7 @@ pub fn dict_write( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let key = get_maybe_relocatable_from_var_name("key", vm, ids_data, ap_tracking)?; let new_value = get_maybe_relocatable_from_var_name("new_value", vm, ids_data, ap_tracking)?; @@ -170,6 +176,7 @@ pub fn dict_update( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let key = get_maybe_relocatable_from_var_name("key", vm, ids_data, ap_tracking)?; let prev_value = get_maybe_relocatable_from_var_name("prev_value", vm, ids_data, ap_tracking)?; @@ -210,6 +217,7 @@ pub fn dict_squash_copy_dict( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let dict_accesses_end = get_ptr_from_var_name("dict_accesses_end", vm, ids_data, ap_tracking)?; let dict_manager_ref = exec_scopes.get_dict_manager()?; @@ -239,6 +247,7 @@ pub fn dict_squash_update_ptr( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let squashed_dict_start = get_ptr_from_var_name("squashed_dict_start", vm, ids_data, ap_tracking)?; diff --git a/vm/src/hint_processor/builtin_hint_processor/ec_recover.rs b/vm/src/hint_processor/builtin_hint_processor/ec_recover.rs index e92bc29c8b..0e50ebf6fb 100644 --- a/vm/src/hint_processor/builtin_hint_processor/ec_recover.rs +++ b/vm/src/hint_processor/builtin_hint_processor/ec_recover.rs @@ -1,7 +1,6 @@ -use num_integer::Integer; - use super::secp::bigint_utils::BigInt3; use crate::stdlib::{collections::HashMap, prelude::*}; +use crate::Felt252; use crate::{ hint_processor::hint_processor_definition::HintReference, math_utils::div_mod, @@ -10,6 +9,7 @@ use crate::{ vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, }; use num_bigint::BigInt; +use num_integer::Integer; use num_traits::Zero; /* Implements Hint: @@ -28,6 +28,7 @@ pub fn ec_recover_divmod_n_packed( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let n = BigInt3::from_var_name("n", vm, ids_data, ap_tracking)?.pack86(); if n.is_zero() { @@ -61,6 +62,7 @@ pub fn ec_recover_sub_a_b( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let a = BigInt3::from_var_name("a", vm, ids_data, ap_tracking)?.pack86(); let b = BigInt3::from_var_name("b", vm, ids_data, ap_tracking)?.pack86(); @@ -89,6 +91,7 @@ pub fn ec_recover_product_mod( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let a = BigInt3::from_var_name("a", vm, ids_data, ap_tracking)?.pack86(); let b = BigInt3::from_var_name("b", vm, ids_data, ap_tracking)?.pack86(); @@ -111,7 +114,13 @@ pub fn ec_recover_product_mod( value = k = product // m %} */ -pub fn ec_recover_product_div_m(exec_scopes: &mut ExecutionScopes) -> Result<(), HintError> { +pub fn ec_recover_product_div_m( + _vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { let product: &BigInt = exec_scopes.get_ref("product")?; let m: &BigInt = exec_scopes.get_ref("m")?; if m.is_zero() { diff --git a/vm/src/hint_processor/builtin_hint_processor/ec_utils.rs b/vm/src/hint_processor/builtin_hint_processor/ec_utils.rs index 2b8b0e7685..5888ec2f31 100644 --- a/vm/src/hint_processor/builtin_hint_processor/ec_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/ec_utils.rs @@ -1,4 +1,5 @@ use crate::stdlib::{borrow::Cow, boxed::Box, collections::HashMap, prelude::*}; +use crate::types::exec_scope::ExecutionScopes; use crate::utils::CAIRO_PRIME; use crate::Felt252; use crate::{ @@ -57,8 +58,10 @@ impl EcPoint<'_> { pub fn random_ec_point_hint( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let p = EcPoint::from_var_name("p", vm, ids_data, ap_tracking)?; let q = EcPoint::from_var_name("q", vm, ids_data, ap_tracking)?; @@ -104,8 +107,10 @@ pub fn random_ec_point_hint( // ids.s.x, ids.s.y = random_ec_point(FIELD_PRIME, ALPHA, BETA, seed)" pub fn chained_ec_op_random_ec_point_hint( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let n_elms = get_integer_from_var_name("len", vm, ids_data, ap_tracking)?; if n_elms.is_zero() || n_elms.to_usize().is_none() { @@ -138,8 +143,10 @@ pub fn chained_ec_op_random_ec_point_hint( // ids.p.y = recover_y(ids.x, ALPHA, BETA, FIELD_PRIME) pub fn recover_y_hint( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let p_x = get_integer_from_var_name("x", vm, ids_data, ap_tracking)?; let p_addr = get_relocatable_from_var_name("p", vm, ids_data, ap_tracking)?; diff --git a/vm/src/hint_processor/builtin_hint_processor/excess_balance.rs b/vm/src/hint_processor/builtin_hint_processor/excess_balance.rs index 4c676053ce..de92d335f1 100644 --- a/vm/src/hint_processor/builtin_hint_processor/excess_balance.rs +++ b/vm/src/hint_processor/builtin_hint_processor/excess_balance.rs @@ -260,10 +260,10 @@ fn balances_list( pub fn excess_balance_hint( vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, constants: &HashMap, - exec_scopes: &ExecutionScopes, ) -> Result<(), HintError> { // Fetch constants & variables let margin_check_type = @@ -824,10 +824,10 @@ mod tests { // EXECUTION assert!(excess_balance_hint( &mut vm, + &mut exec_scopes, &ids, &ApTracking::default(), &constants, - &exec_scopes ) .is_ok()); @@ -1190,10 +1190,10 @@ mod tests { // EXECUTION assert!(excess_balance_hint( &mut vm, + &mut exec_scopes, &ids, &ApTracking::default(), &constants, - &exec_scopes ) .is_ok()); diff --git a/vm/src/hint_processor/builtin_hint_processor/field_arithmetic.rs b/vm/src/hint_processor/builtin_hint_processor/field_arithmetic.rs index 9c4ce28fe3..b872d2d4ce 100644 --- a/vm/src/hint_processor/builtin_hint_processor/field_arithmetic.rs +++ b/vm/src/hint_processor/builtin_hint_processor/field_arithmetic.rs @@ -1,3 +1,4 @@ +use crate::types::exec_scope::ExecutionScopes; use crate::Felt252; use num_bigint::{BigUint, ToBigInt}; use num_integer::Integer; @@ -64,8 +65,10 @@ use crate::{ */ pub fn u384_get_square_root( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let generator = Uint384::from_var_name("generator", vm, ids_data, ap_tracking)?.pack(); let x = Uint384::from_var_name("x", vm, ids_data, ap_tracking)?.pack(); @@ -156,8 +159,10 @@ pub fn u384_get_square_root( // to merge this with u384_get_square_root pub fn u256_get_square_root( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let generator = Uint256::from_var_name("generator", vm, ids_data, ap_tracking)?.pack(); let x = Uint256::from_var_name("x", vm, ids_data, ap_tracking)?.pack(); @@ -238,8 +243,10 @@ pub fn u256_get_square_root( */ pub fn uint384_div( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { // Note: ids.a is not used here, nor is it used by following hints, so we dont need to extract it. let b = Uint384::from_var_name("b", vm, ids_data, ap_tracking)? diff --git a/vm/src/hint_processor/builtin_hint_processor/find_element_hint.rs b/vm/src/hint_processor/builtin_hint_processor/find_element_hint.rs index 1b1ce66a5c..9cffed8420 100644 --- a/vm/src/hint_processor/builtin_hint_processor/find_element_hint.rs +++ b/vm/src/hint_processor/builtin_hint_processor/find_element_hint.rs @@ -20,6 +20,7 @@ pub fn find_element( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let key = get_integer_from_var_name("key", vm, ids_data, ap_tracking)?; let elm_size_bigint = get_integer_from_var_name("elm_size", vm, ids_data, ap_tracking)?; @@ -87,6 +88,7 @@ pub fn search_sorted_lower( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let find_element_max_size = exec_scopes.get::("find_element_max_size"); let n_elms = get_integer_from_var_name("n_elms", vm, ids_data, ap_tracking)?; diff --git a/vm/src/hint_processor/builtin_hint_processor/garaga.rs b/vm/src/hint_processor/builtin_hint_processor/garaga.rs index 7c1322fff9..c9a8957684 100644 --- a/vm/src/hint_processor/builtin_hint_processor/garaga.rs +++ b/vm/src/hint_processor/builtin_hint_processor/garaga.rs @@ -1,6 +1,7 @@ use crate::stdlib::collections::HashMap; use crate::stdlib::prelude::String; - +use crate::types::exec_scope::ExecutionScopes; +use crate::Felt252; use crate::{ hint_processor::hint_processor_definition::HintReference, serde::deserialize_program::ApTracking, @@ -16,8 +17,10 @@ use super::hint_utils::{get_integer_from_var_name, insert_value_from_var_name}; /// ``` pub fn get_felt_bitlenght( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let x = get_integer_from_var_name("x", vm, ids_data, ap_tracking)?; insert_value_from_var_name("bit_length", x.bits(), vm, ids_data, ap_tracking) diff --git a/vm/src/hint_processor/builtin_hint_processor/keccak_utils.rs b/vm/src/hint_processor/builtin_hint_processor/keccak_utils.rs index b348df8a04..c7d486abae 100644 --- a/vm/src/hint_processor/builtin_hint_processor/keccak_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/keccak_utils.rs @@ -50,6 +50,7 @@ pub fn unsafe_keccak( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let length = get_integer_from_var_name("length", vm, ids_data, ap_tracking)?; @@ -129,8 +130,10 @@ Implements hint: */ pub fn unsafe_keccak_finalize( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { /* ----------------------------- Just for reference (cairo code): @@ -212,6 +215,26 @@ pub fn split_output( ) } +pub fn split_output_0_wrapper( + vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + split_output(vm, ids_data, ap_tracking, 0) +} + +pub fn split_output_1_wrapper( + vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + split_output(vm, ids_data, ap_tracking, 1) +} + // Implements hints of type: ids.high{input_key}, ids.low{input_key} = divmod(memory[ids.inputs + {input_key}], 256 ** {exponent}) pub fn split_input( vm: &mut VirtualMachine, @@ -234,9 +257,60 @@ pub fn split_input( insert_value_from_var_name(&format!("low{}", input_key), low, vm, ids_data, ap_tracking) } +pub fn split_input_3_wrapper( + vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + split_input(vm, ids_data, ap_tracking, 3, 1) +} + +pub fn split_input_6_wrapper( + vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + split_input(vm, ids_data, ap_tracking, 6, 2) +} + +pub fn split_input_9_wrapper( + vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + split_input(vm, ids_data, ap_tracking, 9, 3) +} + +pub fn split_input_12_wrapper( + vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + split_input(vm, ids_data, ap_tracking, 12, 4) +} + +pub fn split_input_15_wrapper( + vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + split_input(vm, ids_data, ap_tracking, 15, 5) +} + // Implements hint: ids.n_words_to_copy, ids.n_bytes_left = divmod(ids.n_bytes, ids.BYTES_IN_WORD) pub fn split_n_bytes( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, constants: &HashMap, @@ -272,8 +346,10 @@ pub fn split_n_bytes( // ids.output1_high, ids.output1_mid = divmod(tmp, 2 ** 128) pub fn split_output_mid_low_high( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let binding = get_integer_from_var_name("output1", vm, ids_data, ap_tracking)?; let output1 = binding.as_ref(); diff --git a/vm/src/hint_processor/builtin_hint_processor/math_utils.rs b/vm/src/hint_processor/builtin_hint_processor/math_utils.rs index eb65aed496..c8de4f2ecd 100644 --- a/vm/src/hint_processor/builtin_hint_processor/math_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/math_utils.rs @@ -41,8 +41,10 @@ const ADDR_BOUND: &str = "starkware.starknet.common.storage.ADDR_BOUND"; //Implements hint: memory[ap] = 0 if 0 <= (ids.a % PRIME) < range_check_builtin.bound else 1 pub fn is_nn( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let a = get_integer_from_var_name("a", vm, ids_data, ap_tracking)?; let range_check_bound = vm.get_range_check_builtin()?.bound(); @@ -53,8 +55,10 @@ pub fn is_nn( //Implements hint: memory[ap] = 0 if 0 <= ((-ids.a - 1) % PRIME) < range_check_builtin.bound else 1 pub fn is_nn_out_of_range( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let a = get_integer_from_var_name("a", vm, ids_data, ap_tracking)?; let a = a.as_ref(); @@ -145,8 +149,10 @@ pub fn assert_le_felt( pub fn assert_le_felt_v_0_6( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let a = &get_integer_from_var_name("a", vm, ids_data, ap_tracking)?; let b = &get_integer_from_var_name("b", vm, ids_data, ap_tracking)?; @@ -159,8 +165,10 @@ pub fn assert_le_felt_v_0_6( pub fn assert_le_felt_v_0_8( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let a = &get_integer_from_var_name("a", vm, ids_data, ap_tracking)?; let b = &get_integer_from_var_name("b", vm, ids_data, ap_tracking)?; @@ -173,7 +181,13 @@ pub fn assert_le_felt_v_0_8( insert_value_from_var_name("small_inputs", small_inputs, vm, ids_data, ap_tracking) } -pub fn assert_le_felt_excluded_2(exec_scopes: &mut ExecutionScopes) -> Result<(), HintError> { +pub fn assert_le_felt_excluded_2( + _vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { let excluded: Felt252 = exec_scopes.get("excluded")?; if excluded != Felt252::from(2_i32) { @@ -186,6 +200,9 @@ pub fn assert_le_felt_excluded_2(exec_scopes: &mut ExecutionScopes) -> Result<() pub fn assert_le_felt_excluded_1( vm: &mut VirtualMachine, exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let excluded: Felt252 = exec_scopes.get("excluded")?; @@ -199,6 +216,9 @@ pub fn assert_le_felt_excluded_1( pub fn assert_le_felt_excluded_0( vm: &mut VirtualMachine, exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let excluded: Felt252 = exec_scopes.get("excluded")?; @@ -213,8 +233,10 @@ pub fn assert_le_felt_excluded_0( // memory[ap] = 0 if (ids.a % PRIME) <= (ids.b % PRIME) else 1 pub fn is_le_felt( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let a_mod = get_integer_from_var_name("a", vm, ids_data, ap_tracking)?; let b_mod = get_integer_from_var_name("b", vm, ids_data, ap_tracking)?; @@ -236,8 +258,10 @@ pub fn is_le_felt( // assert (ids.a - ids.b) % PRIME != 0, f'assert_not_equal failed: {ids.a} = {ids.b}.' pub fn assert_not_equal( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let maybe_rel_a = get_maybe_relocatable_from_var_name("a", vm, ids_data, ap_tracking)?; let maybe_rel_b = get_maybe_relocatable_from_var_name("b", vm, ids_data, ap_tracking)?; @@ -275,8 +299,10 @@ pub fn assert_not_equal( // %} pub fn assert_nn( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let a = get_integer_from_var_name("a", vm, ids_data, ap_tracking)?; let range_check_builtin = vm.get_range_check_builtin()?; @@ -297,8 +323,10 @@ pub fn assert_nn( // %} pub fn assert_not_zero( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let value = get_integer_from_var_name("value", vm, ids_data, ap_tracking)?; if value.is_zero() { @@ -313,8 +341,10 @@ pub fn assert_not_zero( //Implements hint: assert ids.value == 0, 'split_int(): value is out of range.' pub fn split_int_assert_range( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let value = get_integer_from_var_name("value", vm, ids_data, ap_tracking)?; //Main logic (assert value == 0) @@ -328,8 +358,10 @@ pub fn split_int_assert_range( // assert res < ids.bound, f'split_int(): Limb {res} is out of range.' pub fn split_int( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let value = get_integer_from_var_name("value", vm, ids_data, ap_tracking)?; let base = get_integer_from_var_name("base", vm, ids_data, ap_tracking)?; @@ -353,8 +385,10 @@ pub fn split_int( // value=ids.value, prime=PRIME, rc_bound=range_check_builtin.bound) else 0 pub fn is_positive( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let value = get_integer_from_var_name("value", vm, ids_data, ap_tracking)?; let value_as_int = signed_felt(value); @@ -382,6 +416,7 @@ pub fn is_positive( // %} pub fn split_felt( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, constants: &HashMap, @@ -419,8 +454,10 @@ pub fn split_felt( // ids.root = isqrt(value) pub fn sqrt( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let mod_value = get_integer_from_var_name("value", vm, ids_data, ap_tracking)?; //This is equal to mod_value > Felt252::from(2).pow(250) @@ -439,8 +476,10 @@ pub fn sqrt( pub fn signed_div_rem( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let div = get_integer_from_var_name("div", vm, ids_data, ap_tracking)?; let value = get_integer_from_var_name("value", vm, ids_data, ap_tracking)?; @@ -494,8 +533,10 @@ ids.q, ids.r = divmod(ids.value, ids.div) */ pub fn unsigned_div_rem( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let div = get_integer_from_var_name("div", vm, ids_data, ap_tracking)?; let value = get_integer_from_var_name("value", vm, ids_data, ap_tracking)?; @@ -519,6 +560,7 @@ pub fn unsigned_div_rem( // ids.high, ids.low = divmod(ids.value, ids.SHIFT) pub fn assert_250_bit( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, constants: &HashMap, @@ -551,8 +593,10 @@ pub fn assert_250_bit( // %{ ids.is_250 = 1 if ids.addr < 2**250 else 0 %} pub fn is_250_bits( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let addr = get_integer_from_var_name("addr", vm, ids_data, ap_tracking)?; @@ -575,6 +619,7 @@ Implements hint: */ pub fn is_addr_bounded( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, constants: &HashMap, @@ -622,8 +667,10 @@ Implements hint: */ pub fn assert_lt_felt( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let a = get_integer_from_var_name("a", vm, ids_data, ap_tracking)?; let b = get_integer_from_var_name("b", vm, ids_data, ap_tracking)?; @@ -640,8 +687,10 @@ pub fn assert_lt_felt( pub fn is_quad_residue( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let x = get_integer_from_var_name("x", vm, ids_data, ap_tracking)?; @@ -684,8 +733,10 @@ fn prime_div_constant(bound: u32) -> Result { */ pub fn a_b_bitand_1( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let a = get_integer_from_var_name("a", vm, ids_data, ap_tracking)?; let b = get_integer_from_var_name("b", vm, ids_data, ap_tracking)?; @@ -726,8 +777,10 @@ lazy_static! { */ pub fn split_xx( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let xx = Uint256::from_var_name("xx", vm, ids_data, ap_tracking)?; let x_addr = get_relocatable_from_var_name("x", vm, ids_data, ap_tracking)?; diff --git a/vm/src/hint_processor/builtin_hint_processor/memcpy_hint_utils.rs b/vm/src/hint_processor/builtin_hint_processor/memcpy_hint_utils.rs index ea0d6e799c..73ff5345dd 100644 --- a/vm/src/hint_processor/builtin_hint_processor/memcpy_hint_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/memcpy_hint_utils.rs @@ -1,5 +1,5 @@ use crate::stdlib::{any::Any, collections::HashMap, prelude::*}; - +use crate::Felt252; use crate::{ hint_processor::{ builtin_hint_processor::hint_utils::{get_integer_from_var_name, insert_value_into_ap}, @@ -11,20 +11,38 @@ use crate::{ }; //Implements hint: memory[ap] = segments.add() -pub fn add_segment(vm: &mut VirtualMachine) -> Result<(), HintError> { +pub fn add_segment( + vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { let new_segment_base = vm.add_memory_segment(); insert_value_into_ap(vm, new_segment_base) } //Implements hint: vm_enter_scope() -pub fn enter_scope(exec_scopes: &mut ExecutionScopes) -> Result<(), HintError> { +pub fn enter_scope( + _vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { exec_scopes.enter_scope(HashMap::new()); Ok(()) } // Implements hint: // %{ vm_exit_scope() %} -pub fn exit_scope(exec_scopes: &mut ExecutionScopes) -> Result<(), HintError> { +pub fn exit_scope( + _vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { exec_scopes.exit_scope().map_err(HintError::FromScopeError) } @@ -35,6 +53,7 @@ pub fn memcpy_enter_scope( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let len: Box = Box::new(get_integer_from_var_name("len", vm, ids_data, ap_tracking)?); exec_scopes.enter_scope(HashMap::from([(String::from("n"), len)])); diff --git a/vm/src/hint_processor/builtin_hint_processor/memset_utils.rs b/vm/src/hint_processor/builtin_hint_processor/memset_utils.rs index 2c5c7e48ca..da027f7cee 100644 --- a/vm/src/hint_processor/builtin_hint_processor/memset_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/memset_utils.rs @@ -20,6 +20,7 @@ pub fn memset_enter_scope( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let n: Box = Box::new(get_integer_from_var_name("n", vm, ids_data, ap_tracking)?); exec_scopes.enter_scope(HashMap::from([(String::from("n"), n)])); @@ -52,6 +53,26 @@ pub fn memset_step_loop( Ok(()) } +pub fn memset_step_loop_copying_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + memset_step_loop(vm, exec_scopes, ids_data, ap_tracking, "continue_copying") +} + +pub fn memset_step_loop_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + memset_step_loop(vm, exec_scopes, ids_data, ap_tracking, "continue_loop") +} + #[cfg(test)] mod tests { use super::*; diff --git a/vm/src/hint_processor/builtin_hint_processor/mod_circuit.rs b/vm/src/hint_processor/builtin_hint_processor/mod_circuit.rs index 22fbc57d19..218b27294d 100644 --- a/vm/src/hint_processor/builtin_hint_processor/mod_circuit.rs +++ b/vm/src/hint_processor/builtin_hint_processor/mod_circuit.rs @@ -1,4 +1,5 @@ use crate::stdlib::prelude::String; +use crate::types::exec_scope::ExecutionScopes; use crate::{ hint_processor::hint_processor_definition::HintReference, serde::deserialize_program::ApTracking, @@ -26,8 +27,10 @@ use super::hint_utils::{get_integer_from_var_name, get_ptr_from_var_name}; */ pub fn run_p_mod_circuit( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { run_p_mod_circuit_inner(vm, ids_data, ap_tracking, 1) } @@ -48,6 +51,7 @@ pub fn run_p_mod_circuit( #[allow(unused_variables)] pub fn run_p_mod_circuit_with_large_batch_size( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, constants: &HashMap, diff --git a/vm/src/hint_processor/builtin_hint_processor/poseidon_utils.rs b/vm/src/hint_processor/builtin_hint_processor/poseidon_utils.rs index 4a8f58f769..4d785dac17 100644 --- a/vm/src/hint_processor/builtin_hint_processor/poseidon_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/poseidon_utils.rs @@ -1,5 +1,6 @@ use crate::stdlib::{collections::HashMap, string::String}; +use crate::types::exec_scope::ExecutionScopes; use crate::Felt252; use crate::{ @@ -14,8 +15,10 @@ use num_traits::ToPrimitive; // Implements hint: "memory[ap] = to_felt_or_relocatable(ids.n >= 10)" pub fn n_greater_than_10( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let n = get_integer_from_var_name("n", vm, ids_data, ap_tracking)? .to_usize() @@ -27,8 +30,10 @@ pub fn n_greater_than_10( // Implements hint: "memory[ap] = to_felt_or_relocatable(ids.n >= 2)" pub fn n_greater_than_2( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let n = get_integer_from_var_name("n", vm, ids_data, ap_tracking)? .to_usize() @@ -50,6 +55,26 @@ pub fn elements_over_x( insert_value_into_ap(vm, value) } +pub fn elements_over_ten_wrapper( + vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + elements_over_x(vm, ids_data, ap_tracking, 10) +} + +pub fn elements_over_two_wrapper( + vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + elements_over_x(vm, ids_data, ap_tracking, 2) +} + #[cfg(test)] mod tests { use crate::any_box; @@ -57,6 +82,7 @@ mod tests { use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData; use crate::hint_processor::hint_processor_definition::HintProcessorLogic; use crate::hint_processor::hint_processor_definition::HintReference; + use crate::serde::deserialize_program::ApTracking; use crate::{hint_processor::builtin_hint_processor::hint_code, utils::test_utils::*}; use assert_matches::assert_matches; diff --git a/vm/src/hint_processor/builtin_hint_processor/pow_utils.rs b/vm/src/hint_processor/builtin_hint_processor/pow_utils.rs index 0a31277a18..c6353ec932 100644 --- a/vm/src/hint_processor/builtin_hint_processor/pow_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/pow_utils.rs @@ -1,5 +1,6 @@ use crate::stdlib::{boxed::Box, collections::HashMap, prelude::*}; +use crate::types::exec_scope::ExecutionScopes; use crate::Felt252; use crate::{ hint_processor::{ @@ -18,8 +19,10 @@ Implements hint: */ pub fn pow( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let prev_locs_exp = vm .get_integer( diff --git a/vm/src/hint_processor/builtin_hint_processor/print.rs b/vm/src/hint_processor/builtin_hint_processor/print.rs index 12a3e2c1df..869a203e8f 100644 --- a/vm/src/hint_processor/builtin_hint_processor/print.rs +++ b/vm/src/hint_processor/builtin_hint_processor/print.rs @@ -18,9 +18,11 @@ use crate::{ }; pub fn print_felt( - vm: &VirtualMachine, + vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let val = get_integer_from_var_name("x", vm, ids_data, ap_tracking)?; println!("{val}"); @@ -40,9 +42,11 @@ fn print_name( } pub fn print_array( - vm: &VirtualMachine, + vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { print_name(vm, ids_data, ap_tracking)?; @@ -75,10 +79,11 @@ impl Debug for DictValue { } pub fn print_dict( - vm: &VirtualMachine, - exec_scopes: &ExecutionScopes, + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { print_name(vm, ids_data, ap_tracking)?; diff --git a/vm/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs b/vm/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs index d355ac67c0..1c023fd069 100644 --- a/vm/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs @@ -131,6 +131,7 @@ pub fn nondet_bigint3( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let res_reloc = get_relocatable_from_var_name("res", vm, ids_data, ap_tracking)?; let value = exec_scopes @@ -149,6 +150,7 @@ pub fn nondet_bigint3( // %{ ids.low = (ids.x.d0 + ids.x.d1 * ids.BASE) & ((1 << 128) - 1) %} pub fn bigint_to_uint256( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, constants: &HashMap, @@ -170,8 +172,10 @@ pub fn bigint_to_uint256( // %{ ids.len_hi = max(ids.scalar_u.d2.bit_length(), ids.scalar_v.d2.bit_length())-1 %} pub fn hi_max_bitlen( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let scalar_u = BigInt3::from_var_name("scalar_u", vm, ids_data, ap_tracking)?; let scalar_v = BigInt3::from_var_name("scalar_v", vm, ids_data, ap_tracking)?; diff --git a/vm/src/hint_processor/builtin_hint_processor/secp/cairo0_hints.rs b/vm/src/hint_processor/builtin_hint_processor/secp/cairo0_hints.rs index 78ffd3b42c..c2b0745828 100644 --- a/vm/src/hint_processor/builtin_hint_processor/secp/cairo0_hints.rs +++ b/vm/src/hint_processor/builtin_hint_processor/secp/cairo0_hints.rs @@ -1,8 +1,10 @@ -use crate::stdlib::{ - collections::HashMap, - ops::Deref, - ops::{Add, Mul, Rem}, - prelude::*, +use crate::{ + stdlib::{ + collections::HashMap, + ops::{Add, Deref, Mul, Rem}, + prelude::*, + }, + utils::CAIRO_PRIME, }; use crate::define_hint_string_map; @@ -200,7 +202,6 @@ pub fn r1_get_point_from_x( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, - _constants: &HashMap, pack_prime: &BigUint, ) -> Result<(), HintError> { exec_scopes.insert_value::("SECP256R1_P", SECP256R1_P.clone()); @@ -256,6 +257,32 @@ pub fn r1_get_point_from_x( Ok(()) } +pub fn r1_get_point_from_x_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + r1_get_point_from_x( + vm, + exec_scopes, + ids_data, + ap_tracking, + SECP256R1_P.magnitude(), + ) +} + +pub fn r1_get_point_from_x_v2_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + r1_get_point_from_x(vm, exec_scopes, ids_data, ap_tracking, &CAIRO_PRIME) +} + pub fn is_on_curve_2( vm: &mut VirtualMachine, exec_scopes: &mut ExecutionScopes, @@ -283,7 +310,6 @@ pub fn secp_double_assign_new_x( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, - _constants: &HashMap, pack_prime: &BigUint, ) -> Result<(), HintError> { exec_scopes.insert_value::("SECP256R1_P", SECP256R1_P.clone()); @@ -308,6 +334,32 @@ pub fn secp_double_assign_new_x( Ok(()) } +pub fn secp_double_assign_new_x_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + secp_double_assign_new_x( + vm, + exec_scopes, + ids_data, + ap_tracking, + SECP256R1_P.magnitude(), + ) +} + +pub fn secp_double_assign_new_x_v2_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + secp_double_assign_new_x(vm, exec_scopes, ids_data, ap_tracking, &CAIRO_PRIME) +} + pub fn generate_nibbles( vm: &mut VirtualMachine, exec_scopes: &mut ExecutionScopes, @@ -553,14 +605,11 @@ mod tests { let x = BigInt::from(18446744069414584321u128); // Example x value let v = BigInt::from(1); // Example v value (must be 0 or 1 for even/odd check) - let constants = HashMap::new(); - r1_get_point_from_x( &mut vm, &mut exec_scopes, &ids_data, &ap_tracking, - &constants, SECP256R1_P.magnitude(), ) .expect("calculate_value() failed"); diff --git a/vm/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs b/vm/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs index 548d6bd8f3..1837bf26ab 100644 --- a/vm/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs @@ -1,3 +1,5 @@ +use crate::hint_processor::builtin_hint_processor::secp::secp_utils::{ALPHA, ALPHA_V2, SECP_P_V2}; +use crate::utils::CAIRO_PRIME; use crate::Felt252; use crate::{ hint_processor::{ @@ -82,6 +84,7 @@ pub fn ec_negate_import_secp_p( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { ec_negate(vm, exec_scopes, ids_data, ap_tracking, SECP_P.clone()) } @@ -102,6 +105,7 @@ pub fn ec_negate_embedded_secp_p( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let secp_p = (BigInt::one() << 255) - 19; ec_negate(vm, exec_scopes, ids_data, ap_tracking, secp_p) @@ -147,6 +151,100 @@ pub fn compute_doubling_slope( Ok(()) } +pub fn compute_doubling_slope_v1_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + compute_doubling_slope( + vm, + exec_scopes, + ids_data, + ap_tracking, + "point", + &CAIRO_PRIME, + &SECP_P, + &ALPHA, + ) +} + +pub fn compute_doubling_slope_v2_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + compute_doubling_slope( + vm, + exec_scopes, + ids_data, + ap_tracking, + "point", + &CAIRO_PRIME, + &SECP_P_V2, + &ALPHA_V2, + ) +} + +pub fn compute_doubling_slope_v3_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + compute_doubling_slope( + vm, + exec_scopes, + ids_data, + ap_tracking, + "pt", + &CAIRO_PRIME, + &SECP_P, + &ALPHA, + ) +} + +pub fn compute_doubling_slope_v4_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + compute_doubling_slope( + vm, + exec_scopes, + ids_data, + ap_tracking, + "point", + SECP256R1_P.magnitude(), + &SECP256R1_P, + &SECP256R1_ALPHA, + ) +} + +pub fn compute_doubling_slope_v5_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + compute_doubling_slope( + vm, + exec_scopes, + ids_data, + ap_tracking, + "point", + &CAIRO_PRIME, + &SECP256R1_P, + &SECP256R1_ALPHA, + ) +} /* Implements hint: %{ @@ -164,6 +262,7 @@ pub fn compute_doubling_slope_external_consts( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { //ids.point let point = EcPoint::from_var_name("point", vm, ids_data, ap_tracking)?; @@ -211,6 +310,60 @@ pub fn compute_slope_and_assing_secp_p( ) } +pub fn compute_slope_and_assing_secp_p_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + compute_slope_and_assing_secp_p( + vm, + exec_scopes, + ids_data, + ap_tracking, + "point0", + "point1", + &SECP_P, + ) +} + +pub fn compute_slope_and_assing_secp_p_v2_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + compute_slope_and_assing_secp_p( + vm, + exec_scopes, + ids_data, + ap_tracking, + "point0", + "point1", + &SECP_P_V2, + ) +} + +pub fn compute_slope_and_assing_secp_p_whitelist_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + compute_slope_and_assing_secp_p( + vm, + exec_scopes, + ids_data, + ap_tracking, + "pt0", + "pt1", + &SECP_P, + ) +} + pub fn compute_slope( vm: &mut VirtualMachine, exec_scopes: &mut ExecutionScopes, @@ -237,6 +390,42 @@ pub fn compute_slope( Ok(()) } +pub fn compute_slope_v1_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + compute_slope( + vm, + exec_scopes, + ids_data, + ap_tracking, + "point0", + "point1", + "SECP_P", + ) +} + +pub fn compute_slope_v2_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + compute_slope( + vm, + exec_scopes, + ids_data, + ap_tracking, + "point0", + "point1", + "SECP256R1_P", + ) +} + /* Implements hint: %{from starkware.cairo.common.cairo_secp.secp_utils import pack @@ -254,6 +443,7 @@ pub fn square_slope_minus_xs( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let secp_p = exec_scopes.get::("SECP_P")?; let point0 = EcPoint::from_var_name("point0", vm, ids_data, ap_tracking)?; @@ -288,6 +478,16 @@ pub fn ec_double_assign_new_x_v2( ec_double_assign_new_x(vm, exec_scopes, ids_data, ap_tracking, &secp_p, point_alias) } +pub fn ec_double_assign_new_x_v2_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + ec_double_assign_new_x_v2(vm, exec_scopes, ids_data, ap_tracking, "point") +} + /* Implements hint: %{ @@ -329,11 +529,47 @@ pub fn ec_double_assign_new_x( Ok(()) } +pub fn ec_double_assign_new_x_v1_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + ec_double_assign_new_x(vm, exec_scopes, ids_data, ap_tracking, &SECP_P, "point") +} + +pub fn ec_double_assign_new_x_v3_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + ec_double_assign_new_x(vm, exec_scopes, ids_data, ap_tracking, &SECP_P_V2, "point") +} + +pub fn ec_double_assign_new_x_v4_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + ec_double_assign_new_x(vm, exec_scopes, ids_data, ap_tracking, &SECP_P, "pt") +} + /* Implements hint: %{ value = new_y = (slope * (x - new_x) - y) % SECP_P %} */ -pub fn ec_double_assign_new_y(exec_scopes: &mut ExecutionScopes) -> Result<(), HintError> { +pub fn ec_double_assign_new_y( + _vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { //Get variables from vm scope let (slope, x, new_x, y, secp_p) = ( exec_scopes.get::("slope")?, @@ -395,11 +631,71 @@ pub fn fast_ec_add_assign_new_x( Ok(()) } +pub fn fast_ec_add_assign_new_x_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + fast_ec_add_assign_new_x( + vm, + exec_scopes, + ids_data, + ap_tracking, + &SECP_P, + "point0", + "point1", + ) +} + +pub fn fast_ec_add_assign_new_x_v2_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + fast_ec_add_assign_new_x( + vm, + exec_scopes, + ids_data, + ap_tracking, + &SECP_P_V2, + "point0", + "point1", + ) +} + +pub fn fast_ec_add_assign_new_x_v3_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + fast_ec_add_assign_new_x( + vm, + exec_scopes, + ids_data, + ap_tracking, + &SECP_P, + "pt0", + "pt1", + ) +} + /* Implements hint: %{ value = new_y = (slope * (x0 - new_x) - y0) % SECP_P %} */ -pub fn fast_ec_add_assign_new_y(exec_scopes: &mut ExecutionScopes) -> Result<(), HintError> { +pub fn fast_ec_add_assign_new_y( + _vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { //Get variables from vm scope let (slope, x0, new_x, y0, secp_p) = ( exec_scopes.get::("slope")?, @@ -421,8 +717,10 @@ Implements hint: */ pub fn ec_mul_inner( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { //(ids.scalar % PRIME) % 2 let scalar = get_integer_from_var_name("scalar", vm, ids_data, ap_tracking)? @@ -435,7 +733,13 @@ pub fn ec_mul_inner( Implements hint: %{ from starkware.cairo.common.cairo_secp.secp256r1_utils import SECP256R1_ALPHA as ALPHA %} */ -pub fn import_secp256r1_alpha(exec_scopes: &mut ExecutionScopes) -> Result<(), HintError> { +pub fn import_secp256r1_alpha( + _vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { exec_scopes.insert_value("ALPHA", SECP256R1_ALPHA.clone()); Ok(()) } @@ -444,7 +748,13 @@ pub fn import_secp256r1_alpha(exec_scopes: &mut ExecutionScopes) -> Result<(), H Implements hint: %{ from starkware.cairo.common.cairo_secp.secp256r1_utils import SECP256R1_N as N %} */ -pub fn import_secp256r1_n(exec_scopes: &mut ExecutionScopes) -> Result<(), HintError> { +pub fn import_secp256r1_n( + _vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { exec_scopes.insert_value("N", SECP256R1_N.clone()); Ok(()) } @@ -455,7 +765,13 @@ Implements hint: from starkware.cairo.common.cairo_secp.secp256r1_utils import SECP256R1_P as SECP_P %} */ -pub fn import_secp256r1_p(exec_scopes: &mut ExecutionScopes) -> Result<(), HintError> { +pub fn import_secp256r1_p( + _vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { exec_scopes.insert_value("SECP_P", SECP256R1_P.clone()); Ok(()) } @@ -472,8 +788,10 @@ Implements hint: */ pub fn quad_bit( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { n_pair_bits(vm, ids_data, ap_tracking, "quad_bit", 2) } @@ -484,8 +802,10 @@ Implements hint: */ pub fn di_bit( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { n_pair_bits(vm, ids_data, ap_tracking, "dibit", 1) } diff --git a/vm/src/hint_processor/builtin_hint_processor/secp/field_utils.rs b/vm/src/hint_processor/builtin_hint_processor/secp/field_utils.rs index d521a4b867..1a821f77d3 100644 --- a/vm/src/hint_processor/builtin_hint_processor/secp/field_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/secp/field_utils.rs @@ -1,3 +1,4 @@ +use crate::hint_processor::builtin_hint_processor::secp::secp_utils::SECP_P_V2; use crate::Felt252; use crate::{ hint_processor::{ @@ -44,6 +45,26 @@ pub fn verify_zero( insert_value_from_var_name("q", Felt252::from(&q), vm, ids_data, ap_tracking) } +pub fn verify_zero_wrapper( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + verify_zero(vm, exec_scopes, ids_data, ap_tracking, &SECP_P) +} + +pub fn verify_zero_wrapper_v2( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + verify_zero(vm, exec_scopes, ids_data, ap_tracking, &SECP_P_V2) +} + /* Implements hint: %{ @@ -59,6 +80,7 @@ pub fn verify_zero_with_external_const( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let secp_p = exec_scopes.get_ref("SECP_P")?; let val = Uint384::from_var_name("val", vm, ids_data, ap_tracking)?.pack86(); @@ -83,6 +105,7 @@ pub fn reduce_v1( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { exec_scopes.insert_value("SECP_P", SECP_P.clone()); let value = Uint384::from_var_name("x", vm, ids_data, ap_tracking)?.pack86(); @@ -102,6 +125,7 @@ pub fn reduce_v2( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let secp_p = exec_scopes.get_ref("SECP_P")?; let value = Uint384::from_var_name("x", vm, ids_data, ap_tracking)?.pack86(); @@ -122,6 +146,7 @@ pub fn is_zero_pack( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { exec_scopes.insert_value("SECP_P", SECP_P.clone()); let x_packed = Uint384::from_var_name("x", vm, ids_data, ap_tracking)?.pack86(); @@ -135,6 +160,7 @@ pub fn is_zero_pack_external_secp( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let secp_p = exec_scopes.get_ref("SECP_P")?; let x_packed = Uint384::from_var_name("x", vm, ids_data, ap_tracking)?.pack86(); @@ -154,6 +180,9 @@ On .json compiled program pub fn is_zero_nondet( vm: &mut VirtualMachine, exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { //Get `x` variable from vm scope let x = exec_scopes.get::("x")?; @@ -175,7 +204,13 @@ Implements hint: value = x_inv = div_mod(1, x, SECP_P) %} */ -pub fn is_zero_assign_scope_variables(exec_scopes: &mut ExecutionScopes) -> Result<(), HintError> { +pub fn is_zero_assign_scope_variables( + _vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { exec_scopes.insert_value("SECP_P", SECP_P.clone()); //Get `x` variable from vm scope let x = exec_scopes.get::("x")?; @@ -195,7 +230,11 @@ Implements hint: %} */ pub fn is_zero_assign_scope_variables_external_const( + _vm: &mut VirtualMachine, exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { //Get variables from vm scope let secp_p = exec_scopes.get_ref::("SECP_P")?; diff --git a/vm/src/hint_processor/builtin_hint_processor/secp/signature.rs b/vm/src/hint_processor/builtin_hint_processor/secp/signature.rs index b495ab073f..279ea1c9f5 100644 --- a/vm/src/hint_processor/builtin_hint_processor/secp/signature.rs +++ b/vm/src/hint_processor/builtin_hint_processor/secp/signature.rs @@ -51,6 +51,7 @@ pub fn div_mod_n_packed_divmod( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { exec_scopes.assign_or_update_variable("N", any_box!(N.clone())); div_mod_n_packed(vm, exec_scopes, ids_data, ap_tracking, &N) @@ -61,6 +62,7 @@ pub fn div_mod_n_packed_external_n( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let n = exec_scopes.get::("N")?; div_mod_n_packed(vm, exec_scopes, ids_data, ap_tracking, &n) @@ -86,6 +88,36 @@ pub fn div_mod_n_safe_div( Ok(()) } +pub fn div_mod_n_safe_div_wrapper( + _vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + div_mod_n_safe_div(exec_scopes, "a", "b", 0) +} + +pub fn div_mod_n_safe_div_plus_one_wrapper( + _vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + div_mod_n_safe_div(exec_scopes, "a", "b", 1) +} + +pub fn div_mod_n_safe_div_xs_wrapper( + _vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + div_mod_n_safe_div(exec_scopes, "x", "s", 0) +} + /* Implements hint: %{ from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack @@ -144,6 +176,7 @@ pub fn pack_modn_div_modn( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let x = Uint384::from_var_name("x", vm, ids_data, ap_tracking)? .pack86() diff --git a/vm/src/hint_processor/builtin_hint_processor/segments.rs b/vm/src/hint_processor/builtin_hint_processor/segments.rs index 0dad83cf4b..9b9ff770be 100644 --- a/vm/src/hint_processor/builtin_hint_processor/segments.rs +++ b/vm/src/hint_processor/builtin_hint_processor/segments.rs @@ -6,8 +6,10 @@ use crate::hint_processor::{ hint_processor_definition::HintReference, }; use crate::serde::deserialize_program::ApTracking; +use crate::types::exec_scope::ExecutionScopes; use crate::vm::errors::hint_errors::HintError; use crate::vm::vm_core::VirtualMachine; +use crate::Felt252; /* Implements hint: @@ -15,8 +17,10 @@ Implements hint: */ pub fn relocate_segment( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let src_ptr = get_ptr_from_var_name("src_ptr", vm, ids_data, ap_tracking)?; // Bugfix: this is a workaround for an issue in the vm related to the way it computes @@ -62,8 +66,10 @@ Implements hint: */ pub fn temporary_array( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let temp_segment = vm.add_temporary_segment(); insert_value_from_var_name("temporary_array", temp_segment, vm, ids_data, ap_tracking)?; diff --git a/vm/src/hint_processor/builtin_hint_processor/set.rs b/vm/src/hint_processor/builtin_hint_processor/set.rs index 86fc4fdadb..c390b572e3 100644 --- a/vm/src/hint_processor/builtin_hint_processor/set.rs +++ b/vm/src/hint_processor/builtin_hint_processor/set.rs @@ -1,5 +1,6 @@ use crate::stdlib::{boxed::Box, collections::HashMap, prelude::*}; +use crate::types::exec_scope::ExecutionScopes; use crate::Felt252; use crate::{ hint_processor::{ @@ -16,8 +17,10 @@ use num_traits::{ToPrimitive, Zero}; pub fn set_add( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let set_ptr = get_ptr_from_var_name("set_ptr", vm, ids_data, ap_tracking)?; let elm_size = diff --git a/vm/src/hint_processor/builtin_hint_processor/sha256_utils.rs b/vm/src/hint_processor/builtin_hint_processor/sha256_utils.rs index a7e77bd42c..884c6607aa 100644 --- a/vm/src/hint_processor/builtin_hint_processor/sha256_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/sha256_utils.rs @@ -1,5 +1,6 @@ use crate::stdlib::{boxed::Box, collections::HashMap, prelude::*}; +use crate::types::exec_scope::ExecutionScopes; use crate::Felt252; use crate::{ hint_processor::{ @@ -29,8 +30,10 @@ const IV: [u32; SHA256_STATE_SIZE_FELTS] = [ pub fn sha256_input( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let n_bytes = get_integer_from_var_name("n_bytes", vm, ids_data, ap_tracking)?; let n_bytes = n_bytes.as_ref(); @@ -111,6 +114,7 @@ segments.write_arg(ids.output, new_state) */ pub fn sha256_main_constant_input_length( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, constants: &HashMap, @@ -134,6 +138,7 @@ segments.write_arg(ids.output, new_state) */ pub fn sha256_main_arbitrary_input_length( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, constants: &HashMap, @@ -176,8 +181,10 @@ pub fn sha256_main_arbitrary_input_length( pub fn sha256_finalize( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let message: Vec = vec![0; 64]; @@ -241,7 +248,16 @@ mod tests { vm.segments = segments![((1, 1), 7)]; vm.run_context.fp = 2; let ids_data = ids_data!["full_word", "n_bytes"]; - assert_matches!(sha256_input(&mut vm, &ids_data, &ApTracking::new()), Ok(())); + assert_matches!( + sha256_input( + &mut vm, + &mut scope!(), + &ids_data, + &ApTracking::new(), + &HashMap::default() + ), + Ok(()) + ); check_memory![vm.segments.memory, ((1, 0), 1)]; } @@ -253,7 +269,16 @@ mod tests { vm.segments = segments![((1, 1), 3)]; vm.run_context.fp = 2; let ids_data = ids_data!["full_word", "n_bytes"]; - assert_matches!(sha256_input(&mut vm, &ids_data, &ApTracking::new()), Ok(())); + assert_matches!( + sha256_input( + &mut vm, + &mut scope!(), + &ids_data, + &ApTracking::new(), + &HashMap::default() + ), + Ok(()) + ); check_memory![vm.segments.memory, ((1, 0), 0)]; } diff --git a/vm/src/hint_processor/builtin_hint_processor/signature.rs b/vm/src/hint_processor/builtin_hint_processor/signature.rs index 2a77752c7e..b5afeb929a 100644 --- a/vm/src/hint_processor/builtin_hint_processor/signature.rs +++ b/vm/src/hint_processor/builtin_hint_processor/signature.rs @@ -1,5 +1,6 @@ use crate::stdlib::{boxed::Box, collections::HashMap, prelude::*}; - +use crate::types::exec_scope::ExecutionScopes; +use crate::Felt252; use crate::{ hint_processor::{ builtin_hint_processor::hint_utils::{get_integer_from_var_name, get_ptr_from_var_name}, @@ -15,8 +16,10 @@ use crate::{ pub fn verify_ecdsa_signature( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let signature_r = get_integer_from_var_name("signature_r", vm, ids_data, ap_tracking)?; let signature_s = get_integer_from_var_name("signature_s", vm, ids_data, ap_tracking)?; diff --git a/vm/src/hint_processor/builtin_hint_processor/skip_next_instruction.rs b/vm/src/hint_processor/builtin_hint_processor/skip_next_instruction.rs index 634140ef84..472383e1a5 100644 --- a/vm/src/hint_processor/builtin_hint_processor/skip_next_instruction.rs +++ b/vm/src/hint_processor/builtin_hint_processor/skip_next_instruction.rs @@ -1,11 +1,22 @@ +use crate::hint_processor::hint_processor_definition::HintReference; +use crate::serde::deserialize_program::ApTracking; +use crate::types::exec_scope::ExecutionScopes; use crate::vm::errors::hint_errors::HintError; use crate::vm::vm_core::VirtualMachine; +use crate::Felt252; +use std::collections::HashMap; /// Prevent the execution of the next instruction /// /// This hint doesn't belong to the Cairo common library /// It's only added for testing purposes -pub fn skip_next_instruction(vm: &mut VirtualMachine) -> Result<(), HintError> { +pub fn skip_next_instruction( + vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { vm.skip_next_instruction_execution(); Ok(()) } diff --git a/vm/src/hint_processor/builtin_hint_processor/squash_dict_utils.rs b/vm/src/hint_processor/builtin_hint_processor/squash_dict_utils.rs index 28304f0a16..70a7b6b10e 100644 --- a/vm/src/hint_processor/builtin_hint_processor/squash_dict_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/squash_dict_utils.rs @@ -48,6 +48,7 @@ pub fn squash_dict_inner_first_iteration( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { //Check that access_indices and key are in scope let key = exec_scopes.get::("key")?; @@ -78,6 +79,7 @@ pub fn squash_dict_inner_skip_loop( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { //Check that current_access_indices is in scope let current_access_indices = exec_scopes.get_list_ref::("current_access_indices")?; @@ -106,6 +108,7 @@ pub fn squash_dict_inner_check_access_index( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { //Check that current_access_indices and current_access_index are in scope let current_access_index = exec_scopes.get::("current_access_index")?; @@ -130,6 +133,7 @@ pub fn squash_dict_inner_continue_loop( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { //Check that ids contains the reference id for each variable used by the hint //Get addr for ids variables @@ -150,7 +154,13 @@ pub fn squash_dict_inner_continue_loop( } // Implements Hint: assert len(current_access_indices) == 0 -pub fn squash_dict_inner_len_assert(exec_scopes: &mut ExecutionScopes) -> Result<(), HintError> { +pub fn squash_dict_inner_len_assert( + _vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { //Check that current_access_indices is in scope let current_access_indices = exec_scopes.get_list_ref::("current_access_indices")?; if !current_access_indices.is_empty() { @@ -165,6 +175,7 @@ pub fn squash_dict_inner_used_accesses_assert( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let key = exec_scopes.get::("key")?; let n_used_accesses = get_integer_from_var_name("n_used_accesses", vm, ids_data, ap_tracking)?; @@ -186,7 +197,11 @@ pub fn squash_dict_inner_used_accesses_assert( // Implements Hint: assert len(keys) == 0 pub fn squash_dict_inner_assert_len_keys( + _vm: &mut VirtualMachine, exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { //Check that current_access_indices is in scope let keys = exec_scopes.get_list_ref::("keys")?; @@ -204,6 +219,7 @@ pub fn squash_dict_inner_next_key( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { //Check that current_access_indices is in scope let keys = exec_scopes.get_mut_list_ref::("keys")?; @@ -241,6 +257,7 @@ pub fn squash_dict( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { //Get necessary variables addresses from ids let address = get_ptr_from_var_name("dict_accesses", vm, ids_data, ap_tracking)?; diff --git a/vm/src/hint_processor/builtin_hint_processor/uint256_utils.rs b/vm/src/hint_processor/builtin_hint_processor/uint256_utils.rs index 0946dd5ad7..8c3cf5821b 100644 --- a/vm/src/hint_processor/builtin_hint_processor/uint256_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/uint256_utils.rs @@ -1,3 +1,4 @@ +use crate::types::exec_scope::ExecutionScopes; use crate::Felt252; use crate::{ hint_processor::builtin_hint_processor::hint_utils::{ @@ -145,6 +146,26 @@ pub fn uint256_add( insert_value_from_var_name("carry_low", carry_low, vm, ids_data, ap_tracking) } +pub fn uint256_add_wrapper( + vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + uint256_add(vm, ids_data, ap_tracking, false) +} + +pub fn uint256_add_low_wrapper( + vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + uint256_add(vm, ids_data, ap_tracking, true) +} + /* Implements hint: %{ @@ -154,8 +175,10 @@ Implements hint: */ pub fn uint128_add( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let shift = pow2_const(128); let a = get_integer_from_var_name("a", vm, ids_data, ap_tracking)?; @@ -196,8 +219,10 @@ Implements hint: pub fn uint256_sub( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let a = Uint256::from_var_name("a", vm, ids_data, ap_tracking)?.pack(); let b = Uint256::from_var_name("b", vm, ids_data, ap_tracking)?.pack(); @@ -240,8 +265,10 @@ Implements hint: */ pub fn split_64( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let a = get_integer_from_var_name("a", vm, ids_data, ap_tracking)?; let digits = a.to_le_digits(); @@ -302,14 +329,36 @@ pub fn uint256_sqrt( Ok(()) } +pub fn uint256_sqrt_wrapper( + vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + uint256_sqrt(vm, ids_data, ap_tracking, false) +} + +pub fn uint256_sqrt_felt_wrapper( + vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { + uint256_sqrt(vm, ids_data, ap_tracking, true) +} + /* Implements hint: %{ memory[ap] = 1 if 0 <= (ids.a.high % PRIME) < 2 ** 127 else 0 %} */ pub fn uint256_signed_nn( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let a_addr = get_relocatable_from_var_name("a", vm, ids_data, ap_tracking)?; let a_high = vm.get_integer((a_addr + 1_usize)?)?; @@ -339,8 +388,10 @@ Implements hint: */ pub fn uint256_unsigned_div_rem( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { uint256_offseted_unsigned_div_rem(vm, ids_data, ap_tracking, 0, 1) } @@ -360,8 +411,10 @@ Implements hint: */ pub fn uint256_expanded_unsigned_div_rem( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { uint256_offseted_unsigned_div_rem(vm, ids_data, ap_tracking, 1, 3) } @@ -425,8 +478,10 @@ ids.remainder.high = remainder >> 128 */ pub fn uint256_mul_div_mod( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { // Extract variables let a_addr = get_relocatable_from_var_name("a", vm, ids_data, ap_tracking)?; diff --git a/vm/src/hint_processor/builtin_hint_processor/uint384.rs b/vm/src/hint_processor/builtin_hint_processor/uint384.rs index a973fb9d3d..061e157d03 100644 --- a/vm/src/hint_processor/builtin_hint_processor/uint384.rs +++ b/vm/src/hint_processor/builtin_hint_processor/uint384.rs @@ -1,3 +1,4 @@ +use crate::types::exec_scope::ExecutionScopes; use crate::Felt252; use num_bigint::BigUint; use num_integer::Integer; @@ -52,8 +53,10 @@ use super::secp::bigint_utils::Uint384; */ pub fn uint384_unsigned_div_rem( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let a = Uint384::from_var_name("a", vm, ids_data, ap_tracking)?.pack(); let div = Uint384::from_var_name("div", vm, ids_data, ap_tracking)?.pack(); @@ -78,8 +81,10 @@ pub fn uint384_unsigned_div_rem( */ pub fn uint384_split_128( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let bound = pow2_const_nz(128); let a = get_integer_from_var_name("a", vm, ids_data, ap_tracking)?; @@ -100,6 +105,7 @@ pub fn uint384_split_128( */ pub fn add_no_uint384_check( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, constants: &HashMap, @@ -161,8 +167,10 @@ pub fn add_no_uint384_check( */ pub fn uint384_sqrt( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let a = Uint384::from_var_name("a", vm, ids_data, ap_tracking)?.pack(); @@ -182,8 +190,10 @@ pub fn uint384_sqrt( */ pub fn uint384_signed_nn( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let a_addr = get_relocatable_from_var_name("a", vm, ids_data, ap_tracking)?; let a_d2 = vm.get_integer((a_addr + 2)?).map_err(|_| { @@ -222,8 +232,10 @@ pub fn uint384_signed_nn( */ pub fn sub_reduced_a_and_reduced_b( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let a = Uint384::from_var_name("a", vm, ids_data, ap_tracking)?.pack(); let b = Uint384::from_var_name("b", vm, ids_data, ap_tracking)?.pack(); diff --git a/vm/src/hint_processor/builtin_hint_processor/uint384_extension.rs b/vm/src/hint_processor/builtin_hint_processor/uint384_extension.rs index b7ee55bf7d..ef3b56110a 100644 --- a/vm/src/hint_processor/builtin_hint_processor/uint384_extension.rs +++ b/vm/src/hint_processor/builtin_hint_processor/uint384_extension.rs @@ -1,6 +1,8 @@ use super::secp::bigint_utils::{Uint384, Uint768}; use crate::stdlib::{collections::HashMap, prelude::*}; use crate::types::errors::math_errors::MathError; +use crate::types::exec_scope::ExecutionScopes; +use crate::Felt252; use crate::{ hint_processor::hint_processor_definition::HintReference, serde::deserialize_program::ApTracking, @@ -48,8 +50,10 @@ use num_traits::Zero; */ pub fn unsigned_div_rem_uint768_by_uint384( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let a = Uint768::from_var_name("a", vm, ids_data, ap_tracking)?.pack(); let div = Uint384::from_var_name("div", vm, ids_data, ap_tracking)?.pack(); diff --git a/vm/src/hint_processor/builtin_hint_processor/usort.rs b/vm/src/hint_processor/builtin_hint_processor/usort.rs index 7c22f94fc7..34cffe6933 100644 --- a/vm/src/hint_processor/builtin_hint_processor/usort.rs +++ b/vm/src/hint_processor/builtin_hint_processor/usort.rs @@ -15,7 +15,13 @@ use crate::{ use num_traits::ToPrimitive; -pub fn usort_enter_scope(exec_scopes: &mut ExecutionScopes) -> Result<(), HintError> { +pub fn usort_enter_scope( + _vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { if let Ok(usort_max_size) = exec_scopes.get::("usort_max_size") { let boxed_max_size: Box = Box::new(usort_max_size); exec_scopes.enter_scope(HashMap::from([( @@ -33,6 +39,7 @@ pub fn usort_body( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let input_ptr = get_ptr_from_var_name("input", vm, ids_data, ap_tracking)?; let usort_max_size = exec_scopes.get::("usort_max_size"); @@ -97,6 +104,7 @@ pub fn verify_usort( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let value = get_integer_from_var_name("value", vm, ids_data, ap_tracking)?; let mut positions = exec_scopes @@ -109,7 +117,13 @@ pub fn verify_usort( Ok(()) } -pub fn verify_multiplicity_assert(exec_scopes: &mut ExecutionScopes) -> Result<(), HintError> { +pub fn verify_multiplicity_assert( + _vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, +) -> Result<(), HintError> { let positions_len = exec_scopes.get_list_ref::("positions")?.len(); if positions_len == 0 { Ok(()) @@ -123,6 +137,7 @@ pub fn verify_multiplicity_body( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let current_pos = exec_scopes .get_mut_list_ref::("positions")? @@ -159,7 +174,16 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn usort_with_max_size() { let mut exec_scopes = scope![("usort_max_size", 1_u64)]; - assert_matches!(usort_enter_scope(&mut exec_scopes), Ok(())); + assert_matches!( + usort_enter_scope( + &mut vm!(), + &mut exec_scopes, + &HashMap::default(), + &ApTracking::default(), + &HashMap::default() + ), + Ok(()) + ); } #[test] diff --git a/vm/src/hint_processor/builtin_hint_processor/vrf/fq.rs b/vm/src/hint_processor/builtin_hint_processor/vrf/fq.rs index 470a96ac8c..e3354a3190 100644 --- a/vm/src/hint_processor/builtin_hint_processor/vrf/fq.rs +++ b/vm/src/hint_processor/builtin_hint_processor/vrf/fq.rs @@ -1,15 +1,15 @@ //! Fq stands for "a finite field of q elements" +use crate::Felt252; use crate::{ - hint_processor::builtin_hint_processor::uint256_utils::Uint256, hint_processor::{ - builtin_hint_processor::secp::bigint_utils::Uint512, + builtin_hint_processor::{secp::bigint_utils::Uint512, uint256_utils::Uint256}, hint_processor_definition::HintReference, }, math_utils::div_mod, serde::deserialize_program::ApTracking, stdlib::{collections::HashMap, prelude::*}, - types::errors::math_errors::MathError, + types::{errors::math_errors::MathError, exec_scope::ExecutionScopes}, vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, }; use num_bigint::{BigInt, ToBigInt}; @@ -51,8 +51,10 @@ use num_traits::{One, Zero}; /// ``` pub fn uint512_unsigned_div_rem( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let x = Uint512::from_var_name("x", vm, ids_data, ap_tracking)?.pack(); let div = Uint256::from_var_name("div", vm, ids_data, ap_tracking)?.pack(); @@ -94,8 +96,10 @@ pub fn uint512_unsigned_div_rem( /// ``` pub fn inv_mod_p_uint256( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { // 'a' is not used here or in following hints, so we skip it let b = Uint256::from_var_name("b", vm, ids_data, ap_tracking)? diff --git a/vm/src/hint_processor/builtin_hint_processor/vrf/inv_mod_p_uint512.rs b/vm/src/hint_processor/builtin_hint_processor/vrf/inv_mod_p_uint512.rs index ef8748a1bc..437903934d 100644 --- a/vm/src/hint_processor/builtin_hint_processor/vrf/inv_mod_p_uint512.rs +++ b/vm/src/hint_processor/builtin_hint_processor/vrf/inv_mod_p_uint512.rs @@ -1,6 +1,7 @@ use crate::hint_processor::builtin_hint_processor::secp::bigint_utils::Uint512; use crate::hint_processor::builtin_hint_processor::uint256_utils::Uint256; use crate::stdlib::prelude::String; +use crate::types::exec_scope::ExecutionScopes; use crate::Felt252; use crate::{ hint_processor::hint_processor_definition::HintReference, math_utils::div_mod, @@ -31,8 +32,10 @@ Implements hint: */ pub fn inv_mod_p_uint512( vm: &mut VirtualMachine, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let x = Uint512::from_var_name("x", vm, ids_data, ap_tracking)?.pack(); diff --git a/vm/src/hint_processor/builtin_hint_processor/vrf/pack.rs b/vm/src/hint_processor/builtin_hint_processor/vrf/pack.rs index e092fb5c1f..d3a51a281c 100644 --- a/vm/src/hint_processor/builtin_hint_processor/vrf/pack.rs +++ b/vm/src/hint_processor/builtin_hint_processor/vrf/pack.rs @@ -1,7 +1,3 @@ -use num_bigint::BigInt; -use num_integer::Integer; -use num_traits::One; - use crate::hint_processor::builtin_hint_processor::secp::bigint_utils::BigInt3; use crate::hint_processor::builtin_hint_processor::secp::secp_utils::SECP_P_V2; use crate::hint_processor::hint_processor_definition::HintReference; @@ -12,6 +8,10 @@ use crate::stdlib::prelude::String; use crate::types::exec_scope::ExecutionScopes; use crate::vm::errors::hint_errors::HintError; use crate::vm::vm_core::VirtualMachine; +use crate::Felt252; +use num_bigint::BigInt; +use num_integer::Integer; +use num_traits::One; /// Implements hint: /// ```python @@ -25,6 +25,7 @@ pub fn ed25519_is_zero_pack( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let x = BigInt3::from_var_name("x", vm, ids_data, ap_tracking)?.pack86(); exec_scopes.insert_value("x", x.mod_floor(&SECP_P_V2)); @@ -45,6 +46,7 @@ pub fn ed25519_reduce( exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let x = BigInt3::from_var_name("x", vm, ids_data, ap_tracking)?.pack86(); exec_scopes.insert_value("value", x.mod_floor(&SECP_P_V2)); @@ -61,7 +63,11 @@ pub fn ed25519_reduce( /// value = x_inv = div_mod(1, x, SECP_P) /// ``` pub fn ed25519_is_zero_assign_scope_vars( + _vm: &mut VirtualMachine, exec_scopes: &mut ExecutionScopes, + _ids_data: &HashMap, + _ap_tracking: &ApTracking, + _constants: &HashMap, ) -> Result<(), HintError> { let x = exec_scopes.get::("x")?; let x_inv = div_mod(&BigInt::one(), &x, &SECP_P_V2)?; @@ -81,6 +87,7 @@ mod test { use crate::hint_processor::builtin_hint_processor::secp::secp_utils::SECP_P_V2; use crate::hint_processor::hint_processor_definition::HintProcessorLogic; use crate::hint_processor::hint_processor_definition::HintReference; + use crate::serde::deserialize_program::ApTracking; use crate::stdlib::collections::HashMap; use crate::types::exec_scope::ExecutionScopes; use crate::utils::test_utils::*; diff --git a/vm/src/hint_processor/hint_processor_definition.rs b/vm/src/hint_processor/hint_processor_definition.rs index 293642b0b0..7abd47ab29 100644 --- a/vm/src/hint_processor/hint_processor_definition.rs +++ b/vm/src/hint_processor/hint_processor_definition.rs @@ -52,6 +52,7 @@ pub trait HintProcessorLogic { ids_data: get_ids_data(reference_ids, references)?, accessible_scopes: accessible_scopes.to_vec(), constants, + f: None, })) } @@ -79,7 +80,7 @@ pub type HintExtension = HashMap>>; pub trait HintProcessor: HintProcessorLogic + ResourceTracker {} impl HintProcessor for T where T: HintProcessorLogic + ResourceTracker {} -fn get_ids_data( +pub fn get_ids_data( reference_ids: &HashMap, references: &[HintReference], ) -> Result, VirtualMachineError> { diff --git a/vm/src/utils.rs b/vm/src/utils.rs index 6824252fe2..5692db60c0 100644 --- a/vm/src/utils.rs +++ b/vm/src/utils.rs @@ -472,29 +472,78 @@ pub mod test_utils { } pub(crate) use exec_scopes_ref; + macro_rules! compile_hint { + ($hint_code:expr, $constants:expr) => {{ + let constants: &crate::stdlib::collections::HashMap = $constants; + let ap_tracking = ApTracking::default(); + let reference_ids = crate::stdlib::collections::HashMap::new(); + let references = crate::stdlib::vec::Vec::new(); + let accessible_scopes = crate::stdlib::vec::Vec::new(); + let hint_processor = BuiltinHintProcessor::new_empty(); + let hint_data_ref = hint_processor.compile_hint( + $hint_code, + &ap_tracking, + &reference_ids, + &references, + &accessible_scopes, + crate::stdlib::rc::Rc::new(constants.clone()), + ); + (hint_data_ref, hint_processor) + }}; + } + pub(crate) use compile_hint; + macro_rules! run_hint { ($vm:expr, $ids_data:expr, $hint_code:expr, $exec_scopes:expr, $constants:expr) => {{ - let mut hint_data = HintProcessorData::new_default($hint_code.to_string(), $ids_data); - let constants: &HashMap = $constants; - hint_data.constants = crate::stdlib::rc::Rc::new(constants.clone()); - let mut hint_processor = BuiltinHintProcessor::new_empty(); - hint_processor.execute_hint(&mut $vm, $exec_scopes, &any_box!(hint_data)) + let (hint_data_ref, mut hint_processor) = compile_hint!($hint_code, $constants); + let mut hint_data = hint_data_ref + .expect("Failed to compile_hint") + .downcast::() + .unwrap(); + hint_data.ids_data = $ids_data; + hint_processor.execute_hint(&mut $vm, $exec_scopes, &any_box!(*hint_data)) }}; ($vm:expr, $ids_data:expr, $hint_code:expr, $exec_scopes:expr) => {{ - let hint_data = HintProcessorData::new_default( - crate::stdlib::string::ToString::to_string($hint_code), - $ids_data, - ); + let ap_tracking = ApTracking::default(); + let reference_ids = crate::stdlib::collections::HashMap::new(); + let references = crate::stdlib::vec::Vec::new(); + let accessible_scopes = crate::stdlib::vec::Vec::new(); + let constants = crate::stdlib::rc::Rc::new(crate::stdlib::collections::HashMap::new()); let mut hint_processor = BuiltinHintProcessor::new_empty(); - hint_processor.execute_hint(&mut $vm, $exec_scopes, &any_box!(hint_data)) + let hint_data_ref = hint_processor + .compile_hint( + $hint_code, + &ap_tracking, + &reference_ids, + &references, + &accessible_scopes, + constants, + ) + .unwrap(); + let mut hint_data = hint_data_ref.downcast::().unwrap(); + hint_data.ids_data = $ids_data; + hint_processor.execute_hint(&mut $vm, $exec_scopes, &any_box!(*hint_data)) }}; ($vm:expr, $ids_data:expr, $hint_code:expr) => {{ - let hint_data = HintProcessorData::new_default( - crate::stdlib::string::ToString::to_string($hint_code), - $ids_data, - ); + let ap_tracking = ApTracking::default(); + let reference_ids = crate::stdlib::collections::HashMap::new(); + let references = crate::stdlib::vec::Vec::new(); + let accessible_scopes = crate::stdlib::vec::Vec::new(); + let constants = crate::stdlib::rc::Rc::new(crate::stdlib::collections::HashMap::new()); let mut hint_processor = BuiltinHintProcessor::new_empty(); - hint_processor.execute_hint(&mut $vm, exec_scopes_ref!(), &any_box!(hint_data)) + let hint_data_ref = hint_processor + .compile_hint( + $hint_code, + &ap_tracking, + &reference_ids, + &references, + &accessible_scopes, + constants, + ) + .unwrap(); + let mut hint_data = hint_data_ref.downcast::().unwrap(); + hint_data.ids_data = $ids_data; + hint_processor.execute_hint(&mut $vm, exec_scopes_ref!(), &any_box!(*hint_data)) }}; } pub(crate) use run_hint; @@ -639,6 +688,7 @@ pub mod test_utils { #[cfg(test)] mod test { use crate::hint_processor::hint_processor_definition::HintProcessorLogic; + use crate::serde::deserialize_program::ApTracking; use crate::stdlib::{cell::RefCell, collections::HashMap, rc::Rc, string::String, vec::Vec}; use crate::types::builtin_name::BuiltinName; use crate::types::program::HintsCollection; diff --git a/vm/src/vm/vm_core.rs b/vm/src/vm/vm_core.rs index 8afc9628be..225374976a 100644 --- a/vm/src/vm/vm_core.rs +++ b/vm/src/vm/vm_core.rs @@ -1429,6 +1429,7 @@ impl VirtualMachineBuilder { mod tests { use super::*; use crate::felt_hex; + use crate::hint_processor::builtin_hint_processor::memcpy_hint_utils::add_segment; use crate::math_utils::{qm31_coordinates_to_packed_reduced, STWO_PRIME}; use crate::stdlib::collections::HashMap; use crate::types::instruction::OpcodeExtension; @@ -4332,10 +4333,12 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn test_step_for_preset_memory_with_alloc_hint() { let mut vm = vm!(true); - let hint_data = vec![any_box!(HintProcessorData::new_default( + let mut hint_processor_data = HintProcessorData::new_default( "memory[ap] = segments.add()".to_string(), HashMap::new(), - ))]; + ); + hint_processor_data.f = Some(add_segment); + let hint_data = vec![any_box!(hint_processor_data)]; //Initialzie registers run_context!(vm, 3, 2, 2);