Skip to content

Commit

Permalink
crypto: Signature of all zero bytes should be Unknown
Browse files Browse the repository at this point in the history
Currently defaults to Ed25519, which does not match octez reference
  • Loading branch information
emturner committed Jan 2, 2024
1 parent 2696597 commit 65a2946
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 @@ -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 decoding of sig: 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 65a2946

Please sign in to comment.