From acb78e05edd0169a55ddd30b24ebd4c109de4532 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 1 Sep 2016 16:36:52 +0200 Subject: [PATCH] clean up some unsafety, help LLVM elide bounds checks --- src/lib.rs | 76 ++++++++++++++++++++++++++---------------------------- 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fd2f5f8..8f68d3c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -94,52 +94,50 @@ macro_rules! FOR5 { } /// keccak-f[1600] -pub fn keccakf(a: &mut [u64]) { - unsafe { - let mut b: [u64; 5] = [0; 5]; - let mut t: u64; - let mut x: usize; - let mut y: usize; - - for i in 0..24 { - // Theta - FOR5!(x, 1, { - *b.get_unchecked_mut(x) = 0; - FOR5!(y, 5, { - *b.get_unchecked_mut(x) ^= *a.get_unchecked(x + y); - }); +pub fn keccakf(a: &mut [u64; PLEN]) { + let mut b: [u64; 5] = [0; 5]; + let mut t: u64; + let mut x: usize; + let mut y: usize; + + for i in 0..24 { + // Theta + FOR5!(x, 1, { + b[x] = 0; + FOR5!(y, 5, { + b[x] ^= a[x + y]; }); + }); - FOR5!(x, 1, { - FOR5!(y, 5, { - *a.get_unchecked_mut(y + x) ^= *b.get_unchecked((x + 4) % 5) ^ b.get_unchecked((x + 1) % 5).rotate_left(1); - }); + FOR5!(x, 1, { + FOR5!(y, 5, { + a[y + x] ^= b[(x + 4) % 5] ^ b[(x + 1) % 5].rotate_left(1); }); + }); - // Rho and pi - t = *a.get_unchecked(1); - x = 0; - REPEAT24!({ - *b.get_unchecked_mut(0) = *a.get_unchecked(*PI.get_unchecked(x)); - *a.get_unchecked_mut(*PI.get_unchecked(x)) = t.rotate_left(*RHO.get_unchecked(x)); - }, { - t = *b.get_unchecked(0); - x += 1; - }); + // Rho and pi + t = a[1]; + x = 0; + REPEAT24!({ + b[0] = a[PI[x]]; + a[PI[x]] = t.rotate_left(RHO[x]); + }, { + t = b[0]; + x += 1; + }); - // Chi - FOR5!(y, 5, { - FOR5!(x, 1, { - *b.get_unchecked_mut(x) = *a.get_unchecked(y + x); - }); - FOR5!(x, 1, { - *a.get_unchecked_mut(y + x) = *b.get_unchecked(x) ^ ((!b.get_unchecked((x + 1) % 5)) & b.get_unchecked((x + 2) % 5)); - }); + // Chi + FOR5!(y, 5, { + FOR5!(x, 1, { + b[x] = a[y + x]; }); + FOR5!(x, 1, { + a[y + x] = b[x] ^ ((!b[(x + 1) % 5]) & (b[(x + 2) % 5])); + }); + }); - // Iota - *a.get_unchecked_mut(0) ^= *RC.get_unchecked(i); - } + // Iota + a[0] ^= RC[i]; } }