From b86f158471d74a32c63eb3538297705d4bf8e21e Mon Sep 17 00:00:00 2001 From: Emma Turner Date: Wed, 20 Dec 2023 11:20:17 +0000 Subject: [PATCH] crypto: Signature of all zero bytes should be Unknown Currently defaults to Ed25519, which does not match octez reference --- CHANGELOG.md | 1 + crypto/src/hash.rs | 26 ++++---------------------- crypto/src/signature.rs | 11 +++++++++++ 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6334e7b8a3..471edd3cc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ parameterized by the lifetime of the input byte slice. - Hash input before signing with `SecretKeyEd25519`, to match octez impl. - Fix `BlsSignature` base58 check encoding/decoding. - Fix `SecretKeyEd25519` base58 check encoding/decoding. +- Fix all zeros signature encoding: should be `Unknown` rather than defaulting to `Ed25519`. ### Security diff --git a/crypto/src/hash.rs b/crypto/src/hash.rs index 7adf2c9ab6..95d52f291c 100644 --- a/crypto/src/hash.rs +++ b/crypto/src/hash.rs @@ -466,12 +466,8 @@ impl HashType { Err(FromBytesError::InvalidSize) } else { let mut hash = Vec::with_capacity(self.base58check_prefix().len() + data.len()); - if matches!(self, Self::UnknownSignature) && data == [0; Self::Ed25519Signature.size()] - { - hash.extend(Self::Ed25519Signature.base58check_prefix()); - } else { - hash.extend(self.base58check_prefix()); - } + + hash.extend(self.base58check_prefix()); hash.extend(data); Ok(hash.to_base58check()) @@ -481,22 +477,8 @@ impl HashType { /// Convert string representation of the hash to bytes form. pub fn b58check_to_hash(&self, data: &str) -> Result { let mut hash = data.from_base58check()?; - if let HashType::UnknownSignature = self { - // zero signature is represented as Ed25519 signature - if hash.len() - == HashType::Ed25519Signature.size() - + HashType::Ed25519Signature.base58check_prefix().len() - { - let (prefix, hash) = - hash.split_at(HashType::Ed25519Signature.base58check_prefix().len()); - if prefix == HashType::Ed25519Signature.base58check_prefix() - && hash == [0; HashType::Ed25519Signature.size()] - { - return Ok(hash.to_vec()); - } - } - } else if !hash.starts_with(self.base58check_prefix()) { - println!("expected: {:?}, found: {hash:?}", self.base58check_prefix()); + + if !hash.starts_with(self.base58check_prefix()) { return Err(FromBase58CheckError::IncorrectBase58Prefix); } diff --git a/crypto/src/signature.rs b/crypto/src/signature.rs index 915b8bcfee..bcaa985680 100644 --- a/crypto/src/signature.rs +++ b/crypto/src/signature.rs @@ -124,3 +124,14 @@ impl ::std::fmt::Display for Signature { write!(f, "{}", self.to_base58_check()) } } + +#[cfg(test)] +mod test { + #[test] + fn test() { + assert_eq!( + &super::Signature::try_from([0; 64].to_vec()).unwrap().to_base58_check(), + "sigMzJ4GVAvXEd2RjsKGfG2H9QvqTSKCZsuB2KiHbZRGFz72XgF6KaKADznh674fQgBatxw3xdHqTtMHUZAGRprxy64wg1aq" + ); + } +}