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

feat: handle invalid signature failures in ECRecover precompile #1101

Merged
merged 15 commits into from
Apr 23, 2024

Conversation

ivokub
Copy link
Collaborator

@ivokub ivokub commented Apr 12, 2024

Description

In zkEVM, the signature values for the ECRecover precompile can be invalid in some cases. The failing cases are:

  • r^3 + ax+b is not a quadratic residue, preventing computing the y-coordinate of commitment R
  • the resulting public key is 0

We need to be able to check such cases to ensure that we can prove reverts. This PR adds a path to also check invalid inputs when isFailure input is given.

Type of change

  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Breaking change due to changing the signature of ECRecover function.

How has this been tested?

  • TestECRecoverQNR - tests that check for QNR failure succeeds
  • TestECRecoverQNRWoFailure - tests that QNR input cannot be provided when isFailure argument is false
  • TestECRecoverInfinity - tests that check for zero public key succeeds
  • TestECRecoverInfinityWoFailure - tests that zero PK input cannot be provided when isFailure is false
  • TestInvalidFailureTag - tests that valid signature cannot be proven when isFailure is true

How has this been benchmarked?

ECRecover master 459082, now 465156

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

@ivokub ivokub added new feature P1: High Issue priority: high labels Apr 12, 2024
@ivokub ivokub self-assigned this Apr 12, 2024
@ivokub ivokub requested a review from yelhousni April 12, 2024 14:14
@ivokub
Copy link
Collaborator Author

ivokub commented Apr 12, 2024

Implemented for now, but the increase in the number of constraints is huge. I recon this is due to needing to use WithCompleteArithmetic when computing the public key in-circuit. I'm trying to figure out maybe we can still use incomplete arithmetic (by adding-subtracting G for example) for scalar multiplication.

@ivokub ivokub added the zk-evm label Apr 12, 2024
@ivokub
Copy link
Collaborator Author

ivokub commented Apr 13, 2024

Implemented for now, but the increase in the number of constraints is huge. I recon this is due to needing to use WithCompleteArithmetic when computing the public key in-circuit. I'm trying to figure out maybe we can still use incomplete arithmetic (by adding-subtracting G for example) for scalar multiplication.

This approach works. Now can avoid complete arithmetic in JointScalarMul, but I still need to do one complete addition. It is now 477275 constraints (vs master 459082)

@ivokub
Copy link
Collaborator Author

ivokub commented Apr 17, 2024

Optimization pointed out by @yelhousni - instead of using AddUnified we can just select between G and P. Saves 10k constraints. Now it is 466703 constraints.

@ivokub
Copy link
Collaborator Author

ivokub commented Apr 17, 2024

And, before we can merge this one, we need to merge Consensys/gnark-crypto#497 and update go.mod here to point to gnark-crypto master.

Copy link
Contributor

@yelhousni yelhousni left a comment

Choose a reason for hiding this comment

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

great work 🔥

@ivokub ivokub merged commit 2fd5c2b into master Apr 23, 2024
7 checks passed
@ivokub ivokub deleted the feat/ecrecover-failures branch April 23, 2024 21:58
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