Skip to content

Commit

Permalink
Merge branch 'main' into improve_hintcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
TAdev0 committed Jul 13, 2024
2 parents bb52813 + 050d0e0 commit d539770
Show file tree
Hide file tree
Showing 12 changed files with 391 additions and 61 deletions.
34 changes: 32 additions & 2 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (

func main() {
var proofmode bool
var buildMemory bool
var collectTrace bool
var maxsteps uint64
var entrypointOffset uint64
var traceLocation string
Expand Down Expand Up @@ -49,12 +51,24 @@ func main() {
Value: 0,
Destination: &entrypointOffset,
},
&cli.BoolFlag{
Name: "collect_trace",
Usage: "collects the trace and builds the relocated trace after execution",
Required: false,
Destination: &collectTrace,
},
&cli.StringFlag{
Name: "tracefile",
Usage: "location to store the relocated trace",
Required: false,
Destination: &traceLocation,
},
&cli.BoolFlag{
Name: "build_memory",
Usage: "builds the relocated memory after execution",
Required: false,
Destination: &buildMemory,
},
&cli.StringFlag{
Name: "memoryfile",
Usage: "location to store the relocated memory",
Expand Down Expand Up @@ -82,10 +96,12 @@ func main() {
if err != nil {
return fmt.Errorf("cannot load program: %w", err)
}

cairoZeroJson, err := zero.ZeroProgramFromJSON(content)
if err != nil {
return fmt.Errorf("cannot load program: %w", err)
}

program, err := runnerzero.LoadCairoZeroProgram(cairoZeroJson)
if err != nil {
return fmt.Errorf("cannot load program: %w", err)
Expand All @@ -95,6 +111,7 @@ func main() {
if err != nil {
return fmt.Errorf("cannot create hints: %w", err)
}

fmt.Println("Running....")
runner, err := runnerzero.NewRunner(program, hints, proofmode, maxsteps, layoutName)
if err != nil {
Expand All @@ -117,18 +134,31 @@ func main() {

if proofmode {
runner.EndRun()

if err := runner.FinalizeSegments(); err != nil {
return fmt.Errorf("cannot finalize segments: %w", err)
}
trace, memory, err := runner.BuildProof()
}

if proofmode || collectTrace {
trace, err := runner.BuildTrace()
if err != nil {
return fmt.Errorf("cannot build proof: %w", err)
return fmt.Errorf("cannot build trace: %w", err)
}

if traceLocation != "" {
if err := os.WriteFile(traceLocation, trace, 0644); err != nil {
return fmt.Errorf("cannot write relocated trace: %w", err)
}
}
}

if proofmode || buildMemory {
memory, err := runner.BuildMemory()
if err != nil {
return fmt.Errorf("cannot build memory: %w", err)
}

if memoryLocation != "" {
if err := os.WriteFile(memoryLocation, memory, 0644); err != nil {
return fmt.Errorf("cannot write relocated memory: %w", err)
Expand Down
96 changes: 96 additions & 0 deletions integration_tests/BenchMarks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,100 @@
| split_felt.small.cairo | 623 | 100 |
---------------------------------------------------------------------------------------------------------------------------
| unsafe_keccak.small.cairo | 499 | 94 |
=======
| is_quad_residue.small.cairo | 815 | 122 |
---------------------------------------------------------------------------------------------------------------------------
| memset.cairo | 723 | 107 |
---------------------------------------------------------------------------------------------------------------------------
| pow.small.cairo | 810 | 109 |
---------------------------------------------------------------------------------------------------------------------------
| search_sorted_lower.small.cairo | 849 | 107 |
---------------------------------------------------------------------------------------------------------------------------
| bitwise_builtin_test.starknet_with_keccak.cairo| 1312 | 108 |
---------------------------------------------------------------------------------------------------------------------------
| dict.cairo | 840 | 106 |
---------------------------------------------------------------------------------------------------------------------------
| hintrefs.cairo | 1029 | 109 |
---------------------------------------------------------------------------------------------------------------------------
| uint256_sqrt.small.cairo | 929 | 109 |
---------------------------------------------------------------------------------------------------------------------------
| assert_250_bits.small.cairo | 843 | 109 |
---------------------------------------------------------------------------------------------------------------------------
| assert_not_equal.cairo | 923 | 182 |
---------------------------------------------------------------------------------------------------------------------------
| div_mod_n.small.cairo | 889 | 113 |
---------------------------------------------------------------------------------------------------------------------------
| ec.small.cairo | 4754 | 158 |
---------------------------------------------------------------------------------------------------------------------------
| import_secp256R1P.small.cairo | 705 | 103 |
---------------------------------------------------------------------------------------------------------------------------
| usort.small.cairo | 916 | 106 |
---------------------------------------------------------------------------------------------------------------------------
| verify_ecdsa_signature.small.cairo | 747 | 104 |
---------------------------------------------------------------------------------------------------------------------------
| cmp.small.cairo | 913 | 110 |
---------------------------------------------------------------------------------------------------------------------------
| signed_div_rem.small.cairo | 1000 | 115 |
---------------------------------------------------------------------------------------------------------------------------
| poseidon_test.starknet_with_keccak.cairo| 1318 | 108 |
---------------------------------------------------------------------------------------------------------------------------
| assert_not_zero.cairo | 1052 | 104 |
---------------------------------------------------------------------------------------------------------------------------
| blake.starknet_with_keccak.cairo | 47471 | 512 |
---------------------------------------------------------------------------------------------------------------------------
| set_add.small.cairo | 707 | 107 |
---------------------------------------------------------------------------------------------------------------------------
| split64.small.cairo | 804 | 106 |
---------------------------------------------------------------------------------------------------------------------------
| sqrt.small.cairo | 853 | 106 |
---------------------------------------------------------------------------------------------------------------------------
| verify_zero.small.cairo | 738 | 105 |
---------------------------------------------------------------------------------------------------------------------------
| simple.cairo | 600 | 103 |
---------------------------------------------------------------------------------------------------------------------------
| dict_squash.small.cairo | 1005 | 115 |
---------------------------------------------------------------------------------------------------------------------------
| memcpy.cairo | 627 | 103 |
---------------------------------------------------------------------------------------------------------------------------
| split_felt.small.cairo | 1041 | 106 |
---------------------------------------------------------------------------------------------------------------------------
| uint256_add.small.cairo | 722 | 105 |
---------------------------------------------------------------------------------------------------------------------------
| factorial.cairo | 1047 | 106 |
---------------------------------------------------------------------------------------------------------------------------
| pedersen_test.small.cairo | 639 | 103 |
---------------------------------------------------------------------------------------------------------------------------
| is_zero.small.cairo | 872 | 107 |
---------------------------------------------------------------------------------------------------------------------------
| random_ec.cairo | 763 | 108 |
---------------------------------------------------------------------------------------------------------------------------
| split_int.small.cairo | 793 | 106 |
---------------------------------------------------------------------------------------------------------------------------
| uint256_signedNN.small.cairo | 767 | 105 |
---------------------------------------------------------------------------------------------------------------------------
| uint256_unsigned_div_rem.small.cairo | 883 | 108 |
---------------------------------------------------------------------------------------------------------------------------
| unsafe_keccak.small.cairo | 740 | 104 |
---------------------------------------------------------------------------------------------------------------------------
| unsigned_div_rem.small.cairo | 826 | 106 |
---------------------------------------------------------------------------------------------------------------------------
| alloc.cairo | 737 | 620 |
---------------------------------------------------------------------------------------------------------------------------
| find_element.small.cairo | 813 | 105 |
---------------------------------------------------------------------------------------------------------------------------
| get_point_from_x.small.cairo | 1039 | 135 |
---------------------------------------------------------------------------------------------------------------------------
| is_positive.small.cairo | 784 | 105 |
---------------------------------------------------------------------------------------------------------------------------
| reduce_v1.small.cairo | 860 | 108 |
---------------------------------------------------------------------------------------------------------------------------
| uint256_mul_div_mod.small.cairo | 977 | 110 |
---------------------------------------------------------------------------------------------------------------------------
| unsafe_keccak_finalize.small.cairo | 662 | 104 |
---------------------------------------------------------------------------------------------------------------------------
| fib.cairo | 641 | 103 |
---------------------------------------------------------------------------------------------------------------------------
| ecdsa_test.starknet_with_keccak.cairo | 1506 | 108 |
---------------------------------------------------------------------------------------------------------------------------
| keccak_test.starknet_with_keccak.cairo| 1369 | 110 |
===========================================================================================================================
3 changes: 0 additions & 3 deletions integration_tests/cairozero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ func TestCairoFiles(t *testing.T) {
errorExpected := false
if name == "range_check.small.cairo" {
errorExpected = true
} else if name == "ecop.starknet_with_keccak.cairo" {
// temporary, being fixed in another PR soon
continue
}

path := filepath.Join(root, name)
Expand Down
16 changes: 15 additions & 1 deletion pkg/hintrunner/utils/math_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func IsQuadResidue(x *fp.Element) bool {
return x.IsZero() || x.IsOne() || x.Legendre() == 1
}

func YSquaredFromX(x, beta, fieldPrime *big.Int) *big.Int {
func ySquaredFromX(x, beta, fieldPrime *big.Int) *big.Int {
// Computes y^2 using the curve equation:
// y^2 = x^3 + alpha * x + beta (mod field_prime)
// We ignore alpha as it is a constant with a value of 1
Expand All @@ -171,3 +171,17 @@ func Sqrt(x, p *big.Int) *big.Int {

return m
}

func RecoverY(x, beta, fieldPrime *big.Int) (*big.Int, error) {
ySquared := ySquaredFromX(x, beta, fieldPrime)
if IsQuadResidue(new(fp.Element).SetBigInt(ySquared)) {
return Sqrt(ySquared, fieldPrime), nil
}
return nil, fmt.Errorf("%s does not represent the x coordinate of a point on the curve", ySquared.String())
}

func GetCairoPrime() (big.Int, bool) {
// 2**251 + 17 * 2**192 + 1
cairoPrime, ok := new(big.Int).SetString("3618502788666131213697322783095070105623107215331596699973092056135872020481", 10)
return *cairoPrime, ok
}
1 change: 1 addition & 0 deletions pkg/hintrunner/zero/hintcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const (
isZeroPackCode string = "from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack\n\nx = pack(ids.x, PRIME) % SECP_P"
isZeroDivModCode string = "from starkware.cairo.common.cairo_secp.secp_utils import SECP_P\nfrom starkware.python.math_utils import div_mod\n\nvalue = x_inv = div_mod(1, x, SECP_P)"
recoverYCode string = "from starkware.crypto.signature.signature import ALPHA, BETA, FIELD_PRIME\nfrom starkware.python.math_utils import recover_y\nids.p.x = ids.x\n# This raises an exception if `x` is not on the curve.\nids.p.y = recover_y(ids.x, ALPHA, BETA, FIELD_PRIME)"
randomEcPointCode string = "from starkware.crypto.signature.signature import ALPHA, BETA, FIELD_PRIME\nfrom starkware.python.math_utils import random_ec_point\nfrom starkware.python.utils import to_bytes\n\n# Define a seed for random_ec_point that's dependent on all the input, so that:\n# (1) The added point s is deterministic.\n# (2) It's hard to choose inputs for which the builtin will fail.\nseed = b\"\".join(map(to_bytes, [ids.p.x, ids.p.y, ids.m, ids.q.x, ids.q.y]))\nids.s.x, ids.s.y = random_ec_point(FIELD_PRIME, ALPHA, BETA, seed)"

// ------ Signature hints related code ------
verifyECDSASignatureCode string = "ecdsa_builtin.add_signature(ids.ecdsa_ptr.address_, (ids.signature_r, ids.signature_s))"
Expand Down
2 changes: 2 additions & 0 deletions pkg/hintrunner/zero/zerohint.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ func GetHintFromCode(program *zero.ZeroProgram, rawHint zero.Hint, hintPC uint64
return createIsZeroDivModHinter()
case recoverYCode:
return createRecoverYHinter(resolver)
case randomEcPointCode:
return createRandomEcPointHinter(resolver)
// Blake hints
case blake2sAddUint256BigendCode:
return createBlake2sAddUint256Hinter(resolver, true)
Expand Down
Loading

0 comments on commit d539770

Please sign in to comment.