Finite fields and modular arithmetic
Arithmetic modulo a prime p is a field with p elements: every nonzero number has an inverse. Polynomials over it, taken modulo an irreducible polynomial of degree k, give a field with pᵏ elements. Every finite field is one of these.
F_p and its multiplicative group
The nonzero elements of F_p form a cyclic group of order p − 1: some primitive root g has every nonzero element as a power gˣ. The exponent x is the discrete logarithm, easy to state and hard to compute for large p, which is what makes Diffie–Hellman work. The library finds orders by dividing p − 1 by its prime factors while the power stays 1, primitive roots by testing g^((p−1)/q) ≠ 1 for each prime q dividing p − 1, and logarithms by baby-step giant-step in about √p steps.
Square roots mod p use Tonelli–Shanks; whether one exists is the Legendre symbol (a/p), computed by quadratic reciprocity. Integers are factored by trial division and then Pollard's rho, which found 2⁶⁷ − 1 = 193707721 · 761838257287, the factorisation Frank Cole multiplied out on a blackboard in 1903.
GF(pᵏ)
For k > 1 an element is a polynomial of degree below k with coefficients mod p; the library picks the first monic irreducible modulus of degree k automatically (or takes yours). Multiplication reduces modulo it. The table shows the field's multiplication or its powers of a generator, which list every nonzero element exactly once.
Uses
Finite fields are where the factoring algorithm does its first, fast step, and where exact linear algebra gets its speed: @ratmath/linalg computes determinants modulo many primes and reassembles them with the Chinese remainder theorem (crt in @ratmath/numtheory). The field objects plug into its field-generic algorithms directly.
In code
import { primeField, galoisField, factorInteger, sqrtMod, discreteLogMod } from "@ratmath/algebraic";
const F = primeField(101);
F.inv(2n); // 51n
F.order(2n); // 100n: 2 is a primitive root mod 101
F.log(5n); // x with 2ˣ = 5
const G = galoisField(2, 8); // GF(256), with the first irreducible modulus of degree 8
const g = G.primitive();
G.format(G.pow(g, 10));
G.log(G.mul(g, g)); // 2n
factorInteger(600851475143n); // [{ prime: 71n, exponent: 1 }, …]
@ratmath/algebraic and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.