Skip to main content

tor_proto/crypto/cell/
bench_utils.rs

1//! Benchmark utilities for the `cell` module.
2pub use super::ClientLayer;
3pub use super::CryptInit;
4pub use super::InboundClientCrypt;
5pub use super::InboundClientLayer;
6pub use super::InboundRelayLayer;
7pub use super::OutboundClientCrypt;
8pub use super::OutboundClientLayer;
9pub use super::OutboundRelayLayer;
10pub use super::RelayCellBody;
11pub use super::RelayLayer;
12#[cfg(feature = "counter-galois-onion")]
13pub use super::cgo::bench_utils as cgo;
14pub use super::tor1::bench_utils as tor1;
15use super::*;
16
17/// The channel command used as additional data for the cryptographic operations benchmarks.
18pub const BENCH_CHAN_CMD: ChanCmd = ChanCmd::RELAY;
19
20impl InboundClientCrypt {
21    /// Helper method to add an inbound layer from a client layer pair.
22    pub fn add_layer_from_pair<F, B>(&mut self, pair: impl ClientLayer<F, B>)
23    where
24        F: OutboundClientLayer,
25        B: InboundClientLayer + Send + 'static,
26    {
27        let (_, inbound, _) = pair.split_client_layer();
28        self.add_layer(Box::new(inbound));
29    }
30}
31
32impl OutboundClientCrypt {
33    /// Helper method to add an outbound layer from a client layer pair.
34    pub fn add_layer_from_pair<F, B>(&mut self, pair: impl ClientLayer<F, B>)
35    where
36        F: OutboundClientLayer + Send + 'static,
37        B: InboundClientLayer,
38    {
39        let (outbound, _, _) = pair.split_client_layer();
40        self.add_layer(Box::new(outbound));
41    }
42}
43
44/// Encrypts the given `RelayCellBody` in the inbound direction by all the relays in a circuit.
45pub fn circuit_encrypt_inbound<F, B>(
46    cmd: ChanCmd,
47    cell: &mut RelayCellBody,
48    relay_states: Vec<impl RelayLayer<F, B>>,
49) where
50    F: OutboundRelayLayer,
51    B: InboundRelayLayer,
52{
53    for (i, state) in relay_states.into_iter().rev().enumerate() {
54        let (_, mut inbound, _) = state.split_relay_layer();
55        if i == 0 {
56            inbound.originate(cmd, cell);
57        } else {
58            inbound.encrypt_inbound(cmd, cell);
59        }
60    }
61}