Crate g2p

source ·
Expand description

This crate can generate types that implement fast finite field arithmetic.

Many error correcting codes rely on some form of finite field of the form GF(2^p), where p is relatively small. Similarly some cryptographic algorithms such as AES use finite field arithmetic.

While addition and subtraction can be done quickly using just a simple XOR, multiplication is more involved. To speed things up, you can use a precomputed table of logarithms and use the fact that log(a* b) = log(a) + log(b). Expanding back using the same base as the logarithm gives the result for multiplication. The same method can be applied to division.

WARNING: The types generated by this library are probably not suitable for cryptographic purposes, as multiplication is not guaranteed to be constant time.

WARNING Currently only small precomputed tables are supported, the compiler may hang on bigger inputs such as for GF(65536)

Examples

use g2p;
g2p::g2p!(GF16, 4, modulus: 0b10011);

let one: GF16 = 1.into();
let a: GF16 = 5.into();
let b: GF16 = 4.into();
let c: GF16 = 7.into();
assert_eq!(a + c, 2.into());
assert_eq!(a - c, 2.into());
assert_eq!(a * b, c);
assert_eq!(a / c, one / b);
assert_eq!(b / b, one);

Macros

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