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
|
// Copyright (c) 2012-2013 The Cryptonote developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <cstddef>
#include <cstring>
#include <fstream>
#include <string>
#include <vector>
#include "warnings.h"
#include "crypto/crypto.h"
#include "crypto/hash.h"
#include "crypto-tests.h"
#include "../io.h"
using namespace std;
using namespace crypto;
typedef crypto::hash chash;
bool operator !=(const ec_scalar &a, const ec_scalar &b) {
return 0 != memcmp(&a, &b, sizeof(ec_scalar));
}
bool operator !=(const ec_point &a, const ec_point &b) {
return 0 != memcmp(&a, &b, sizeof(ec_point));
}
bool operator !=(const secret_key &a, const secret_key &b) {
return 0 != memcmp(&a, &b, sizeof(secret_key));
}
bool operator !=(const key_derivation &a, const key_derivation &b) {
return 0 != memcmp(&a, &b, sizeof(key_derivation));
}
DISABLE_GCC_WARNING(maybe-uninitialized)
int main(int argc, char *argv[]) {
fstream input;
string cmd;
size_t test = 0;
bool error = false;
setup_random();
if (argc != 2) {
cerr << "invalid arguments" << endl;
return 1;
}
input.open(argv[1], ios_base::in);
for (;;) {
++test;
input.exceptions(ios_base::badbit);
if (!(input >> cmd)) {
break;
}
input.exceptions(ios_base::badbit | ios_base::failbit | ios_base::eofbit);
if (cmd == "check_scalar") {
ec_scalar scalar;
bool expected, actual;
get(input, scalar, expected);
actual = check_scalar(scalar);
if (expected != actual) {
goto error;
}
} else if (cmd == "random_scalar") {
ec_scalar expected, actual;
get(input, expected);
random_scalar(actual);
if (expected != actual) {
goto error;
}
} else if (cmd == "hash_to_scalar") {
vector<char> data;
ec_scalar expected, actual;
get(input, data, expected);
hash_to_scalar(data.data(), data.size(), actual);
if (expected != actual) {
goto error;
}
} else if (cmd == "generate_keys") {
public_key expected1, actual1;
secret_key expected2, actual2;
get(input, expected1, expected2);
generate_keys(actual1, actual2);
if (expected1 != actual1 || expected2 != actual2) {
goto error;
}
} else if (cmd == "check_key") {
public_key key;
bool expected, actual;
get(input, key, expected);
actual = check_key(key);
if (expected != actual) {
goto error;
}
} else if (cmd == "secret_key_to_public_key") {
secret_key sec;
bool expected1, actual1;
public_key expected2, actual2;
get(input, sec, expected1);
if (expected1) {
get(input, expected2);
}
actual1 = secret_key_to_public_key(sec, actual2);
if (expected1 != actual1 || (expected1 && expected2 != actual2)) {
goto error;
}
} else if (cmd == "generate_key_derivation") {
public_key key1;
secret_key key2;
bool expected1, actual1;
key_derivation expected2, actual2;
get(input, key1, key2, expected1);
if (expected1) {
get(input, expected2);
}
actual1 = generate_key_derivation(key1, key2, actual2);
if (expected1 != actual1 || (expected1 && expected2 != actual2)) {
goto error;
}
} else if (cmd == "derive_public_key") {
key_derivation derivation;
size_t output_index;
public_key base;
bool expected1, actual1;
public_key expected2, actual2;
get(input, derivation, output_index, base, expected1);
if (expected1) {
get(input, expected2);
}
actual1 = derive_public_key(derivation, output_index, base, actual2);
if (expected1 != actual1 || (expected1 && expected2 != actual2)) {
goto error;
}
} else if (cmd == "derive_secret_key") {
key_derivation derivation;
size_t output_index;
secret_key base;
secret_key expected, actual;
get(input, derivation, output_index, base, expected);
derive_secret_key(derivation, output_index, base, actual);
if (expected != actual) {
goto error;
}
} else if (cmd == "generate_signature") {
chash prefix_hash;
public_key pub;
secret_key sec;
signature expected, actual;
get(input, prefix_hash, pub, sec, expected);
generate_signature(prefix_hash, pub, sec, actual);
if (expected != actual) {
goto error;
}
} else if (cmd == "check_signature") {
chash prefix_hash;
public_key pub;
signature sig;
bool expected, actual;
get(input, prefix_hash, pub, sig, expected);
actual = check_signature(prefix_hash, pub, sig);
if (expected != actual) {
goto error;
}
} else if (cmd == "hash_to_point") {
chash h;
ec_point expected, actual;
get(input, h, expected);
hash_to_point(h, actual);
if (expected != actual) {
goto error;
}
} else if (cmd == "hash_to_ec") {
public_key key;
ec_point expected, actual;
get(input, key, expected);
hash_to_ec(key, actual);
if (expected != actual) {
goto error;
}
} else if (cmd == "generate_key_image") {
public_key pub;
secret_key sec;
key_image expected, actual;
get(input, pub, sec, expected);
generate_key_image(pub, sec, actual);
if (expected != actual) {
goto error;
}
} else if (cmd == "generate_ring_signature") {
chash prefix_hash;
key_image image;
vector<public_key> vpubs;
vector<const public_key *> pubs;
size_t pubs_count;
secret_key sec;
size_t sec_index;
vector<signature> expected, actual;
size_t i;
get(input, prefix_hash, image, pubs_count);
vpubs.resize(pubs_count);
pubs.resize(pubs_count);
for (i = 0; i < pubs_count; i++) {
get(input, vpubs[i]);
pubs[i] = &vpubs[i];
}
get(input, sec, sec_index);
expected.resize(pubs_count);
getvar(input, pubs_count * sizeof(signature), expected.data());
actual.resize(pubs_count);
generate_ring_signature(prefix_hash, image, pubs.data(), pubs_count, sec, sec_index, actual.data());
if (expected != actual) {
goto error;
}
} else if (cmd == "check_ring_signature") {
chash prefix_hash;
key_image image;
vector<public_key> vpubs;
vector<const public_key *> pubs;
size_t pubs_count;
vector<signature> sigs;
bool expected, actual;
size_t i;
get(input, prefix_hash, image, pubs_count);
vpubs.resize(pubs_count);
pubs.resize(pubs_count);
for (i = 0; i < pubs_count; i++) {
get(input, vpubs[i]);
pubs[i] = &vpubs[i];
}
sigs.resize(pubs_count);
getvar(input, pubs_count * sizeof(signature), sigs.data());
get(input, expected);
actual = check_ring_signature(prefix_hash, image, pubs.data(), pubs_count, sigs.data());
if (expected != actual) {
goto error;
}
} else {
throw ios_base::failure("Unknown function: " + cmd);
}
continue;
error:
cerr << "Wrong result on test " << test << endl;
error = true;
}
return error ? 1 : 0;
}
|