forked from tuneinsight/lattigo
/
lut.go
34 lines (26 loc) · 1.05 KB
/
lut.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Package lut implements look-up tables evaluation for R-LWE schemes.
package lut
import (
"github.com/tuneinsight/lattigo/v4/ring"
)
// InitLUT takes a function g, and creates a LUT polynomial for the function in the interval [a, b].
// Inputs to the LUT evaluation are assumed to have been normalized with the change of basis (2*x - a - b)/(b-a).
// Interval [a, b] should take into account the "drift" of the value x, caused by the change of modulus from Q to 2N.
func InitLUT(g func(x float64) (y float64), scale float64, ringQ *ring.Ring, a, b float64) (F *ring.Poly) {
F = ringQ.NewPoly()
Q := ringQ.Modulus
// Discretization interval
interval := 2.0 / float64(ringQ.N)
for j, qi := range Q {
// Interval [-1, 0] of g(x)
for i := 0; i < (ringQ.N>>1)+1; i++ {
F.Coeffs[j][i] = scaleUp(g(normalizeInv(-interval*float64(i), a, b)), scale, qi)
}
// Interval ]0, 1[ of g(x)
for i := (ringQ.N >> 1) + 1; i < ringQ.N; i++ {
F.Coeffs[j][i] = scaleUp(-g(normalizeInv(interval*float64(ringQ.N-i), a, b)), scale, qi)
}
}
ringQ.NTT(F, F)
return
}