Path: blob/main/scripts/ecnf/elliptic_curve_to_ecnf_format.py
1128 views
1from sage.all import ZZ,QQ,PolynomialRing,EllipticCurve,pari2from lmfdb.WebNumberField import WebNumberField3from lmfdb.nfutils.psort import ideal_label4from scripts.ecnf.import_utils import make_curves_line56debug = False789def EllipticCurve_from_hoeij_data(line):10"""Given a line of the file "http://www.math.fsu.edu/~hoeij/files/X1N/LowDegreePlaces"11that is actually corresponding to an elliptic curve, this function returns the elliptic12curve corresponding to this13"""14Rx=PolynomialRing(QQ,'x')15x = Rx.gen(0)16Rxy = PolynomialRing(Rx,'y')17y = Rxy.gen(0)1819N=ZZ(line.split(",")[0].split()[-1])20x_rel=Rx(line.split(',')[-2][2:-4])21assert x_rel.leading_coefficient()==122y_rel=line.split(',')[-1][1:-5]23K = QQ.extension(x_rel,'x')24x = K.gen(0)2526y_rel=Rxy(y_rel).change_ring(K)27y_rel=y_rel/y_rel.leading_coefficient()28if y_rel.degree()==1:29y = - y_rel[0]30else:31#print("needing an extension!!!!")32L = K.extension(y_rel,'y')33y = L.gen(0)34K = L35#B=L.absolute_field('s')36#f1,f2 = B.structure()37#x,y=f2(x),f2(y)38r = (x**2*y-x*y+y-1)/x/(x*y-1)39s = (x*y-y+1)/x/y40b = r*s*(r-1)41c = s*(r-1)42E=EllipticCurve([1-c,-b,-b,0,0])43return N,E,K44454647def to_polredabs(K):48"""4950INPUT:5152* "K" - a number field5354OUTPUT:5556* "phi" - an isomorphism K -> L, where L = QQ['x']/f and f a polynomial such that f = polredabs(f)57"""58R = PolynomialRing(QQ,'x')59x = R.gen(0)60if K == QQ:61L = QQ.extension(x,'w')62return QQ.hom(L)63L = K.absolute_field('a')64m1 = L.structure()[1]65f = L.absolute_polynomial()66g = pari(f).polredabs(1)67g,h = g[0].sage(locals={'x':x}),g[1].lift().sage(locals={'x':x})68if debug:69print('f', f)70print('g', g)71print('h', h)72M = QQ.extension(g,'w')73m2 = L.hom([h(M.gen(0))])74return m2*m1757677def base_change(E, phi):78"""79INPUT:8081- E -- an elliptic curve82- phi -- morphism whose domain is the base ring of E8384OUTPUT:8586the elliptic curve obtained by applying phi to the coefficients of E87"""88return EllipticCurve(phi.codomain(),map(phi,E.a_invariants()))899091def EllipticCurve_polredabs_a_invariants(E,morphism=True):92"""93Input:94- E - an elliptic curve over a number field K95Output:96- [a1,a2,a3,a4,a6] - the a_invariants of E base changed along phi: K -> L97where phi is the morphism from K to its polredabs field98"""99K = E.base_field()100phi = to_polredabs(K)101ainvs = map(phi,E.a_invariants())102if morphism:103return ainvs,phi104return ainvs105#E_polred = base_change(E,phi)106#assert E.conductor().norm() == E_polred.conductor().norm()107#return E_polred108109def EllipticCurve_polredabs(E):110"""111Input:112- E - an elliptic curve over a number field K113Output:114- E1 - the elliptic curve that is the base change of E along phi: K -> L115where phi is the morphism from K to its polredabs field116"""117return EllipticCurve(EllipticCurve_polredabs_a_invariants(E,False))118119def EllipticCurve_to_ecnf_dict(E):120"""121Make the dict that should be fed to `make_curves_line` in `lmfdb/scripts/ecnf/import_utils.py`.122123It sets `iso_label`, 'a' and `number` to '1' and `cm` and `base_change` to '?'124125INPUT:126127* E - A sage elliptic curve over a number field128"""129E = EllipticCurve_polredabs(E)130K = E.base_field()131WNF = WebNumberField.from_polredabs(K.polynomial())132ainvs = [map(str,ai) for ai in map(list,E.a_invariants())]133conductor = E.conductor()134conductor_str = "".join(str([conductor.norm()]+list(conductor.gens_two())).split())135ec = {'field_label':WNF.label,136'conductor_label':ideal_label(conductor),137'iso_label':'a',138'number':'1',139'conductor_ideal':conductor_str,140'conductor_norm':str(conductor.norm()),141'ainvs':ainvs,142'cm':'?',143'base_change':'?'}144return ec145146def EllipticCurve_make_line(E):147"""148Just make_curves_line applied to EllipticCurve_to_ecnf_dict149150INPUT:151152* E - A sage elliptic curve over a number field153"""154return make_curves_line(EllipticCurve_to_ecnf_dict(E))155156def hoeij_to_ecnf(url="http://www.math.fsu.edu/~hoeij/files/X1N/LowDegreePlaces",path="./curves.hoeij"):157import urllib2158file = urllib2.urlopen(url)159data = file.read()160file.close()161lines = [l for l in data.splitlines() if l[:3] == "N =" and "[" in l]162163with open(path, 'w') as out_file:164for line in lines:165E = EllipticCurve_from_hoeij_data(line)166out_line = EllipticCurve_make_line(E[1])167out_file.write(out_line + "\n")168169170