Number theory

vfhe.arith.is_prime(n: int) → bool

Whether n is prime: trial division by the primes up to 37, then Miller-Rabin on bases that are deterministic below 2^64.

vfhe.arith.crt(values: list[int], moduli: list[int]) → int

The integer in [0, prod(moduli)) congruent to values[i] modulo moduli[i] for every i, the moduli pairwise coprime.

vfhe.arith.gen_pseudo_mersenne_prime(bits: int, two_adicity: int = 0, max_two_adicity: int | None = None, max_c_bits: int = 52) → int

Generate the pseudo-Mersenne prime p = 2^bits - c with the smallest c > 0 such that 2^two_adicity divides p - 1 (i.e. p admits at least 2^two_adicity-th roots of unity).

p - 1 = 2^bits - (c + 1), so for bits > two_adicity the 2-adicity of p - 1 is governed entirely by c + 1: restricting the search to the residue class c = 2^v - 1 (mod 2^v) makes 2^v divide c + 1 and hence p - 1. A smaller c is strictly better (it is the reduction constant), so the search returns the first hit in that class.

two_adicity is a lower bound, and the prime found may well exceed it – for free, and typically with a smaller c. max_two_adicity caps it instead, so max_two_adicity=two_adicity pins the 2-adicity exactly, and a wider pair (e.g. 6, 7) searches a band of acceptable root-of-unity counts. c is capped at max_c_bits bits, defaulting to the 52-bit IFMA limb width so that the reduction constant stays a single-limb value.