Skip to content

Commit

Permalink
Merge pull request #61 from trilitech/emturner@sig-of-zeroes
Browse files Browse the repository at this point in the history
crypto: Signature of all zero bytes should be Unknown
  • Loading branch information
emturner authored Jan 3, 2024
2 parents 59ee9e5 + b86f158 commit 18507cf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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

Expand Down
26 changes: 4 additions & 22 deletions crypto/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -481,22 +477,8 @@ impl HashType {
/// Convert string representation of the hash to bytes form.
pub fn b58check_to_hash(&self, data: &str) -> Result<Hash, FromBase58CheckError> {
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);
}

Expand Down
11 changes: 11 additions & 0 deletions crypto/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}
}

0 comments on commit 18507cf

Please sign in to comment.