I wish to see how Schoof's algorithm of counting points on elliptic curves over finite fields is implemented on SageMath. I have came across this answer https://math.stackexchange.com/a/3722724/1093844 which shows the steps of finding the Frobenius trace modulo small primes where the given field is $\mathbb{F}_p$ for some prime $p$. I am unable to upgrade the code to calculate the Frobenius trace modulo small primes for some arbitrary Galois Field $\mathbb{F}_{p^n}$. I want to know how to upgrade this code.
For reference here is the code that @dan_fulea wrote
The code would be:
p = 13
E = EllipticCurve(GF(p), [2, 1])
el = 5 # i will never write l for a variable
R.<x> = PolynomialRing(GF(p))
g = E.division_polynomial(el)
g1, mul1 = g.factor()[0]
K.<a> = GF(p^g1.degree(), modulus=g1)
S.<y> = PolynomialRing(K)
F.<b> = K.extension( E.defining_polynomial()(a, y, 1) )
EF = E.base_extend(F)
P = EF.point([a, b])
FrobP = EF.point([a^p, b^p])
FrobFrobP = EF.point([a^(p*p), b^(p*p)])
Q = FrobFrobP + (p % el)*P
t_mod_el = None
for k in [0..(el-1)]:
if k*FrobP == Q:
t_mod_el = k
break
print(f"t modulo {el} = {t_mod_el}")
Here are some words on the algorithm, i will also provide some lines of code.
To have a clear example to work with, consider the following one:
Let $p$ be the small prime $p=2027$. We use $q=p^2=2027^2$, then $F=\Bbb F_q$ may be realized as $\Bbb F_p[j]$ with $j^2=-1$, and let us work with the elliptic curve given by the equation:
$$ \bbox[yellow]{\qquad E\text{ over }\Bbb F_{2027^2}\ :\qquad y^2 = x^3+jx+1\ ,\qquad j^2 =-1\qquad\ .} $$ The following code initializes this curve.
We can already ask for its order:
OK, let us try to get this order. I will not implement a function in full generality, instead consider this $E$ as given, and do the job only for it. So $E$, $p$, $q$, $F$, $a_4$, $a_6$ are considered as globals in the implementation. (It should be easy to adapt the code for the needed purpose.)
We expect the order of $E$, the cardinality of the abelian group $E(F)=E(\Bbb F_q)=E(\Bbb F_{p^2})$ to be between the Hasse bounds $(q+1)\pm2\sqrt q=p^2\pm2p+1=(p\pm1)^2$. It is enough to know this cardinality modulo some primes with product bigger $4p$, the width of the interval where the order lives in. So we consider the primes $3$, $5$, $7$, $11$, $13$, having product $15015$. Let $\ell$ be one of these primes.
We do "the same" as in the code above. (And not exactly like in the original algorithm...)
Now we use ad-hoc this function to get in a lazy manner (no Chinese Reminder implemented, so no
CRT) the "defect" $t$ with $\#E(F)=(q+1)-t$:This gives:
Note that we could have used the
CRT_listfunctionality. In our case:However, we want a value in the range $[-2p, 2p]$, so we have to adjust the above representative to...
so we obtain the same $t$-defect.
Note on the implementation. Working with towers of finite fields is a nightmare in sage now. For this reason, instead of building towers by adjoining one by one $j$, then $a$ - a root of the division polynomial, then $b$ - a root of the equation $-y^2 +y^3+a_4x+a_6$ with $x$ specified to be the already known value $a$ of a possible point of $\ell$-torsion, so instead of repeatedly adjoining algebraic elements to the base field $\Bbb F_p$, it may be better to initialize a big field of the right degree, then find in it a possible realization of the numbers $j,a,b$. This is done above.
Here is an other example of an elliptic curve:
So $E$ is the curve:
$$ \bbox[yellow]{\qquad E\text{ over }\Bbb F_{13^7}\ :\qquad y^2 = x^3 +j^2 x + j+1\ ,\qquad} $$ where $j\in\Bbb F_{13^7}$ satisfies the algebraic equation: $$ j^7 + j^4 + 2 =0\ . $$ Its order is:
Doing the same as above, with a defect bound of $2\sqrt q=2\cdot 13^{7/2}\approx 15842.7923043\dots$, we need some primes with product bigger $4\sqrt q$,
and the code:
is calling the same function
get_t_modulo_ell- but please reinsert it into the code to get the new globals, and the protocol is:The computation took its time, since working for $\ell=11$ some bigger extension was needed. Again, we can use
CRT_listto get the right answer in the right interval:We take the defect $t$ from the Hasse defect interval $[-2\sqrt q\ , \ +2\sqrt q]$, which is the last number, $-13482$.