forked from ZenGo-X/fs-dkr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.rs
394 lines (349 loc) · 14.8 KB
/
test.rs
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#[cfg(test)]
mod tests {
use crate::refresh_message::RefreshMessage;
use curv::arithmetic::Converter;
use curv::cryptographic_primitives::secret_sharing::feldman_vss::{
ShamirSecretSharing, VerifiableSS,
};
use curv::elliptic::curves::secp256_k1::Secp256k1Point;
use curv::elliptic::curves::Secp256k1;
use curv::BigInt;
use multi_party_ecdsa::protocols::multi_party_ecdsa::gg_2020::party_i::verify;
use multi_party_ecdsa::protocols::multi_party_ecdsa::gg_2020::party_i::Keys;
use multi_party_ecdsa::protocols::multi_party_ecdsa::gg_2020::state_machine::keygen::{
Keygen, LocalKey,
};
use multi_party_ecdsa::protocols::multi_party_ecdsa::gg_2020::state_machine::sign::{
CompletedOfflineStage, OfflineStage, SignManual,
};
use sha2::Sha256;
use crate::add_party_message::JoinMessage;
use crate::error::FsDkrResult;
use curv::{
elliptic::curves::Scalar,
cryptographic_primitives::{
hashing::Digest, proofs::sigma_dlog::DLogProof}
};
use paillier::DecryptionKey;
use round_based::dev::Simulation;
use std::collections::HashMap;
type GE = Secp256k1Point;
#[test]
fn test1() {
//simulate keygen
let t = 3;
let n = 6;
let mut keys = simulate_keygen(t, n);
let old_keys = keys.clone();
simulate_dkr::<{ crate::M_SECURITY }>(&mut keys);
// check that sum of old keys is equal to sum of new keys
let old_linear_secret_key: Vec<_> = (0..old_keys.len())
.map(|i| old_keys[i].keys_linear.x_i.clone())
.collect();
let new_linear_secret_key: Vec<_> = (0..keys.len())
.map(|i| keys[i].keys_linear.x_i.clone())
.collect();
let indices: Vec<_> = (0..(t + 1) as u16).collect();
let vss = VerifiableSS::<Secp256k1, sha2::Sha256> {
parameters: ShamirSecretSharing {
threshold: t,
share_count: n,
},
commitments: Vec::new(),
proof: DLogProof::<Secp256k1, sha2::Sha256>::prove(
&Scalar::random()),
};
assert_eq!(
vss.reconstruct(&indices[..], &old_linear_secret_key[0..(t + 1) as usize]),
vss.reconstruct(&indices[..], &new_linear_secret_key[0..(t + 1) as usize])
);
assert_ne!(old_linear_secret_key, new_linear_secret_key);
}
#[test]
fn test_sign_rotate_sign() {
let mut keys = simulate_keygen(2, 5);
let offline_sign = simulate_offline_stage(keys.clone(), &[1, 2, 3]);
simulate_signing(offline_sign, b"ZenGo");
simulate_dkr::<{ crate::M_SECURITY }>(&mut keys);
let offline_sign = simulate_offline_stage(keys.clone(), &[2, 3, 4]);
simulate_signing(offline_sign, b"ZenGo");
simulate_dkr::<{ crate::M_SECURITY }>(&mut keys);
let offline_sign = simulate_offline_stage(keys, &[1, 3, 5]);
simulate_signing(offline_sign, b"ZenGo");
}
#[test]
fn test_remove_sign_rotate_sign() {
let mut keys = simulate_keygen(2, 5);
let offline_sign = simulate_offline_stage(keys.clone(), &[1, 2, 3]);
simulate_signing(offline_sign, b"ZenGo");
simulate_dkr_removal::<{ crate::M_SECURITY }>(&mut keys, [1].to_vec());
let offline_sign = simulate_offline_stage(keys.clone(), &[2, 3, 4]);
simulate_signing(offline_sign, b"ZenGo");
simulate_dkr_removal::<{ crate::M_SECURITY }>(&mut keys, [1, 2].to_vec());
let offline_sign = simulate_offline_stage(keys, &[3, 4, 5]);
simulate_signing(offline_sign, b"ZenGo");
}
#[test]
fn test_add_party_with_permute() {
fn simulate_replace<const M: usize>(
keys: &mut Vec<LocalKey<Secp256k1>>,
party_indices: &[u16],
old_to_new_map: &HashMap<u16, u16>,
t: u16,
n: u16,
) -> FsDkrResult<()> {
fn generate_join_messages_and_keys<const M: usize>(
number_of_new_parties: usize,
) -> (Vec<JoinMessage<Secp256k1, Sha256, M>>, Vec<Keys>) {
// the new party generates it's join message to start joining the computation
(0..number_of_new_parties)
.map(|_| JoinMessage::distribute())
.unzip()
}
fn generate_refresh_parties_replace<const M: usize>(
keys: &mut [LocalKey<Secp256k1>],
old_to_new_map: &HashMap<u16, u16>,
join_messages: &[JoinMessage<Secp256k1, Sha256, M>],
) -> (
Vec<RefreshMessage<Secp256k1, Sha256, M>>,
Vec<DecryptionKey>,
) {
let new_n = (&keys.len() + join_messages.len()) as u16;
keys.iter_mut()
.map(|key| {
RefreshMessage::replace(join_messages, key, old_to_new_map, new_n).unwrap()
})
.unzip()
}
// each party that wants to join generates a join message and a pair of paillier keys.
let (mut join_messages, new_keys) =
generate_join_messages_and_keys::<{ crate::M_SECURITY }>(party_indices.len());
// each new party has to be informed through offchannel communication what party index
// it has been assigned (the information is public).
for (join_message, party_index) in join_messages.iter_mut().zip(party_indices) {
join_message.party_index = Some(*party_index);
}
// each existing party has to generate it's refresh message aware of the new parties
let (refresh_messages, dk_keys) =
generate_refresh_parties_replace(keys, &old_to_new_map, join_messages.as_slice());
let mut new_keys_vec: Vec<(u16, LocalKey<Secp256k1>)> =
Vec::with_capacity(keys.len() + join_messages.len());
// all existing parties rotate aware of the join_messages
for i in 0..keys.len() as usize {
RefreshMessage::collect(
refresh_messages.as_slice(),
&mut keys[i],
dk_keys[i].clone(),
join_messages.as_slice(),
)
.expect("");
new_keys_vec.push((keys[i].i - 1, keys[i].clone()));
}
// all new parties generate a local key
for (join_message, dk) in join_messages.iter().zip(new_keys) {
let party_index = join_message.party_index.unwrap();
let local_key = join_message.collect(
refresh_messages.as_slice(),
dk,
join_messages.as_slice(),
t,
n,
)?;
new_keys_vec.push((party_index - 1, local_key));
}
new_keys_vec.sort_by(|a, b| a.0.cmp(&b.0));
let keys_replacements = new_keys_vec
.iter()
.map(|a| a.1.clone())
.collect::<Vec<LocalKey<Secp256k1>>>();
*keys = keys_replacements;
Ok(())
}
let t = 2;
let n = 7;
let all_keys = simulate_keygen(t, n);
// Remove the 2nd and 7th party
let mut keys = all_keys.clone();
keys.remove(6);
keys.remove(1);
let mut old_to_new_map: HashMap<u16, u16> = HashMap::new();
old_to_new_map.insert(1, 4);
old_to_new_map.insert(3, 1);
old_to_new_map.insert(4, 3);
old_to_new_map.insert(5, 6);
old_to_new_map.insert(6, 5);
// Simulate the replace
simulate_replace::<{ crate::M_SECURITY }>(&mut keys, &[2, 7], &old_to_new_map, t, n)
.unwrap();
// check that sum of old keys is equal to sum of new keys
let old_linear_secret_key: Vec<_> = (0..all_keys.len())
.map(|i| all_keys[i].keys_linear.x_i.clone())
.collect();
let new_linear_secret_key: Vec<_> = (0..keys.len())
.map(|i| keys[i].keys_linear.x_i.clone())
.collect();
let indices: Vec<_> = (0..(t + 1) as u16).collect();
let vss = VerifiableSS::<Secp256k1, sha2::Sha256> {
parameters: ShamirSecretSharing {
threshold: t,
share_count: n,
},
commitments: Vec::new(),
proof: DLogProof::<Secp256k1, sha2::Sha256>::prove(
&Scalar::random()),
};
assert_eq!(
vss.reconstruct(&indices[..], &old_linear_secret_key[0..(t + 1) as usize]),
vss.reconstruct(&indices[..], &new_linear_secret_key[0..(t + 1) as usize])
);
assert_ne!(old_linear_secret_key, new_linear_secret_key);
let offline_sign = simulate_offline_stage(keys, &[1, 2, 7]);
simulate_signing(offline_sign, b"ZenGo");
}
fn simulate_keygen(t: u16, n: u16) -> Vec<LocalKey<Secp256k1>> {
//simulate keygen
let mut simulation = Simulation::new();
simulation.enable_benchmarks(false);
for i in 1..=n {
simulation.add_party(Keygen::new(i, t, n).unwrap());
}
simulation.run().unwrap()
}
fn simulate_dkr_removal<const M: usize>(
keys: &mut Vec<LocalKey<Secp256k1>>,
remove_party_indices: Vec<u16>,
) {
let mut broadcast_messages: HashMap<usize, Vec<RefreshMessage<Secp256k1, Sha256, M>>> =
HashMap::new();
let mut new_dks: HashMap<usize, DecryptionKey> = HashMap::new();
let mut refresh_messages: Vec<RefreshMessage<Secp256k1, Sha256, M>> = Vec::new();
let mut party_key: HashMap<usize, LocalKey<Secp256k1>> = HashMap::new();
// TODO: Verify this is correct
let new_n = keys.len() as u16;
for key in keys.iter_mut() {
let (refresh_message, new_dk) = RefreshMessage::distribute(key.i, key, new_n).unwrap();
refresh_messages.push(refresh_message.clone());
new_dks.insert(refresh_message.party_index.into(), new_dk);
party_key.insert(refresh_message.party_index.into(), key.clone());
}
for refresh_message in refresh_messages.iter() {
broadcast_messages.insert(refresh_message.party_index.into(), Vec::new());
}
for refresh_message in refresh_messages.iter_mut() {
if !remove_party_indices.contains(&refresh_message.party_index.into()) {
refresh_message.remove_party_indices = remove_party_indices.clone();
} else {
let mut new_remove_party_indices = remove_party_indices.clone();
new_remove_party_indices
.retain(|value| *value != refresh_message.party_index.into());
refresh_message.remove_party_indices = new_remove_party_indices;
}
for (party_index, refresh_bucket) in broadcast_messages.iter_mut() {
if refresh_message
.remove_party_indices
.contains(&(*party_index as u16))
{
continue;
}
refresh_bucket.push(refresh_message.clone());
}
}
for remove_party_index in remove_party_indices.iter() {
assert_eq!(broadcast_messages[&(*remove_party_index as usize)].len(), 1);
}
// keys will be updated to refreshed values
for (party, key) in party_key.iter_mut() {
if remove_party_indices.contains(&(*party as u16)) {
continue;
}
RefreshMessage::collect(
broadcast_messages[party].clone().as_slice(),
key,
new_dks[party].clone(),
&[],
)
.expect("");
}
for remove_party_index in remove_party_indices {
let result = RefreshMessage::collect(
&broadcast_messages[&(remove_party_index as usize)],
&mut keys[remove_party_index as usize],
new_dks[&(remove_party_index as usize)].clone(),
&[],
);
assert!(result.is_err());
}
}
fn simulate_dkr<const M: usize>(
keys: &mut Vec<LocalKey<Secp256k1>>,
) -> (
Vec<RefreshMessage<Secp256k1, Sha256, M>>,
Vec<DecryptionKey>,
) {
let mut broadcast_vec: Vec<RefreshMessage<Secp256k1, Sha256, M>> = Vec::new();
let mut new_dks: Vec<DecryptionKey> = Vec::new();
let keys_len = keys.len();
for key in keys.iter_mut() {
let (refresh_message, new_dk) =
RefreshMessage::distribute(key.i, key, keys_len as u16).unwrap();
broadcast_vec.push(refresh_message);
new_dks.push(new_dk);
}
// keys will be updated to refreshed values
for i in 0..keys.len() as usize {
RefreshMessage::collect(&broadcast_vec, &mut keys[i], new_dks[i].clone(), &[])
.expect("");
}
(broadcast_vec, new_dks)
}
fn simulate_offline_stage(
local_keys: Vec<LocalKey<Secp256k1>>,
s_l: &[u16],
) -> Vec<CompletedOfflineStage> {
let mut simulation = Simulation::new();
simulation.enable_benchmarks(false);
for (i, &keygen_i) in (1..).zip(s_l) {
simulation.add_party(
OfflineStage::new(
i,
s_l.to_vec(),
local_keys[usize::from(keygen_i - 1)].clone(),
)
.unwrap(),
);
}
simulation.run().unwrap()
}
fn simulate_signing(offline: Vec<CompletedOfflineStage>, message: &[u8]) {
let message = create_hash(&[&BigInt::from_bytes(message)]);
let pk = &offline[0].public_key();
let parties = offline
.iter()
.map(|o| SignManual::new(message.clone(), o.clone()))
.collect::<Result<Vec<_>, _>>()
.unwrap();
let (parties, local_sigs): (Vec<_>, Vec<_>) = parties.into_iter().unzip();
// parties.remove(0).complete(&local_sigs[1..]).unwrap();
let local_sigs_except = |i: usize| {
let mut v = vec![];
v.extend_from_slice(&local_sigs[..i]);
if i + 1 < local_sigs.len() {
v.extend_from_slice(&local_sigs[i + 1..]);
}
v
};
assert!(parties
.into_iter()
.enumerate()
.map(|(i, p)| p.complete(&local_sigs_except(i)).unwrap())
.all(|signature| verify(&signature, &pk, &message).is_ok()));
}
fn create_hash(big_ints: &[&BigInt]) -> BigInt {
let hasher = Sha256::new();
for value in big_ints {
hasher.clone().chain(&BigInt::to_bytes(value));
}
let result_hex = hasher.finalize();
BigInt::from_bytes(&result_hex[..])
}
}