PageRenderTime 72ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/sage/schemes/elliptic_curves/ell_point.py

https://bitbucket.org/wcauchois/sage-geom
Python | 2699 lines | 2510 code | 35 blank | 154 comment | 32 complexity | c60c51a2fbf3b4d4881d598e919b0bcd MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. # -*- coding: utf-8 -*-
  2. r"""
  3. Points on elliptic curves
  4. The base class ``EllipticCurvePoint_field``, derived from
  5. ``AdditiveGroupElement``, provides support for points on elliptic
  6. curves defined over general fields. The derived classes
  7. ``EllipticCurvePoint_number_field`` and
  8. ``EllipticCurvePoint_finite_field`` provide further support for point
  9. on curves defined over number fields (including the rational field
  10. `\QQ`) and over finite fields. Although there is no special
  11. class for points over `\QQ`, there is currently greater
  12. functionality implemented over `\QQ` than over other number
  13. fields.
  14. The class ``EllipticCurvePoint``, which is based on
  15. ``SchemeMorphism_projective_coordinates_ring``, currently has little
  16. extra functionality.
  17. EXAMPLES:
  18. An example over `\QQ`::
  19. sage: E = EllipticCurve('389a1')
  20. sage: P = E(-1,1); P
  21. (-1 : 1 : 1)
  22. sage: Q = E(0,-1); Q
  23. (0 : -1 : 1)
  24. sage: P+Q
  25. (4 : 8 : 1)
  26. sage: P-Q
  27. (1 : 0 : 1)
  28. sage: 3*P-5*Q
  29. (328/361 : -2800/6859 : 1)
  30. An example over a number field::
  31. sage: K.<i> = QuadraticField(-1)
  32. sage: E = EllipticCurve(K,[1,0,0,0,-1])
  33. sage: P = E(0,i); P
  34. (0 : i : 1)
  35. sage: P.order()
  36. +Infinity
  37. sage: 101*P-100*P==P
  38. True
  39. An example over a finite field::
  40. sage: K.<a> = GF(101^3)
  41. sage: E = EllipticCurve(K,[1,0,0,0,-1])
  42. sage: P = E(40*a^2 + 69*a + 84 , 58*a^2 + 73*a + 45)
  43. sage: P.order()
  44. 1032210
  45. sage: E.cardinality()
  46. 1032210
  47. Arithmetic with a point over an extension of a finite field::
  48. sage: k.<a> = GF(5^2)
  49. sage: E = EllipticCurve(k,[1,0]); E
  50. Elliptic Curve defined by y^2 = x^3 + x over Finite Field in a of size 5^2
  51. sage: P = E([a,2*a+4])
  52. sage: 5*P
  53. (2*a + 3 : 2*a : 1)
  54. sage: P*5
  55. (2*a + 3 : 2*a : 1)
  56. sage: P + P + P + P + P
  57. (2*a + 3 : 2*a : 1)
  58. ::
  59. sage: F = Zmod(3)
  60. sage: E = EllipticCurve(F,[1,0]);
  61. sage: P = E([2,1])
  62. sage: import sys
  63. sage: n = sys.maxint
  64. sage: P*(n+1)-P*n == P
  65. True
  66. Arithmetic over `\ZZ/N\ZZ` with composite `N` is supported. When an
  67. operation tries to invert a non-invertible element, a
  68. ZeroDivisionError is raised and a factorization of the modulus appears
  69. in the error message::
  70. sage: N = 1715761513
  71. sage: E = EllipticCurve(Integers(N),[3,-13])
  72. sage: P = E(2,1)
  73. sage: LCM([2..60])*P
  74. Traceback (most recent call last):
  75. ...
  76. ZeroDivisionError: Inverse of 1520944668 does not exist (characteristic = 1715761513 = 26927*63719)
  77. AUTHORS:
  78. - William Stein (2005) -- Initial version
  79. - Robert Bradshaw et al....
  80. - John Cremona (Feb 2008) -- Point counting and group structure for
  81. non-prime fields, Frobenius endomorphism and order, elliptic logs
  82. - John Cremona (Aug 2008) -- Introduced ``EllipticCurvePoint_number_field`` class
  83. - Tobias Nagel, Michael Mardaus, John Cremona (Dec 2008) -- `p`-adic elliptic logarithm over `\QQ`
  84. - David Hansen (Jan 2009) -- Added ``weil_pairing`` function to ``EllipticCurvePoint_finite_field`` class
  85. """
  86. #*****************************************************************************
  87. # Copyright (C) 2005 William Stein <wstein@gmail.com>
  88. #
  89. # Distributed under the terms of the GNU General Public License (GPL)
  90. #
  91. # This code is distributed in the hope that it will be useful,
  92. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  93. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  94. # General Public License for more details.
  95. #
  96. # The full text of the GPL is available at:
  97. #
  98. # http://www.gnu.org/licenses/
  99. #*****************************************************************************
  100. import math
  101. from sage.structure.element import AdditiveGroupElement, RingElement
  102. from sage.interfaces import gp
  103. import sage.plot.all as plot
  104. from sage.rings.padics.factory import Qp
  105. from sage.rings.padics.precision_error import PrecisionError
  106. import ell_generic
  107. import sage.rings.all as rings
  108. import sage.rings.arith as arith
  109. import sage.misc.misc as misc
  110. from sage.groups.all import AbelianGroup
  111. import sage.groups.generic as generic
  112. from sage.libs.pari.all import pari, PariError
  113. from sage.structure.sequence import Sequence
  114. from sage.schemes.generic.morphism import (SchemeMorphism_projective_coordinates_ring,
  115. SchemeMorphism_abelian_variety_coordinates_field,
  116. is_SchemeMorphism, SchemeMorphism_coordinates)
  117. import sage.schemes.generic.scheme as scheme
  118. from constructor import EllipticCurve
  119. oo = rings.infinity # infinity
  120. class EllipticCurvePoint(SchemeMorphism_projective_coordinates_ring):
  121. """
  122. A point on an elliptic curve.
  123. """
  124. def __cmp__(self, other):
  125. """
  126. Standard comparison function for points on elliptic curves, to
  127. allow sorting and equality testing.
  128. EXAMPLES:
  129. sage: E=EllipticCurve(QQ,[1,1])
  130. sage: P=E(0,1)
  131. sage: P.order()
  132. +Infinity
  133. sage: Q=P+P
  134. sage: P==Q
  135. False
  136. sage: Q+Q == 4*P
  137. True
  138. """
  139. if isinstance(other, (int, long, rings.Integer)) and other == 0:
  140. if self.is_zero():
  141. return 0
  142. else:
  143. return -1
  144. return SchemePoint_projective_abelian_scheme.__cmp__(self, other)
  145. class EllipticCurvePoint_field(AdditiveGroupElement): # SchemeMorphism_abelian_variety_coordinates_field):
  146. """
  147. A point on an elliptic curve over a field. The point has coordinates
  148. in the base field.
  149. EXAMPLES::
  150. sage: E = EllipticCurve('37a')
  151. sage: E([0,0])
  152. (0 : 0 : 1)
  153. sage: E(0,0) # brackets are optional
  154. (0 : 0 : 1)
  155. sage: E([GF(5)(0), 0]) # entries are coerced
  156. (0 : 0 : 1)
  157. sage: E(0.000, 0)
  158. (0 : 0 : 1)
  159. sage: E(1,0,0)
  160. Traceback (most recent call last):
  161. ...
  162. TypeError: Coordinates [1, 0, 0] do not define a point on
  163. Elliptic Curve defined by y^2 + y = x^3 - x over Rational Field
  164. ::
  165. sage: E = EllipticCurve([0,0,1,-1,0])
  166. sage: S = E(QQ); S
  167. Abelian group of points on Elliptic Curve defined by y^2 + y = x^3 - x over Rational Field
  168. sage: K.<i>=NumberField(x^2+1)
  169. sage: E=EllipticCurve(K,[0,1,0,-160,308])
  170. sage: P=E(26,-120)
  171. sage: Q=E(2+12*i,-36+48*i)
  172. sage: P.order() == Q.order() == 4 # long time (3s)
  173. True
  174. sage: 2*P==2*Q
  175. False
  176. ::
  177. sage: K.<t>=FractionField(PolynomialRing(QQ,'t'))
  178. sage: E=EllipticCurve([0,0,0,0,t^2])
  179. sage: P=E(0,t)
  180. sage: P,2*P,3*P
  181. ((0 : t : 1), (0 : -t : 1), (0 : 1 : 0))
  182. TESTS::
  183. sage: loads(S.dumps()) == S
  184. True
  185. sage: E = EllipticCurve('37a')
  186. sage: P = E(0,0); P
  187. (0 : 0 : 1)
  188. sage: loads(P.dumps()) == P
  189. True
  190. sage: T = 100*P
  191. sage: loads(T.dumps()) == T
  192. True
  193. Test pickling an elliptic curve that has known points on it::
  194. sage: e = EllipticCurve([0, 0, 1, -1, 0]); g = e.gens(); loads(dumps(e)) == e
  195. True
  196. """
  197. def __init__(self, curve, v, check=True):
  198. """
  199. Constructor for a point on an elliptic curve.
  200. INPUT:
  201. - curve -- an elliptic curve
  202. - v -- data determining a point (another point, the integer
  203. 0, or a tuple of coordinates)
  204. EXAMPLE::
  205. sage: E = EllipticCurve('43a')
  206. sage: P = E([2, -4, 2]); P
  207. (1 : -2 : 1)
  208. sage: P == E([1,-2])
  209. True
  210. sage: P = E(0); P
  211. (0 : 1 : 0)
  212. sage: P=E(2, -4, 2); P
  213. (1 : -2 : 1)
  214. """
  215. point_homset = curve.point_homset()
  216. AdditiveGroupElement.__init__(self, point_homset)
  217. if check:
  218. # mostly from SchemeMorphism_projective_coordinates_field
  219. d = point_homset.codomain().ambient_space().ngens()
  220. if is_SchemeMorphism(v) or isinstance(v, EllipticCurvePoint_field):
  221. v = list(v)
  222. elif v == 0:
  223. self._coords = Sequence((0,1,0), point_homset.value_ring())
  224. return
  225. if not isinstance(v,(list,tuple)):
  226. raise TypeError, \
  227. "Argument v (= %s) must be a scheme point, list, or tuple."%str(v)
  228. if len(v) != d and len(v) != d-1:
  229. raise TypeError, "v (=%s) must have %s components"%(v, d)
  230. v = Sequence(v, point_homset.value_ring())
  231. if len(v) == d-1: # very common special case
  232. v.append(v.universe()(1))
  233. n = len(v)
  234. all_zero = True
  235. for i in range(n):
  236. c = v[n-1-i]
  237. if c:
  238. all_zero = False
  239. if c == 1:
  240. break
  241. for j in range(n-i):
  242. v[j] /= c
  243. break
  244. if all_zero:
  245. raise ValueError, "%s does not define a valid point since all entries are 0"%repr(v)
  246. x, y, z = v
  247. if z == 0:
  248. test = x
  249. else:
  250. a1, a2, a3, a4, a6 = curve.ainvs()
  251. test = y**2 + (a1*x+a3)*y - (((x+a2)*x+a4)*x+a6)
  252. if not test == 0:
  253. raise TypeError, "Coordinates %s do not define a point on %s"%(list(v),curve)
  254. # point_homset.codomain()._check_satisfies_equations(v)
  255. self._coords = v
  256. def _repr_(self):
  257. """
  258. Return a string representation of this point.
  259. EXAMPLE::
  260. sage: E = EllipticCurve('39a')
  261. sage: P = E([-2, 1, 1])
  262. sage: P._repr_()
  263. '(-2 : 1 : 1)'
  264. """
  265. return self.codomain().ambient_space()._repr_generic_point(self._coords)
  266. def _latex_(self):
  267. """
  268. Return a LaTeX representation of this point.
  269. EXAMPLE::
  270. sage: E = EllipticCurve('40a')
  271. sage: P = E([3, 0])
  272. sage: P._latex_()
  273. '\\left(3 : 0 : 1\\right)'
  274. """
  275. return self.codomain().ambient_space()._latex_generic_point(self._coords)
  276. def __getitem__(self, n):
  277. """
  278. Return the n'th coordinate of this point.
  279. EXAMPLE::
  280. sage: E = EllipticCurve('42a')
  281. sage: P = E([-17, -51, 17])
  282. sage: [P[i] for i in [2,1,0]]
  283. [1, -3, -1]
  284. """
  285. return self._coords[n]
  286. def __iter__(self):
  287. """
  288. Return the coordinates of this point as a list.
  289. EXAMPLE::
  290. sage: E = EllipticCurve('37a')
  291. sage: list(E([0,0]))
  292. [0, 0, 1]
  293. """
  294. return iter(self._coords)
  295. def __tuple__(self):
  296. """
  297. Return the coordinates of this point as a tuple.
  298. EXAMPLE::
  299. sage: E = EllipticCurve('44a')
  300. sage: P = E([1, -2, 1])
  301. sage: P.__tuple__()
  302. (1, -2, 1)
  303. """
  304. return tuple(self._coords) # Warning: _coords is a list!
  305. def __cmp__(self, other):
  306. """
  307. Comparison function for points to allow sorting and equality testing.
  308. EXAMPLES::
  309. sage: E = EllipticCurve('45a')
  310. sage: P = E([2, -1, 1])
  311. sage: P == E(0)
  312. False
  313. sage: P+P == E(0)
  314. True
  315. """
  316. if not isinstance(other, EllipticCurvePoint_field):
  317. try:
  318. other = self.codomain().ambient_space()(other)
  319. except TypeError:
  320. return -1
  321. return cmp(self._coords, other._coords)
  322. def _pari_(self):
  323. r"""
  324. Converts this point to PARI format.
  325. EXAMPLES::
  326. sage: E = EllipticCurve([0,0,0,3,0])
  327. sage: O = E(0)
  328. sage: P = E.point([1,2])
  329. sage: O._pari_()
  330. [0]
  331. sage: P._pari_()
  332. [1, 2]
  333. The following implicitly calls O._pari_() and P._pari_()::
  334. sage: pari(E).elladd(O,P)
  335. [1, 2]
  336. TESTS::
  337. Try the same over a finite field::
  338. sage: E = EllipticCurve(GF(11), [0,0,0,3,0])
  339. sage: O = E(0)
  340. sage: P = E.point([1,2])
  341. sage: O._pari_()
  342. [0]
  343. sage: P._pari_()
  344. [Mod(1, 11), Mod(2, 11)]
  345. sage: pari(E).elladd(O,P)
  346. [Mod(1, 11), Mod(2, 11)]
  347. """
  348. if self[2]:
  349. return pari([self[0]/self[2], self[1]/self[2]])
  350. else:
  351. return pari([0])
  352. def scheme(self):
  353. """
  354. Return the scheme of this point, i.e., the curve it is on.
  355. This is synonymous with curve() which is perhaps more
  356. intuitive.
  357. EXAMPLES::
  358. sage: E=EllipticCurve(QQ,[1,1])
  359. sage: P=E(0,1)
  360. sage: P.scheme()
  361. Elliptic Curve defined by y^2 = x^3 + x + 1 over Rational Field
  362. sage: P.scheme() == P.curve()
  363. True
  364. sage: K.<a>=NumberField(x^2-3,'a')
  365. sage: P=E.base_extend(K)(1,a)
  366. sage: P.scheme()
  367. Elliptic Curve defined by y^2 = x^3 + x + 1 over Number Field in a with defining polynomial x^2 - 3
  368. """
  369. #The following text is just not true: it applies to the class
  370. #EllipticCurvePoint, which appears to be never used, but does
  371. #not apply to EllipticCurvePoint_field which is simply derived
  372. #from AdditiveGroupElement.
  373. #
  374. #"Technically, points on curves in Sage are scheme maps from
  375. # the domain Spec(F) where F is the base field of the curve to
  376. # the codomain which is the curve. See also domain() and
  377. # codomain()."
  378. return self.codomain()
  379. def domain(self):
  380. """
  381. Return the domain of this point, which is `Spec(F)` where `F` is
  382. the field of definition.
  383. EXAMPLES::
  384. sage: E=EllipticCurve(QQ,[1,1])
  385. sage: P=E(0,1)
  386. sage: P.domain()
  387. Spectrum of Rational Field
  388. sage: K.<a>=NumberField(x^2-3,'a')
  389. sage: P=E.base_extend(K)(1,a)
  390. sage: P.domain()
  391. Spectrum of Number Field in a with defining polynomial x^2 - 3
  392. """
  393. return self.parent().domain()
  394. def codomain(self):
  395. """
  396. Return the codomain of this point, which is the curve it is
  397. on. Synonymous with curve() which is perhaps more intuitive.
  398. EXAMPLES::
  399. sage: E=EllipticCurve(QQ,[1,1])
  400. sage: P=E(0,1)
  401. sage: P.domain()
  402. Spectrum of Rational Field
  403. sage: K.<a>=NumberField(x^2-3,'a')
  404. sage: P=E.base_extend(K)(1,a)
  405. sage: P.codomain()
  406. Elliptic Curve defined by y^2 = x^3 + x + 1 over Number Field in a with defining polynomial x^2 - 3
  407. sage: P.codomain() == P.curve()
  408. True
  409. """
  410. return self.parent().codomain()
  411. def order(self):
  412. r"""
  413. Return the order of this point on the elliptic curve.
  414. If the point is zero, returns 1, otherwise raise a
  415. NotImplementedError.
  416. For curves over number fields and finite fields, see below.
  417. .. note::
  418. :meth:`additive_order` is a synonym for :meth:`order`
  419. EXAMPLE::
  420. sage: K.<t>=FractionField(PolynomialRing(QQ,'t'))
  421. sage: E=EllipticCurve([0,0,0,-t^2,0])
  422. sage: P=E(t,0)
  423. sage: P.order()
  424. Traceback (most recent call last):
  425. ...
  426. NotImplementedError: Computation of order of a point not implemented over general fields.
  427. sage: E(0).additive_order()
  428. 1
  429. sage: E(0).order() == 1
  430. True
  431. """
  432. if self.is_zero():
  433. return rings.Integer(1)
  434. raise NotImplementedError, "Computation of order of a point not implemented over general fields."
  435. additive_order = order
  436. def curve(self):
  437. """
  438. Return the curve that this point is on.
  439. EXAMPLES::
  440. sage: E = EllipticCurve('389a')
  441. sage: P = E([-1,1])
  442. sage: P.curve()
  443. Elliptic Curve defined by y^2 + y = x^3 + x^2 - 2*x over Rational Field
  444. """
  445. return self.scheme()
  446. def __nonzero__(self):
  447. """
  448. Return True if this is not the zero point on the curve.
  449. EXAMPLES::
  450. sage: E = EllipticCurve('37a')
  451. sage: P = E(0); P
  452. (0 : 1 : 0)
  453. sage: P.is_zero()
  454. True
  455. sage: P = E.gens()[0]
  456. sage: P.is_zero()
  457. False
  458. """
  459. return bool(self[2])
  460. def has_finite_order(self):
  461. """
  462. Return True if this point has finite additive order as an element
  463. of the group of points on this curve.
  464. For fields other than number fields and finite fields, this is
  465. NotImplemented unless self.is_zero().
  466. EXAMPLES::
  467. sage: K.<t>=FractionField(PolynomialRing(QQ,'t'))
  468. sage: E=EllipticCurve([0,0,0,-t^2,0])
  469. sage: P = E(0)
  470. sage: P.has_finite_order()
  471. True
  472. sage: P=E(t,0)
  473. sage: P.has_finite_order()
  474. Traceback (most recent call last):
  475. ...
  476. NotImplementedError: Computation of order of a point not implemented over general fields.
  477. sage: (2*P).is_zero()
  478. True
  479. """
  480. if self.is_zero(): return True
  481. return self.order() != oo
  482. is_finite_order = has_finite_order # for backward compatibility
  483. def has_infinite_order(self):
  484. """
  485. Return True if this point has infinite additive order as an element
  486. of the group of points on this curve.
  487. For fields other than number fields and finite fields, this is
  488. NotImplemented unless self.is_zero().
  489. EXAMPLES::
  490. sage: K.<t>=FractionField(PolynomialRing(QQ,'t'))
  491. sage: E=EllipticCurve([0,0,0,-t^2,0])
  492. sage: P = E(0)
  493. sage: P.has_infinite_order()
  494. False
  495. sage: P=E(t,0)
  496. sage: P.has_infinite_order()
  497. Traceback (most recent call last):
  498. ...
  499. NotImplementedError: Computation of order of a point not implemented over general fields.
  500. sage: (2*P).is_zero()
  501. True
  502. """
  503. if self.is_zero(): return False
  504. return self.order() == oo
  505. def plot(self, **args):
  506. """
  507. Plot this point on an elliptic curve.
  508. INPUT:
  509. - ``**args`` -- all arguments get passed directly onto the point
  510. plotting function.
  511. EXAMPLES::
  512. sage: E = EllipticCurve('389a')
  513. sage: P = E([-1,1])
  514. sage: P.plot(pointsize=30, rgbcolor=(1,0,0))
  515. """
  516. if self.is_zero():
  517. return plot.text("$\\infty$", (-3,3), **args)
  518. else:
  519. return plot.point((self[0], self[1]), **args)
  520. def _add_(self, right):
  521. """
  522. Add self to right.
  523. EXAMPLES::
  524. sage: E = EllipticCurve('389a')
  525. sage: P = E([-1,1]); Q = E([0,0])
  526. sage: P + Q
  527. (1 : 0 : 1)
  528. sage: P._add_(Q) == P + Q
  529. True
  530. Example to show that bug \#4820 is fixed::
  531. sage: [type(c) for c in 2*EllipticCurve('37a1').gen(0)]
  532. [<type 'sage.rings.rational.Rational'>,
  533. <type 'sage.rings.rational.Rational'>,
  534. <type 'sage.rings.rational.Rational'>]
  535. """
  536. # Use Prop 7.1.7 of Cohen "A Course in Computational Algebraic Number Theory"
  537. if self.is_zero():
  538. return right
  539. if right.is_zero():
  540. return self
  541. E = self.curve()
  542. a1, a2, a3, a4, a6 = E.ainvs()
  543. x1, y1 = self[0], self[1]
  544. x2, y2 = right[0], right[1]
  545. if x1 == x2 and y1 == -y2 - a1*x2 - a3:
  546. return E(0) # point at infinity
  547. if x1 == x2 and y1 == y2:
  548. try:
  549. m = (3*x1*x1 + 2*a2*x1 + a4 - a1*y1) / (2*y1 + a1*x1 + a3)
  550. except ZeroDivisionError:
  551. R = E.base_ring()
  552. if R.is_finite():
  553. N = R.characteristic()
  554. from sage.rings.all import ZZ
  555. N1 = N.gcd(ZZ(2*y1 + a1*x1 + a3))
  556. N2 = N//N1
  557. raise ZeroDivisionError, "Inverse of %s does not exist (characteristic = %s = %s*%s)"%(2*y1 + a1*x1 + a3, N,N1,N2)
  558. else:
  559. raise ZeroDivisionError, "Inverse of %s does not exist"%(2*y1 + a1*x1 + a3)
  560. else:
  561. try:
  562. m = (y1-y2)/(x1-x2)
  563. except ZeroDivisionError:
  564. R = E.base_ring()
  565. if R.is_finite():
  566. N = R.characteristic()
  567. from sage.rings.all import ZZ
  568. N1 = N.gcd(ZZ(x1-x2))
  569. N2 = N//N1
  570. raise ZeroDivisionError, "Inverse of %s does not exist (characteristic = %s = %s*%s)"%(x1-x2, N,N1,N2)
  571. else:
  572. raise ZeroDivisionError, "Inverse of %s does not exist"%(x1-x2)
  573. x3 = -x1 - x2 - a2 + m*(m+a1)
  574. y3 = -y1 - a3 - a1*x3 + m*(x1-x3)
  575. # See \#4820 for why we need to coerce 1 into the base ring here:
  576. return E.point([x3, y3, E.base_ring()(1)], check=False)
  577. def _sub_(self, right):
  578. """
  579. Subtract right from self.
  580. EXAMPLES::
  581. sage: E = EllipticCurve('389a')
  582. sage: P = E([-1,1]); Q = E([0,0])
  583. sage: P - Q
  584. (4 : 8 : 1)
  585. sage: P - Q == P._sub_(Q)
  586. True
  587. sage: (P - Q) + Q
  588. (-1 : 1 : 1)
  589. sage: P
  590. (-1 : 1 : 1)
  591. """
  592. return self + (-right)
  593. def __neg__(self):
  594. """
  595. Return the additive inverse of this point.
  596. EXAMPLES::
  597. sage: E = EllipticCurve('389a')
  598. sage: P = E([-1,1])
  599. sage: Q = -P; Q
  600. (-1 : -2 : 1)
  601. sage: Q + P
  602. (0 : 1 : 0)
  603. Example to show that bug \#4820 is fixed::
  604. sage: [type(c) for c in -EllipticCurve('37a1').gen(0)]
  605. [<type 'sage.rings.rational.Rational'>,
  606. <type 'sage.rings.rational.Rational'>,
  607. <type 'sage.rings.rational.Rational'>]
  608. """
  609. if self.is_zero():
  610. return self
  611. E, x, y = self.curve(), self[0], self[1]
  612. # See \#4820 for why we need to coerce 1 into the base ring here:
  613. return E.point([x, -y - E.a1()*x - E.a3(), E.base_ring()(1)], check=False)
  614. def xy(self):
  615. """
  616. Return the `x` and `y` coordinates of this point, as a 2-tuple.
  617. If this is the point at infinity a ZeroDivisionError is raised.
  618. EXAMPLES::
  619. sage: E = EllipticCurve('389a')
  620. sage: P = E([-1,1])
  621. sage: P.xy()
  622. (-1, 1)
  623. sage: Q = E(0); Q
  624. (0 : 1 : 0)
  625. sage: Q.xy()
  626. Traceback (most recent call last):
  627. ...
  628. ZeroDivisionError: Rational division by zero
  629. """
  630. if self[2] == 1:
  631. return self[0], self[1]
  632. else:
  633. return self[0]/self[2], self[1]/self[2]
  634. def is_divisible_by(self, m):
  635. """
  636. Return True if there exists a point `Q` defined over the same
  637. field as self such that `mQ` == self.
  638. INPUT:
  639. - ``m`` -- a positive integer.
  640. OUTPUT:
  641. (bool) -- True if there is a solution, else False.
  642. .. warning::
  643. This function usually triggers the computation of the
  644. `m`-th division polynomial of the associated elliptic
  645. curve, which will be expensive if `m` is large, though it
  646. will be cached for subsequent calls with the same `m`.
  647. EXAMPLES::
  648. sage: E = EllipticCurve('389a')
  649. sage: Q = 5*E(0,0); Q
  650. (-2739/1444 : -77033/54872 : 1)
  651. sage: Q.is_divisible_by(4)
  652. False
  653. sage: Q.is_divisible_by(5)
  654. True
  655. A finite field example::
  656. sage: E = EllipticCurve(GF(101),[23,34])
  657. sage: E.cardinality().factor()
  658. 2 * 53
  659. sage: Set([T.order() for T in E.points()])
  660. {1, 106, 2, 53}
  661. sage: len([T for T in E.points() if T.is_divisible_by(2)])
  662. 53
  663. sage: len([T for T in E.points() if T.is_divisible_by(3)])
  664. 106
  665. TESTS:
  666. This shows that the bug reported at #10076 is fixed::
  667. sage: K = QuadraticField(8,'a')
  668. sage: E = EllipticCurve([K(0),0,0,-1,0])
  669. sage: P = E([-1,0])
  670. sage: P.is_divisible_by(2)
  671. False
  672. sage: P.division_points(2)
  673. []
  674. Note that it is not sufficient to test that
  675. ``self.division_points(m,poly_only=True)`` has roots::
  676. sage: P.division_points(2, poly_only=True).roots()
  677. [(1/2*a - 1, 1), (-1/2*a - 1, 1)]
  678. sage: tor = E.torsion_points(); len(tor)
  679. 8
  680. sage: [T.order() for T in tor]
  681. [2, 4, 4, 2, 4, 1, 4, 2]
  682. sage: all([T.is_divisible_by(3) for T in tor])
  683. True
  684. sage: Set([T for T in tor if T.is_divisible_by(2)])
  685. {(0 : 1 : 0), (1 : 0 : 1)}
  686. sage: Set([2*T for T in tor])
  687. {(0 : 1 : 0), (1 : 0 : 1)}
  688. """
  689. # Coerce the input m to an integer
  690. m = rings.Integer(m)
  691. # Check for trivial cases of m = 1, -1 and 0.
  692. if m == 1 or m == -1:
  693. return True
  694. if m == 0:
  695. return self == 0 # then m*self=self for all m!
  696. m = m.abs()
  697. # Now the following line would of course be correct, but we
  698. # work harder to be more efficient:
  699. # return len(self.division_points(m)) > 0
  700. P = self
  701. # If P has finite order n and gcd(m,n)=1 then the result is
  702. # True. However, over general fields computing P.order() is
  703. # not implemented.
  704. try:
  705. n = P.order()
  706. if not n == oo:
  707. if m.gcd(n)==1:
  708. return True
  709. except NotImplementedError:
  710. pass
  711. P_is_2_torsion = (P==-P)
  712. g = P.division_points(m, poly_only=True)
  713. if not P_is_2_torsion:
  714. # In this case deg(g)=m^2, and each root in K lifts to two
  715. # points Q,-Q both in E(K), of which exactly one is a
  716. # solution. So we just check the existence of roots:
  717. return len(g.roots())>0
  718. # Now 2*P==0
  719. if m%2==1:
  720. return True # P itself is a solution when m is odd
  721. # Now m is even and 2*P=0. Roots of g in K may or may not
  722. # lift to solutions in E(K), so we fall back to the default.
  723. # Note that division polynomials are cached so this is not
  724. # inefficient:
  725. return len(self.division_points(m)) > 0
  726. def division_points(self, m, poly_only=False):
  727. r"""
  728. Return a list of all points `Q` such that `mQ=P` where `P` = self.
  729. Only points on the elliptic curve containing self and defined
  730. over the base field are included.
  731. INPUT:
  732. - ``m`` -- a positive integer
  733. - ``poly_only`` -- bool (default: False); if True return polynomial whose roots give all possible `x`-coordinates of `m`-th roots of self.
  734. OUTPUT:
  735. (list) -- a (possibly empty) list of solutions `Q` to `mQ=P`, where `P` = self.
  736. EXAMPLES:
  737. We find the five 5-torsion points on an elliptic curve::
  738. sage: E = EllipticCurve('11a'); E
  739. Elliptic Curve defined by y^2 + y = x^3 - x^2 - 10*x - 20 over Rational Field
  740. sage: P = E(0); P
  741. (0 : 1 : 0)
  742. sage: P.division_points(5)
  743. [(0 : 1 : 0), (5 : -6 : 1), (5 : 5 : 1), (16 : -61 : 1), (16 : 60 : 1)]
  744. Note above that 0 is included since [5]*0 = 0.
  745. We create a curve of rank 1 with no torsion and do a consistency check::
  746. sage: E = EllipticCurve('11a').quadratic_twist(-7)
  747. sage: Q = E([44,-270])
  748. sage: (4*Q).division_points(4)
  749. [(44 : -270 : 1)]
  750. We create a curve over a non-prime finite field with group of order `18`::
  751. sage: k.<a> = GF(25)
  752. sage: E = EllipticCurve(k, [1,2+a,3,4*a,2])
  753. sage: P = E([3,3*a+4])
  754. sage: factor(E.order())
  755. 2 * 3^2
  756. sage: P.order()
  757. 9
  758. We find the `1`-division points as a consistency check -- there
  759. is just one, of course::
  760. sage: P.division_points(1)
  761. [(3 : 3*a + 4 : 1)]
  762. The point `P` has order coprime to 2 but divisible by 3, so::
  763. sage: P.division_points(2)
  764. [(2*a + 1 : 3*a + 4 : 1), (3*a + 1 : a : 1)]
  765. We check that each of the 2-division points works as claimed::
  766. sage: [2*Q for Q in P.division_points(2)]
  767. [(3 : 3*a + 4 : 1), (3 : 3*a + 4 : 1)]
  768. Some other checks::
  769. sage: P.division_points(3)
  770. []
  771. sage: P.division_points(4)
  772. [(0 : 3*a + 2 : 1), (1 : 0 : 1)]
  773. sage: P.division_points(5)
  774. [(1 : 1 : 1)]
  775. An example over a number field (see trac #3383)::
  776. sage: E = EllipticCurve('19a1')
  777. sage: K.<t> = NumberField(x^9-3*x^8-4*x^7+16*x^6-3*x^5-21*x^4+5*x^3+7*x^2-7*x+1)
  778. sage: EK = E.base_extend(K)
  779. sage: E(0).division_points(3)
  780. [(0 : 1 : 0), (5 : -10 : 1), (5 : 9 : 1)]
  781. sage: EK(0).division_points(3)
  782. [(0 : 1 : 0), (5 : 9 : 1), (5 : -10 : 1)]
  783. sage: E(0).division_points(9)
  784. [(0 : 1 : 0), (5 : -10 : 1), (5 : 9 : 1)]
  785. sage: EK(0).division_points(9)
  786. [(0 : 1 : 0), (5 : 9 : 1), (5 : -10 : 1), (-150/121*t^8 + 414/121*t^7 + 1481/242*t^6 - 2382/121*t^5 - 103/242*t^4 + 629/22*t^3 - 367/242*t^2 - 1307/121*t + 625/121 : 35/484*t^8 - 133/242*t^7 + 445/242*t^6 - 799/242*t^5 + 373/484*t^4 + 113/22*t^3 - 2355/484*t^2 - 753/242*t + 1165/484 : 1), (-150/121*t^8 + 414/121*t^7 + 1481/242*t^6 - 2382/121*t^5 - 103/242*t^4 + 629/22*t^3 - 367/242*t^2 - 1307/121*t + 625/121 : -35/484*t^8 + 133/242*t^7 - 445/242*t^6 + 799/242*t^5 - 373/484*t^4 - 113/22*t^3 + 2355/484*t^2 + 753/242*t - 1649/484 : 1), (-1383/484*t^8 + 970/121*t^7 + 3159/242*t^6 - 5211/121*t^5 + 37/484*t^4 + 654/11*t^3 - 909/484*t^2 - 4831/242*t + 6791/484 : 927/121*t^8 - 5209/242*t^7 - 8187/242*t^6 + 27975/242*t^5 - 1147/242*t^4 - 1729/11*t^3 + 1566/121*t^2 + 12873/242*t - 10871/242 : 1), (-1383/484*t^8 + 970/121*t^7 + 3159/242*t^6 - 5211/121*t^5 + 37/484*t^4 + 654/11*t^3 - 909/484*t^2 - 4831/242*t + 6791/484 : -927/121*t^8 + 5209/242*t^7 + 8187/242*t^6 - 27975/242*t^5 + 1147/242*t^4 + 1729/11*t^3 - 1566/121*t^2 - 12873/242*t + 10629/242 : 1), (-4793/484*t^8 + 6791/242*t^7 + 10727/242*t^6 - 18301/121*t^5 + 2347/484*t^4 + 2293/11*t^3 - 7311/484*t^2 - 17239/242*t + 26767/484 : 30847/484*t^8 - 21789/121*t^7 - 34605/121*t^6 + 117164/121*t^5 - 10633/484*t^4 - 29437/22*t^3 + 39725/484*t^2 + 55428/121*t - 176909/484 : 1), (-4793/484*t^8 + 6791/242*t^7 + 10727/242*t^6 - 18301/121*t^5 + 2347/484*t^4 + 2293/11*t^3 - 7311/484*t^2 - 17239/242*t + 26767/484 : -30847/484*t^8 + 21789/121*t^7 + 34605/121*t^6 - 117164/121*t^5 + 10633/484*t^4 + 29437/22*t^3 - 39725/484*t^2 - 55428/121*t + 176425/484 : 1)]
  787. """
  788. # Coerce the input m to an integer
  789. m = rings.Integer(m)
  790. # Check for trivial cases of m = 1, -1 and 0.
  791. if m == 1 or m == -1:
  792. return [self]
  793. if m == 0:
  794. if self == 0: # then every point Q is a solution, but...
  795. return [self]
  796. else:
  797. return []
  798. # ans will contain the list of division points.
  799. ans = []
  800. # We compute a polynomial g whose roots give all possible x
  801. # coordinates of the m-division points. The number of
  802. # solutions (over the algebraic closure) is m^2, assuming that
  803. # the characteristic does not divide m.
  804. E = self.curve()
  805. P = self
  806. nP = -P
  807. P_is_2_torsion = (P==nP)
  808. # If self is the 0, then self is a solution, and the correct
  809. # poly is the m'th division polynomial
  810. if P == 0:
  811. ans.append(P)
  812. g = E.division_polynomial(m)
  813. else:
  814. # The poly g here is 0 at x(Q) iff x(m*Q) = x(P).
  815. g = E._multiple_x_numerator(m) - P[0]*E._multiple_x_denominator(m)
  816. # When 2*P=0, then -Q is a solution iff Q is. For even m,
  817. # no 2-torsion point is a solution, so that g is the
  818. # square of a polynomial g1 of degree m^2/2, and each root
  819. # of g1 leads to a pair of solutions Q, -Q to m*Q=P. For
  820. # odd m, P itself is the only 2-torsion solution, so g has
  821. # the form (x-x(P))*g1(x)^2 where g1 has degree (m^2-1)/2
  822. # and each root of g1 leads to a pair Q, -Q.
  823. if P_is_2_torsion:
  824. if m%2==0:
  825. # This computes g.sqrt() which is not implemented
  826. g = g.gcd(g.derivative())*g.leading_coefficient().sqrt()
  827. # When 2*P!=0, then for each solution Q to m*Q=P, -Q is
  828. # not a solution (and points of order 2 are not
  829. # solutions). Hence the roots of g are distinct and each
  830. # gives rise to precisely one solution Q.
  831. else:
  832. g0 = g.variables()[0] - P[0]
  833. g = g // g0
  834. g = g.gcd(g.derivative())*g.leading_coefficient().sqrt()
  835. g = g0*g
  836. if poly_only:
  837. return g
  838. for x in g.roots(multiplicities=False):
  839. if E.is_x_coord(x):
  840. # Make a point on the curve with this x coordinate.
  841. Q = E.lift_x(x)
  842. nQ = -Q
  843. mQ = m*Q
  844. # if P==-P then Q works iff -Q works, so we include
  845. # both unless they are equal:
  846. if P_is_2_torsion:
  847. if mQ == P:
  848. ans.append(Q)
  849. if nQ != Q:
  850. ans.append(nQ)
  851. else:
  852. # P is not 2-torsion so at most one of Q, -Q works
  853. # and we must try both:
  854. if mQ == P:
  855. ans.append(Q)
  856. elif mQ == nP:
  857. ans.append(nQ)
  858. # Finally, sort and return
  859. ans.sort()
  860. return ans
  861. def _divide_out(self,p):
  862. r"""
  863. Return `(Q,k)` where `p^kQ` == self and `Q` cannot be divided by `p`.
  864. ..WARNING:
  865. It is up to the caller to make sure that this does not loop
  866. endlessly. It is used in
  867. ``EllipticCurve_generic._p_primary_torsion_basis()``, when
  868. self will always have (finite) order which is a power of `p`,
  869. so that the order of `Q` increases by a factor of `p` at each
  870. stage.
  871. Since it will clearly be in danger of looping when
  872. self.is_zero(), this case is caught, but otherwise caveat
  873. user.
  874. EXAMPLES::
  875. sage: E = EllipticCurve('37a1')
  876. sage: P = E([0, 0])
  877. sage: R = 12*P
  878. sage: R._divide_out(2)
  879. ((-1 : -1 : 1), 2)
  880. sage: R._divide_out(3)
  881. ((2 : -3 : 1), 1)
  882. sage: R._divide_out(5)
  883. ((1357/841 : 28888/24389 : 1), 0)
  884. sage: R._divide_out(12)
  885. Traceback (most recent call last):
  886. ...
  887. ValueError: p (=12) should be prime.
  888. """
  889. p = rings.Integer(p)
  890. if not p.is_prime():
  891. raise ValueError, "p (=%s) should be prime."%p
  892. if self.is_zero():
  893. raise ValueError, "self must not be 0."
  894. k=0; Q=self
  895. pts = Q.division_points(p)
  896. while len(pts) > 0:
  897. Q = pts[0]
  898. k += 1
  899. pts = Q.division_points(p)
  900. return (Q,k)
  901. ############################## end ################################
  902. def _line_(self,R,Q):
  903. r"""
  904. Computes the value at `Q` of a straight line through points self and `R`.
  905. INPUT:
  906. - ``R, Q`` -- points on self.curve() with ``Q`` nonzero.
  907. OUTPUT:
  908. An element of the base field self.curve().base_field().
  909. A ValueError is raised if ``Q`` is zero.
  910. EXAMPLES::
  911. sage: F.<a>=GF(2^5)
  912. sage: E=EllipticCurve(F,[0,0,1,1,1])
  913. sage: P = E(a^4 + 1, a^3)
  914. sage: Q = E(a^4, a^4 + a^3)
  915. sage: O = E(0)
  916. sage: P._line_(P,-2*P) == 0
  917. True
  918. sage: P._line_(Q,-(P+Q)) == 0
  919. True
  920. sage: O._line_(O,Q) == F(1)
  921. True
  922. sage: P._line_(O,Q) == a^4 - a^4 + 1
  923. True
  924. sage: P._line_(13*P,Q) == a^4
  925. True
  926. sage: P._line_(P,Q) == a^4 + a^3 + a^2 + 1
  927. True
  928. See trac #7116::
  929. sage: P._line_ (Q,O)
  930. Traceback (most recent call last):
  931. ...
  932. ValueError: Q must be nonzero.
  933. ..NOTES:
  934. This function is used in _miller_ algorithm.
  935. AUTHOR:
  936. - David Hansen (2009-01-25)
  937. """
  938. if Q.is_zero():
  939. raise ValueError, "Q must be nonzero."
  940. if self.is_zero() or R.is_zero():
  941. if self == R:
  942. return self.curve().base_field().one_element()
  943. if self.is_zero():
  944. return Q[0] - R[0]
  945. if R.is_zero():
  946. return Q[0] - self[0]
  947. elif self != R:
  948. if self[0] == R[0]:
  949. return Q[0] - self[0]
  950. else:
  951. l = (R[1] - self[1])/(R[0] - self[0])
  952. return Q[1] - self[1] - l * (Q[0] - self[0])
  953. else:
  954. a1, a2, a3, a4, a6 = self.curve().a_invariants()
  955. numerator = (3*self[0]**2 + 2*a2*self[0] + a4 - a1*self[1])
  956. denominator = (2*self[1] + a1*self[0] + a3)
  957. if denominator == 0:
  958. return Q[0] - self[0]
  959. else:
  960. l = numerator/denominator
  961. return Q[1] - self[1] - l * (Q[0] - self[0])
  962. def _miller_(self,Q,n):
  963. r"""
  964. Compute the value at `Q` of the rational function `f_{n,P}`, where the divisor of `f_{n,P}` is `n[P]-n[O]`.
  965. INPUT:
  966. - ``Q`` -- a nonzero point on self.curve().
  967. - ``n`` -- an integer such that `n*P = n*Q = (0:1:0)` where `P`=self.
  968. OUTPUT:
  969. An element in the base field self.curve().base_field()
  970. A ValueError is raised if ``Q`` is zero.
  971. EXAMPLES::
  972. sage: F.<a>=GF(2^5)
  973. sage: E=EllipticCurve(F,[0,0,1,1,1])
  974. sage: P = E(a^4 + 1, a^3)
  975. sage: Fx.<b>=GF(2^(4*5))
  976. sage: Ex=EllipticCurve(Fx,[0,0,1,1,1])
  977. sage: phi=Hom(F,Fx)(F.gen().minpoly().roots(Fx)[0][0])
  978. sage: Px=Ex(phi(P.xy()[0]),phi(P.xy()[1]))
  979. sage: Qx = Ex(b^19 + b^18 + b^16 + b^12 + b^10 + b^9 + b^8 + b^5 + b^3 + 1, b^18 + b^13 + b^10 + b^8 + b^5 + b^4 + b^3 + b)
  980. sage: Px._miller_(Qx,41) == b^17 + b^13 + b^12 + b^9 + b^8 + b^6 + b^4 + 1
  981. True
  982. sage: Qx._miller_(Px,41) == b^13 + b^10 + b^8 + b^7 + b^6 + b^5
  983. True
  984. sage: P._miller_(E(0),41)
  985. Traceback (most recent call last):
  986. ...
  987. ValueError: Q must be nonzero.
  988. An example of even order::
  989. sage: F.<a> = GF(19^4)
  990. sage: E = EllipticCurve(F,[-1,0])
  991. sage: P = E(15*a^3 + 17*a^2 + 14*a + 13,16*a^3 + 7*a^2 + a + 18)
  992. sage: Q = E(10*a^3 + 16*a^2 + 4*a + 2, 6*a^3 + 4*a^2 + 3*a + 2)
  993. sage: x=P.weil_pairing(Q,360)
  994. sage: x^360 == F(1)
  995. True
  996. You can use the _miller_ function on linearly dependent points, but with the risk of a dividing with zero::
  997. sage: Px._miller_(2*Px,41)
  998. Traceback (most recent call last):
  999. ...
  1000. ZeroDivisionError: division by zero in finite field.
  1001. ALGORITHM:
  1002. Double-and-add.
  1003. REFERENCES:
  1004. - [Mil04] Victor S. Miller, "The Weil pairing, and its efficient calculation", J. Cryptol., 17(4):235-261, 2004
  1005. AUTHOR:
  1006. - David Hansen (2009-01-25)
  1007. """
  1008. if Q.is_zero():
  1009. raise ValueError, "Q must be nonzero."
  1010. t = self.curve().base_field().one_element()
  1011. V = self
  1012. S = 2*V
  1013. nbin = n.bits()
  1014. i = n.nbits() - 2
  1015. while i > -1:
  1016. S = 2*V
  1017. t = (t**2)*(V._line_(V,Q)/S._line_(-S,Q))
  1018. V = S
  1019. if nbin[i] == 1:
  1020. S = V+self
  1021. t=t*(V._line_(self,Q)/S._line_(-S,Q))
  1022. V = S
  1023. i=i-1
  1024. return t
  1025. def weil_pairing(self, Q, n):
  1026. r"""
  1027. Compute the Weil pairing of self and `Q` using Miller's algorithm.
  1028. INPUT:
  1029. - ``Q`` -- a point on self.curve().
  1030. - ``n`` -- an integer `n` such that `nP = nQ = (0:1:0)` where `P` = self.
  1031. OUTPUT:
  1032. An `n`'th root of unity in the base field self.curve().base_field()
  1033. EXAMPLES::
  1034. sage: F.<a>=GF(2^5)
  1035. sage: E=EllipticCurve(F,[0,0,1,1,1])
  1036. sage: P = E(a^4 + 1, a^3)
  1037. sage: Fx.<b>=GF(2^(4*5))
  1038. sage: Ex=EllipticCurve(Fx,[0,0,1,1,1])
  1039. sage: phi=Hom(F,Fx)(F.gen().minpoly().roots(Fx)[0][0])
  1040. sage: Px=Ex(phi(P.xy()[0]),phi(P.xy()[1]))
  1041. sage: O = Ex(0)
  1042. sage: Qx = Ex(b^19 + b^18 + b^16 + b^12 + b^10 + b^9 + b^8 + b^5 + b^3 + 1, b^18 + b^13 + b^10 + b^8 + b^5 + b^4 + b^3 + b)
  1043. sage: Px.weil_pairing(Qx,41) == b^19 + b^15 + b^9 + b^8 + b^6 + b^4 + b^3 + b^2 + 1
  1044. True
  1045. sage: Px.weil_pairing(17*Px,41) == Fx(1)
  1046. True
  1047. sage: Px.weil_pairing(O,41) == Fx(1)
  1048. True
  1049. An error is raised if either point is not n-torsion::
  1050. sage: Px.weil_pairing(O,40)
  1051. Traceback (most recent call last):
  1052. ...
  1053. ValueError: points must both be n-torsion
  1054. A larger example (see trac \#4964)::
  1055. sage: P,Q = EllipticCurve(GF(19^4,'a'),[-1,0]).gens()
  1056. sage: P.order(), Q.order()
  1057. (360, 360)
  1058. sage: z = P.weil_pairing(Q,360)
  1059. sage: z.multiplicative_order()
  1060. 360
  1061. An example over a number field::
  1062. sage: P,Q = EllipticCurve('11a1').change_ring(CyclotomicField(5)).torsion_subgroup().gens() # long time (10s)
  1063. sage: P,Q = (P.element(), Q.element()) # long time
  1064. sage: (P.order(),Q.order()) # long time
  1065. (5, 5)
  1066. sage: P.weil_pairing(Q,5) # long time
  1067. zeta5^2
  1068. sage: Q.weil_pairing(P,5) # long time
  1069. zeta5^3
  1070. ALGORITHM:
  1071. Implemented using Proposition 8 in [Mil04]. The value 1 is
  1072. returned for linearly dependent input points. This condition
  1073. is caught via a DivisionByZeroError, since the use of a
  1074. discrete logarithm test for linear dependence, is much to slow
  1075. for large `n`.
  1076. REFERENCES:
  1077. [Mil04] Victor S. Miller, "The Weil pairing, and its efficient
  1078. calculation", J. Cryptol., 17(4):235-261, 2004
  1079. AUTHOR:
  1080. - David Hansen (2009-01-25)
  1081. """
  1082. P = self
  1083. E = P.curve()
  1084. if not Q.curve() is E:
  1085. raise ValueError, "points must both be on the same curve"
  1086. # Test if P, Q are both in E[n]
  1087. if not ((n*P).is_zero() and (n*Q).is_zero()):
  1088. raise ValueError, "points must both be n-torsion"
  1089. one = E.base_field().one_element()
  1090. # Case where P = Q
  1091. if P == Q:
  1092. return one
  1093. # Case where P = O or Q = O
  1094. if P.is_zero() or Q.is_zero():
  1095. return one
  1096. # The non-trivial case P != Q
  1097. # Note (2010-12-29): The following code block should not be
  1098. # used. It attempts to reduce the pairing calculation to order
  1099. # d = gcd(|P|,|Q|) instead of order n, but this approach is
  1100. # counterproductive, since calculating |P| and |Q| is much
  1101. # slower than calculating the pairing [BGN05].
  1102. #
  1103. # [BGN05] D. Boneh, E. Goh, and K. Nissim, "Evaluating 2-DNF
  1104. # Formulas on Ciphertexts", TCC 2005, LNCS 3378, pp. 325-341.
  1105. #
  1106. # Reduction to order d = gcd(|P|,|Q|); value is a d'th root of unity
  1107. # try:
  1108. # nP = P.order()
  1109. # except AttributeError:
  1110. # nP = generic.order_from_multiple(P,n,operation='+')
  1111. # try:
  1112. # nQ = Q.order()
  1113. # except AttributeError:
  1114. # nQ = generic.order_from_multiple(Q,n,operation='+')
  1115. # d = arith.gcd(nP,nQ)
  1116. # if d==1:
  1117. # return one
  1118. #
  1119. # P = (nP//d)*P # order d
  1120. # Q = (nQ//d)*Q # order d
  1121. # n = d
  1122. try:
  1123. return ((-1)**n.test_bit(0))*(P._miller_(Q,n)/Q._miller_(P,n))
  1124. except ZeroDivisionError, detail:
  1125. return one
  1126. class EllipticCurvePoint_number_field(EllipticCurvePoint_field):
  1127. """
  1128. A point on an elliptic curve over a number field.
  1129. Most of the functionality is derived from the parent class
  1130. ``EllipticCurvePoint_field``. In addition we have support for the
  1131. order of a point, and heights (currently only implemented over
  1132. `\QQ`).
  1133. EXAMPLES::
  1134. sage: E = EllipticCurve('37a')
  1135. sage: E([0,0])
  1136. (0 : 0 : 1)
  1137. sage: E(0,0) # brackets are optional
  1138. (0 : 0 : 1)
  1139. sage: E([GF(5)(0), 0]) # entries are coerced
  1140. (0 : 0 : 1)
  1141. sage: E(0.000, 0)
  1142. (0 : 0 : 1)
  1143. sage: E(1,0,0)
  1144. Traceback (most recent call last):
  1145. ...
  1146. TypeError: Coordinates [1, 0, 0] do not define a point on
  1147. Elliptic Curve defined by y^2 + y = x^3 - x over Rational Field
  1148. ::
  1149. sage: E = EllipticCurve([0,0,1,-1,0])
  1150. sage: S = E(QQ); S
  1151. Abelian group of points on Elliptic Curve defined by y^2 + y = x^3 - x over Rational Field
  1152. TESTS::
  1153. sage: loads(S.dumps()) == S
  1154. True
  1155. sage: P = E(0,0); P
  1156. (0 : 0 : 1)
  1157. sage: loads(P.dumps()) == P
  1158. True
  1159. sage: T = 100*P
  1160. sage: loads(T.dumps()) == T
  1161. True
  1162. Test pickling an elliptic curve that has known points on it::
  1163. sage: e = EllipticCurve([0, 0, 1, -1, 0]); g = e.gens(); loads(dumps(e)) == e
  1164. True
  1165. """
  1166. def order(self):
  1167. r"""
  1168. Return the order of this point on the elliptic curve.
  1169. If the point has infinite order, returns +Infinity. For
  1170. curves defined over `\QQ`, we call PARI; over other
  1171. number fields we implement the function here.
  1172. .. note::
  1173. :meth:`additive_order` is a synonym for :meth:`order`
  1174. EXAMPLES::
  1175. sage: E = EllipticCurve([0,0,1,-1,0])
  1176. sage: P = E([0,0]); P
  1177. (0 : 0 : 1)
  1178. sage: P.order()
  1179. +Infinity
  1180. ::
  1181. sage: E = EllipticCurve([0,1])
  1182. sage: P = E([-1,0])
  1183. sage: P.order()
  1184. 2
  1185. sage: P.additive_order()
  1186. 2
  1187. """
  1188. try:
  1189. return self._order
  1190. except AttributeError:
  1191. pass
  1192. if self.is_zero():
  1193. self._order = rings.Integer(1)
  1194. return self._order
  1195. E = self.curve()
  1196. # Special code for curves over Q, calling PARI
  1197. try:
  1198. n = int(E.pari_curve().ellorder(self))
  1199. if n == 0: n = oo
  1200. self._order = n
  1201. return n
  1202. except PariError:
  1203. pass
  1204. # Get the torsion order if known, else a bound on (multiple
  1205. # of) the order. We do not compute the torsion if it is not
  1206. # already known, since computing the bound is faster (and is
  1207. # also cached).
  1208. try:
  1209. N = E._torsion_order
  1210. except AttributeError:
  1211. N = E._torsion_bound()
  1212. # Now self is a torsion point iff it is killed by N:
  1213. if not (N*self).is_zero():
  1214. self._order = oo
  1215. return self._order
  1216. # Finally we find the exact order using the generic code:
  1217. self._order = generic.order_from_multiple(self,N,operation='+')
  1218. return self._order
  1219. additive_order = order
  1220. def has_finite_order(self):
  1221. """
  1222. Return True iff this point has finite order on the elliptic curve.
  1223. EXAMPLES::
  1224. sage: E = EllipticCurve([0,0,1,-1,0])
  1225. sage: P = E([0,0]); P
  1226. (0 : 0 : 1)
  1227. sage: P.has_finite_order()
  1228. False
  1229. ::
  1230. sage: E = EllipticCurve([0,1])
  1231. sage: P = E([-1,0])
  1232. sa

Large files files are truncated, but you can click here to view the full file