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