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

[Bug] Signatures from sign_hash return Parity, but if converting to Vec and restoring, it returns NonEip155 #705

Open
gusinacio opened this issue Aug 5, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@gusinacio
Copy link

gusinacio commented Aug 5, 2024

Component

primitives

What version of Alloy are you on?

0.2.1

Operating System

None

Describe the bug

The following code doesn't work, it looks like it's returned Parity() every signature, but if we convert to bytes and restore elsewhere, it returns NonEip155.

The following test fails:

    #[test]
    fn test_parity() {
        let signer = PrivateKeySigner::random();
        // The message to sign.
        let message = b"hello";

        // Sign the message asynchronously with the signer.
        let signature = signer.sign_message_sync(message).unwrap();

        let value = signature.as_bytes().to_vec();
        let recovered_signature: Signature = value.as_slice().try_into().unwrap();
        assert_eq!(signature, recovered_signature);
    }

The error is the following:

assertion `left == right` failed
  left: Signature { inner: ecdsa::Signature<Secp256k1>(33828FC1FE49FB3177AA80459AE130BD8147158000FA701D7E33BC33B8EDDF25586239AF447392DD8AC6DACBE5E3F91B72AB5593BE3ED6DC702489CA4E2BE1B6), v: Parity(false), r: 23298637575944829774526378124809190621347391490807772432023406486951885135653, s: 39977079812416937747731152449858872909720600328418637835900651766296202699190 }
 right: Signature { inner: ecdsa::Signature<Secp256k1>(33828FC1FE49FB3177AA80459AE130BD8147158000FA701D7E33BC33B8EDDF25586239AF447392DD8AC6DACBE5E3F91B72AB5593BE3ED6DC702489CA4E2BE1B6), v: NonEip155(false), r: 23298637575944829774526378124809190621347391490807772432023406486951885135653, s: 39977079812416937747731152449858872909720600328418637835900651766296202699190 }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
test tap::context::rav::test::test_parity ... FAILED

failures:

failures:
    test::test_parity
@gusinacio gusinacio added the bug Something isn't working label Aug 5, 2024
@mattsse
Copy link
Member

mattsse commented Aug 5, 2024

hmm these are indeed not symmetrical

#[cfg(feature = "k256")]
impl From<k256::ecdsa::RecoveryId> for Parity {
fn from(value: k256::ecdsa::RecoveryId) -> Self {
Self::Parity(value.is_y_odd())
}
}

impl TryFrom<u64> for Parity {
type Error = SignatureError;
fn try_from(value: u64) -> Result<Self, Self::Error> {
match value {
0 | 1 => Ok(Self::Parity(value != 0)),
27 | 28 => Ok(Self::NonEip155((value - 27) != 0)),
value @ 35..=u64::MAX => Ok(Self::Eip155(value)),
_ => Err(SignatureError::InvalidParity(value)),
}
}
}

I assume this is the bug here:

/// Return the y-parity byte as 27 or 28,
/// in the case of a non-EIP155 signature.
pub const fn y_parity_byte_non_eip155(&self) -> Option<u8> {
match self {
Self::NonEip155(v) | Self::Parity(v) => Some(*v as u8 + 27),
_ => None,
}
}

invoked by

sig[64] = self.v.y_parity_byte_non_eip155().unwrap_or(self.v.y_parity_byte());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants