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

Cairo 1 contracts & Circuits Optimizations #129

Merged
merged 111 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
111 commits
Select commit Hold shift + click to select a range
04575c4
init cairo1
feltroidprime Jun 11, 2024
f0fcade
bn & bls final exp hints
feltroidprime Jun 14, 2024
ecbcf0c
add scalar_to_base_neg3_le python version
feltroidprime Jun 14, 2024
12fb77f
implementing scalar_to_base_neg3_le
luiz-lvj Jun 15, 2024
c1fbc79
performance updates
luiz-lvj Jun 15, 2024
b4c3157
updating tests
luiz-lvj Jun 15, 2024
0612d3e
Tweak neg_3_base_le to use one divmod
feltroidprime Jun 16, 2024
9c689fc
clean neg3 tests
feltroidprime Jun 16, 2024
cdc278a
compute (-3) powers in loop + returns abs(multiplicity)
feltroidprime Jun 16, 2024
257e149
move utils tests to utils.cairo
feltroidprime Jun 16, 2024
06dc9d9
add cairo transcript hasher fn & tests
feltroidprime Jun 16, 2024
3f06074
Merge commit 'dac43112e3298cf64cc8eb656dff05f1d4072ec6'
feltroidprime Jun 17, 2024
52ced17
hydra: allow arbitrary number of Fpk elmts in extf_mul
feltroidprime Jun 17, 2024
71ec916
hydra: use dedicated fp2_square instead of extf_mul
feltroidprime Jun 17, 2024
1b57dc4
bls sage math
feltroidprime Jun 18, 2024
ed1bab3
hydra: handle extf_square automatically
feltroidprime Jun 18, 2024
56ac985
bls sage ^ 2
feltroidprime Jun 18, 2024
4afef4b
bls ^3
feltroidprime Jun 19, 2024
def46a3
hydrat/repo : small updates
feltroidprime Jun 19, 2024
3447312
hydra/fustat : batch extf muls per miller loop bit
feltroidprime Jun 19, 2024
213131f
update to latest nightly
feltroidprime Jun 19, 2024
1d78e7e
cairo: add some utils fns.
feltroidprime Jun 19, 2024
e533494
hydra: Add G1Point to Cairo struct util
feltroidprime Jun 20, 2024
ce36474
cairo: basic ec_add circuit test and failing contract
feltroidprime Jun 20, 2024
df4f544
optimize loops
feltroidprime Jun 24, 2024
b8cd1be
wip: cairo1 compiler
feltroidprime Jun 24, 2024
23c4ae0
wip: cairo1 compiler^2
feltroidprime Jun 24, 2024
14b51c4
upd docs
feltroidprime Jun 27, 2024
183834a
wip: cairo1 compiler^3
feltroidprime Jun 27, 2024
abc2f27
add io utils
feltroidprime Jun 27, 2024
bd06410
hydra: remove unused computation
feltroidprime Jun 27, 2024
f400901
replace CurveID enum to match Cairo1
feltroidprime Jun 28, 2024
27efa87
update circuit syntax in compiler
feltroidprime Jun 28, 2024
05132a0
hydra auto-gen cairo1 unit tests
feltroidprime Jun 28, 2024
34d2539
rename tests/cairo_programs to tests/fustat_programs
feltroidprime Jun 28, 2024
dbcb607
rm deprecated doc
feltroidprime Jun 28, 2024
7d4dc79
clean algebra.py
feltroidprime Jun 28, 2024
f54debd
🔥 pure python zk-ecip
feltroidprime Jun 29, 2024
710cb10
py-ecip: add some comments
feltroidprime Jun 29, 2024
014ed37
pure python bls witness ok
feltroidprime Jun 30, 2024
45eeb7f
⚡️ hydra: multi pairing check for bn & bls
feltroidprime Jul 1, 2024
836c986
hydra: small comment/fixes
feltroidprime Jul 3, 2024
684dd5d
small cairo updates
feltroidprime Jul 3, 2024
3a2b7a5
hydra: always compile functions in the same order
feltroidprime Jul 3, 2024
b24e630
hydra: small fixes/checks
feltroidprime Jul 4, 2024
50470e2
Efficient hint computation for bls curves (#131)
akinovak Jul 5, 2024
014454d
use extf_inv instead of div
feltroidprime Jul 6, 2024
b23af55
separate lhs/rhs accumulation to re-use Pi+1[0](z) and update cairo1 …
feltroidprime Jul 6, 2024
8f63749
small updates
feltroidprime Jul 6, 2024
236cd42
add handmade bit0/bit1 Miller Cairo1 sub-circuits
feltroidprime Jul 6, 2024
a43b7d0
update compiled cairo circuits and benchmarks
feltroidprime Jul 6, 2024
b701e0d
derive_point_fromX circuit & test
feltroidprime Jul 9, 2024
cdfdbb6
move frobenius utils to separate file and improve perf with caching/t…
feltroidprime Jul 17, 2024
addd79a
Test MPCheck with pre-computed extra miller loop result
feltroidprime Jul 17, 2024
e9feea7
Add Cairo1SerializableStruct class
feltroidprime Jul 17, 2024
6935699
minor cleaning
feltroidprime Jul 17, 2024
a2111f1
minor fixes/refactor
feltroidprime Jul 17, 2024
29cddd9
refactor cairo1 compiler to handle structs in input/output
feltroidprime Jul 17, 2024
31031ee
update scarb
feltroidprime Jul 18, 2024
a3e012e
add basic field operations
feltroidprime Jul 18, 2024
9eb1266
mpcheck: prepare_pairs, prepare_lambda_root_evals, and init_bit circu…
feltroidprime Jul 19, 2024
b0ebec7
update (circuits, definitions, utils).cairo
feltroidprime Jul 19, 2024
35081c4
WIP: Pairing/Groth16 fns
feltroidprime Jul 19, 2024
e736bb8
Support comment per circuit operation
feltroidprime Jul 19, 2024
fcd4284
Support empty circuit in compilation
feltroidprime Jul 20, 2024
784a501
split bn254 finalize step to access the lines
feltroidprime Jul 20, 2024
fa68742
fixes & comments support in eval poly
feltroidprime Jul 20, 2024
0fae3bd
add Cairo1 structs
feltroidprime Jul 20, 2024
9e65983
small fixes & better generated comments
feltroidprime Jul 20, 2024
3b59b79
smal refactor
feltroidprime Jul 20, 2024
307c711
update generated circuits
feltroidprime Jul 20, 2024
6f0692e
bls MPcheck Cairo1 interfaces
feltroidprime Jul 20, 2024
d148171
compiler : pack inputs 2 by 2
feltroidprime Jul 21, 2024
eaf3e8f
bls 3 pairs test passing + fixes
feltroidprime Jul 21, 2024
38bc870
BLS 2 pairs passing
feltroidprime Jul 21, 2024
6504305
add bit_00 support, bn pairing, and full tests
feltroidprime Jul 23, 2024
4e6be8f
Wip : Arbitrary Msm
feltroidprime Jul 23, 2024
805d35d
msm tests with fixes and neg_3 hint support
feltroidprime Jul 25, 2024
35588dc
apply isort to imports
feltroidprime Jul 26, 2024
86fbaac
ZK-ECIP on G2 adapation 🔥
feltroidprime Jul 26, 2024
7c2c53b
Refactor Curve object into Weirstrass/TwistedEdwards/Pairing
feltroidprime Jul 27, 2024
3b9662d
add pytests
feltroidprime Jul 27, 2024
b864efb
test_ecip.py
feltroidprime Jul 27, 2024
1776088
add scarb fmt CI
feltroidprime Jul 27, 2024
08bda3d
Hydra CI
feltroidprime Jul 27, 2024
2857fcb
CI: cairoZero format, scarb test
feltroidprime Jul 27, 2024
9fcaa04
set pytest cache to build
feltroidprime Jul 28, 2024
84c8ccb
update to cairo-lang 0.13.2a0 and add fustat tests in CI
feltroidprime Jul 29, 2024
da5d143
test multi miller loop with precomputed lines
feltroidprime Jul 30, 2024
1c26afc
♻️ simplify Mpcheck circuits generation
feltroidprime Aug 1, 2024
c1bbed7
Improve BLS final exp witness computation (#134)
akinovak Aug 1, 2024
3ea0325
apply isort | remove unused imports
feltroidprime Aug 1, 2024
f85925a
add groth16 circuits with line precomputation and remove mpcheck for …
feltroidprime Aug 1, 2024
7d1033c
Merge branch 'contracts' of github.com:keep-starknet-strange/garaga i…
feltroidprime Aug 1, 2024
f00a0d1
update pyproject.toml
feltroidprime Aug 1, 2024
cbc444e
Move final exp tests to proper test
feltroidprime Aug 2, 2024
6a2baff
Bump Cairo version to 2.7.0
feltroidprime Aug 2, 2024
d7f6dce
compiler : pack inputs by 3 and add reference to input number in comment
feltroidprime Aug 4, 2024
7f2148c
refactor mpcheck circuits generation for any # of fixed G2 points
feltroidprime Aug 4, 2024
d1c1c71
improve python polynomial perf
feltroidprime Aug 5, 2024
502996d
ecip; minor comments
feltroidprime Aug 5, 2024
3330594
add to_int helper
feltroidprime Aug 5, 2024
71525cb
use fastecdsa's scalar mul C binding
feltroidprime Aug 5, 2024
de32b8e
refactor mpcheck test writer
feltroidprime Aug 5, 2024
0e80f60
refactor msm test writer
feltroidprime Aug 5, 2024
5473c23
add calldata parsers
feltroidprime Aug 5, 2024
67732d7
fix random sparsity test
feltroidprime Aug 5, 2024
2b50432
parallel test gen
feltroidprime Aug 6, 2024
17eed5b
groth16 contract generator
feltroidprime Aug 6, 2024
47e8eab
msm contract
feltroidprime Aug 6, 2024
418a3f7
Merge branch 'main' into contracts
feltroidprime Aug 6, 2024
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
18 changes: 18 additions & 0 deletions .github/workflows/cairo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Cairo 1 tests

on:
push:
merge_group:
pull_request_target:

jobs:
test-cairo:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.7.0"
- run: scarb fmt --check
working-directory: src/cairo
- run: cd src/cairo && scarb test
38 changes: 38 additions & 0 deletions .github/workflows/fustat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Cairo Zero (Fustat) tests

on:
push:
merge_group:
pull_request_target:

jobs:
test-fustat:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10.14
uses: actions/setup-python@v2
with:
python-version: 3.10.14
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: '^1.17'
- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
components: rustfmt, clippy

- name: Setup repo
run: make setup
- name: Check cairo Formatting
run: |
source venv/bin/activate && ./tools/make/fustat_format_check.sh
- name: Compile cairo files
run: source venv/bin/activate && make build
- name: Run fustat programs
run: |
source venv/bin/activate
pytest tests/fustat_programs/test_fustat.py -n auto -s
44 changes: 44 additions & 0 deletions .github/workflows/hydra.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Hydra backend test

on:
push:
merge_group:
pull_request_target:

jobs:
test-hydra:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10.14
uses: actions/setup-python@v2
with:
python-version: 3.10.14
- name: Install dependencies
run: |
python -m venv venv
source venv/bin/activate
echo 'export PYTHONPATH="$PWD:$PYTHONPATH"' >> venv/bin/activate
pip install -r tools/make/requirements.txt
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: '^1.17'
- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
components: rustfmt, clippy

- name: Compile Gnark
run: make go
- name: Compile Rust bindings with maturin
run: |
source venv/bin/activate
cd tools/hades_binding
maturin develop --release
- name: Run pytest
run: |
source venv/bin/activate
pytest -n auto
10 changes: 0 additions & 10 deletions .github/workflows/test.yml

This file was deleted.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ run:

run-no-compile:
@echo "A script to select, run one Cairo file without compiling it"
@echo "Thank you for proving Garaga!"
@echo "Thank you for testing Garaga!"
./tools/make/launch_cairo_files.py -no_compile

run-pie:
Expand Down
84 changes: 27 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ Ensure you have the following installed:
- [graphviz](https://graphviz.org/download/) - Necessary for generating graphical representations of profiling data.
- A functional [SageMath](https://www.sagemath.org/download.html) installation or an operational [Docker](https://www.docker.com/get-started/) daemon with non-sudo privileges.

If you're using SageMath directly, make sure the edit the default parameter `use_docker` to `False` in the `EcipCLI` class inside `tools/ecip_cli.py`

### Setup

Once you have the prerequisites installed, clone the repository, and set up your development environment with the following command. Be sure to run this command from the root of the repository.
Expand All @@ -86,71 +84,43 @@ make run



| OP | Weight in steps |
|--------------------|-------------------|
| MULMOD | 8 |
| ADDMOD | 4 |
| ASSERT_EQ | 2 |
| RLC | 28 |
| POSEIDON BN254 | 14 |
| POSEIDON BLS12_381 | 17 |
| OP | Weight in steps | Comment |
|------------------|-------------------|-----------------------------------------------------------------------------------------|
| MULMOD | 8 | Equivalent cost of a*b % p with the modulo builtin in VM steps |
| ADDMOD | 4 | Equivalent cost of a+b % p with the modulo builtin in VM steps |
| ASSERT_EQ | 2 | Equivalent cost of a==b % p with the modulo builtin in VM steps |
| RLC | 28 | Cost of writing a field element to the value segment and retrieving random coefficients |
| POSEIDON 3 LIMBS | 14 | Cost of hashing the first 3 limbs of 384 bits emulated field element with Poseidon |
| POSEIDON 4 LIMBS | 17 | Cost of hashing the 4 limbs of 384 bits emulated field element with Poseidon |


| circuit | MULMOD | ADDMOD | ASSERT_EQ | POSEIDON | RLC | ~steps |
|-------------------------------------------|----------|----------|-------------|------------|-------|----------|
| Derive Point From X | 6 | 2 | 0 | 0 | 0 | 56 |
| Double Step BLS12_381 | 24 | 26 | 2 | 0 | 0 | 300 |
| Double Step BN254 | 26 | 26 | 2 | 0 | 0 | 316 |
| Fp6 SQUARE_TORUS | 12 | 22 | 0 | 7 | 1 | 324 |
| Mul L by L | 18 | 8 | 0 | 11 | 1 | 380 |
| Double-and-Add Step BLS12_381 | 34 | 47 | 4 | 0 | 0 | 468 |
| Fp12 SQUARE | 25 | 11 | 0 | 13 | 1 | 480 |
| Triple Step | 38 | 43 | 4 | 0 | 0 | 484 |
| Mul LL by L | 26 | 13 | 0 | 13 | 1 | 496 |
| Double-and-Add Step BN254 | 38 | 47 | 4 | 0 | 0 | 500 |
| Mul by L | 28 | 15 | 0 | 13 | 1 | 520 |
| Mul LL by LL | 32 | 18 | 0 | 13 | 1 | 564 |
| Mul by LL | 34 | 20 | 0 | 13 | 1 | 588 |
| Fp12 MUL | 36 | 22 | 0 | 13 | 1 | 612 |
| Fp6 MUL_TORUS | 36 | 34 | 0 | 13 | 2 | 688 |
| MSM 1 points | 159 | 130 | 0 | 52 | 0 | 2624 |
| MSM 2 points | 203 | 168 | 0 | 64 | 0 | 3320 |
| MSM 3 points | 247 | 206 | 0 | 76 | 0 | 4016 |
| MSM 10 points | 555 | 472 | 0 | 160 | 0 | 8888 |
| MSM 50 points | 2315 | 1992 | 0 | 640 | 0 | 36728 |
| Miller n=1 BLS12_381 | 4936 | 4966 | 137 | 1580 | 131 | 90154 |
| Miller n=1 BN254 | 5984 | 5927 | 177 | 1810 | 153 | 101558 |
| Final Exp BN254 | 4686 | 7223 | 3 | 1931 | 317 | 102296 |
| Final Exp BLS12_381 | 5128 | 9061 | 3 | 2333 | 384 | 127687 |
| Miller n=2 BLS12_381 | 8030 | 8171 | 273 | 2276 | 199 | 141734 |
| Miller n=2 BN254 | 10132 | 10107 | 353 | 2740 | 241 | 167298 |
| Miller n=3 BLS12_381 | 11356 | 11608 | 409 | 3088 | 267 | 198070 |
| MultiPairing n=1 BN254 | 10670 | 13150 | 180 | 3741 | 470 | 203854 |
| MultiPairing n=1 BLS12_381 | 10064 | 14027 | 140 | 3913 | 515 | 217841 |
| MSM 1 points | 150 | 128 | 0 | 52 | 0 | 2544 |
| MSM 2 points | 194 | 166 | 0 | 64 | 0 | 3240 |
| MSM 3 points | 238 | 204 | 0 | 76 | 0 | 3936 |
| MSM 10 points | 546 | 470 | 0 | 160 | 0 | 8808 |
| MSM 50 points | 2306 | 1990 | 0 | 640 | 0 | 36648 |
| Miller n=1 BLS12_381 | 2672 | 2686 | 137 | 790 | 63 | 47588 |
| Miller n=1 BN254 | 3303 | 3228 | 177 | 828 | 66 | 53130 |
| Miller n=2 BLS12_381 | 4418 | 4525 | 273 | 812 | 63 | 69558 |
| Miller n=2 BN254 | 5639 | 5576 | 353 | 852 | 66 | 81898 |
| Miller n=3 BLS12_381 | 6164 | 6364 | 409 | 834 | 63 | 91528 |
| Final Exp BN254 | 4681 | 7218 | 3 | 1931 | 317 | 102236 |
| Miller n=3 BN254 | 7975 | 7924 | 529 | 876 | 66 | 110666 |
| Final Exp BLS12_381 | 5123 | 9056 | 3 | 2333 | 384 | 127627 |
| MultiPairing n=1 BN254 | 7984 | 10446 | 180 | 2759 | 383 | 155366 |
| MultiPairing n=1 BLS12_381 | 7795 | 11742 | 140 | 3123 | 447 | 175215 |
| MultiPairing n=2 BN254 | 10320 | 12794 | 356 | 2783 | 383 | 184134 |
| MultiPairing n=2 BLS12_381 | 9541 | 13581 | 276 | 3145 | 447 | 197185 |
| MultiPairing n=3 BN254 | 12656 | 15142 | 532 | 2807 | 383 | 212902 |
| MultiPairing n=3 BLS12_381 | 11287 | 15420 | 412 | 3167 | 447 | 219155 |
| BLS12FinalExp Fp12 Karabina No EXTF Trick | 7774 | 43002 | 0 | 0 | 0 | 234200 |
| Miller n=3 BN254 | 14456 | 14463 | 529 | 3758 | 329 | 236382 |
| MultiPairing n=2 BLS12_381 | 13158 | 17232 | 276 | 4609 | 583 | 269421 |
| MultiPairing n=2 BN254 | 14818 | 17330 | 356 | 4671 | 558 | 269594 |
| MultiPairing n=3 BLS12_381 | 16484 | 20669 | 412 | 5421 | 651 | 325757 |
| MultiPairing n=3 BN254 | 19142 | 21686 | 532 | 5689 | 646 | 338678 |


| | Final Exp BN254 | Final Exp BLS12_381 | Miller n=1 BLS12_381 | Miller n=1 BN254 | Miller n=2 BLS12_381 | Miller n=2 BN254 | Miller n=3 BLS12_381 | Miller n=3 BN254 |
|---------------------|-------------------|-----------------------|------------------------|--------------------|------------------------|--------------------|------------------------|--------------------|
| EXTF_SQUARE | 0 | 0 | 63 | 65 | 63 | 65 | 63 | 65 |
| EXTF_MUL_DENSE | 62 | 33 | 0 | 0 | 5 | 22 | 63 | 66 |
| MUL_TORUS | 62 | 33 | 0 | 0 | 0 | 0 | 0 | 0 |
| SQUARE_TORUS | 189 | 315 | 0 | 0 | 0 | 0 | 0 | 0 |
| Double Step | 0 | 0 | 58 | 44 | 116 | 88 | 174 | 132 |
| Double-and-Add Step | 0 | 0 | 4 | 21 | 8 | 42 | 12 | 63 |
| Triple Step | 0 | 0 | 1 | 0 | 2 | 0 | 3 | 0 |
| MUL_L_BY_L | 0 | 0 | 5 | 22 | 68 | 88 | 73 | 110 |
| MUL_LL_BY_LL | 0 | 0 | 0 | 0 | 5 | 22 | 5 | 23 |
| MUL_LL_BY_L | 0 | 0 | 0 | 0 | 0 | 0 | 58 | 44 |
| MUL_BY_L | 0 | 0 | 58 | 44 | 0 | 0 | 0 | 0 |
| MUL_BY_LL | 0 | 0 | 5 | 22 | 58 | 44 | 5 | 21 |



## Support & How to Contribute

Expand Down
Empty file added build/.gitkeep
Empty file.
Loading
Loading