Skip to content

Commit

Permalink
Merge pull request #29 from dvdplm/dont-unroll-outer-loop
Browse files Browse the repository at this point in the history
Add a test for overlapping buffers (WAS: Don't unroll outer loop for speed™)
  • Loading branch information
debris authored May 19, 2018
2 parents 2ea10c6 + 3513571 commit fabf4e7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
Cargo.lock
*.swp
.idea
35 changes: 21 additions & 14 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extern crate tiny_keccak;
extern crate core;

use tiny_keccak::*;

Expand All @@ -20,20 +21,26 @@ fn empty_keccak() {
}

#[test]
fn string_keccak_256() {
let mut keccak_256 = Keccak::new_keccak256();
let data: Vec<u8> = From::from("hello world");
keccak_256.update(&data);
let mut res : [u8;32] = [0;32];
keccak_256.finalize(&mut res);
let expected = vec![
0x47, 0x17, 0x32, 0x85, 0xa8, 0xd7, 0x34, 0x1e,
0x5e, 0x97, 0x2f, 0xc6, 0x77, 0x28, 0x63, 0x84,
0xf8, 0x02, 0xf8, 0xef, 0x42, 0xa5, 0xec, 0x5f,
0x03, 0xbb, 0xfa, 0x25, 0x4c, 0xb0, 0x1f, 0xad
];
let ref_ex: &[u8] = &expected;
assert_eq!(&res, ref_ex);
fn string_keccak_256_overlapping_buffer() {
let mut in_and_out : [u8; 32] = [0;32];
for i in 1..6 { in_and_out[i as usize - 1] = i }

let ptr = in_and_out.as_mut_ptr();
Keccak::keccak256(
unsafe {
core::slice::from_raw_parts(ptr, 5) // read a piece from start of in_and_out
},
&mut in_and_out, // write over the whole array
);

let expected = vec![125, 135, 197, 234, 117, 247, 55, 139, 183, 1, 228, 4, 197, 6, 57, 22, 26, 243, 239, 246, 98, 147, 233, 243, 117, 181, 241, 126, 181, 4, 118, 244];
assert_eq!(&in_and_out, &expected.as_ref());

// Verify using overlapping in/out buffers yields same result as a "normal" hash
let control_in : [u8;5] = [1,2,3,4,5];
let mut control_out : [u8;32] = [0;32];
Keccak::keccak256(&control_in, &mut control_out);
assert_eq!(&control_out, &in_and_out);
}

#[test]
Expand Down

0 comments on commit fabf4e7

Please sign in to comment.