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

fix: Ecdsa Malleability Bug #512

Merged
merged 5 commits into from
Jun 26, 2023
Merged

fix: Ecdsa Malleability Bug #512

merged 5 commits into from
Jun 26, 2023

Conversation

suyash67
Copy link
Contributor

@suyash67 suyash67 commented Jun 7, 2023

Description

ECDSA has an inherent malleability because of the way $s$ in an ECDSA signature is computed. Read more about this here. This PR adds the constraint $s < |Fr|/2$ in ECDSA verification to fix the malleability. Additionally, in the signature construction, we make sure that $s < |Fr|/2$.

Acknowledgements: @Rumata888 corrected the constraint on $s$ in verification.

Checklist:

  • I have reviewed my diff in github, line by line.
  • Every change is related to the PR description.
  • I have linked this pull request to the issue(s) that it resolves.
  • There are no unexpected formatting changes, superfluous debug logs, or commented-out code.
  • There are no circuit changes, OR specifications in /markdown/specs have been updated.
  • There are no circuit changes, OR a cryptographer has been assigned for review.
  • I've updated any terraform that needs updating (e.g. environment variables) for deployment.
  • The branch has been rebased against the head of its merge target.
  • I'm happy for the PR to be merged at the reviewer's next convenience.
  • New functions, classes, etc. have been documented according to the doxygen comment format. Classes and structs must have @brief describing the intended functionality.
  • If existing code has been modified, such documentation has been added or updated.

@suyash67 suyash67 marked this pull request as ready for review June 8, 2023 13:22
@suyash67 suyash67 requested a review from Rumata888 June 8, 2023 17:14
Copy link
Contributor

@Rumata888 Rumata888 left a comment

Choose a reason for hiding this comment

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

I've made two changes:

  1. The function was actually less or equal.
  2. Once I changed it to be less_than, the logic for constraining s became incorrect. You were checking that s < (Fr::modulus / 2). It is different from 2*s < Fr::modulus, because Fr::modulus is odd. Think mod 3. If you check that something is less than 3/2, then the only option is zero, while 1 should also be a legitimate value.

bool result = composer.check_circuit();
EXPECT_EQ(result, true);
// Checking edge conditions
fq random_input = fq::random_element();
Copy link
Contributor

Choose a reason for hiding this comment

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

nit, but not a blocker: unit tests should ideally not contain random elements as they then become unreproducable

Copy link
Contributor

Choose a reason for hiding this comment

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

You are partially right. I'd add something like "random_element_for_test" into the field, so that it would automatically print it. But it is actually better to have random elements than static, because if there is a bug there is a chance that it triggers. And then you can stress the test to find it (that was the case with construct_addition_chains)

Copy link
Contributor

@kevaundray kevaundray left a comment

Choose a reason for hiding this comment

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

Looks good to me

@kevaundray kevaundray changed the title Ecdsa Malleability Bug Fix fix: Ecdsa Malleability Bug Jun 12, 2023
@kevaundray
Copy link
Contributor

Since this modifies the stdlib by adding extra constraints, I'm marking this a breaking change

@kevaundray kevaundray changed the title fix: Ecdsa Malleability Bug fix!: Ecdsa Malleability Bug Jun 12, 2023
@Rumata888 Rumata888 changed the title fix!: Ecdsa Malleability Bug fix: Ecdsa Malleability Bug Jun 14, 2023
@kevaundray
Copy link
Contributor

Update: It was decided that changes to the stdlib will not be marked as breaking changes as this would include most commits

@suyash67 suyash67 force-pushed the sb/ecdsa-malleability-fix branch from f72d2ca to c189a14 Compare June 26, 2023 13:19
@suyash67
Copy link
Contributor Author

I've made two changes:

  1. The function was actually less or equal.
  2. Once I changed it to be less_than, the logic for constraining s became incorrect. You were checking that s < (Fr::modulus / 2). It is different from 2*s < Fr::modulus, because Fr::modulus is odd. Think mod 3. If you check that something is less than 3/2, then the only option is zero, while 1 should also be a legitimate value.

Nice catch, thanks Kesha!

@suyash67 suyash67 merged commit 5cf856c into master Jun 26, 2023
@suyash67 suyash67 deleted the sb/ecdsa-malleability-fix branch June 26, 2023 13:56
ludamad pushed a commit to AztecProtocol/aztec-packages that referenced this pull request Jul 22, 2023
ludamad pushed a commit to AztecProtocol/aztec-packages that referenced this pull request Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants