Skip to content

Commit e953dae

Browse files
committed
Move zbase32 implementation to base32 file
1 parent c4485eb commit e953dae

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 {
@@ -46,7 +57,10 @@ impl Alphabet {
4657
return String::from_utf8(ret).expect("Invalid UTF-8");
4758
}
4859
ret
49-
}
60+
},
61+
Self::ZBase32 => {
62+
Self::encode_data(data, ZBASE_ALPHABET)
63+
},
5064
};
5165
ret.truncate(output_length);
5266

@@ -71,6 +85,9 @@ impl Alphabet {
7185
});
7286
}
7387
(&data[..unpadded_data_length], RFC4648_INV_ALPHABET)
88+
},
89+
Self::ZBase32 => {
90+
(data, ZBASE_INV_ALPHABET)
7491
}
7592
};
7693
// If the string has more characters than are required to alphabet_encode the number of bytes
@@ -151,6 +168,44 @@ impl Alphabet {
151168
mod tests {
152169
use super::*;
153170

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