fix(bigint): fix modpow normalization, right shift, and bit_length bugs#3338
Open
fix(bigint): fix modpow normalization, right shift, and bit_length bugs#3338
Conversation
Collaborator
Pull Request Test Coverage Report for Build 3159Details
💛 - Coveralls |
9db8408 to
618e830
Compare
Three correctness fixes for the non-JS BigInt backend:
1. Modular exponentiation (pow with modulus):
- Initialize result as 1%modulus (not 1) so modulus=1 returns 0
- Normalize base into [0, modulus) before the loop so negative
bases produce canonical non-negative results
2. Arithmetic right shift (>>):
- When shift amount is an exact multiple of 32 (r==0), the code
was not checking if any dropped low limbs were non-zero.
For negative numbers, floor division requires rounding toward
negative infinity, so (-2^32-1)>>32 should be -2, not -1.
3. bit_length for negative multi-limb powers of two:
- The power-of-two check started with limbs[0].popcnt()==1,
which fails for numbers like -2^32 where limb 0 is 0 and
limb 1 is 1. Fixed to count total set bits across all limbs.
Added regression tests for all three bugs.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
618e830 to
55189f3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three correctness fixes for the non-JS BigInt backend:
Modular exponentiation (pow with modulus): Initialize result as
1%modulus(not1) somodulus=1returns0. Normalize base into[0, modulus)before the loop so negative bases produce canonical non-negative results.Arithmetic right shift (>>): When shift amount is an exact multiple of 32 (
r==0), the code was not checking if any dropped low limbs were non-zero. For negative numbers, floor division requires rounding toward negative infinity, so(-2^32-1)>>32should be-2, not-1.bit_length for negative multi-limb powers of two: The power-of-two check started with
limbs[0].popcnt()==1, which fails for numbers like-2^32where limb 0 is 0 and limb 1 is 1. Fixed to count total set bits across all limbs.Test plan
moon testpasses (151 bigint tests)🤖 Generated with Claude Code