Constrain a Mina zkApp debit to a nonnegative UInt64

A finite-field subtraction always has a field result. An unsigned application balance needs checked integer arithmetic so an excessive debit cannot wrap into a valid field element.

Exercise the first failing amount

Set the stored balance to four units and try withdrawing five. With o1js UInt64.sub the checked subtraction detects underflow. Include a successful debit of four and an attempted credit above UInt64.MAXINT in the test suite. Bind the input balance to the actual account state before constructing the new balance.

const available = UInt64.from(4);
const requested = UInt64.from(5);
const remaining = available.sub(requested);

Review conversions at the boundary

Parse external amounts as exact smallest-unit integers and avoid lossy JavaScript Number conversions. A valid range constraint cannot fix an amount rounded before it entered the circuit. Verify the same representation is used for signed amounts, displayed decimals and the value the contract ultimately debits.

Sources