use core::iter::{Product, Sum};
use core::ops::{Add, Div, Mul, Sub};
#[cfg(feature = "fuzzing")]
use arbitrary::Arbitrary;
#[cfg(feature = "zeroize_memory")]
use zeroize::Zeroize;
#[inline]
const fn gf256_mul(a: u8, b: u8, poly: u16) -> u8 {
let mut result = 0u16;
let mut a_val = a as u16;
let mut b_val = b as u16;
while b_val > 0 {
if (b_val & 1) == 1 {
result ^= a_val;
}
let carry = (a_val & 0x80) != 0;
a_val <<= 1;
if carry {
a_val ^= poly;
}
b_val >>= 1;
}
(result & 0xFF) as u8
}
#[inline]
const fn gf256_pow(base: u8, exponent: u8, poly: u16) -> u8 {
let mut result = 1u8;
let mut base_val = base;
let mut exponent_val = exponent;
while exponent_val > 0 {
if (exponent_val & 1) == 1 {
result = gf256_mul(result, base_val, poly);
}
base_val = gf256_mul(base_val, base_val, poly);
exponent_val >>= 1;
}
result
}
pub const PRIMITIVE_POLYS: &[u16] = &[
0x11B, 0x11D, 0x12B, 0x12D, 0x139, 0x13F, 0x14D, 0x15F, 0x163, 0x165, 0x169, 0x171, 0x177,
0x17B, 0x187, 0x18B, 0x18D, 0x19F, 0x1A3, 0x1A9, 0x1B1, 0x1BD, 0x1C3, 0x1CF, 0x1D7, 0x1DD,
0x1E7, 0x1F3, 0x1F5, 0x1F9,
];
#[inline]
const fn is_primitive(poly: u16) -> bool {
let mut i = 0;
while i < PRIMITIVE_POLYS.len() {
if PRIMITIVE_POLYS[i] == poly {
return true;
}
i += 1;
}
false
}
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "fuzzing", derive(Arbitrary))]
#[cfg_attr(feature = "zeroize_memory", derive(Zeroize))]
#[cfg_attr(feature = "zeroize_memory", zeroize(drop))]
pub struct GF256<const POLY: u16>(pub u8);
pub struct Tables<const POLY: u16> {
pub log: [u8; 256],
pub exp: [u8; 512],
}
impl<const POLY: u16> Default for Tables<POLY> {
fn default() -> Self {
Self::new()
}
}
#[inline]
const fn is_primitive_element<const POLY: u16>(x: u8) -> bool {
const FACTORS: [u8; 3] = [3, 5, 17];
let mut i = 0;
while i < FACTORS.len() {
if gf256_pow(x, 255 / FACTORS[i], POLY) == 1 {
return false;
}
i += 1;
}
gf256_pow(x, 255, POLY) == 1
}
#[inline]
const fn find_generator<const POLY: u16>() -> u8 {
let mut i = 1u16;
while i <= 255 {
if is_primitive_element::<POLY>(i as u8) {
return i as u8;
}
i += 1;
}
panic!("No primitive element found");
}
impl<const POLY: u16> Tables<POLY> {
pub const fn new() -> Self {
assert!(is_primitive(POLY), "POLY must be primitive");
let mut log = [0u8; 256];
let mut exp = [0u8; 512];
let gen = find_generator::<POLY>();
let mut i = 0usize;
let mut x = 1u8;
while i < 255 {
exp[i] = x;
log[x as usize] = i as u8;
x = gf256_mul(x, gen, POLY);
i += 1;
}
let mut j = 255usize;
while j < 512 {
exp[j] = exp[j - 255];
j += 1;
}
Self { log, exp }
}
}
impl<const POLY: u16> GF256<POLY> {
const POLY_CHECK: () = assert!(is_primitive(POLY), "POLY must be primitive");
pub const TABLES: Tables<POLY> = Tables::new();
pub fn add(self, other: Self) -> Self {
#[allow(path_statements)]
Self::POLY_CHECK; Self(self.0 ^ other.0)
}
pub fn sub(self, other: Self) -> Self {
#[allow(path_statements)]
Self::POLY_CHECK;
Self(self.0 ^ other.0)
}
pub fn mul(self, other: Self) -> Self {
let log_x = Self::TABLES.log[self.0 as usize] as usize;
let log_y = Self::TABLES.log[other.0 as usize] as usize;
if self.0 == 0 || other.0 == 0 {
Self(0)
} else {
Self(Self::TABLES.exp[log_x + log_y])
}
}
pub fn div(self, other: Self) -> Self {
let log_x = Self::TABLES.log[self.0 as usize] as usize;
let log_y = Self::TABLES.log[other.0 as usize] as usize;
if self.0 == 0 {
Self(0)
} else {
Self(Self::TABLES.exp[log_x + 255 - log_y])
}
}
}
#[allow(clippy::suspicious_arithmetic_impl)]
impl<const POLY: u16> Add for GF256<POLY> {
type Output = Self;
fn add(self, other: Self) -> Self::Output {
self.add(other)
}
}
#[allow(clippy::suspicious_arithmetic_impl)]
impl<const POLY: u16> Sub for GF256<POLY> {
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
self.sub(other)
}
}
#[allow(clippy::suspicious_arithmetic_impl)]
impl<const POLY: u16> Mul for GF256<POLY> {
type Output = Self;
fn mul(self, other: Self) -> Self::Output {
self.mul(other)
}
}
#[allow(clippy::suspicious_arithmetic_impl)]
impl<const POLY: u16> Div for GF256<POLY> {
type Output = Self;
fn div(self, other: Self) -> Self::Output {
self.div(other)
}
}
impl<const POLY: u16> Sum for GF256<POLY> {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(Self(0), |acc, x| acc + x)
}
}
impl<const POLY: u16> Product for GF256<POLY> {
fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(Self(1), |acc, x| acc * x)
}
}
#[cfg(test)]
mod tests {
use super::GF256;
use alloc::vec;
use rstest::rstest;
const POLY: u16 = 0x11d_u16;
const LOG_TABLE: [u8; 256] = [
0x00, 0x00, 0x01, 0x19, 0x02, 0x32, 0x1a, 0xc6, 0x03, 0xdf, 0x33, 0xee, 0x1b, 0x68, 0xc7,
0x4b, 0x04, 0x64, 0xe0, 0x0e, 0x34, 0x8d, 0xef, 0x81, 0x1c, 0xc1, 0x69, 0xf8, 0xc8, 0x08,
0x4c, 0x71, 0x05, 0x8a, 0x65, 0x2f, 0xe1, 0x24, 0x0f, 0x21, 0x35, 0x93, 0x8e, 0xda, 0xf0,
0x12, 0x82, 0x45, 0x1d, 0xb5, 0xc2, 0x7d, 0x6a, 0x27, 0xf9, 0xb9, 0xc9, 0x9a, 0x09, 0x78,
0x4d, 0xe4, 0x72, 0xa6, 0x06, 0xbf, 0x8b, 0x62, 0x66, 0xdd, 0x30, 0xfd, 0xe2, 0x98, 0x25,
0xb3, 0x10, 0x91, 0x22, 0x88, 0x36, 0xd0, 0x94, 0xce, 0x8f, 0x96, 0xdb, 0xbd, 0xf1, 0xd2,
0x13, 0x5c, 0x83, 0x38, 0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 0xb8, 0x6d, 0xda,
0xa9, 0x4f, 0x9e, 0x21, 0x42, 0x84, 0x15, 0x2a, 0x54, 0xa8, 0x4d, 0x9a, 0x29, 0x52, 0xa4,
0x55, 0xaa, 0x49, 0x92, 0x39, 0x72, 0xe4, 0xd5, 0xb7, 0x73, 0xe6, 0xd1, 0xbf, 0x63, 0xc6,
0x91, 0x3f, 0x7e, 0xfc, 0xe5, 0xd7, 0xb3, 0x7b, 0xf6, 0xf1, 0xff, 0xe3, 0xdb, 0xab, 0x4b,
0x96, 0x31, 0x62, 0xc4, 0x95, 0x37, 0x6e, 0xdc, 0xa5, 0x57, 0xae, 0x41, 0x82, 0x19, 0x32,
0x64, 0xc8, 0x8d, 0x07, 0x0e, 0x1c, 0x38, 0x70, 0xe0, 0xdd, 0xa7, 0x53, 0xa6, 0x51, 0xa2,
0x59, 0xb2, 0x79, 0xf2, 0xf9, 0xef, 0xc3, 0x9b, 0x2b, 0x56, 0xac, 0x45, 0x8a, 0x09, 0x12,
0x24, 0x48, 0x90, 0x3d, 0x7a, 0xf4, 0xf5, 0xf7, 0xf3, 0xfb, 0xeb, 0xcb, 0x8b, 0x0b, 0x16,
0x2c, 0x58, 0xb0, 0x7d, 0xfa, 0xe9, 0xcf, 0x83, 0x1b, 0x36, 0x6c, 0xd8, 0xad, 0x47, 0x8e,
0x01, 0x02,
];
#[test]
fn add_works() {
let answers: [u8; 256] = [
1, 2, 5, 17, 18, 18, 90, 70, 30, 229, 71, 6, 214, 239, 212, 109, 72, 252, 205, 84, 128,
248, 5, 72, 147, 194, 111, 244, 208, 56, 44, 177, 152, 173, 43, 179, 196, 110, 155, 20,
95, 71, 59, 173, 30, 211, 29, 102, 91, 57, 199, 119, 126, 15, 169, 25, 148, 32, 96,
170, 244, 139, 172, 7, 89, 1, 234, 160, 255, 242, 110, 65, 135, 82, 172, 188, 14, 173,
90, 120, 203, 55, 71, 117, 228, 64, 106, 194, 15, 51, 204, 255, 216, 142, 55, 162, 199,
237, 245, 37, 210, 106, 58, 230, 102, 32, 28, 60, 42, 56, 221, 243, 75, 65, 165, 227,
242, 248, 190, 184, 117, 162, 9, 105, 228, 192, 193, 155, 130, 103, 238, 171, 52, 237,
185, 164, 40, 212, 255, 175, 181, 208, 212, 76, 75, 232, 3, 94, 116, 28, 225, 214, 88,
214, 171, 171, 199, 245, 62, 93, 209, 238, 110, 56, 83, 45, 240, 179, 108, 98, 64, 1,
167, 10, 79, 158, 17, 141, 120, 224, 130, 27, 63, 90, 17, 11, 87, 143, 226, 58, 239,
227, 157, 52, 113, 188, 127, 246, 163, 120, 216, 47, 57, 12, 162, 171, 60, 80, 61, 3,
98, 224, 80, 111, 172, 69, 56, 251, 173, 231, 23, 137, 180, 83, 217, 125, 23, 32, 161,
211, 84, 164, 252, 6, 237, 0, 177, 254, 39, 193, 99, 246, 101, 148, 28, 14, 98, 107,
111, 224, 152, 50, 5, 23, 214, 174,
];
for (i, a) in answers.iter().enumerate() {
assert_eq!(
(GF256::<POLY>(LOG_TABLE[i]) + GF256::<POLY>(EXP_TABLE[i])).0,
*a
);
}
}
#[test]
fn sub_works() {
add_works();
}
#[test]
fn mul_works() {
let answers: [u8; 256] = [
0, 0, 4, 200, 32, 14, 206, 179, 39, 134, 169, 160, 32, 59, 184, 50, 45, 121, 69, 43,
102, 43, 139, 169, 18, 94, 107, 84, 18, 157, 159, 51, 211, 1, 52, 13, 51, 128, 31, 219,
240, 230, 212, 219, 197, 19, 11, 135, 93, 163, 237, 53, 91, 177, 135, 124, 240, 224, 6,
158, 167, 155, 155, 38, 223, 144, 70, 54, 50, 45, 134, 170, 126, 223, 103, 207, 253,
176, 75, 98, 137, 87, 59, 50, 208, 116, 29, 200, 128, 82, 13, 138, 107, 53, 42, 34,
123, 203, 65, 174, 111, 101, 19, 78, 165, 62, 115, 108, 175, 139, 126, 107, 55, 196,
30, 209, 126, 8, 15, 211, 57, 191, 37, 254, 24, 136, 30, 111, 188, 30, 209, 208, 49,
132, 181, 22, 207, 241, 28, 2, 97, 58, 244, 179, 190, 120, 249, 174, 99, 6, 215, 232,
173, 1, 20, 216, 224, 191, 247, 78, 223, 101, 153, 1, 182, 203, 213, 75, 132, 98, 53,
204, 13, 177, 22, 88, 218, 21, 32, 68, 247, 153, 11, 190, 47, 128, 214, 33, 110, 194,
102, 77, 5, 178, 74, 65, 134, 62, 91, 190, 133, 15, 134, 94, 37, 247, 205, 51, 224,
152, 15, 13, 13, 233, 189, 206, 100, 131, 222, 5, 70, 182, 231, 176, 167, 150, 156,
249, 29, 189, 96, 149, 239, 162, 43, 239, 89, 8, 9, 57, 118, 227, 168, 243, 164, 188,
125, 8, 8, 240, 36, 45, 21, 20, 44, 175,
];
for (i, a) in answers.iter().enumerate() {
assert_eq!(
(GF256::<POLY>(LOG_TABLE[i]) * GF256::<POLY>(EXP_TABLE[i])).0,
*a
);
}
}
#[test]
fn div_works() {
let answers: [u8; 256] = [
0, 0, 71, 174, 173, 87, 134, 213, 152, 231, 124, 39, 203, 113, 13, 198, 88, 171, 55,
150, 177, 227, 25, 225, 227, 180, 157, 225, 252, 122, 88, 161, 45, 87, 148, 78, 40,
165, 74, 134, 142, 120, 121, 163, 156, 75, 154, 241, 239, 27, 152, 130, 125, 235, 230,
32, 138, 225, 145, 90, 214, 226, 182, 168, 155, 175, 179, 124, 105, 169, 249, 58, 201,
14, 155, 217, 196, 254, 201, 143, 229, 12, 178, 24, 100, 226, 163, 234, 177, 36, 75,
106, 114, 208, 162, 63, 235, 181, 108, 131, 248, 51, 190, 187, 235, 115, 112, 37, 79,
90, 112, 237, 195, 121, 136, 110, 174, 143, 113, 134, 229, 255, 35, 175, 156, 208, 240,
222, 94, 202, 228, 34, 123, 23, 48, 18, 122, 114, 75, 243, 212, 139, 56, 132, 157, 119,
219, 170, 236, 11, 51, 86, 224, 221, 142, 200, 154, 136, 179, 72, 3, 32, 142, 149, 180,
209, 253, 17, 210, 134, 162, 106, 38, 108, 154, 154, 74, 181, 115, 142, 204, 195, 23,
162, 178, 41, 9, 90, 190, 14, 2, 45, 227, 253, 115, 93, 155, 244, 83, 219, 11, 196,
167, 241, 33, 60, 103, 69, 181, 189, 145, 130, 174, 137, 65, 65, 45, 153, 79, 236, 199,
209, 41, 10, 205, 44, 182, 38, 222, 209, 253, 247, 64, 71, 32, 1, 27, 53, 4, 110, 170,
221, 215, 4, 179, 163, 64, 90, 152, 163, 235, 6, 41, 93, 176, 175,
];
for (i, a) in answers.iter().enumerate() {
assert_eq!(
(GF256::<POLY>(LOG_TABLE[i]) / GF256::<POLY>(EXP_TABLE[i])).0,
*a
);
}
}
#[test]
fn sum_works() {
let values = vec![GF256::<POLY>(0x53), GF256::<POLY>(0xCA), GF256::<POLY>(0)];
assert_eq!(values.into_iter().sum::<GF256<POLY>>().0, 0x99);
}
#[test]
fn product_works() {
let values = vec![GF256::<POLY>(1), GF256::<POLY>(1), GF256::<POLY>(4)];
assert_eq!(values.into_iter().product::<GF256<POLY>>().0, 4);
}
#[rstest]
#[case(0x11B)]
#[case(0x11D)]
#[case(0x12B)]
#[case(0x12D)]
fn primitive_poly(#[case] poly: u16) {
assert!(super::is_primitive(poly));
}
#[rstest]
#[case(0x100)]
#[case(0x102)]
#[case(0x103)]
fn non_primitive_poly(#[case] poly: u16) {
assert!(!super::is_primitive(poly));
}
fn run_ops_all<const POLY: u16>() {
let mut a_val = 0u16;
while a_val <= 255 {
let a = a_val as u8;
assert_eq!((GF256::<{ POLY }>(a) + GF256::<{ POLY }>(0)).0, a);
assert_eq!((GF256::<{ POLY }>(a) - GF256::<{ POLY }>(0)).0, a);
assert_eq!((GF256::<{ POLY }>(a) - GF256::<{ POLY }>(a)).0, 0);
assert_eq!((GF256::<{ POLY }>(a) + GF256::<{ POLY }>(a)).0, 0);
a_val += 1;
}
let samples: [u8; 16] = [0, 1, 2, 3, 4, 7, 11, 13, 29, 63, 64, 95, 127, 128, 199, 255];
for &x in &samples {
for &y in &samples {
let prod = (GF256::<{ POLY }>(x) * GF256::<{ POLY }>(y)).0;
let ref_prod = super::gf256_mul(x, y, POLY);
assert_eq!(prod, ref_prod);
if y != 0 {
assert_eq!(
((GF256::<{ POLY }>(x) * GF256::<{ POLY }>(y)) / GF256::<{ POLY }>(y)).0,
x
);
}
assert_eq!((GF256::<{ POLY }>(0) * GF256::<{ POLY }>(y)).0, 0);
assert_eq!((GF256::<{ POLY }>(x) * GF256::<{ POLY }>(0)).0, 0);
if x != 0 {
assert_eq!((GF256::<{ POLY }>(x) / GF256::<{ POLY }>(x)).0, 1);
} else {
assert_eq!((GF256::<{ POLY }>(0) / GF256::<{ POLY }>(1)).0, 0);
}
}
}
}
macro_rules! gen_ops_tests {
( $( ($poly:expr, $name:ident) ),+ $(,)? ) => {
$(
#[test]
fn $name() {
const POLY: u16 = $poly;
run_ops_all::<{ POLY }>();
}
)+
};
}
gen_ops_tests!(
(0x11B, ops_poly_11b),
(0x11D, ops_poly_11d),
(0x12B, ops_poly_12b),
(0x12D, ops_poly_12d),
(0x139, ops_poly_139),
(0x13F, ops_poly_13f),
(0x14D, ops_poly_14d),
(0x15F, ops_poly_15f),
(0x163, ops_poly_163),
(0x165, ops_poly_165),
(0x169, ops_poly_169),
(0x171, ops_poly_171),
(0x177, ops_poly_177),
(0x17B, ops_poly_17b),
(0x187, ops_poly_187),
(0x18B, ops_poly_18b),
(0x18D, ops_poly_18d),
(0x19F, ops_poly_19f),
(0x1A3, ops_poly_1a3),
(0x1A9, ops_poly_1a9),
(0x1B1, ops_poly_1b1),
(0x1BD, ops_poly_1bd),
(0x1C3, ops_poly_1c3),
(0x1CF, ops_poly_1cf),
(0x1D7, ops_poly_1d7),
(0x1DD, ops_poly_1dd),
(0x1E7, ops_poly_1e7),
(0x1F3, ops_poly_1f3),
(0x1F5, ops_poly_1f5),
(0x1F9, ops_poly_1f9),
);
}