units: Replace manual ceil division with div_ceil
What changed, and why it matters
This commit is a routine code cleanup in the rust-bitcoin library. It replaces hand-written ceiling-division logic with Rust's newer built-in `div_ceil` function. The math result is intended to be identical, and there is no indication of a security bug being fixed.
No action required. This is a refactoring change with no security relevance.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch updates three functions in the units crate (div_by_weight_ceil, div_by_fee_rate_ceil, and mul_by_weight) to use u64::div_ceil instead of manually adding (divisor - 1) before integer division. The previous manual pattern is the standard idiom for ceiling division and is functionally equivalent to div_ceil for positive unsigned integers. The change removes overflow checks that are no longer needed because div_ceil cannot overflow in these contexts. No functional or security change is evident from the diff.
Changed components
units/src/amount/unsigned.rsunits/src/fee_rate/mod.rsInspect captured patch +7 / −19
diff --git a/units/src/amount/unsigned.rs b/units/src/amount/unsigned.rs
index a8a4e33a..abf9d788 100644
--- a/units/src/amount/unsigned.rs
+++ b/units/src/amount/unsigned.rs
@@ -466,11 +466,9 @@ impl Amount {
// Mul by 1,000 because we use per/kwu.
if let Some(sats) = self.to_sat().checked_mul(1_000) {
// No need to use checked arithmetic because wu is non-zero.
- if let Some(bump) = sats.checked_add(wu - 1) {
- let fee_rate = bump / wu;
- if let Ok(amount) = Self::from_sat(fee_rate) {
- return FeeRate::from_per_kwu(amount);
- }
+ let fee_rate = sats.div_ceil(wu);
+ if let Ok(amount) = Self::from_sat(fee_rate) {
+ return FeeRate::from_per_kwu(amount);
}
}
// Use `MathOp::Mul` because `Div` implies div by zero.
@@ -505,14 +503,7 @@ impl Amount {
debug_assert!(Self::MAX.to_sat().checked_mul(1_000).is_some());
let msats = self.to_sat() * 1_000;
- match msats.checked_add(rate - 1) {
- Some(bump) => {
- let wu = bump / rate;
- NumOpResult::Valid(Weight::from_wu(wu))
- }
- // Use `MathOp::Add` because `Div` implies div by zero.
- None => R::Error(E::while_doing(MathOp::Add)),
- }
+ NumOpResult::Valid(Weight::from_wu(msats.div_ceil(rate)))
}
}
diff --git a/units/src/fee_rate/mod.rs b/units/src/fee_rate/mod.rs
index 8575f7fb..e94e4973 100644
--- a/units/src/fee_rate/mod.rs
+++ b/units/src/fee_rate/mod.rs
@@ -210,12 +210,9 @@ impl FeeRate {
pub const fn mul_by_weight(self, weight: Weight) -> NumOpResult<Amount> {
let wu = weight.to_wu();
if let Some(fee_kwu) = self.to_sat_per_kwu_floor().checked_mul(wu) {
- // Bump by 999 to do ceil division using kwu.
- if let Some(bump) = fee_kwu.checked_add(999) {
- let fee = bump / 1_000;
- if let Ok(fee_amount) = Amount::from_sat(fee) {
- return NumOpResult::Valid(fee_amount);
- }
+ let fee = fee_kwu.div_ceil(1_000);
+ if let Ok(fee_amount) = Amount::from_sat(fee) {
+ return NumOpResult::Valid(fee_amount);
}
}
NumOpResult::Error(E::while_doing(MathOp::Mul))
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.