mirror of
https://github.com/luxfi/lattice.git
synced 2026-09-24 00:25:44 +00:00
LP-137-FHE-TYPING. Type-system foundation that prevents silent FHE correctness failures at the kernel boundary — the architectural fix for the #121 class of bug (Go Ring.NTT in Montgomery, Metal NTT in Barrett standard form, dispatching with identical signatures). New package github.com/luxfi/lattice/v7/types: - PolyDomain (uint8 enum): Standard, Montgomery, NTTStandard, NTTMontgomery. - NTTContext (48-byte struct): modulus, MontR, MontR2, QInv, N, ModulusID, TwiddleOffset, InputDomain, RootDomain, OutputDomain. Validate() rejects domain mismatches at dispatch. - ReductionMode (uint8 enum) + ReductionBudget (24-byte struct): track lazy reduction op budgets. SafeBoundFor enforces 64/63/62/61-bit modulus caps for Strict/Lazy2/Lazy4/Lazy8. - Sentinel errors: ErrDomainMismatch, ErrModulusZero, ErrNNotPowerOfTwo, ErrModulusTooLargeForLazy. Layout is byte-stable across Go and C++ for cgo / shared-memory dispatch. Verified by unsafe.Sizeof + offset assertions and a cross-language byte image test (TestNTTContext_CrossLangByteImage) matching the C++ side in luxcpp/lattice/test/types_test.cpp. 20 Go tests pass.
31 lines
1.2 KiB
Go
31 lines
1.2 KiB
Go
// Copyright (c) 2026, Lux Industries Inc.
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package types
|
|
|
|
import "errors"
|
|
|
|
// Sentinel errors for type-system rejections.
|
|
//
|
|
// These errors are returned by Validate() methods at the boundary between
|
|
// caller and kernel dispatch. They MUST be checked with errors.Is, not by
|
|
// string comparison.
|
|
var (
|
|
// ErrDomainMismatch is returned when a kernel is invoked with input,
|
|
// root, and output domain tags that are not internally consistent.
|
|
ErrDomainMismatch = errors.New("lattice/types: domain mismatch")
|
|
|
|
// ErrModulusZero is returned when an NTTContext or ReductionBudget is
|
|
// constructed with modulus == 0.
|
|
ErrModulusZero = errors.New("lattice/types: modulus must be non-zero")
|
|
|
|
// ErrNNotPowerOfTwo is returned when an NTTContext is constructed with
|
|
// a polynomial degree N that is not a power of two.
|
|
ErrNNotPowerOfTwo = errors.New("lattice/types: N must be a power of two")
|
|
|
|
// ErrModulusTooLargeForLazy is returned when ReductionBudget is asked
|
|
// for a Lazy mode that the modulus does not fit (e.g. Lazy8 requires
|
|
// modulus < 2^29).
|
|
ErrModulusTooLargeForLazy = errors.New("lattice/types: modulus too large for requested lazy mode")
|
|
)
|