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

In-circuit implementation for BN254 precompiles: ecadd, ecmul and ecpairing. #444

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

NikitaMasych
Copy link

@NikitaMasych NikitaMasych commented May 3, 2024

What ❔

  • Update yul wrappers for ecadd and ecmul from contract implementation to circuit.
  • Add ecpairing contract-wrapper in yul to delegate in-circuit.

Why ❔

To improve performance and boost EVM equivalence.

Checklist

  • PR title corresponds to the body of PR (we generate changelog entries from PRs).
  • Tests for the changes have been added / updated.
  • Documentation comments have been added / updated.

)
let gasToPay := ECADD_GAS_COST()

let success := precompileCall(precompileParams, gasToPay)
Copy link
Member

Choose a reason for hiding this comment

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

You should remove all unused code.

Copy link
Author

Choose a reason for hiding this comment

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

Hi, @vladbochok, the code I have left is used to check that the point is on the curve as well as coordinates are less than modulo

Copy link
Member

Choose a reason for hiding this comment

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

@NikitaMasych You also left code related to Montgomery form. It is useful when you do many operations with the numbers, but you only need to check that the numbers are less than the modulo and are in the curve. Please remove all code related to Montgomery and instead use traditional math as it would be cheaper.

Copy link
Author

Choose a reason for hiding this comment

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

Right, I have just though that it is better to not modify audited code as it serves its purpose. Additionally, there is premature handling of corner-case operations involving Montgomery form, such as Inf + 0 etc. In-circuit we bring only after passing all the corner-cases and checks.

Copy link
Author

Choose a reason for hiding this comment

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

Of course, I would however try to leverage in-circuit checks, leaving a blank call to it from yul code to achieve end-to-end best performance and security

Copy link
Member

Choose a reason for hiding this comment

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

Right, I have just though that it is better to not modify audited code as it serves its purpose

We will reaudit it fully, please go ahead change & simplify :)

Copy link
Member

@vladbochok vladbochok left a comment

Choose a reason for hiding this comment

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

if and(eq(x1, x2), eq(y1, y2)) {

I think circuit can handle this better than performing calculation in contract

@NikitaMasych
Copy link
Author

think circuit can handle this better than performing calculation in contract

Probably yes, however I guess there might be a necessity of vanguard validation, so that we won't get any issues with proof generation failing for bad input

@vladbochok
Copy link
Member

think circuit can handle this better than performing calculation in contract

Probably yes, however I guess there might be a necessity of vanguard validation, so that we won't get any issues with proof generation failing for bad input

I think this is specific if branching was introduced to optimize the edge case, not because it is a special case. So you can remove this optimisation as circuit will do bettter job anywat

@NikitaMasych
Copy link
Author

Hey guys, @vladbochok @StanislavBreadless, I have simplified yul contracts as much as possible, moving all validations inside the circuits. Also, I am wondering what values shall be selected as GasPrice for each of ecadd, ecmul and ecpairing, because for now I've just put something akin to EVM equivalent prices.

@NikitaMasych NikitaMasych changed the title Migrate to in-circuit implementation for BN254 precompiles In-circuit implementation for BN254 precompiles: ecadd, ecmul and ecpairing. May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants