From c1025f7ddc2b20bc6c71446d1d8a5e21141c8cbe Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 11 Sep 2023 10:45:04 -0500 Subject: [PATCH] Add Codex32 checksum algo back in --- src/lib.rs | 2 ++ src/primitives/mod.rs | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index eced7ff1f..7a3bca7ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,8 @@ //! - Non-segwit stuff and you do *not* have an allocator, use the //! [`primitives::decode::CheckedHrpstring`] type for decoding. For encoding we provide various //! top level functions of the form `_to_fmt`. +//! - To define your own checksum algorithm implement [`bech32::primitives::Checksum`]. +//! See the implementation on [`primitives::checksum::Codex32`] for an example. //! //! The original description in [BIP-0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki) //! has more details. See also [BIP-0350](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki). diff --git a/src/primitives/mod.rs b/src/primitives/mod.rs index 1d496ba2b..a3c9a9847 100644 --- a/src/primitives/mod.rs +++ b/src/primitives/mod.rs @@ -26,6 +26,11 @@ pub enum Bech32 {} #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum Bech32m {} +/// The codex32 checksum algorithm, defined in [BIP-93]. +/// [BIP-350]: +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +enum Codex32 {} + impl Checksum for NoChecksum { type MidstateRepr = PackedNull; const CHECKSUM_LENGTH: usize = 0; @@ -50,6 +55,20 @@ impl Checksum for Bech32m { const TARGET_RESIDUE: u32 = 0x2bc830a3; } +impl Checksum for Codex32 { + type MidstateRepr = u128; + const CHECKSUM_LENGTH: usize = 13; + // Copied from BIP-93 + const GENERATOR_SH: [u128; 5] = [ + 0x19dc500ce73fde210, + 0x1bfae00def77fe529, + 0x1fbd920fffe7bee52, + 0x1739640bdeee3fdad, + 0x07729a039cfc75f5a, + ]; + const TARGET_RESIDUE: u128 = 0x10ce0795c2fd1e62a; +} + #[cfg(test)] mod tests { use super::*;