Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small enhancements to generic discrete logs #33900

Closed
k3w1k0d3r opened this issue May 25, 2022 · 71 comments
Closed

small enhancements to generic discrete logs #33900

k3w1k0d3r opened this issue May 25, 2022 · 71 comments

Comments

@k3w1k0d3r
Copy link

Previously, the generic discrete log function wouldn't ever use Pollard's rho. From what I can tell, no generic Pohlig-Hellman implementation existed which used Pollard's rho.

Additionally, discrete log functions did not seem to properly use the custom operation arguments.

CC: @yyyyx4

Component: group theory

Keywords: discrete, logarithm, group theory, generic

Author: Julien Grijalva

Branch/Commit: 63684ec

Reviewer: Lorenz Panny

Issue created by migration from https://trac.sagemath.org/ticket/33900

@k3w1k0d3r k3w1k0d3r added this to the sage-9.7 milestone May 25, 2022
@k3w1k0d3r k3w1k0d3r self-assigned this May 25, 2022
@k3w1k0d3r
Copy link
Author

Branch: u/gh-k3w1k0d3r/better_discrete_log

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 25, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

e27d84etest
2fa0275Added Pollard's rho support to discrete_log
5048adfdiscrete log functions properly handle op argument
16c5f45undid irrelevant change

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 25, 2022

Commit: 16c5f45

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 25, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

abc6d2dfixed docstring

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 25, 2022

Changed commit from 16c5f45 to abc6d2d

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 25, 2022

Changed commit from abc6d2d to 9684efd

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 25, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

9684efdfixed docstring again

@yyyyx4
Copy link
Member

yyyyx4 commented May 26, 2022

comment:8

Thanks for the contribution!

One first comment: The use_rho keyword argument should be replaced by an algorithm argument (with options "bsgs"/"rho" and a reasonable default choice among these), as is standard practice throughout most of the Sage library. This makes sure the interface doesn't get awkward if someone wants to add another algorithm option later.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 26, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

1888426changed use_rho keyword to algorithm

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 26, 2022

Changed commit from 9684efd to 1888426

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 27, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

54bf499fix discrete_log_generic

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 27, 2022

Changed commit from 1888426 to 54bf499

@yyyyx4
Copy link
Member

yyyyx4 commented May 27, 2022

comment:11

Thanks. Some more comments:

  • There's a spurious whitespace change in build/bin/sage-bootstrap-python.
  • Your ValueError("unkown algorithm") contains a typo ("unkown" -> "unknown").
  • That same ValueError is immediately caught by the function and results in an incorrect "no discrete log" error.
  • The logic here:
    mult = op if op is not None else [add, mul][operation in multiplication_names]
    power = [mul, pow][operation in multiplication_names]
    if(op is not None):
        power = lambda x, y: multiple(x, y, operation=operation, identity=identity, inverse=inverse, op=op)
    if ord is None:
        if operation in multiplication_names:
            mult = mul
            power = pow
            try:
                ord = base.multiplicative_order()
            except Exception:
                ord = base.order()
        elif operation in addition_names:
            mult = add
            power = mul

is weird. First, it should probably throw an error if operation is neither in addition_names nor in multiplication_names. Second, if operation is given but ord is not, this sets both mult and power twice (to the same value). Third, coding style: if(foo): should be if foo: in Python. Fourth, you use an if to set power, but the ternary x if y else z to set mult; this can be streamlined into a single if.

  • This should probably say "in the group":
+    - ``op()`` - function of 2 arguments ``x``, ``y`` returning ``x*y`` in group

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 27, 2022

Changed commit from 54bf499 to f2cc977

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 27, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

f2cc977more specific bounds passed to bsgs by discrete_log

@k3w1k0d3r
Copy link
Author

comment:13

Replying to @yyyyx4:

Thanks. Some more comments:

  • There's a spurious whitespace change in build/bin/sage-bootstrap-python.
  • Your ValueError("unkown algorithm") contains a typo ("unkown" -> "unknown").
  • That same ValueError is immediately caught by the function and results in an incorrect "no discrete log" error.
  • The logic here:
    mult = op if op is not None else [add, mul][operation in multiplication_names]
    power = [mul, pow][operation in multiplication_names]
    if(op is not None):
        power = lambda x, y: multiple(x, y, operation=operation, identity=identity, inverse=inverse, op=op)
    if ord is None:
        if operation in multiplication_names:
            mult = mul
            power = pow
            try:
                ord = base.multiplicative_order()
            except Exception:
                ord = base.order()
        elif operation in addition_names:
            mult = add
            power = mul

is weird. First, it should probably throw an error if operation is neither in addition_names nor in multiplication_names. Second, if operation is given but ord is not, this sets both mult and power twice (to the same value). Third, coding style: if(foo): should be if foo: in Python. Fourth, you use an if to set power, but the ternary x if y else z to set mult; this can be streamlined into a single if.

  • This should probably say "in the group":
+    - ``op()`` - function of 2 arguments ``x``, ``y`` returning ``x*y`` in group

thanks

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 27, 2022

Changed commit from f2cc977 to 56526fc

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented May 27, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

461197aa few adjustments
56526fcchanges to discrete_log bounds handling

@k3w1k0d3r
Copy link
Author

comment:15

I didn't think sagemath/sagetrac-mirror@f2cc977 through very well and the tests didn't detect my mistake, I've now undone that change.

@k3w1k0d3r
Copy link
Author

comment:16

is it looking better now?

@k3w1k0d3r
Copy link
Author

comment:17

is there any update on this?

@yyyyx4
Copy link
Member

yyyyx4 commented Aug 3, 2022

comment:18

Sorry for the long silence. What's still missing are tests for the new algorithm flag: I guess you can just copy some of the existing tests and show that they also work if you pass algorithm='rho'.

Once that's done, I think this can be merged — while there are still plenty of things I would change in that file, it's certainly better than before.

@yyyyx4
Copy link
Member

yyyyx4 commented Aug 3, 2022

Reviewer: Lorenz Panny

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 3, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

7261907added tests for algorithm='rho'

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 9, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

3d7f800fixed order correction

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 9, 2022

Changed commit from 7f9cdb6 to 3d7f800

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 9, 2022

Changed commit from 3d7f800 to 0a648f1

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 9, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

0a648f1randomly test group order instead of just base order

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 9, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

eb28739fix silent failure issue when base == identity

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 9, 2022

Changed commit from 0a648f1 to eb28739

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 9, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

d2a9e32i, j initialized off by one

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 9, 2022

Changed commit from eb28739 to d2a9e32

@k3w1k0d3r
Copy link
Author

comment:50

finally got the last bug I've noticed out.

@yyyyx4
Copy link
Member

yyyyx4 commented Aug 10, 2022

comment:51

Looks good, thank you for all the work.

Now we just need to avoid the doctest failures when lambda fails randomly. Maybe like this?

--- a/src/sage/groups/generic.py
+++ b/src/sage/groups/generic.py
@@ -851,9 +851,14 @@ def discrete_log(a, base, ord=None, bounds=None, operation='*', identity=None, i
         ....:     hi = randrange(sol+1, 2*order)
         ....:     assert lo <= sol <= hi
         ....:     kwargs['bounds'] = (lo, hi)
-        sage: res = discrete_log(*args, **kwargs)
-        sage: res == sol
-        True
+        sage: try:
+        ....:     res = discrete_log(*args, **kwargs)
+        ....: except ValueError:
+        ....:     # lambda can fail randomly
+        ....:     if kwargs['algorithm'] != 'lambda':
+        ....:         raise
+        ....: else:
+        ....:     assert res == sol

     AUTHORS:

You may also want to consider adding yourself to the list of authors for the discrete_log function.

@k3w1k0d3r
Copy link
Author

comment:52

thanks. is there any reason to leave lambda in the random tests if we don't actually check if it worked?

@yyyyx4
Copy link
Member

yyyyx4 commented Aug 10, 2022

comment:53

We still check res == sol in case there is no exception. Thus, the test is not vacuous: It guarantees that if the lambda implementation returns something, then it's correct.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 10, 2022

Changed commit from d2a9e32 to 63684ec

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 10, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

07682a6made lambda test not randomly fail
63684ecadded to authors list

@k3w1k0d3r
Copy link
Author

comment:55

thank you!

@vbraun
Copy link
Member

vbraun commented Aug 30, 2022

Changed branch from u/gh-k3w1k0d3r/better_discrete_log to 63684ec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants