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
#[cfg(feature = "counter-galois-onion")]
13
pub use super::cgo::bench_utils as cgo;
14
pub use super::tor1::bench_utils as tor1;
15
use super::*;
16

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

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

            
32
impl 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.
45
pub 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
}