-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathminimal_key_gen_mt_test.cpp
210 lines (187 loc) · 7.19 KB
/
minimal_key_gen_mt_test.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
#include <future>
#include <vector>
#include <google/protobuf/stubs/common.h>
#include "gtest/gtest.h"
#include "crypto-suites/crypto-bn/rand.h"
#include "crypto-suites/crypto-curve/curve.h"
#include "crypto-suites/exception/located_exception.h"
#include "multi-party-sig/multi-party-ecdsa/cmp/cmp.h"
#include "../thread_safe_queue.h"
#include "../message.h"
#include "../party_message_queue.h"
using std::string;
using std::vector;
using safeheron::bignum::BN;
using safeheron::curve::Curve;
using safeheron::curve::CurvePoint;
using safeheron::curve::CurveType;
using safeheron::multi_party_ecdsa::cmp::minimal_key_gen::Context;
using safeheron::multi_party_ecdsa::cmp::MinimalSignKey;
using safeheron::mpc_flow::mpc_parallel_v2::ErrorInfo;
void print_context_stack_if_failed(Context *ctx) {
std::string err_info;
vector<ErrorInfo> error_stack;
ctx->get_error_stack(error_stack);
for(const auto &err: error_stack){
err_info += "error code ( " + std::to_string(err.code_) + " ) : " + err.info_ + "\n";
}
printf("%s", err_info.c_str());
}
void print_sign_key_info(Context *ctx) {
EXPECT_TRUE(ctx->minimal_sign_key_.ValidityTest());
string json_str, base64;
EXPECT_TRUE(ctx->minimal_sign_key_.ToJsonString(json_str));
EXPECT_TRUE(ctx->minimal_sign_key_.ToBase64(base64));
std::string sign_key_info;
sign_key_info += ctx->minimal_sign_key_.local_party_.party_id_ + ": \n";
sign_key_info += "\"" + base64 + "\"\n";
sign_key_info += json_str;
printf("%s\n", sign_key_info.c_str());
}
std::map<std::string, PartyMessageQue<Msg>> map_id_message_queue;
#define ROUNDS 4
#define N_PARTIES 3
#define THRESHOLD 2
bool key_gen(CurveType curve_type, int n_parties, int threshold, std::string party_id, BN index, std::vector<std::string> remote_party_ids, std::vector<BN> remote_party_indexes, std::string ssid) {
bool ok = true;
std::string status;
//create context (define in cmp/minimal_key_gen/context.h)
const Curve * curv = safeheron::curve::GetCurveParam(curve_type);
BN sk = safeheron::rand::RandomBNLt(curv->n);
Context ctx(n_parties);
ok = Context::CreateContext(ctx, curve_type, threshold, n_parties, sk, index, party_id, remote_party_indexes, remote_party_ids, ssid);
if (!ok) return false;
status = "<== Context of " + party_id + " was created\n";
printf("%s", status.c_str());
//perform 3 rounds of MPC
for (int round = 0; round < ROUNDS; ++round) {
if (round == 0) {
ok = ctx.PushMessage();
if (!ok) {
print_context_stack_if_failed(&ctx);
return false;
}
} else {
for(int k = 0; k < n_parties - 1; k++) {
Msg m;
ThreadSafeQueue<Msg> &in_queue = map_id_message_queue.at(ctx.minimal_sign_key_.local_party_.party_id_).get(round - 1);
in_queue.Pop(m);
ok = ctx.PushMessage(m.p2p_msg_, m.bc_msg_, m.src_, round - 1);
if (!ok) {
print_context_stack_if_failed(&ctx);
return false;
}
}
}
ok = ctx.IsCurRoundFinished();
if (!ok) {
print_context_stack_if_failed(&ctx);
return false;
}
status = "<== Round " + std::to_string(round) + ", " + party_id + " \n";
printf("%s", status.c_str());
std::string out_bc_message;
vector<string> out_p2p_message_arr;
vector<string> out_des_arr;
ok = ctx.PopMessages(out_p2p_message_arr, out_bc_message, out_des_arr);
if (!ok) {
print_context_stack_if_failed(&ctx);
return false;
}
for (size_t j = 0; j < out_des_arr.size(); ++j) {
Msg m = {ctx.minimal_sign_key_.local_party_.party_id_, out_bc_message, out_p2p_message_arr.empty() ? "": out_p2p_message_arr[j]};
ThreadSafeQueue<Msg> &out_queue = map_id_message_queue.at(out_des_arr[j]).get(round);
out_queue.Push(m);
}
}
ok = ctx.IsFinished();
if (!ok) {
print_context_stack_if_failed(&ctx);
return false;
}
print_sign_key_info(&ctx);
return true;
}
TEST(cmp, minimal_key_gen_mt) {
std::future<bool> res[N_PARTIES];
//The common parameters for different curves
string ssid = "ssid";
std::string party_ids[N_PARTIES] = {
"co_signer1",
"co_signer2",
"co_signer3"
};
BN indexes[N_PARTIES] = {
BN(1),
BN(2),
BN(3)
};
//SECP256K1 sample
printf("Test cmp key generation with secp256k1 curve\n");
//Initialize the message queue
for (int i = 0; i < N_PARTIES; ++i) {
map_id_message_queue[party_ids[i]] = PartyMessageQue<Msg>(ROUNDS);
}
for (int i = 0; i < N_PARTIES; ++i) {
std::vector<std::string> remote_party_ids;
std::vector<BN> remote_party_indexes;
for (int j = 0; j < N_PARTIES; ++j) {
if (j != i) {
remote_party_ids.push_back(party_ids[j]);
remote_party_indexes.push_back(indexes[j]);
}
}
res[i] = std::async(std::launch::async, key_gen, CurveType::SECP256K1, N_PARTIES, THRESHOLD, party_ids[i], indexes[i], remote_party_ids, remote_party_indexes, ssid);
}
for (int i = 0; i < N_PARTIES; ++i) {
EXPECT_TRUE(res[i].get());
}
//P256 sample
printf("Test cmp key generation with p256 curve\n");
//Initialize the message queue
for (int i = 0; i < N_PARTIES; ++i) {
map_id_message_queue[party_ids[i]] = PartyMessageQue<Msg>(ROUNDS);
}
for (int i = 0; i < N_PARTIES; ++i) {
std::vector<std::string> remote_party_ids;
std::vector<BN> remote_party_indexes;
for (int j = 0; j < N_PARTIES; ++j) {
if (j != i) {
remote_party_ids.push_back(party_ids[j]);
remote_party_indexes.push_back(indexes[j]);
}
}
res[i] = std::async(std::launch::async, key_gen, CurveType::P256, N_PARTIES, THRESHOLD, party_ids[i], indexes[i], remote_party_ids, remote_party_indexes, ssid);
}
for (int i = 0; i < N_PARTIES; ++i) {
EXPECT_TRUE(res[i].get());
}
#ifdef TEST_STARK_CURVE
//STARK sample
printf("Test cmp key generation with stark curve\n");
//Initialize the message queue
for (int i = 0; i < N_PARTIES; ++i) {
map_id_message_queue[party_ids[i]] = PartyMessageQue<Msg>(ROUNDS);
}
for (int i = 0; i < N_PARTIES; ++i) {
std::vector<std::string> remote_party_ids;
std::vector<BN> remote_party_indexes;
for (int j = 0; j < N_PARTIES; ++j) {
if (j != i) {
remote_party_ids.push_back(party_ids[j]);
remote_party_indexes.push_back(indexes[j]);
}
}
res[i] = std::async(std::launch::async, key_gen, CurveType::STARK, N_PARTIES, THRESHOLD, party_ids[i], indexes[i], remote_party_ids, remote_party_indexes, ssid);
}
for (int i = 0; i < N_PARTIES; ++i) {
EXPECT_TRUE(res[i].get());
}
#endif
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
int ret = RUN_ALL_TESTS();
google::protobuf::ShutdownProtobufLibrary();
return ret;
}