Skip to content

BigInteger PHP engine: don't return the divisor as the common residue (3.0 backport) - #2167

Merged
terrafrost merged 1 commit into
phpseclib:3.0from
dngr2:backport/php-engine-negative-exact-division
Aug 18, 2026
Merged

terrafrost merged 1 commit into
phpseclib:3.0from
dngr2:backport/php-engine-negative-exact-division

Conversation

@dngr2

@dngr2 dngr2 commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Backport of the divide() common-residue fix from #2165 to 3.0, as requested there.

For an exact negative division the remainder is already 0, but divideHelper still
added the divisor to it, returning the modulus itself, which is never a valid
common residue. Both the single-digit and long-division branches did this. Guard
on a non-zero remainder so the PHP engine matches GMP and BCMath.

The toString ?? fix from #2165 is not included here: 3.0 predates that
modernization and uses isset(...) ? ... : '', so it does not have that bug.

Verified against the GMP engine across every sign/size combination (0 disagreements
after the fix, and it reproduces without it). Added testDivideNegativeExact covering
both branches.

An exact negative division left the remainder at 0, but divideHelper still added
the divisor to it -- returning the modulus itself, which is never a valid common
residue. Both the single-digit and long-division branches did this. Guard on a
non-zero remainder, matching the GMP and BCMath engines. (toString's separate
?? fix from master does not apply here; 3.0 predates that modernization.)
@terrafrost
terrafrost merged commit 93b5c5e into phpseclib:3.0 Aug 18, 2026
43 of 46 checks passed
terrafrost added a commit that referenced this pull request Aug 18, 2026
…exact-division

BigInteger PHP engine common-residue fix (4.0, merge of #2167)
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.

2 participants