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

ecPairing #53

Merged
merged 100 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 95 commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
bce8cbe
ecadd tests
ilitteri Jul 28, 2023
eb18443
ecmul tests
ilitteri Jul 28, 2023
067c16e
Add test utils
ilitteri Jul 28, 2023
36af2b6
Add test suit generator
ilitteri Jul 28, 2023
1a8fa73
Remove old tests
ilitteri Jul 28, 2023
69f44fc
Update tests
ilitteri Aug 1, 2023
00500c5
Fix ecadd
ilitteri Aug 1, 2023
f0db051
Fix ecmul
ilitteri Aug 1, 2023
eb08ec9
Handle modexp tests
ilitteri Aug 1, 2023
d795f87
Add modexp tests
ilitteri Aug 1, 2023
9b3073b
Update call helper function
ilitteri Aug 1, 2023
ca6645c
Fix helper function
ilitteri Aug 2, 2023
032f93c
Fix test utils
ilitteri Aug 2, 2023
aaefd0a
Improve ecmul precompile
IAvecilla Aug 4, 2023
2afbca8
Add test helper
ilitteri Aug 4, 2023
f858171
Add python script to test double and add algorithm (#13)
IAvecilla Aug 4, 2023
545e701
Merge branch 'add_eth_tests' of github.com:lambdaclass/zksync_era_pre…
ilitteri Aug 4, 2023
0491085
Add montgomery reduction algorithm
ilitteri Aug 4, 2023
d084888
Add Rust playground crate
ilitteri Aug 4, 2023
cba561e
Update montgomery reduction script
ilitteri Aug 4, 2023
b3ced8d
Add Montgomery reduction precompile
ilitteri Aug 4, 2023
69d4596
ecAdd refactor
ilitteri Aug 7, 2023
ce87027
Montgomery fix
ilitteri Aug 7, 2023
d8ef6d3
Add montgomery inv & modexp
ilitteri Aug 7, 2023
24732ab
Fix N'
ilitteri Aug 8, 2023
bb48215
Fix REDC
ilitteri Aug 8, 2023
36b5f3d
Fix Montgomery modular inverse
ilitteri Aug 8, 2023
de3c03e
Remove comments
ilitteri Aug 8, 2023
88dff03
Fix prints
ilitteri Aug 8, 2023
813d15b
Change to bytes comparison instead of decoded output
IAvecilla Aug 9, 2023
e9590d9
Patch tests with extra check for errors in ecadd and ecmul
IAvecilla Aug 9, 2023
8a59ba6
Add EOL
IAvecilla Aug 9, 2023
11c6a35
Update modexp precompile
IAvecilla Aug 10, 2023
5f4988d
Finish optimizing ecAdd & ecMul
ilitteri Aug 11, 2023
2b73a7e
Implement optimized montgomery invmod
ilitteri Aug 11, 2023
44f1ead
modexpGasCost
ilitteri Aug 13, 2023
8a87322
Cleanup
ilitteri Aug 13, 2023
15d43bc
Last version of ecAdd, ecMul & modexp
ilitteri Aug 13, 2023
6b93802
Fix modexp tests calldata
ilitteri Aug 14, 2023
a4b3f88
Fix modexp tests to pass with actual vm state
IAvecilla Aug 15, 2023
4f45d16
Update Montgomery precompile
ilitteri Aug 16, 2023
365468d
Rename montgomery lib
ilitteri Aug 16, 2023
b9b6c46
Add alt_bn128 projective point lib
ilitteri Aug 16, 2023
fca4c7d
Add quadratic extension field arithmetic lib
ilitteri Aug 16, 2023
acb0fa1
Add alt_bn128 pairing script
ilitteri Aug 16, 2023
17505b3
Finish quadratic extension field arithmetics
ilitteri Aug 16, 2023
1b06688
Fix Montgomery lib
ilitteri Aug 16, 2023
d7d4cf9
Implement double and addition steps
ilitteri Aug 16, 2023
6d59006
Arithmetics over Fp2 in Python (#14)
ilitteri Aug 17, 2023
7cfed2d
Merge branch 'main' of github.com:lambdaclass/zksync_era_precompiles …
ilitteri Aug 17, 2023
ce27c79
Fix point at infinity
ilitteri Aug 22, 2023
1696048
Rename lib
ilitteri Aug 22, 2023
ba369b1
Arithmetics over Fp6 in Python (#15)
IAvecilla Aug 22, 2023
b7a030d
Fp12 arithmetic (#21)
ColoCarletti Aug 22, 2023
dd7ed95
Merge branch 'main' of github.com:lambdaclass/zksync_era_precompiles …
ilitteri Aug 22, 2023
a7652f4
Add jacobian coordinates in Python (#22)
IAvecilla Aug 24, 2023
a83b64d
Fp2, Fp6 and Fp12 arithmetics in Yul (#23)
IAvecilla Aug 24, 2023
e77146a
Add point addition and line evaluation algorithm (#24)
IAvecilla Aug 25, 2023
c698633
Point doubling and line evaluation (#25)
ColoCarletti Aug 25, 2023
ec141a3
Refactor for line evaluation functions (#29)
IAvecilla Aug 25, 2023
575a0e6
add mul by gamma functions (#31)
ColoCarletti Aug 28, 2023
3c91ddb
Improve fp6 and fp12 arithmetics return type (#35)
IAvecilla Aug 29, 2023
8c6f24f
add millers loop (#30)
ColoCarletti Aug 30, 2023
31057fb
Add Final exponentiation (#32)
IAvecilla Aug 30, 2023
526dbee
fix fp6 arithmetic (#42)
ColoCarletti Sep 7, 2023
2bf9833
Precompute Frobenius constants (#44)
ilitteri Sep 7, 2023
6540a34
Add pairing tests for python implementation (#36)
IAvecilla Sep 8, 2023
a069ac5
Frobenius Operator in Yul (#43)
ilitteri Sep 8, 2023
2a5cf36
add mix addition in yul (#45)
ColoCarletti Sep 8, 2023
fb0a627
Add cyclotomicSquare and nSquare (#47)
ColoCarletti Sep 8, 2023
853c658
Add `t` exponentiation for fp12 in Yul (#48)
IAvecilla Sep 8, 2023
4b20a2a
Add final exp in yul (#49)
ColoCarletti Sep 8, 2023
ed3b7d3
Point doubling step of the miller loop in Yul (#46)
IAvecilla Sep 8, 2023
35b257d
G2 Twist in Yul (#50)
ilitteri Sep 8, 2023
e86dc55
Rename functions and fix compilation errors
IAvecilla Sep 8, 2023
0ac466a
Miller loop in yul (#52)
ColoCarletti Sep 11, 2023
aed093a
move from playground to ecPairing
ColoCarletti Sep 11, 2023
527cd5b
merge main
ColoCarletti Sep 11, 2023
8e31407
add pair function
ColoCarletti Sep 11, 2023
13e96c6
Fix pair check
ilitteri Sep 11, 2023
dcfff1f
Transform calldata into Montgomery form
ilitteri Sep 11, 2023
9391b6e
Remove console logs
ilitteri Sep 11, 2023
9cbfeb7
Add check for points in twisted curve
IAvecilla Sep 11, 2023
1fbdf66
Checks for points in group order
IAvecilla Sep 11, 2023
2cb416c
Fix pairing tests to match with L1 response
IAvecilla Sep 11, 2023
37ce4ab
Merge branch 'main' of github.com:lambdaclass/zksync_era_precompiles …
ilitteri Sep 11, 2023
c130dbb
Update README.md
ilitteri Sep 11, 2023
9f2ebb5
Some refactor
ilitteri Sep 12, 2023
faa56ab
Start g2 scalar mul implementation
ilitteri Sep 12, 2023
0f2336c
Fix compilation errors from new g2 operations
IAvecilla Sep 12, 2023
94e85b2
Correct some test assertions
IAvecilla Sep 12, 2023
8a3591b
Clean pairing precompile
IAvecilla Sep 14, 2023
f51512f
Merge branch 'main' into ecPairing
IAvecilla Sep 19, 2023
082e70a
Ec pairing documentation (#68)
ColoCarletti Sep 19, 2023
17a06c2
Merge branch 'main' of github.com:lambdaclass/zksync_era_precompiles …
ilitteri Sep 20, 2023
b499573
Apply previous audit fixes (#97)
ilitteri Sep 21, 2023
ae291c2
Add missing docs (#96)
ilitteri Sep 21, 2023
847e868
Fix ecPairing (#103)
ilitteri Sep 21, 2023
0e5a303
Fix `ecPairing` tests (#107)
ilitteri Sep 21, 2023
0b679b9
`ecPairing` refactors & cleanup (#98)
ilitteri Sep 21, 2023
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
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,27 @@ In the next weeks we will add more optimizations and benchmarks.
| --- | --- | --- |
| ecAdd | ✅ | ✅ |
| ecMul | ✅ | ✅ |
| ecPairing | 🏗️ | |
| ecPairing | | |
| modexp | ✅ | 🏗️ |

## Summary

- `ecAdd` is optimized with finite field arithmetic in Montgomery form and optimized modular inverse with a modification of the binary extended Euclidean algorithm that skips the Montgomery reduction step for inverting. There is not much more room for optimizations, maybe we could think of Montgomery squaring (SOS) to improve the finite field squaring.
- `ecMul` is optimized with finite field arithmetic in Montgomery form, optimized modular inverse with a modification of the binary extended Euclidean algorithm that skips the Montgomery reduction step for inverting, and the elliptic curve point arithmetic is being done in homogeneous projective coordinates. There are some other possible optimizations to implement, one is the one discussed in the Slack channel (endomorphism: GLV or wGLV), the [windowed method](https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Windowed_method), the [sliding-window method](https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Sliding-window_method), [wNAF (windowed non-adjacent form)](https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#w-ary_non-adjacent_form_(wNAF)_method) to improve the elliptic curve point arithmetic, and Montgomery squaring (SOS) to improve the finite field squaring, Jacobian projective coordinates (this would have similar performance and gas costs as working with the homogeneous projective coordinates but it would be free to add it since we need this representation for `ecPairing`).
- `modexp` status: TODO
- `ecPairing` will be implemented as it is detailed in this document. We currently have the towered field extensions working and started working on the line functions for the addition and the double step of the miller loop.
- `ecPairing`:
We have based our algorithm implementation primarily on the guidelines presented in the paper ["High-Speed Software Implementation of the Optimal Ate Pairing over Barreto–Naehrig Curves"](https://eprint.iacr.org/2010/354.pdf) . This implementation includes the utilization of Tower Extension Field Arithmetic and the Frobenius Operator.

To enhance the performance of the Miller loop, we have incorporated specific optimizations, we have optimized line evaluation based on the techniques outlined in ["The Realm of the Pairings"](https://eprint.iacr.org/2013/722.pdf) . Also, instead of using Jacobian coordinates, we have adopted projective coordinates. This choice is particularly advantageous given the large inversion/multiplication ratio in this context.

In the final exponentiation phase, we have integrated the methods presented in ["Memory-saving computation of the pairing final exponentiation on BN curves"](https://eprint.iacr.org/2015/192.pdf). This includes the Fuentes et al. method and the addition chain. We have also applied Faster Squaring in the Cyclotomic Subgroup, as described in [”Faster Squaring in the Cyclotomic Subgroup of Sixth Degree Extensions”](https://eprint.iacr.org/2009/565.pdf).

**Remaining Optimizations:** While our implementation has achieved notable results, there are still some straightforward optimizations that can be implemented:

- **Initial Iterations of Miller Loop:** We can avoid unnecessary multiplications by handling the first iterations of the Miller loop separately.
- **Optimizing Accumulated Value:** We are currently naively multiplying two fp12 elements, which contain many zeros. Modifying this calculation could enhance efficiency.

**Future Investigations:** We need to investigate the reliability of additional optimizations, such as the application of the GLV method for multiplication of rational points of elliptic curves.


# Used algorithms
Expand All @@ -35,12 +47,7 @@ In the next weeks we will add more optimizations and benchmarks.

## Resources

- [EVM precompiles list](https://www.evm.codes/precompiled?fork=shanghai)
- [EIP-196: Precompiled contracts for addition and scalar multiplication on the elliptic curve alt_bn128](https://eips.ethereum.org/EIPS/eip-196)
- [EIP-197: Precompiled contracts for optimal ate pairing check on the elliptic curve alt_bn128](https://eips.ethereum.org/EIPS/eip-197)
- [EIP-198: Big integer modular exponentiation](https://eips.ethereum.org/EIPS/eip-198)
- [EIP-1108: Reduce alt_bn128 precompile gas costs](https://eips.ethereum.org/EIPS/eip-1108)
- [EIP-2565: ModExp Gas Cost](https://eips.ethereum.org/EIPS/eip-2565)
You can find a curated list of helpful resources that we've used for guiding our implementations in [References](./References.md)

## Development

Expand Down
Loading