solmath 0.2.0 · Rust 1.79+

Fixed-point maths for Solana programs.

SolMath is a zero-dependency Rust crate for decimal arithmetic, probability, option pricing and DeFi calculations. It uses integer operations at runtime and publishes compute-unit measurements from deployed SBF programs.

cargo add solmathcrates.io
no_std runtimeunsafe forbiddeninteger-only executionfeature-gated models

Fixed-point representation

SolMath stores each number as an integer scaled by 1,000,000,000,000. The value 1.25 is stored as 1,250,000,000,000. Calculations therefore use integers rather than floating point.

Every public operation checks for overflow. Settlement helpers provide separate floor and ceiling variants, so the caller chooses how a result is rounded.

Human value1.250000000000
Stored integer1,250,000,000,000
OperationsChecked integer arithmetic

Compute-unit measurements

In the published SBF runs, the log, exponential and normal-CDF functions each had an observed maximum below 1,000 CU. The table also shows averages and maxima for the larger pricing functions.

A figure may cover a single function or a complete benchmark instruction. The last column states which. Your program will have additional transaction costs.

Published SBF compute-unit measurements
OperationAverage CUObserved maximumMeasurement
ln_fixed_i705808math call
exp_fixed_i961992math call
norm_cdf_poly960993math call
Black–Scholes + Greeks24,71725,650standard precision
Black–Scholes HP + Greeks113,177149,925high precision
Arithmetic-Asian / TWAP137,997182,458math call
Exponential NIG129,872382,441full instruction
American KBI call / put381,096 / 371,876390,628 / 390,786full instruction

CU figures come from the published v0.2.0 SBF campaigns. They are not guarantees for an application's complete transaction.

Validation

The release repository includes bit-precise arithmetic checks, certificates for selected functions, comparison corpora and SBF measurements. This is internal validation. SolMath has not had an independent third-party audit.

01

Core arithmetic

545 / 545 bit-precise Kani checks across 13 harnesses, including widened arithmetic, division and square-root transitions.

02

Model comparisons

The Asian/TWAP, American KBI and NIG tests each include 100,000 production cases and 10,000 adversarial cases.

03

Audit status

No independent audit is claimed. The validation and security files state the tested input ranges and the caller's responsibilities.

Functions and models

Browse every API
01

Core arithmetic

Checked multiply, divide, square root, rounding and widened intermediate arithmetic at 12 decimal places.

  • fp_mul
  • fp_div
  • fp_sqrt
  • DoubleWord
02

Probability and functions

Logs, exponentials, powers, trigonometry, normal distributions and bivariate probabilities.

  • ln_fixed_i
  • exp_fixed_i
  • norm_cdf
  • bivariate_normal_cdf
03

Pricing models

European, barrier, Asian/TWAP, American, rainbow and NIG options with Greeks and implied volatility.

  • bs_full
  • implied_vol
  • american_kbi_price
  • nig_price_certified
04

DeFi calculations

Weighted-pool swaps, token conversion and explicit payout or collection rounding for settlement code.

  • weighted_out_given_in
  • token conversion
  • floor payout
  • ceil collection

Feature flags

Core arithmetic does not require any pricing model. Disable default features, add the required module, then measure the resulting SBF program.

Open the API reference
Cargo.tomlBlack–Scholes only
[dependencies]
solmath = { version = "0.2", default-features = false, features = ["bs"] }
arithmetic.rsSCALE = 1e12
use solmath::{fp, fp_mul};

let amount = fp("1250.50")?;
let rate = fp("0.035")?;
let interest = fp_mul(amount, rate)?;

FAQ

What is fixed-point maths?

Fixed-point arithmetic stores a decimal as an integer plus an agreed scale. SolMath uses 12 decimal places, so 1.25 is represented as 1,250,000,000,000. The decimal point is a convention; runtime operations remain integer operations.

Why use fixed point in a Solana program?

On-chain code needs deterministic results and an explicit rounding policy. Fixed point makes both visible. SolMath also publishes SBF compute-unit measurements so teams can choose a precision tier against a real transaction budget.

Is SolMath only for financial models?

No. Core arithmetic and transcendental functions can be used without linking any pricing model. Feature flags let a program include only the capabilities it calls.

Is the crate audited?

SolMath does not claim an independent third-party audit. Its internal release evidence is public: bit-precise Kani checks, reference corpora, certificate summaries, SBF measurements and a documented security contract.

Does SolMath work with no_std?

Yes. The runtime crate is no_std, has no dependencies, forbids unsafe Rust and performs no heap allocation, network access or off-chain pricing.