Skip to main content

Crate solmath

Crate solmath 

Source
Expand description

§solmath

Deterministic fixed-point mathematics and quantitative finance for Solana and no_std Rust. The crate combines checked decimal arithmetic, transcendentals, probability functions, Black-Scholes and Greeks, implied volatility, barriers, arithmetic-Asian/TWAP settlement, American KBI, exponential NIG, two-asset rainbow options, deterministic Heston, SABR, and weighted-pool math behind one dependency-free API.

All values use u128 or i128 scaled by 1e12 (12 decimal places). HP variants use 1e15 internally but accept and return 1e12 values.

use solmath::*;

// 1.5 in fixed-point
let x: u128 = 1_500_000_000_000;
let ln_x = ln_fixed_i(x)?; // ≈ 0.405 * 1e12

// Black-Scholes: S=100, K=100, r=5%, σ=20%, T=1yr
let s = 100 * SCALE;
let k = 100 * SCALE;
let r = 50_000_000_000u128;  // 0.05
let sigma = 200_000_000_000u128; // 0.20
let t = SCALE; // 1.0
let greeks = bs_full_hp(s, k, r, sigma, t)?;
// greeks.call, greeks.put, greeks.gamma, greeks.vega, ...

The HP Black-Scholes path agrees with QuantLib’s AnalyticEuropeanEngine to roughly 10-14 significant figures on non-tiny outputs in the reference corpus.

Re-exports§

pub use double_word::DoubleWord;
pub use encoding::fp;
pub use encoding::fp_i;
pub use error::SolMathError;
pub use arithmetic::fp_div;
pub use arithmetic::fp_div_ceil;
pub use arithmetic::fp_div_floor;
pub use arithmetic::fp_div_i;
pub use arithmetic::fp_div_round;
pub use arithmetic::fp_mul;
pub use arithmetic::fp_mul_i;
pub use arithmetic::fp_mul_i_round;
pub use arithmetic::fp_mul_i_round_dw;
pub use arithmetic::fp_mul_round;
pub use arithmetic::fp_sqrt;
pub use overflow::checked_mul_div_ceil_i;
pub use overflow::checked_mul_div_floor_i;
pub use overflow::checked_mul_div_i;
pub use mul_div::mul_div_ceil;
pub use mul_div::mul_div_ceil_u128;
pub use mul_div::mul_div_floor;
pub use mul_div::mul_div_floor_u128;
pub use transcendental::exp_fixed_i;
pub use transcendental::expm1_fixed;
pub use transcendental::ln_1p_fixed;
pub use transcendental::ln_fixed_i;
pub use transcendental::pow_fixed;
pub use transcendental::pow_fixed_i;
pub use transcendental::pow_int;
pub use trig::cos_fixed;
pub use trig::sin_fixed;
pub use trig::sincos_fixed;
pub use normal::inverse_norm_cdf;
pub use normal::norm_cdf_and_pdf;
pub use normal::norm_cdf_and_pdf_poly;
pub use normal::norm_cdf_poly;
pub use normal::norm_pdf;
pub use hp::black_scholes_price_hp;
pub use hp::bs_full_hp;
pub use hp::exp_fixed_hp;
pub use hp::fp_div_hp_safe;
pub use hp::fp_mul_hp_i;
pub use hp::fp_mul_hp_u;
pub use hp::ln_fixed_hp;
pub use hp::norm_cdf_poly_hp;
pub use hp::pow_fixed_hp;
pub use hp::pow_product_hp;
pub use i64_math::nig_call_64;
pub use i64_math::nig_put_64;
pub use complex::complex_div;
pub use complex::complex_exp;
pub use complex::complex_mul;
pub use complex::complex_sqrt;
pub use complex::Complex;
pub use bs::black_scholes_price;
pub use bs::bs_delta;
pub use bs::bs_full;
pub use bs::bs_gamma;
pub use bs::bs_rho;
pub use bs::bs_theta;
pub use bs::bs_vega;
pub use checked::BarrierInputs;
pub use checked::EuropeanInputs;
pub use checked::ImpliedVolInputs;
pub use checked::PoolSwapInputs;
pub use checked::TwapInputs;
pub use checked::Price;
pub use checked::Rate;
pub use checked::Time;
pub use checked::Vol;
pub use iv::implied_vol;
pub use american_kbi::american_kbi_price;
pub use american_kbi::AmericanKbiKind;
pub use american_kbi::AMERICAN_KBI_ARTIFACT_SHA256;
pub use american_kbi::AMERICAN_KBI_NODES;
pub use american_kbi::AMERICAN_KBI_PRICE_POINTS;
pub use rainbow::best_of_call;
pub use rainbow::worst_of_call;
pub use barrier::barrier_option;
pub use barrier::barrier_option_with_state;
pub use barrier::BarrierResult;
pub use barrier::BarrierType;
pub use asian::arithmetic_asian_price;
pub use asian::twap_option_price;
pub use asian::AsianOptionResult;
pub use nig::nig_call_price;
pub use nig::nig_price_certified;
pub use nig::CertifiedNigPrice;
pub use nig::NigParams;
pub use nig::NIG_MAX_ALPHA;
pub use nig::NIG_MAX_DELTA_TIME;
pub use nig::NIG_MIN_ALPHA;
pub use nig::NIG_MIN_DELTA_TIME;
pub use nig::NIG_QUADRATURE_NODES;
pub use heston::heston_price;
pub use sabr::certify_sabr_surface;
pub use sabr::sabr_greeks;
pub use sabr::sabr_implied_vol;
pub use sabr::sabr_precompute;
pub use sabr::sabr_price;
pub use sabr::sabr_vol_at;
pub use sabr::sabr_z_over_chi_pade;
pub use sabr::CertifiedSabrQuote;
pub use sabr::CertifiedSabrSurface;
pub use sabr::SabrSmile;
pub use sabr::MAX_SABR_SURFACE_MATURITIES;
pub use sabr::MAX_SABR_SURFACE_QUOTES;
pub use sabr::MAX_SABR_SURFACE_STRIKES;
pub use pool::fp_to_token_ceil;
pub use pool::fp_to_token_floor;
pub use pool::token_to_fp;
pub use pool::weighted_pool_swap;
pub use bvn_cdf::bvn_cdf;
pub use bvn_cdf::bvn_cdf_hp;
pub use phi2table::CertifiedPhi2Evaluator;
pub use phi2table::Phi2Certificate;
pub use phi2table::Phi2DenseTable;
pub use phi2table::Phi2Interpolation;
pub use phi2table::Phi2Reference;
pub use phi2table::Phi2Table;
pub use phi2table::PHI2_DENSE_GRID_SIZE;
pub use phi2table::PHI2_GRID_SIZE;
pub use phi2table::PHI2_ROW_DIGEST_BYTES;

Modules§

american_kbi
Fully on-chain American pricing by Kim Boundary Integration (KBI).
arithmetic
asian
Continuous arithmetic-Asian / TWAP-settled option pricing.
barrier
bs
bvn_cdf
checked
Safe-by-construction pricing inputs.
complex
double_word
encoding
error
heston
hp
i64_math
Compatibility NIG interface at 1e6 fixed-point scale.
iv
mul_div
nig
Certified-domain European options in the exponential NIG Levy model.
normal
overflow
phi2table
Fast bivariate normal CDF at fixed ρ via monotone bilinear lookup.
pool
rainbow
Two-asset (rainbow) option pricing via the bivariate normal CDF.
sabr
transcendental
trig

Structs§

BsFull
Complete Black-Scholes output: prices and all Greeks at SCALE.

Constants§

LN2_HP_LO
Sub-ULP residual for split LN2 at HP scale.
LN2_LO
Sub-ULP residual for split LN2 at standard scale. true_ln2 × SCALE ≈ LN2_I + LN2_LO / SCALE to sub-ULP precision. Replaces the crude -(k*55+500)/1000 correction in ln and exp.
LN_REMEZ_COEFFS
Remez degree-7 ln polynomial as array for compensated evaluation. Same coefficients as LN_REMEZ_W0..W7, ascending order.
LN_REMEZ_HP_COEFFS
HP Remez degree-9 ln polynomial as array for compensated evaluation. Same coefficients as LN_REMEZ_HP0..HP9, ascending order.
SCALE
Standard fixed-point scale factor: 1e12.
SCALE_I
Signed standard scale factor: 1e12.