Bignum Arithmetic 10: Prime-Field Arithmetic
Bignum arithmetic notes / 10
Prime-field arithmetic
A prime-field layer is a bignum layer with a stronger invariant: every operation is interpreted modulo one fixed prime.
For an odd prime \(p\), the field \(\mathbb F_p\) consists of residue classes modulo \(p\). Its representation has three independent choices: the limb radix, the encoding of a field element as \(x\) or \(xR\bmod p\), and the permitted integer range. Both ordinary and Montgomery encodings can use canonical integers in \([0,p)\) or a documented lazy range.
Field element contract
For canonical ordinary residues:
- input: \(0\le a,b<p\);
- add/sub: return \(0\le r<p\);
- multiply/square: return \(0\le r<p\);
- inversion: if \(a\ne0\), return \(a^{-1}\bmod p\).
For Montgomery representation, the stored value of \(x\) is \(xR\bmod p\). The field API either keeps this representation internal or exposes separate conversion and Montgomery-operation functions.
This is the field contract consumed by elliptic curve equations and the elliptic field interface: point formulas are only valid when their field inputs satisfy the promised canonical or lazy ranges.
Inversion by Fermat
Since \(p\) is prime,
\[a^{p-1}=1\quad(a\ne0), \qquad a^{-1}=a^{p-2}\pmod p.\]This uses modular exponentiation and can be made regular with a fixed addition chain for a fixed prime. In Montgomery form, the chain uses Montgomery multiplication and squaring, starting from the encoded identity; its output represents \(a^{-1}\) as \(a^{-1}R\bmod p\). Ordinary integer exponentiation of the stored value \(aR\bmod p\) would instead invert that stored integer.
Inversion by extended Euclid
The extended Euclidean algorithm finds \(u,v\) such that
\[ua+vp=\gcd(a,p)=1.\]Then \(u\bmod p\) is the inverse. Classical Euclid uses a data-dependent number of steps. A constant-time variant uses a proved public iteration bound and secret-independent updates.
Example: P-256 prime
For \(p=2^{256}-2^{224}+2^{192}+2^{96}-1\), reduction can exploit the sparse relation defining \(2^{256}\) modulo \(p\). The teaching implementation keeps the first version auditable: field elements are eight little-endian uint32_t words, and a word product is decomposed into 16-bit halves before it is accumulated.
typedef uint32_t word_t;
typedef struct { word_t v[8]; } fe256_t;
This type has mathematical radix \(B=2^{32}\). Its multiplication proof is the 32-by-32 decomposition from the schoolbook multiplication article.
Example: Curve25519-style prime
For \(p=2^{255}-19\) in the same 32-bit-word model, a uniform implementation can use eight uint32_t words. The high bit of the top word is unused for canonical residues, and reduction folds the excess using shifts, additions, and multiplication by 19 with an explicit high word and carry propagation.
typedef struct { uint32_t v[8]; } fe_25519;
C API boundary
void fe256_add(fe256_t *r, const fe256_t *a, const fe256_t *b);
void fe256_mul(fe256_t *r, const fe256_t *a, const fe256_t *b);
void fe256_sqr(fe256_t *r, const fe256_t *a);
void fe256_inv(fe256_t *r, const fe256_t *a);
void fe256_to_mont(fe256_t *r, const fe256_t *a);
void fe256_from_mont(fe256_t *r, const fe256_t *a);
void fe256_mont_mul(fe256_t *r, const fe256_t *a, const fe256_t *b);
The type fixes the word count; the API contract fixes the modulus. The shared C struct alone cannot distinguish ordinary from Montgomery residues, so function names and conversion boundaries carry that distinction.
SageMath verification
p = 2^256 - 2^224 + 2^192 + 2^96 - 1
F = GF(p)
def words(x):
return [hex((Integer(x) >> (32*i)) & 0xffffffff) for i in range(8)]
for a in [1, 2, 19, p-2]:
inv = power_mod(a, p-2, p)
print(words(inv))
print((a * inv) % p == 1)
Encoding contract
Apply the protocol’s decoding rule at the byte boundary. When canonical encodings are required, reject integers outside \([0,p)\) before constructing a field element. When reduction is permitted, perform it consistently before arithmetic checks. This keeps byte-level identities and field-element identities aligned with the protocol.
