-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathadvanced_eddsa_frost_sign_v2.rs
More file actions
202 lines (175 loc) · 6.94 KB
/
Copy pathadvanced_eddsa_frost_sign_v2.rs
File metadata and controls
202 lines (175 loc) · 6.94 KB
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
#![allow(clippy::indexing_slicing)]
use criterion::{Criterion, criterion_group, criterion_main};
use rand::Rng;
use rand_core::SeedableRng;
mod bench_utils;
use crate::bench_utils::{
MAX_MALICIOUS, PreparedOutputs, RECONSTRUCTION_LOWER_BOUND, SAMPLE_SIZE,
analyze_received_sizes, ed25519_prepare_presign, ed25519_prepare_sign_v2, participant_rng,
};
use threshold_signatures::{
ReconstructionThreshold,
frost::eddsa::{
KeygenOutput, PresignArguments, PresignOutput, SignatureOption, presign, sign::sign_v2,
},
participants::Participant,
protocol::Protocol,
test_utils::{
MockCryptoRng, Simulator, run_protocol, run_protocol_and_take_snapshots,
run_simulated_protocol,
},
};
type PreparedPresig = PreparedOutputs<PresignOutput>;
type PreparedSimulatedSig = PreparedOutputs<SignatureOption>;
/// Benches the presigning protocol
fn bench_presign(c: &mut Criterion) {
let num = RECONSTRUCTION_LOWER_BOUND.value();
let max_malicious = *MAX_MALICIOUS;
let setup = setup_presign_snapshot(num);
let size = setup.cached_simulator.get_view_size();
let mut group = c.benchmark_group("presign");
group.sample_size(*SAMPLE_SIZE);
group.bench_function(
format!("frost_ed25519_presign_advanced_MAX_MALICIOUS_{max_malicious}_PARTICIPANTS_{num}"),
|b| {
b.iter_batched(
|| prepare_simulate_presign(&setup),
|preps| {
run_simulated_protocol(preps.participant, preps.protocol, preps.simulator)
.expect("simulated replay should complete")
},
criterion::BatchSize::SmallInput,
);
},
);
analyze_received_sizes(&[size], true);
}
/// Benches the signing protocol
fn bench_sign(c: &mut Criterion) {
let num = RECONSTRUCTION_LOWER_BOUND.value();
let max_malicious = *MAX_MALICIOUS;
let setup = setup_sign_snapshot(*RECONSTRUCTION_LOWER_BOUND);
let size = setup.cached_simulator.get_view_size();
let mut group = c.benchmark_group("sign");
group.sample_size(*SAMPLE_SIZE);
group.bench_function(
format!("frost_ed25519_sign_v2_advanced_MAX_MALICIOUS_{max_malicious}_PARTICIPANTS_{num}"),
|b| {
b.iter_batched(
|| prepare_simulated_sign(&setup, *RECONSTRUCTION_LOWER_BOUND),
|preps| {
run_simulated_protocol(preps.participant, preps.protocol, preps.simulator)
.expect("simulated replay should complete")
},
criterion::BatchSize::SmallInput,
);
},
);
analyze_received_sizes(&[size], true);
}
criterion_group!(benches, bench_presign, bench_sign);
criterion_main!(benches);
struct PresignSetup {
participants: Vec<Participant>,
real_participant: Participant,
keygen_out: KeygenOutput,
real_participant_rng: MockCryptoRng,
cached_simulator: Simulator,
}
/// Expensive one-time setup for presign: runs the full N-party protocol to capture snapshots
fn setup_presign_snapshot(num_participants: usize) -> PresignSetup {
// Running presign a first time with snapshots
let mut rng = MockCryptoRng::seed_from_u64(42);
let preps = ed25519_prepare_presign(num_participants, &mut rng);
let (_, protocol_snapshot) = run_protocol_and_take_snapshots(preps.protocols)
.expect("Running protocol with snapshot should not have issues");
// choose the real_participant at random
let index_real_participant = rng.gen_range(0..num_participants);
let (real_participant, keygen_out) = preps.key_packages[index_real_participant].clone();
// rebuild the exact rng the real participant used during snapshot capture
let real_participant_rng = participant_rng(&preps.seeds, real_participant);
let cached_simulator = Simulator::new(real_participant, &protocol_snapshot)
.expect("Simulator should not be empty");
PresignSetup {
participants: preps.participants,
real_participant,
keygen_out,
real_participant_rng,
cached_simulator,
}
}
/// Cheap per-sample setup: creates fresh presign protocol and clones the cached simulator
fn prepare_simulate_presign(setup: &PresignSetup) -> PreparedPresig {
let real_protocol = presign(
&setup.participants,
setup.real_participant,
&PresignArguments {
private_share: setup.keygen_out.private_share,
threshold: *RECONSTRUCTION_LOWER_BOUND,
},
setup.real_participant_rng.clone(), // provide the exact same randomness
)
.map(|presig| Box::new(presig) as Box<dyn Protocol<Output = PresignOutput>>)
.expect("Presignature should succeed");
PreparedPresig {
participant: setup.real_participant,
protocol: real_protocol,
simulator: setup.cached_simulator.clone(),
}
}
struct SignSetup {
participants: Vec<Participant>,
real_participant: Participant,
keygen_out: KeygenOutput,
presig: PresignOutput,
message: Vec<u8>,
cached_simulator: Simulator,
}
/// Expensive one-time setup for sign: runs the full N-party protocol to capture snapshots
fn setup_sign_snapshot(threshold: ReconstructionThreshold) -> SignSetup {
let mut rng = MockCryptoRng::seed_from_u64(41);
let preps = ed25519_prepare_presign(threshold.value(), &mut rng);
let result = run_protocol(preps.protocols).expect("Prepare sign should not fail");
let preps = ed25519_prepare_sign_v2(&result, preps.key_packages, threshold, &mut rng);
let (_, protocol_snapshot) = run_protocol_and_take_snapshots(preps.protocols)
.expect("Running protocol with snapshot should not have issues");
let participants: Vec<Participant> = preps
.key_packages
.iter()
.map(|(participant, _)| *participant)
.collect();
// choose the real_participant being the coordinator
let (real_participant, keygen_out) = preps.key_packages[preps.index].clone();
let cached_simulator = Simulator::new(real_participant, &protocol_snapshot)
.expect("Simulator should not be empty");
SignSetup {
participants,
real_participant,
keygen_out,
presig: preps.presig,
message: preps.message,
cached_simulator,
}
}
/// Cheap per-sample setup: creates fresh sign protocol and clones the cached simulator
fn prepare_simulated_sign(
setup: &SignSetup,
threshold: ReconstructionThreshold,
) -> PreparedSimulatedSig {
let real_protocol = sign_v2(
&setup.participants,
threshold,
setup.real_participant,
setup.real_participant,
setup.keygen_out.clone(),
setup.presig.clone(),
setup.message.clone(),
)
.map(|sig| Box::new(sig) as Box<dyn Protocol<Output = SignatureOption>>)
.expect("Presignature should succeed");
PreparedSimulatedSig {
participant: setup.real_participant,
protocol: real_protocol,
simulator: setup.cached_simulator.clone(),
}
}