]> Programming ECC - Pell Equations

Programming ECC

Suppose we wish to solve x 2 Dy 2 =1 where D is not a square. First we compute the continued fraction expansion of D. P 0 = 0 Q 0 = 1 a 0 = (D) P 1 = a 0 Q 1 = Da 0 2 a n = a 0 +P nQ n P n = a n1 Q n1 P n1 Q n = DP n 2 Q n1

For some k we have a k+1 =2 a 0 . Then the continued fraction expansion of D is D=[a 0 a 1 ...a k+1 a 1 ...a k+1 ...]

The convergents are computed by p 0 = a 0 p 1 = a 0 a 1 +1 p n = a np n1 +p n2 q 0 = 1 q 1 = a 1 q n = a nq n1 +q n2 The convergents satisfy p n 2 Dq n 2 =(1 ) n+1 Q n+1 It turns out (x,y)=(p k,q k) is the smallest integer solution of the Pell equation for odd k, and (x,y)=(p 2 k+1 ,q 2 k+1 ) if k is even. From a minimal positive solution (t,u), we may generate the other positive solutions (x,y) via x+yD=(t+uD) n for all positive n.

Generalized Pell Equations

Now consider the equation x 2 Dy 2 =N If N 2 <D we may do the following. Compute the convergents p n,q n until the smallest integer solution of the Pell equation x 2 Dy 2 =1 is found. In the meantime, check if each p n,q n satisfies p n 2 Dq n 2 =N/f 2 for some f>0 . If so, append (fp n,fq n) to the list of solutions.

We now use this list of solutions to generate all other solutions. If (r,s) is on the list, and (t,u) is a minimal positive solution of the corresponding Pell equation, then we have a family of solutions (x,y) given by x+yD=(r+sD)(t+uD) n for all positive n.

If N 2 D then one possibility is brute force. If N>0 set L 1 =0 ,L 2 =N(t1 )/2 D), otherwise set L 1 =N/D,L 2 =N(t+1 )/(2 D). For L 1 yL 2 check if N+Dy 2 =x 2 for some integer x. If so, then append (x,y) to the list of fundamental solutions. Also append (x,y) if not equivalent to (x,y). (TODO: can avoid since we don't want negative solutions?) Then proceed as above to generate all solutions.