Skip to content

BoxedUint: parse "0" as a one-limb zero - #1382

Merged
tarcieri merged 2 commits into
RustCrypto:masterfrom
wihansaem1991:fix/boxed-radix-zero
Nov 3, 2026
Merged

tarcieri merged 2 commits into
RustCrypto:masterfrom
wihansaem1991:fix/boxed-radix-zero

Conversation

@wihansaem1991

Copy link
Copy Markdown
Contributor

BoxedUint::from_str_radix_vartime() strips leading zeros before decoding, so an input that is zero ("0", "00", "+0") leaves no digits and the result has no limbs at all:

let n = BoxedUint::from_str_radix_vartime("0", 10).unwrap();
assert_eq!(n.as_limbs().len(), 0);          // BoxedUint::zero() has 1 limb
assert_eq!(n.to_string_radix_vartime(10), ""); // docs say a zero value gives "0"
n.bits_vartime();                            // panics: index out of bounds (ref_type/bits.rs:68)

This returns BoxedUint::zero() when no digits are left. from_str_radix_with_precision_vartime() and the byte-slice constructors already produce at least one limb, so only this path changes.

The new from_str_radix_zero test covers "0", "00", "+0" and "0_0" in radix 2, 10, 16 and 36; it panics without the change. cargo test --features alloc --lib uint::boxed::encoding (release and debug), cargo clippy --all-targets --all-features -- -D warnings and cargo fmt --all -- --check pass.

This was found by differential fuzzing against num-bigint and fixed with an AI assistant (Claude Code), which also ran the checks above.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Sjtw7YxFWFKBH5rRZfwi9T

from_str_radix_vartime() strips leading zeros before decoding, so "0"
(likewise "00" or "+0") leaves no digits and produces a BoxedUint with
no limbs at all. That value formats as "" rather than "0", and
bits_vartime() panics on it with an out-of-bounds index.
BoxedUint::zero() has one limb.

Return BoxedUint::zero() when no digits are left, and add a test for
several spellings of zero in a few radixes.

Assisted-by: Claude Code:claude-opus-5-5
wihansaem1991 added a commit to wihansaem1991/wihansaem1991.github.io that referenced this pull request Nov 1, 2026
@codecov

codecov Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.44%. Comparing base (c945e98) to head (b832f6d).
⚠️ Report is 5 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1382      +/-   ##
==========================================
+ Coverage   91.37%   91.44%   +0.06%     
==========================================
  Files         190      190              
  Lines       23201    23207       +6     
==========================================
+ Hits        21201    21222      +21     
+ Misses       2000     1985      -15     

☔ 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.

Comment thread src/uint/boxed/encoding.rs Resolved
@tarcieri
tarcieri merged commit 00501d3 into RustCrypto:master Nov 3, 2026
33 checks passed
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