Skip to content

Commit 98190f5

Browse files
committed
Improve error message raised in _ord_from_op
1 parent 1d73a51 commit 98190f5

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/sage/groups/generic.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,17 @@ def _power_func(operation, identity, inverse, op):
241241
return lambda x, y: multiple(x, y, operation=operation, identity=identity, inverse=inverse, op=op)
242242

243243

244-
def _ord_from_op(x, op):
244+
def _ord_from_op(x, op, param_name='ord'):
245245
"""
246246
Return the order of ``x`` in a group with binary operation ``op``.
247+
This is an internal helper method, only used inside other functions
248+
in this file.
247249
248250
INPUT:
249251
250252
- ``x`` -- an element of a group
251253
- ``op`` -- output of :func:`_parse_group_def`
254+
- ``param_name`` -- the named used in the error message
252255
253256
OUTPUT: An integer representing the order of ``x`` in the group.
254257
@@ -260,12 +263,20 @@ def _ord_from_op(x, op):
260263
5
261264
sage: _ord_from_op(mod(2, 5), operator.mul)
262265
4
266+
sage: _ord_from_op(mod(2, 5), operator.xor)
267+
Traceback (most recent call last):
268+
...
269+
ValueError: ord must be specified when operation is neither addition nor multiplication
270+
sage: _ord_from_op(1, operator.xor, 'ord_p')
271+
Traceback (most recent call last):
272+
...
273+
ValueError: ord_p must be specified when operation is neither addition nor multiplication
263274
"""
264275
if op is operator.add:
265276
return x.additive_order()
266277
if op is operator.mul:
267278
return x.multiplicative_order()
268-
raise ValueError("ord must be specified when operation is neither addition nor multiplication")
279+
raise ValueError(f"{param_name} must be specified when operation is neither addition nor multiplication")
269280

270281

271282
def multiple(a, n, operation='*', identity=None, inverse=None, op=None):
@@ -1218,7 +1229,7 @@ def discrete_log_lambda(a, base, bounds, operation='*', identity=None, inverse=N
12181229
################################################################
12191230

12201231

1221-
def linear_relation(P, Q, operation='+', identity=None, inverse=None, op=None):
1232+
def linear_relation(P, Q, operation='+', identity=None, inverse=None, op=None, *, ord_p=None, ord_q=None):
12221233
r"""
12231234
Function which solves the equation ``a*P=m*Q`` or ``P^a=Q^m``.
12241235
@@ -1267,8 +1278,8 @@ def linear_relation(P, Q, operation='+', identity=None, inverse=None, op=None):
12671278
Z = integer_ring.ZZ
12681279

12691280
operation, identity, inverse, op = _parse_group_def(parent(P), operation, identity, inverse, op)
1270-
n = _ord_from_op(P, op)
1271-
m = _ord_from_op(Q, op)
1281+
n = ord_p if ord_p is not None else _ord_from_op(P, op, 'ord_p')
1282+
m = ord_q if ord_q is not None else _ord_from_op(Q, op, 'ord_q')
12721283
g = n.gcd(m)
12731284
if g == 1:
12741285
return (m, Z.zero())

0 commit comments

Comments
 (0)