Algebraic Solution for Point Doubling

This section provides an algebraic solution for calculating the addition operation of two points at the same location on an elliptic curve.

Case 3: P and Q are the same point. This case is also called "Point Doubling", because the addition can be expressed as P + P (or 2P):

```If:
P = Q

Then:
P + Q = P + P = 2P

Let m be the slope of the straight line passing through P,
the line can be expressed as the following equation:
y = m(x - xP) + yP

The other intersection, -R, of the line and the curve must satisfy
the line equation and the curve equation:
-yR = m(xR - xP) + yP          (1)
(-yR)2 = (xR)3 + a(xR) + b     (2)

Note that -yR in equation (2) can be replaced by the right
hand side of equation (1):
(m(xR - xP) + yP)2 = (xR)3 + a(xR) + b

Regrouping terms will result a cubic equation for xR:
(xR)3 - m2(xR)2 + (a - 2m(yP) + 2m2(xP))(xR) + b - (m(xP)-yP)2 = 0

We know that a cubic equation has 3 roots and their sum is the negation
of the coefficient of the second term based on Vieta's first formula,
see Vieta's formulas at wikipedia.org:
xP + xP + xR = m2              (3)

Note that xP is used twice, because it is a double root.

Rearrange terms of equations (1) and (3) gives us the simplest way
to calculate, R:
xR = m2 - 2xP                  (4)
yR = m(xP - xR) - yP           (5)

The slope of the line, m, can be calculated by taking the first
derivative of the elliptic curve equation, because the line must
be tangent to the curve:
3(xP)2 + a
m = ---------                 (6)
2(yP)
```

Now we can calculate R = 2P with equations (4), (5) and (6) with any given point, P, except that yP is not 0.

If yP, then R = (∞, ∞), because the other intersection is the infinity point.

By the way, proof of Vieta's formulas can be found in "Cubic Equations" by Arkajyoti Banerjee and Ayush G Rai at https://brilliant.org/wiki/cubic-equations/