This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
/
ell_point.py
3542 lines (2875 loc) · 121 KB
/
ell_point.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
r"""
Points on elliptic curves
The base class ``EllipticCurvePoint_field``, derived from
``AdditiveGroupElement``, provides support for points on elliptic
curves defined over general fields. The derived classes
``EllipticCurvePoint_number_field`` and
``EllipticCurvePoint_finite_field`` provide further support for point
on curves defined over number fields (including the rational field
`\QQ`) and over finite fields.
The class ``EllipticCurvePoint``, which is based on
``SchemeMorphism_point_projective_ring``, currently has little extra
functionality.
EXAMPLES:
An example over `\QQ`::
sage: E = EllipticCurve('389a1')
sage: P = E(-1,1); P
(-1 : 1 : 1)
sage: Q = E(0,-1); Q
(0 : -1 : 1)
sage: P+Q
(4 : 8 : 1)
sage: P-Q
(1 : 0 : 1)
sage: 3*P-5*Q
(328/361 : -2800/6859 : 1)
An example over a number field::
sage: K.<i> = QuadraticField(-1)
sage: E = EllipticCurve(K,[1,0,0,0,-1])
sage: P = E(0,i); P
(0 : i : 1)
sage: P.order()
+Infinity
sage: 101*P-100*P==P
True
An example over a finite field::
sage: K.<a> = GF(101^3)
sage: E = EllipticCurve(K,[1,0,0,0,-1])
sage: P = E(40*a^2 + 69*a + 84 , 58*a^2 + 73*a + 45)
sage: P.order()
1032210
sage: E.cardinality()
1032210
Arithmetic with a point over an extension of a finite field::
sage: k.<a> = GF(5^2)
sage: E = EllipticCurve(k,[1,0]); E
Elliptic Curve defined by y^2 = x^3 + x over Finite Field in a of size 5^2
sage: P = E([a,2*a+4])
sage: 5*P
(2*a + 3 : 2*a : 1)
sage: P*5
(2*a + 3 : 2*a : 1)
sage: P + P + P + P + P
(2*a + 3 : 2*a : 1)
::
sage: F = Zmod(3)
sage: E = EllipticCurve(F,[1,0]);
sage: P = E([2,1])
sage: import sys
sage: n = sys.maxsize
sage: P*(n+1)-P*n == P
True
Arithmetic over `\ZZ/N\ZZ` with composite `N` is supported. When an
operation tries to invert a non-invertible element, a
ZeroDivisionError is raised and a factorization of the modulus appears
in the error message::
sage: N = 1715761513
sage: E = EllipticCurve(Integers(N),[3,-13])
sage: P = E(2,1)
sage: LCM([2..60])*P
Traceback (most recent call last):
...
ZeroDivisionError: Inverse of 1520944668 does not exist (characteristic = 1715761513 = 26927*63719)
AUTHORS:
- William Stein (2005) -- Initial version
- Robert Bradshaw et al....
- John Cremona (Feb 2008) -- Point counting and group structure for
non-prime fields, Frobenius endomorphism and order, elliptic logs
- John Cremona (Aug 2008) -- Introduced ``EllipticCurvePoint_number_field`` class
- Tobias Nagel, Michael Mardaus, John Cremona (Dec 2008) -- `p`-adic elliptic logarithm over `\QQ`
- David Hansen (Jan 2009) -- Added ``weil_pairing`` function to ``EllipticCurvePoint_finite_field`` class
- Mariah Lenox (March 2011) -- Added ``tate_pairing`` and ``ate_pairing``
functions to ``EllipticCurvePoint_finite_field`` class
"""
#*****************************************************************************
# Copyright (C) 2005 William Stein <wstein@gmail.com>
#
# Distributed under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
import math
import sage.plot.all as plot
from sage.rings.padics.factory import Qp
from sage.rings.padics.precision_error import PrecisionError
import sage.rings.all as rings
from sage.rings.real_mpfr import is_RealField
from sage.rings.integer import Integer
from sage.groups.all import AbelianGroup
import sage.groups.generic as generic
from sage.libs.pari.pari_instance import pari, prec_words_to_bits
from sage.structure.sequence import Sequence
from sage.schemes.plane_curves.projective_curve import Hasse_bounds
from sage.schemes.projective.projective_point import (SchemeMorphism_point_projective_ring,
SchemeMorphism_point_abelian_variety_field)
from sage.schemes.generic.morphism import is_SchemeMorphism
from constructor import EllipticCurve
from sage.misc.superseded import deprecated_function_alias
oo = rings.infinity # infinity
class EllipticCurvePoint(SchemeMorphism_point_projective_ring):
"""
A point on an elliptic curve.
"""
def __cmp__(self, other):
"""
Standard comparison function for points on elliptic curves, to
allow sorting and equality testing.
.. NOTE::
``__eq__`` and ``__ne__`` are implemented in
SchemeMorphism_point_projective_ring
EXAMPLES:
sage: E=EllipticCurve(QQ,[1,1])
sage: P=E(0,1)
sage: P.order()
+Infinity
sage: Q=P+P
sage: P==Q
False
sage: Q+Q == 4*P
True
"""
assert isinstance(other, (int, long, Integer)) and other == 0
if self.is_zero():
return 0
else:
return -1
class EllipticCurvePoint_field(SchemeMorphism_point_abelian_variety_field):
"""
A point on an elliptic curve over a field. The point has coordinates
in the base field.
EXAMPLES::
sage: E = EllipticCurve('37a')
sage: E([0,0])
(0 : 0 : 1)
sage: E(0,0) # brackets are optional
(0 : 0 : 1)
sage: E([GF(5)(0), 0]) # entries are coerced
(0 : 0 : 1)
sage: E(0.000, 0)
(0 : 0 : 1)
sage: E(1,0,0)
Traceback (most recent call last):
...
TypeError: Coordinates [1, 0, 0] do not define a point on
Elliptic Curve defined by y^2 + y = x^3 - x over Rational Field
::
sage: E = EllipticCurve([0,0,1,-1,0])
sage: S = E(QQ); S
Abelian group of points on Elliptic Curve defined by y^2 + y = x^3 - x over Rational Field
sage: K.<i>=NumberField(x^2+1)
sage: E=EllipticCurve(K,[0,1,0,-160,308])
sage: P=E(26,-120)
sage: Q=E(2+12*i,-36+48*i)
sage: P.order() == Q.order() == 4 # long time (3s)
True
sage: 2*P==2*Q
False
::
sage: K.<t>=FractionField(PolynomialRing(QQ,'t'))
sage: E=EllipticCurve([0,0,0,0,t^2])
sage: P=E(0,t)
sage: P,2*P,3*P
((0 : t : 1), (0 : -t : 1), (0 : 1 : 0))
TESTS::
sage: loads(S.dumps()) == S
True
sage: E = EllipticCurve('37a')
sage: P = E(0,0); P
(0 : 0 : 1)
sage: loads(P.dumps()) == P
True
sage: T = 100*P
sage: loads(T.dumps()) == T
True
Test pickling an elliptic curve that has known points on it::
sage: e = EllipticCurve([0, 0, 1, -1, 0]); g = e.gens(); loads(dumps(e)) == e
True
Test that the refactoring from :trac:`14711` did preserve the behaviour
of domain and codomain::
sage: E=EllipticCurve(QQ,[1,1])
sage: P=E(0,1)
sage: P.domain()
Spectrum of Rational Field
sage: K.<a>=NumberField(x^2-3,'a')
sage: P=E.base_extend(K)(1,a)
sage: P.domain()
Spectrum of Number Field in a with defining polynomial x^2 - 3
sage: P.codomain()
Elliptic Curve defined by y^2 = x^3 + x + 1 over Number Field in a with defining polynomial x^2 - 3
sage: P.codomain() == P.curve()
True
"""
def __init__(self, curve, v, check=True):
"""
Constructor for a point on an elliptic curve.
INPUT:
- curve -- an elliptic curve
- v -- data determining a point (another point, the integer
0, or a tuple of coordinates)
EXAMPLE::
sage: E = EllipticCurve('43a')
sage: P = E([2, -4, 2]); P
(1 : -2 : 1)
sage: P == E([1,-2])
True
sage: P = E(0); P
(0 : 1 : 0)
sage: P=E(2, -4, 2); P
(1 : -2 : 1)
"""
point_homset = curve.point_homset()
if is_SchemeMorphism(v) or isinstance(v, EllipticCurvePoint_field):
v = list(v)
elif v == 0:
# some of the code assumes that E(0) has integral entries
# irregardless of the base ring...
#R = self.base_ring()
#v = (R.zero(),R.one(),R.zero())
v = (0, 1, 0)
if check:
# mostly from SchemeMorphism_point_projective_field
d = point_homset.codomain().ambient_space().ngens()
if not isinstance(v, (list, tuple)):
raise TypeError("Argument v (= %s) must be a scheme point, list, or tuple." % str(v))
if len(v) != d and len(v) != d-1:
raise TypeError("v (=%s) must have %s components" % (v, d))
v = Sequence(v, point_homset.value_ring())
if len(v) == d-1: # very common special case
v.append(v.universe()(1))
n = len(v)
all_zero = True
for i in range(n):
c = v[n-1-i]
if c:
all_zero = False
if c == 1:
break
for j in range(n-i):
v[j] /= c
break
if all_zero:
raise ValueError("%s does not define a valid point "
"since all entries are 0" % repr(v))
x, y, z = v
if z == 0:
test = x
else:
a1, a2, a3, a4, a6 = curve.ainvs()
test = y**2 + (a1*x+a3)*y - (((x+a2)*x+a4)*x+a6)
if not test == 0:
raise TypeError("Coordinates %s do not define a point on %s" % (list(v), curve))
SchemeMorphism_point_abelian_variety_field.__init__(self, point_homset, v, check=False)
#AdditiveGroupElement.__init__(self, point_homset)
def _repr_(self):
"""
Return a string representation of this point.
EXAMPLE::
sage: E = EllipticCurve('39a')
sage: P = E([-2, 1, 1])
sage: P._repr_()
'(-2 : 1 : 1)'
"""
return self.codomain().ambient_space()._repr_generic_point(self._coords)
def _latex_(self):
"""
Return a LaTeX representation of this point.
EXAMPLE::
sage: E = EllipticCurve('40a')
sage: P = E([3, 0])
sage: P._latex_()
'\\left(3 : 0 : 1\\right)'
"""
return self.codomain().ambient_space()._latex_generic_point(self._coords)
def __getitem__(self, n):
"""
Return the n'th coordinate of this point.
EXAMPLE::
sage: E = EllipticCurve('42a')
sage: P = E([-17, -51, 17])
sage: [P[i] for i in [2,1,0]]
[1, -3, -1]
"""
return self._coords[n]
def __iter__(self):
"""
Return the coordinates of this point as a list.
EXAMPLE::
sage: E = EllipticCurve('37a')
sage: list(E([0,0]))
[0, 0, 1]
"""
return iter(self._coords)
def __tuple__(self):
"""
Return the coordinates of this point as a tuple.
EXAMPLE::
sage: E = EllipticCurve('44a')
sage: P = E([1, -2, 1])
sage: P.__tuple__()
(1, -2, 1)
"""
return tuple(self._coords) # Warning: _coords is a list!
def __cmp__(self, other):
"""
Comparison function for points to allow sorting and equality testing.
.. NOTE::
``__eq__`` and ``__ne__`` are implemented in
SchemeMorphism_point_projective_field
EXAMPLES::
sage: E = EllipticCurve('45a')
sage: P = E([2, -1, 1])
sage: P == E(0)
False
sage: P+P == E(0)
True
"""
if not isinstance(other, EllipticCurvePoint_field):
try:
other = self.codomain().ambient_space()(other)
except TypeError:
return -1
return cmp(self._coords, other._coords)
def _pari_(self):
r"""
Converts this point to PARI format.
EXAMPLES::
sage: E = EllipticCurve([0,0,0,3,0])
sage: O = E(0)
sage: P = E.point([1,2])
sage: O._pari_()
[0]
sage: P._pari_()
[1, 2]
The following implicitly calls O._pari_() and P._pari_()::
sage: pari(E).elladd(O,P)
[1, 2]
TESTS::
Try the same over a finite field::
sage: E = EllipticCurve(GF(11), [0,0,0,3,0])
sage: O = E(0)
sage: P = E.point([1,2])
sage: O._pari_()
[0]
sage: P._pari_()
[Mod(1, 11), Mod(2, 11)]
We no longer need to explicitly call ``pari(O)`` and ``pari(P)``
after :trac:`11868`::
sage: pari(E).elladd(O, P)
[Mod(1, 11), Mod(2, 11)]
"""
if self[2]:
return pari([self[0]/self[2], self[1]/self[2]])
else:
return pari([0])
def scheme(self):
"""
Return the scheme of this point, i.e., the curve it is on.
This is synonymous with :meth:`curve` which is perhaps more
intuitive.
EXAMPLES::
sage: E=EllipticCurve(QQ,[1,1])
sage: P=E(0,1)
sage: P.scheme()
Elliptic Curve defined by y^2 = x^3 + x + 1 over Rational Field
sage: P.scheme() == P.curve()
True
sage: K.<a>=NumberField(x^2-3,'a')
sage: P=E.base_extend(K)(1,a)
sage: P.scheme()
Elliptic Curve defined by y^2 = x^3 + x + 1 over Number Field in a with defining polynomial x^2 - 3
"""
#The following text is just not true: it applies to the class
#EllipticCurvePoint, which appears to be never used, but does
#not apply to EllipticCurvePoint_field which is simply derived
#from AdditiveGroupElement.
#
#"Technically, points on curves in Sage are scheme maps from
# the domain Spec(F) where F is the base field of the curve to
# the codomain which is the curve. See also domain() and
# codomain()."
return self.codomain()
def order(self):
r"""
Return the order of this point on the elliptic curve.
If the point is zero, returns 1, otherwise raise a
NotImplementedError.
For curves over number fields and finite fields, see below.
.. NOTE::
:meth:`additive_order` is a synonym for :meth:`order`
EXAMPLE::
sage: K.<t>=FractionField(PolynomialRing(QQ,'t'))
sage: E=EllipticCurve([0,0,0,-t^2,0])
sage: P=E(t,0)
sage: P.order()
Traceback (most recent call last):
...
NotImplementedError: Computation of order of a point not implemented over general fields.
sage: E(0).additive_order()
1
sage: E(0).order() == 1
True
"""
if self.is_zero():
return Integer(1)
raise NotImplementedError("Computation of order of a point "
"not implemented over general fields.")
additive_order = order
def curve(self):
"""
Return the curve that this point is on.
EXAMPLES::
sage: E = EllipticCurve('389a')
sage: P = E([-1,1])
sage: P.curve()
Elliptic Curve defined by y^2 + y = x^3 + x^2 - 2*x over Rational Field
"""
return self.scheme()
def __nonzero__(self):
"""
Return True if this is not the zero point on the curve.
EXAMPLES::
sage: E = EllipticCurve('37a')
sage: P = E(0); P
(0 : 1 : 0)
sage: P.is_zero()
True
sage: P = E.gens()[0]
sage: P.is_zero()
False
"""
return bool(self[2])
def has_finite_order(self):
"""
Return True if this point has finite additive order as an element
of the group of points on this curve.
For fields other than number fields and finite fields, this is
NotImplemented unless self.is_zero().
EXAMPLES::
sage: K.<t>=FractionField(PolynomialRing(QQ,'t'))
sage: E=EllipticCurve([0,0,0,-t^2,0])
sage: P = E(0)
sage: P.has_finite_order()
True
sage: P=E(t,0)
sage: P.has_finite_order()
Traceback (most recent call last):
...
NotImplementedError: Computation of order of a point not implemented over general fields.
sage: (2*P).is_zero()
True
"""
if self.is_zero():
return True
return self.order() != oo
is_finite_order = has_finite_order # for backward compatibility
def has_infinite_order(self):
"""
Return True if this point has infinite additive order as an element
of the group of points on this curve.
For fields other than number fields and finite fields, this is
NotImplemented unless self.is_zero().
EXAMPLES::
sage: K.<t>=FractionField(PolynomialRing(QQ,'t'))
sage: E=EllipticCurve([0,0,0,-t^2,0])
sage: P = E(0)
sage: P.has_infinite_order()
False
sage: P=E(t,0)
sage: P.has_infinite_order()
Traceback (most recent call last):
...
NotImplementedError: Computation of order of a point not implemented over general fields.
sage: (2*P).is_zero()
True
"""
if self.is_zero():
return False
return self.order() == oo
def plot(self, **args):
"""
Plot this point on an elliptic curve.
INPUT:
- ``**args`` -- all arguments get passed directly onto the point
plotting function.
EXAMPLES::
sage: E = EllipticCurve('389a')
sage: P = E([-1,1])
sage: P.plot(pointsize=30, rgbcolor=(1,0,0))
Graphics object consisting of 1 graphics primitive
"""
if self.is_zero():
return plot.text("$\\infty$", (-3, 3), **args)
else:
return plot.point((self[0], self[1]), **args)
def _add_(self, right):
"""
Add self to right.
EXAMPLES::
sage: E = EllipticCurve('389a')
sage: P = E([-1,1]); Q = E([0,0])
sage: P + Q
(1 : 0 : 1)
sage: P._add_(Q) == P + Q
True
Example to show that bug :trac:`4820` is fixed::
sage: [type(c) for c in 2*EllipticCurve('37a1').gen(0)]
[<type 'sage.rings.rational.Rational'>,
<type 'sage.rings.rational.Rational'>,
<type 'sage.rings.rational.Rational'>]
Checks that :trac:`15964` is fixed::
sage: N = 1715761513
sage: E = EllipticCurve(Integers(N),[3,-13])
sage: P = E(2,1)
sage: LCM([2..60])*P
Traceback (most recent call last):
...
ZeroDivisionError: Inverse of 1520944668 does not exist
(characteristic = 1715761513 = 26927*63719)
sage: N = 35
sage: E = EllipticCurve(Integers(N),[5,1])
sage: P = E(0,1)
sage: LCM([2..6])*P
Traceback (most recent call last):
...
ZeroDivisionError: Inverse of 28 does not exist
(characteristic = 35 = 7*5)
"""
# Use Prop 7.1.7 of Cohen "A Course in Computational Algebraic
# Number Theory"
if self.is_zero():
return right
if right.is_zero():
return self
E = self.curve()
a1, a2, a3, a4, a6 = E.ainvs()
x1, y1 = self[0], self[1]
x2, y2 = right[0], right[1]
if x1 == x2 and y1 == -y2 - a1*x2 - a3:
return E(0) # point at infinity
if x1 == x2 and y1 == y2:
try:
m = (3*x1*x1 + 2*a2*x1 + a4 - a1*y1) / (2*y1 + a1*x1 + a3)
except ZeroDivisionError:
R = E.base_ring()
if R.is_finite():
N = R.characteristic()
N1 = N.gcd(Integer(2*y1 + a1*x1 + a3))
N2 = N//N1
raise ZeroDivisionError("Inverse of %s does not exist (characteristic = %s = %s*%s)" % (2*y1 + a1*x1 + a3, N, N1, N2))
else:
raise ZeroDivisionError("Inverse of %s does not exist" % (2*y1 + a1*x1 + a3))
else:
try:
m = (y1-y2)/(x1-x2)
except ZeroDivisionError:
R = E.base_ring()
if R.is_finite():
N = R.characteristic()
N1 = N.gcd(Integer(x1-x2))
N2 = N//N1
raise ZeroDivisionError("Inverse of %s does not exist (characteristic = %s = %s*%s)" % (x1-x2, N, N1, N2))
else:
raise ZeroDivisionError("Inverse of %s does not exist" % (x1-x2))
x3 = -x1 - x2 - a2 + m*(m+a1)
y3 = -y1 - a3 - a1*x3 + m*(x1-x3)
# See trac #4820 for why we need to coerce 1 into the base ring here:
return E.point([x3, y3, E.base_ring()(1)], check=False)
def _sub_(self, right):
"""
Subtract right from self.
EXAMPLES::
sage: E = EllipticCurve('389a')
sage: P = E([-1,1]); Q = E([0,0])
sage: P - Q
(4 : 8 : 1)
sage: P - Q == P._sub_(Q)
True
sage: (P - Q) + Q
(-1 : 1 : 1)
sage: P
(-1 : 1 : 1)
"""
return self + (-right)
def __neg__(self):
"""
Return the additive inverse of this point.
EXAMPLES::
sage: E = EllipticCurve('389a')
sage: P = E([-1,1])
sage: Q = -P; Q
(-1 : -2 : 1)
sage: Q + P
(0 : 1 : 0)
Example to show that bug :trac:`4820` is fixed::
sage: [type(c) for c in -EllipticCurve('37a1').gen(0)]
[<type 'sage.rings.rational.Rational'>,
<type 'sage.rings.rational.Rational'>,
<type 'sage.rings.rational.Rational'>]
"""
if self.is_zero():
return self
E, x, y = self.curve(), self[0], self[1]
# See trac #4820 for why we need to coerce 1 into the base ring here:
return E.point([x, -y - E.a1()*x - E.a3(), E.base_ring()(1)], check=False)
def xy(self):
"""
Return the `x` and `y` coordinates of this point, as a 2-tuple.
If this is the point at infinity a ZeroDivisionError is raised.
EXAMPLES::
sage: E = EllipticCurve('389a')
sage: P = E([-1,1])
sage: P.xy()
(-1, 1)
sage: Q = E(0); Q
(0 : 1 : 0)
sage: Q.xy()
Traceback (most recent call last):
...
ZeroDivisionError: Rational division by zero
"""
if self[2] == 1:
return self[0], self[1]
else:
return self[0]/self[2], self[1]/self[2]
def is_divisible_by(self, m):
"""
Return True if there exists a point `Q` defined over the same
field as self such that `mQ` == self.
INPUT:
- ``m`` -- a positive integer.
OUTPUT:
(bool) -- True if there is a solution, else False.
.. WARNING::
This function usually triggers the computation of the
`m`-th division polynomial of the associated elliptic
curve, which will be expensive if `m` is large, though it
will be cached for subsequent calls with the same `m`.
EXAMPLES::
sage: E = EllipticCurve('389a')
sage: Q = 5*E(0,0); Q
(-2739/1444 : -77033/54872 : 1)
sage: Q.is_divisible_by(4)
False
sage: Q.is_divisible_by(5)
True
A finite field example::
sage: E = EllipticCurve(GF(101),[23,34])
sage: E.cardinality().factor()
2 * 53
sage: Set([T.order() for T in E.points()])
{1, 106, 2, 53}
sage: len([T for T in E.points() if T.is_divisible_by(2)])
53
sage: len([T for T in E.points() if T.is_divisible_by(3)])
106
TESTS:
This shows that the bug reported at :trac:`10076` is fixed::
sage: K = QuadraticField(8,'a')
sage: E = EllipticCurve([K(0),0,0,-1,0])
sage: P = E([-1,0])
sage: P.is_divisible_by(2)
False
sage: P.division_points(2)
[]
Note that it is not sufficient to test that
``self.division_points(m,poly_only=True)`` has roots::
sage: P.division_points(2, poly_only=True).roots()
[(1/2*a - 1, 1), (-1/2*a - 1, 1)]
sage: tor = E.torsion_points(); len(tor)
8
sage: [T.order() for T in tor]
[2, 4, 4, 2, 1, 2, 4, 4]
sage: all([T.is_divisible_by(3) for T in tor])
True
sage: Set([T for T in tor if T.is_divisible_by(2)])
{(0 : 1 : 0), (1 : 0 : 1)}
sage: Set([2*T for T in tor])
{(0 : 1 : 0), (1 : 0 : 1)}
"""
# Coerce the input m to an integer
m = Integer(m)
# Check for trivial cases of m = 1, -1 and 0.
if m == 1 or m == -1:
return True
if m == 0:
return self == 0 # then m*self=self for all m!
m = m.abs()
# Now the following line would of course be correct, but we
# work harder to be more efficient:
# return len(self.division_points(m)) > 0
P = self
# If P has finite order n and gcd(m,n)=1 then the result is
# True. However, over general fields computing P.order() is
# not implemented.
try:
n = P.order()
if not n == oo:
if m.gcd(n) == 1:
return True
except NotImplementedError:
pass
P_is_2_torsion = (P == -P)
g = P.division_points(m, poly_only=True)
if not P_is_2_torsion:
# In this case deg(g)=m^2, and each root in K lifts to two
# points Q,-Q both in E(K), of which exactly one is a
# solution. So we just check the existence of roots:
return len(g.roots()) > 0
# Now 2*P==0
if m % 2 == 1:
return True # P itself is a solution when m is odd
# Now m is even and 2*P=0. Roots of g in K may or may not
# lift to solutions in E(K), so we fall back to the default.
# Note that division polynomials are cached so this is not
# inefficient:
return len(self.division_points(m)) > 0
def division_points(self, m, poly_only=False):
r"""
Return a list of all points `Q` such that `mQ=P` where `P` = self.
Only points on the elliptic curve containing self and defined
over the base field are included.
INPUT:
- ``m`` -- a positive integer
- ``poly_only`` -- bool (default: False); if True return
polynomial whose roots give all possible `x`-coordinates of
`m`-th roots of self.
OUTPUT:
(list) -- a (possibly empty) list of solutions `Q` to `mQ=P`,
where `P` = self.
EXAMPLES:
We find the five 5-torsion points on an elliptic curve::
sage: E = EllipticCurve('11a'); E
Elliptic Curve defined by y^2 + y = x^3 - x^2 - 10*x - 20 over Rational Field
sage: P = E(0); P
(0 : 1 : 0)
sage: P.division_points(5)
[(0 : 1 : 0), (5 : -6 : 1), (5 : 5 : 1), (16 : -61 : 1), (16 : 60 : 1)]
Note above that 0 is included since [5]*0 = 0.
We create a curve of rank 1 with no torsion and do a consistency check::
sage: E = EllipticCurve('11a').quadratic_twist(-7)
sage: Q = E([44,-270])
sage: (4*Q).division_points(4)
[(44 : -270 : 1)]
We create a curve over a non-prime finite field with group of
order `18`::
sage: k.<a> = GF(25)
sage: E = EllipticCurve(k, [1,2+a,3,4*a,2])
sage: P = E([3,3*a+4])
sage: factor(E.order())
2 * 3^2
sage: P.order()
9
We find the `1`-division points as a consistency check -- there
is just one, of course::
sage: P.division_points(1)
[(3 : 3*a + 4 : 1)]
The point `P` has order coprime to 2 but divisible by 3, so::
sage: P.division_points(2)
[(2*a + 1 : 3*a + 4 : 1), (3*a + 1 : a : 1)]
We check that each of the 2-division points works as claimed::
sage: [2*Q for Q in P.division_points(2)]
[(3 : 3*a + 4 : 1), (3 : 3*a + 4 : 1)]
Some other checks::
sage: P.division_points(3)
[]
sage: P.division_points(4)
[(0 : 3*a + 2 : 1), (1 : 0 : 1)]
sage: P.division_points(5)
[(1 : 1 : 1)]
An example over a number field (see :trac:`3383`)::
sage: E = EllipticCurve('19a1')
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)
sage: EK = E.base_extend(K)
sage: E(0).division_points(3)
[(0 : 1 : 0), (5 : -10 : 1), (5 : 9 : 1)]
sage: EK(0).division_points(3)
[(0 : 1 : 0), (5 : 9 : 1), (5 : -10 : 1)]
sage: E(0).division_points(9)
[(0 : 1 : 0), (5 : -10 : 1), (5 : 9 : 1)]
sage: EK(0).division_points(9)
[(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)]
"""
# Coerce the input m to an integer
m = Integer(m)
# Check for trivial cases of m = 1, -1 and 0.
if m == 1 or m == -1: