Fix BoxedUint GCD with mixed-precision zero operands#1291
Conversation
Handle zero operands symmetrically before calling the nonzero GCD helper. The helper assumes the left operand is nonzero and is not valid for a mixed-precision zero right operand, because zero's trailing-zero count is bounded by its allocated precision. Replace either zero operand with one for the constant-time path, compute the dummy GCD, then conditionally correct the result with the original operand. For the variable-time path, return the nonzero operand directly, and return zero for gcd(0, 0). 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 #1291 +/- ##
==========================================
- Coverage 91.26% 91.02% -0.25%
==========================================
Files 189 189
Lines 22390 22634 +244
==========================================
+ Hits 20434 20602 +168
- Misses 1956 2032 +76 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
I think we should be explicit in the size of the |
Signed-off-by: Andrew Whitehead <cywolf@gmail.com>
…y return highest bit width Signed-off-by: Andrew Whitehead <cywolf@gmail.com>
| let j = g | ||
| .is_nonzero() | ||
| .select_u32(f.bits_precision(), g.trailing_zeros()); | ||
| let k = u32_min(i, j); |
There was a problem hiding this comment.
This seems to be the real issue. Previously, if g was zero and smaller than f, then k would max out at the number of bits in g. This could lead to the wrong GCD being computed.
There was a problem hiding this comment.
oh yeah, apologies for that being unclear! Currently if z holds 0, gcd(z,x) panics if x.precision() < z.precision(), while gcd(x,z) = x >> max(0, x.trailing_zeros() - z.precision())
There was a problem hiding this comment.
That's from two commits I added to your PR BTW, it's an alternative fix really.
Summary
BoxedUint::gcddoes not correctly handle mixed-precision zero operands.The constant-time path only substitutes a nonzero placeholder for the left operand before calling the nonzero GCD helper. That causes two problems:
gcd(0_wide, x_narrow)can panic when the final constant-time correction assigns a narrow value into a wider result.gcd(x_wide, 0_narrow)can return the wrong value whenxhas more trailing zero bits than the zero right operand has precision.The second case occurs because
gcd_nzcomputes a common power-of-two factor usingmin(lhs.trailing_zeros(), rhs.trailing_zeros()). For a zeroBoxedUint,trailing_zeros()is capped by that value's precision, so a narrow zero right operand can cause the result to lose part of the power-of-two factor.Fix
This changes the
BoxedUintGCD wrapper to handle zero operands symmetrically.For the variable-time path, the wrapper now returns the nonzero operand directly when either side is zero, and returns zero for
gcd(0, 0).For the constant-time path, the wrapper now substitutes one for both zero operands before calling
gcd_nz, then conditionally corrects the result with the original operands. This avoids passing a zero right operand intogcd_nzwhile keeping the output precision a public function of the input precisions.Regression Tests
The new tests cover:
gcd(0_wide, x_narrow)gcd(x_narrow, 0_wide)gcd(x_wide, 0_narrow)wherexhas more trailing zero bits than the zero operand has precisiongcd_vartimegcd(0, 0)Verification
This 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.