ntt: rename canonical/ to subring/ - clearer naming for ring quotient

This commit is contained in:
2026-05-05 10:17:55 -07:00
parent 33d95498d6
commit 8212121173
12 changed files with 34 additions and 34 deletions
+14 -14
View File
@@ -7,22 +7,22 @@ import (
"fmt"
"sync"
"github.com/luxfi/math/ntt/canonical"
"github.com/luxfi/math/ntt/subring"
"github.com/luxfi/math/params"
)
// pureGoBackend is the canonical pure-Go NTT realization. It owns the
// Lattigo-derived Montgomery NTT body via the internal canonical
// package; lattice/v7/ring re-exports the same body for downstream
// consumers, so callers see no behavior change vs the v0.1.x lattice
// path.
// pureGoBackend is the pure-Go NTT realization. It owns the
// Lattigo-derived Montgomery NTT body via the subring package;
// lattice/v7/ring re-exports the same body for downstream
// consumers, so callers see no behavior change vs the v0.1.x
// lattice path.
//
// LP-107 Phase 3 (this file): the canonical kernel body now lives in
// luxfi/math/ntt/internal/canonical. luxfi/lattice/ring is a thin shim
// LP-107 Phase 3 (this file): the kernel body lives in
// luxfi/math/ntt/subring. luxfi/lattice/ring is a thin shim
// that delegates to it.
type pureGoBackend struct {
mu sync.RWMutex
subRings map[params.NTTParamID]*canonical.SubRing
subRings map[params.NTTParamID]*subring.SubRing
}
// PureGoBackend returns the singleton pure-Go NTT backend. Always
@@ -32,7 +32,7 @@ func PureGoBackend() Backend {
}
var thePureGo = pureGoBackend{
subRings: make(map[params.NTTParamID]*canonical.SubRing),
subRings: make(map[params.NTTParamID]*subring.SubRing),
}
func init() {
@@ -49,8 +49,8 @@ func (b *pureGoBackend) Supports(p *Params) bool {
return p != nil && p.Validate() == nil
}
// resolveSubRing returns or builds the cached *canonical.SubRing for p.
func (b *pureGoBackend) resolveSubRing(p *Params) (*canonical.SubRing, error) {
// resolveSubRing returns or builds the cached *subring.SubRing for p.
func (b *pureGoBackend) resolveSubRing(p *Params) (*subring.SubRing, error) {
b.mu.RLock()
sr, ok := b.subRings[p.ID]
b.mu.RUnlock()
@@ -62,9 +62,9 @@ func (b *pureGoBackend) resolveSubRing(p *Params) (*canonical.SubRing, error) {
if sr, ok := b.subRings[p.ID]; ok {
return sr, nil
}
sr, err := canonical.NewSubRing(int(p.N), p.Q)
sr, err := subring.NewSubRing(int(p.N), p.Q)
if err != nil {
return nil, fmt.Errorf("ntt(pure-go): canonical.NewSubRing(N=%d, Q=%d): %w",
return nil, fmt.Errorf("ntt(pure-go): subring.NewSubRing(N=%d, Q=%d): %w",
p.N, p.Q, err)
}
if err := sr.GenerateNTTConstants(); err != nil {
+5 -5
View File
@@ -1,8 +1,8 @@
// Copyright (c) 2026 Lux Industries Inc.
// SPDX-License-Identifier: BSD-3-Clause
// Package canonical owns the canonical Lattigo-derived Montgomery NTT
// body — SubRing layout, root-table generator, scalar primitives
// Package subring owns the Lattigo-derived Montgomery NTT body —
// SubRing layout, root-table generator, scalar primitives
// (MRed/MRedLazy/MForm/BRed/CRed/...), the loop-unrolled-by-16 NTT
// kernel, and the SIMD AVX2 dispatch hooks.
//
@@ -12,12 +12,12 @@
// luxfi/threshold, ...) compile unchanged. Application callers should
// route through one of:
//
// - github.com/luxfi/math/ntt — the canonical Service /
// Backend interface (recommended for new code).
// - github.com/luxfi/math/ntt — the Service / Backend
// interface (recommended for new code).
// - github.com/luxfi/lattice/v7/ring — the historical SubRing API
// for FHE-flavoured callers.
//
// This package is NOT a general-purpose import target: its surface is
// optimized for the two consumers above and is not part of the public
// luxfi/math semver contract for downstream code.
package canonical
package subring
@@ -1,12 +1,12 @@
// Body migrated from luxfi/lattice/v7/utils/factorization/factorization.go.
// LP-107 Phase 3 — moved into the canonical NTT package so the
// LP-107 Phase 3 — moved into the NTT package so the
// SubRing constructor can factor q-1 to find a primitive root without
// importing lattice.
//
// Renamed: IsPrime -> isPrimeBig (avoids collision with utils.IsPrime
// which takes uint64; both stay package-internal here).
package canonical
package subring
import (
"math"
@@ -3,10 +3,10 @@
//
// Body migrated from github.com/luxfi/lattice/v7/ring/modular_reduction.go
// (Lattigo-derived). LP-107 Phase 3 establishes luxfi/math as the
// canonical owner of the Montgomery / Barrett scalar primitives; the
// owner of the Montgomery / Barrett scalar primitives; the
// lattice copy now delegates to this package.
package canonical
package subring
import (
"math/big"
+2 -2
View File
@@ -2,10 +2,10 @@
// SPDX-License-Identifier: BSD-3-Clause
//
// Body migrated verbatim from luxfi/lattice/v7/ring/ntt.go.
// LP-107 Phase 3 — luxfi/math is the canonical owner of the
// LP-107 Phase 3 — luxfi/math is the owner of the
// Lattigo-derived Montgomery NTT body.
package canonical
package subring
import (
"fmt"
@@ -3,7 +3,7 @@
// SIMD-accelerated NTT body for AVX2 amd64 with GOEXPERIMENT=simd.
// LP-107 Phase 3 — verbatim from luxfi/lattice/v7/ring/ntt_simd.go.
package canonical
package subring
import (
"math/bits"
@@ -3,7 +3,7 @@
// SIMD stub for non-AVX2 / non-experimental-simd builds.
// LP-107 Phase 3 — verbatim from luxfi/lattice/v7/ring/ntt_simd_stub.go.
package canonical
package subring
// nttCoreLazyAccel is the stub for non-SIMD builds.
// Returns false to indicate no acceleration was applied.
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: BSD-3-Clause
//
// Body migrated from luxfi/lattice/v7/ring/subring.go.
// LP-107 Phase 3 — luxfi/math is the canonical owner of the Lattigo-derived
// LP-107 Phase 3 — luxfi/math is the owner of the Lattigo-derived
// Montgomery NTT body and SubRing layout. luxfi/lattice/ring re-exports.
//
// Changes from upstream:
@@ -10,7 +10,7 @@
// - utils.BitReverse64 -> BitReverse64 (same package)
// - bignum.NewInt(uint64) -> new(big.Int).SetUint64(...)
package canonical
package subring
import (
"fmt"
@@ -1,7 +1,7 @@
// Copyright (c) 2026 Lux Industries Inc.
// SPDX-License-Identifier: BSD-3-Clause
package canonical
package subring
import (
"math/rand/v2"
@@ -14,7 +14,7 @@ const (
pulsarQ = uint64(0x1000000004A01)
)
// TestSubRing_RoundTrip exercises the full canonical path:
// TestSubRing_RoundTrip exercises the full path:
// constructor + GenerateNTTConstants + forward NTT + inverse NTT.
func TestSubRing_RoundTrip(t *testing.T) {
sr, err := NewSubRing(pulsarN, pulsarQ)
@@ -4,7 +4,7 @@
// Helpers migrated from luxfi/lattice/v7/ring (utils.go, primes.go) and
// luxfi/lattice/v7/utils (BitReverse64). LP-107 Phase 3.
package canonical
package subring
import (
"math/big"
@@ -5,7 +5,7 @@
// LP-107 Phase 3 — only the helpers required by the NTT body
// (reducevec, mulscalarmontgomeryvec, mulscalarmontgomerylazyvec).
package canonical
package subring
import "unsafe"
@@ -2,7 +2,7 @@
// LP-107 Phase 3 — randInt inlined to drop the lattice/v7/utils/sampling
// import so this package has zero lattice dependencies.
package canonical
package subring
import (
"crypto/rand"