From 4b42138dc15e0c1c5f9dcae990112198bef7f167 Mon Sep 17 00:00:00 2001 From: optout <13562139+optout21@users.noreply.github.com> Date: Tue, 16 Jul 2024 17:49:08 +0200 Subject: [PATCH] Add Fe32::ZERO const --- src/primitives/checksum.rs | 2 +- src/primitives/gf32.rs | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/primitives/checksum.rs b/src/primitives/checksum.rs index 3e5f82f7d..d932780d2 100644 --- a/src/primitives/checksum.rs +++ b/src/primitives/checksum.rs @@ -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); } } } diff --git a/src/primitives/gf32.rs b/src/primitives/gf32.rs index 0c56841a0..1c030cdee 100644 --- a/src/primitives/gf32.rs +++ b/src/primitives/gf32.rs @@ -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); @@ -257,7 +260,7 @@ impl AsRef 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; @@ -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)]