#blockchain #exp #fixed-point-math #fixed-point

no-std brine-fp

192-bit fixed-point math library with logarithmic and exponential functions. Designed for blockchain, scientific, and financial applications.

5 unstable releases

0.3.0 Oct 9, 2025
0.2.1 Jul 28, 2025
0.2.0 Jul 26, 2025
0.1.1 Jul 25, 2025
0.1.0 Apr 10, 2025

#372 in Math

Download history 46/week @ 2025-09-11 36/week @ 2025-09-18 30/week @ 2025-09-25 41/week @ 2025-10-02 224/week @ 2025-10-09 107/week @ 2025-10-16 12/week @ 2025-10-23 4/week @ 2025-10-30 2/week @ 2025-11-13 4/week @ 2025-11-20 16/week @ 2025-11-27 207/week @ 2025-12-04 123/week @ 2025-12-11 166/week @ 2025-12-18 9/week @ 2025-12-25

520 downloads per month

Apache-2.0

74KB
1.5K SLoC

brine-fp

license crates.io

brine-fp is a 192-bit fixed-point math library built for high-precision, deterministic computation in constrained environments like the Solana SVM. It provides arithmetic, exponentiation, logarithms, and powers using u192-based representations, and is optimized for low compute unit (CU) usage on-chain.


⚡ Performance

Operation CU (Approx.)
log(2) ~18,500
exp(0.5) ~15,000
pow(2, ½) ~100
frexp(2) ~150

These values are measured inside the Solana SVM (via test programs).


Features

  • 192-bit unsigned & signed fixed-point types with 18 decimal places.
  • Supports log, exp, pow, floor, ceil, almost_eq, etc.
  • Remez polynomial approximations based on FreeBSD msun.
  • Designed for deterministic and overflow-aware computation.
  • Suitable for smart contract environments and financial logic.

Quick Start

use brine_fp::UnsignedNumeric;

// Construct a fixed-point number: 5.0
let five = UnsignedNumeric::new(5);

// Compute its exponential
let result = five.signed().exp().unwrap();

println!("e^5 ≈ {}", result.to_string());

Internal Representation

Each UnsignedNumeric wraps a InnerUint([lo, mid, hi]), representing a 192-bit unsigned integer:

InnerUint([lo, mid, hi])

// Equivalent to:
// value = lo + (mid << 64) + (hi << 128)

All values are scaled by 10^18, enabling 18-digit decimal precision.

This format ensures:

  • Extreme range: from 1e-18 up to ~6.3 × 10³⁹ real-world units
  • High precision: 18 decimals
  • Fully deterministic integer math (no f64, no float)

Use Cases

  • On-chain bonding curves
  • DeFi math & simulations
  • Deterministic pricing formulas
  • Token economics
  • Time-value calculations
  • Anywhere you want exp() or log() without f64

Upstream Acknowledgements

brine-fp is heavily based on prior work and stands on the shoulders of giants. The core math and algorithms are derived from:


Contributing

Contributions are welcome! Please open issues or PRs on the GitHub repo.

Dependencies

~250KB