Elliptic Arithmetic 04: Jacobian Addition and Doubling
Elliptic arithmetic notes / 04
Jacobian addition and doubling
Each point formula has a domain of validity. Stating that domain and the range of every field temporary connects the algebraic identity to its C implementation.
For a curve \(y^2=x^3-3x+b\) over a prime field with \(p>3\), the doubling numerator \(3X^2+aZ^4\) factors as \(3(X-Z^2)(X+Z^2)\). This factorization gives the specialized schedule below, including the P-256 case.
Doubling for \(a=-3\)
Let \(P=(X_1:Y_1:Z_1)\) with \(Z_1\ne 0\) and \(Y_1\ne 0\). Define
\[A=3(X_1-Z_1^2)(X_1+Z_1^2),\quad B=2Y_1,\quad C=B^2,\quad D=CX_1.\]Then a common Jacobian doubling schedule is
\[X_3=A^2-2D,\qquad Y_3=A(D-X_3)-C^2/2,\qquad Z_3=BZ_1.\]The division by 2 is multiplication by \(2^{-1}\bmod p\). For odd prime fields this is well-defined, but the field API should expose fe256_half with a range proof.
static void p256_jac_double_core(p256_jac_t *r, const p256_jac_t *p) {
fe256_t z2, xmz2, xpz2, A, B, C, D, twoD, C2;
fe256_sqr(&z2, &p->Z);
fe256_sub(&xmz2, &p->X, &z2);
fe256_add(&xpz2, &p->X, &z2);
fe256_mul(&A, &xmz2, &xpz2);
fe256_mul_small(&A, &A, 3u);
fe256_add(&B, &p->Y, &p->Y);
fe256_sqr(&C, &B);
fe256_mul(&D, &C, &p->X);
fe256_add(&twoD, &D, &D);
fe256_sqr(&r->X, &A);
fe256_sub(&r->X, &r->X, &twoD);
fe256_sqr(&C2, &C);
fe256_half(&C2, &C2);
fe256_sub(&D, &D, &r->X);
fe256_mul(&r->Y, &A, &D);
fe256_sub(&r->Y, &r->Y, &C2);
fe256_mul(&r->Z, &B, &p->Z);
}
The core implements the stated \(Z_1\ne0\) and \(Y_1\ne0\) case. Its wrapper returns infinity for an infinity input or for \(Y_1=0\). When these conditions depend on a secret state, the wrapper computes the relevant candidates and selects with masks.
Mixed affine-Jacobian addition
Let \(P=(X_1:Y_1:Z_1)\) with \(Z_1\ne0\) and let \(Q=(x_2,y_2)\) be an affine point on the same curve. Define
\[Z_1^2=A,\quad Z_1^3=B,\quad U_2=x_2A,\quad S_2=y_2B,\] \[H=U_2-X_1,\qquad R=S_2-Y_1.\]If \(H\ne 0\), then
\[X_3=R^2-H^2(X_1+U_2),\quad Y_3=R(X_1H^2-X_3)-Y_1H^3,\quad Z_3=Z_1H.\]Indeed, the affine coordinate differences are \(x_2-x_1=H/Z_1^2\) and \(y_2-y_1=R/Z_1^3\), so the secant slope is \(R/(HZ_1)\). Substituting that slope into the affine formulas and clearing denominators gives the displayed coordinates.
If \(H=0\) and \(R=0\), the input is a doubling. If \(H=0\) and \(R\ne 0\), the sum is infinity.
Incomplete formulas
A total addition routine combines the generic \(H\ne0\) formula with identity, doubling, and inverse-point cases. For secret-dependent points, compute arithmetic-safe candidates and select with masks, or use a complete formula whose hypotheses include the selected curve, coordinate model, and infinity representation. Completeness for mathematical point representatives does not extend automatically to an implementation sentinel outside the curve equation.
SageMath numeric verification
p = 17
F = GF(p)
E = EllipticCurve(F, [-3, 5])
P = E(5, 8)
Q = E(6, 4)
T = E(8, 0)
def jac_double_a_minus_3(X, Y, Z):
A = 3*(X - Z^2)*(X + Z^2)
B = 2*Y
C = B^2
D = C*X
X3 = A^2 - 2*D
Y3 = A*(D - X3) - C^2/2
Z3 = B*Z
return X3, Y3, Z3
def mixed_add_jac_affine(X1, Y1, Z1, x2, y2):
A = Z1^2
B = Z1^3
U2 = x2*A
S2 = y2*B
H = U2 - X1
R = S2 - Y1
X3 = R^2 - H^2*(X1 + U2)
Y3 = R*(X1*H^2 - X3) - Y1*H^3
Z3 = Z1*H
return X3, Y3, Z3
X3, Y3, Z3 = jac_double_a_minus_3(F(5), F(8), F(1))
MX, MY, MZ = mixed_add_jac_affine(F(5), F(8), F(1), F(6), F(4))
print(E.discriminant() != 0)
print(2*P == E(6, 13))
print((X3/Z3^2, Y3/Z3^3) == (F(6), F(13)))
print(P + Q == E(5, 9))
print((MX/MZ^2, MY/MZ^3) == (F(5), F(9)))
print(2*T == E(0))
for k in range(1, 8):
print(k, k*P)
Proof obligation
Each temporary must have a range compatible with the field API. If fe256_add returns \(<2p\) and fe256_mul accepts only canonical residues, the implementation violates the multiplication precondition, even though the field identity remains valid. Canonicalizing between operations or accepting the documented lazy ranges restores the required contract.
The field primitives used here support exact output/input aliasing, as specified in Article 05. With that contract, the displayed doubling core also supports r == p: all uses of the original X precede the first write to r->X, the original Y is saved in B before r->Y is written, and the final multiplication reads the original Z before replacing it. Partial overlap is excluded. This argument concerns this assignment schedule; each addition routine needs its own aliasing contract.
