Skip to content

Commit

Permalink
Add Default trait to Fe32 (derived)
Browse files Browse the repository at this point in the history
  • Loading branch information
optout21 committed Jul 16, 2024
1 parent 86f7dc0 commit c44e602
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/primitives/gf32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit c44e602

Please sign in to comment.