-
Notifications
You must be signed in to change notification settings - Fork 1
/
polyvec.c
234 lines (191 loc) · 4.86 KB
/
polyvec.c
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Implementors: EagleSign Team
* This implementation is highly inspired from Dilithium and
* Falcon Signatures' implementations
*/
#include <stdint.h>
#include <stdio.h>
#include "params.h"
#include "polyvec.h"
#include "poly.h"
#include "fips202.h"
/**************************************************************/
/************ Vectors of polynomials of length L **************/
/**************************************************************/
void polyvecl_uniform_eta_g(polyvecl *v, const uint8_t seed[CRHBYTES], uint16_t *nonce)
{
unsigned int i;
for (i = 0; i < L; ++i)
poly_uniform_eta_g(&v->vec[i], seed, (*nonce)++);
}
void polyvecl_uniform_eta_d(polyvecl *v, const uint8_t seed[CRHBYTES], uint16_t *nonce)
{
unsigned int i;
for (i = 0; i < L; ++i)
poly_uniform_eta_d(&v->vec[i], seed, (*nonce)++);
}
void polyvecl_challenge_y1_c(polyvecl *v, const uint8_t seed[SEEDBYTES], uint16_t *nonce, int param)
{
unsigned int i;
for (i = 0; i < L; ++i)
poly_challenge_y1_c(&v->vec[i], seed, (*nonce)++, param);
}
void polyvecl_add(polyvecl *w, const polyvecl *u, const polyvecl *v)
{
unsigned int i;
for (i = 0; i < L; ++i)
poly_add(&w->vec[i], &u->vec[i], &v->vec[i]);
}
void polyvecl_ntt(polyvecl *v)
{
unsigned int i;
for (i = 0; i < L; ++i)
poly_ntt(&v->vec[i]);
}
void polyvecl_invntt_tomont(polyvecl *v)
{
unsigned int i;
for (i = 0; i < L; ++i)
poly_invntt_tomont(&v->vec[i]);
}
void polyvecl_pointwise_poly_montgomery(polyvecl *r, const poly *a, const polyvecl *v)
{
unsigned int i;
for (i = 0; i < L; ++i)
poly_pointwise_montgomery(&r->vec[i], a, &v->vec[i]);
}
void polyvecl_pointwise_acc_montgomery(poly *w,
const polyvecl *u,
const polyvecl *v)
{
unsigned int i;
poly t;
poly_pointwise_montgomery(w, &u->vec[0], &v->vec[0]);
for (i = 1; i < L; ++i)
{
poly_pointwise_montgomery(&t, &u->vec[i], &v->vec[i]);
poly_add(w, w, &t);
}
}
/**************************************************************/
/************ Vectors of polynomials of length K **************/
/**************************************************************/
void polyveck_uniform_eta_y2(polyveck *v, const uint8_t seed[CRHBYTES], uint16_t *nonce)
{
unsigned int i;
for (i = 0; i < K; ++i)
poly_uniform_eta_y2(&v->vec[i], seed, (*nonce)++);
}
void polyveck_add(polyveck *w, const polyveck *u, const polyveck *v)
{
unsigned int i;
for (i = 0; i < K; ++i)
poly_add(&w->vec[i], &u->vec[i], &v->vec[i]);
}
void polyveck_sub(polyveck *w, const polyveck *u, const polyveck *v)
{
unsigned int i;
for (i = 0; i < K; ++i)
poly_sub(&w->vec[i], &u->vec[i], &v->vec[i]);
}
void polyveck_ntt(polyveck *v)
{
unsigned int i;
for (i = 0; i < K; ++i)
poly_ntt(&v->vec[i]);
}
void polyveck_invntt_tomont(polyveck *v)
{
unsigned int i;
for (i = 0; i < K; ++i)
poly_invntt_tomont(&v->vec[i]);
}
void polyveck_pointwise_poly_montgomery(polyveck *r, const poly *a, const polyveck *v)
{
unsigned int i;
for (i = 0; i < K; ++i)
poly_pointwise_montgomery(&r->vec[i], a, &v->vec[i]);
}
void polyveck_pack_P(uint8_t r[K * NBYTES * LOGQ], polyveck *P)
{
unsigned int i;
for (i = 0; i < K; ++i)
polyQ_pack(r + i * NBYTES * LOGQ, &P->vec[i]);
}
void polyveck_unpack_P(polyveck *P, uint8_t r[K * NBYTES * LOGQ])
{
unsigned int i;
for (i = 0; i < K; ++i)
polyQ_unpack(&P->vec[i], r + i * NBYTES * LOGQ);
}
int polyvec_chknorms(const polyvecl *Z, const polyveck *W)
{
unsigned int i, j, min = L, max = K;
Q_SIZE tmp;
if (K < L)
{
min = K;
max = L;
}
for (i = 0; i < min; ++i)
{
for (j = 0; j < N; ++j)
{
tmp = (Q_SIZE)Z->vec[i].coeffs[j];
tmp -= (2 * tmp) & -(tmp >> 15); // |x| = x if x >= 0 and -x if x < 0
if ((S_Q_SIZE)tmp > DELTA)
{
return -1;
}
tmp = (Q_SIZE)W->vec[i].coeffs[j];
tmp -= (2 * tmp) & -(tmp >> 15);
if ((S_Q_SIZE)tmp > DELTA_PRIME)
return -1;
}
}
if (max == L)
{
for (i = min; i < max; ++i)
{
for (j = 0; j < N; ++j)
{
tmp = (Q_SIZE)Z->vec[i].coeffs[j];
tmp -= (2 * tmp) & -(tmp >> 15);
if ((S_Q_SIZE)tmp > DELTA)
return -1;
}
}
}
else
{
for (i = min; i < max; ++i)
{
for (j = 0; j < N; ++j)
{
tmp = (Q_SIZE)W->vec[i].coeffs[j];
tmp -= (2 * tmp) & -(tmp >> 15);
if ((S_Q_SIZE)tmp > DELTA_PRIME)
return -1;
}
}
}
return 0;
}
int polyvecl_chknorms(const polyvecl *Z, const unsigned int borm)
{
unsigned int i, j;
Q_SIZE tmp;
for (i = 0; i < L; ++i)
{
for (j = 0; j < N; ++j)
{
tmp = (Q_SIZE)Z->vec[i].coeffs[j];
tmp -= (2 * tmp) & -(tmp >> 15); // |x| = x if x >= 0 and -x if x < 0
if ((S_Q_SIZE)tmp > borm)
{
return -1;
}
}
}
return 0;
}