Elliptic arithmetic notes / 11

Testing with SageMath

SageMath is the algebra oracle. It should generate vectors, expose edge cases, and catch formula mistakes, while C-specific bounds are checked separately.

The bignum testing article already explains differential testing against exact integers. Elliptic testing adds group structure and exceptional cases.

Test layers

Layer SageMath reference C target
Field arithmetic in GF(p) fe256_add, fe256_mul, fe256_sqr, fe256_inv
Affine point EllipticCurve(GF(p), [a,b]) p256_affine_on_curve, public/spec addition, validation
Jacobian point explicit coordinate conversion p256_jac_double, p256_jac_add_mixed
Scalar multiplication k*P p256_scalar_mul_public and separate secret-scalar routines

Edge cases

Include infinity, \(P+\mathcal O\), \(P+(-P)\), noncanonical encodings, points not on the curve, and scalars 0, 1, and \(n-1\). Test doubling with \(y=0\) and other low-order inputs on a toy curve that actually contains such points. P-256 and the order-19 toy curve below have no nonidentity point of order 2.

Vector generator

The example prints vectors for inspection. Automated regression tests should compare the C results with these vectors and fail on a mismatch.

p = 17
E = EllipticCurve(GF(p), [2, 2])
P = E(5, 1)
for k in range(0, 12):
    Q = k*P
    if Q == E(0):
        print(k, "INF")
    else:
        print(k, int(Q[0]), int(Q[1]))
print("order", P.order())

Jacobian conversion check

p = 17
F = GF(p)
E = EllipticCurve(F, [2, 2])
def from_jac(X, Y, Z):
    if Z == 0:
        return E(0)
    return E(X/Z^2, Y/Z^3)
X, Y, Z = F(5), F(1), F(1)
print(from_jac(X, Y, Z) == E(5, 1))

This helper maps every triple with \(Z=0\) to the mathematical identity. A C differential test also checks the representation contract before this conversion: field coordinates must have their promised ranges, and an infinity output must have the library’s prescribed sentinel. Otherwise an incorrect triple such as \((0,0,0)\) would compare equal to the correct group result.

Randomized C harness shape

/* C test harness prints hex limbs; SageMath parses and checks them. */
fe256_t a, b, c;
for (uint32_t i = 0; i < trials; i++) {
    p256_random_fe_for_test(&a);
    p256_random_fe_for_test(&b);
    fe256_mul(&c, &a, &b);
    p256_print_case("mul", &a, &b, &c);
}

Constructed boundary cases must respect the routine under test. For canonical P-256 field operations, use zero, one, \(p-1\), and canonical operands that produce long carry chains. An all-one eight-word array exceeds \(p\) and therefore belongs in decoding or rejection tests, not among valid inputs to a canonical arithmetic routine. Values near \(2p\) belong only to lazy interfaces whose storage includes the required extra carry or word.

Connecting tests to proofs

Differential tests check that the implementation matches the algebraic reference on selected inputs. Field bounds, point-formula derivations, and scalar-loop invariants establish the value relation for all allowed inputs. Compiler and leakage checks cover the execution properties that the algebraic model does not describe.