https://github.com/dijkstra-keystone/keystone
Deterministic precision arithmetic for financial computation and verifiable systems
https://github.com/dijkstra-keystone/keystone
defi deterministic financial-computation no-std precision-arithmetic rust wasm
Last synced: 20 days ago
JSON representation
Deterministic precision arithmetic for financial computation and verifiable systems
- Host: GitHub
- URL: https://github.com/dijkstra-keystone/keystone
- Owner: dijkstra-keystone
- License: apache-2.0
- Created: 2026-01-24T12:24:54.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2026-01-29T21:50:06.000Z (5 months ago)
- Last Synced: 2026-06-09T18:21:26.647Z (26 days ago)
- Topics: defi, deterministic, financial-computation, no-std, precision-arithmetic, rust, wasm
- Language: Rust
- Homepage: https://crates.io/crates/precision-core
- Size: 215 KB
- Stars: 4
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# Keystone
Deterministic precision arithmetic for financial computation and verifiable systems.
[](https://github.com/dijkstra-keystone/keystone/actions/workflows/ci.yml)
[](https://crates.io/crates/precision-core)
[](https://docs.rs/precision-core)
[](LICENSE-MIT)
## Why Keystone?
DeFi protocols make critical decisions—liquidations, pricing, risk assessments—based on mathematical calculations. When your laptop, a validator node, and a WASM frontend compute the same formula and get *different* results due to floating-point inconsistencies, bad things happen.
Keystone guarantees **bit-identical results** across x86, ARM, and WASM. No floating-point surprises. No platform-dependent rounding. The same input produces the same output, every time, everywhere.
## Overview
Keystone provides a suite of libraries for financial calculations with guaranteed determinism across platforms. Built for DeFi applications, verifiable computation, and any system requiring bit-exact reproducibility.
## Crates
| Crate | Description |
|-------|-------------|
| [precision-core](crates/precision-core) | 128-bit decimal arithmetic with 7 rounding modes |
| [financial-calc](crates/financial-calc) | Interest, time value, options pricing, percentages |
| [risk-metrics](crates/risk-metrics) | Health factor, liquidation, and position metrics |
| [keystone-wasm](crates/wasm-bindings) | WebAssembly bindings for browser usage |
## Stylus Examples
Ready-to-deploy examples for Arbitrum Stylus:
| Example | Description |
|---------|-------------|
| [stylus-lending](examples/stylus-lending) | Health factor and liquidation calculations |
| [stylus-amm](examples/stylus-amm) | Constant product AMM math |
| [stylus-vault](examples/stylus-vault) | ERC4626-style vault calculations |
### Deployed on Arbitrum One
| Contract | Address |
|----------|---------|
| stylus-lending | [`0x4dff9348275ac3c24e2d3abf54af61d3ebee1585`](https://arbiscan.io/address/0x4dff9348275ac3c24e2d3abf54af61d3ebee1585) |
| stylus-amm | [`0x9615cc2f65d8bbe4cdc80343db75a6ec32da93cd`](https://arbiscan.io/address/0x9615cc2f65d8bbe4cdc80343db75a6ec32da93cd) |
| stylus-vault | [`0xdaf8f1a5f8025210f07665d4ccf2d2c0622a41fa`](https://arbiscan.io/address/0xdaf8f1a5f8025210f07665d4ccf2d2c0622a41fa) |
## Features
- **Deterministic**: Identical results on all platforms (x86, ARM, WASM)
- **no_std**: Core library works in embedded and WASM environments
- **Precise**: 128-bit decimals with up to 28 significant digits
- **Safe**: `#![forbid(unsafe_code)]` throughout
- **Financial**: Banker's rounding and 6 other rounding modes
- **Fast**: Nanosecond-level operations (~8ns add, ~36ns divide)
## Quick Start
```rust
use precision_core::{Decimal, RoundingMode};
// Create decimals
let price = Decimal::new(9999, 2); // 99.99
let quantity = Decimal::from(5i64);
let tax_rate = Decimal::new(825, 4); // 8.25%
// Calculate
let subtotal = price.checked_mul(quantity)?;
let tax = subtotal.checked_mul(tax_rate)?;
let total = subtotal.checked_add(tax)?;
// Round for display
let display = total.round(2, RoundingMode::HalfUp);
```
## WASM Usage
```javascript
import * as keystone from '@dijkstra-keystone/wasm';
const subtotal = keystone.multiply("99.99", "5");
const tax = keystone.multiply(subtotal, "0.0825");
const total = keystone.add(subtotal, tax);
const display = keystone.round(total, 2, "half_up");
```
## Documentation
- [Online Documentation](https://docs.dijkstrakeystone.com)
- [API Reference](https://docs.rs/precision-core)
## Building
```bash
# Run tests
cargo test --all
# Build WASM
cd crates/wasm-bindings
wasm-pack build --target web --release
# Build documentation
cd docs && mdbook build
```
## Benchmarks
| Operation | Time |
|-----------|------|
| Addition | ~8 ns |
| Multiplication | ~8 ns |
| Division | ~36 ns |
| compound_interest | ~850 ns |
```bash
cargo bench
```
## License
Licensed under either of:
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE))
- MIT License ([LICENSE-MIT](LICENSE-MIT))
at your option.