From c44e6022688d10454eeebdaab0f32678c8e7d8f0 Mon Sep 17 00:00:00 2001 From: optout <13562139+optout21@users.noreply.github.com> Date: Tue, 16 Jul 2024 09:30:16 +0200 Subject: [PATCH] Add Default trait to Fe32 (derived) --- src/primitives/gf32.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/primitives/gf32.rs b/src/primitives/gf32.rs index 0c56841a0..e138def63 100644 --- a/src/primitives/gf32.rs +++ b/src/primitives/gf32.rs @@ -66,11 +66,14 @@ const CHARS_INV: [i8; 128] = [ ]; /// An element in GF(32), the finite field containing elements `[0,31]` inclusive. -#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] +#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)] #[repr(transparent)] 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); @@ -487,6 +490,18 @@ mod tests { assert_eq!(fe * Fe32::P, fe) // Fe32::P == Fe32(1) } } + + #[test] + fn default() { + assert_eq!(Fe32::default().to_u8(), 0); + assert_eq!(Fe32::default(), Fe32::ZERO); + } + + #[test] + fn const_zero() { + assert_eq!(Fe32::ZERO.to_u8(), 0); + assert_eq!(Fe32::ZERO.to_char(), 'q'); + } } #[cfg(kani)]