//////////////////////////////////////////////////////////////////
// This code determines the rational points on X619
//
// See the folder "X619-additional_code" for the code that 
// precomputes equations for the maps E --> P1
//////////////////////////////////////////////////////////////////

// X619 is (-2705*x^2 + 1681*y^2 - 1967*y*z + 2048*z^2)*(-2*x^2 + y^2 - 2*y*z + 2*z^2) - (73*x^2 - 41*y^2 + 64*y*z - 64*z^2)^2

   P2<x,y,z> := ProjectiveSpace(Rationals(),2);        
   Q := [
      -2705*x^2 + 1681*y^2 - 1967*y*z + 2048*z^2 ,
      73*x^2 - 41*y^2 + 64*y*z - 64*z^2 ,
      -2*x^2 + y^2 - 2*y*z + 2*z^2 ];

   C := Curve(P2,Q[1]*Q[3]-Q[2]^2);

   P4<u,v,w,r,s> := ProjectiveSpace(Rationals(),4);
   D := [Curve(P4,[Evaluate(Q[1],[u,v,w]) -d*r^2, Evaluate(Q[2],[u,v,w]) 
               -d*r*s, Evaluate(Q[3],[u,v,w]) -d*s^2]) : d in [1,-1,2,-2]];
   
   
// Check for 2-adic points 
    for d in D do  IsEmpty(d(pAdicField(2,20))); end for; 
    // d = 1,-1,2 have 2 adic points (and global points)
    
   // Can check which twists lift global points as follows: 
     // pts :=     RationalPoints(C : Bound := 100);
     // (Integers()!Evaluate(Q[3],[pts[5][i] : i in [1..3]]));
  
  //////////////////////////////////////////////////////////////////
  // Automorphisms of D_d
  //////////////////////////////////////////////////////////////////

  P<t> := PolynomialRing(Rationals());
    K<a> :=  NumberField((t^2- 2)^2 -2);
    PK<s> := PolynomialRing(K);
    DK := [BaseExtend(d,K) : d in D];  
    P4K<u,v,w,r,s> := AmbientSpace(DK[1]);

    // Precomputed the following via the script auto4.txt
//    aut1 := map<DK[1] -> DK[1] | [u,-v,-w,r,s]>;
//    aut2 := map<DK[2] -> DK[2] | 
//      [u,
//       v*(1/2)*(z^2-2) - w,
//       v*(-1/2) + w*(1/2)*(-z^2+2),
//       r*(1/18)*(-73*z^3+228*z) + s*(1/18)*(-2624*z^3 + 8529*z), 
//       r*(1/9)*(z^3-3*z) + s*(1/18)*(73*z^3-228*z)        ]>
//      where z is a^2;
  
  
     
  //////////////////////////////////////////////////////////////////
  // Run elliptic Chabauty on the map E_d --> P^1
  //////////////////////////////////////////////////////////////////
  P<t> := PolynomialRing(Rationals());
   K<a> :=  NumberField((t^2- 2)^2 -2);
   PK<s> := PolynomialRing(K);
   P1 := ProjectiveSpace(Rationals(),1);  
   OK := Integers(K);
   
 // elliptic chabauty 
   // d = 1 // to do
   e2<x,y,z>:=   EllipticCurve([0,a^3 + 1, 0, 194*a^3 + 153*a^2 - 660*a - 509, -1815*a^3 - 1389*a^2 + 6202*a + 4747]);     
    eqns2 := [z*(x + (5*a^3 + 4*a^2 - 16*a - 13)*z), (2*a^2 - 1)*x^2 + (6*a^3 + 2*a^2 - 8*a - 2)*x*z + (- 2*a^3 + 5*a^3 + 5*a^2 + (2*a+7))*z^2];
    xmap2 := Extend(map<e2 -> P1 | eqns2>);    
    success,Gk,GtoEK := PseudoMordellWeilGroup(e2);
   
    // Elliptic Chabauty
    //  N, V, R, C :=   Chabauty(GtoEK, xmap2); 
    N, V, R, C :=   Chabauty(GtoEK, xmap2, 3); N;

    // apres nils
      theta := ChabautyEquations(Identity(e2),xmap2,GtoEK,Support(3*OK));
      P<X,Y> := Parent(theta[1]);
      pr := 3;
  
    // Finish the analysis
    PrintToPrecision(theta[1],pr);"";PrintToPrecision(theta[2],pr);"";PrintToPrecision(theta[3],pr);
    //O(3^7) + O(3^7)*X + O(3^7)*Y + (107*3^2 + O(3^7))*X^2 - (26*3^2 + O(3^7))*X*Y - (22*3^3 + O(3^7))*Y^2 + O(3^7)*X^3 + O(3^7)*X^2*Y + O(3^7)*X*Y^2 + O  (3  ^7)*Y^3
    //O(3^7) + O(3^7)*X + O(3^7)*Y - (41*3^2 + O(3^7))*X^2 + (8*3^3 + O(3^7))*X*Y + (113*3^2 + O(3^7))*Y^2 + O(3^7)*X^3 + O(3^7)*X^2*Y + O(3^7)*X*Y^2 + O(  3^  7)*Y^3
    //O(3^7) + O(3^7)*X + O(3^7)*Y + (26*3^3 + O(3^7))*X^2 + (10*3^2 + O(3^7))*X*Y + (104*3^2 + O(3^7))*Y^2 + O(3^7)*X^3 + O(3^7)*X^2*Y + O(3^7)*X*Y^2 + O  (3  ^7)*Y^3
    
    // You can see that the equations are not linearly independent mod (X,Y)^2, which is what was causing problems in Nils method.                
    // Linear algebra mod (X,Y)^3 gives a unique adic solution

    // Mod 3^3, the equations have the form 
    //   (107*3^2)*X^2 - (26*3^2)*X*Y - (22*3^3)*Y^2 = 0
    //   - (41*3^2)*X^2 + (8*3^3)*X*Y + (113*3^2)*Y^2 = 0
    //   (26*3^3)*X^2 + (10*3^2)*X*Y + (104*3^2)*Y^2 = 0
  
    // Factoring out the 3^2 gives the matrix

    // Matrix(FiniteField(3),3,[
    //    107, 26, 0,
    //    - 41, 0, 113,
    //    0, 10, 104]); 

    // By Lemma 4.1, part 2, of Nils Bruin's "Chabauty methods for elliptic curves" paper, it follows that the system of power series has only the trivial solution (since the matrix above has rank 1, the projective variety in the hypothesis of that lemma has no points mod p).


    
      
        
   // d = -1 // todo, modify the equations
   e2<x,y,z>:=   EllipticCurve([0,(a^3 + a^2 + a + 1),0, (4*a^3 + 8*a^2 + 6*a - 11), (-3*a^3 + 29*a^2 + 11*a - 27)]   );  

    eqns2 := [z*(x + (5*a^3 + 4*a^2 - 16*a - 13)*z), (2*a^2 - 1)*x^2 + (6*a^3 + 2*a^2 - 8*a - 2)*x*z + (- 2*a^3 + 5*a^3 + 5*a^2 + (2*a+7))*z^2];      
    xmap2 := Extend(map<e2 -> P1 | eqns2>);    
    success,Gk,GtoEK := PseudoMordellWeilGroup(e2);
   
    // Elliptic Chabauty
    //  N, V, R, C :=   Chabauty(GtoEK, xmap2); 
    N, V, R, C :=   Chabauty(GtoEK, xmap2, 3); N;

    // apres nils
      theta := ChabautyEquations(Identity(e2),xmap2,GtoEK,Support(3*OK));
      P<X,Y> := Parent(theta[1]);
      pr := 3;
  
    // Finish the analysis
    PrintToPrecision(theta[1],pr);"";PrintToPrecision(theta[2],pr);"";PrintToPrecision(theta[3],pr);
    //O(3^7) + O(3^7)*X + O(3^7)*Y + (107*3^2 + O(3^7))*X^2 - (26*3^2 + O(3^7))*X*Y - (22*3^3 + O(3^7))*Y^2 + O(3^7)*X^3 + O(3^7)*X^2*Y + O(3^7)*X*Y^2 + O  (3  ^7)*Y^3
    //O(3^7) + O(3^7)*X + O(3^7)*Y - (41*3^2 + O(3^7))*X^2 + (8*3^3 + O(3^7))*X*Y + (113*3^2 + O(3^7))*Y^2 + O(3^7)*X^3 + O(3^7)*X^2*Y + O(3^7)*X*Y^2 + O(  3^  7)*Y^3
    //O(3^7) + O(3^7)*X + O(3^7)*Y + (26*3^3 + O(3^7))*X^2 + (10*3^2 + O(3^7))*X*Y + (104*3^2 + O(3^7))*Y^2 + O(3^7)*X^3 + O(3^7)*X^2*Y + O(3^7)*X*Y^2 + O  (3  ^7)*Y^3

    // You can see that the equations are not linearly independent mod (X,Y)^2, which is what was causing problems in Nils method.                
    // Linear algebra mod (X,Y)^3 gives a unique adic solution
    
   // d = 2
   // Same E and same map as d = 1
   e2<x,y,z>:=   EllipticCurve([0,a^3 + 1, 0, 194*a^3 + 153*a^2 - 660*a - 509, -1815*a^3 - 1389*a^2 + 6202*a + 4747]);  
    eqns2 := [z*(x + (5*a^3 + 4*a^2 - 16*a - 13)*z), (2*a^2 - 1)*x^2 + (6*a^3 + 2*a^2 - 8*a - 2)*x*z + (- 2*a^3 + 5*a^3 + 5*a^2 + (2*a+7))*z^2];
    xmap2 := Extend(map<e2 -> P1 | eqns2>);    
    success,Gk,GtoEK := PseudoMordellWeilGroup(e2);
   
    // Elliptic Chabauty
    //  N, V, R, C :=   Chabauty(GtoEK, xmap2); 
    N, V, R, C :=   Chabauty(GtoEK, xmap2, 3); N;

    // apres nils
      theta := ChabautyEquations(Identity(e2),xmap2,GtoEK,Support(3*OK));
      P<X,Y> := Parent(theta[1]);
      pr := 3;
  
    // Finish the analysis
    PrintToPrecision(theta[1],pr);"";PrintToPrecision(theta[2],pr);"";PrintToPrecision(theta[3],pr);
    //O(3^7) + O(3^7)*X + O(3^7)*Y + (107*3^2 + O(3^7))*X^2 - (26*3^2 + O(3^7))*X*Y - (22*3^3 + O(3^7))*Y^2 + O(3^7)*X^3 + O(3^7)*X^2*Y + O(3^7)*X*Y^2 + O  (3  ^7)*Y^3
    //O(3^7) + O(3^7)*X + O(3^7)*Y - (41*3^2 + O(3^7))*X^2 + (8*3^3 + O(3^7))*X*Y + (113*3^2 + O(3^7))*Y^2 + O(3^7)*X^3 + O(3^7)*X^2*Y + O(3^7)*X*Y^2 + O(  3^  7)*Y^3
    //O(3^7) + O(3^7)*X + O(3^7)*Y + (26*3^3 + O(3^7))*X^2 + (10*3^2 + O(3^7))*X*Y + (104*3^2 + O(3^7))*Y^2 + O(3^7)*X^3 + O(3^7)*X^2*Y + O(3^7)*X*Y^2 + O  (3  ^7)*Y^3
    
    // You can see that the equations are not linearly independent mod (X,Y)^2, which is what was causing problems in Nils method.                
    // Linear algebra mod (X,Y)^3 gives a unique adic solution
    
    
    