|
| 1 | +use criterion::{criterion_group, criterion_main, Criterion}; |
| 2 | + |
| 3 | +mod simplpedpop_benches { |
| 4 | + use std::collections::{BTreeMap, BTreeSet}; |
| 5 | + |
| 6 | + use super::*; |
| 7 | + use criterion::BenchmarkId; |
| 8 | + use merlin::Transcript; |
| 9 | + use rand_core::OsRng; |
| 10 | + use schnorrkel::{ |
| 11 | + identifier::Identifier, |
| 12 | + simplpedpop::{ |
| 13 | + round1::{self, PrivateData, PublicData, PublicMessage}, |
| 14 | + round2::{self, Messages}, |
| 15 | + round3, Identifiers, Parameters, |
| 16 | + }, |
| 17 | + }; |
| 18 | + |
| 19 | + fn generate_parameters(max_signers: u16, min_signers: u16) -> Vec<Parameters> { |
| 20 | + (1..=max_signers) |
| 21 | + .map(|_| Parameters::new(max_signers, min_signers)) |
| 22 | + .collect() |
| 23 | + } |
| 24 | + |
| 25 | + fn round1( |
| 26 | + participants: u16, |
| 27 | + threshold: u16, |
| 28 | + ) -> ( |
| 29 | + Vec<Parameters>, |
| 30 | + Vec<PrivateData>, |
| 31 | + Vec<PublicData>, |
| 32 | + Vec<BTreeSet<PublicMessage>>, |
| 33 | + ) { |
| 34 | + let parameters_list = generate_parameters(participants, threshold); |
| 35 | + |
| 36 | + let mut participants_round1_private_data = Vec::new(); |
| 37 | + let mut participants_round1_public_data = Vec::new(); |
| 38 | + let mut all_public_messages_vec = Vec::new(); |
| 39 | + |
| 40 | + for parameters in ¶meters_list { |
| 41 | + let (private_data, public_message, public_data) = |
| 42 | + round1::run(parameters.clone(), OsRng).unwrap(); |
| 43 | + participants_round1_private_data.push(private_data); |
| 44 | + participants_round1_public_data.push(public_data); |
| 45 | + all_public_messages_vec.push(public_message); |
| 46 | + } |
| 47 | + |
| 48 | + let mut received_round1_public_messages: Vec<BTreeSet<round1::PublicMessage>> = Vec::new(); |
| 49 | + |
| 50 | + for (i, own_message) in all_public_messages_vec.iter().enumerate() { |
| 51 | + let mut messages_for_participant = all_public_messages_vec |
| 52 | + .iter() |
| 53 | + .enumerate() |
| 54 | + .filter(|&(j, _)| i != j) |
| 55 | + .map(|(_, message)| message.clone()) |
| 56 | + .collect::<BTreeSet<_>>(); |
| 57 | + |
| 58 | + received_round1_public_messages.push(messages_for_participant); |
| 59 | + } |
| 60 | + |
| 61 | + ( |
| 62 | + parameters_list, |
| 63 | + participants_round1_private_data, |
| 64 | + participants_round1_public_data, |
| 65 | + received_round1_public_messages, |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + fn round2( |
| 70 | + parameters_list: &Vec<Parameters>, |
| 71 | + participants_round1_private_data: Vec<PrivateData>, |
| 72 | + participants_round1_public_data: &Vec<PublicData>, |
| 73 | + participants_round1_public_messages: &Vec<BTreeSet<PublicMessage>>, |
| 74 | + ) -> ( |
| 75 | + Vec<round2::PublicData<Transcript>>, |
| 76 | + Vec<Messages>, |
| 77 | + Vec<Identifiers>, |
| 78 | + Vec<Identifier>, |
| 79 | + ) { |
| 80 | + let mut participants_round2_public_data = Vec::new(); |
| 81 | + let mut participants_round2_public_messages = Vec::new(); |
| 82 | + let mut participants_set_of_participants = Vec::new(); |
| 83 | + let mut identifiers_vec = Vec::new(); |
| 84 | + |
| 85 | + for i in 0..*parameters_list[0].participants() { |
| 86 | + let result = round2::run( |
| 87 | + participants_round1_private_data[i as usize].clone(), |
| 88 | + &participants_round1_public_data[i as usize].clone(), |
| 89 | + participants_round1_public_messages[i as usize].clone(), |
| 90 | + Transcript::new(b"simplpedpop"), |
| 91 | + ) |
| 92 | + .expect("Round 2 should complete without errors!"); |
| 93 | + |
| 94 | + participants_round2_public_data.push(result.0.clone()); |
| 95 | + participants_round2_public_messages.push(result.1); |
| 96 | + participants_set_of_participants.push(result.0.identifiers().clone()); |
| 97 | + identifiers_vec.push(*result.0.identifiers().own_identifier()); |
| 98 | + } |
| 99 | + |
| 100 | + ( |
| 101 | + participants_round2_public_data, |
| 102 | + participants_round2_public_messages, |
| 103 | + participants_set_of_participants, |
| 104 | + identifiers_vec, |
| 105 | + ) |
| 106 | + } |
| 107 | + |
| 108 | + fn benchmark_simplpedpop(c: &mut Criterion) { |
| 109 | + let mut group = c.benchmark_group("SimplPedPoP"); |
| 110 | + |
| 111 | + group |
| 112 | + .sample_size(10) |
| 113 | + .warm_up_time(std::time::Duration::from_secs(2)) |
| 114 | + .measurement_time(std::time::Duration::from_secs(30)); |
| 115 | + |
| 116 | + for &n in [3, 10, 100].iter() { |
| 117 | + let participants = n; |
| 118 | + let threshold = (n * 2 + 2) / 3; |
| 119 | + let parameters_list = generate_parameters(participants, threshold); |
| 120 | + |
| 121 | + group.bench_function(BenchmarkId::new("round1", participants), |b| { |
| 122 | + b.iter(|| { |
| 123 | + round1::run(parameters_list[0].clone(), OsRng).unwrap(); |
| 124 | + }) |
| 125 | + }); |
| 126 | + |
| 127 | + let ( |
| 128 | + parameters_list, |
| 129 | + participants_round1_private_data, |
| 130 | + participants_round1_public_data, |
| 131 | + participants_round1_public_messages, |
| 132 | + ) = round1(participants, threshold); |
| 133 | + |
| 134 | + group.bench_function(BenchmarkId::new("round2", participants), |b| { |
| 135 | + b.iter(|| { |
| 136 | + round2::run( |
| 137 | + participants_round1_private_data[0].clone(), |
| 138 | + &participants_round1_public_data[0], |
| 139 | + participants_round1_public_messages[0].clone(), |
| 140 | + Transcript::new(b"simplpedpop"), |
| 141 | + ) |
| 142 | + .unwrap(); |
| 143 | + }) |
| 144 | + }); |
| 145 | + |
| 146 | + let ( |
| 147 | + participants_round2_public_data, |
| 148 | + participants_round2_messages, |
| 149 | + participants_sets_of_participants, |
| 150 | + identifiers_vec, |
| 151 | + ) = round2( |
| 152 | + ¶meters_list, |
| 153 | + participants_round1_private_data.clone(), |
| 154 | + &participants_round1_public_data, |
| 155 | + &participants_round1_public_messages, |
| 156 | + ); |
| 157 | + |
| 158 | + let participants_round2_public_messages: Vec<round2::PublicMessage> = |
| 159 | + participants_round2_messages |
| 160 | + .iter() |
| 161 | + .map(|msg| msg.public_message().clone()) |
| 162 | + .collect(); |
| 163 | + |
| 164 | + let participants_round2_private_messages: Vec< |
| 165 | + BTreeMap<Identifier, round2::PrivateMessage>, |
| 166 | + > = participants_round2_messages |
| 167 | + .iter() |
| 168 | + .map(|msg| msg.private_messages().clone()) |
| 169 | + .collect(); |
| 170 | + |
| 171 | + let received_round2_public_messages = participants_round2_public_messages |
| 172 | + .iter() |
| 173 | + .enumerate() |
| 174 | + .filter(|(index, _msg)| { |
| 175 | + identifiers_vec[*index] |
| 176 | + != *participants_sets_of_participants[0].own_identifier() |
| 177 | + }) |
| 178 | + .map(|(index, msg)| (identifiers_vec[index], msg.clone())) |
| 179 | + .collect::<BTreeMap<Identifier, round2::PublicMessage>>(); |
| 180 | + |
| 181 | + let mut round2_private_messages: Vec<BTreeMap<Identifier, round2::PrivateMessage>> = |
| 182 | + Vec::new(); |
| 183 | + |
| 184 | + for participants in participants_sets_of_participants.iter() { |
| 185 | + let mut messages_for_participant = BTreeMap::new(); |
| 186 | + |
| 187 | + for (i, round_messages) in participants_round2_private_messages.iter().enumerate() { |
| 188 | + if let Some(message) = round_messages.get(&participants.own_identifier()) { |
| 189 | + messages_for_participant.insert(identifiers_vec[i], message.clone()); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + round2_private_messages.push(messages_for_participant); |
| 194 | + } |
| 195 | + |
| 196 | + group.bench_function(BenchmarkId::new("round3", participants), |b| { |
| 197 | + b.iter(|| { |
| 198 | + round3::run( |
| 199 | + &received_round2_public_messages, |
| 200 | + &participants_round2_public_data[0], |
| 201 | + &participants_round1_public_data[0], |
| 202 | + participants_round1_private_data[0].clone(), |
| 203 | + &round2_private_messages[0], |
| 204 | + ) |
| 205 | + .unwrap(); |
| 206 | + }) |
| 207 | + }); |
| 208 | + } |
| 209 | + |
| 210 | + group.finish(); |
| 211 | + } |
| 212 | + |
| 213 | + criterion_group! { |
| 214 | + name = simplpedpop_benches; |
| 215 | + config = Criterion::default(); |
| 216 | + targets = |
| 217 | + benchmark_simplpedpop, |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +criterion_main!(simplpedpop_benches::simplpedpop_benches); |
0 commit comments