galois-field-gf256
v1.2.0
Published
Finite field GF(2^8) arithmetic with the AES reduction polynomial: multiply, inverse, exp/log tables and Reed-Solomon helpers.
Maintainers
Readme
galois-field-gf256
Arithmetic over the finite field GF(2^8), using the same irreducible polynomial as
AES: x^8 + x^4 + x^3 + x + 1 (0x11B). Handy for building Reed-Solomon codecs, AES
internals, or any bytewise finite-field code.
Zero dependencies. Pure ESM.
Install
npm install galois-field-gf256Usage
import { gmul, mul, inverse, div, pow } from "galois-field-gf256";
gmul(0x57, 0x83); // 0xC1 (the AES spec example)
mul(0x53, inverse(0x53)); // 1
div(0x06, 0x02); // 0x03
pow(0x02, 8); // 0x1BAPI
| Function | Description |
| --------------- | ------------------------------------------------------------------ |
| gadd(a, b) | Field addition — this is just XOR. |
| gsub(a, b) | Subtraction — identical to addition in characteristic 2. |
| gmul(a, b) | Carry-less multiply with on-the-fly reduction (no tables). |
| mul(a, b) | Table-driven multiply. Same result as gmul, faster for hot loops.|
| inverse(a) | Multiplicative inverse. Throws for a === 0. |
| div(a, b) | a * inverse(b). Throws for b === 0. |
| pow(a, n) | Integer power; negative n uses the inverse. |
| exp(i) | generator^i where the generator is 0x03. |
| log(a) | Discrete log base the generator. Throws for a === 0. |
Notes
The exp/log tables are built at import time from the primitive element 0x03. All bytes
are masked to 8 bits, so passing larger integers is safe (only the low byte is used).
License
MIT
