Skip to content

Commit

Permalink
Merge branch 'main' into newhint17-newhint18-imports
Browse files Browse the repository at this point in the history
  • Loading branch information
mfachal committed Apr 26, 2023
2 parents 88992db + 327ee7d commit 736db30
Show file tree
Hide file tree
Showing 11 changed files with 343 additions and 36 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/canary.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: QA Canary Workflow

on:
workflow_run:
workflows: [QA]
types:
- completed

jobs:
on-qa-success:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- run: exit 0
on-qa-failure:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
steps:
- run: exit 1
30 changes: 19 additions & 11 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Fetch from cache
uses: actions/cache@v3
Expand All @@ -30,6 +32,11 @@ jobs:
key: ${{ matrix.program-target }}-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }}
restore-keys: ${{ matrix.program-target }}-cache-

# This is not pretty, but we need `make` to see the compiled programs are
# actually newer than the sources, otherwise it will try to rebuild them
- name: Restore timestamps
uses: chetan/git-restore-mtime-action@v1

- name: Python3 Build
if: ${{ steps.cache-programs.outputs.cache-hit != 'true' }}
uses: actions/setup-python@v4
Expand Down Expand Up @@ -138,7 +145,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
with:
depth: 0
fetch-depth: 0
# This is not pretty, but we need `make` to see the compiled programs are
# actually newer than the sources, otherwise it will try to rebuild them
- name: Restore timestamps
Expand Down Expand Up @@ -207,19 +214,19 @@ jobs:
matrix:
include:
- program-target: cairo_proof_programs
programs-dir: cairo_programs/proof_programs
trace-target: cairo_proof_trace
nprocs: 1
extra-args: '--proof_mode'
- program-target: cairo_test_programs
programs-dir: cairo_programs
trace-target: cairo_trace
nprocs: 2
extra-args: ''
name: Compute memory and execution traces with cairo-lang
needs: build-programs
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Check cache
uses: actions/cache@v3
Expand All @@ -229,6 +236,7 @@ jobs:
cairo_programs/**/*.memory
cairo_programs/**/*.trace
key: ${{ matrix.program-target }}-reference-trace-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }}
restore-keys: ${{ matrix.program-target }}-reference-trace-cache-

- name: Python3 Build
if: steps.trace-cache.outputs.cache-hit != 'true'
Expand All @@ -248,14 +256,14 @@ jobs:
key: ${{ matrix.program-target }}-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }}
fail-on-cache-miss: true

# This is not pretty, but we need `make` to see the compiled programs are
# actually newer than the sources, otherwise it will try to rebuild them
- name: Restore timestamps
uses: chetan/git-restore-mtime-action@v1

- name: Generate traces
if: ${{ steps.trace-cache.outputs.cache-hit != 'true' }}
run: |
ls ${{ matrix.programs-dir }}/*.json | cut -f1 -d'.' | \
xargs -P ${{ matrix.nprocs }} -I '{program}' \
cairo-run --program '{program}'.json --layout starknet_with_keccak \
--memory_file '{program}'.memory --trace_file '{program}'.trace \
${{ matrix.extra-args }}
run: make -j ${{ matrix.nprocs }} ${{ matrix.trace-target }}


run-cairo-release:
Expand Down
36 changes: 36 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,50 @@

#### Upcoming Changes

* Add missing hint on vrf.json lib [#1052](https://github.com/lambdaclass/cairo-rs/pull/1052):

`BuiltinHintProcessor` now supports the following hint:

```python
%{
from starkware.cairo.common.cairo_secp.secp_utils import pack
SECP_P = 2**255-19

slope = pack(ids.slope, PRIME)
x0 = pack(ids.point0.x, PRIME)
x1 = pack(ids.point1.x, PRIME)
y0 = pack(ids.point0.y, PRIME)

value = new_x = (pow(slope, 2, SECP_P) - x0 - x1) % SECP_P
```

Add missing hint on vrf.json lib [#1053](https://github.com/lambdaclass/cairo-rs/pull/1053):

`BuiltinHintProcessor` now supports the following hint:

```python
%{
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
SECP_P = 2**255-19

slope = pack(ids.slope, PRIME)
x = pack(ids.point.x, PRIME)
y = pack(ids.point.y, PRIME)

value = new_x = (pow(slope, 2, SECP_P) - 2 * x) % SECP_P
%}
```

* Implement hint on 0.6.0.json whitelist [#1044](https://github.com/lambdaclass/cairo-rs/pull/1044):

`BuiltinHintProcessor` now supports the following hints:

```
%{
ids.a_lsb = ids.a & 1
ids.b_lsb = ids.b & 1
%}
```

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

Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ DBGBIN:=target/debug/cairo-rs-run
compare_vm_output compare_trace_memory compare_trace compare_memory \
compare_trace_memory_proof compare_trace_proof compare_memory_proof \
cairo_bench_programs cairo_proof_programs cairo_test_programs \
cairo_trace cairo-rs_trace $(RELBIN) $(DBGBIN)
cairo_trace cairo-rs_trace cairo_proof_trace cairo-rs_proof_trace \
$(RELBIN) $(DBGBIN)

# Proof mode consumes too much memory with cairo-lang to execute
# two instances at the same time in the CI without getting killed
Expand Down Expand Up @@ -134,6 +135,9 @@ cairo_test_programs: $(COMPILED_TESTS) $(COMPILED_BAD_TESTS) $(COMPILED_NORETROC
cairo_proof_programs: $(COMPILED_PROOF_TESTS)
cairo_bench_programs: $(COMPILED_BENCHES)

cairo_proof_trace: $(CAIRO_TRACE_PROOF) $(CAIRO_MEM_PROOF)
cairo-rs_proof_trace: $(CAIRO_RS_TRACE_PROOF) $(CAIRO_RS_MEM_PROOF)

cairo_trace: $(CAIRO_TRACE) $(CAIRO_MEM)
cairo-rs_trace: $(CAIRO_RS_TRACE) $(CAIRO_RS_MEM)

Expand Down
80 changes: 80 additions & 0 deletions cairo_programs/ec_double_assign_new_x_v3.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from starkware.cairo.common.cairo_secp.bigint import BigInt3, nondet_bigint3, UnreducedBigInt3
from cairo_programs.compute_slope_v2 import verify_zero, unreduced_mul
from cairo_programs.compute_doubling_slope_v2 import compute_doubling_slope, EcPoint, unreduced_sqr

// Computes the addition of a given point to itself.
//
// Arguments:
// point - the point to operate on.
//
// Returns:
// res - a point representing point + point.
func ec_double{range_check_ptr}(point: EcPoint) -> (res: EcPoint) {
// The zero point.
if (point.x.d0 == 0) {
if (point.x.d1 == 0) {
if (point.x.d2 == 0) {
return (res=point);
}
}
}

let (slope: BigInt3) = compute_doubling_slope(point);
let (slope_sqr: UnreducedBigInt3) = unreduced_sqr(slope);

%{
from starkware.cairo.common.cairo_secp.secp_utils import pack
SECP_P = 2**255-19
slope = pack(ids.slope, PRIME)
x = pack(ids.point.x, PRIME)
y = pack(ids.point.y, PRIME)
value = new_x = (pow(slope, 2, SECP_P) - 2 * x) % SECP_P
%}

let (new_x: BigInt3) = nondet_bigint3();

%{ value = new_y = (slope * (x - new_x) - y) % SECP_P %}
let (new_y: BigInt3) = nondet_bigint3();

verify_zero(
UnreducedBigInt3(
d0=slope_sqr.d0 - new_x.d0 - 2 * point.x.d0,
d1=slope_sqr.d1 - new_x.d1 - 2 * point.x.d1,
d2=slope_sqr.d2 - new_x.d2 - 2 * point.x.d2,
),
);

let (x_diff_slope: UnreducedBigInt3) = unreduced_mul(
BigInt3(d0=point.x.d0 - new_x.d0, d1=point.x.d1 - new_x.d1, d2=point.x.d2 - new_x.d2), slope
);

verify_zero(
UnreducedBigInt3(
d0=x_diff_slope.d0 - point.y.d0 - new_y.d0,
d1=x_diff_slope.d1 - point.y.d1 - new_y.d1,
d2=x_diff_slope.d2 - point.y.d2 - new_y.d2,
),
);

return (res=EcPoint(new_x, new_y));
}

func main{range_check_ptr}() {
let x = BigInt3(1,2,3);
let y = BigInt3(4,5,6);
let p = EcPoint(x, y);

let (r) = ec_double(p);

assert r.x.d0 = 15463639180909693576579425;
assert r.x.d1 = 18412232947780787290771221;
assert r.x.d2 = 2302636566907525872042731;

assert r.y.d0 = 62720835442754730087165024;
assert r.y.d1 = 51587896485732116326812460;
assert r.y.d2 = 1463255073263285938516131;

return ();
}
101 changes: 101 additions & 0 deletions cairo_programs/fast_ec_add_v2.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@

from starkware.cairo.common.cairo_secp.bigint import BigInt3, nondet_bigint3, UnreducedBigInt3
from starkware.cairo.common.cairo_secp.field import (
is_zero,
unreduced_sqr,
)
from cairo_programs.compute_slope_v2 import compute_slope, EcPoint, verify_zero, unreduced_mul
// Computes the addition of two given points.
//
// Arguments:
// point0, point1 - the points to operate on.
//
// Returns:
// res - the sum of the two points (point0 + point1).
//
// Assumption: point0.x != point1.x (however, point0 = point1 = 0 is allowed).
// Note that this means that the function cannot be used if point0 = point1 != 0
// (use ec_double() in this case) or point0 = -point1 != 0 (the result is 0 in this case).
func fast_ec_add{range_check_ptr}(point0: EcPoint, point1: EcPoint) -> (res: EcPoint) {
// Check whether point0 is the zero point.
if (point0.x.d0 == 0) {
if (point0.x.d1 == 0) {
if (point0.x.d2 == 0) {
return (res=point1);
}
}
}

// Check whether point1 is the zero point.
if (point1.x.d0 == 0) {
if (point1.x.d1 == 0) {
if (point1.x.d2 == 0) {
return (res=point0);
}
}
}

let (slope: BigInt3) = compute_slope(point0, point1);
let (slope_sqr: UnreducedBigInt3) = unreduced_sqr(slope);

%{
from starkware.cairo.common.cairo_secp.secp_utils import pack
SECP_P = 2**255-19
slope = pack(ids.slope, PRIME)
x0 = pack(ids.point0.x, PRIME)
x1 = pack(ids.point1.x, PRIME)
y0 = pack(ids.point0.y, PRIME)
value = new_x = (pow(slope, 2, SECP_P) - x0 - x1) % SECP_P
%}
let (new_x: BigInt3) = nondet_bigint3();

%{ value = new_y = (slope * (x0 - new_x) - y0) % SECP_P %}
let (new_y: BigInt3) = nondet_bigint3();

verify_zero(
UnreducedBigInt3(
d0=slope_sqr.d0 - new_x.d0 - point0.x.d0 - point1.x.d0,
d1=slope_sqr.d1 - new_x.d1 - point0.x.d1 - point1.x.d1,
d2=slope_sqr.d2 - new_x.d2 - point0.x.d2 - point1.x.d2,
),
);

let (x_diff_slope: UnreducedBigInt3) = unreduced_mul(
BigInt3(d0=point0.x.d0 - new_x.d0, d1=point0.x.d1 - new_x.d1, d2=point0.x.d2 - new_x.d2),
slope,
);

verify_zero(
UnreducedBigInt3(
d0=x_diff_slope.d0 - point0.y.d0 - new_y.d0,
d1=x_diff_slope.d1 - point0.y.d1 - new_y.d1,
d2=x_diff_slope.d2 - point0.y.d2 - new_y.d2,
),
);

return (res=EcPoint(new_x, new_y));
}

func main{range_check_ptr}() {
let x_0 = BigInt3(1,2,3);
let y_0 = BigInt3(4,5,6);
let p_0 = EcPoint(x_0, y_0);

let x_1 = BigInt3(7,8,9);
let y_1 = BigInt3(10,11,12);
let p_1 = EcPoint(x_1, y_1);

let (r) = fast_ec_add(p_0, p_1);

assert r.x.d0 = 77371252455336267181195238;
assert r.x.d1 = 77371252455336267181195253;
assert r.x.d2 = 9671406556917033397649395;

assert r.y.d0 = 4;
assert r.y.d1 = 7;
assert r.y.d2 = 9;

return ();
}
Loading

0 comments on commit 736db30

Please sign in to comment.