Skip to content

Conversation

user202729
Copy link
Contributor

@user202729 user202729 commented Aug 3, 2025

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

  • 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

Copy link

github-actions bot commented Aug 3, 2025

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

Copy link
Collaborator

@tscrim tscrim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. LGTM.

vbraun pushed a commit to vbraun/sage that referenced this pull request Aug 10, 2025
sagemathgh-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,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#40537
Reported by: user202729
Reviewer(s): Travis Scrimshaw
vbraun pushed a commit to vbraun/sage that referenced this pull request Aug 12, 2025
sagemathgh-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,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#40537
Reported by: user202729
Reviewer(s): Travis Scrimshaw
vbraun pushed a commit to vbraun/sage that referenced this pull request Aug 13, 2025
sagemathgh-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,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#40537
Reported by: user202729
Reviewer(s): Travis Scrimshaw
vbraun pushed a commit to vbraun/sage that referenced this pull request Aug 14, 2025
sagemathgh-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,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#40537
Reported by: user202729
Reviewer(s): Travis Scrimshaw
@vbraun vbraun merged commit a50db92 into sagemath:develop Aug 16, 2025
23 of 25 checks passed
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.

3 participants