8000 Implemented error when incorrect bounds cause discrete_log to give incorrect answer by CalebVL · Pull Request #39484 · sagemath/sage · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Implemented error when incorrect bounds cause discrete_log to give incorrect answer #39484

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

Merged
merged 6 commits into from
Feb 28, 2025

Conversation

CalebVL
Copy link
Contributor
@CalebVL CalebVL commented Feb 10, 2025

Fixes #38316 .

When the bounds provided to the discrete_log function did not contain the correct answer the function would sometimes return an incorrect answer.

This was fixed by adding the parameter 'check' to the function. This parameter is True by default. If 'check' is True then the function verifies the answer, returning it if it is correct and throwing a Value Error if it is not. If 'check' is False then the function returns the (possibly incorrect) answer without verifying it.
A doctest was added to verify that the function correctly throws an error in a situation where it previously returned an incorrect answer.

📝 Checklist

  • 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

@DaveWitteMorris
Copy link
Member

Thanks for fixing this, but I have a few minor comments/corrections.

In sagemath, the keyword check is reserved for specifying whether to verify that the input is valid, so you should not use it for verifying the output. I don't know whether there is an established name for this parameter, but I think verify is reasonable, and it is what the sage_input function uses.

Please put *, before the new parameter in the signature of discrete_log. (Combined with the previous comment, I am asking you to change check=True to *, verify=True, or whatever new name has been chosen for the parameter.) The asterisk makes all subsequent parameters "keyword-only". There are already so many positional parameters that I don't think it makes much sense to add another one.

The python style guide (PEP8) specifies that variable names should be lowercase (with underscores between the words). So you should change originalA to original_a.

Generally speaking, comments should be complete sentences (this is in PEP8), so it would be better to change "Stores" to "Store" in line 911.

This is not your mistake, but the error message in the final line can be incorrect, because it prints a, rather than original_a, so please fix it. While you are at it, I think it would be good to improve the message by including the bounds (if any). I suggest something like:

	with_bounds = f" with bounds {bounds}" if bounds else ""
	raise ValueError(f"no discrete log of {a_original} found to base {base}{with_bounds}")

Copy link
github-actions bot commented Feb 20, 2025

Documentation preview for this PR (built with commit cc680ff; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

8000

@DaveWitteMorris
Copy link
Member

Sorry I didn't notice earlier: you need a blank line after line 893 ("Verify that :issue:`38316` is fixed::"). In a docstring, there is always a blank line between the comment that ends with a double-colon and the following doctest. Other than that, I think everything is good if the CI finds no problems.

@DaveWitteMorris
Copy link
Member

The failures on required CI Build & Test are:

 ----------------------------------------------------------------------
sage -t src/sage/rings/polynomial/plural.pyx  # SignalError in doctesting framework
sage -t src/sage/schemes/elliptic_curves/ell_rational_field.py  # 1 doctest failed
----------------------------------------------------------------------

I don't attribute either of these to the PR, because I think plural often has failures like this, and the failure in ell_rational_field.py is showing up a lot. Therefore, I consider CI to be successful.

vbraun pushed a commit to vbraun/sage that referenced this pull request Feb 24, 2025
sagemathgh-39484: Implemented error when incorrect bounds cause discrete_log to give incorrect answer
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

Fixes sagemath#38316 .

When the bounds provided to the discrete_log function did not contain
the correct answer the function would sometimes return an incorrect
answer.

This was fixed by adding the parameter 'check' to the function. This
parameter is True by default. If 'check' is True then the function
verifies the answer, returning it if it is correct and throwing a Value
Error if it is not. If 'check' is False then the function returns the
(possibly incorrect) answer without verifying it.
A doctest was added to verify that the function correctly throws an
error in a situation where it previously returned an incorrect answer.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [X] The title is concise and informative.
- [X] The description explains in detail what this PR is about.
- [X] I have linked a relevant issue or discussion.
- [X] I have created tests covering the changes.
- [X] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#39484
Reported by: Caleb Van't Land
Reviewer(s):
@vbraun vbraun merged commit aa2a162 into sagemath:develop Feb 28, 2025
19 of 22 checks passed
@CalebVL CalebVL deleted the 38316_discrete_log branch March 9, 2025 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

raise ValueError when discrete_log does not exist
3 participants
0