Skip to content

Commit

Permalink
crypto: fix base58 encoding for BlsSignature
Browse files Browse the repository at this point in the history
The current base58 dep has a 128 byte limit, which is not long enough for bls sigs
  • Loading branch information
emturner committed Dec 18, 2023
1 parent d15ccf0 commit 4e39b21
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ parameterized by the lifetime of the input byte slice.
- Fix prefix used in `SeedEd25519` encoding.
- Add explicit prefix check during base58check decoding.
- Hash input before signing with `SecretKeyEd25519`, to match octez impl.
- Fix `BlsSignature` base58 check encoding/decoding.

### Security

Expand Down
32 changes: 25 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repository = "https://github.com/trilitech/tezedge.git"

[dependencies]
anyhow = "1.0"
base58 = "0.1.0"
bs58 = { version = "0.5", default-features = false, features = ["alloc"] }
thiserror = "1.0"
hex = "0.4"
libsecp256k1 = { version = "0.7", default-features = false, features = ["static-context"] }
Expand Down
28 changes: 2 additions & 26 deletions crypto/src/base58.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-FileCopyrightText: 2023 TriliTech <contact@trili.tech>
// SPDX-License-Identifier: MIT

use base58::{FromBase58, ToBase58};
use cryptoxide::hashing::sha256;
use thiserror::Error;

Expand All @@ -18,9 +17,6 @@ pub enum FromBase58CheckError {
/// The input is missing checksum.
#[error("missing checksum")]
MissingChecksum,
/// Data is too long
#[error("data too long")]
DataTooLong,
#[error("mismatched data lenght: expected {expected}, actual {actual}")]
MismatchedLength { expected: usize, actual: usize },
/// Prefix does not match expected.
Expand Down Expand Up @@ -60,25 +56,19 @@ pub trait FromBase58Check {

impl ToBase58Check for [u8] {
fn to_base58check(&self) -> Result<String, ToBase58CheckError> {
if self.len() > 128 {
return Err(ToBase58CheckError::DataTooLong);
}
// 4 bytes checksum
let mut payload = Vec::with_capacity(self.len() + 4);
payload.extend(self);
let checksum = double_sha256(self);
payload.extend(&checksum[..4]);

Ok(payload.to_base58())
Ok(bs58::encode(payload).into_string())
}
}

impl FromBase58Check for str {
fn from_base58check(&self) -> Result<Vec<u8>, FromBase58CheckError> {
if self.len() > 128 {
return Err(FromBase58CheckError::DataTooLong);
}
match self.from_base58() {
match bs58::decode(self).into_vec() {
Ok(payload) => {
if payload.len() >= Self::CHECKSUM_BYTE_SIZE {
let data_len = payload.len() - Self::CHECKSUM_BYTE_SIZE;
Expand Down Expand Up @@ -123,18 +113,4 @@ mod tests {

Ok(())
}

#[test]
fn test_encode_fail() {
let data = [0; 129].to_vec();
let res = data.to_base58check();
assert!(matches!(res, Err(ToBase58CheckError::DataTooLong)));
}

#[test]
fn test_decode_fail() {
let encoded = "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
let res = encoded.from_base58check();
assert!(matches!(res, Err(FromBase58CheckError::DataTooLong)));
}
}
6 changes: 6 additions & 0 deletions crypto/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,12 @@ mod tests {
["BLsk1WTwJFkLU2P57itDq1cgEUqJK7Fwygvtj49vT4HeLfNBXRgpDA"]
);

test!(
sig_bls,
BlsSignature,
["BLsigAmLKnuw12tethjMmotFPaQ6u4XCKrVk6c15dkRXKkjDDjHywbhS3nd4rBT31yrCvvQrS2HntWhDRu7sX8Vvek53zBUwQHqfcHRiVKVj1ehq8CBYs1Z7XW2rkL2XkVNHua4cnvxY7F"]
);

test!(ed25519_sig, Ed25519Signature, ["edsigtXomBKi5CTRf5cjATJWSyaRvhfYNHqSUGrn4SdbYRcGwQrUGjzEfQDTuqHhuA8b2d8NarZjz8TRf65WkpQmo423BtomS8Q"]);

test!(generic_sig, Signature, ["sigNCaj9CnmD94eZH9C7aPPqBbVCJF72fYmCFAXqEbWfqE633WNFWYQJFnDUFgRUQXR8fQ5tKSfJeTe6UAi75eTzzQf7AEc1"]);
Expand Down

0 comments on commit 4e39b21

Please sign in to comment.