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

crypto: fix base58 encoding for BlsSignature #56

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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
35 changes: 6 additions & 29 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,10 +17,7 @@ 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}")]
#[error("mismatched data length: expected {expected}, actual {actual}")]
MismatchedLength { expected: usize, actual: usize },
/// Prefix does not match expected.
#[error("incorrect base58 prefix for hash type")]
Expand Down Expand Up @@ -60,26 +56,20 @@ 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() {
Ok(payload) => {
match bs58::decode(self).into_vec() {
Ok(mut payload) => {
if payload.len() >= Self::CHECKSUM_BYTE_SIZE {
let data_len = payload.len() - Self::CHECKSUM_BYTE_SIZE;
let data = &payload[..data_len];
Expand All @@ -89,7 +79,8 @@ impl FromBase58Check for str {
let checksum_expected = &checksum_expected[..4];

if checksum_expected == checksum_provided {
Ok(data.to_vec())
payload.truncate(data_len);
Ok(payload)
} else {
Err(FromBase58CheckError::InvalidChecksum)
}
Expand Down Expand Up @@ -123,18 +114,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