From f1dbfe55a34862554632e9fd44a356cf523c3e8c Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 27 Apr 2023 10:31:19 -0300 Subject: [PATCH 1/4] Add alternative hint code IS_ZERO_PACK_V2 --- src/hint_processor/builtin_hint_processor/hint_code.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/hint_processor/builtin_hint_processor/hint_code.rs b/src/hint_processor/builtin_hint_processor/hint_code.rs index cdb44bc186..0a2894a815 100644 --- a/src/hint_processor/builtin_hint_processor/hint_code.rs +++ b/src/hint_processor/builtin_hint_processor/hint_code.rs @@ -537,10 +537,13 @@ ids.low = int.from_bytes(hashed[16:32], 'big')"#; pub const IS_ZERO_NONDET: &str = "memory[ap] = to_felt_or_relocatable(x == 0)"; pub const IS_ZERO_INT: &str = "memory[ap] = int(x == 0)"; -pub const IS_ZERO_PACK: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack +pub const IS_ZERO_PACK_V1: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack x = pack(ids.x, PRIME) % SECP_P"#; +pub const IS_ZERO_PACK_V2: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack +x = pack(ids.x, PRIME) % SECP_P"#; + pub const IS_ZERO_PACK_EXTERNAL_SECP: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack x = pack(ids.x, PRIME) % SECP_P"#; From ab5354fc014c5ae98c94cd8425bd8ef65e68f781 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 27 Apr 2023 10:35:57 -0300 Subject: [PATCH 2/4] Integrate into existing tests --- cairo_programs/is_zero.cairo | 49 +++++++++++++++++++ .../builtin_hint_processor_definition.rs | 2 +- .../secp/field_utils.rs | 3 +- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/cairo_programs/is_zero.cairo b/cairo_programs/is_zero.cairo index aeb2607045..81b1fc8991 100644 --- a/cairo_programs/is_zero.cairo +++ b/cairo_programs/is_zero.cairo @@ -59,6 +59,33 @@ func is_zero_alt{range_check_ptr}(x: BigInt3) -> (res: felt) { return (res=0); } +// Returns 1 if x == 0 (mod secp256k1_prime), and 0 otherwise. +// +// Completeness assumption: x's limbs are in the range (-BASE, 2*BASE). +// Soundness assumption: x's limbs are in the range (-2**107.49, 2**107.49). +func is_zero_v2_pack{range_check_ptr}(x: BigInt3) -> (res: felt) { + %{ + from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack + x = pack(ids.x, PRIME) % SECP_P + %} + if (nondet %{ x == 0 %} != 0) { + verify_zero(UnreducedBigInt3(d0=x.d0, d1=x.d1, d2=x.d2)); + return (res=1); + } + + %{ + from starkware.python.math_utils import div_mod + + value = x_inv = div_mod(1, x, SECP_P) + %} + let (x_inv) = nondet_bigint3(); + let (x_x_inv) = unreduced_mul(x, x_inv); + + // Check that x * x_inv = 1 to verify that x != 0. + verify_zero(UnreducedBigInt3(d0=x_x_inv.d0 - 1, d1=x_x_inv.d1, d2=x_x_inv.d2)); + return (res=0); +} + func test_is_zero{range_check_ptr}() -> () { let zero = BigInt3(0, 0, 0); @@ -101,6 +128,28 @@ func test_is_zero_alt{range_check_ptr}() -> () { return (); } +func test_is_zero_v2_pack{range_check_ptr}() -> () { + let zero = BigInt3(0, 0, 0); + + let (res: felt) = is_zero(zero); + assert res = 1; + + let one = BigInt3(1, 0, 0); + + let (res: felt) = is_zero(one); + assert res = 0; + + let secp256k1_prime = BigInt3( + 77371252455336262886226991, 77371252455336267181195263, 19342813113834066795298815 + ); + + let (res: felt) = is_zero_v2_pack(secp256k1_prime); + assert res = 1; + + return (); +} + + func main{range_check_ptr}() -> () { test_is_zero(); test_is_zero_alt(); 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 ba666d33c2..3f8b751ae2 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 @@ -396,7 +396,7 @@ impl HintProcessor for BuiltinHintProcessor { hint_code::BIGINT_TO_UINT256 => { bigint_to_uint256(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants) } - hint_code::IS_ZERO_PACK => { + 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), diff --git a/src/hint_processor/builtin_hint_processor/secp/field_utils.rs b/src/hint_processor/builtin_hint_processor/secp/field_utils.rs index f7cc4488ca..a135900003 100644 --- a/src/hint_processor/builtin_hint_processor/secp/field_utils.rs +++ b/src/hint_processor/builtin_hint_processor/secp/field_utils.rs @@ -408,7 +408,8 @@ mod tests { fn run_is_zero_pack_ok() { let mut exec_scopes = ExecutionScopes::new(); let hint_codes = vec![ - hint_code::IS_ZERO_PACK, + hint_code::IS_ZERO_PACK_V1, + hint_code::IS_ZERO_PACK_V2, // NOTE: this one requires IS_ZERO_ASSIGN_SCOPE_VARS to execute first. hint_code::IS_ZERO_PACK_EXTERNAL_SECP, ]; From 523635aedfd16a56bfbfe1fd85bffb97847fc8ef Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 27 Apr 2023 10:37:49 -0300 Subject: [PATCH 3/4] Add changelog entry --- CHANGELOG.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c605e7234..b1e2790307 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,17 @@ #### Upcoming Changes +* feat(hints): Add alternative string for hint IS_ZERO_PACK [#1081](https://github.com/lambdaclass/cairo-rs/pull/1081) + + `BuiltinHintProcessor` now supports the following hint: + + ```python + %{ + from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack + x = pack(ids.x, PRIME) % SECP_P + %} + ``` + * Add alternative hint code for nondet_bigint3 hint [#1071](https://github.com/lambdaclass/cairo-rs/pull/1071) `BuiltinHintProcessor` now supports the following hint: From a2a957a2b2dee7f4d82dc6915dc1b4ce95c52a7b Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Thu, 27 Apr 2023 10:47:41 -0300 Subject: [PATCH 4/4] Update is_zero.cairo --- cairo_programs/is_zero.cairo | 1 + 1 file changed, 1 insertion(+) diff --git a/cairo_programs/is_zero.cairo b/cairo_programs/is_zero.cairo index 81b1fc8991..88d3551d13 100644 --- a/cairo_programs/is_zero.cairo +++ b/cairo_programs/is_zero.cairo @@ -153,6 +153,7 @@ func test_is_zero_v2_pack{range_check_ptr}() -> () { func main{range_check_ptr}() -> () { test_is_zero(); test_is_zero_alt(); + test_is_zero_v2_pack(); return (); }