mirror of
https://github.com/luxfi/math.git
synced 2026-07-27 03:38:49 +00:00
8e81daacc6bb29ca96347a45ab4e8971d0af3537
Migrates the canonical Lattigo-derived Montgomery NTT body from luxfi/lattice/v7/ring into luxfi/math/ntt/canonical. This is the ownership inversion LP-107 Phase 3 calls for: the math substrate now OWNS the canonical body, and luxfi/lattice/ring becomes a thin shim over it. ntt/canonical/ — new public package with the canonical body: * subring.go — SubRing struct + constructors (269 LoC) * ntt.go — NTT/INTT body, loop-unrolled-by-16 (1320 LoC) * ntt_simd.go — SIMD AVX2 dispatcher (405 LoC, build-tagged) * ntt_simd_stub.go — purego dispatcher stub for non-SIMD builds * modular_reduction.go — Barrett + Montgomery scalars * factorization.go — prime factorization helpers * utils.go — primitive root finders, parameter helpers * vec_ops.go — vectorized polynomial helpers * weierstrass.go — companion algebraic helpers ntt/purego.go — REWORK: the pureGoBackend now holds *canonical.SubRing instead of *lattice/v7/ring.SubRing; the lattice/v7/ring import is GONE. Cycle-free: GOWORK=off go list -deps github.com/luxfi/math/ntt shows zero github.com/luxfi/lattice imports. Total: ~3.7K LoC of canonical body migrated. Public API unchanged for downstream callers — luxfi/lattice/ring re-exports the same SubRing.NTT/INTT surface from the new owner location. Tests: GOWORK=off go test ./... in luxfi/math/.claude/worktrees/lp-107-phase3 ok github.com/luxfi/math/ntt (cached) ok github.com/luxfi/math/ntt/canonical 0.276s + every other package green.
Lux Math Library
A comprehensive mathematical utilities library for the Lux ecosystem.
Features
- Big integer utilities:
HexOrDecimal256,ParseBig256,U256,BigPowetc. - Safe arithmetic: Overflow-checked
SafeAdd,SafeSub,SafeMul - Bit operations: XOR, AND, compression utilities
- Set operations: Efficient set implementations including bit sets
- Data structures: Linked lists, hash maps, heaps
- Averagers: Time-windowed averaging utilities
Installation
go get github.com/luxfi/math
Package Structure
| Package | Description |
|---|---|
github.com/luxfi/math |
Root package with re-exports for backwards compatibility |
github.com/luxfi/math/big |
Big integer utilities (HexOrDecimal256, U256, parsing) |
github.com/luxfi/math/safe |
Overflow-safe arithmetic operations |
github.com/luxfi/math/bit |
Bit manipulation utilities |
github.com/luxfi/math/set |
Set data structures |
github.com/luxfi/math/linked |
Linked data structures |
github.com/luxfi/math/heap |
Heap implementations |
Usage
// Import root package (re-exports from subpackages)
import "github.com/luxfi/math"
// Or import specific subpackages directly
import (
"github.com/luxfi/math/big"
"github.com/luxfi/math/safe"
"github.com/luxfi/math/set"
)
Big Integer Operations
import "github.com/luxfi/math/big"
// Parse hex or decimal
val, _ := big.ParseBig256("0x1234")
// 256-bit unsigned operations
result := big.U256(someInt)
// Power operation
power := big.BigPow(2, 256)
Safe Arithmetic
import "github.com/luxfi/math/safe"
// Returns (result, overflow bool)
sum, overflow := safe.SafeAdd(a, b)
product, overflow := safe.SafeMul(x, y)
// Returns (result, error)
sum, err := safe.Add64(a, b)
License
See the LICENSE file for licensing terms.
Languages
Go
100%