The Pattern
View Code
View Code
Why Compact arithmetic needs casts
When you add twoUint<128> values, the result is not a Uint<128> — it’s a wider type that can hold any possible sum:
Uint<128> is expected fails to compile:
/ or % operator in Compact.
Overflow Checks
Addition Overflow
a + b won’t exceed the maximum value, then cast the widened result back.
Example: Increasing total supply during mint.
Subtraction Underflow
Multiplication Overflow
Because Compact has no division operator, multiplication overflow is checked by widening the result and asserting it fits:Uint<128> is a widening (zero-cost) cast; the cast back to Uint<64> is a narrowing cast that the preceding assertion makes safe.
Compact does not provide
/ or %. If your problem requires division or
modulo, perform it off-chain in the witness and pass quotient and
remainder as inputs, then assert(dividend == quotient * divisor + remainder)
inside the circuit. This is the standard ZK pattern for division.Type Bounds
DifferentUint sizes have different maximum values:
Uint<8>max: 255Uint<16>max: 65,535Uint<32>max: 4,294,967,295Uint<64>max: 18,446,744,073,709,551,615Uint<128>max: 340,282,366,920,938,463,463,374,607,431,768,211,455
const placement: top-level const is not allowed in Compact. Declare
constants inside the circuits that use them (as shown above), or define them
as Field literals at module level only if the type permits.What’s Next
Transfer
Apply overflow checks in transfers
Minting
Use overflow protection when creating tokens