Skip to content

Commit 26407fa

Browse files
committed
Move zbase32 implementation to base32 file
1 parent 930efed commit 26407fa

File tree

5 files changed

+67
-159
lines changed

5 files changed

+67
-159
lines changed

fuzz/src/zbase32.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@
77
// You may not use this file except in accordance with one or both of these
88
// licenses.
99

10-
use lightning::util::zbase32;
10+
use lightning::util::base32;
1111

1212
use crate::utils::test_logger;
1313

1414
#[inline]
1515
pub fn do_test(data: &[u8]) {
16-
let res = zbase32::encode(data);
17-
assert_eq!(&zbase32::decode(&res).unwrap()[..], data);
16+
let res = base32::Alphabet::ZBase32.encode(data);
17+
assert_eq!(&base32::Alphabet::ZBase32.decode(&res).unwrap()[..], data);
1818

1919
if let Ok(s) = std::str::from_utf8(data) {
20-
if let Ok(decoded) = zbase32::decode(s) {
21-
assert_eq!(&zbase32::encode(&decoded), &s.to_ascii_lowercase());
20+
let res = base32::Alphabet::ZBase32.decode(s);
21+
if let Ok(decoded) = res {
22+
assert_eq!(&base32::Alphabet::ZBase32.encode(&decoded), &s.to_ascii_lowercase());
2223
}
2324
}
2425
}

lightning/src/util/base32.rs

+57-2
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,31 @@ use crate::prelude::*;
1212
/// RFC4648 encoding table
1313
const RFC4648_ALPHABET: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
1414

15+
/// Zbase encoding alphabet
16+
const ZBASE_ALPHABET: &'static [u8] = b"ybndrfg8ejkmcpqxot1uwisza345h769";
17+
1518
/// RFC4648 decoding table
1619
const RFC4648_INV_ALPHABET: [i8; 43] = [
1720
-1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,
1821
9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
1922
];
2023

24+
/// Zbase decoding table
25+
const ZBASE_INV_ALPHABET: [i8; 43] = [
26+
-1, 18, -1, 25, 26, 27, 30, 29, 7, 31, -1, -1, -1, -1, -1, -1, -1, 24, 1, 12, 3, 8, 5, 6, 28,
27+
21, 9, 10, -1, 11, 2, 16, 13, 14, 4, 22, 17, 19, -1, 20, 15, 0, 23,
28+
];
29+
2130
/// Alphabet used for encoding and decoding.
2231
#[derive(Copy, Clone)]
2332
pub enum Alphabet {
2433
/// RFC4648 encoding.
2534
RFC4648 {
2635
/// Whether to use padding.
2736
padding: bool
28-
}
37+
},
38+
/// Zbase32 encoding.
39+
ZBase32
2940
}
3041

3142
impl Alphabet {
@@ -47,7 +58,10 @@ impl Alphabet {
4758
return String::from_utf8(ret).expect("Invalid UTF-8");
4859
}
4960
ret
50-
}
61+
},
62+
Self::ZBase32 => {
63+
Self::encode_data(data, ZBASE_ALPHABET)
64+
},
5165
};
5266
ret.truncate(output_length);
5367

@@ -72,6 +86,9 @@ impl Alphabet {
7286
});
7387
}
7488
(&data[..unpadded_data_length], RFC4648_INV_ALPHABET)
89+
},
90+
Self::ZBase32 => {
91+
(data, ZBASE_INV_ALPHABET)
7592
}
7693
};
7794
// If the string has more characters than are required to alphabet_encode the number of bytes
@@ -148,6 +165,44 @@ impl Alphabet {
148165
mod tests {
149166
use super::*;
150167

168+
const ZBASE32_TEST_DATA: &[(&str, &[u8])] = &[
169+
("", &[]),
170+
("yy", &[0x00]),
171+
("oy", &[0x80]),
172+
("tqrey", &[0x8b, 0x88, 0x80]),
173+
("6n9hq", &[0xf0, 0xbf, 0xc7]),
174+
("4t7ye", &[0xd4, 0x7a, 0x04]),
175+
("6im5sdy", &[0xf5, 0x57, 0xbb, 0x0c]),
176+
("ybndrfg8ejkmcpqxot1uwisza345h769", &[0x00, 0x44, 0x32, 0x14, 0xc7, 0x42, 0x54, 0xb6,
177+
0x35, 0xcf, 0x84, 0x65, 0x3a, 0x56, 0xd7, 0xc6,
178+
0x75, 0xbe, 0x77, 0xdf])
179+
];
180+
181+
#[test]
182+
fn test_zbase32_encode() {
183+
for &(zbase32, data) in ZBASE32_TEST_DATA {
184+
assert_eq!(Alphabet::ZBase32.encode(data), zbase32);
185+
}
186+
}
187+
188+
#[test]
189+
fn test_zbase32_decode() {
190+
for &(zbase32, data) in ZBASE32_TEST_DATA {
191+
assert_eq!(Alphabet::ZBase32.decode(zbase32).unwrap(), data);
192+
}
193+
}
194+
195+
#[test]
196+
fn test_decode_wrong() {
197+
const WRONG_DATA: &[&str] = &["00", "l1", "?", "="];
198+
for &data in WRONG_DATA {
199+
match Alphabet::ZBase32.decode(data) {
200+
Ok(_) => assert!(false, "Data shouldn't be decodable"),
201+
Err(_) => assert!(true),
202+
}
203+
}
204+
}
205+
151206
const RFC4648_NON_PADDED_TEST_VECTORS: &[(&[u8], &[u8])] = &[
152207
(&[0xF8, 0x3E, 0x7F, 0x83, 0xE7], b"7A7H7A7H"),
153208
(&[0x77, 0xC1, 0xF7, 0x7C, 0x1F], b"O7A7O7A7"),

lightning/src/util/message_signing.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
//! <https://api.lightning.community/#signmessage>
2222
2323
use crate::prelude::*;
24-
use crate::util::zbase32;
24+
use crate::util::base32;
2525
use bitcoin::hashes::{sha256d, Hash};
2626
use bitcoin::secp256k1::ecdsa::{RecoverableSignature, RecoveryId};
2727
use bitcoin::secp256k1::{Error, Message, PublicKey, Secp256k1, SecretKey};
@@ -58,15 +58,15 @@ pub fn sign(msg: &[u8], sk: &SecretKey) -> Result<String, Error> {
5858
let msg_hash = sha256d::Hash::hash(&[LN_MESSAGE_PREFIX, msg].concat());
5959

6060
let sig = secp_ctx.sign_ecdsa_recoverable(&Message::from_slice(&msg_hash)?, sk);
61-
Ok(zbase32::encode(&sigrec_encode(sig)))
61+
Ok(base32::Alphabet::ZBase32.encode(&sigrec_encode(sig)))
6262
}
6363

6464
/// Recovers the PublicKey of the signer of the message given the message and the signature.
6565
pub fn recover_pk(msg: &[u8], sig: &str) -> Result<PublicKey, Error> {
6666
let secp_ctx = Secp256k1::verification_only();
6767
let msg_hash = sha256d::Hash::hash(&[LN_MESSAGE_PREFIX, msg].concat());
6868

69-
match zbase32::decode(&sig) {
69+
match base32::Alphabet::ZBase32.decode(&sig) {
7070
Ok(sig_rec) => {
7171
match sigrec_decode(sig_rec) {
7272
Ok(sig) => secp_ctx.recover_ecdsa(&Message::from_slice(&msg_hash)?, &sig),
@@ -143,4 +143,4 @@ mod test {
143143
assert!(verify(c[1].as_bytes(), c[2], &PublicKey::from_str(c[3]).unwrap()))
144144
}
145145
}
146-
}
146+
}

lightning/src/util/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ pub(crate) mod base32;
3030
pub(crate) mod atomic_counter;
3131
pub(crate) mod byte_utils;
3232
pub(crate) mod chacha20;
33-
#[cfg(fuzzing)]
34-
pub mod zbase32;
35-
#[cfg(not(fuzzing))]
36-
pub(crate) mod zbase32;
3733
#[cfg(not(fuzzing))]
3834
pub(crate) mod poly1305;
3935
pub(crate) mod chacha20poly1305rfc;

lightning/src/util/zbase32.rs

-144
This file was deleted.

0 commit comments

Comments
 (0)