///////////////////////////////////////////////////////////////////////////
//
// 		"Explicit characterization of the torsion growth of
//     rational elliptic curves with complex multiplication
//              over quadratic fields"
//
// 		Enrique González-Jiménez
//
//      Magma script 
// 		
//
///////////////////////////////////////////////////////////////////////////
// Magma 2.24
//////////////////////////////////////////////////////////////////////////
load "CM_aux.m";
 ////////////////////////////////////////////////////////////////////////////////////////////////
// 
// Torsion over quadratic fields for CM and D ne -3.-4
// i.e. only quadratic twists
//
////////////////////////////////////////////////////////////////////////////////////////////////

// D =-11,-19,-43,-67,-163
// Trivial Torsion over quadratic fields for D in [-11,-19,-43,-67,-163] 
// Since there are not points of order 3 for any quadratic twist there could not be points of order 3 over quadratic fields
// since n is odd: E(Q(\sqrt{d}))[n]=E(Q)[n]+E^d(Q)[n]
 boo:=&and[&and[#Roots(DivisionPolynomial(CM_EllipticCurve(D,1),p)) eq 0 : p in [2,3]] : D in [-11,-19,-43,-67,-163]];
 assert boo;
  
///////////////////////////////////////////////
//D=-7,-28,-8 
// Torsion Z/2Z+Z/2Z over Q(\sqrt{d}) for d=-7,7,2 for D=-7,-28,-8 respectively

// Since there are not points of order 3 for any quadratic twist there could not be points of order 3 over quadratic fields
// since n is odd: E(Q(\sqrt{d}))[n]=E(Q)[n]+E^d(Q)[n]

// The torsion can only grow to Z/2Z+Z/2Z or Z/4Z  
// E[2]:
 
assert [[[ -7, -7 ]],[[ -28, 7 ]],[[ -8, 2 ]]] eq [[[D,Squarefree(Integers()!Discriminant(f[1]))] : f in Factorization(DivisionPolynomial(CM_EllipticCurve(D,1),2)) | Degree(f[1]) eq 2] : D in [-7,-28,-8]];
 
// No points of order 4 over quadratic
D:=-7; 
 E,AB:=CM_EllipticCurve(D,1);
 P4:=PrimitiveDivisionPolynomial(E,4);
 PP4:=Factorization(P4);
 Q<x>:=PolynomialRing(Rationals());
 assert P4 eq 2*(x^2 - 126*x - 5103)*(x^2 + 567)*(x^2 + 126*x + 6237); 
 assert #[f[1] : f in PP4 | Degree(f[1]) eq 1] eq 0;
 L4:=[f[1] : f in PP4 | Degree(f[1]) le 2];
 assert [7,-7,-7] eq [Squarefree(Integers()!Discriminant(f)) : f in L4];
 f:=L4[1]; 
 coe:=Coefficients(f);
 K<r7>:=QuadraticField(7);
 r:=Roots(Polynomial(K,coe))[2][1];
 assert 36*r7 + 63 eq r;
 y2:=r^3+AB[1]*r+AB[2];
 assert y2 eq (3*r7 + 8)*r7*(r7*2^2*3^3)^2;
 OK:=MaximalOrder(K);
 U,map:=UnitGroup(OK);
 3*r7 + 8 eq map(U.2);

D:=-28;
 E,AB:=CM_EllipticCurve(D,1);
 P4:=PrimitiveDivisionPolynomial(E,4);
 PP4:=Factorization(P4);
 assert #[f[1] : f in PP4 | Degree(f[1]) eq 1] eq 0;
 L4:=[f[1] : f in PP4 | Degree(f[1]) le 2];
 assert [Squarefree(Integers()!Discriminant(f)) : f in L4] eq [-7];
 f:=L4[1]; 
 coe:=Coefficients(f);
 K<r7>:=QuadraticField(-7);
 r:=Roots(Polynomial(K,coe))[2][1];
 assert r7 + 14 eq r;
 y2:=r^3+AB[1]*r+AB[2];
 assert y2 eq -7*r7*(1+r7)^4/4;

D:=-8;
 E,AB:=CM_EllipticCurve(D,1);
 P4:=PrimitiveDivisionPolynomial(E,4);
 PP4:=Factorization(P4);
 assert #[f[1] : f in PP4 | Degree(f[1]) eq 1] eq 0;
 L4:=[f[1] : f in PP4 | Degree(f[1]) le 2];
 assert [Squarefree(Integers()!Discriminant(f)) : f in L4] eq [2];
 assert 2 eq Squarefree(Integers()!Discriminant(DivisionPolynomial(CM_EllipticCurve(D,1),2)));
 
///////////////////////////////////////////////
 //D:=-16;
 D:=-16;
 // d=1,2 over Q has Torsion Z/4Z. Growth over quadratic can only be Z/2Z+Z/4Z.
 // quadratic =Q(\sqrt{2})
 for d in [1,2] do 
 	E,AB:=CM_EllipticCurve(D,d);
 	assert [2] eq [Squarefree(Integers()!Discriminant(f[1])) : f in Factorization(DivisionPolynomial(E,2)) | Degree(f[1]) eq 2];
 end for;
// d\ne 1,2 over Q has Torsion Z/2Z. Growth over quadratic can only be Z/4Z, Z/2Z+Z/2Z or Z/2Z+Z/4Z.
// Z/2Z+Z/2Z over (\sqrt{2})
	E,AB:=CM_EllipticCurve(D,1);
 	assert [2] eq [Squarefree(Integers()!Discriminant(f[1])) : f in Factorization(DivisionPolynomial(E,2)) | Degree(f[1]) eq 2];
// Study growth to Z/4Z over quadratic
 E,AB:=CM_EllipticCurve(D,1);
 P4:=PrimitiveDivisionPolynomial(E,4);
 PP4:=Factorization(P4);
 assert P4 eq 2*(x-1)*(x-3)*(x^4 + 4*x^3 - 42*x^2 + 100*x - 79);
 assert #[f[1] : f in PP4 | Degree(f[1]) eq 2] eq 0;
 R4:=[r[1] : r in Roots(P4)];
 assert [1,2] eq [Squarefree(Integers()!(r^3+AB[1]*r+AB[2])) : r in R4];
// Conclusion: Growth to Z/4Z over quadratic field Q(sqrt{d}) and Q(sqrt{2d}) since d*y^2=x^3+A*x+B

///////////////////////////////////////////////
//D:=-27;
// Torsion Z/3Z if and only if d=1.
// No points of order 2 over quadratic since 2-division polynomial is irreducible for any quadratic twist 
// Torsion growth over quadratic can only be Z/3Z or Z/3Z+Z/3Z
// d=1 there is not growth over quadratic fields since E(Q(\sqrt{t}))[3]=E(Q)[3]+E^t(Q)[3] but E^t(Q)[3] is trivial for any t\ne 1
// d\ne 1
 D:=-27;
 E,AB:=CM_EllipticCurve(D,1);
 // Study of points of order 3 over quadratic
 P3:=PrimitiveDivisionPolynomial(E,3);
 PP3:=Factorization(P3);
 assert #[f[1] : f in PP3 | Degree(f[1]) eq 2] eq 0;
 R3:=[r[1] : r in Roots(P3)];
 assert #R3 eq 1;
 r:=R3[1];
 assert r eq 12;
 assert 16 eq r^3+AB[1]*r+AB[2];
 // Conclusion: Growth to Z/3Z over quadratic field Q(sqrt{d}) since d*y^2=x^3+A*x+B
// NO Z/3Z+Z/3Z since the decomposition if P3
assert [3] eq AbelianInvariants(TorsionSubgroup(BaseChange(E,QuadraticField(-3))));

 
///////////////////////////////////////////////
//D:=-12;
 D:=-12;
// d=1 over Q has Torsion Z/6Z. Growth over quadratic can only be Z/2Z+Z/6Z.
// quadratic =Q(\sqrt{3})
 E,AB:=CM_EllipticCurve(D,1);
 assert [3] eq [Squarefree(Integers()!Discriminant(f[1])) : f in Factorization(DivisionPolynomial(E,2)) | Degree(f[1]) eq 2];
// d=3 over Q has Torsion Z/2Z. Growth over quadratic can only be Z/4Z, Z/6Z, Z/2Z+Z/2Z, Z/2Z+Z/4Z Z/2Z+Z/6Z.
// Study points of order 3
 P3:=PrimitiveDivisionPolynomial(E,3);
 PP3:=Factorization(P3);
 assert #[f[1] : f in PP3 | Degree(f[1]) eq 2] eq 0;
 R3:=[r[1] : r in Roots(P3)];
 assert [1] eq [Squarefree(Integers()!(r^3+AB[1]*r+AB[2])) : r in R3];
// Conclusion: It has E(K)[3]=Z/3Z over the quadratic field K=Q(sqrt{d}) since d*y^2=x^3+A*x+B
// NO Z/3Z+Z/3Z since the decomposition if P3


//d:=3
assert [2,6] eq AbelianInvariants(TorsionSubgroup(BaseChange(CM_EllipticCurve(D,3),QuadraticField(3))));
// Conclusion: For d=3 Growth to Z/2Z+Z/6Z over Q(\sqrt{3})

// Study points of order 4
 P4:=PrimitiveDivisionPolynomial(E,4);
 PP4:=Factorization(P4);
 assert #[f[1] : f in PP4 | Degree(f[1]) eq 1] eq 0;
 L4:=[f[1] : f in PP4 | Degree(f[1]) le 2];
 assert #L4 eq 1;
 f:=L4[1];
 assert Squarefree(Integers()!Discriminant(f)) eq -3;
 coe:=Coefficients(f);
 K<r3>:=QuadraticField(-3);
 r:=Roots(Polynomial(K,coe))[2][1];
 assert r3 + 2 eq r;
 y2:=r^3+AB[1]*r+AB[2];
 assert y2 eq (-r3)*(3-r3)^2;

// No points of order 4 over quadratic fields


// d\ne 1,3 
// Torsion growth to Z/2Z+Z/6Z over Q(\sqrt{d})
// Torsion growth to Z/2Z+Z/2Z over Q(\sqrt{3}) since Discriminant(Ed)=2^8*3^3*d^6=3*square  
  


////////////////////////////////////////////////////////////////////////////////////////////////
// 
// Torsion over quadratic fields for CM and D=-4
//
// Cases y^2=x^3+d^2*x and y^2=x^3-d^2*x -->quadratic twists
//
////////////////////////////////////////////////////////////////////////////////////////////////


// Cases y^2=x^3+d^2*x
// d=2,-2
// Torsion Z/4Z over Q
// Torsion growth over quadratic only to Z/2Z+Z/4Z over Q(Sqrt{Discriminant(E)})=Q(\sqrt{-1})
E:=EllipticCurve([4,0]);
assert -1 eq Squarefree(Integers()!Discriminant(E)); 
assert [2,4] eq AbelianInvariants(TorsionSubgroup(BaseChange(E,QuadraticField(-1))));

// d\ne 2,-2
// Torsion Z/2Z over Q
// Torsion growth possibilities: Z/2Z+Z/2Z,Z/4Z,Z/6Z,Z/2Z+Z/6Z
// No points of order 3 over quadratic since E(Q(\sqrt{t}))[3]=E(Q)[3]+E^t(Q)[3] but E^t(Q)[3] is trivial for any t
// Z/2Z+Z/2Z subgroups of the torsion of E over Q(Sqrt{Discriminant(E)})=Q(\sqrt{-d})
// Study points of order 4:
 E:=EllipticCurve([1,0]);
 P4:=PrimitiveDivisionPolynomial(E,4);
 assert P4 eq 2*(x^2-1)*(x^4+6*x^2+1);
 PP4:=Factorization(P4);
 assert #[f[1] : f in PP4 | Degree(f[1]) eq 2] eq 0;
 R4:=[r[1] : r in Roots(P4)];
 assert [-1,1] eq R4;
 assert [-2,2] eq [Squarefree(Integers()!(r^3+r)) : r in R4];
// Conclusion: Growth to Z/4Z over quadratic field Q(sqrt{2*d}) and Q(sqrt{-2d}) since d*y^2=x^3+A*x+B

////////////////////////////////////////////////////////////////////////////////////////////////

// Cases y^2=x^3-d^2*x
E:=EllipticCurve([-1,0]); // Z/2Z+Z/2Z
// Torsion growth possibilities: Z/2Z+Z/4Z,Z/2Z+Z/6Z
// No points of order 3 over quadratic since E(Q(\sqrt{t}))[3]=E(Q)[3]+E^t(Q)[3] but E^t(Q)[3] is trivial for any t
// Study points of order 4:
 P4:=PrimitiveDivisionPolynomial(E,4);
 PP4:=Factorization(P4);
 assert #[f[1] : f in PP4 | Degree(f[1]) eq 1] eq 0;
 L4:=[f[1] : f in PP4 | Degree(f[1]) le 2];
 assert #L4 eq 3;

 f:=L4[1];
 coe:=Coefficients(f);
 K<r2>:=QuadraticField(2);
 r:=Roots(Polynomial(K,coe))[2][1];
 assert r2 + 1 eq r;
 y2:=r^3-r;
 assert y2 eq (r2+2)^2;
  
 //K<r>:=NumberField(f);
 //dd:=Squarefree(Integers()!Discriminant(K));
 //assert dd eq 2;
 //assert (r^3-r)/(r+1)^2 eq 1; 
 // Conclusion: point of order 4 only if d=1,2
 
 f:=L4[2];
 coe:=Coefficients(f);
 K<r1>:=QuadraticField(-1);
 r:=Roots(Polynomial(K,coe))[2][1];
 assert r eq r1;
 y2:=r^3-r;
 assert y2 eq (r1-1)^2; 
 // Conclusion: point of order 4 only if d=-1,1

 f:=L4[3];
 coe:=Coefficients(f);
 K<r2>:=QuadraticField(2);
 r:=Roots(Polynomial(K,coe))[2][1];
 assert r2 - 1 eq r;
 y2:=r^3-r;
 assert y2 eq -((r2 - 1)*r2)^2; 
 
 //K<r>:=NumberField(f);
 //dd:=Squarefree(Integers()!Discriminant(K));
 //assert dd eq 2;
 //assert (r^3-r)/(r*(r+1))^2 eq -1; 
 // Conclusion: point of order 4 only if d=-1,-2 
 
 // Conclusion: 
 // d=1,-1 (y^2=x^3-x): Z/2Z+Z/4Z in Q(\sqrt{-1}) and Q(\sqrt{2})
 // d=2,-2 (y^2=x^3-4x): Z/2Z+Z/4Z in Q(\sqrt{2})
 // d\ne 1,-1,2,-2: no points of order 4 over quadratics
 
 ////////////////////////////////////////////////////////////////////////////////////////////////
// 
// Torsion over quadratic fields for CM and D=-4
//
// Cases y^2=x^3+d*x -->No quadratic twists (d \ne \pm k^2)
//
////////////////////////////////////////////////////////////////////////////////////////////////

_<d>:=RationalFunctionField(Rationals());
E:=EllipticCurve([d,0]);
// Since d\ne \pm k^2 the torsion over Q is Z/2Z
// Study case Z/2Z+Z/2Z Torsion growth over Q(\sqrt{-d})
assert Discriminant(E) eq -d*(8*d)^2;
// Conclusion: Z/2Z+Z/2Z is a subgroup of the torsion over Q(\sqrt{-d})
// Torsion growth Possibilities: Z/4Z, Z/6Z, Z/2Z+Z/2Z, Z/2Z+Z/4Z, Z/2Z+Z/6Z
// No points of order 3 over quadratic since E(Q(\sqrt{t}))[3]=E(Q)[3]+E^t(Q)[3] but E^t(Q)[3] is trivial for any t
// Study points of order 4:
 P4:=PrimitiveDivisionPolynomial(E,4);
 PP4:=Factorization(P4);
/* BY HAND:
 P4=2*(x^2 - d)*(x^4 + 6*d*x^2 + d^2)
 --> x=sqrt{d}--> x^3+d*x=2*sqrt{d}^3=y^2? NO. 2*sqrt{d}^3 is never an square over Q(sqrt{d}).	
 --> z=x^2 and z^4 + 6*d*z^2 + d^2=0 --> z=d*(-3+\pm 2*sqrt{2})=x^2? Impossible over Q(sqrt{2})
*/
// Conclusion: No points of order 4 over quadratic fields for y^2=x^3+d*x if \pm d is a non-square.



////////////////////////////////////////////////////////////////////////////////////////////////
// 
// Torsion over quadratic fields for CM and D=-3
//
// Cases y^2=x^3+d^3 -->quadratic twists, d squarefree
//
////////////////////////////////////////////////////////////////////////////////////////////////

// y^2=x^3+d^3
// d=1: Torsion over Q is Z/6Z
E:=EllipticCurve([0,1]);
// Possibilities; Z/2Z+Z/6Z over Q(sqrt(Delta(E)))
assert -3 eq Squarefree(Integers()!Discriminant(E));
// Torsion Growth Z/2Z+Z/6Z over Q(sqrt(-3))

// d\ne 1: Torsion over Q is Z/2Z
// Possibilities; Z/4Z, Z/6Z, Z/2Z+Z/2Z,Z/2Z+Z/4Z and Z/2Z+Z/6Z over Q(sqrt(Delta(E)))
E:=EllipticCurve([0,1]);
// Study case Z/2Z+Z/2Z Torsion growth over Q(Delta(E))
// Conclusion: Z/2Z+Z/2Z is a subgroup of the torsion over Q(\sqrt{-3})
assert -3 eq Squarefree(Integers()!Discriminant(E));
// Study points of order 3 over quadratics
 P3:=PrimitiveDivisionPolynomial(E,3);
 assert P3 eq 3*x*(x^3 + 4);
 PP3:=Factorization(P3);
 assert #[f[1] : f in PP3 | Degree(f[1]) eq 2] eq 0;
 R3:=[r[1] : r in Roots(P3)];
 assert [1] eq [Squarefree(Integers()!(r^3+1)) : r in R3];
// Conclusion: Order 3 over the quadratic field Q(sqrt{d}) since d*y^2=x^3+1.
// If d=-3, then torsion growth Z/2Z+Z/6Z over Q(sqrt(-3))
// If d\ne -3 then torsion growth Z/2Z+Z/2Z over Q(sqrt(-3)) and Z/6Z over Q(sqrt(d))

// Study points of order 4 over quadratics
 P4:=PrimitiveDivisionPolynomial(E,4);
 PP4:=Factorization(P4);
 assert #[f[1] : f in PP4 | Degree(f[1]) eq 1] eq 0;
 assert #[f[1] : f in PP4 | Degree(f[1]) eq 2] eq 1;
 f:=[f[1] : f in PP4 | Degree(f[1]) eq 2][1];
 assert f eq x^2 + 2*x - 2;
 K<r>:=NumberField(f);
 assert 3 eq Squarefree(Integers()!Discriminant(K));
 coe:=Coefficients(f);
 K<r3>:=QuadraticField(3);
 r:=Roots(Polynomial(K,coe))[2][1];
 assert r3 - 1 eq r;
 y2:=r^3+1;
 u:=(2-r3);
 assert y2 eq 3*r3*u;
 
 OK:=MaximalOrder(K);
 U,map:=UnitGroup(OK);
 assert Inverse(map)(u) eq U.2;
 assert [2,0] eq AbelianInvariants(U);
 //Conclusion: No pints of order 4 since r^3+1 \ne d*y^2 for any d,y \in Q(sqrt(3))  
 
 //CONCLUSION Torsion growth over quadratic field for y^2=x^3+d^3:
 // d=1--> Z/2Z+Z/6Z over Q(sqrt(-3))
 // d=-3 --> Z/2Z+Z/6Z over Q(sqrt(-3))
 // d\ne 1,-3 --> Z/2Z+Z/2Z over Q(sqrt(-3)) and Z/6Z over Q(sqrt(d))
 
 
 ////////////////////////////////////////////////////////////////////////////////////////////////
// 
// Torsion over quadratic fields for CM and D=-3
//
// Cases y^2=x^3+d^2 --> d cubefree
//
////////////////////////////////////////////////////////////////////////////////////////////////

// y^2=x^3+d^2
_<d>:=RationalFunctionField(Rationals());
E:=EllipticCurve([0,d^2]);
// Since d\ne 1 the torsion over Q is Z/3Z
// Possibilities of Torsion growth over quadratic: Z/6Z, Z/2Z+Z/6Z or Z/3Z+Z/3Z
// Z/2Z is not subgroup of the torsion subgroup over quadratic fields since x^3+d^2 is irreducible over Q for any d\ne 1
// Conclusion: No EVEN torsion growth over quadratic fields 

// Study Z/3Z+Z/3Z. Only possible over Q(sqrt(-3))
 P3:=PrimitiveDivisionPolynomial(E,3);
 PP3:=Factorization(P3);
 /* BY HAND
 P3=3*x*(x^3+4*d^2)
 --> x^3+4*d^2=0 --> x=(-4*d^2)^(1/3) over a quadratic field --> d=4*r^3, but since d is cube free d=4
 */
 E:=EllipticCurve([0,4^2]);
 assert [3,3] eq AbelianInvariants(TorsionSubgroup(BaseChange(E,QuadraticField(-3))));
 // Conclusion: No torsion growth over quadratic fields except if d=4 and then Z/3Z+Z/3Z over Q(sqrt(-3))

 ////////////////////////////////////////////////////////////////////////////////////////////////
// 
// Torsion over quadratic fields for CM and D=-3
//
// Cases y^2=x^3-432 
//
////////////////////////////////////////////////////////////////////////////////////////////////

E:=EllipticCurve([0,-432]);
// Torsion over Q is Z/3Z
// Possibilities of Torsion growth over quadratic: Z/6Z, Z/2Z+Z/6Z or Z/3Z+Z/3Z
// Z/2Z is not subgroup of the torsion subgroup over quadratic fields since x^3+d^2 is irreducible over Q for any d\ne 1
// Conclusion: No EVEN torsion growth over quadratic fields 
// Study Z/3Z+Z/3Z. Only possible over Q(sqrt(-3))
 assert [3,3] eq AbelianInvariants(TorsionSubgroup(BaseChange(E,QuadraticField(-3))));
 // Conclusion: Torsion growth Z/3Z+Z/3Z over Q(sqrt(-3))


////////////////////////////////////////////////////////////////////////////////////////////////
// 
// Torsion over quadratic fields for CM and D=-3
//
// Cases y^2=x^3+d --> d sixthpowerfree (Generic case)
//
// d\ne 1,-432,r^2,r^3 for any r
//
////////////////////////////////////////////////////////////////////////////////////////////////

// y^2=x^3+d
_<d>:=RationalFunctionField(Rationals());
E:=EllipticCurve([0,d]);
// Trivial Torsion over Q
// No even torsion over quadratic fields since x^3+d is irreducible over Q then over quadratic fields
// Possibilities of Torsion growth over quadratic: Z/3Z or Z/3Z+Z/3Z
 P3:=PrimitiveDivisionPolynomial(E,3);
 PP3:=Factorization(P3);
  /* BY HAND
 P3=3*x*(x^3+4*d)
 --> x=0 --> y^2=d --> Point of order 3 over Q(sqrt(d))
 --> x^3+4*d=0 --> x=-(4*d)^(1/3) over a quadratic field --> d=2*r^3
 ------->  y^2=x^3+d=x^3+4*d-3*d=-3*d -> y=sqrt(-3*d)=sqrt(-3*2*r^3) --> Points of order 3 over Q(sqrt(-6*r)) with d=2*r^3
 */
 // Conclusion: 
 // d=2*r^3 (r\ne 2,-6 since in that case 16, -432) 
 // Torsion growth Z/3Z over Q(sqrt(2*r)) and Q(sqrt(-6*r))
 // NO Z/3Z+Z/3Z since 2*r and -6*r are never -3 (r \ne -6,2 resp.)
 
 // d \ne 2*r^3 then Torsion growth Z/3Z over Q(sqrt(d))

