Skip to content

Commit 454a3fc

Browse files
committed
add multiple= option to EllipticCurvePoint_field.set_order()
1 parent 1ca4a47 commit 454a3fc

File tree

1 file changed

+59
-7
lines changed

1 file changed

+59
-7
lines changed

src/sage/schemes/elliptic_curves/ell_point.py

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,16 +1182,24 @@ def _divide_out(self, p):
11821182
pts = Q.division_points(p)
11831183
return (Q, k)
11841184

1185-
def set_order(self, value, *, check=True):
1185+
def set_order(self, value=None, *, multiple=None, check=True):
11861186
r"""
1187-
Set the value of ``self._order`` to ``value``.
1187+
Set the cached order of this point (i.e., the value of
1188+
``self._order``) to the given ``value``.
11881189
1189-
Use this when you know a priori the order of this point to avoid a
1190-
potentially expensive order calculation.
1190+
Alternatively, when ``multiple`` is given, this method will
1191+
first run :func:`~sage.groups.generic.order_from_multiple`
1192+
to determine the exact order from the given multiple of the
1193+
point order, then cache the result.
1194+
1195+
Use this when you know a priori the order of this point, or
1196+
a multiple of the order, to avoid a potentially expensive
1197+
order calculation.
11911198
11921199
INPUT:
11931200
11941201
- ``value`` -- positive integer
1202+
- ``multiple`` -- positive integer; mutually exclusive with ``value``
11951203
11961204
OUTPUT: ``None``
11971205
@@ -1206,6 +1214,10 @@ def set_order(self, value, *, check=True):
12061214
sage: G.set_order(2) # optional - sage.rings.finite_rings
12071215
sage: 2*G # optional - sage.rings.finite_rings
12081216
(0 : 1 : 0)
1217+
sage: G = E(0, 6) # optional - sage.rings.finite_rings
1218+
sage: G.set_order(multiple=12) # optional - sage.rings.finite_rings
1219+
sage: G._order # optional - sage.rings.finite_rings
1220+
3
12091221
12101222
We now give a more interesting case, the NIST-P521 curve. Its
12111223
order is too big to calculate with Sage, and takes a long time
@@ -1227,6 +1239,30 @@ def set_order(self, value, *, check=True):
12271239
(0 : 1 : 0)
12281240
sage: proof.arithmetic(prev_proof_state) # restore state
12291241
1242+
Using ``.set_order()`` with a ``multiple=`` argument can
1243+
be used to compute a point's order *significantly* faster
1244+
than calling :meth:`order` if the point is already known
1245+
to be `m`-torsion::
1246+
1247+
sage: F.<a> = GF((10007, 23))
1248+
sage: E = EllipticCurve(F, [9,9])
1249+
sage: n = E.order()
1250+
sage: m = 5 * 47 * 139 * 1427 * 2027 * 4831 * 275449 * 29523031
1251+
sage: assert m.divides(n)
1252+
sage: P = n/m * E.lift_x(6747+a)
1253+
sage: assert m * P == 0
1254+
sage: P.set_order(multiple=m) # compute exact order
1255+
sage: factor(m // P.order()) # order is now cached
1256+
47 * 139
1257+
1258+
The algorithm used internally for this functionality is
1259+
:meth:`~sage.groups.generic.order_from_multiple`.
1260+
Indeed, simply calling :meth:`order` on ``P`` would take
1261+
much longer since factoring ``n`` is fairly expensive::
1262+
1263+
sage: n == m * 6670822796985115651 * 441770032618665681677 * 9289973478285634606114927
1264+
True
1265+
12301266
It is an error to pass a `value` equal to `0`::
12311267
12321268
sage: E = EllipticCurve(GF(7), [0, 1]) # This curve has order 12 # optional - sage.rings.finite_rings
@@ -1251,9 +1287,8 @@ def set_order(self, value, *, check=True):
12511287
...
12521288
ValueError: Value 11 illegal: 11 * (5 : 0 : 1) is not the identity
12531289
1254-
However, ``set_order`` can be fooled, though it's not likely in "real cases
1255-
of interest". For instance, the order can be set to a multiple the
1256-
actual order::
1290+
However, ``set_order`` can be fooled. For instance, the order
1291+
can be set to a multiple the actual order::
12571292
12581293
sage: E = EllipticCurve(GF(7), [0, 1]) # This curve has order 12 # optional - sage.rings.finite_rings
12591294
sage: G = E(5, 0) # G has order 2 # optional - sage.rings.finite_rings
@@ -1264,7 +1299,21 @@ def set_order(self, value, *, check=True):
12641299
AUTHORS:
12651300
12661301
- Mariah Lenox (2011-02-16)
1302+
- Lorenz Panny (2022): add ``multiple=`` option
12671303
"""
1304+
if multiple is not None:
1305+
if value is not None:
1306+
raise ValueError('cannot pass both value and multiple')
1307+
1308+
if hasattr(self, '_order'): # already known
1309+
if check and not self._order.divides(multiple):
1310+
raise ValueError(f'previously cached order {self._order} does not divide given multiple {multiple}')
1311+
return
1312+
1313+
from sage.groups.generic import order_from_multiple
1314+
value = order_from_multiple(self, multiple, check=check)
1315+
check = False
1316+
12681317
value = Integer(value)
12691318

12701319
if check:
@@ -1277,6 +1326,9 @@ def set_order(self, value, *, check=True):
12771326
raise ValueError('Value %s illegal: outside max Hasse bound' % value)
12781327
if value * self != E(0):
12791328
raise ValueError('Value %s illegal: %s * %s is not the identity' % (value, value, self))
1329+
if hasattr(self, '_order') and self._order != value: # already known
1330+
raise ValueError(f'value {value} contradicts previously cached order {self._order}')
1331+
12801332
self._order = value
12811333

12821334
# ############################# end ################################

0 commit comments

Comments
 (0)