Files
lattice/ring/modular_reduction.go
zeekay 241e161307 Pin luxfi/math v1.4.0; replace with local working tree
v2.0.0+incompatible was a placeholder; no v2 tag exists. Pinned to
v1.4.0 (current canonical). Replace points at the local math working
tree because lattice imports ntt/subring which lives in uncommitted
canonical-relocation work.
2026-05-05 10:13:37 -07:00

86 lines
2.7 KiB
Go

// Copyright (c) 2026 Lux Industries Inc.
// SPDX-License-Identifier: BSD-3-Clause
//
// Thin shim over github.com/luxfi/math/ntt/subring.
// LP-107 Phase 3 inverted the dependency: luxfi/math owns the
// Montgomery / Barrett body. This package re-exports it so
// downstream consumers (luxfi/pulsar, luxfi/fhe, luxfi/threshold,
// luxfi/multiparty, ...) see no source change.
package ring
import (
"github.com/luxfi/math/ntt/subring"
)
// MForm switches a to the Montgomery domain by computing a*2^64 mod q.
func MForm(a, q uint64, bredconstant [2]uint64) uint64 {
return subring.MForm(a, q, bredconstant)
}
// MFormLazy switches a to the Montgomery domain by computing
// a*2^64 mod q in constant time. Result in [0, 2q-1].
func MFormLazy(a, q uint64, bredconstant [2]uint64) uint64 {
return subring.MFormLazy(a, q, bredconstant)
}
// IMForm switches a from the Montgomery domain back to the standard
// domain by computing a*(1/2^64) mod q.
func IMForm(a, q, mredconstant uint64) uint64 {
return subring.IMForm(a, q, mredconstant)
}
// IMFormLazy switches a from the Montgomery domain back to the
// standard domain by computing a*(1/2^64) mod q in constant time.
// Result in [0, 2q-1].
func IMFormLazy(a, q, mredconstant uint64) uint64 {
return subring.IMFormLazy(a, q, mredconstant)
}
// GenMRedConstant computes the constant (q^-1) mod 2^64 used by MRed.
func GenMRedConstant(q uint64) uint64 {
return subring.GenMRedConstant(q)
}
// MRed computes x * y * (1/2^64) mod q.
func MRed(x, y, q, mredconstant uint64) uint64 {
return subring.MRed(x, y, q, mredconstant)
}
// MRedLazy computes x * y * (1/2^64) mod q in constant time.
// Result in [0, 2q-1].
func MRedLazy(x, y, q, mredconstant uint64) uint64 {
return subring.MRedLazy(x, y, q, mredconstant)
}
// GenBRedConstant computes the constant pair for the BRed algorithm.
// Returns ((2^128)/q)/(2^64) and (2^128)/q mod 2^64.
func GenBRedConstant(q uint64) [2]uint64 {
return subring.GenBRedConstant(q)
}
// BRedAdd computes a mod q.
func BRedAdd(a, q uint64, bredconstant [2]uint64) uint64 {
return subring.BRedAdd(a, q, bredconstant)
}
// BRedAddLazy computes a mod q in constant time. Result in [0, 2q-1].
func BRedAddLazy(x, q uint64, bredconstant [2]uint64) uint64 {
return subring.BRedAddLazy(x, q, bredconstant)
}
// BRed computes x*y mod q.
func BRed(x, y, q uint64, bredconstant [2]uint64) uint64 {
return subring.BRed(x, y, q, bredconstant)
}
// BRedLazy computes x*y mod q in constant time. Result in [0, 2q-1].
func BRedLazy(x, y, q uint64, bredconstant [2]uint64) uint64 {
return subring.BRedLazy(x, y, q, bredconstant)
}
// CRed reduces a in [0, 2q-1] to [0, q-1].
func CRed(a, q uint64) uint64 {
return subring.CRed(a, q)
}