Skip to content

Commit

Permalink
checksum: add unit test to print parameters of the descirptor checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
apoelstra committed Mar 26, 2024
1 parent 5c3c72b commit a929a57
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/primitives/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,4 +533,44 @@ mod tests {
#[cfg(feature = "std")]
println!("{}", _s);
}

#[test]
#[cfg(feature = "alloc")]
fn descriptor() {
// In codes that Pieter specifies typically the generator polynomial is
// only given indirectly, in the reference code which encodes it in a
// packed form (shifted multiple times).
//
// For example in the BIP173 Python reference code you will see an array
// called `generator` whose first entry is 0x3b6a57b2. This first entry
// is the generator polynomial in packed form.
//
// To get the expanded polynomial form you can use `u128::unpack` like so:
let unpacked_poly = (0..8)
.rev() // Note .rev() to convert from BE integer literal to LE polynomial!
.map(|i| 0xf5dee51989u64.unpack(i))
.map(|u| Fe32::try_from(u).unwrap())
.collect::<Vec<_>>();
assert_eq!(
unpacked_poly,
[Fe32::_7, Fe32::H, Fe32::_0, Fe32::W, Fe32::_2, Fe32::X, Fe32::V, Fe32::F],
);
// To get a version of the above with bech32 chars instead of Fe32s, which
// can be a bit hard to print, just stick a `.map(Fe32::to_char)` into the
// above iterator chain.

// Ok, exposition over. The actual unit test follows.

// Run with -- --nocapture to see the output of this. This unit test
// does not check the exact output because it is not deterministic,
// and cannot check the code semantics because Rust does not have
// any sort of `eval`, but you can manually check the output works.
let _s = PrintImpl::<crate::Fe32768>::new(
"DescriptorChecksum",
&[Fe32::_7, Fe32::H, Fe32::_0, Fe32::W, Fe32::_2, Fe32::X, Fe32::V, Fe32::F],
&[Fe32::Q, Fe32::Q, Fe32::Q, Fe32::Q, Fe32::Q, Fe32::Q, Fe32::Q, Fe32::P],
)
.to_string();
panic!("{}", _s);
}
}

0 comments on commit a929a57

Please sign in to comment.