diff --git a/src/lib.rs b/src/lib.rs index 84f7cb4..768c082 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,8 +28,8 @@ mod tests { 0x80, 0xf8, 0x43, 0x4a, ]; - for i in 0..32 { - print!("{:#001x}", res[i]); + for i in res { + print!("{:#001x}", i); } println!(); diff --git a/src/main.rs b/src/main.rs index 659cadb..b0af881 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::{env, fs::File, io::Read, time}; +use std::{env, fs::File, io::Read}; use sha3::sha3::Sha3_256; @@ -17,38 +17,14 @@ fn main() { file.read_to_end(&mut file_data).unwrap(); - // println!("{:?}", (0x01 as u64).to_ne_bytes()); + let mut sha = Sha3_256::default(); - // let text = "hola"; - - let mut time = 0_f32; - - let mut res: [u8; 32] = [0_u8; 32]; - - for _ in 0..1000 { - let mut sha = Sha3_256::default(); - let now = time::Instant::now(); - - sha.absorb(&file_data); - res = sha.squeeze(); - - let elapsed = now.elapsed().as_micros() as f32; - time += elapsed; - } - - // let expected_res: [u8; 32] = [ - // 0x8a, 0xf1, 0x3d, 0x92, 0x44, 0x61, 0x8e, 0xee, 0x87, 0x6d, 0x04, 0x31, 0xf3, 0x44, 0x9a, - // 0xa4, 0xff, 0x95, 0x27, 0x4c, 0xa3, 0xe7, 0xe5, 0xc6, 0x54, 0x19, 0x79, 0x49, 0x9f, 0x5b, - // 0x85, 0xde, - // ]; + sha.absorb(&file_data); + let res: [u8; 32] = sha.squeeze(); print!("SHA3-256: "); - for i in 0..32 { - print!("{:x}", res[i]); + for x in res { + print!("{:x}", x); } println!(); - - println!("Avg Time taken: {} ms", (time / 1000_f32) / 1000_f32); - - // assert!(res == expected_res); } diff --git a/src/sha3.rs b/src/sha3.rs index fe35faf..41edbdc 100644 --- a/src/sha3.rs +++ b/src/sha3.rs @@ -1,8 +1,6 @@ // Rate: 1088 // Capacity: 512 -use std::arch::x86_64::*; - use std::array; use crate::consts::LFSR_LUT; @@ -45,10 +43,6 @@ impl Sha3_256 { } } - // for bytes in inputs_u64 { - - // } - self.state[in_len] ^= DELIMITER_SUFFIX; if (DELIMITER_SUFFIX & 0x80) != 0 && in_len == RATE_256 - 1 { @@ -80,10 +74,9 @@ impl Sha3_256 { } fn keccak_permute(input: &mut [u8; TOTAL_STATE_SIZE]) { - // let (lanes, _) = input.as_chunks_mut::<8>(); let (pre, lanes, post) = unsafe { input.align_to_mut::() }; - assert!(pre.len() == 0); - assert!(post.len() == 0); + assert!(pre.is_empty()); + assert!(post.is_empty()); assert!(lanes.len() == 25); let mut lfsr_state = 0x01_u8; for _ in 0..ROUNDS { @@ -100,27 +93,7 @@ fn keccak_permute(input: &mut [u8; TOTAL_STATE_SIZE]) { for x in 0..5 { d = c[(x + 4) % 5] ^ rol64(c[(x + 1) % 5], 1); - // let mut out = [0_u64; 8]; - // unsafe { - // let a: __m512i = - // _mm512_set_epi64(d as i64, d as i64, d as i64, d as i64, d as i64, 0, 0, 0); - // let b: __m512i = _mm512_set_epi64( - // get_lane2(lanes, x, 0) as i64, - // get_lane2(lanes, x, 1) as i64, - // get_lane2(lanes, x, 2) as i64, - // get_lane2(lanes, x, 3) as i64, - // get_lane2(lanes, x, 4) as i64, - // 0, - // 0, - // 0, - // ); - // let res = _mm512_xor_epi64(a, b); - // _mm512_storeu_epi64(out.as_mut_ptr() as *mut i64, res); - // } - // for i in 0..5 { - // set_lane2(out[i], x, i, lanes); - // } for y in 0..5 { xor_lane2(d, lanes, x, y); } @@ -144,10 +117,6 @@ fn keccak_permute(input: &mut [u8; TOTAL_STATE_SIZE]) { // χ step for y in 0..5 { - // let mut temp2 = [0_u64; 5]; - // for x in 0..5 { - // temp2[x] = get_lane(lanes, x, y); - // } let temp2: [u64; 5] = array::from_fn(|x| get_lane2(lanes, x, y)); for x in 0..5 { set_lane2( @@ -165,27 +134,14 @@ fn keccak_permute(input: &mut [u8; TOTAL_STATE_SIZE]) { let bit_pos: usize = (1 << j) - 1; let (lfsr_out, new_lfsr) = LFSR_LUT[lfsr_state as usize]; lfsr_state = new_lfsr; - // if lfsr86540(&mut lfsr_state) { - // xor_lane((1 as u64) << bit_pos, lanes, 0, 0); - // } if lfsr_out { - xor_lane2((1 as u64) << bit_pos, lanes, 0, 0); + xor_lane2((1_u64) << bit_pos, lanes, 0, 0); } } } } -#[inline] -fn get_lane(lanes: &[[u8; 8]], x: usize, y: usize) -> u64 { - u64::from_ne_bytes(lanes[x + 5 * y]) -} - -#[inline] -fn set_lane(lane: u64, x: usize, y: usize, lanes: &mut [[u8; 8]]) { - lanes[x + 5 * y] = lane.to_ne_bytes(); -} - #[inline] fn get_lane2(lanes: &[u64], x: usize, y: usize) -> u64 { lanes[x + 5 * y] @@ -201,25 +157,7 @@ fn rol64(v: u64, off: usize) -> u64 { ((v) << off) ^ ((v) >> (64 - off)) } -#[inline] -fn xor_lane(lane: u64, lanes: &mut [[u8; 8]], x: usize, y: usize) { - set_lane(get_lane(lanes, x, y) ^ lane, x, y, lanes); -} - #[inline] fn xor_lane2(lane: u64, lanes: &mut [u64], x: usize, y: usize) { set_lane2(get_lane2(lanes, x, y) ^ lane, x, y, lanes); } - -// Function that computes the linear feedback shift register (LFSR) -// I have absolutely no idea wtf is this shit. Copied from a github repo lol. -// SUSCEPTIBLE TO BE CONVERTED INTO A TABLE -// fn lfsr86540(lfsr: &mut u8) -> bool { -// let res = (*lfsr & 0x01) != 0; -// if (*lfsr & 0x80) != 0 { -// *lfsr = (*lfsr << 1) ^ 0x71; -// } else { -// *lfsr <<= 1; -// } -// res -// }