aboutsummaryrefslogtreecommitdiff
path: root/nearby/presence/np_adv/benches/deser_adv.rs
blob: b52f53a5184d57f4c4748341120b55e7e9736144 (plain)
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
395
396
397
398
399
400
401
402
403
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![allow(
    missing_docs,
    unused_results,
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::indexing_slicing,
    clippy::panic
)]

use core::marker::PhantomData;
use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use crypto_provider::{CryptoProvider, CryptoRng};
use crypto_provider_default::CryptoProviderImpl;
use ldt_np_adv::LegacySalt;

use np_adv::deserialization_arena;
use np_adv::extended::serialize::AdvertisementType;
use np_adv::{
    credential::{book::*, v0::*, v1::*, *},
    de_type::EncryptedIdentityDataElementType,
    deserialize_advertisement,
    extended::{
        data_elements::{GenericDataElement, TxPowerDataElement},
        deserialize::VerificationMode,
        serialize::{
            AdvBuilder as ExtendedAdvBuilder, MicEncryptedSectionEncoder, PublicSectionEncoder,
            SectionBuilder, SectionEncoder, SignedEncryptedSectionEncoder,
        },
    },
    legacy::{
        actions::{ActionBits, ActionsDataElement},
        serialize::{AdvBuilder as LegacyAdvBuilder, LdtIdentity},
        ShortMetadataKey,
    },
    shared_data::{ContextSyncSeqNum, TxPower},
    MetadataKey, PublicIdentity,
};
use rand::{Rng as _, SeedableRng as _};
use strum::IntoEnumIterator;

pub fn deser_adv_v1_encrypted(c: &mut Criterion) {
    let mut crypto_rng = <CryptoProviderImpl as CryptoProvider>::CryptoRng::new();

    for crypto_type in CryptoMaterialType::iter() {
        for &identity_type in &[VerificationMode::Mic, VerificationMode::Signature] {
            for &num_identities in &[10, 100, 1000] {
                for &num_sections in &[1, 2] {
                    // measure worst-case performance -- the correct identities will be the last
                    // num_sections of the identities to be tried
                    c.bench_function(
                        &format!(
                            "Deser V1 encrypted: crypto={crypto_type:?}/mode={identity_type:?}/ids={num_identities}/sections={num_sections}"
                        ),
                        |b| {
                            let identities = (0..num_identities)
                                .map(|_| V1Identity::random(&mut crypto_rng))
                                .collect::<Vec<_>>();

                            let mut adv_builder = ExtendedAdvBuilder::new(AdvertisementType::Encrypted);

                            // take the first n identities, one section per identity
                            for identity in identities.iter().take(num_sections) {
                                let broadcast_cm = SimpleSignedBroadcastCryptoMaterial::new(
                                    identity.key_seed,
                                    identity.extended_metadata_key,
                                    identity.key_pair.private_key(),
                                );
                                match identity_type {
                                    VerificationMode::Mic => {
                                        let mut sb = adv_builder
                                            .section_builder(MicEncryptedSectionEncoder::<CryptoProviderImpl>::new_random_salt(
                                                &mut crypto_rng,
                                                EncryptedIdentityDataElementType::Private,
                                                &broadcast_cm,
                                            ))
                                            .unwrap();

                                        add_des(&mut sb);
                                        sb.add_to_advertisement();
                                    }
                                    VerificationMode::Signature => {
                                        let mut sb = adv_builder
                                            .section_builder(SignedEncryptedSectionEncoder::<CryptoProviderImpl>::new_random_salt(
                                                &mut crypto_rng,
                                                EncryptedIdentityDataElementType::Private,
                                                &broadcast_cm,
                                            ))
                                            .unwrap();

                                        add_des(&mut sb);
                                        sb.add_to_advertisement();
                                    }
                                }
                            }

                            let adv = adv_builder.into_advertisement();

                            run_with_v1_creds::<
                                CryptoProviderImpl
                            >(
                                b, crypto_type, identities, adv.as_slice()
                            )
                        },
                    );
                }
            }
        }
    }
}

pub fn deser_adv_v1_plaintext(c: &mut Criterion) {
    c.bench_function("Deser V1 plaintext: sections=1", |b| {
        let mut adv_builder = ExtendedAdvBuilder::new(AdvertisementType::Plaintext);

        let mut sb = adv_builder.section_builder(PublicSectionEncoder::default()).unwrap();

        add_des(&mut sb);
        sb.add_to_advertisement();

        let adv = adv_builder.into_advertisement();

        run_with_v1_creds::<CryptoProviderImpl>(
            b,
            CryptoMaterialType::MinFootprint,
            vec![],
            adv.as_slice(),
        )
    });
}

pub fn deser_adv_v0_encrypted(c: &mut Criterion) {
    let mut rng = rand::rngs::StdRng::from_entropy();
    for crypto_type in CryptoMaterialType::iter() {
        for &num_identities in &[10, 100, 1000] {
            // measure worst-case performance -- the correct identities will be the last
            // num_sections of the identities to be tried
            c.bench_function(
                &format!("Deser V0 encrypted: crypto={crypto_type:?}/ids={num_identities}"),
                |b| {
                    let identities = (0..num_identities)
                        .map(|_| V0Identity::random(&mut rng))
                        .collect::<Vec<_>>();

                    let identity = &identities[0];

                    let broadcast_cm = SimpleBroadcastCryptoMaterial::<V0>::new(
                        identity.key_seed,
                        identity.legacy_metadata_key,
                    );

                    let mut adv_builder =
                        LegacyAdvBuilder::new(LdtIdentity::<CryptoProviderImpl>::new(
                            EncryptedIdentityDataElementType::Private,
                            LegacySalt::from(rng.gen::<[u8; 2]>()),
                            &broadcast_cm,
                        ));

                    let mut action_bits = ActionBits::default();
                    action_bits.set_action(ContextSyncSeqNum::try_from(3).unwrap());
                    adv_builder.add_data_element(ActionsDataElement::from(action_bits)).unwrap();

                    let adv = adv_builder.into_advertisement().unwrap();

                    run_with_v0_creds::<CryptoProviderImpl>(
                        b,
                        crypto_type,
                        identities,
                        adv.as_slice(),
                    )
                },
            );
        }
    }
}

pub fn deser_adv_v0_plaintext(c: &mut Criterion) {
    let mut adv_builder = LegacyAdvBuilder::new(PublicIdentity);

    let mut action_bits = ActionBits::default();
    action_bits.set_action(ContextSyncSeqNum::try_from(3).unwrap());
    adv_builder.add_data_element(ActionsDataElement::from(action_bits)).unwrap();
    let adv = adv_builder.into_advertisement().unwrap();

    let cred_book = CredentialBookBuilder::<EmptyMatchedCredential>::build_cached_slice_book::<
        0,
        0,
        CryptoProviderImpl,
    >(&[], &[]);

    for &num_advs in &[1, 10, 100, 1000] {
        c.bench_function(
            format!("Deser V0 plaintext with {num_advs} advertisements").as_str(),
            |b| {
                b.iter(|| {
                    for _ in 0..num_advs {
                        black_box(
                            deserialize_advertisement::<_, CryptoProviderImpl>(
                                deserialization_arena!(),
                                black_box(adv.as_slice()),
                                black_box(&cred_book),
                            )
                            .expect("Should succeed"),
                        );
                    }
                })
            },
        );
    }
}

/// Benchmark decrypting a V0 advertisement with credentials built from the reversed list of
/// identities
fn run_with_v0_creds<C>(
    b: &mut Bencher,
    crypto_material_type: CryptoMaterialType,
    identities: Vec<V0Identity<C>>,
    adv: &[u8],
) where
    C: CryptoProvider,
{
    let mut creds = identities
        .into_iter()
        .map(|identity| identity.into_discovery_credential())
        .map(|discovery_credential| MatchableCredential {
            discovery_credential,
            match_data: EmptyMatchedCredential,
        })
        .collect::<Vec<_>>();

    // reverse the identities so that we're scanning to the end of the
    // cred source for predictably bad performance
    creds.reverse();

    match crypto_material_type {
        CryptoMaterialType::MinFootprint => {
            // Cache size of 0 => only min-footprint creds
            let cred_book = CredentialBookBuilder::<_>::build_cached_slice_book::<
                0,
                0,
                CryptoProviderImpl,
            >(&creds, &[]);

            b.iter(|| {
                black_box(
                    deserialize_advertisement::<_, C>(deserialization_arena!(), adv, &cred_book)
                        .map(|_| 0_u8)
                        .unwrap(),
                )
            });
        }
        CryptoMaterialType::Precalculated => {
            let cred_book = CredentialBookBuilder::<_>::build_precalculated_owned_book::<C>(
                creds,
                core::iter::empty(),
            );
            b.iter(|| {
                black_box(
                    deserialize_advertisement::<_, C>(deserialization_arena!(), adv, &cred_book)
                        .map(|_| 0_u8)
                        .unwrap(),
                )
            });
        }
    }
}

/// Benchmark decrypting a V1 advertisement with credentials built from the reversed list of
/// identities
fn run_with_v1_creds<C>(
    b: &mut Bencher,
    crypto_material_type: CryptoMaterialType,
    identities: Vec<V1Identity<C>>,
    adv: &[u8],
) where
    C: CryptoProvider,
{
    let mut creds = identities
        .into_iter()
        .map(|identity| identity.into_discovery_credential())
        .map(|discovery_credential| MatchableCredential {
            discovery_credential,
            match_data: EmptyMatchedCredential,
        })
        .collect::<Vec<_>>();

    // reverse the identities so that we're scanning to the end of the
    // cred source for predictably bad performance
    creds.reverse();

    match crypto_material_type {
        CryptoMaterialType::MinFootprint => {
            // Cache size of 0 => only min-footprint creds
            let cred_book = CredentialBookBuilder::<_>::build_cached_slice_book::<
                0,
                0,
                CryptoProviderImpl,
            >(&[], &creds);

            b.iter(|| {
                black_box(
                    deserialize_advertisement::<_, C>(deserialization_arena!(), adv, &cred_book)
                        .map(|_| 0_u8)
                        .unwrap(),
                )
            });
        }
        CryptoMaterialType::Precalculated => {
            let cred_book = CredentialBookBuilder::<_>::build_precalculated_owned_book::<C>(
                core::iter::empty(),
                creds,
            );
            b.iter(|| {
                black_box(
                    deserialize_advertisement::<_, C>(deserialization_arena!(), adv, &cred_book)
                        .map(|_| 0_u8)
                        .unwrap(),
                )
            });
        }
    }
}
fn add_des<I: SectionEncoder>(
    sb: &mut SectionBuilder<&mut np_adv::extended::serialize::AdvBuilder, I>,
) {
    sb.add_de_res(|_| TxPower::try_from(17).map(TxPowerDataElement::from)).unwrap();
    sb.add_de_res(|_| GenericDataElement::try_from(100_u32.into(), &[0; 10])).unwrap();
}
criterion_group!(
    benches,
    deser_adv_v1_encrypted,
    deser_adv_v1_plaintext,
    deser_adv_v0_encrypted,
    deser_adv_v0_plaintext
);
criterion_main!(benches);

struct V0Identity<C: CryptoProvider> {
    key_seed: [u8; 32],
    legacy_metadata_key: ShortMetadataKey,
    _marker: PhantomData<C>,
}

impl<C: CryptoProvider> V0Identity<C> {
    /// Generate a new identity with random crypto material
    fn random<R: rand::Rng + rand::CryptoRng>(rng: &mut R) -> Self {
        Self {
            key_seed: rng.gen(),
            legacy_metadata_key: ShortMetadataKey(rng.gen()),
            _marker: PhantomData,
        }
    }
    /// Convert this `V0Identity` into a V0 discovery credential.
    fn into_discovery_credential(self) -> V0DiscoveryCredential {
        SimpleBroadcastCryptoMaterial::<V0>::new(self.key_seed, self.legacy_metadata_key)
            .derive_v0_discovery_credential::<C>()
    }
}

struct V1Identity<C: CryptoProvider> {
    key_seed: [u8; 32],
    extended_metadata_key: MetadataKey,
    key_pair: np_ed25519::KeyPair<C>,
}

impl<C: CryptoProvider> V1Identity<C> {
    /// Generate a new identity with random crypto material
    fn random(rng: &mut C::CryptoRng) -> Self {
        Self {
            key_seed: rng.gen(),
            extended_metadata_key: MetadataKey(rng.gen()),
            key_pair: np_ed25519::KeyPair::<C>::generate(),
        }
    }
    /// Convert this `V1Identity` into a `V1DiscoveryCredential`.
    fn into_discovery_credential(self) -> V1DiscoveryCredential {
        SimpleSignedBroadcastCryptoMaterial::new(
            self.key_seed,
            self.extended_metadata_key,
            self.key_pair.private_key(),
        )
        .derive_v1_discovery_credential::<C>()
    }
}

#[derive(strum_macros::EnumIter, Clone, Copy, Debug)]
enum CryptoMaterialType {
    MinFootprint,
    Precalculated,
}