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

refactor: remove Signature generic #719

Merged
merged 4 commits into from
Sep 6, 2024
Merged

refactor: remove Signature generic #719

merged 4 commits into from
Sep 6, 2024

Conversation

klkvr
Copy link
Member

@klkvr klkvr commented Sep 2, 2024

Motivation

Currently it is impossible to construct an invalid signature, because k256 feature requires it to contain a valid k256::Signature. This makes it impossible to decode and store invalid signatures.

For example, we require this in reth to keep deposit transaction signatures: https://github.com/paradigmxyz/reth/blob/42dc5eea1685385b835edd03029640609f4c4ab4/crates/primitives/src/transaction/signature.rs#L200

Solution

Remove generic from Signature. Instead, we now convert signature into k256::Signature during recovery.

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

I like this a lot, for the exact reasons you described

Instead, we now convert signature into k256::Signature during recovery.

this makes sense, we then can do different recovery even

crates/primitives/src/signature/sig.rs Show resolved Hide resolved
crates/primitives/src/lib.rs Outdated Show resolved Hide resolved
crates/primitives/src/signature/sig.rs Outdated Show resolved Hide resolved
type Error = k256::ecdsa::Error;

fn try_from(value: Signature) -> Result<Self, Self::Error> {
Self::from_scalars(value.r.to_be_bytes(), value.s.to_be_bytes())
Copy link
Member

Choose a reason for hiding this comment

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

recovery becomes more expensive because this is not a free conversion

Copy link
Member

Choose a reason for hiding this comment

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

worth looking into easier ways to construct the scalars without going through big endian bytes

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think k256 Signature allows instantiation without going through bytes

Given that signature fields are not public, should we consider adding this to it:

#[cfg(feature = "k256")]
signature: Option<k256::ecdsa::Signature>,

which will be set to None if invalid signature is instantiated, and recovery will just fail if its None

Copy link
Member

Choose a reason for hiding this comment

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

I don't think k256 Signature allows instantiation without going through bytes

it doesn't

I think we should be okay with this overhead. this only affects the alloy sig -> k256 signature conversion and most common is decoding this from rlp

and getting rid of the additional footprint is more impactful, at least from reth pov

crates/primitives/src/signature/sig.rs Show resolved Hide resolved
crates/primitives/src/signature/sig.rs Show resolved Hide resolved
crates/primitives/src/signature/sig.rs Outdated Show resolved Hide resolved
crates/primitives/src/signature/sig.rs Show resolved Hide resolved
Copy link
Member

@DaniPopes DaniPopes left a comment

Choose a reason for hiding this comment

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

lgtm

@DaniPopes DaniPopes merged commit 1b98d56 into main Sep 6, 2024
30 checks passed
@DaniPopes DaniPopes deleted the klkvr/signature branch September 6, 2024 13:35
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