Skip to content

Add explicit overflow guard for r * bps remainder product in compute_share #356

@thlpkee20-wq

Description

@thlpkee20-wq

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
    • cargo test
  • 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

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions