Description
compute_share computes let remainder_product = r * bps; without a checked multiply, relying on the comment that r is bounded to (-10_000, 10_000) and bps <= 10_000. While that holds for the validated path, the public method returns early for bps > 10000 but the unchecked multiply is fragile to refactors. Replace it with checked_mul and add an explicit invariant test to lock the bound.
Requirements and context
- Must be secure, tested, and documented
- Should be efficient and easy to review
- Relevant code:
src/lib.rs (compute_share), src/test_compute_share_invariants.rs
- Result must remain in
[min(0,amount), max(0,amount)] for all inputs
Suggested execution
- Fork the repo and create a branch
git checkout -b feat/compute-share-remainder-checked-mul
- Implement changes
- Use
r.checked_mul(bps) with a saturating fallback consistent with the base path
- Add property assertions for the
0 <= result <= amount invariant
- Document the decomposition bound formally in the doc comment
- Validate security and correctness assumptions
Test and commit
- Run tests
- Cover edge cases
i128::MAX amount, bps 1/9999/10000, both rounding modes, negative amounts
- Include test output and security notes
Example commit message
feat: harden compute_share remainder math with checked multiply
Guidelines
- Minimum 95 percent test coverage
- Clear documentation
- Timeframe: 96 hours
Description
compute_sharecomputeslet remainder_product = r * bps;without a checked multiply, relying on the comment thatris bounded to(-10_000, 10_000)andbps <= 10_000. While that holds for the validated path, the public method returns early forbps > 10000but the unchecked multiply is fragile to refactors. Replace it withchecked_muland add an explicit invariant test to lock the bound.Requirements and context
src/lib.rs(compute_share),src/test_compute_share_invariants.rs[min(0,amount), max(0,amount)]for all inputsSuggested execution
git checkout -b feat/compute-share-remainder-checked-mulr.checked_mul(bps)with a saturating fallback consistent with thebasepath0 <= result <= amountinvariantTest and commit
cargo testi128::MAXamount, bps 1/9999/10000, both rounding modes, negative amountsExample commit message
feat: harden compute_share remainder math with checked multiplyGuidelines