Math & the Quote-Parity Guarantee
Zenith computes every price, fee, and share with integer-exact fixed-point math — no floating point anywhere in the money path. This is what lets the SDK promise that a quote equals the on-chain result to the token unit.
The core: crates/zenith-math
A pure Rust crate (no I/O, no Solana dependencies) with the shared primitives:
- Q64.64 fixed point — a
u128split into a 64-bit integer part and a 64-bit fraction. Prices, sqrt-prices, and fee-growth accumulators are Q64.64. - U256 — a 256-bit integer for intermediate products, so multiplying two Q64.64 values never overflows before the shift back down.
powand integersqrt— the proven-monotonic machinery behind both tick pricing and bin pricing.- Tick math —
sqrt_price_at_tick=sqrt(1.0001^tick), and its inversetick_at_sqrt_price(a floored binary search). Monotonic across the whole tick domain (±221_000), which is what makes the swap walk correct. - Fee / LP / yield math — fee splitting, fee-growth-inside (wrapping
u128), constant-product swaps, LP-share minting, and yield accrual.
Everything is deterministic and rounding is explicit (each function documents whether it rounds up or down), so the same inputs always give the same bits.
Rounding
Rounding direction is chosen to protect the pool, never the caller: deposits round in the pool's favor, withdrawals round down, fee growth truncates. The SDK mirrors the exact same rounding, so quotes agree with settlement rather than drifting by a unit.
Golden vectors
The crate emits its results as golden vector fixtures — large tables of input → output pairs generated from the Rust functions and written under sdk/test/fixtures/ (for example, hundreds of tick-math and constant-product vectors). The SDK's TypeScript ports re-check themselves against these vectors in CI. If a port ever diverges from the crate by a single bit, the SDK test suite fails.
This gives two independent implementations (Rust on-chain, TypeScript in the SDK) that are pinned to the same source of truth.
Parity against the real program
Golden vectors prove the math matches. A second layer proves the whole swap matches: sdk/localnet/parity.test.ts runs the SDK's quoteSwap against the real compiled program inside a local bankrun VM, and asserts:
- the realized on-chain output equals the quoted output exactly (ExactIn),
- the realized input equals the quoted input exactly (ExactOut),
- the pool's resulting price equals the quoted next price.
Run it locally after building the program .so:
cargo build-sbf --manifest-path programs/zenith-amm/Cargo.toml
cd sdk && npm run test:localnetThe DLMM and CAMM engines carry the same guarantee, verified additionally by devnet e2e scripts in sdk/scripts that compare a live on-chain swap to its quote.
Why this matters
An AMM whose quotes disagree with settlement leaks value and confuses users. Zenith treats quote/settlement equality as an invariant enforced by tests at two levels — vector parity for the primitives, and end-to-end parity for the full swap — rather than as something to eyeball.