-
Notifications
You must be signed in to change notification settings - Fork 137
zkevm: amortized bn128_pairings #1656
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
Conversation
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
base_cost = 45_000 | ||
pairing_cost = 34_000 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LMK if these costs can be pulled from somewhere. If not, we can leave it this way.
tx = Transaction( | ||
to=code_address, | ||
gas_limit=env.gas_limit, | ||
data=_generate_bn128_pairs(optimal_per_call_num_pairings, 42), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the discovered optimal number of pairings per precompile call, generate the corresponding pairs with a fixed seed.
@@ -314,7 +312,20 @@ def test_worst_modexp( | |||
"090689D0585FF075EC9E99AD690C3395BC4B313370B38EF355ACDADCD122975B", | |||
"12C85EA5DB8C6DEB4AAB71808DCB408FE3D1E7690C43D37B4CE6CC0166FA7DAA", | |||
], | |||
id="bn128_pairing", | |||
id="bn128_two_pairings", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leaving this test as two_pairings
to cover the point multiplication of the pairing check.
"2BB8324AF6CFC93537A2AD1A445CFD0CA2A71ACD7AC41FADBF933C2A51BE344D", | ||
"120A2A4CF30C1BF9845F20C6FE39E07EA2CCE61F0C9BB048165FE5E4DE877550", | ||
], | ||
id="bn128_one_pairing", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creating the case of one pairing, which maximizes the number of total final exponentiations in the block.
|
||
@pytest.mark.valid_from("Cancun") | ||
@pytest.mark.slow() | ||
def test_amortized_bn128_pairings( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the new test that optimizes for number of miller loops. That is, amortize the base cost (final exponentiation) in as many miller loops as possible while keeping in check quadratic memory expansion costs.
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
Not sure why the coverage CI is failing. |
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to see if we could optimize the loop to find the maximum number of pairings per call and tried to use binary search but I couldn't find a solution.
LGTM, thanks!
This PR adds a new test to cover
bn128_pairings
but trying to amortize the base cost.Initially, I thought this amortized one had 8 billion cycles, but now I'm wondering if I missed one digit. These numbers make more sense.