1
//! Benchmark utilities for the `cell` module.
2
pub use super::ClientLayer;
3
pub use super::CryptInit;
4
pub use super::InboundClientCrypt;
5
pub use super::InboundClientLayer;
6
pub use super::InboundRelayLayer;
7
pub use super::OutboundClientCrypt;
8
pub use super::OutboundClientLayer;
9
pub use super::OutboundRelayLayer;
10
pub use super::RelayCellBody;
11
pub use super::RelayLayer;
12
pub use super::cgo::bench_utils as cgo;
13
pub use super::tor1::bench_utils as tor1;
14
use super::*;
15

            
16
/// The channel command used as additional data for the cryptographic operations benchmarks.
17
pub const BENCH_CHAN_CMD: ChanCmd = ChanCmd::RELAY;
18

            
19
impl 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

            
31
impl 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.
44
pub 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
}