tor_proto/crypto/cell/
bench_utils.rs

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