Skip to content

Commit a50db92

Browse files
author
Release Manager
committed
gh-40537: Simplify group generic algorithm change a singleton set to a single element. The difference is that in case of singleton set, the element's hash is computed then compared with the pre-stored hash, but then both computing the hash and equality checking is linear time. not sure if it's an improvement. on the other hand the new implementation works even when the type is not hashable. edit: it's likely an improvement, for example with ZZ: ``` a = 3^floor(log(2, 3)*1000) b = 5^floor(log(2, 5)*1000) set_a = {a} %timeit a == b # 32.4 ns %timeit b in set_a # 106 ns ``` `==` can be done in sublinear time too if `==` exits early. The second modification is to call discrete_log_lambda if ord=oo and algorithm parameter is 'lambda' (the documentation says algorithm is only used for prime order group, but discrete_log_lambda works either way). Which makes more sense. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [ ] The title is concise and informative. - [ ] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - #12345: short description why this is a dependency --> <!-- - #34567: ... --> URL: #40537 Reported by: user202729 Reviewer(s): Travis Scrimshaw
2 parents b47cd75 + 07873ba commit a50db92

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/sage/groups/generic.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,14 @@ def discrete_log(a, base, ord=None, bounds=None, operation='*', identity=None, i
951951
ord = integer_ring.ZZ(ord)
952952
try:
953953
if ord == Infinity:
954-
return bsgs(base, a, bounds, identity=identity, inverse=inverse, op=op, operation=operation)
954+
if algorithm == 'bsgs':
955+
return bsgs(base, a, bounds, identity=identity, inverse=inverse, op=op, operation=operation)
956+
elif algorithm == 'lambda':
957+
return discrete_log_lambda(base, a, bounds, inverse=inverse, identity=identity, op=op, operation=operation)
958+
elif algorithm == 'rho':
959+
raise ValueError('pollard rho algorithm does not work with infinite order elements')
960+
else:
961+
raise ValueError(f"unknown algorithm {algorithm}")
955962
if base == power(base, 0) and a != base:
956963
raise ValueError
957964
f = ord.factor()
@@ -984,6 +991,8 @@ def discrete_log(a, base, ord=None, bounds=None, operation='*', identity=None, i
984991
c = discrete_log_rho(h, gamma, ord=pi, inverse=inverse, identity=identity, op=op, operation=operation)
985992
elif algorithm == 'lambda':
986993
c = discrete_log_lambda(h, gamma, (0, temp_bound), inverse=inverse, identity=identity, op=op, operation=operation)
994+
else:
995+
raise ValueError(f"unknown algorithm {algorithm}")
987996
l[i] += c * (pi**j)
988997
running_bound //= pi
989998
running_mod *= pi
@@ -1100,14 +1109,14 @@ def discrete_log_lambda(a, base, bounds, operation='*', identity=None, inverse=N
11001109
c += r
11011110
if mut:
11021111
H.set_immutable()
1103-
mem = {H}
1112+
mem = H
11041113
# second random walk
11051114
H = a
11061115
d = 0
11071116
while c - d >= lb:
11081117
if mut:
11091118
H.set_immutable()
1110-
if ub >= c - d and H in mem:
1119+
if ub >= c - d and H == mem:
11111120
return c - d
11121121
r, e = M[hash_function(H) % k]
11131122
H = mult(H, e)

0 commit comments

Comments
 (0)