Handle power-of-two moduli in mul_mod_special#1282
Open
tob-joe wants to merge 1 commit into
Open
Conversation
For mul_mod_special, c = 0 represents the modulus 2^bits. Multiplication modulo that value is ordinary wrapping multiplication. Return wrapping_mul for this case before the one-limb reduction paths attempt to construct NonZero<Limb> from zero, and cover both fixed and boxed integer implementations. Co-authored-by: GPT 5.5 <gpt-5.5@openai.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1282 +/- ##
==========================================
+ Coverage 91.01% 91.02% +0.01%
==========================================
Files 189 189
Lines 22160 22186 +26
==========================================
+ Hits 20169 20195 +26
Misses 1991 1991 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
|
These methods are not marked as vartime, I don't think it's appropriate to be short circuiting based on the value of the modulus. They should probably accept a |
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.
Closes #1281.
mul_mod_specialcomputes multiplication moduloMAX + 1 - c, i.e.2^bits - c. Whenc = 0, that modulus is2^bits, so reduction is ordinary fixed-width wrapping multiplication.The one-limb fixed and boxed implementations reached a limb reduction path which constructs
NonZero<Limb>from0whenc = 0, causing a panic. This adds an earlywrapping_mulreturn for the power-of-two modulus case and regression tests for both fixed and boxed integers.Testing:
cargo test --all-features mul_mod_special_zero_c_is_wrapping_multiplication -- --nocapturecargo test --all-features uint::mul_mod::tests -- --nocapturecargo test --all-features uint::boxed::mul_mod::tests -- --nocapturecargo fmt --all -- --checkgit diff --checkThis work was completed by Trail of Bits as part of the Patch The Planet project in collaboration with OpenAI. The vulnerability was identified primarily by the Codex coding agent, and manually reviewed before submission.