-
Notifications
You must be signed in to change notification settings - Fork 7
/
polyvec.go
197 lines (167 loc) · 3.81 KB
/
polyvec.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package dilithium
import (
"fmt"
)
type polyVecK struct {
vec [K]poly
}
type polyVecL struct {
vec [L]poly
}
func polyVecLUniformGamma1(v *polyVecL, seed [CRHBytes]uint8, nonce uint16) {
for i := uint16(0); i < L; i++ {
polyUniformGamma1(&v.vec[i], seed, L*nonce+i)
}
}
func polyVecLReduce(v *polyVecL) {
for i := 0; i < L; i++ {
polyReduce(&v.vec[i])
}
}
func polyVecLAdd(w, u, v *polyVecL) {
for i := 0; i < L; i++ {
polyAdd(&w.vec[i], &u.vec[i], &v.vec[i])
}
}
func polyVecLNTT(v *polyVecL) {
for i := 0; i < L; i++ {
polyNTT(&v.vec[i])
}
}
func polyVecLInvNTTToMont(v *polyVecL) {
for i := 0; i < L; i++ {
polyInvNTTToMont(&v.vec[i])
}
}
func polyVecLPointWisePolyMontgomery(r *polyVecL, a *poly, v *polyVecL) {
for i := 0; i < L; i++ {
polyPointWiseMontgomery(&r.vec[i], a, &v.vec[i])
}
}
func polyVecMatrixExpand(mat *[K]polyVecL, rho *[SeedBytes]uint8) error {
for i := 0; i < K; i++ {
for j := 0; j < L; j++ {
if err := polyUniform(&mat[i].vec[j], rho, (uint16(i)<<8)+uint16(j)); err != nil {
return err
}
}
}
return nil
}
func polyVecLChkNorm(v *polyVecL, bound int32) (ret int) {
for i := 0; i < L; i++ {
if polyChkNorm(&v.vec[i], bound) != 0 {
return 1
}
}
return 0
}
func polyVecKAdd(w, u, v *polyVecK) {
for i := 0; i < K; i++ {
polyAdd(&w.vec[i], &u.vec[i], &v.vec[i])
}
}
func polyVecKSub(w, u, v *polyVecK) {
for i := 0; i < K; i++ {
polySub(&w.vec[i], &u.vec[i], &v.vec[i])
}
}
func polyVecKShiftL(v *polyVecK) {
for i := 0; i < K; i++ {
polyShiftL(&v.vec[i])
}
}
func polyVecKNTT(v *polyVecK) {
for i := 0; i < K; i++ {
polyNTT(&v.vec[i])
}
}
func polyVecKInvNTTToMont(v *polyVecK) {
for i := 0; i < K; i++ {
polyInvNTTToMont(&v.vec[i])
}
}
func polyVecKPointWisePolyMontgomery(r *polyVecK, a *poly, v *polyVecK) {
for i := 0; i < K; i++ {
polyPointWiseMontgomery(&r.vec[i], a, &v.vec[i])
}
}
func polyVecKChkNorm(v *polyVecK, bound int32) (ret int) {
for i := 0; i < K; i++ {
if polyChkNorm(&v.vec[i], bound) != 0 {
return 1
}
}
return 0
}
func polyVecKPower2Round(v1, v0, v *polyVecK) {
for i := 0; i < K; i++ {
polyPower2Round(&v1.vec[i], &v0.vec[i], &v.vec[i])
}
}
func polyVecKDecompose(v1, v0, v *polyVecK) {
for i := 0; i < K; i++ {
polyDecompose(&v1.vec[i], &v0.vec[i], &v.vec[i])
}
}
func polyVecKMakeHint(h, v0, v1 *polyVecK) (s uint) {
for i := 0; i < K; i++ {
s += polyMakeHint(&h.vec[i], &v0.vec[i], &v1.vec[i])
}
return s
}
func polyVecKUseHint(w, u, h *polyVecK) {
for i := 0; i < K; i++ {
polyUseHint(&w.vec[i], &u.vec[i], &h.vec[i])
}
}
func polyVecLPointWiseAccMontgomery(w *poly, u, v *polyVecL) {
var t poly
polyPointWiseMontgomery(w, &u.vec[0], &v.vec[0])
for i := 1; i < L; i++ {
polyPointWiseMontgomery(&t, &u.vec[i], &v.vec[i])
polyAdd(w, w, &t)
}
}
func polyVecMatrixPointWiseMontgomery(t *polyVecK, mat *[K]polyVecL, v *polyVecL) {
for i := 0; i < K; i++ {
polyVecLPointWiseAccMontgomery(&t.vec[i], &mat[i], v)
}
}
func polyVecLUniformETA(v *polyVecL, seed *[CRHBytes]uint8, nonce uint16) error {
for i := 0; i < L; i++ {
if err := polyUniformEta(&v.vec[i], seed, nonce); err != nil {
return err
}
nonce++
}
return nil
}
func polyVecKUniformETA(v *polyVecK, seed *[CRHBytes]uint8, nonce uint16) error {
for i := 0; i < K; i++ {
if err := polyUniformEta(&v.vec[i], seed, nonce); err != nil {
return err
}
nonce++
}
return nil
}
func polyVecKReduce(v *polyVecK) {
for i := 0; i < K; i++ {
polyReduce(&v.vec[i])
}
}
func polyVecKCAddQ(v *polyVecK) {
for i := 0; i < K; i++ {
polyCAddQ(&v.vec[i])
}
}
func polyVecKPackW1(r []uint8, w1 *polyVecK) error {
if len(r) != K*PolyW1PackedBytes {
return fmt.Errorf("invalid length | length expected %d | found %d", K*PolyW1PackedBytes, len(r))
}
for i := 0; i < K; i++ {
polyW1Pack(r[i*PolyW1PackedBytes:], &w1.vec[i])
}
return nil
}