Skip to content

Commit

Permalink
feat(hints): Add alternative string for hint IS_ZERO_PACK_EXTERNAL_SE…
Browse files Browse the repository at this point in the history
…CP (lambdaclass#1082)

* Add alternative hint code IS_ZERO_PACK_V2

* Integrate into existing tests

* Add changelog entry

* Add alternative string for hint IS_ZERO_PACK_EXTERNAL_SECP

* Add changelog entry + fix unit test
  • Loading branch information
fmoletta authored and kariy committed Jun 23, 2023
1 parent 57c9123 commit 3f9b7c2
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

#### Upcoming Changes

* feat(hints): Add alternative string for hint IS_ZERO_PACK_EXTERNAL_SECP [#1082](https://github.com/lambdaclass/cairo-rs/pull/1082)

`BuiltinHintProcessor` now supports the following hint:

```python
%{
from starkware.cairo.common.cairo_secp.secp_utils import pack
x = pack(ids.x, PRIME) % SECP_P
%}
```
* Add alternative hint code for ec_double hint [#1083](https://github.com/lambdaclass/cairo-rs/pull/1083)

`BuiltinHintProcessor` now supports the following hint:
Expand Down Expand Up @@ -30,7 +40,7 @@

* Implement hint for `starkware.cairo.common.cairo_keccak.keccak._copy_inputs` as described by whitelist `starknet/security/whitelists/cairo_keccak.json` [#1058](https://github.com/lambdaclass/cairo-rs/pull/1058)

`BuiltinHintProcessor` now supports the following hint:
`BuiltinHintProcessor` now supports the following hint:

```python
%{ ids.full_word = int(ids.n_bytes >= 8) %}
Expand Down
39 changes: 39 additions & 0 deletions cairo_programs/is_zero_pack.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ func is_zero_pack{range_check_ptr}(x: BigInt3) -> (res: felt) {
return (res=0);
}

func is_zero_pack_v2{range_check_ptr}(x: BigInt3) -> (res: felt) {
// just to import SECP_P
%{
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
value = pack(ids.x, PRIME) % SECP_P
%}
%{
from starkware.cairo.common.cairo_secp.secp_utils import pack
x = pack(ids.x, PRIME) % SECP_P
%}
if (nondet %{ x == 0 %} != 0) {
return (res=1);
}
return (res=0);
}

func test_is_zero_pack{range_check_ptr}() -> () {
let zero = BigInt3(0, 0, 0);

Expand All @@ -42,8 +59,30 @@ func test_is_zero_pack{range_check_ptr}() -> () {
return ();
}

func test_is_zero_pack_v2{range_check_ptr}() -> () {
let zero = BigInt3(0, 0, 0);

let (res: felt) = is_zero_pack(zero);
assert res = 1;

let one = BigInt3(1, 0, 0);

let (res: felt) = is_zero_pack(one);
assert res = 0;

let secp256k1_prime = BigInt3(
77371252455336262886226991, 77371252455336267181195263, 19342813113834066795298815
);

let (res: felt) = is_zero_pack_v2(secp256k1_prime);
assert res = 1;

return ();
}

func main{range_check_ptr}() -> () {
test_is_zero_pack();
test_is_zero_pack_v2();

return ();
}
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,14 @@ impl HintProcessor for BuiltinHintProcessor {
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),
hint_code::IS_ZERO_PACK_EXTERNAL_SECP => is_zero_pack_external_secp(
vm,
exec_scopes,
&hint_data.ids_data,
&hint_data.ap_tracking,
),
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_ASSIGN_SCOPE_VARS => is_zero_assign_scope_variables(exec_scopes),
hint_code::IS_ZERO_ASSIGN_SCOPE_VARS_EXTERNAL_SECP => {
is_zero_assign_scope_variables_external_const(exec_scopes)
Expand Down
5 changes: 4 additions & 1 deletion src/hint_processor/builtin_hint_processor/hint_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,10 +553,13 @@ 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
pub const IS_ZERO_PACK_EXTERNAL_SECP_V1: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack
x = pack(ids.x, PRIME) % SECP_P"#;

pub const IS_ZERO_PACK_EXTERNAL_SECP_V2: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack
x = pack(ids.x, PRIME) % SECP_P"#;

pub const IS_ZERO_ASSIGN_SCOPE_VARS: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import SECP_P
from starkware.python.math_utils import div_mod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,8 @@ mod tests {
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,
hint_code::IS_ZERO_PACK_EXTERNAL_SECP_V1,
hint_code::IS_ZERO_PACK_EXTERNAL_SECP_V2,
];
for hint_code in hint_codes {
let mut vm = vm_with_range_check!();
Expand Down

0 comments on commit 3f9b7c2

Please sign in to comment.