pq_address 0.2.0

A Rust library for encoding and decoding post‑quantum public keys into human‑friendly Bech32m addresses.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! A Bech32m address encoder/decoder for PQC public keys.
//!
//! This crate supports:
//! - Mainnet vs Testnet via HRP.
//! - Disjoint byte ranges for version and pubkey type.
//! - Support for multiple PQC public keys.
//! - Full encode/decode round‑trip with Bech32m checksum per BIP‑350.

use bech32::{Bech32m, Hrp, decode, encode};
use hex::encode as hex_encode;
use sha2::{Digest as ShaDigest, Sha256};
use std::convert::TryFrom;
use std::fmt;
use thiserror::Error;

/// A Bech32 string is at most 90 characters long [BIP-173]
/// PQ address length is 64 characters.
const ADDRESS_LENGTH: usize = 64;

/// Which network you’re on.
///
/// This determines the human‑readable part (HRP) of the Bech32m address.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Network {
    /// Mainnet addresses use `yp…`
    Mainnet,
    /// Testnet addresses use `rh…`
    Testnet,
}

impl Network {
    /// Returns the Bech32m HRP for this network.
    ///
    /// # Panics
    ///
    /// This will panic if the hard-coded HRP literal fails to parse.
    /// In practice `"yp"` and `"rh"` are spec-defined and known to be valid given `try_from`,
    /// so this only ever fires if there’s a bug in this code or in `Hrp::parse`.
    #[must_use]
    pub fn hrp(self) -> Hrp {
        match self {
            Network::Mainnet => Hrp::parse("yp").expect("Mainnet HRP is valid"),
            Network::Testnet => Hrp::parse("rh").expect("Testnet HRP is valid"),
        }
    }
}

/// Attempts to parse a network from a string.
impl TryFrom<&str> for Network {
    type Error = AddressDecodeError;

    fn try_from(s: &str) -> Result<Self, Self::Error> {
        match s {
            "yp" => Ok(Network::Mainnet),
            "rh" => Ok(Network::Testnet),
            other => Err(AddressDecodeError::UnknownHrp(other.to_string())),
        }
    }
}

/// Address format version.
///
/// Codes are in the range `0x00..=0x3F` (64 total slots):
/// - `0x00` = V1
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Version {
    /// V1
    V1,
}

impl Version {
    /// Byte code to embed in the payload.
    ///
    /// Reserved range: 0x00..=0x3F (up to 64 versions).
    #[must_use]
    pub fn code(self) -> u8 {
        match self {
            Version::V1 => 0x00,
        }
    }

    /// Reverse lookup from byte code to enum.
    #[must_use]
    pub fn from_code(code: u8) -> Option<Version> {
        match code {
            0x00 => Some(Version::V1),
            _ => None,
        }
    }
}

/// Supported public-key types.
///
/// Codes are in the range `0x40..=0xFF` (192 total slots),
/// giving us plenty of room for future PQC schemes:
/// - `0x40` = ML-DSA 44
/// - `0x41` = SLH-DSA SHA2 128 S
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum PubKeyType {
    /// ML-DSA 44 public key.
    MlDsa44,
    /// SLH-DSA-SHA2 128 s public key.
    SlhDsaSha2S128,
}

impl PubKeyType {
    /// Byte code to embed in the payload.
    ///
    /// Reserved range: 0x40..=0xFF (up to 192 public key types).
    #[must_use]
    pub fn code(self) -> u8 {
        match self {
            PubKeyType::MlDsa44 => 0x40,
            PubKeyType::SlhDsaSha2S128 => 0x41,
        }
    }

    /// Reverse lookup from byte code to enum.
    #[must_use]
    pub fn from_code(code: u8) -> Option<PubKeyType> {
        match code {
            0x40 => Some(PubKeyType::MlDsa44),
            0x41 => Some(PubKeyType::SlhDsaSha2S128),
            _ => None,
        }
    }

    /// Returns the public‐key length in bytes for this algorithm.
    #[must_use]
    pub fn pubkey_length(self) -> usize {
        match self {
            PubKeyType::MlDsa44 => 1312,
            PubKeyType::SlhDsaSha2S128 => 32,
        }
    }
}

/// The default hash function for `pq_address`: SHA-256.
///
/// 256 bit hash function is currently considered secure against Grover's attack.
/// Even if the preimage is recovered, it only reveals a PQ secure public key and thus Shor's is not applicable.
pub struct Hasher;

impl Hasher {
    /// SHA-256 digest length (bytes).
    pub const DIGEST_LENGTH: usize = 32;

    /// Compute SHA-256(data).
    #[must_use]
    pub fn digest(data: &[u8]) -> Vec<u8> {
        let mut h = Sha256::new();
        h.update(data);
        h.finalize().to_vec()
    }
}

/// Parameters needed to encode an address.
pub struct AddressParams<'a> {
    /// Mainnet vs Testnet
    pub network: Network,
    /// Which address version
    pub version: Version,
    /// Which public‑key type
    pub pubkey_type: PubKeyType,
    /// Raw public‑key bytes
    pub pubkey_bytes: &'a [u8],
}

/// Errors that can occur during address encoding.
#[derive(Error, Debug)]
pub enum AddressEncodeError {
    /// Invalid Bech32 structure or checksum
    #[error("Bech32 error: {0}")]
    Bech32(#[from] bech32::EncodeError),

    /// A PQ address is 64 characters long
    #[error("A PQ address is 64 characters long: got {0}")]
    InvalidEncodingLength(usize),

    /// Invalid public key length
    #[error("Invalid public key length: expected {0}, got {1}")]
    InvalidPubKeyLength(usize, usize),
}

/// Encodes a Bech32m address from given params.
///
/// # Errors
/// Returns a `bech32::EncodeError` if the encode step fails.
pub fn encode_address(params: &AddressParams) -> Result<String, AddressEncodeError> {
    // Check pubkey length
    let expected_pubkey_length = params.pubkey_type.pubkey_length();
    if expected_pubkey_length != params.pubkey_bytes.len() {
        return Err(AddressEncodeError::InvalidPubKeyLength(
            expected_pubkey_length,
            params.pubkey_bytes.len(),
        ));
    }

    // Hash the public key
    let digest = Hasher::digest(params.pubkey_bytes);

    // Build the raw payload:
    //  [ version.code(), pubkey_type.code(), digest bytes… ]
    let mut payload = Vec::with_capacity(2 + digest.len());
    payload.push(params.version.code());
    payload.push(params.pubkey_type.code());
    payload.extend(&digest);

    // Bech32m‑encode (adds the 6‑word checksum)
    let hrp = params.network.hrp();
    let encoded = encode::<Bech32m>(hrp, &payload)?;

    if encoded.len() != ADDRESS_LENGTH {
        return Err(AddressEncodeError::InvalidEncodingLength(encoded.len()));
    }

    Ok(encoded)
}

/// The decoded components of a Bech32m address.
#[derive(Debug)]
pub struct DecodedAddress {
    /// Which network (from the HRP)
    pub network: Network,
    /// Which address version
    pub version: Version,
    /// Which public‑key type
    pub pubkey_type: PubKeyType,
    /// The raw public key hash bytes
    pub pubkey_hash: Vec<u8>,
}

impl DecodedAddress {
    /// The raw public key hash bytes
    #[must_use]
    pub fn pubkey_hash_bytes(&self) -> &[u8] {
        &self.pubkey_hash
    }

    /// The hex representation of the public key hash
    #[must_use]
    pub fn pubkey_hash_hex(&self) -> String {
        hex_encode(&self.pubkey_hash)
    }
}

impl fmt::Display for DecodedAddress {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // rebuild the raw payload: [ version, pubkey_type, hash… ]
        let mut payload = Vec::with_capacity(2 + self.pubkey_hash.len());
        payload.push(self.version.code());
        payload.push(self.pubkey_type.code());
        payload.extend(&self.pubkey_hash);

        // hrp() gives the correct prefix for Mainnet/Testnet
        let hrp = self.network.hrp();

        // re-encode as Bech32m; map any encode error to fmt::Error
        let s = encode::<Bech32m>(hrp, &payload).map_err(|_| fmt::Error)?;
        write!(f, "{s}")
    }
}

/// Errors that can occur during address decoding.
#[derive(Error, Debug)]
pub enum AddressDecodeError {
    /// Invalid Bech32 structure or checksum
    #[error("Bech32 error: {0}")]
    Bech32(#[from] bech32::DecodeError),

    /// HRP wasn’t `yp` or `rh`
    #[error("unknown HRP: {0}")]
    UnknownHrp(String),

    /// Payload too short
    #[error("payload too short (need at least version+public key type+hash algorithm)")]
    TooShort,

    /// First byte isn’t a known version code
    #[error("unknown version code: 0x{0:02X}")]
    UnknownVersion(u8),

    /// Second byte isn’t a known public key type
    #[error("unknown public key type code: 0x{0:02X}")]
    UnknownPubKeyType(u8),

    /// Digest length didn’t match the algorithm’s expectation
    #[error("invalid digest length: got {got}, expected {expected}")]
    InvalidHashLength { got: usize, expected: usize },
}

/// Decode a Bech32m address string back into its parts.
///
/// # Errors
/// Returns `AddressDecodeError` on any failure:
/// - Bad checksum or format
/// - Unknown HRP
/// - Invalid payload length
/// - Unrecognized version, public key type or hash algorithm codes
/// - Digest length mismatch
pub fn decode_address(s: &str) -> Result<DecodedAddress, AddressDecodeError> {
    // Decode Bech32
    let (hrp, bytes) = decode(s)?;

    // Map HRP to network enum
    let network = Network::try_from(hrp.as_str())?;

    // Validate minimum length for version + public key type + hash algorithm
    if bytes.len() < 3 {
        return Err(AddressDecodeError::TooShort);
    }

    // Parse version code
    let v = bytes[0];
    let version = Version::from_code(v).ok_or(AddressDecodeError::UnknownVersion(v))?;

    // Parse public key type code
    let p = bytes[1];
    let pubkey_type = PubKeyType::from_code(p).ok_or(AddressDecodeError::UnknownPubKeyType(p))?;

    // Extract and verify digest length
    let pubkey_hash = bytes[2..].to_vec();
    let expected = Hasher::DIGEST_LENGTH;
    if pubkey_hash.len() != expected {
        return Err(AddressDecodeError::InvalidHashLength {
            got: pubkey_hash.len(),
            expected,
        });
    }

    Ok(DecodedAddress {
        network,
        version,
        pubkey_type,
        pubkey_hash,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use base64::{Engine, engine::general_purpose};

    /// Round‑trip test on Mainnet with SHA‑256 + `ML‑DSA 44`
    #[test]
    fn roundtrip_mainnet_mldsa_44() {
        let ml_dsa_44_pub_key_str = "e+ffcul9XkuQCkiCEYX2ES6KMGJ9c7+Z0PFfhnJRckbaHzh4EH9hcEkUoFZ4gK2ta6/xPzgxB1yTT92wPZw8SmrK3DeLMz9mkst0IWkSzJ/TPPHRcSYJekO+CLV8k7uXsGSSoK4fbLqkX8leQFMCzjzRYg06zb3SD7iQwK3O8dP2WWLa9PkBMl1LECCBtTHrxoqyYtKopNbn3wICOOxI1jjTTL46AZnE6Vw2vQdLB/Qg59Pq6su8P3zEqBbsVPwPpT9ZbBNCHE+puWjdYnOfttj6DZ748CRHibQ9WTkH+VpxssIxU62nsYes/fV85nDozwddZggZoLfRsmSlG1Yz6h4m5hMMu9Nku9myTTw4UCiGSxZmad+yIjl7hh6J3wDaLMDA6SXajLSXTk2RwmnsEUlYs+uXS6Wj5wzg+bLQDQVMkU+doOf4vPTArf4uwzJdZ9Ghp8vjHd+rQgKjuo+Hy+HWz4JgvaQXlln+3yF0eY4/v01Bhe8BwVCbFZX8ts2Ay53gJmZEtsnXw3d5xedAMO9LJt4UqwovnmWCuApzAG9jyvG3Wxxe572E725S4vLtgnESzfrsD3wWo/A0oP+wk4oOFjhRDdVwHzwBDiHPhl43b/lt6omQuxK+xF0BJ77X/VhAoCx5zw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ub_key_bytes = general_purpose::STANDARD
            .decode(ml_dsa_44_pub_key_str)
            .expect("valid base64");

        let params = AddressParams {
            network: Network::Mainnet,
            version: Version::V1,
            pubkey_type: PubKeyType::MlDsa44,
            pubkey_bytes: &pub_key_bytes,
        };
        let addr = encode_address(&params).unwrap();
        assert_eq!(
            addr,
            "yp1qpqg39uw700gcctpahe650p9zlzpnjt60cpz09m4kx7ncz8922635hs5cdx7q"
        );
        assert!(addr.starts_with("yp"));
        let decoded = decode_address(&addr).unwrap();
        assert_eq!(decoded.network, params.network);
        assert_eq!(decoded.version, params.version);
        assert_eq!(decoded.pubkey_type, params.pubkey_type);
        assert_eq!(decoded.pubkey_hash, Hasher::digest(&pub_key_bytes));
        assert_eq!(decoded.pubkey_hash_bytes(), Hasher::digest(&pub_key_bytes));
        let mut hasher = Sha256::new();
        hasher.update(pub_key_bytes);
        let hash = hasher.finalize().to_vec();
        assert_eq!(decoded.pubkey_hash_hex(), hex_encode(&hash));
        assert_eq!(decoded.pubkey_hash, hash);
        assert_eq!(decoded.pubkey_hash_bytes(), hash);
        assert_eq!(
            decoded.to_string(),
            "yp1qpqg39uw700gcctpahe650p9zlzpnjt60cpz09m4kx7ncz8922635hs5cdx7q"
        );
    }

    /// Round‑trip test on Testnet with `ML-DSA 44`
    #[test]
    fn roundtrip_testnet_mldsa_44() {
        let ml_dsa_44_pub_key_str = "e+ffcul9XkuQCkiCEYX2ES6KMGJ9c7+Z0PFfhnJRckbaHzh4EH9hcEkUoFZ4gK2ta6/xPzgxB1yTT92wPZw8SmrK3DeLMz9mkst0IWkSzJ/TPPHRcSYJekO+CLV8k7uXsGSSoK4fbLqkX8leQFMCzjzRYg06zb3SD7iQwK3O8dP2WWLa9PkBMl1LECCBtTHrxoqyYtKopNbn3wICOOxI1jjTTL46AZnE6Vw2vQdLB/Qg59Pq6su8P3zEqBbsVPwPpT9ZbBNCHE+puWjdYnOfttj6DZ748CRHibQ9WTkH+VpxssIxU62nsYes/fV85nDozwddZggZoL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YlIq2SDDKeZdlZexeNJOvhr8yntOQzLK6WWVONUgilTFNKX3+NQTmMR1LhA7VSP17+/3NjM0wEaz/JpKRoqMMvrgzl2A/6s019UMoT81hGXNtk9Ed8vxtdeNi1BC+SHWWyazundxXMQ4/gD7PnJXQJduz0QZ8quxRQZZTn+u+t1hKyMQikRKqephJaIQv9NLnKffPncEii9ukfRuLLCy7hPFuAho1Bfgi6rJMN0AxlX9URe6LB6vjLMNdTvWVqCHtBvay4scJg58my00razBF8BhQe7db+UJiv5JwADSJ2fwO/oooReksH3Sv1U4UOx5Y7kK8bbChFg==";

        let pub_key_bytes = general_purpose::STANDARD
            .decode(ml_dsa_44_pub_key_str)
            .expect("valid base64");

        let params = AddressParams {
            network: Network::Testnet,
            version: Version::V1,
            pubkey_type: PubKeyType::MlDsa44,
            pubkey_bytes: &pub_key_bytes,
        };
        let addr = encode_address(&params).unwrap();
        assert_eq!(
            addr,
            "rh1qpqg39uw700gcctpahe650p9zlzpnjt60cpz09m4kx7ncz8922635hsmmfzpd"
        );
        assert!(addr.starts_with("rh"));
        let decoded = decode_address(&addr).unwrap();
        assert_eq!(decoded.network, params.network);
        assert_eq!(decoded.version, params.version);
        assert_eq!(decoded.pubkey_type, params.pubkey_type);
        assert_eq!(decoded.pubkey_hash, Hasher::digest(&pub_key_bytes));
        assert_eq!(decoded.pubkey_hash_bytes(), Hasher::digest(&pub_key_bytes));
        let mut hasher = Sha256::new();
        hasher.update(pub_key_bytes);
        let hash = hasher.finalize().to_vec();
        assert_eq!(decoded.pubkey_hash_hex(), hex_encode(&hash));
        assert_eq!(decoded.pubkey_hash, hash);
        assert_eq!(decoded.pubkey_hash_bytes(), hash);
        assert_eq!(
            decoded.to_string(),
            "rh1qpqg39uw700gcctpahe650p9zlzpnjt60cpz09m4kx7ncz8922635hsmmfzpd"
        );
    }

    /// Round‑trip test on Mainnet with `SLH-DSA-SHA2 S 128`
    #[test]
    fn roundtrip_mainnet_slhdsa_sha2s128() {
        let slh_dsa_sha2s128_pub_key_str = "Wi6WLwN39BUnK7X4gkIG101E2zMZWNAVdOsrG8/IxN4=";

        let pub_key_bytes = general_purpose::STANDARD
            .decode(slh_dsa_sha2s128_pub_key_str)
            .expect("valid base64");

        let params = AddressParams {
            network: Network::Mainnet,
            version: Version::V1,
            pubkey_type: PubKeyType::SlhDsaSha2S128,
            pubkey_bytes: &pub_key_bytes,
        };
        let addr = encode_address(&params).unwrap();
        assert_eq!(
            addr,
            "yp1qpq3z7j5vfjd9y5vlc86al02ujud4tynj73rahcdaa9cdgu47matt5smc3rlz"
        );
        assert!(addr.starts_with("yp"));
        let decoded = decode_address(&addr).unwrap();
        assert_eq!(decoded.network, params.network);
        assert_eq!(decoded.version, params.version);
        assert_eq!(decoded.pubkey_type, params.pubkey_type);
        assert_eq!(decoded.pubkey_hash, Hasher::digest(&pub_key_bytes));
        assert_eq!(decoded.pubkey_hash_bytes(), Hasher::digest(&pub_key_bytes));
        let mut hasher = Sha256::new();
        hasher.update(pub_key_bytes);
        let hash = hasher.finalize().to_vec();
        assert_eq!(decoded.pubkey_hash_hex(), hex_encode(&hash));
        assert_eq!(decoded.pubkey_hash, hash);
        assert_eq!(decoded.pubkey_hash_bytes(), hash);
        assert_eq!(
            decoded.to_string(),
            "yp1qpq3z7j5vfjd9y5vlc86al02ujud4tynj73rahcdaa9cdgu47matt5smc3rlz"
        );
    }

    /// Round‑trip test on Testnet with `SLH-DSA-SHA2 S 128`
    #[test]
    fn roundtrip_testnet_slhdsa_sha2s128() {
        let slh_dsa_sha2s128_pub_key_str = "Wi6WLwN39BUnK7X4gkIG101E2zMZWNAVdOsrG8/IxN4=";

        let pub_key_bytes = general_purpose::STANDARD
            .decode(slh_dsa_sha2s128_pub_key_str)
            .expect("valid base64");

        let params = AddressParams {
            network: Network::Testnet,
            version: Version::V1,
            pubkey_type: PubKeyType::SlhDsaSha2S128,
            pubkey_bytes: &pub_key_bytes,
        };
        let addr = encode_address(&params).unwrap();
        assert_eq!(
            addr,
            "rh1qpq3z7j5vfjd9y5vlc86al02ujud4tynj73rahcdaa9cdgu47matt5s5m48q0"
        );
        assert!(addr.starts_with("rh"));
        let decoded = decode_address(&addr).unwrap();
        assert_eq!(decoded.network, params.network);
        assert_eq!(decoded.version, params.version);
        assert_eq!(decoded.pubkey_type, params.pubkey_type);
        assert_eq!(decoded.pubkey_hash, Hasher::digest(&pub_key_bytes));
        assert_eq!(decoded.pubkey_hash_bytes(), Hasher::digest(&pub_key_bytes));
        let mut hasher = Sha256::new();
        hasher.update(pub_key_bytes);
        let hash = hasher.finalize().to_vec();
        assert_eq!(decoded.pubkey_hash_hex(), hex_encode(&hash));
        assert_eq!(decoded.pubkey_hash, hash);
        assert_eq!(decoded.pubkey_hash_bytes(), hash);
        assert_eq!(
            decoded.to_string(),
            "rh1qpq3z7j5vfjd9y5vlc86al02ujud4tynj73rahcdaa9cdgu47matt5s5m48q0"
        );
    }
}