pub struct CtRunner { /* private fields */ }Expand description
Used for timing single operations at a time
Implementations§
Source§impl CtRunner
impl CtRunner
Sourcepub fn run_one<T, F>(&mut self, class: Class, f: F)where
F: Fn() -> T,
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§
Auto Trait Implementations§
impl Freeze for CtRunner
impl RefUnwindSafe for CtRunner
impl Send for CtRunner
impl Sync for CtRunner
impl Unpin for CtRunner
impl UnwindSafe for CtRunner
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more