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
|
/* Copyright (c) 2016, Google Inc.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#include <string>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <openssl/crypto.h>
#include <openssl/rand.h>
#include "../test/scoped_types.h"
#include "internal.h"
namespace bssl {
static const unsigned kNumTests = 1000;
static bool TestNoise(void) {
printf("noise distribution:\n");
size_t buckets[1 + 2 * PARAM_K];
memset(buckets, 0, sizeof(buckets));
for (size_t i = 0; i < kNumTests; i++) {
NEWHOPE_POLY s;
NEWHOPE_POLY_noise(&s);
for (int j = 0; j < PARAM_N; j++) {
uint16_t value = (s.coeffs[j] + PARAM_K) % PARAM_Q;
buckets[value]++;
}
}
int64_t sum = 0, square_sum = 0;
for (int64_t i = 0; i < 1 + 2 * PARAM_K; i++) {
sum += (i - PARAM_K) * (int64_t) buckets[i];
square_sum += (i - PARAM_K) * (i - PARAM_K) * (int64_t) buckets[i];
}
double mean = double(sum) / double(PARAM_N * kNumTests);
double expected_variance = 0.5 * 0.5 * double(PARAM_K * 2);
double variance = double(square_sum) / double(PARAM_N * kNumTests) - mean * mean;
for (size_t i = 0; i < 1 + 2 * PARAM_K; i++) {
std::string dots;
for (size_t j = 0; j < 79 * buckets[i] / buckets[PARAM_K]; j++) {
dots += "+";
}
printf("%+zd\t%zd\t%s\n", i - PARAM_K, buckets[i], dots.c_str());
}
printf("mean: got %f, want %f\n", mean, 0.0);
printf("variance: got %f, want %f\n", variance, expected_variance);
printf("\n");
if (mean < -0.5 || 0.5 < mean) {
fprintf(stderr, "mean out of range: %f\n", mean);
return false;
}
if (variance < expected_variance - 1.0 || expected_variance + 1.0 < variance) {
fprintf(stderr, "variance out of range: got %f, want %f\n", variance,
expected_variance);
return false;
}
return true;
}
static int Hamming32(const uint8_t key[NEWHOPE_KEY_LENGTH]) {
static int kHamming[256] = {
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
};
int r = 0;
for(int i = 0; i < NEWHOPE_KEY_LENGTH; i++) {
r += kHamming[key[i]];
}
return r;
}
static bool TestKeys(void) {
printf("keys (prior to whitening):\n");
uint8_t key[NEWHOPE_KEY_LENGTH];
uint8_t offermsg[NEWHOPE_OFFERMSG_LENGTH];
ScopedNEWHOPE_POLY sk(NEWHOPE_POLY_new()), pk(NEWHOPE_POLY_new()),
sp(NEWHOPE_POLY_new()), ep(NEWHOPE_POLY_new()), epp(NEWHOPE_POLY_new()),
a(NEWHOPE_POLY_new()), bp(NEWHOPE_POLY_new()), rec(NEWHOPE_POLY_new());
int ones = 0;
for (size_t i = 0; i < kNumTests; i++) {
NEWHOPE_offer(offermsg, sk.get());
NEWHOPE_offer_frommsg(pk.get(), a.get(), offermsg);
NEWHOPE_POLY_noise_ntt(sp.get());
NEWHOPE_POLY_noise_ntt(ep.get());
NEWHOPE_POLY_noise(epp.get()); /* intentionally not NTT */
uint8_t rand[32];
RAND_bytes(rand, 32);
NEWHOPE_accept_computation(key, bp.get(), rec.get(),
sp.get(), ep.get(), epp.get(), rand,
pk.get(), a.get());
ones += Hamming32(key);
}
int bits = NEWHOPE_KEY_LENGTH * 8 * kNumTests;
int diff = bits - 2 * ones;
double fraction = (double) abs(diff) / bits;
printf("ones: %d\n", ones);
printf("zeroes: %d\n", (bits - ones));
printf("diff: got %d (%f), want 0\n", diff, fraction);
printf("\n");
if (fraction > 0.01) {
fprintf(stderr, "key bias is too high (%f)\n", fraction);
return false;
}
return true;
}
} // namespace bssl
int main(void) {
if (!bssl::TestKeys() ||
!bssl::TestNoise()) {
return 1;
}
printf("PASS\n");
return 0;
}
|