Skip to content

Add primitive root calculator based on sympy#2583

Merged
asraa merged 1 commit intomainfrom
test_861835640
Jan 29, 2026
Merged

Add primitive root calculator based on sympy#2583
asraa merged 1 commit intomainfrom
test_861835640

Conversation

@copybara-service
Copy link
Contributor

@copybara-service copybara-service bot commented Jan 27, 2026

Add primitive root calculator based on sympy

Reviewer beware: this implementation was generated entirely by Gemini. I have given it a light review, and verified the references are correct, but I am mainly relying on the robustness of the fuzz testing to ensure correctness.

Runtimes for the primitive root calculator are ~3us with optimizations and ~30us without

@j2kun
Copy link
Collaborator

j2kun commented Jan 27, 2026

CC @crockeea

@j2kun
Copy link
Collaborator

j2kun commented Jan 27, 2026

Fixes #644

(by computing roots quickly at runtime we avoid the need to hard-code any particular roots for particular primes)

#include <cassert>
#include <cstdint>
#include <utility>
#include <vector>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: Unless required by coding standards, is already included in the header.

const APInt& modulus) {
APInt res(modulus.getBitWidth(), 1);
APInt b = base.urem(modulus);
APInt e = exponent;
Copy link
Collaborator

Choose a reason for hiding this comment

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

The modulus can be reduced as well (AI claims e -> ((e mod totient(q)) + totient(q) for arbitrary base/exponent). I guess if this is for computing compile-time constants that may not matter so much?


#include <cmath>
#include <cstdint>
#include <optional>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: in header

PiperOrigin-RevId: 862408126
@asraa asraa merged commit 4f72d4c into main Jan 29, 2026
6 of 7 checks passed
@asraa asraa deleted the test_861835640 branch January 29, 2026 16:27
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