fermat-core
128-bit fixed-point decimal arithmetic for Solana's sBPF runtime.
Design Goals
no_std: Works on Solana sBPF without any OS dependencies.- Zero external dependencies: Only
proptestappears as a dev-dependency. - Panic-free: Every fallible operation returns
Result<_, ArithmeticError>. #![forbid(unsafe_code)]: No unsafe blocks anywhere in the crate.- Overflow-safe
mul_div: Uses a 256-bit intermediate (U256) to prevent the class of bugs where(a × b) / csilently wraps whena × boverflowsi128.
Core Type
Decimal { mantissa: i128, scale: u8 }
value = mantissa × 10^(-scale)
The scale is bounded to [0, 28] (MAX_SCALE). On-chain Borsh encoding is
exactly 17 bytes (16 bytes mantissa LE + 1 byte scale).
Quick Start
use ;
let price = new.unwrap; // 150.000000
let amount = new.unwrap; // 2.500000
let total = price.checked_mul.unwrap; // 375.000000...
let result = total.round.unwrap;