Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hints): Add alternative string for hint IS_ZERO_PACK_EXTERNAL_SECP #1082

Merged
merged 6 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

#### 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
%}
```

* 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:
Expand Down
49 changes: 49 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,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();
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 @@ -396,16 +396,18 @@ 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),
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
10 changes: 8 additions & 2 deletions src/hint_processor/builtin_hint_processor/hint_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,12 +537,18 @@ 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_EXTERNAL_SECP: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack
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_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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,11 @@ 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,
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