fermat-core
128-bit fixed-point decimal arithmetic for Solana's sBPF runtime.
fermat-core is the core arithmetic crate of the fermat-math project. It provides a Decimal type backed by an i128 mantissa and u8 scale, with checked arithmetic, 7 IEEE 754-2008 rounding modes, and a 256-bit intermediate for overflow-safe mul_div.
Features
#![no_std]— compiles to bare-metal sBPF#![forbid(unsafe_code)]— no unsafe blocks anywhere- Zero external dependencies — minimal binary size
- Every operation returns
Result— no panics, ever - 7 IEEE 754-2008 rounding modes — explicit rounding direction
checked_mul_div— 256-bit intermediate prevents(a*b)/coverflow- 17 bytes on-chain — compact
i128+u8Borsh encoding
Quick Start
use ;
let price = new?; // 150.000000
let amount = new?; // 2.500000
let total = price.checked_mul?;
let result = total.round?; // 375.000000
// Overflow-safe: (a * b) / c via U256 intermediate
let health = collateral.checked_mul_div?;
Installation
[]
= "0.1"
Modules
| Module | Contents |
|---|---|
decimal |
Decimal struct, constants (ZERO, ONE, MAX, MIN) |
arithmetic |
checked_add/sub/mul/div, checked_mul_div (U256), checked_neg/abs |
rounding |
7 modes: Down, Up, TowardZero, AwayFromZero, HalfUp, HalfDown, HalfEven |
convert |
from_u64/i64/u128, from_str_exact, to_token_amount |
compare |
Ord/PartialOrd with scale normalisation |
display |
Human-readable Display (e.g. 1.500000) |
error |
ArithmeticError: Overflow, DivisionByZero, ScaleExceeded, InvalidInput |
See Also
- fermat-solana — Borsh + SPL token integration
- GitHub — full repository with benchmarks and lending example
- fermatmath.net — project website
License
Licensed under either MIT or Apache-2.0 at your option.
Copyright 2026 XXIX Labs