-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_ntt.c
102 lines (88 loc) · 1.93 KB
/
test_ntt.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
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include "randombytes.h"
#include "params.h"
#include "poly.h"
#include "cpucycles.h"
static int32_t pow_simple(int32_t a, unsigned int e) {
int32_t r;
if(e == 0) return 1;
else if(e == 1) return a;
r = pow_simple(a,e/2);
r = (int64_t)r*r % Q;
if(e&1) r = (int64_t)r*a % Q;
return r;
}
static int idx(int i) {
int r;
r = i/32;
i %= 32;
r *= 32;
r += 8*(i%4);
r += i/4;
return r;
}
static int32_t zeta[N];
static int32_t zetapow[N][N];
int main(void) {
int i,j;
uint64_t t[20], overhead;
int64_t out[N];
uint8_t seed[SYMBYTES];
poly a, b;
overhead = cpucycles_overhead();
randombytes(seed,SYMBYTES);
for(i=0;i<N;i++)
a.coeffs[i] = 0;
a.coeffs[1] = 1;
poly_ntt(&a);
for(i=0;i<N;i++) {
zeta[i] = a.coeffs[idx(i)] % Q;
assert((pow_simple(zeta[i],N) + 1) % Q == 0);
for(j=0;j<i;j++)
assert((zeta[j] - zeta[i]) % Q);
}
for(i=0;i<N;i++)
for(j=0;j<N;j++)
zetapow[i][j] = pow_simple(zeta[i],j);
for(i=0;i<N;i++) {
for(j=0;j<N;j++)
a.coeffs[j] = 0;
a.coeffs[i] = 1;
poly_ntt(&a);
for(j=0;j<N;j++) {
a.coeffs[idx(j)] %= Q;
assert((a.coeffs[idx(j)] - zetapow[j][i]) % Q == 0);
}
}
poly_uniform(&a,seed,0);
for(i=0;i<N;i++) {
out[i] = 0;
for(j=0;j<N;j++)
out[i] += (int64_t)a.coeffs[j]*zetapow[i][j] % Q;
out[i] %= Q;
}
poly_ntt(&a);
for(i=0;i<N;i++)
assert((a.coeffs[idx(i)] - out[i]) % Q == 0);
poly_uniform(&a,seed,1);
b = a;
poly_ntt(&a);
poly_invntt(&a);
for(i=0;i<N;++i)
assert((a.coeffs[i] - b.coeffs[i]) % Q == 0);
for(i=0;i<20;i++) {
t[i] = cpucycles();
poly_ntt(&a);
}
for(i=0;i<19;i++)
printf("ntt: %2d: %lu\n", i+1, t[i+1] - t[i] - overhead);
for(i=0;i<20;i++) {
t[i] = cpucycles();
poly_invntt(&a);
}
for(i=0;i<19;i++)
printf("invntt: %2d: %lu\n", i, t[i+1] - t[i] - overhead);
return 0;
}