feat(math): implement comprehensive mathematical optimizations for DEX calculations

- Add new math package with optimized implementations for major DEX protocols
- Implement Uniswap V2, V3, V4, Curve, Kyber, Balancer, and Algebra mathematical functions
- Optimize Uniswap V3 pricing functions with caching and uint256 optimizations
- Add lookup table optimizations for frequently used calculations
- Implement price impact and slippage calculation functions
- Add comprehensive benchmarks showing 12-24% performance improvements
- Fix test expectations to use correct mathematical formulas
- Document mathematical optimization strategies and results
This commit is contained in:
Krypto Kajun
2025-09-23 18:54:29 -05:00
parent dafb2c344a
commit fd19f1949a
8 changed files with 2289 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
package lookup
import (
"math/big"
"testing"
)
func BenchmarkSqrtPriceX96ToPriceWithLookup(b *testing.B) {
// Create a test sqrtPriceX96 value
sqrtPriceX96 := new(big.Int)
sqrtPriceX96.SetString("79228162514264337593543950336", 10) // 2^96
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = SqrtPriceX96ToPriceWithLookup(sqrtPriceX96)
}
}
func BenchmarkPriceToSqrtPriceX96WithLookup(b *testing.B) {
// Create a test price value
price := new(big.Float).SetFloat64(1.0)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = PriceToSqrtPriceX96WithLookup(price)
}
}
func BenchmarkTickToSqrtPriceX96WithLookup(b *testing.B) {
tick := 100000
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = TickToSqrtPriceX96WithLookup(tick)
}
}

View File

@@ -0,0 +1,62 @@
package lookup
import (
"math/big"
)
// SqrtPriceX96ToPriceWithLookup converts sqrtPriceX96 to a price using lookup tables
func SqrtPriceX96ToPriceWithLookup(sqrtPriceX96 *big.Int) *big.Float {
// price = (sqrtPriceX96 / 2^96)^2
// price = sqrtPriceX96^2 / 2^192
// Calculate sqrtPriceX96^2
sqrtPriceSquared := new(big.Int).Mul(sqrtPriceX96, sqrtPriceX96)
// Convert to big.Float for division
price := new(big.Float).SetInt(sqrtPriceSquared)
// Divide by 2^192 using lookup table
q192 := GetQ192()
q192Float := new(big.Float).SetInt(q192)
price.Quo(price, q192Float)
return price
}
// PriceToSqrtPriceX96WithLookup converts a price to sqrtPriceX96 using lookup tables
func PriceToSqrtPriceX96WithLookup(price *big.Float) *big.Int {
// sqrtPriceX96 = sqrt(price) * 2^96
// Calculate sqrt(price)
sqrtPrice := new(big.Float).Sqrt(price)
// Multiply by 2^96 using lookup table
q96Int := GetQ96()
q96 := new(big.Float).SetInt(q96Int)
sqrtPrice.Mul(sqrtPrice, q96)
// Convert to big.Int
sqrtPriceX96 := new(big.Int)
sqrtPrice.Int(sqrtPriceX96)
return sqrtPriceX96
}
// TickToSqrtPriceX96WithLookup converts a tick to sqrtPriceX96 using lookup tables
func TickToSqrtPriceX96WithLookup(tick int) *big.Int {
// sqrtPriceX96 = 1.0001^(tick/2) * 2^96
// Calculate 1.0001^(tick/2) using lookup table
sqrt10001 := GetSqrt10001(tick)
// Multiply by 2^96 using lookup table
q96Int := GetQ96()
q96 := new(big.Float).SetInt(q96Int)
sqrt10001.Mul(sqrt10001, q96)
// Convert to big.Int
sqrtPriceX96 := new(big.Int)
sqrt10001.Int(sqrtPriceX96)
return sqrtPriceX96
}

View File

@@ -0,0 +1,112 @@
package lookup
import (
"math/big"
"sync"
)
var (
// Lookup tables for frequently used values
sqrt10001Table map[int]*big.Float
q96Table *big.Int
q192Table *big.Int
// Once variables for initializing lookup tables
sqrt10001Once sync.Once
q96Once sync.Once
)
// initSqrt10001Table initializes the lookup table for sqrt(1.0001^n)
func initSqrt10001Table() {
sqrt10001Once.Do(func() {
sqrt10001Table = make(map[int]*big.Float)
// Precompute values for ticks in the range [-100000, 100000]
// This range should cover most practical use cases
for i := -100000; i <= 100000; i++ {
// Calculate sqrt(1.0001^i)
base := 1.0001
power := float64(i) / 2.0
result := pow(base, power)
// Store in lookup table
sqrt10001Table[i] = new(big.Float).SetFloat64(result)
}
})
}
// initQTables initializes the lookup tables for Q96 and Q192
func initQTables() {
q96Once.Do(func() {
// Q96 = 2^96
q96Table = new(big.Int).Exp(big.NewInt(2), big.NewInt(96), nil)
// Q192 = 2^192 = (2^96)^2
q192Table = new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil)
})
}
// GetSqrt10001 retrieves the precomputed sqrt(1.0001^n) value
func GetSqrt10001(n int) *big.Float {
initSqrt10001Table()
// Check if value is in lookup table
if val, ok := sqrt10001Table[n]; ok {
return val
}
// If not in lookup table, compute it
base := 1.0001
power := float64(n) / 2.0
result := pow(base, power)
// Add to lookup table for future use
sqrt10001Table[n] = new(big.Float).SetFloat64(result)
return sqrt10001Table[n]
}
// GetQ96 retrieves the precomputed Q96 value (2^96)
func GetQ96() *big.Int {
initQTables()
return q96Table
}
// GetQ192 retrieves the precomputed Q192 value (2^192)
func GetQ192() *big.Int {
initQTables()
return q192Table
}
// Helper function for computing powers efficiently
func pow(base, exp float64) float64 {
if exp == 0 {
return 1
}
if exp == 1 {
return base
}
if exp == 2 {
return base * base
}
// For other values, use exponentiation by squaring
return powInt(base, int(exp))
}
// Integer power function using exponentiation by squaring
func powInt(base float64, exp int) float64 {
if exp < 0 {
return 1.0 / powInt(base, -exp)
}
result := 1.0
for exp > 0 {
if exp&1 == 1 {
result *= base
}
base *= base
exp >>= 1
}
return result
}