Advertisement
Guest User

Untitled

a guest
May 10th, 2021
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 KB | None | 0 0
  1. from itertools import combinations
  2.  
  3. def cornacchia(d, m):
  4.     assert(gcd(d,m)==1)
  5.     try:
  6.         rs = [ ZZ(x) for x in Zmod(m)(-d).sqrt(all=True) ]
  7.     except:
  8.         return None
  9.     for r in rs:
  10.         r0 = m; r1 = r
  11.         while r0^2 >= m:
  12.             r0, r1 = r1, r0 % r1
  13.         if is_square((m - r0^2)/d):
  14.             return r0, sqrt((m - r0^2)/d)
  15.     return None
  16.  
  17.  
  18. def find_curve_of_order(n):
  19.     r = 0
  20.     logn = int(n.log(2).n())
  21.     while True:
  22.         p = [ legendre_symbol(-1, p)*p for p in prime_range(max(3,r*logn), (r+1)*logn) if legendre_symbol(n, p) == 1 ]
  23.         S = [ p for p in p ]
  24.         for i in range(1, len(p)):
  25.             for L in combinations(S, i):
  26.                 D = prod(L)
  27.                 if D % 8 != 5:# or D.log(2).n() > (r * n.log(2).n())^2:
  28.                     continue
  29.                 solution = cornacchia(-D, 4*n)
  30.                 if solution is None:
  31.                     continue
  32.                 (x, y) = solution
  33.                 if is_prime(n+1+x):
  34.                     p = n+1+x
  35.                 elif is_prime(n+1-x):
  36.                     p = n+1-x
  37.                 else:
  38.                     continue
  39.                 P = hilbert_class_polynomial(D)
  40.                 roots = P.roots(ring=GF(p))
  41.                 if len(roots) > 0:
  42.                     E = EllipticCurve_from_j(roots[0][0]).change_ring(GF(p))
  43.                     if E.order() == n:
  44.                         return E
  45.                     else:
  46.                         return E.quadratic_twist()
  47.         r += 1
  48.  
  49.  
  50.  
  51. sage: time E = find_curve_of_order(2**127-1)
  52. CPU times: user 583 ms, sys: 7 ยตs, total: 583 ms
  53. Wall time: 583 ms
  54. sage: E
  55. Elliptic Curve defined by y^2 = x^3 + 29825378467770542984135446927558639468*x + 54083907264064377833823915102218955679 over Finite Field of size 170141183460469231709232497652167941199
  56. sage: E.order()
  57. 170141183460469231731687303715884105727
  58. sage: 2**127-1
  59. 170141183460469231731687303715884105727
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement