Debian Codegraph @

debian/rust-vendor/constant_time_eq/src/neon.rs

raw
1 //! NEON implementation of `constant_time_eq` and `constant_time_eq_n`.
2
3 use core::arch::asm;
4 use core::mem::size_of;
5
6 #[cfg(target_arch = "aarch64")]
7 use core::arch::aarch64::*;
8
9 use crate::with_dit;
10
11 /// Equivalent to `vceqq_u8`, but hidden from the compiler.
12 ///
13 /// The use of inline assembly instead of an intrinsic prevents a sufficiently
14 /// smart compiler from computing the mask in other ways which might not be
15 /// constant time (for instance, looping through the input and using branching
16 /// to set the vector elements).
17 #[must_use]
18 #[inline(always)]
19 fn vceqq_u8_hide(a: uint8x16_t, b: uint8x16_t) -> uint8x16_t {
20 let mut c;
21 // SAFETY: this file is compiled only when NEON is available
22 // SAFETY: assembly instruction touches only these registers
23 #[cfg(target_arch = "aarch64")]
24 unsafe {
25 asm!("cmeq {c:v}.16b, {a:v}.16b, {b:v}.16b",
26 c = lateout(vreg) c,
27 a = in(vreg) a,
28 b = in(vreg) b,
29 options(pure, nomem, preserves_flags, nostack));
30 }
31 c
32 }
33
34 /// Equivalent to `vandq_u8`, but hidden from the compiler.
35 ///
36 /// The use of inline assembly instead of an intrinsic prevents a sufficiently
37 /// smart compiler from short circuiting the computation once the mask becomes
38 /// all zeros.
39 #[must_use]
40 #[inline(always)]
41 fn vandq_u8_hide(a: uint8x16_t, b: uint8x16_t) -> uint8x16_t {
42 let mut c;
43 // SAFETY: this file is compiled only when NEON is available
44 // SAFETY: assembly instruction touches only these registers
45 #[cfg(target_arch = "aarch64")]
46 unsafe {
47 asm!("and {c:v}.16b, {a:v}.16b, {b:v}.16b",
48 c = lateout(vreg) c,
49 a = in(vreg) a,
50 b = in(vreg) b,
51 options(pure, nomem, preserves_flags, nostack));
52 }
53 c
54 }
55
56 /// Equivalent to `vshrn_n_u16(..., 4)`, but hidden from the compiler.
57 ///
58 /// The use of inline assembly instead of an intrinsic prevents a sufficiently
59 /// smart compiler from extracting the mask in other ways which might not be
60 /// constant time (for instance, looping through the elements of the vector).
61 #[must_use]
62 #[inline(always)]
63 fn vshrn_n_u16_4_hide(a: uint16x8_t) -> uint8x8_t {
64 let mut mask;
65 // SAFETY: this file is compiled only when NEON is available
66 // SAFETY: assembly instruction touches only these registers
67 #[cfg(target_arch = "aarch64")]
68 unsafe {
69 asm!("shrn {mask:v}.8b, {a:v}.8h, #{n}",
70 mask = lateout(vreg) mask,
71 a = in(vreg) a,
72 n = const 4,
73 options(pure, nomem, preserves_flags, nostack));
74 }
75 mask
76 }
77
78 /// Moves a mask created by `vceqq_u8` to a `u64` register, with each all-zero or
79 /// all-ones mask byte represented as an all-zero or all-ones half-byte.
80 #[must_use]
81 #[inline(always)]
82 fn get_mask_u64(mask: uint8x16_t) -> u64 {
83 // SAFETY: this file is compiled only when NEON is available
84 unsafe {
85 let mask = vshrn_n_u16_4_hide(vreinterpretq_u16_u8(mask));
86 vget_lane_u64(vreinterpret_u64_u8(mask), 0)
87 }
88 }
89
90 /// Safe equivalent to `vld1q_u8` for byte slices.
91 #[must_use]
92 #[inline(always)]
93 fn vld1q_u8_safe(src: &[u8]) -> uint8x16_t {
94 assert_eq!(src.len(), size_of::<uint8x16_t>());
95
96 // SAFETY: this file is compiled only when NEON is available
97 // SAFETY: the slice has enough bytes for a `uint8x16_t`
98 unsafe { vld1q_u8(src.as_ptr()) }
99 }
100
101 /// Safe equivalent to `vld1q_u8_x2` for byte slices.
102 #[must_use]
103 #[inline(always)]
104 fn vld1q_u8_x2_safe(src: &[u8]) -> uint8x16x2_t {
105 assert_eq!(src.len(), size_of::<uint8x16x2_t>());
106
107 // SAFETY: this file is compiled only when NEON is available
108 // SAFETY: the slice has enough bytes for a `uint8x16x2_t`
109 unsafe { vld1q_u8_x2(src.as_ptr()) }
110 }
111
112 /// NEON implementation of `constant_time_eq` and `constant_time_eq_n`.
113 #[must_use]
114 #[inline(always)]
115 fn constant_time_eq_neon(mut a: &[u8], mut b: &[u8]) -> bool {
116 if a.len() != b.len() {
117 return false;
118 }
119
120 // This statement does nothing, because a.len() == b.len() here,
121 // but it makes the optimizer elide some useless bounds checks.
122 b = &b[..a.len()];
123
124 const LANES: usize = 16;
125
126 let tmp = if a.len() >= LANES * 2 {
127 let tmpa = vld1q_u8_x2_safe(&a[..LANES * 2]);
128 let tmpb = vld1q_u8_x2_safe(&b[..LANES * 2]);
129
130 a = &a[LANES * 2..];
131 b = &b[LANES * 2..];
132
133 let mut mask0 = vceqq_u8_hide(tmpa.0, tmpb.0);
134 let mut mask1 = vceqq_u8_hide(tmpa.1, tmpb.1);
135
136 while a.len() >= LANES * 2 {
137 let tmpa = vld1q_u8_x2_safe(&a[..LANES * 2]);
138 let tmpb = vld1q_u8_x2_safe(&b[..LANES * 2]);
139
140 a = &a[LANES * 2..];
141 b = &b[LANES * 2..];
142
143 let tmp0 = vceqq_u8_hide(tmpa.0, tmpb.0);
144 let tmp1 = vceqq_u8_hide(tmpa.1, tmpb.1);
145
146 mask0 = vandq_u8_hide(mask0, tmp0);
147 mask1 = vandq_u8_hide(mask1, tmp1);
148 }
149
150 if a.len() >= LANES {
151 let tmpa = vld1q_u8_safe(&a[..LANES]);
152 let tmpb = vld1q_u8_safe(&b[..LANES]);
153
154 a = &a[LANES..];
155 b = &b[LANES..];
156
157 let tmp = vceqq_u8_hide(tmpa, tmpb);
158
159 mask0 = vandq_u8_hide(mask0, tmp);
160 }
161
162 let mask = vandq_u8_hide(mask0, mask1);
163 get_mask_u64(mask) ^ !0
164 } else if a.len() >= LANES {
165 let tmpa = vld1q_u8_safe(&a[..LANES]);
166 let tmpb = vld1q_u8_safe(&b[..LANES]);
167
168 a = &a[LANES..];
169 b = &b[LANES..];
170
171 let mask = vceqq_u8_hide(tmpa, tmpb);
172
173 get_mask_u64(mask) ^ !0
174 } else {
175 0
176 };
177
178 // Note: be careful to not short-circuit ("tmp == 0 &&") the comparison here
179 crate::generic::constant_time_eq_impl(a, b, tmp)
180 }
181
182 #[must_use]
183 pub fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
184 with_dit(|| constant_time_eq_neon(a, b))
185 }
186
187 #[must_use]
188 pub fn constant_time_eq_n<const N: usize>(a: &[u8; N], b: &[u8; N]) -> bool {
189 with_dit(|| constant_time_eq_neon(&a[..], &b[..]))
190 }