Skip to content

Commit 3c75b74

Browse files
only enable neon on ARM64
32-bit ARM is both [locked behind nightly](rust-lang/rust#111800) and [unsound](rust-lang/rust#129880).
1 parent 34bc8bc commit 3c75b74

File tree

3 files changed

+14
-29
lines changed

3 files changed

+14
-29
lines changed

src/secure/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ cfg_if! {
2929
use soft::Matrix;
3030
}
3131
}
32-
} else if #[cfg(target_feature = "neon")] {
32+
} else if #[cfg(all(target_feature = "neon", any(target_arch = "aarch64", target_arch = "arm64ec")))] {
3333
mod neon;
3434
use neon::Matrix;
3535
} else {

src/secure/neon.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use super::{util::DEPTH, ChaCha, Machine, BUF_LEN, ROW_A};
2-
#[cfg(any(target_arch = "aarch64", target_arch = "arm64ec"))]
32
use core::arch::aarch64::*;
4-
#[cfg(target_arch = "arm")]
5-
use core::arch::arm::*;
63
use core::{mem::transmute, ops::Add};
74

85
#[derive(Clone)]

src/secure/util.rs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,18 @@ impl<M> From<[u8; CHACHA_SEED_LEN]> for ChaCha<M> {
8787
}
8888
}
8989

90-
impl<M: Machine> ChaCha<M> {
90+
#[cfg(test)]
91+
impl<M> ChaCha<M> {
9192
/// Utility for setting all bytes when testing.
92-
#[cfg(test)]
93-
fn broadcast<const VALUE: u64>(&mut self) {
93+
pub fn broadcast<const VALUE: u64>(&mut self) {
9494
self.row_b.u64x2 = [VALUE, VALUE];
9595
self.row_c.u64x2 = [VALUE, VALUE];
9696
// Tests always expect the counter to start at 0.
9797
self.row_d.u64x2 = [0, VALUE];
9898
}
99+
}
99100

101+
impl<M: Machine> ChaCha<M> {
100102
/// Computes 4 blocks of chacha and fills `buf` with the output.
101103
///
102104
/// This is the inline boundary. Everything beneath this should be
@@ -134,9 +136,9 @@ mod tests {
134136

135137
#[test]
136138
fn correct_constant() {
137-
const EXPECTED: &[u8; 16] = b"expand 32-byte k";
138-
const ACTUAL: [u8; 16] = unsafe { transmute(ROW_A) };
139-
assert!(ACTUAL == *EXPECTED);
139+
const EXPECTED: [u8; 16] = *b"expand 32-byte k";
140+
const ACTUAL: [u8; 16] = unsafe { ROW_A.u8x16 };
141+
assert!(ACTUAL == EXPECTED);
140142
}
141143

142144
#[cfg(target_feature = "neon")]
@@ -379,26 +381,12 @@ mod tests {
379381
assert_blocks_match(&buf, &KEYSTREAM_BLOCK_0, &KEYSTREAM_BLOCK_1);
380382
}
381383

382-
/// We're only able to retrieve chacha output in blocks of 4, but we
384+
/// We always compute chacha output in blocks of 4, but we
383385
/// only test the first 2 blocks, discarding the rest.
384-
///
385-
/// Only checking the first 2 is just as good as checking many more,
386-
/// since if our implementation were even slightly incorrect the output
387-
/// would diverge almost instantly. Even more so because we test against
388-
/// multiple keystreams.
389-
fn assert_blocks_match(buf: &[u64], block_0: &[u8], block_1: &[u8]) {
390-
// Sanity checks
391-
assert!(buf.len() == BUF_LEN);
392-
assert!(block_0.len() == 64);
393-
assert!(block_1.len() == 64);
394-
395-
// Reinterpret &[u64] as &[u8]
396-
let buf = unsafe {
397-
let data = buf.as_ptr().cast::<u8>();
398-
let len = buf.len() * size_of::<u64>();
399-
core::slice::from_raw_parts(data, len)
400-
};
401-
// Compare chacha output against expected results
386+
#[inline]
387+
fn assert_blocks_match(buf: &[u64; BUF_LEN], block_0: &[u8], block_1: &[u8]) {
388+
const LEN: usize = size_of::<[u64; BUF_LEN]>();
389+
let buf: &[u8; LEN] = unsafe { transmute(buf) };
402390
assert!(buf[..64] == *block_0);
403391
assert!(buf[64..128] == *block_1);
404392
}

0 commit comments

Comments
 (0)