Elliptic arithmetic notes / 01

Fields and curve equations

Before adding points, the library must decide what a field element is, what a valid curve is, and which residues a C function may accept or return.

Point formulas are compositions of field additions, subtractions, multiplications, squarings, and inversions, so their implementation builds directly on finite-field arithmetic. Here we work over a prime field \(\mathbb F_p\) with \(p>3\).

Prime-field elements

Mathematically,

\[\mathbb F_p=\mathbb Z/p\mathbb Z.\]

In C, a field element is stored as a fixed-size array whose value represents a residue class and satisfies a stated range bound. A canonical residue satisfies \(0\le x<p\). A lazy residue may satisfy \(0\le x<cp\) for a small constant \(c\). The field API must say which one it accepts.

typedef uint32_t word_t;
typedef struct {
    word_t v[8];       /* P-256: little-endian 32-bit words */
} fe256_t;

This representation has mathematical radix \(B=2^{32}\), but multiplication must still split each word into 16-bit halves so that every primitive product and carry fits in uint32_t. This connects directly to bignum limb representation and prime-field arithmetic.

Short Weierstrass curves

For \(p>3\), the implementation target is

\[E/\mathbb F_p:\qquad y^2=x^3+ax+b.\]

The curve is nonsingular exactly when

\[\Delta=-16(4a^3+27b^2)\ne 0.\]

Since \(-16\) is nonzero in \(\mathbb F_p\), a C parameter loader can check \(4a^3+27b^2\not\equiv 0\pmod p\).

Example: a valid small curve

Over \(\mathbb F_{17}\), \(a=2,b=2\) gives \(4a^3+27b^2=140\equiv 4\), so the curve is nonsingular. The point \((5,1)\) lies on it because \(1^2\equiv 5^3+2\cdot 5+2\equiv 1\pmod {17}\).

Example: a singular equation

Over any odd field, \(y^2=x^3\) has \(a=b=0\) and discriminant zero. It has a cusp at the origin. Implementing the chord-and-tangent formulas on this equation does not give an elliptic-curve group.

Point representation

The group \(E(\mathbb F_p)\) consists of the affine pairs \((x,y)\in\mathbb F_p^2\) satisfying the equation together with the point at infinity \(\mathcal O\). The following C type stores either an affine pair or a flag representing \(\mathcal O\):

typedef struct {
    fe256_t x;
    fe256_t y;
    uint32_t infinity; /* public flag for decoded/public P-256 points */
} p256_affine_t;

The same fixed-size struct supports masked selection of secret-dependent points by selecting both coordinates and the infinity flag. A validation routine may branch on a public decoded key’s flag. In a secret-scalar multiplication loop, an intermediate infinity flag can depend on the scalar, so its use must preserve the loop’s leakage contract.

SageMath parameter checks

p = 17
F = GF(p)
for a, b in [(2, 2), (0, 0)]:
    disc = F(4*a^3 + 27*b^2)
    print(a, b, disc != 0)
    if disc != 0:
        E = EllipticCurve(F, [a, b])
        print(E.cardinality(), E(5, 1) in E if (F(1)^2 == F(5)^3 + a*F(5) + b) else "skip")

C proof obligations

  1. fe256_is_canonical(x) must be correct for the chosen limb order.
  2. fe256_add, fe256_sub, fe256_mul, and fe256_sqr must document output ranges.
  3. Curve validation must reject noncanonical encodings unless the protocol explicitly permits reduction.
  4. Generate parameter constants from the specified exact integers and verify that the emitted limbs reconstruct those integers.