Advertisement
saleks28

EC4

Jan 16th, 2020
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None | 0 0
  1. from random import randint
  2.  
  3. #Check if h is between 30 and 70
  4. def CheckClassNumber(D):
  5.     h = QuadraticField(D).class_number()
  6.     if (h in Primes()) and h > 30 and h < 70:
  7.         return h
  8.     else:
  9.         return 0
  10.  
  11. #Generate degrees of isogeny and j_invariant
  12. def GetDegreesAndJ(D, K):
  13.     j = hilbert_class_polynomial(D).roots(ring = K, multiplicities = False)
  14.     if j == []:
  15.         return 0, [], 0
  16.     else:
  17.         isogenyDegrees = list()
  18.         for pr in Primes():
  19.             if kronecker_symbol(D, pr) == 1:
  20.                 isogenyDegrees.append(pr)
  21.             if len(isogenyDegrees) == 5:
  22.                 return j[0], isogenyDegrees, 1
  23.  
  24. #Elliptic Curve generation
  25. def GenerateEC():
  26.     while 1:
  27.         p = next_prime(randint(10**4, 10**5))
  28.         K = GF(p)
  29.         A, B = randint(1, p), randint(1, p)
  30.         p, A, B = 53831, 20363, 31029
  31.         K = GF(p)
  32.         E = EllipticCurve(K, [A, B])
  33.         T = E.trace_of_frobenius()
  34.         D = T**2 - 4*p
  35.         h = CheckClassNumber(D)
  36.         if h == 0:
  37.             continue
  38.         else:
  39.             j, isogenyDegrees, flag = GetDegreesAndJ(D, K)
  40.             if flag != 1:
  41.                 continue
  42.             else:
  43.                 return E, isogenyDegrees, j
  44. #
  45. def GetCurveInvariant(E, j, degree):
  46.     PHI = ClassicalModularPolynomialDatabase()
  47.     K = GF(E.base_field().characteristic())
  48.     phi = (PHI[degree]).change_ring(K)
  49.     PR.<x> = PolynomialRing(K)
  50.     curveInvariants = list()
  51.     curveInvariants.append(j)
  52.     buf = j
  53.     while 1:
  54.         root = phi(buf, x).roots(ring = K, multiplicities = False)
  55.         if curveInvariants.count(root[0]) != 0:
  56.             if curveInvariants.count(root[1]) != 0:
  57.                 break
  58.             else:
  59.                 curveInvariants.append(root[1])
  60.                 buf = root[1]
  61.         else:
  62.             curveInvariants.append(root[0])
  63.             buf = root[0]
  64.     return curveInvariants
  65.  
  66.  
  67. #Printing 5 graps
  68. def PrintGraphs(E, isogenyDegrees, j):
  69.     bufGraph = Graph()
  70.     commonGraph = Graph()
  71.     for degree in isogenyDegrees:
  72.         isogenyCurveInvariants = GetCurveInvariant(E, j, degree)
  73.         print("For degree {} invariants are next: {} " . format(degree, isogenyCurveInvariants))
  74.         buf = isogenyCurveInvariants[len(isogenyCurveInvariants) - 1]
  75.         for j_invariant in isogenyCurveInvariants:
  76.             bufGraph.add_edge(buf, j_invariant)
  77.             commonGraph.add_edge(buf, j_invariant)
  78.             buf = j_invariant
  79.         graph = bufGraph.graphplot(vertex_labels = True, vertex_size = 1200, layout = 'circular', vertex_color = 'white')
  80.         graph.show(figsize=[12,12])
  81.     print("Last one: ")
  82.     graph = commonGraph.graphplot(vertex_labels = True, vertex_size = 1200, layout = 'circular', vertex_color = 'white')
  83.     graph.show(figsize=[12,12])
  84.  
  85.  
  86.  
  87.  
  88.  
  89. #main
  90. E, isogenyDegrees, j = GenerateEC()
  91. print("E: {}" . format(E))
  92. print("Degrees: {}" . format(isogenyDegrees))
  93. print("j-invariant: {}" . format(j))
  94. PrintGraphs(E, isogenyDegrees, j)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement