Skip to content
Permalink
Browse files

Use const generics for the number of Haraka rounds.

  • Loading branch information
gendx committed Aug 22, 2019
1 parent f8e4ce2 commit 31668f483c986ad98abdb7a3de347d5a4880bf83
Showing with 19 additions and 65 deletions.
  1. +2 −2 src/hash.rs
  2. +1 −0 src/lib.rs
  3. +6 −25 src/primitives/haraka256.rs
  4. +10 −38 src/primitives/haraka512.rs
@@ -43,7 +43,7 @@ pub fn long_hash(src: &[u8]) -> Hash {
}

pub fn hash_n_to_n(dst: &mut Hash, src: &Hash) {
haraka256::haraka256_6round(&mut dst.h, &src.h)
haraka256::haraka256::<6>(&mut dst.h, &src.h)
}

#[cfg(test)]
@@ -54,7 +54,7 @@ pub fn hash_n_to_n_ret(src: &Hash) -> Hash {
}

pub fn hash_2n_to_n(dst: &mut Hash, src0: &Hash, src1: &Hash) {
haraka512::haraka512_6round(&mut dst.h, &src0.h, &src1.h)
haraka512::haraka512::<6>(&mut dst.h, &src0.h, &src1.h)
}

#[inline(always)]
@@ -1,4 +1,5 @@
#![feature(asm)]
#![feature(const_generics)]
#![feature(repr_simd)]
#![feature(test)]

@@ -24,30 +24,11 @@ fn aes_mix2(s0: &mut u64x2, s1: &mut u64x2, rci: usize) {
mix2(s0, s1);
}

// TODO: parametrize by number of rounds when supported by Rust
#[cfg(test)]
pub fn haraka256_5round(dst: &mut [u8; 32], src: &[u8; 32]) {
let mut s0 = u64x2::read(array_ref![src, 0, 16]);
let mut s1 = u64x2::read(array_ref![src, 16, 16]);

for i in 0..5 {
aes_mix2(&mut s0, &mut s1, 4 * i);
}

let t0 = u64x2::read(array_ref![src, 0, 16]);
let t1 = u64x2::read(array_ref![src, 16, 16]);
intrinsics::pxor(&mut s0, &t0);
intrinsics::pxor(&mut s1, &t1);

s0.write(array_mut_ref![dst, 0, 16]);
s1.write(array_mut_ref![dst, 16, 16]);
}

pub fn haraka256_6round(dst: &mut [u8; 32], src: &[u8; 32]) {
pub fn haraka256<const N_ROUNDS: usize>(dst: &mut [u8; 32], src: &[u8; 32]) {
let mut s0 = u64x2::read(array_ref![src, 0, 16]);
let mut s1 = u64x2::read(array_ref![src, 16, 16]);

for i in 0..6 {
for i in 0..N_ROUNDS {
aes_mix2(&mut s0, &mut s1, 4 * i);
}

@@ -147,7 +128,7 @@ mod tests {
\x78\xd0\x54\x5f\xb7\x2b\xf7\x0c\
\x69\x5c\x2a\x09\x23\xcb\xd4\x7b\
\xba\x11\x59\xef\xbf\x2b\x2c\x1c";
haraka256_5round(&mut dst, &src);
haraka256::<5>(&mut dst, &src);
assert_eq!(&dst, expect);
}

@@ -162,7 +143,7 @@ mod tests {
\xff\xf8\xcc\xf4\x69\x03\xd1\xc8\
\x18\x4b\x40\x4c\xc8\x37\x35\x55\
\x1c\x80\xa7\x2b\x5f\xb3\x20\x45";
haraka256_6round(&mut dst, &src);
haraka256::<6>(&mut dst, &src);
assert_eq!(&dst, expect);
}

@@ -175,7 +156,7 @@ mod tests {
\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\
\x10\x11\x12\x13\x14\x15\x16\x17\
\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
b.iter(|| haraka256_5round(&mut dst, &src));
b.iter(|| haraka256::<5>(&mut dst, &src));
}

#[bench]
@@ -185,6 +166,6 @@ mod tests {
\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\
\x10\x11\x12\x13\x14\x15\x16\x17\
\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
b.iter(|| haraka256_6round(&mut dst, &src));
b.iter(|| haraka256::<6>(&mut dst, &src));
}
}
@@ -47,37 +47,13 @@ fn truncstore(dst: &mut [u8; 32], s0: &u64x2, s1: &u64x2, s2: &u64x2, s3: &u64x2
LittleEndian::write_u64(array_mut_ref![dst, 24, 8], s3.0);
}

// TODO: parametrize by number of rounds when supported by Rust
#[cfg(test)]
pub fn haraka512_5round(dst: &mut [u8; 32], src0: &[u8; 32], src1: &[u8; 32]) {
pub fn haraka512<const N_ROUNDS: usize>(dst: &mut [u8; 32], src0: &[u8; 32], src1: &[u8; 32]) {
let mut s0 = u64x2::read(array_ref![src0, 0, 16]);
let mut s1 = u64x2::read(array_ref![src0, 16, 16]);
let mut s2 = u64x2::read(array_ref![src1, 0, 16]);
let mut s3 = u64x2::read(array_ref![src1, 16, 16]);

for i in 0..5 {
aes_mix4(&mut s0, &mut s1, &mut s2, &mut s3, 8 * i);
}

let t0 = u64x2::read(array_ref![src0, 0, 16]);
let t1 = u64x2::read(array_ref![src0, 16, 16]);
let t2 = u64x2::read(array_ref![src1, 0, 16]);
let t3 = u64x2::read(array_ref![src1, 16, 16]);
intrinsics::pxor(&mut s0, &t0);
intrinsics::pxor(&mut s1, &t1);
intrinsics::pxor(&mut s2, &t2);
intrinsics::pxor(&mut s3, &t3);

truncstore(dst, &s0, &s1, &s2, &s3);
}

pub fn haraka512_6round(dst: &mut [u8; 32], src0: &[u8; 32], src1: &[u8; 32]) {
let mut s0 = u64x2::read(array_ref![src0, 0, 16]);
let mut s1 = u64x2::read(array_ref![src0, 16, 16]);
let mut s2 = u64x2::read(array_ref![src1, 0, 16]);
let mut s3 = u64x2::read(array_ref![src1, 16, 16]);

for i in 0..6 {
for i in 0..N_ROUNDS {
aes_mix4(&mut s0, &mut s1, &mut s2, &mut s3, 8 * i);
}

@@ -230,8 +206,8 @@ mod tests {
assert_eq!(dst, expect);
}

pub fn haraka512_5round_bis(dst: &mut [u8; 32], src: &[u8; 64]) {
haraka512_5round(dst, array_ref![src, 0, 32], array_ref![src, 32, 32])
pub fn haraka512_bis<const N_ROUNDS: usize>(dst: &mut [u8; 32], src: &[u8; 64]) {
haraka512::<{ N_ROUNDS }>(dst, array_ref![src, 0, 32], array_ref![src, 32, 32])
}

#[test]
@@ -250,14 +226,10 @@ mod tests {
\x13\xb2\x92\x28\x7f\x30\x6f\x62\
\x5a\x6d\x57\x33\x1c\xae\x5f\x34\
\xdd\x92\x77\xb0\x94\x5b\xe2\xaa";
haraka512_5round_bis(&mut dst, &src);
haraka512_bis::<5>(&mut dst, &src);
assert_eq!(&dst, expect);
}

pub fn haraka512_6round_bis(dst: &mut [u8; 32], src: &[u8; 64]) {
haraka512_6round(dst, array_ref![src, 0, 32], array_ref![src, 32, 32])
}

#[test]
fn test_haraka512_6round() {
let mut dst = [0; 32];
@@ -273,7 +245,7 @@ mod tests {
\x15\x3c\x9a\x54\x13\xfb\x1e\x98\
\x4a\x91\x4f\x5b\x6f\xea\x17\x22\
\x85\x41\xce\x17\x07\xfc\x4e\x64";
haraka512_6round_bis(&mut dst, &src);
haraka512_bis::<6>(&mut dst, &src);
assert_eq!(&dst, expect);
}

@@ -290,7 +262,7 @@ mod tests {
\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\
\x30\x31\x32\x33\x34\x35\x36\x37\
\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f";
b.iter(|| haraka512_5round(&mut dst, &src1, &src2));
b.iter(|| haraka512::<5>(&mut dst, &src1, &src2));
}

#[bench]
@@ -304,7 +276,7 @@ mod tests {
\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\
\x30\x31\x32\x33\x34\x35\x36\x37\
\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f";
b.iter(|| haraka512_5round_bis(&mut dst, &src));
b.iter(|| haraka512_bis::<5>(&mut dst, &src));
}

#[bench]
@@ -318,7 +290,7 @@ mod tests {
\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\
\x30\x31\x32\x33\x34\x35\x36\x37\
\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f";
b.iter(|| haraka512_6round(&mut dst, &src1, &src2));
b.iter(|| haraka512::<6>(&mut dst, &src1, &src2));
}

#[bench]
@@ -332,6 +304,6 @@ mod tests {
\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\
\x30\x31\x32\x33\x34\x35\x36\x37\
\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f";
b.iter(|| haraka512_6round_bis(&mut dst, &src));
b.iter(|| haraka512_bis::<6>(&mut dst, &src));
}
}

0 comments on commit 31668f4

Please sign in to comment.
You can’t perform that action at this time.