Elliptic-curve addition formula (source code)

= Elliptic-curve addition formula

Let $E:y^2=x^3+a_2x^2+a_4x+a_6$ be a <smooth> <Weierstrass equation of an elliptic curve> over a <field> of <characteristic> different from two, with identity $O$ at infinity. For affine <points> $P,Q$ with $x(P)\ne x(Q)$, put
$$
\lambda=\frac{y(Q)-y(P)}{x(Q)-x(P)}.
$$
For $P=Q$ with $y(P)\ne0$, use the tangent <slope> $\lambda=(3x(P)^2+2a_2x(P)+a_4)/(2y(P))$. In either case,
$$
x(P+Q)=\lambda^2-a_2-x(P)-x(Q),\qquad y(P+Q)=-y(P)+\lambda\bigl(x(P)-x(P+Q)\bigr).
$$
Substitute the secant or tangent equation into the cubic: the sum of its three <intersection> $x$-coordinates is $\lambda^2-a_2$. Reflecting the third <intersection> in $y=0$ gives the sum.<Elliptic Curves by James S. Milne (2nd edition)>[Chapter II, group law and addition formulas]{ref}

The omitted cases belong to the <elliptic curve group law> on the full <projective curve>: $P+O=P$ and $P+(-P)=O$, where $-P=(x(P),-y(P))$. In particular, a <point> with $y(P)=0$ satisfies $2P=O$; its vertical tangent cannot be handled by dividing by $2y(P)$.