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

Feature/bn128 montgomery multiplication #3

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

AllFi
Copy link
Collaborator

@AllFi AllFi commented Dec 1, 2023

What does this PR do?
This PR optimizes BN128Multiplication, and BN128Pairing precompiled contracts by implementing Montgomery form arithmetic. It is worth mentioning that this PR makes BN128Addition a little bit slower because of the overhead of the conversion to and from the Montgomery form.

Why are these changes required?
The current implementation of these precompiles is relatively slow. In general, this hinders on-chain zkSNARKs verification and, consequently, makes zk-based apps almost unfeasible (tronprotocol#4311). This PR is aiming to mitigate this issue.

This PR is far worse than tronprotocol#5507 in terms of optimization but it doesn't require JNI and doesn't introduce a lot of changes in the implementation.

This PR has been tested by:

  • Unit Tests
  • Manual Testing

Follow up

Extra details
I've performed some benchmarks before and after these modifications. The results are below:

The average time of operations before (Intel(R) Core(TM) i7-10750H CPU, 32 GB RAM):

BN128Addition cost: 41165 ns
BN128Multiplication cost: 3490156 ns
BN128Pairing cost (2 pairs): 87665281 ns

The average time of operations after:

BN128Addition cost: 48147 ns (+17%)
BN128Multiplication cost: 2393792 ns (-31%)
BN128Pairing cost (2 pairs): 57961905 ns (-33%)

1. implement montgomery arithmetic
2. add tests from go-ethereum
@AllFi AllFi force-pushed the feature/bn128_montgomery_multiplication branch from 7de7fe9 to 70b9645 Compare December 5, 2023 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant