Elliptic arithmetic notes / 05

Field arithmetic interface

Point formulas compose field routines. Each intermediate result must satisfy the representation and range contract of the next routine.

The point layer should not know how Montgomery reduction works. It should know exactly what the field layer promises.

Representation contract

Let \(B=2^w\), and let an \(n\)-word integer represent

\[\operatorname{val}_B(a)=\sum_{i=0}^{n-1}a_iB^i.\]

This is inherited from bignum arithmetic. The teaching implementation has \(w=32\) and \(n=8\) for P-256, while each word product is implemented through 16-bit partial products. The stored value may represent \(x\) directly or encode it as \(\tilde x=xR\bmod p\), where \(R=B^n\). Either convention can use canonical integers in \([0,p)\); the encoding choice and the allowed integer range are separate parts of the contract.

typedef uint32_t word_t;
enum { P256_WORDS = 8 };

typedef struct { word_t v[P256_WORDS]; } fe256_t;

void fe256_add(fe256_t *r, const fe256_t *a, const fe256_t *b); /* canonical output here */
void fe256_sub(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); /* normal field product */
void fe256_sqr(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 field operations used in the point schedules permit the output to equal an input object exactly; partial overlap is excluded. This includes fe256_mul_small and fe256_half, which are called in place in Article 04. Multiplication forms its product in scratch storage, and halving copies the input to its carry buffer before writing output. This aliasing contract is separate from the representation and range contracts.

Montgomery form

If all nontrivial field multiplications are Montgomery products, elliptic formulas can stay in Montgomery representation:

\[\operatorname{MontMul}(\tilde x,\tilde y)=xyR\bmod p.\]

Addition and subtraction are representation-preserving because

\[\tilde x+\tilde y\equiv (x+y)R\pmod p.\]

This is why Montgomery arithmetic is the natural bridge from bignum to ECC.

The representation choice also applies to constants. A field constant passed to Montgomery multiplication is encoded as \(cR\bmod p\). A helper that multiplies a stored integer by the ordinary small integer 3 preserves the encoding directly, since \(3\tilde x\equiv(3x)R\pmod p\); modular halving similarly preserves it. The API must distinguish such integer helpers from multiplication by an encoded field element.

Zero and equality tests

Zero and equality predicates compare field elements, using the same representation and range contract as arithmetic. For canonical inputs in one encoding, comparing limbs is sufficient. This also holds in Montgomery form because multiplication by \(R\) is invertible modulo \(p\). If a lazy range contains \(p\), however, the nonzero integer \(p\) represents the zero field element. Such predicates must first canonicalize or compare residues modulo \(p\) directly. Point routines require these field-level tests when detecting infinity, order-two points, or equal and inverse inputs.

Constant-time conditional move

static void fe256_cmov(fe256_t *r, const fe256_t *a, uint32_t take) {
    uint32_t mask = 0u - take;
    for (uint32_t i = 0; i < P256_WORDS; i++) {
        word_t t = mask & (r->v[i] ^ a->v[i]);
        r->v[i] ^= t;
    }
}

Precondition: take is 0 or 1. Postcondition: r is unchanged if take=0, and becomes a if take=1. The loop count and memory locations are public.

Two range examples

If fe256_add returns a value below \(2p\), then a later fe256_mul must either accept inputs below \(2p\) or the addition result must be canonicalized. For P-256, eight 32-bit words cannot represent the entire interval \([0,2p)\). A lazy interface using that interval must retain a high carry or use an additional word.

If fe256_half receives odd \(x\), it may compute \((x+p)/2\). The addition \(x+p\) must fit the temporary representation. For canonical P-256 input, retain eight low words and one carry word for \(x+p<2p\), then shift the full value right by one bit.

SageMath contract model

p = 2^256 - 2^224 + 2^192 + 2^96 - 1
R = 2^256
def enc(x): return (x*R) % p
def mont(x, y): return (x*y*inverse_mod(R, p)) % p
for x, y in [(1, 2), (p-2, p-3), (123456789, 987654321)]:
    print(mont(enc(x), enc(y)) == enc((x*y) % p))
    print((enc(x) + enc(y)) % p == enc((x+y) % p))