Skip to content

Commit

Permalink
Add Fe32::ZERO const
Browse files Browse the repository at this point in the history
  • Loading branch information
optout21 committed Jul 16, 2024
1 parent 86f7dc0 commit f2df128
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/primitives/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ impl<'hrp> Iterator for HrpFe32Iter<'hrp> {
Some(high) => return Some(Fe32(high >> 5)),
None => {
self.high_iter = None;
return Some(Fe32::Q);
return Some(Fe32::ZERO);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/primitives/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl<'s> UncheckedHrpstring<'s> {
///
/// let mut unchecked = UncheckedHrpstring::new(&addr).unwrap();
/// let witness_version = unchecked.remove_witness_version().unwrap();
/// assert_eq!(witness_version, Fe32::Q);
/// assert_eq!(witness_version, Fe32::ZERO);
/// assert!(unchecked.data_part_ascii().iter().eq(ascii.as_bytes().iter()))
/// ```
#[inline]
Expand Down Expand Up @@ -210,7 +210,7 @@ impl<'s> UncheckedHrpstring<'s> {
/// let addr = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzffffff";
///
/// let unchecked = UncheckedHrpstring::new(&addr).unwrap();
/// assert_eq!(unchecked.witness_version(), Some(Fe32::Q));
/// assert_eq!(unchecked.witness_version(), Some(Fe32::ZERO));
/// ```
#[inline]
pub fn witness_version(&self) -> Option<Fe32> {
Expand Down
11 changes: 10 additions & 1 deletion src/primitives/gf32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ const CHARS_INV: [i8; 128] = [
pub struct Fe32(pub(crate) u8);

impl Fe32 {
/// The Zero element is 0 numeric (character 'Q')
pub const ZERO: Fe32 = Fe32(0);

// These are a little gratuitous for a reference implementation, but it makes me happy to do it.
/// Numeric value maps to bech32 character: 0 == "q".
pub const Q: Fe32 = Fe32(0);
Expand Down Expand Up @@ -257,7 +260,7 @@ impl AsRef<u8> for Fe32 {
}

impl super::Field for Fe32 {
const ZERO: Self = Fe32::Q;
const ZERO: Self = Fe32::ZERO;
const ONE: Self = Fe32::P;
const GENERATOR: Self = Fe32::Z;
const MULTIPLICATIVE_ORDER: usize = 31;
Expand Down Expand Up @@ -487,6 +490,12 @@ mod tests {
assert_eq!(fe * Fe32::P, fe) // Fe32::P == Fe32(1)
}
}

#[test]
fn const_zero() {
assert_eq!(Fe32::ZERO.to_u8(), 0);
assert_eq!(Fe32::ZERO.to_char(), 'q');
}
}

#[cfg(kani)]
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/segwit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::primitives::gf32::Fe32;
pub const MAX_STRING_LENGTH: usize = 90;

/// The field element representing segwit version 0.
pub const VERSION_0: Fe32 = Fe32::Q;
pub const VERSION_0: Fe32 = Fe32::ZERO;
/// The field element representing segwit version 1 (taproot).
pub const VERSION_1: Fe32 = Fe32::P;

Expand Down
2 changes: 1 addition & 1 deletion tests/bip_173_test_vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ macro_rules! check_valid_address_roundtrip {
// bech32 checksum algorithm can be used with any witness version, and this is
// tested by the test vectors. However when BIP-350 came into effect only witness
// version 0 uses bech32 (and this is enforced by encode/decode).
if let Ok((hrp, bech32::Fe32::Q, program)) = bech32::segwit::decode($addr) {
if let Ok((hrp, bech32::Fe32::ZERO, program)) = bech32::segwit::decode($addr) {
let encoded = bech32::segwit::encode_v0(hrp, &program).expect("failed to encode address");
// The bips specifically say that encoder should output lowercase characters so we uppercase manually.
if encoded != $addr {
Expand Down

0 comments on commit f2df128

Please sign in to comment.