Debian Codegraph @

debian/rust-vendor/constant_time_eq/tests/exhaustive.rs

raw
1 #![cfg(not(miri))]
2
3 use core::mem::size_of_val;
4 use core::slice::from_raw_parts_mut;
5
6 /// Misaligns the slice by one byte, to ensure no SIMD load instructions require alignment.
7 fn misalign_slice(buf: &mut [u128]) -> &mut [u8] {
8 let ptr = buf.as_mut_ptr() as *mut u8;
9 let len = size_of_val(buf);
10 unsafe { from_raw_parts_mut(ptr.add(1), len - 1) }
11 }
12
13 /// Confirms that all bit positions are being used for comparison, for a given length.
14 fn test_one_length<CTEQ>(a: &mut [u8], b: &mut [u8], n: usize, cteq: &CTEQ)
15 where
16 CTEQ: Fn(&[u8], &[u8]) -> bool,
17 {
18 let a = &mut a[..n];
19 let b = &mut b[..n];
20
21 assert!(cteq(a, b));
22 for i in 0..n {
23 for m in [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80] {
24 a[i] ^= m;
25 assert!(!cteq(a, b), "len={} a[{}] mask 0x{:02x}", n, i, m);
26 a[i] ^= m;
27
28 b[i] ^= m;
29 assert!(!cteq(a, b), "len={} b[{}] mask 0x{:02x}", n, i, m);
30 b[i] ^= m;
31 }
32 }
33 assert!(cteq(a, b));
34 }
35
36 /// Confirms that all bit positions are being used for comparison, for all lengths up to 1024 bits.
37 fn test_all_lengths<F: FnOnce(&mut [u8]), CTEQ>(fill: F, cteq: &CTEQ)
38 where
39 CTEQ: Fn(&[u8], &[u8]) -> bool,
40 {
41 let mut a = [0u128; 9];
42 let mut b = [0u128; 9];
43
44 let a = misalign_slice(&mut a);
45 let b = misalign_slice(&mut b);
46
47 fill(a);
48 b.copy_from_slice(a);
49
50 // Note: this is quadratic; do not increase the maximum length too much.
51 for n in 0..=128 {
52 test_one_length(a, b, n, cteq);
53 }
54 }
55
56 fn exhaustive_test_zeros<CTEQ>(cteq: &CTEQ)
57 where
58 CTEQ: Fn(&[u8], &[u8]) -> bool,
59 {
60 test_all_lengths(|buf| buf.fill(0), cteq);
61 }
62
63 fn exhaustive_test_ones<CTEQ>(cteq: &CTEQ)
64 where
65 CTEQ: Fn(&[u8], &[u8]) -> bool,
66 {
67 test_all_lengths(|buf| buf.fill(!0), cteq);
68 }
69
70 fn exhaustive_test_random<CTEQ>(cteq: &CTEQ)
71 where
72 CTEQ: Fn(&[u8], &[u8]) -> bool,
73 {
74 // Simple xorshift PRNG, from https://www.jstatsoft.org/article/view/v008i14
75 let mut state: u32 = 2463534242;
76 let xorshift32 = || {
77 state ^= state << 13;
78 state ^= state >> 17;
79 state ^= state << 5;
80 state as u8
81 };
82
83 test_all_lengths(|buf| buf.fill_with(xorshift32), cteq);
84 }
85
86 #[test]
87 fn exhaustive_test_zeros_simd() {
88 use constant_time_eq::constant_time_eq;
89 exhaustive_test_zeros(&constant_time_eq);
90 }
91
92 #[test]
93 fn exhaustive_test_ones_simd() {
94 use constant_time_eq::constant_time_eq;
95 exhaustive_test_ones(&constant_time_eq);
96 }
97
98 #[test]
99 fn exhaustive_test_random_simd() {
100 use constant_time_eq::constant_time_eq;
101 exhaustive_test_random(&constant_time_eq);
102 }
103
104 #[test]
105 fn exhaustive_test_zeros_generic() {
106 use constant_time_eq::generic::constant_time_eq;
107 exhaustive_test_zeros(&constant_time_eq);
108 }
109
110 #[test]
111 fn exhaustive_test_ones_generic() {
112 use constant_time_eq::generic::constant_time_eq;
113 exhaustive_test_ones(&constant_time_eq);
114 }
115
116 #[test]
117 fn exhaustive_test_random_generic() {
118 use constant_time_eq::generic::constant_time_eq;
119 exhaustive_test_random(&constant_time_eq);
120 }