Checked arithmetic

From 0xMiden/protocol · View interactive version

Checked arithmetic is an AI agent skill from the GitHub repository 0xMiden/protocol. Use when writing Rust arithmetic on amounts or other quantities derived from external/user input — guard against silent overflow.

How to use this skill

Skill file

Checked Arithmetic on User-Supplied Values

Rule

Any arithmetic where one or more operands originates from external input (transaction body, advice provider, user RPC, deserialized payload) must use checked or overflowing arithmetic and surface the overflow:

Why

The default +, -, * operators wrap silently in release builds, so an overflow on a balance or amount yields a wrong value with no error. Checked and overflowing operations surface the overflow so it can be rejected.

Examples

// Good: checked
let total = balance.checked_add(amount).ok_or(Error::Overflow)?;

// Good: overflowing with explicit flag check
let (product, overflow) = a.widening_mul(b);
if overflow { return Err(Error::Overflow); }

// Bad: wraps on overflow in release
let total = balance + amount;
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.