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

Simplify handling of invalid signatures #4372

Merged
merged 11 commits into from
Sep 21, 2021

Conversation

benjaminion
Copy link
Contributor

The previous implementation used a flag to track whether a signature was valid or invalid. An example of an invalid signature was one that encodes a point not in the G2 group.

This led to oddities like having both valid and invalid infinity signatures. It also led to ambiguity about whether we check the G2 group membership at deserialisation or on usage of a signature. In batch verification we ended up doing both (but not explicitly checking the signature's validity in the calling code). This was all a bit messy and hard to maintain.

In this PR we explicitly delegate the group membership check to the Blst library, and we do not perform the group membership check at deserialisation of the signature. This ought to slightly speed things up (due to no longer duplicating the check in batch verification) and should make things more maintainable.

The risk is that if Blst behaviour changes, or we swap in a different library in future, we could inadvertantly end up without the group membership check. I have added some tests to guard against this.

As a bonus, this closes #4111.

Copy link
Contributor

@ajsutton ajsutton left a comment

Choose a reason for hiding this comment

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

This generally looks good, I think it would be worth cleaning up how we're handling exceptions though so that we consistently throw a well-typed exception (probably rename DeserializeException to BlsException). it would still extend from RuntimeException so be unchecked but makes handling it upstream so much easier.

return new BlstSignature(sum.to_affine());
} catch (RuntimeException e) {
// Blst performs a G2 group membership test on each signature. We end up here if it fails.
throw new RuntimeException("Failed to aggregate signatures: " + e.getMessage());
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm really not a fan of throwing a generic RuntimeException here, it's just too generic and you wind up catching RuntimeException all over the place then, potentially sweeping up exceptions you didn't intend to catch. I'd probably rename DeserializeException to BlsException so we can throw it here. And worth keeping the full details of the original exception too with new BlsException("Failed to aggregate signatures", e)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good plan - done. I've reworked exceptions generally across the BLS classes. It's not perfect, but simpler than it was.

Copy link
Contributor

@ajsutton ajsutton left a comment

Choose a reason for hiding this comment

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

LGTM.

bls/src/main/java/tech/pegasys/teku/bls/BLS.java Outdated Show resolved Hide resolved
bls/src/main/java/tech/pegasys/teku/bls/BLS.java Outdated Show resolved Hide resolved
@benjaminion benjaminion merged commit 22aa754 into Consensys:master Sep 21, 2021
@benjaminion benjaminion deleted the invalid-signatures branch September 21, 2021 13:33
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.

Public key aggregation ambiguous infinite points handling
2 participants