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

perf: Groth16 verifier #1238

Closed
wants to merge 12 commits into from
Closed

perf: Groth16 verifier #1238

wants to merge 12 commits into from

Conversation

yelhousni
Copy link
Contributor

@yelhousni yelhousni commented Aug 7, 2024

Description

currently we do GT == e(a1,b1)*e(a2,b2)*e(a3,b3) using Pair for the triple-pairing, which means computing the multi-Miller loop and then reducing it to a unique value with FinalExponentiation that should match GT from the VerifyingKey.
In this PR I wanted to check if including GT in the pairing computation (triple-pairing becomes quadruple) and using FinalExponentiationCheck (inside PairingCheck) would result in less constraints (using #1143, #1155, #1207).

N.B.:

  • We can do better by implementing a QuadruplePairingCheck method that merges the squares of the equivalence check with the Miller loop (similar to perf: optimize DoublePairingCheck i.e. e(a,b)e(c,d) == 1 #1230 but for 4 pairs). I implemented this and it saves 5.7% but add more code to gnark that is only useful for G16 verifier circuit.
  • We can keep GT in the VerifyingKey and include it the triple-Miller loop to compute the hinted witness residue. This also should work but again more specialized methods and more lines of code for a single circuit that we probably don't care about much.

Type of change

  • New feature (non-breaking change which adds functionality)

How has this been tested?

Tests pass.

How has this been benchmarked?

BN254-G16 verifier in a BN254 circuit:

  • old: 1,455,104 r1cs
  • new: 1,390,556 scs
  • new (with QuadruplePairingCheck): 1,310,579 scs

Checklist:

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • I did not modify files generated from templates
  • golangci-lint does not output errors locally
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@yelhousni yelhousni requested a review from ivokub August 7, 2024 16:42
@yelhousni yelhousni self-assigned this Aug 7, 2024
@yelhousni yelhousni added the perf label Aug 7, 2024
@yelhousni yelhousni requested a review from gbotrel August 7, 2024 16:42
@ivokub
Copy link
Collaborator

ivokub commented Sep 2, 2024

Yes, I agree that maybe it is a very specialized method for Groth16 only which we indeed do not care too much about. We can however avoid defining the QuadruplePairingCheck in the algebra.Pairing interface and then explicitly test that the Pairing object implements it and then switch.

But even in this case this means that we have some overhead in the future when we change some implementation etc (to upgrade all the hints etc.), which I think is not probably worth it (or maybe with a comment which says that we can ditch the implementation if it causes any problems in the future, and everything should continue working nicely because we check that we have Quadruple method. And we can have a test which checks that 2*DoublePairingCheck is less efficient than QuadruplePairingCheck).

If it looks good, then let me know, I can add the interface checks etc. But after #1230 is merged.

@yelhousni
Copy link
Contributor Author

yelhousni commented Sep 3, 2024

@ivokub I suggest to close this PR since it's not for a very useful use-case right now and it makes the code more complex and the Groth16 verification different that the out-circuit counter part in gnark. Wdyt?

@ivokub
Copy link
Collaborator

ivokub commented Sep 3, 2024

@ivokub I suggest to close this PR since it's not for a very useful use-case right now and it makes the code more complex and the Groth16 verification different that the out-circuit counter part in gnark. Wdyt?

I agree. Lets close it for now.

@yelhousni yelhousni closed this Sep 3, 2024
@yelhousni yelhousni deleted the perf/G16-Verifier branch September 3, 2024 22:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants