Macro g2p::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 or u16.

Example

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 coffiecient.
                            // Can be left out in case it is not needed.
    generator: 0b10         // The element that generates the cyclic group. Can be left out,
                            // there should not really be a reason to specify it.
);

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");