Skip to content

Commit

Permalink
feat(hints): Add alternative string for hint IS_ZERO_PACK (lambdaclas…
Browse files Browse the repository at this point in the history
…s#1081)

* Add alternative hint code IS_ZERO_PACK_V2

* Integrate into existing tests

* Add changelog entry

* Update is_zero.cairo

---------

Co-authored-by: Pedro Fontana <fontana.pedro93@gmail.com>
  • Loading branch information
2 people authored and kariy committed Jun 23, 2023
1 parent f552f7b commit c9006e9
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
10 changes: 10 additions & 0 deletions 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 [#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
%}

* 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:
Expand Down
50 changes: 50 additions & 0 deletions cairo_programs/is_zero.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -101,9 +128,32 @@ 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();
test_is_zero_v2_pack();

return ();
}
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,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),
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 @@ -546,10 +546,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"#;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
Expand Down

0 comments on commit c9006e9

Please sign in to comment.