Skip to content

Commit

Permalink
Merge pull request #5 from rphmeier/master
Browse files Browse the repository at this point in the history
clean up some unsafety, help LLVM elide bounds checks
  • Loading branch information
debris authored Sep 2, 2016
2 parents 784ab31 + acb78e0 commit d4b56b9
Showing 1 changed file with 37 additions and 39 deletions.
76 changes: 37 additions & 39 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}

Expand Down

0 comments on commit d4b56b9

Please sign in to comment.