-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtesting.c
299 lines (230 loc) · 5.38 KB
/
testing.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// testing.c
// 2021-01-31 Markku-Juhani O. Saarinen <[email protected]>
// Copyright (c) 2021, PQShield Ltd. All rights reserved.
// === things for experiments
#include <math.h>
#include <fftw3.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bitpat.h"
#include "xcrand.h"
// debug hex string
void hexvec(const uint8_t *x, size_t xlen, const char *lab)
{
size_t i;
printf("%s[%zu] = ", lab, xlen);
for (i = 0; i < xlen; i++) {
printf("%02X", x[i]);
}
printf("\n");
}
void realvec(const double *v, size_t n, const char *lab)
{
size_t i;
for (i = 0; i < n; i++) {
printf("%s[%4zu] %17.14f\n", lab, i, v[i]);
}
}
void clxvec(const fftw_complex *v, size_t n, const char *lab)
{
size_t i;
for (i = 0; i < n; i++) {
printf("%s[%4zu] ( %17.14f, %17.14f )\n", lab, i, v[i][0], v[i][1]);
}
}
// conventional convolution
void vconv(double *z, const double *x, const double *y, size_t n)
{
size_t i, j;
for (i = 0; i < n; i++) {
z[i] = 0.0;
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
z[(i + j) % n] += x[i] * y[j];
}
}
}
// write a file
size_t wrfile(const char *path, const uint8_t *x, size_t xlen)
{
FILE *f;
size_t wlen;
f = fopen(path, "wb");
if (f == NULL) {
perror(path);
return 0;
}
wlen = fwrite(x, 1, xlen, f);
if (ferror(f)) perror(path);
fclose(f);
return wlen;
}
// write in binary
size_t wrbin(const char *path, const uint8_t *x, size_t xlen)
{
FILE *f;
size_t i, j;
uint8_t buf[8];
f = fopen(path, "wb");
if (f == NULL) {
perror(path);
return 0;
}
for (i = 0; i < xlen; i++) {
for (j = 0; j < 8; j++) {
buf[7 - j] = '0' + ((x[i] >> j) & 1);
}
if (fwrite(buf, 1, 8, f) != 8) break;
}
if (ferror(f)) perror(path);
fclose(f);
return 8 * i;
}
// used to generate the test files for SP 800-90B suites
#ifndef LEN_90B
#define LEN_90B 1000000
#endif
int gen_90b_dat()
{
double f, d, s;
xcrand_t xcr;
char fn[100];
uint8_t dat[LEN_90B];
xcrand_init(&xcr);
// randomize parameters
s = xcrand_d(&xcr);
f = xcrand_d(&xcr);
d = 0.5;
snprintf(fn, sizeof(fn), "s%08ld-f%08ld.dat", lrint(100000000.0 * s),
lrint(100000000.0 * f));
zbytes(dat, LEN_90B, f, d, s * s);
printf("%s\n", fn);
wrfile(fn, dat, LEN_90B);
// strncat(fn, ".bin", sizeof(fn));
// printf("%s\n", fn);
// wrbin(fn, dat, LEN);
return 0;
}
// === Estimate min-entropy -log2(max p_z) by depth-first search on z
// f fequency [0,1] (peak)
// d cutoff (0.5 = no bias)
// s2 jitter variance
// n Zn -- the bit sample size
// m FFT size (must be power of 2)
// dfs strategy
// v verbose (0 = print nothing, 1 = distribution to stdout)
double entropy_dfs(double f, double d, double s2, size_t n, size_t m,
int v, dfs_how_t how)
{
int bit;
size_t i, j;
double *vx, *vy;
double *g0, *g1;
fftw_complex *vt, *vu;
fftw_plan px, py, pz;
double h, q0, q1;
double x, r, t;
// distributions vt = fft(vx)
g0 = (double *)fftw_malloc(sizeof(double) * m);
g1 = (double *)fftw_malloc(sizeof(double) * m);
vx = (double *)fftw_malloc(sizeof(double) * m);
vt = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * m);
px = fftw_plan_dft_r2c_1d(m, vx, vt, FFTW_ESTIMATE);
// step function vu = fft(vy)
vy = (double *)fftw_malloc(sizeof(double) * m);
vu = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * m);
py = fftw_plan_dft_r2c_1d(m, vy, vu, FFTW_ESTIMATE);
// convolution result vx = fft^-1(vx)
pz = fftw_plan_dft_c2r_1d(m, vt, vx, FFTW_ESTIMATE);
// compute transformed step function
r = vec_fs(vy, m, f, s2);
fftw_execute(py);
// uniform start
for (i = 0; i < m; i++) {
vx[i] = 1.0;
}
// for deterministic step, xenter of the larger bias
x = d >= 0.5 ? d / 2.0 : d + 0.5 * (1.0 - d);
// iterate over bits
h = 0.0;
for (j = 0; j < n; j++) {
// convolution with the step function
if (j > 0) {
fftw_execute(px);
for (i = 0; i < m; i++) {
t = vt[i][0] * vu[i][0] - vt[i][1] * vu[i][1];
vt[i][1] = vt[i][0] * vu[i][1] + vt[i][1] * vu[i][0];
vt[i][0] = t;
}
fftw_execute(pz);
}
// select bit, normalize
q0 = vec_chop(g0, vx, m, d, 0);
q1 = vec_chop(g1, vx, m, d, 1);
// track a point
x += f;
x -= floor(x);
// strategy
switch (how) {
case DFS_BIG_MASS:
bit = q0 > q1 ? 0 : 1;
break;
default:
case DFS_FOLLOW_X:
bit = x < d ? 1 : 0;
break;
}
// bit selection
if (bit == 0) {
h -= log2(q0 / (q0 + q1));
r = 1.0 / q0;
for (i = 0; i < m; i++) {
vx[i] = r * g0[i];
}
} else {
h -= log2(q1 / (q0 + q1));
r = 1.0 / q1;
for (i = 0; i < m; i++) {
vx[i] = r * g1[i];
}
}
t = (q0 + q1);
if (v) {
printf("[%3zu] %d q0= %16.14f q1= %16.14f h= %16.8f\n",
j, bit, q0 / t, q1 / t, h / ((double)j + 1));
}
}
fftw_destroy_plan(px);
fftw_destroy_plan(py);
fftw_destroy_plan(pz);
fftw_free(g0);
fftw_free(g1);
fftw_free(vx);
fftw_free(vt);
fftw_free(vy);
fftw_free(vu);
return h / ((double) n);
}
// create data for scatterplot
void gen_estim()
{
double f, d, s;
xcrand_t xcr;
size_t i, n, m;
double h_mass, h_usex;
xcrand_init(&xcr);
n = 100;
m = 1 << 10;
for (i = 0; i < 10000; i++) {
f = xcrand_d(&xcr);
d = 0.5;
s = 0.5 * xcrand_d(&xcr);
h_mass = entropy_dfs(f, d, s*s, n, m, 0, DFS_BIG_MASS);
h_usex = entropy_dfs(f, d, s*s, n, m, 0, DFS_FOLLOW_X);
printf("s= %10.8f f= %10.7f h_mass= %10.8f h_usex= %10.8f %s\n",
s, f, h_mass, h_usex,
h_mass < h_usex ? "mass" : "usex");
}
}