diff --git a/CHANGELOG.md b/CHANGELOG.md index aad336f782..d842c2f619 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,23 @@ #### Upcoming Changes +* Implement hint on ec_recover.json whitelist [#1036](https://github.com/lambdaclass/cairo-rs/pull/1036): + + `BuiltinHintProcessor` now supports the following hint: + + ```python + + %{ + from starkware.cairo.common.cairo_secp.secp_utils import pack + from starkware.python.math_utils import div_mod, safe_div + + a = pack(ids.a, PRIME) + b = pack(ids.b, PRIME) + value = res = a - b + %} + + ``` + * Implement hint on ec_recover.json whitelist [#1032](https://github.com/lambdaclass/cairo-rs/pull/1032): `BuiltinHintProcessor` now supports the following hint: diff --git a/cairo_programs/ec_recover.cairo b/cairo_programs/ec_recover.cairo index 83606c8992..b066663ba2 100644 --- a/cairo_programs/ec_recover.cairo +++ b/cairo_programs/ec_recover.cairo @@ -6,6 +6,7 @@ func test_div_mod_n_packed_hint{range_check_ptr: felt}() { tempvar n = BigInt3(177, 0, 0); tempvar x = BigInt3(25, 0, 0); tempvar s = BigInt3(5, 0, 0); + %{ from starkware.cairo.common.cairo_secp.secp_utils import pack from starkware.python.math_utils import div_mod, safe_div @@ -18,10 +19,32 @@ func test_div_mod_n_packed_hint{range_check_ptr: felt}() { let (res) = nondet_bigint3(); assert res = BigInt3(5,0,0); + + return(); +} + +func test_sub_a_b_hint{range_check_ptr: felt}() { + + tempvar a = BigInt3(100, 0, 0); + tempvar b = BigInt3(25, 0, 0); + + %{ + from starkware.cairo.common.cairo_secp.secp_utils import pack + from starkware.python.math_utils import div_mod, safe_div + + a = pack(ids.a, PRIME) + b = pack(ids.b, PRIME) + value = res = a - b + %} + + let (res) = nondet_bigint3(); + assert res = BigInt3(75,0,0); + return(); } func main{range_check_ptr: felt}() { test_div_mod_n_packed_hint(); + test_sub_a_b_hint(); return(); } diff --git a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs index b261947546..b4a0d811d5 100644 --- a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs +++ b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs @@ -85,7 +85,7 @@ use felt::Felt252; #[cfg(feature = "skip_next_instruction_hint")] use crate::hint_processor::builtin_hint_processor::skip_next_instruction::skip_next_instruction; -use super::ec_recover::ec_recover_divmod_n_packed; +use super::ec_recover::{ec_recover_divmod_n_packed, ec_recover_sub_a_b}; use super::field_arithmetic::uint384_div; use super::vrf::inv_mod_p_uint512::inv_mod_p_uint512; @@ -592,6 +592,9 @@ impl HintProcessor for BuiltinHintProcessor { &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) + } #[cfg(feature = "skip_next_instruction_hint")] hint_code::SKIP_NEXT_INSTRUCTION => skip_next_instruction(vm), code => Err(HintError::UnknownHint(code.to_string())), diff --git a/src/hint_processor/builtin_hint_processor/ec_recover.rs b/src/hint_processor/builtin_hint_processor/ec_recover.rs index 927b0a9f0f..603f148d0d 100644 --- a/src/hint_processor/builtin_hint_processor/ec_recover.rs +++ b/src/hint_processor/builtin_hint_processor/ec_recover.rs @@ -36,6 +36,31 @@ pub fn ec_recover_divmod_n_packed( Ok(()) } +/* Implements Hint: +%{ + from starkware.cairo.common.cairo_secp.secp_utils import pack + from starkware.python.math_utils import div_mod, safe_div + + a = pack(ids.x, PRIME) + b = pack(ids.s, PRIME) + value = res = a - b +%} + */ +pub fn ec_recover_sub_a_b( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, +) -> Result<(), HintError> { + let a = pack(BigInt3::from_var_name("a", vm, ids_data, ap_tracking)?); + let b = pack(BigInt3::from_var_name("b", vm, ids_data, ap_tracking)?); + + let value = a - b; + exec_scopes.insert_value("value", value.clone()); + exec_scopes.insert_value("res", value); + Ok(()) +} + #[cfg(test)] mod tests { use num_bigint::BigInt; @@ -99,4 +124,38 @@ mod tests { [("value", BigInt::from(5)), ("res", BigInt::from(5))] ); } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn run_ec_recover_sub_a_b_ok() { + let mut vm = vm!(); + let mut exec_scopes = ExecutionScopes::new(); + + vm.run_context.fp = 8; + let ids_data = non_continuous_ids_data![("a", -8), ("b", -5)]; + + vm.segments = segments![ + //a + ((1, 0), 100), + ((1, 1), 0), + ((1, 2), 0), + //b + ((1, 3), 25), + ((1, 4), 0), + ((1, 5), 0), + ]; + + assert!(run_hint!( + vm, + ids_data, + hint_code::EC_RECOVER_SUB_A_B, + &mut exec_scopes + ) + .is_ok()); + + check_scope!( + &exec_scopes, + [("value", BigInt::from(75)), ("res", BigInt::from(75))] + ); + } } diff --git a/src/hint_processor/builtin_hint_processor/hint_code.rs b/src/hint_processor/builtin_hint_processor/hint_code.rs index 115a485bdd..2df3602b8c 100644 --- a/src/hint_processor/builtin_hint_processor/hint_code.rs +++ b/src/hint_processor/builtin_hint_processor/hint_code.rs @@ -987,5 +987,13 @@ N = pack(ids.n, PRIME) x = pack(ids.x, PRIME) % N s = pack(ids.s, PRIME) % N value = res = div_mod(x, s, N)"#; + +pub const EC_RECOVER_SUB_A_B: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack +from starkware.python.math_utils import div_mod, safe_div + +a = pack(ids.a, PRIME) +b = pack(ids.b, PRIME) +value = res = a - b"#; + #[cfg(feature = "skip_next_instruction_hint")] pub const SKIP_NEXT_INSTRUCTION: &str = "skip_next_instruction()";