-
Notifications
You must be signed in to change notification settings - Fork 168
/
mpc_crypto_bench.cpp
280 lines (237 loc) · 7.64 KB
/
mpc_crypto_bench.cpp
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
/*
* NOTICE
*
* The blockchain-crypto-mpc software is licensed under a proprietary license or the GPL v.3.
* If you choose to receive it under the GPL v.3 license, the following applies:
* Blockchain-crypto-mpc is a Multiparty Computation (MPC)-based cryptographic library for securing blockchain wallets and applications.
*
* Copyright (C) 2018, Unbound Tech Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "precompiled.h"
#include "mpc_crypto.h"
void help()
{
printf("USAGE: mpc_crypto_bench [ecdsa-gen|ecdsa-sign|eddsa-gen|eddsa-sign|bip-initial|bip-hardened|bip-normal] <iterations>\n");
exit(-1);
}
void halt(int rv)
{
printf("\nError %08x occured\n", rv);
exit(rv);
}
class bench_t
{
public:
bench_t();
virtual ~bench_t();
virtual void init() {}
virtual void run() = 0;
protected:
MPCCryptoShare* client_share;
MPCCryptoShare* server_share;
MPCCryptoContext* client_ctx;
MPCCryptoContext* server_ctx;
void run_protocol();
void free_shares();
void free_ctx();
};
bench_t::bench_t() : client_share(nullptr), server_share(nullptr), client_ctx(nullptr), server_ctx(nullptr)
{
}
bench_t::~bench_t()
{
free_ctx();
free_shares();
}
void bench_t::free_shares()
{
MPCCrypto_freeShare(client_share); client_share = nullptr;
MPCCrypto_freeShare(server_share); server_share = nullptr;
}
void bench_t::free_ctx()
{
MPCCrypto_freeContext(client_ctx); client_ctx = nullptr;
MPCCrypto_freeContext(server_ctx); server_ctx = nullptr;
}
void bench_t::run_protocol()
{
bool client_finished = false;
bool server_finished = false;
MPCCryptoMessage* in = nullptr;
MPCCryptoMessage* out = nullptr;
while (!client_finished || !server_finished)
{
int rv = 0;
if (!client_finished)
{
unsigned flags = 0;
if (rv = MPCCrypto_step(client_ctx, in, &out, &flags)) return halt(rv);
if (in) MPCCrypto_freeMessage(in);
in = out;
client_finished = (flags & mpc_protocol_finished) != 0;
}
if (!out) break;
if (!server_finished)
{
unsigned flags = 0;
if (rv = MPCCrypto_step(server_ctx, in, &out, &flags)) return halt(rv);
if (in) MPCCrypto_freeMessage(in);
in = out;
server_finished = (flags & mpc_protocol_finished) != 0;
}
}
}
class ecdsa_gen_t : public bench_t
{
public:
void run()
{
int rv = 0;
if (rv = MPCCrypto_initGenerateEcdsaKey(1, &client_ctx)) halt(rv);
if (rv = MPCCrypto_initGenerateEcdsaKey(2, &server_ctx)) halt(rv);
run_protocol();
free_ctx();
}
};
class ecdsa_sign_t : public bench_t
{
public:
void init()
{
int rv = 0;
if (rv = MPCCrypto_initGenerateEcdsaKey(1, &client_ctx)) halt(rv);
if (rv = MPCCrypto_initGenerateEcdsaKey(2, &server_ctx)) halt(rv);
run_protocol();
if (rv = MPCCrypto_getShare(client_ctx, &client_share)) halt(rv);
if (rv = MPCCrypto_getShare(server_ctx, &server_share)) halt(rv);
}
void run()
{
char test[] = "123456";
int rv = 0;
if (rv = MPCCrypto_initEcdsaSign(1, client_share, (const uint8_t*)test, sizeof(test), 0, &client_ctx)) halt(rv);
if (rv = MPCCrypto_initEcdsaSign(2, server_share, (const uint8_t*)test, sizeof(test), 0, &server_ctx)) halt(rv);
run_protocol();
int sig_size = 0;
if (rv = MPCCrypto_getResultEcdsaSign(client_ctx, nullptr, &sig_size)) halt(rv);
std::vector<uint8_t> sig(sig_size);
if (rv = MPCCrypto_getResultEcdsaSign(client_ctx, sig.data(), &sig_size)) halt(rv);
}
};
class eddsa_sign_t : public bench_t
{
public:
void init()
{
int rv = 0;
if (rv = MPCCrypto_initGenerateEddsaKey(1, &client_ctx)) halt(rv);
if (rv = MPCCrypto_initGenerateEddsaKey(2, &server_ctx)) halt(rv);
run_protocol();
if (rv = MPCCrypto_getShare(client_ctx, &client_share)) halt(rv);
if (rv = MPCCrypto_getShare(server_ctx, &server_share)) halt(rv);
}
void run()
{
char test[] = "123456";
int rv = 0;
if (rv = MPCCrypto_initEddsaSign(1, client_share, (const uint8_t*)test, sizeof(test), 0, &client_ctx)) halt(rv);
if (rv = MPCCrypto_initEddsaSign(2, server_share, (const uint8_t*)test, sizeof(test), 0, &server_ctx)) halt(rv);
run_protocol();
uint8_t sig[64];
if (rv = MPCCrypto_getResultEddsaSign(client_ctx, sig)) halt(rv);
}
};
class eddsa_gen_t : public bench_t
{
public:
void run()
{
int rv = 0;
if (rv = MPCCrypto_initGenerateEddsaKey(1, &client_ctx)) halt(rv);
if (rv = MPCCrypto_initGenerateEddsaKey(2, &server_ctx)) halt(rv);
run_protocol();
free_ctx();
}
};
class bip_t : public bench_t
{
public:
bip_t(int _mode) : mode(_mode) {}
void init()
{
uint8_t seed[16] = {1,2,3,4,5};
int rv = 0;
if (rv = MPCCrypto_initImportGenericSecret(1, seed, sizeof(seed), &client_ctx)) halt(rv);
if (rv = MPCCrypto_initImportGenericSecret(2, seed, sizeof(seed), &server_ctx)) halt(rv);
run_protocol();
if (rv = MPCCrypto_getShare(client_ctx, &client_share)) halt(rv);
if (rv = MPCCrypto_getShare(server_ctx, &server_share)) halt(rv);
free_ctx();
if (mode<0) mode = 0;
else
{
if (rv = MPCCrypto_initDeriveBIP32(1, client_share, 0, 0, &client_ctx)) halt(rv);
if (rv = MPCCrypto_initDeriveBIP32(2, server_share, 0, 0, &server_ctx)) halt(rv);
run_protocol();
free_shares();
if (rv = MPCCrypto_getResultDeriveBIP32(client_ctx, &client_share)) halt(rv);
if (rv = MPCCrypto_getResultDeriveBIP32(server_ctx, &server_share)) halt(rv);
free_ctx();
}
}
void run()
{
int rv = 0;
if (rv = MPCCrypto_initDeriveBIP32(1, client_share, mode, 0, &client_ctx)) halt(rv);
if (rv = MPCCrypto_initDeriveBIP32(2, server_share, mode, 0, &server_ctx)) halt(rv);
run_protocol();
free_ctx();
}
private:
int mode;
};
int main(int argc, char* argv[])
{
if (argc<3) help();
int count = atoi(argv[2]);
if (count<=0) count = 1;
const char* name = argv[1];
bench_t* bench = nullptr;
if (false);
else if (0==strcmp(name, "ecdsa-gen")) bench = new ecdsa_gen_t();
else if (0==strcmp(name, "ecdsa-sign")) bench = new ecdsa_sign_t();
else if (0==strcmp(name, "eddsa-gen")) bench = new eddsa_gen_t();
else if (0==strcmp(name, "eddsa-sign")) bench = new eddsa_sign_t();
else if (0==strcmp(name, "bip-initial")) bench = new bip_t(-1);
else if (0==strcmp(name, "bip-hardened")) bench = new bip_t(1);
else if (0==strcmp(name, "bip-normal")) bench = new bip_t(0);
else help();
printf("Initialization... "); fflush(stdout);
bench->init();
printf("\n");
printf("Running %s for %d iterations\n", name, count);
std::chrono::high_resolution_clock::time_point begin = std::chrono::high_resolution_clock::now();
for (int i=0; i<count; i++)
{
printf("."); fflush(stdout);
bench->run();
}
std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now();
double duration = (double)std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
printf("\n%f seconds per operation\n", duration / (count * 1000));
delete bench;
return 0;
}