Skip to content

Add Ord trait to Fe32 (derived) #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/primitives/gf32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ 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, PartialEq, Eq, Hash, Ord, PartialOrd)]
#[repr(transparent)]
pub struct Fe32(pub(crate) u8);

Expand Down Expand Up @@ -487,6 +487,33 @@ mod tests {
assert_eq!(fe * Fe32::P, fe) // Fe32::P == Fe32(1)
}
}

#[test]
fn order() {
use core::cmp::Ordering;

assert_eq!(Fe32(0).cmp(&Fe32(0)), Ordering::Equal);
assert_eq!(Fe32(1).cmp(&Fe32(1)), Ordering::Equal);
assert_eq!(Fe32(0).cmp(&Fe32(1)), Ordering::Less);
assert_eq!(Fe32(0).cmp(&Fe32(31)), Ordering::Less);
assert_eq!(Fe32(1).cmp(&Fe32(0)), Ordering::Greater);
assert_eq!(Fe32(31).cmp(&Fe32(0)), Ordering::Greater);

assert_eq!(Fe32(0).partial_cmp(&Fe32(0)).unwrap(), Ordering::Equal);
assert_eq!(Fe32(1).partial_cmp(&Fe32(1)).unwrap(), Ordering::Equal);
assert_eq!(Fe32(0).partial_cmp(&Fe32(1)).unwrap(), Ordering::Less);
assert_eq!(Fe32(0).partial_cmp(&Fe32(31)).unwrap(), Ordering::Less);
assert_eq!(Fe32(1).partial_cmp(&Fe32(0)).unwrap(), Ordering::Greater);
assert_eq!(Fe32(31).partial_cmp(&Fe32(0)).unwrap(), Ordering::Greater);

// Compare for all variations that order is the same as the order of the inner
for i in Fe32::iter_alpha() {
for j in Fe32::iter_alpha() {
assert_eq!(i.cmp(&j), i.to_u8().cmp(&j.to_u8()));
assert_eq!(i.partial_cmp(&j), i.to_u8().partial_cmp(&j.to_u8()));
}
}
}
}

#[cfg(kani)]
Expand Down