Skip to content

Fix truncated Karatsuba carry#1305

Merged
tarcieri merged 1 commit into
masterfrom
fix-truncated-karatsuba-carry
Jun 22, 2026
Merged

Fix truncated Karatsuba carry#1305
tarcieri merged 1 commit into
masterfrom
fix-truncated-karatsuba-carry

Conversation

@tarcieri

Copy link
Copy Markdown
Member

Note: this was originally privately disclosed by @tob-joe as part of his work for Trail of Bits on the Patch the Planet project in collaboration with OpenAI, having used Codex to discover it. I am propagating it into a public PR shortly before a release.

Fix karatsuba::wrapping_mul when a truncated wide output cuts through the initial z0 = x0 * y0 placement window.

The previous path copied or added only the available prefix of z0, then returned no carry for omitted high z0 limbs.

The fix routes that short assignment window through schoolbook::wrapping_mul_add(...), clearing it first for overwrite mode, so the returned carry matches the truncated schoolbook operation.

Note: this was originally privately disclosed by @tob-joe as part of his
work for Trail of Bits on the Patch the Planet project in collaboration
with OpenAI, having used Codex to discover it. I am propagating it into
a public PR shortly before a release.

Fix `karatsuba::wrapping_mul` when a truncated wide output cuts through
the initial `z0 = x0 * y0` placement window.

The previous path copied or added only the available prefix of `z0`,
then returned no carry for omitted high `z0` limbs.

The fix routes that short assignment window through
`schoolbook::wrapping_mul_add(...)`, clearing it first for overwrite
mode, so the returned carry matches the truncated schoolbook operation.
@tarcieri tarcieri requested a review from andrewwhitehead June 22, 2026 15:23
@codecov

codecov Bot commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.93%. Comparing base (9d46a6b) to head (365b2fa).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1305      +/-   ##
==========================================
+ Coverage   90.92%   90.93%   +0.01%     
==========================================
  Files         189      189              
  Lines       22493    22523      +30     
==========================================
+ Hits        20451    20481      +30     
  Misses       2042     2042              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tarcieri tarcieri merged commit 48c6428 into master Jun 22, 2026
32 checks passed
@tarcieri tarcieri deleted the fix-truncated-karatsuba-carry branch June 22, 2026 15:34
tarcieri added a commit that referenced this pull request Jun 22, 2026
## Added
- `BoxedUint::{from_be_slice_truncated, from_le_slice_truncated}` (#1266)
- `Encoding::{from_bytes, to_bytes}` (#1297)
- `Uint::{from_be_slice_truncated, from_le_slice_truncated}` (#1299)
- `Limb::{from_*_bytes, from_*_slice_truncated, to_*_bytes}` as
  `const fn`s (#1300)
- `Encoding::{from_be_slice_truncated, from_le_slice_truncated}` trait
  methods (#1301)
- `ArrayEncoding::{from_byte_array, to_byte_array}` (#1302)

## Fixed
- Preserve `NonZero` and `Odd` invariants in `Zeroize` impls (#1287)
- `bitlen` performance (#1298)
- `floor_sqrt` regression (#1304)
- Truncated Karatsuba carry (#1305)
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.

1 participant