-
Notifications
You must be signed in to change notification settings - Fork 179
/
automorphism.go
173 lines (125 loc) · 4.32 KB
/
automorphism.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package ring
import (
"fmt"
"math/bits"
"unsafe"
"github.com/tuneinsight/lattigo/v5/utils"
)
// AutomorphismNTTIndex computes the look-up table for the automorphism X^{i} -> X^{i*k mod NthRoot}.
func AutomorphismNTTIndex(N int, NthRoot, GalEl uint64) (index []uint64, err error) {
if N&(N-1) != 0 {
return nil, fmt.Errorf("N must be a power of two")
}
if NthRoot&(NthRoot-1) != 0 {
return nil, fmt.Errorf("NthRoot must be w power of two")
}
var mask, tmp1, tmp2 uint64
logNthRoot := int(bits.Len64(NthRoot-1) - 1)
mask = NthRoot - 1
index = make([]uint64, N)
for i := 0; i < N; i++ {
tmp1 = 2*utils.BitReverse64(i, logNthRoot) + 1
tmp2 = ((GalEl * tmp1 & mask) - 1) >> 1
index[i] = utils.BitReverse64(tmp2, logNthRoot)
}
return
}
// AutomorphismNTT applies the automorphism X^{i} -> X^{i*gen} on a polynomial in the NTT domain.
// It must be noted that the result cannot be in-place.
func (r Ring) AutomorphismNTT(polIn Poly, gen uint64, polOut Poly) {
index, err := AutomorphismNTTIndex(r.N(), r.NthRoot(), gen)
// Sanity check, this error should not happen.
if err != nil {
panic(err)
}
r.AutomorphismNTTWithIndex(polIn, index, polOut)
}
// AutomorphismNTTWithIndex applies the automorphism X^{i} -> X^{i*gen} on a polynomial in the NTT domain.
// `index` is the lookup table storing the mapping of the automorphism.
// It must be noted that the result cannot be in-place.
func (r Ring) AutomorphismNTTWithIndex(polIn Poly, index []uint64, polOut Poly) {
level := r.level
N := r.N()
for j := 0; j < N; j = j + 8 {
/* #nosec G103 -- behavior and consequences well understood, possible buffer overflow if len(index)%8 != 0 */
x := (*[8]uint64)(unsafe.Pointer(&index[j]))
for i := 0; i < level+1; i++ {
/* #nosec G103 -- behavior and consequences well understood, possible buffer overflow if len(polOut.Coeffs)%8 != 0 */
z := (*[8]uint64)(unsafe.Pointer(&polOut.Coeffs[i][j]))
y := polIn.Coeffs[i]
z[0] = y[x[0]]
z[1] = y[x[1]]
z[2] = y[x[2]]
z[3] = y[x[3]]
z[4] = y[x[4]]
z[5] = y[x[5]]
z[6] = y[x[6]]
z[7] = y[x[7]]
}
}
}
// AutomorphismNTTWithIndexThenAddLazy applies the automorphism X^{i} -> X^{i*gen} on a polynomial in the NTT domain .
// `index` is the lookup table storing the mapping of the automorphism.
// The result of the automorphism is added on polOut.
func (r Ring) AutomorphismNTTWithIndexThenAddLazy(polIn Poly, index []uint64, polOut Poly) {
level := r.level
N := r.N()
for j := 0; j < N; j = j + 8 {
/* #nosec G103 -- behavior and consequences well understood, possible buffer overflow if len(index)%8 != 0 */
x := (*[8]uint64)(unsafe.Pointer(&index[j]))
for i := 0; i < level+1; i++ {
/* #nosec G103 -- behavior and consequences well understood, possible buffer overflow if len(polOut.Coeffs)%8 != 0 */
z := (*[8]uint64)(unsafe.Pointer(&polOut.Coeffs[i][j]))
y := polIn.Coeffs[i]
z[0] += y[x[0]]
z[1] += y[x[1]]
z[2] += y[x[2]]
z[3] += y[x[3]]
z[4] += y[x[4]]
z[5] += y[x[5]]
z[6] += y[x[6]]
z[7] += y[x[7]]
}
}
}
// Automorphism applies the automorphism X^{i} -> X^{i*gen} on a polynomial outside of the NTT domain.
// It must be noted that the result cannot be in-place.
func (r Ring) Automorphism(polIn Poly, gen uint64, polOut Poly) {
var mask, index, indexRaw, logN, tmp uint64
N := uint64(r.N())
level := r.level
if r.Type() == ConjugateInvariant {
mask = 2*N - 1
logN = uint64(bits.Len64(mask))
// TODO: find a more efficient way to do
// the automorphism on Z[X+X^-1]
for i := uint64(0); i < 2*N; i++ {
indexRaw = i * gen
index = indexRaw & mask
tmp = (indexRaw >> logN) & 1
// Only consider i -> index if within [0, N-1]
if index < N {
idx := i
// If the starting index is within [N, 2N-1]
if idx >= N {
idx = 2*N - idx // Wrap back between [0, N-1]
tmp ^= 1 // Negate
}
for j, s := range r.SubRings[:level+1] {
polOut.Coeffs[j][index] = polIn.Coeffs[j][idx]*(tmp^1) | (s.Modulus-polIn.Coeffs[j][idx])*tmp
}
}
}
} else {
mask = N - 1
logN = uint64(bits.Len64(mask))
for i := uint64(0); i < N; i++ {
indexRaw = i * gen
index = indexRaw & mask
tmp = (indexRaw >> logN) & 1
for j, s := range r.SubRings[:level+1] {
polOut.Coeffs[j][index] = polIn.Coeffs[j][i]*(tmp^1) | (s.Modulus-polIn.Coeffs[j][i])*tmp
}
}
}
}