Skip to content

Add primitive root calculator based on sympy - #2583

Merged
asraa merged 1 commit into
mainfrom
test_861835640
Jan 29, 2026
Merged

asraa merged 1 commit into
mainfrom
test_861835640

Conversation

@copybara-service

@copybara-service copybara-service Bot commented Jan 27, 2026 •

Copy link
Copy Markdown
Contributor

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

j2kun commented Jan 27, 2026

Copy link
Copy Markdown
Collaborator

CC @crockeea

@j2kun

j2kun commented Jan 27, 2026

Copy link
Copy Markdown
Collaborator

Fixes #644

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

Comment thread lib/Utils/APIntUtils.cpp
#include <cassert>
#include <cstdint>
#include <utility>
#include <vector>

Copy link
Copy Markdown
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.

Comment thread lib/Utils/APIntUtils.cpp
const APInt& modulus) {
APInt res(modulus.getBitWidth(), 1);
APInt b = base.urem(modulus);
APInt e = exponent;

Copy link
Copy Markdown
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?

Comment thread lib/Utils/MathUtils.cpp

#include <cmath>
#include <cstdint>
#include <optional>

Copy link
Copy Markdown
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
@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