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

Update EIP-2537: Clarify the gast cost calculation formula for MSM #9245

Merged
merged 1 commit into from
Jan 23, 2025

Conversation

rodiazet
Copy link
Contributor

@rodiazet rodiazet commented Jan 15, 2025

This PR fixes value of multiplication_cost for G2 which according to comment in the G1/G2 MSM section saying that we want to avoid non-integer arithmetic should be divisible by 1000 the multiplier.
Keeping this as it is now makes the result of the formula depends on the order of multiplications.

This PR clarifies gas cost formula calculation for MSM precompile.

@rodiazet rodiazet requested a review from eth-bot as a code owner January 15, 2025 16:44
@github-actions github-actions bot added c-update Modifies an existing proposal s-review This EIP is in Review t-core labels Jan 15, 2025
@eth-bot
Copy link
Collaborator

eth-bot commented Jan 15, 2025

✅ All reviewers have approved.

@eth-bot eth-bot added the a-review Waiting on author to review label Jan 15, 2025
@jochem-brouwer
Copy link
Member

I might be wrong here, but doesn't the formula: (k * multiplication_cost * discount) / multiplier already explicitly state that the numerator should be calculated first and then thus dividing by the multiplier? The discount numbers are not divisibile by 1000 also (except 1000). I think what is meant with "To avoid non-integer arithmetic" that the formula is not: k * multiplication_cost * discount where discount for example could be (2 multiplications): 0.949 and not 949.

@rodiazet
Copy link
Contributor Author

yes but even if we calculate (k * multiplication_cost * discount) we cannot be sure that the result will be divisible by 1000. So we can change to make it divisible or add comment that this division must be integer division.

@chfast
Copy link
Member

chfast commented Jan 15, 2025

I might be wrong here, but doesn't the formula: (k * multiplication_cost * discount) / multiplier already explicitly state that the numerator should be calculated first and then thus dividing by the multiplier? The discount numbers are not divisibile by 1000 also (except 1000). I think what is meant with "To avoid non-integer arithmetic" that the formula is not: k * multiplication_cost * discount where discount for example could be (2 multiplications): 0.949 and not 949.

Not exactly. If you follow the formula strictly e.g. for G2MSM k=3 you get 62302.5. So you would need to specify that the division in the formula is integer division.

But the spec does the opposite. It says that the constants have been chosen carefully so that the division happens without fractions. However, we haven't read the spec and updated the constants so that this is now in conflict.

If we want to pick the "let's do integer division" path, I'd argue the formula should group constants like this

(multiplication_cost * discount // multiplier) * k

@jochem-brouwer
Copy link
Member

jochem-brouwer commented Jan 15, 2025

Ah ok good point, I see now. I am rather sure the original intent was for integer division, so the formula should likely have read: (maybe an older version of the BLS gas schedule could work with or without integer division yielding the same results)

(k * multiplication_cost * discount) // multiplier

EDIT: never mind, if multiplication_cost is an integer multiple of multiplier then non-integer arithmetic is indeed avoided no matter the k or discount pairs.

Out of interest, why do you choose to group the formula like this?

(multiplication_cost * discount // multiplier) * k

(and not (k * multiplication_cost * discount) // multiplier?)

I have not tested, but does this yield different results from the first formula when applied to the relevant discount and k values?

@garyschulte
Copy link
Contributor

I have not tested, but does this yield different results from the first formula when applied to the relevant discount and k values?

Seems it would, integer division would drop the last significant digit for discounts that are not multiples of 10.

@chfast
Copy link
Member

chfast commented Jan 16, 2025

Ah ok good point, I see now. I am rather sure the original intent was for integer division, so the formula should likely have read: (maybe an older version of the BLS gas schedule could work with or without integer division yielding the same results)

It seems to be it was the opposite, the constants in the spec were selected in a way the kind of the division don't matter.
But the original intent is not very important.

Out of interest, why do you choose to group the formula like this?

(multiplication_cost * discount // multiplier) * k

(and not (k * multiplication_cost * discount) // multiplier?)

Because multiplication_cost * discount // multiplier is a constant per k. So instead of a table of discounts you can have a table of these constants.

@chfast
Copy link
Member

chfast commented Jan 16, 2025

Using formula (k * multiplication_cost * discount) // multiplier introduces some "problematic" values for analysis. E.g. the cost per point of G2MSM, k=3 is 20767.333333333.

@rodiazet
Copy link
Contributor Author

Why cannot we just have a table with exact costs for each input length instead of discount table and the formula?

@Marchhill
Copy link
Contributor

I don't see that that this is a problem? As long as we all use the same formula (k * multiplication_cost * discount) // multiplier

@jochem-brouwer
Copy link
Member

Using formula (k * multiplication_cost * discount) // multiplier introduces some "problematic" values for analysis. E.g. the cost per point of G2MSM, k=3 is 20767.333333333.

@chfast Ah great, thanks for the insight, this makes a lot of sense. For completeness / "correctness" my preference would go to (multiplication_cost * discount // multiplier) * k as well

@rodiazet rodiazet force-pushed the update-eip2537-fix-gas-for-msm-g2 branch from acd98fd to 6b14aa8 Compare January 17, 2025 10:30
@rodiazet rodiazet changed the title Update EIP-2537: multiplication_cost for G2 update to make it divisible by 1000 Update EIP-2537: Clarify the gast cost calculation formula for MSM Jan 17, 2025
@rodiazet
Copy link
Contributor Author

ACD decided to stay with old formula with clarification that we are using an integer division.
https://ethereum-magicians.org/t/all-core-devs-execution-acde-203-january-16-2025/22346
This approach avoids a need to apply additional changes in clients and tests.

Copy link
Member

@jochem-brouwer jochem-brouwer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Love to clarification for // being integer division 👍 😄

@MarekM25
Copy link
Contributor

This PR should be closed without merging

@rodiazet
Copy link
Contributor Author

rodiazet commented Jan 23, 2025

I think we agreed during ACDE to change this as in this PR.
#9245 (comment)

@rodiazet rodiazet force-pushed the update-eip2537-fix-gas-for-msm-g2 branch from 6b14aa8 to e10561f Compare January 23, 2025 09:37
@Marchhill
Copy link
Contributor

This PR should be closed without merging

@MarekM25 it now just clarifies to use integer division rather than changing gas costs, so I think it's good to merge

@MarekM25
Copy link
Contributor

yes, my bad, good to merge indeed

Copy link
Member

@ralexstokes ralexstokes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed to merge following ethereum/pm#1227

@eth-bot eth-bot enabled auto-merge (squash) January 23, 2025 14:51
Copy link
Collaborator

@eth-bot eth-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All Reviewers Have Approved; Performing Automatic Merge...

@eth-bot eth-bot merged commit 62c3b28 into ethereum:master Jan 23, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-review Waiting on author to review c-update Modifies an existing proposal s-review This EIP is in Review t-core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants