Macro g2gen::g2p

source ·
g2p!() { /* proc-macro */ }
Expand description

Generate a newtype of the given name and implement finite field arithmetic on it.

The generated type have implementations for Add, Sub, Mul and Div.

There are also implementations for equality, copy and debug. Conversion from and to the base type are implemented via the From trait. Depending on the size of p the underlying type is u8, u16 or u32.

Example

g2gen::g2p!(
    GF256,                  // Name of the newtype
    8,                      // The power of 2 specifying the field size 2^8 = 256 in this
                            // case.
    modulus: 0b1_0001_1101, // The reduction polynomial to use, each bit is a coefficient.
                            // Can be left out in case it is not needed.
);

let a: GF256 = 255.into();  // Conversion from the base type
assert_eq!(a - a, a + a);   // Finite field arithmetic.
assert_eq!(format!("{}", a), "255_GF256");