fermat-core 0.1.0

128-bit fixed-point arithmetic for Solana sBPF — no_std, zero dependencies
Documentation
  • Coverage
  • 100%
    58 out of 58 items documented1 out of 28 items with examples
  • Size
  • Source code size: 100.06 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.09 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 16s Average build duration of successful builds.
  • all releases: 15s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • XXIX-labs/fermat-math
    1 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • kunal-drall

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 proptest appears 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) / c silently wraps when a × b overflows i128.

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 fermat_core::{Decimal, RoundingMode};

let price  = Decimal::new(150_000_000, 6).unwrap(); // 150.000000
let amount = Decimal::new(2_500_000,   6).unwrap(); //   2.500000
let total  = price.checked_mul(amount).unwrap();    // 375.000000...
let result = total.round(6, RoundingMode::HalfEven).unwrap();