#finite-fields #galois #newtype #macro

no-std g2p

A crate to create types that implement fast finite field arithmetic

9 releases (2 stable)

1.0.1 Jan 17, 2023
1.0.0 Jan 3, 2023
0.4.0 May 25, 2019
0.3.2-pre Apr 3, 2019
0.1.2 Dec 27, 2018

#171 in Cryptography

Download history 2343/week @ 2024-01-28 1851/week @ 2024-02-04 1875/week @ 2024-02-11 2073/week @ 2024-02-18 20896/week @ 2024-02-25 25255/week @ 2024-03-03 20236/week @ 2024-03-10 26115/week @ 2024-03-17 25369/week @ 2024-03-24 28514/week @ 2024-03-31 9592/week @ 2024-04-07 22948/week @ 2024-04-14 25100/week @ 2024-04-21 23966/week @ 2024-04-28 20677/week @ 2024-05-05 25885/week @ 2024-05-12

96,018 downloads per month
Used in 26 crates (4 directly)


390 lines



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. Typically this table is just copied into the source code directly. Using this crate, you can have the benefits in speed of precomputed table, without the need to create your own type with custom multiplication and division implementation.


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


The implementation was tested for finite fields up to 2^17 in size, which compiles reasonably fast. The space requirements are linear to the field size for the inversion table and log^2(N) for the multiplication table. This means it is not feasible to use this to generate fields of size 2^32, which would 4*4GB memory.


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


There is a benchmark suite comparing the result of this crate to galois_2p8 and reed-solomon-erasure which both implement a finite field with 256 elements.

multiplication division

Note that the competing libraries implement some special functionality to improve performance when one of the operands is fixed. Here are the results:

const operand multiplication const operand division

Implementation details

g2p generates a new type that implements all the common arithmetic operations. The calculations are performed on either u8, u16 or u32, depending on the field size.

Addition and subtraction are implemented using regular Xor. For division, the divisor inverted using a precomputed inversion table, which is then multiplied using the multiplication outlined below


Multiplication uses a number of precomputed tables to determine the result. Because a full table would grow with the square of the field size, this approach was not deemed feasible. For example, using a full table for GF65536 = GF(2^16), one would need 2^32 entries, which would mean the program reserves 2*4GB just for these tables alone.

Instead a number n is split into 8bit components n = a + 256 * b + 65536 * c .... Using this representation we can multiply two numbers by cross-multiplying all the components and then adding them up again. So assuming 16bit numbers n = n0 + 256 * n1 and m = m0 + 256 * m1 we get n*m = n0*m0 + 256*n0*m1 + 256*n1*m0 + 65536*n1*m1.

We can now create precomputed tables for multiplying the different components together. There is a table for first component times first component, first times second etc. The results then just have to be added together using the normal finite field addition. For our GF65536 example this means the multiplication tables use 4 * 256 * 256 entries á 2 byte which is ~0.5MB


Licensed under the Apache License, Version 2.0 LICENSE-APACHE or the MIT license LICENSE-MIT>, at your option.


~34K SLoC