Skip to content

PM-22018: Harden arithmetic using checked operations in single_tx.rs#1293

Open
m2ux wants to merge 8 commits intomainfrom
task/PM-22018-harden-arithmetic-checked-operations
Open

PM-22018: Harden arithmetic using checked operations in single_tx.rs#1293
m2ux wants to merge 8 commits intomainfrom
task/PM-22018-harden-arithmetic-checked-operations

Conversation

@m2ux
Copy link
Copy Markdown
Contributor

@m2ux m2ux commented Apr 10, 2026

Summary

Replace unchecked arithmetic operations in single_tx.rs with checked alternatives (checked_mul, checked_sub, checked_add) and proper error propagation, converting panics on overflow/underflow into recoverable errors. Addresses informational finding from the Least Authority Node DIFF Audit (Feb 2026).

🎫 Ticket 📐 Engineering


Motivation

The transaction builder in util/toolkit/src/tx_generator/builder/builders/single_tx.rs uses checked_mul for amount aggregation but calls .expect() on the result, converting arithmetic overflow into a panic rather than a recoverable error. Similarly, subtraction for change calculation may underflow with adversarial inputs. While the toolkit is not consensus-critical, panics in production tooling are undesirable and violate defence-in-depth principles.

This change ensures that large or adversarial input values produce typed errors that callers can handle gracefully, rather than crashing the process.


Changes

Implementation:

  • Replace .expect() calls on checked_mul results with proper error propagation via ? / ok_or()
  • Guard subtraction underflow paths with checked operations and error returns
  • Extend existing error types (ShieldedCoinSelectionError, UtxoSelectionError) with arithmetic overflow/underflow variants
  • Add unit tests covering boundary values (u128::MAX, 0, values near overflow thresholds)

📌 Submission Checklist

  • Changes are backward-compatible (or flagged if breaking)
  • Pull request description explains why the change is needed
  • Self-reviewed the diff
  • I have included a change file, or skipped for this reason: [reason]
  • If the changes introduce a new feature, I have bumped the node minor version
  • Update documentation (if relevant)
  • No new todos introduced

🔱 Fork Strategy

  • Node Runtime Update
  • Node Client Update
  • Other
  • N/A

🗹 TODO before merging

  • Ready for review

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 12, 2026

CLA assistant check
All committers have signed the CLA.

…tions

Signed-off-by: Mike Clay <mike.clay@shielded.io>
@m2ux m2ux force-pushed the task/PM-22018-harden-arithmetic-checked-operations branch from 91de2a7 to 785cddd Compare April 13, 2026 15:07
m2ux and others added 7 commits April 13, 2026 17:23
…arithmetic-checked-operations

Signed-off-by: Mike Clay <mike.clay@shielded.io>
…22018)

Replace unchecked u128 arithmetic with checked alternatives in select_inputs
functions and convert .expect() panics to Result-based error propagation in
build_shielded_offer/build_unshielded_intents. Add ArithmeticOverflow error
variants to both ShieldedCoinSelectionError and UtxoSelectionError enums.
Include boundary-value unit tests covering overflow, underflow, and normal paths.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Mike Clay <mike.clay@shielded.io>
…into task/PM-22018-harden-arithmetic-checked-operations

Signed-off-by: Mike Clay <mike.clay@shielded.io>
…(PM-22018)

- Replace unchecked multiplication in batches.rs with checked_mul to
  prevent potential overflow panic in initial_shielded_offer
- Fix stale comment referencing map_or(false, ...) to match actual
  is_some_and usage in input.rs
- Add missing test cases to utxo_spend.rs for parity with input.rs:
  multiple_sum_to_required, zero_required, insufficient_returns_none

Signed-off-by: Mike Clay <mike.clay@shielded.io>
Signed-off-by: Mike Clay <mike.clay@shielded.io>
…st_module (PM-22018)

Signed-off-by: Mike Clay <mike.clay@shielded.io>
Signed-off-by: Mike Clay <mike.clay@shielded.io>
@m2ux m2ux self-assigned this Apr 15, 2026
@m2ux m2ux requested a review from ozgb April 15, 2026 08:42
@m2ux m2ux marked this pull request as ready for review April 15, 2026 09:19
@m2ux m2ux requested a review from a team as a code owner April 15, 2026 09:19
@m2ux m2ux requested a review from gilescope April 15, 2026 09:19
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