CtRunner

Struct CtRunner 

Source
pub struct CtRunner { /* private fields */ }
Expand description

Used for timing single operations at a time

Implementations§

Source§

impl CtRunner

Source

pub fn run_one<T, F>(&mut self, class: Class, f: F)
where F: Fn() -> T,

Runs and times a single operation whose constant-timeness is in question

Examples found in repository?
examples/ctbench-foo.rs (line 29)
12fn arith(runner: &mut CtRunner, rng: &mut BenchRng) {
13    let mut inputs = Vec::new();
14    let mut classes = Vec::new();
15
16    // Make 100,000 inputs on each run
17    for _ in 0..100_000 {
18        inputs.push(rng.gen::<usize>());
19        // Randomly pick which distribution this example belongs to
20        if rng.gen::<bool>() {
21            classes.push(Class::Left);
22        } else {
23            classes.push(Class::Right);
24        }
25    }
26
27    for (u, class) in inputs.into_iter().zip(classes.into_iter()) {
28        // Time some random arithmetic operations
29        runner.run_one(class, || ((u + 10) / 6) << 5);
30    }
31}
32
33// Benchmark for equality of vectors. This does an early return when it finds an inequality, so it
34// should be very much not constant-time
35fn vec_eq(runner: &mut CtRunner, rng: &mut BenchRng) {
36    // Make vectors of size 100
37    let vlen = 100;
38    let mut inputs: Vec<(Vec<u8>, Vec<u8>)> = Vec::new();
39    let mut classes = Vec::new();
40
41    // Make 100,000 random pairs of vectors
42    for _ in 0..100_000 {
43        // Flip a coin. If true, make a pair of vectors that are equal to each other and put it
44        // in the Left distribution
45        if rng.gen::<bool>() {
46            let v1 = rand_vec(vlen, rng);
47            let v2 = v1.clone();
48            inputs.push((v1, v2));
49            classes.push(Class::Left);
50        }
51        // Otherwise, make a pair of vectors that differ at the 6th element and put it in the
52        // right distribution
53        else {
54            let v1 = rand_vec(vlen, rng);
55            let mut v2 = v1.clone();
56            v2[5] = 7;
57            inputs.push((v1, v2));
58            classes.push(Class::Right);
59        }
60    }
61
62    for (class, (u, v)) in classes.into_iter().zip(inputs.into_iter()) {
63        // Now time how long it takes to do a vector comparison
64        runner.run_one(class, || u == v);
65    }
66}

Trait Implementations§

Source§

impl Default for CtRunner

Source§

fn default() -> CtRunner

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V