Skip to content

Commit

Permalink
Add Codex32 checksum algo back in
Browse files Browse the repository at this point in the history
  • Loading branch information
tcharding committed Sep 11, 2023
1 parent d2623ec commit c1025f7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
19 changes: 19 additions & 0 deletions src/primitives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]: <https://github.com/bitcoin/bips/blob/master/bip-0093.mediawiki>
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
enum Codex32 {}

impl Checksum for NoChecksum {
type MidstateRepr = PackedNull;
const CHECKSUM_LENGTH: usize = 0;
Expand All @@ -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::*;
Expand Down

0 comments on commit c1025f7

Please sign in to comment.