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

fastcrypto: impl bech32 encoding #712

Merged
merged 2 commits into from
Dec 19, 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
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions fastcrypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ lazy_static = "1.4.0"
fastcrypto-derive = { path = "../fastcrypto-derive", version = "0.1.3" }
serde_json = "1.0.93"
num-bigint = "0.4.4"
bech32 = "0.9.1"

[[bench]]
name = "crypto"
Expand Down
34 changes: 34 additions & 0 deletions fastcrypto/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//! ```

use base64ct::Encoding as _;
use bech32::{FromBase32, Variant};
use eyre::{eyre, Result};
use schemars::JsonSchema;
use serde;
Expand Down Expand Up @@ -266,3 +267,36 @@
Ok(array)
}
}

/// Bech32 encoding
pub struct Bech32(String);

impl Bech32 {
/// Decodes the Bech32 string to bytes, validating the given human readable part (hrp). See spec: https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
/// # Example:
/// ```
/// use fastcrypto::encoding::Bech32;
/// let bytes = Bech32::decode("split1qqqqsk5gh5","split").unwrap();
/// assert_eq!(bytes, vec![0, 0]);
/// ```
pub fn decode(s: &str, hrp: &str) -> Result<Vec<u8>, eyre::Report> {
benr-ml marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

encode receives hrp as an input, why not return it here as an output and let the caller verify consistency?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let (parsed, data, variant) = bech32::decode(s).map_err(|e| eyre::eyre!(e))?;
if parsed != hrp || variant != Variant::Bech32 {
Err(eyre!("invalid hrp or variant"))

Check warning on line 285 in fastcrypto/src/encoding.rs

View check run for this annotation

Codecov / codecov/patch

fastcrypto/src/encoding.rs#L285

Added line #L285 was not covered by tests
} else {
Vec::<u8>::from_base32(&data).map_err(|e| eyre::eyre!(e))
}
}

/// Encodes bytes into a Bech32 encoded string, with the given human readable part (hrp). See spec: https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
/// # Example:
/// ```
/// use fastcrypto::encoding::Bech32;
/// let str = Bech32::encode(vec![0, 0],"split").unwrap();
/// assert_eq!(str, "split1qqqqsk5gh5".to_string());
/// ```
pub fn encode<T: AsRef<[u8]>>(data: T, hrp: &str) -> Result<String> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it uses AsRef, data can be a ref.
(in general we should reduce dependency on AsRef for fastcrypt types, and move to non ref byte arrays / serialized objects, but we can do that later)

use bech32::ToBase32;
bech32::encode(hrp, data.to_base32(), Variant::Bech32).map_err(|e| eyre::eyre!(e))
}
}
55 changes: 54 additions & 1 deletion fastcrypto/src/tests/encoding_tests.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use crate::encoding::{encode_with_format, Base58, Base64, Encoding, Hex};
use crate::encoding::{encode_with_format, Base58, Base64, Bech32, Encoding, Hex};
use proptest::{arbitrary::Arbitrary, prop_assert_eq};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;

macro_rules! check_valid_address_roundtrip {
($($test_name:ident, $addr:literal);* $(;)?) => {
$(
#[test]
#[cfg(feature = "alloc")]
fn $test_name() {
let decoded = Bech32::decode($addr, "bc").unwrap();
let encoded = Bech32::encode(decoded, "bc").unwrap();
assert_eq!(encoded, $addr);
}
)*
}
}

#[test]
fn test_hex_roundtrip() {
let bytes = &[1, 10, 100];
Expand Down Expand Up @@ -143,6 +157,38 @@ fn test_base58_err() {
assert!(Base58::try_from("invalid\0".to_string()).is_err());
}

#[test]
fn test_bech32() {
// Test vectors from https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#test-vectors
let bytes = [0; 32];
let encoded = Bech32::encode(bytes, "suiprivkey").unwrap();
let decoded = Bech32::decode(&encoded, "suiprivkey").unwrap();
assert_eq!(bytes, decoded.as_slice());

assert!(Bech32::decode("A12UEL5L", "a").is_ok());
assert!(Bech32::decode("a12uel5l", "a").is_ok());
assert!(Bech32::decode("an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs", "an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio").is_ok());
assert!(Bech32::decode("abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw", "abcdef").is_ok());
assert!(Bech32::decode("11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j", "1").is_ok());
assert!(Bech32::decode(
"split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w",
"split"
)
.is_ok());
assert!(Bech32::decode("?1ezyfcl", "?").is_ok());
assert!(Bech32::decode("an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx", "an84characterslonghumanreadablepartthatcontainsthenumber").is_err());
assert!(Bech32::decode("pzry9x0s0muk", "").is_err());

check_valid_address_roundtrip! {
bip_173_valid_address_roundtrip_0, "BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4";
bip_173_valid_address_roundtrip_1, "tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7";
bip_173_valid_address_roundtrip_2, "bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx";
bip_173_valid_address_roundtrip_3, "BC1SW50QA3JX3S";
bip_173_valid_address_roundtrip_4, "bc1zw508d6qejxtdg4y5r3zarvaryvg6kdaj";
bip_173_valid_address_roundtrip_5, "tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy";
}
}

proptest::proptest! {
#[test]
fn roundtrip_hex(bytes in <[u8; 20]>::arbitrary()) {
Expand All @@ -164,4 +210,11 @@ proptest::proptest! {
let decoded = Base58::decode(&encoded).unwrap();
prop_assert_eq!(bytes, decoded.as_slice());
}

#[test]
fn roundtrip_bech32(bytes in <[u8; 20]>::arbitrary()) {
let encoded = Bech32::encode(bytes, "suiprivkey").unwrap();
let decoded = Bech32::decode(&encoded, "suiprivkey").unwrap();
prop_assert_eq!(bytes, decoded.as_slice());
}
}
Loading