forked from lambdaclass/cairo-vm
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hints): add NewHint#23 (lambdaclass#996)
* Add NewHint#23 * Update changelog * Make hint codes public * Get and insert SECP_P from/into exec_scope in hint * Fix test * Change assert_matches for assert This should make the codecov patch check pass. --------- Co-authored-by: Mario Rugiero <mario.rugiero@lambdaclass.com>
- Loading branch information
Showing
6 changed files
with
140 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
%builtins range_check | ||
|
||
from starkware.cairo.common.cairo_secp.bigint import BigInt3, UnreducedBigInt3, nondet_bigint3 | ||
from starkware.cairo.common.cairo_secp.field import unreduced_mul, verify_zero | ||
|
||
// 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{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); | ||
|
||
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(secp256k1_prime); | ||
assert res = 1; | ||
|
||
return (); | ||
} | ||
|
||
func main{range_check_ptr}() -> () { | ||
test_is_zero(); | ||
|
||
return (); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters