Commit Graph
34 Commits
Author SHA1 Message Date
Hanzo AI fbe1648f8a LP-107 Phase 7: cross-runtime KAT release gate
codec/kat.go: KATEntry + KATBundle types with canonical JSON
serialization. Header field embeds params.KATHeader so every entry
carries (parameter_set, modulus_id, backend_id, hash_suite_id,
implementation, version) — required by LP-107 §"Parameter registry"
for cross-runtime equality.

codec/cmd/emit_codec_kat/main.go: emits 5 canonical entries:
  1. ReadUint64Slice/happy-path/3-elements
  2. ReadUint64Slice/empty
  3. ReadUint64Slice/reject/lattice-issue-4-70T  (regression: huge
     length must be rejected by both runtimes)
  4. ReadUint16Slice/happy-path/5-elements
  5. ReadUint32Slice/happy-path/4-elements

Each entry records:
  - InputHex: the wire-format bytes the Reader consumes
  - OutputHex: the canonical output byte stream (or "REJECTED")
  - OutputSHA256: commitment for fast cross-runtime equality

codec/kat_test.go: TestKATBundle_RoundTrip replays every bundle
entry through the Go Reader and asserts byte-equality + SHA-256
commitment match. PASS in 0.29s.

codec/testdata/codec_kat.json: the Go-emitted bundle. The C++ side
at luxcpp/crypto/math/test/codec_cross_runtime_test.cpp loads the
same file and replays every entry; in this commit, all 5 entries
PASS byte-equal between runtimes.

This closes LP-107 Phase 7 for codec/. Subsequent commits extend the
gate to modarith/, ntt/, poly/, sample/ KATs.
v1.3.0
2026-05-04 09:43:59 -07:00
Hanzo AI 6261d9a310 LP-107 Phase 2: math substrate — params, backend, codec, modarith,
ntt, poly, rns, sample

Eight pure-Go reference packages that own the canonical semantics of
every cryptographic-math primitive Lux protocols share. Backends
(AVX2 / NEON / cgo+C++ / CUDA / Metal / WGSL) plug in behind the
substrate; KATs gate byte-equality across them.

Packages added:

* params/    — ModulusID, NTTParamID, FHEParamID, PulsarParamID,
               HashSuiteID, BackendID; KATHeader required-fields
               schema. Stable string IDs; renaming is a breaking
               change.

* backend/   — Policy enum (PureGo / NativeCPU / GPUPreferred /
               GPURequired); Resolve(policy, registered) returns the
               BackendID per LP-107's fallback chain. GPURequired
               returns ErrUnavailable when no GPU backend is
               registered.

* codec/     — Bounded readers (closes lattice issues #2 + #4 DoS
               class permanently). Limits + LimitError + Reader.
               ReadUint{16,32,64}Slice all reject the 70T-element
               attack input before allocating; depth + frame-bytes
               caps; iterative (no recursion). Regression test
               TestReadUint64Slice_RejectsHugeLength encodes the
               original lattice issue #4 input.

* modarith/  — Modulus type with QInv, R2, Barrett constants
               (computed via math/big once per modulus). AddMod /
               SubMod / MulMod (Div64 reference path). MontMulMod /
               ToMontgomery / FromMontgomery cross-checked against
               MulMod across 100 random Pulsar-Q pairs.
               ReductionMode enum byte-equal to lattice/types.

* ntt/       — Service + Backend interface; pure-Go backend
               delegates to lattice/v7/ring.SubRing.NTT/INTT (the
               canonical Lattigo-derived Montgomery NTT body — no
               re-implementation, just dispatch). Round-trip +
               batch + determinism tests on Pulsar N=256.

* poly/      — Add / Sub / ScalarMul / PointwiseMul (NTT-domain) /
               negacyclic Mul (NTT round-trip). Composes modarith +
               ntt; no lower-level reach.

* rns/       — Basis(Moduli, Name) for FHE RNS chains. Single +
               two-prime construction validated; rejects even moduli.
               Phase 3 will add basis-extension and modulus-switching.

* sample/    — Uniform (rejection sampling, mask-and-retry); Ternary
               (density + sign byte); CenteredBinomial (popcount
               difference); DiscreteGaussianRejection (6-sigma cutoff).
               All take an io.Reader so callers can KAT-replay.

Architecture invariants enforced in package docs:

  - Go is the canonical semantic reference.
  - Backend selection MUST NOT alter transcript bytes (consensus
    paths default to PolicyPureGo / PolicyNativeCPU).
  - No unbounded codec readers anywhere near wire formats.
  - No re-implementation: where lattice/v7/ring already owns the
    canonical body, we delegate, not fork.
  - One ID space; renaming is a breaking change.

Test posture: 8/8 packages green via `GOWORK=off go test ./...`.

Phases 3-7 (queued):
  3. lattice consumes math
  4. pulsar consumes lattice + math
  5. fhe consumes math
  6. luxcpp/crypto/math native backend mirror
  7. cross-runtime KAT release gate

See SUBSTRATE.md for the full posture and migration plan; full LP at
~/work/lux/lps/LP-107-lux-math-substrate.md.
2026-05-04 09:28:23 -07:00
Hanzo AI 8d638e5218 deps: update go.mod/go.sum 2026-04-19 17:04:50 -07:00
Hanzo AI f11d8d0eab chore: update dependencies 2026-04-01 14:03:38 -07:00
Hanzo Dev c7436768c8 chore: sync local changes 2026-03-26 22:47:13 -07:00
Hanzo Dev 647262cb63 chore: bump Go 1.26.0 → 1.26.1
Fixes 5 stdlib CVEs (html/template, os, net/url, crypto/x509 x2).
2026-03-12 01:09:17 -07:00
Hanzo Dev 7db778821a chore: update Go module dependencies 2026-03-03 22:39:12 -08:00
Zach Kelling 4adc036b28 chore: add BSD-3-Clause LICENSE 2026-02-14 05:26:21 -08:00
Zach Kelling f80474aedf chore: sync dependencies and format code v1.2.4 2026-02-04 15:43:42 -08:00
Zach Kelling a2c22dbba7 chore: remove local replace directives v1.2.3 2026-01-08 23:44:14 -08:00
Zach Kelling 21b7243e9f refactor: remove local replace, use published big v0.1.0 v1.2.2 safe/v0.0.1 v1.2.1 2026-01-05 01:38:02 -08:00
Zach Kelling 356605c2c1 chore: update go.mod/sum big/v0.1.0 2026-01-04 19:29:50 -08:00
Zach Kelling 8edeed68cd safe: add DEX math functions for DRY consolidation
Added functions from node/vms/dexvm/math:
- Div64: safe division with error on zero
- MulBig: multiply uint64s returning big.Int
- MulDiv64: (a * b) / c without overflow
- MulDivRoundUp64: ceiling division variant
- BigMulDiv: big.Int multiplication+division
- BigMulDivRoundUp: big.Int ceiling variant
- Clamp: generic ordered type clamping

Also added common big.Int values (bigZero, bigOne, maxUint64Big)
for performance in division operations.
2026-01-03 18:53:26 -08:00
Zach Kelling fd27f2ecb0 Reorganize math package into subpackages
- Create math/big for big.Int utilities (HexOrDecimal256, U256, parsing)
- Create math/safe for overflow-safe arithmetic (SafeAdd, SafeMul, etc.)
- Move averager files to root package level
- Add re-exports in root package for backwards compatibility
- Update README with new package structure documentation
- Remove old math/meter directory (consolidated elsewhere)
- Remove redundant math/ subdirectory structure
2026-01-03 18:22:33 -08:00
Zach Kelling 7870d52f6f Remove units/ - use luxfi/const for denominations 2026-01-03 17:39:26 -08:00
Zach Kelling 45e2df82ae Add big.Int and integer utilities from geth/common/math
- big.go: BigPow, ParseBig256, U256, U256Bytes, PaddedBigBytes, etc.
- integer.go: HexOrDecimal64, ParseUint64, SafeAdd/Sub/Mul

Consolidating math utilities into luxfi/math to eliminate
geth/common/math dependency.
2026-01-03 17:36:34 -08:00
Zach Kelling 517b493821 Add bit and units subpackages, remove safemath wrapper
- Add math/bit: XOR, AND, compress operations (from bitutil)
- Add math/units: Lux denomination constants (NanoLux, MicroLux, etc.)
- Remove safemath/: was just re-exporting geth/common/math
- Use github.com/luxfi/geth/common/math directly for safe math ops
2026-01-03 15:52:15 -08:00
Zach Kelling 8e13df0a8b chore: update go.mod 2025-12-31 22:21:37 -08:00
Zach Kelling 107c43637e feat: add heap, linked, sorting, zero - remove utils dependency
- Add Zero[T any]() generic function for zero values
- Add IsSortedBytes for byte slice sorting checks
- Add heap/queue.go with priority Queue[T] implementation
- Add heap/map.go with indexed Map[K,V] for heap operations
- Add linked/list.go with doubly-linked List[T]
- Add linked/hashmap.go with ordered HashMap[K,V]
- Update sampleable_set.go to use local luxmath.Zero[T]()
- Update averager_heap.go to use github.com/luxfi/math/heap
- Update safe_math.go to use luxmath.Zero[T]()
- Remove github.com/luxfi/utils dependency entirely
v1.2.0
2025-12-24 15:06:41 -08:00
Zach Kelling 086468c3ca refactor: remove node dependency, use extracted packages
- Replace github.com/luxfi/node/utils with github.com/luxfi/utils
- Replace github.com/luxfi/node/utils/sampler with github.com/luxfi/sampler
- Replace github.com/luxfi/node/utils/wrappers with github.com/luxfi/utils/wrappers
- Replace github.com/luxfi/node/utils/json with github.com/luxfi/utils/json
v1.0.5
2025-12-24 14:12:00 -08:00
Zach Kelling 5675cd73a6 deps: upgrade crypto to v1.17.25 v1.1.1 2025-12-21 11:11:46 -08:00
Zach Kelling 83bf9799d5 Add generic safe math operations
Provides overflow/underflow protected Add, Sub, Mul operations
with generic constraints for all unsigned integer types.
v1.1.0
2025-12-19 08:45:58 -08:00
Zach Kelling 8015ec9f82 Update dependencies to latest versions v1.0.2 2025-12-13 10:42:20 +00:00
Zach Kelling 9cf2d96eec Remove local replace, update to latest luxfi versions
- Remove local replace directive for luxfi/utils
- Use published github.com/luxfi/utils v1.0.0
- Update github.com/luxfi/geth: v1.16.34 -> v1.16.43
- Update github.com/luxfi/ids: v1.0.2 -> v1.1.4
- Update github.com/luxfi/crypto: v1.2.9 -> v1.17.15
- Update Go version to 1.25.5
- All tests pass
v1.0.1
2025-12-11 05:44:25 +00:00
Hanzo Dev 7ec590f9f9 Release v1.0.0 v1.0.0 2025-12-04 08:38:47 -08:00
Hanzo Dev 7fbb28a575 feat: math v0.1.5 - set improvements and documentation
- Add clear method to Set type
- Add documentation site structure
- Update Go module version
v0.1.5
2025-12-03 23:58:07 -08:00
Hanzo Dev 84ce29ae02 Add Peek method to Set v0.1.4 2025-10-30 12:33:06 -07:00
Hanzo Dev c25b6e7f89 Update go.mod v0.1.3 2025-10-30 12:12:02 -07:00
Hanzo Dev a154081bc1 Add missing Set methods and fix tests
- Make Add() variadic to accept multiple elements
- Add Overlaps() method to check for common elements
- Add Pop() method to remove and return an element
- Add NewSet() constructor with optional initial capacity
- Implement MarshalJSON/UnmarshalJSON for JSON serialization
- Fix test to use return values from Union/Difference

All tests now passing.
2025-10-28 09:59:35 -07:00
Hanzo Dev 93e59bf1d4 Update set implementation and add documentation 2025-10-28 09:59:35 -07:00
Zach Kelling bd2d8bed31 Update go.mod dependencies 2025-09-24 02:39:13 +00:00
Zach Kelling 2a3a7e8a14 chore: standardize on Go 1.25.1 2025-09-21 08:44:30 +00:00
Zach Kelling 4ba63b8a06 fix: set Go version to 1.22.8 for compatibility v0.1.1 2025-09-21 07:58:55 +00:00
Hanzo Dev f271a0362b Initial commit: Lux math library v0.1.0 2025-08-18 13:18:19 -05:00