diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e0f90c404..1cbb839310 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,17 @@ #### Upcoming Changes +* Add alternative hint code for nondet_bigint3 hint [#1071](https://github.com/lambdaclass/cairo-rs/pull/1071) + + `BuiltinHintProcessor` now supports the following hint: + + ```python + %{ + from starkware.cairo.common.cairo_secp.secp_utils import split + segments.write_arg(ids.res.address_, split(value)) + %} + ``` + * Add missing hint on vrf.json lib [#1052](https://github.com/lambdaclass/cairo-rs/pull/1052): `BuiltinHintProcessor` now supports the following hint: @@ -19,7 +30,7 @@ value = new_x = (pow(slope, 2, SECP_P) - x0 - x1) % SECP_P ``` -Add missing hint on vrf.json lib [#1053](https://github.com/lambdaclass/cairo-rs/pull/1053): +* Add missing hint on vrf.json lib [#1053](https://github.com/lambdaclass/cairo-rs/pull/1053): `BuiltinHintProcessor` now supports the following hint: diff --git a/cairo_programs/nondet_bigint3_v2.cairo b/cairo_programs/nondet_bigint3_v2.cairo new file mode 100644 index 0000000000..45bbb2a9c0 --- /dev/null +++ b/cairo_programs/nondet_bigint3_v2.cairo @@ -0,0 +1,45 @@ +%builtins range_check + +from starkware.cairo.common.cairo_secp.bigint import BigInt3, BASE + +// Hint arguments: value. +func nondet_bigint3{range_check_ptr}() -> (res: BigInt3) { + // The result should be at the end of the stack after the function returns. + let res: BigInt3 = [cast(ap + 5, BigInt3*)]; + %{ + from starkware.cairo.common.cairo_secp.secp_utils import split + segments.write_arg(ids.res.address_, split(value)) + %} + // The maximal possible sum of the limbs, assuming each of them is in the range [0, BASE). + const MAX_SUM = 3 * (BASE - 1); + assert [range_check_ptr] = MAX_SUM - (res.d0 + res.d1 + res.d2); + + // Prepare the result at the end of the stack. + tempvar range_check_ptr = range_check_ptr + 4; + [range_check_ptr - 3] = res.d0, ap++; + [range_check_ptr - 2] = res.d1, ap++; + [range_check_ptr - 1] = res.d2, ap++; + static_assert &res + BigInt3.SIZE == ap; + return (res=res); +} + +func main{range_check_ptr}() { + alloc_locals; + // Take these hints from div_mod_n just to have a value in scope + local a: BigInt3 = BigInt3(1,2,3); + local b: BigInt3 = BigInt3(4,5,6); + %{ + from starkware.cairo.common.cairo_secp.secp_utils import N, pack + from starkware.python.math_utils import div_mod, safe_div + + a = pack(ids.a, PRIME) + b = pack(ids.b, PRIME) + value = res = div_mod(a, b, N) + %} + let (r) = nondet_bigint3(); + assert r.d0 = 46511138620617205537268188; + assert r.d1 = 26286155657000021849694253; + assert r.d2 = 3102515549921694024741409; + 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 c3c9a2bcea..37b6246415 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 @@ -296,7 +296,7 @@ impl HintProcessor for BuiltinHintProcessor { &hint_data.ids_data, &hint_data.ap_tracking, ), - hint_code::NONDET_BIGINT3 => { + 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 => { diff --git a/src/hint_processor/builtin_hint_processor/hint_code.rs b/src/hint_processor/builtin_hint_processor/hint_code.rs index 612a88b690..3166917f84 100644 --- a/src/hint_processor/builtin_hint_processor/hint_code.rs +++ b/src/hint_processor/builtin_hint_processor/hint_code.rs @@ -471,10 +471,13 @@ new_state = blake2s_compress( segments.write_arg(ids.output, new_state)"#; -pub const NONDET_BIGINT3: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import split +pub const NONDET_BIGINT3_V1: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import split segments.write_arg(ids.res.address_, split(value))"#; +pub const NONDET_BIGINT3_V2: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import split +segments.write_arg(ids.res.address_, split(value))"#; + pub const VERIFY_ZERO_V1: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack q, r = divmod(pack(ids.val, PRIME), SECP_P) diff --git a/src/tests/cairo_run_test.rs b/src/tests/cairo_run_test.rs index 9d8c6d4e41..e7133cecdb 100644 --- a/src/tests/cairo_run_test.rs +++ b/src/tests/cairo_run_test.rs @@ -897,3 +897,10 @@ fn ec_double_assign_new_x_v3() { let program_data = include_bytes!("../../cairo_programs/ec_double_assign_new_x_v3.json"); run_program_simple(program_data.as_slice()); } + +#[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] +fn nondet_bigint3_v2() { + let program_data = include_bytes!("../../cairo_programs/nondet_bigint3_v2.json"); + run_program_simple(program_data.as_slice()); +}