From d99728a988e64e0993cbd0bacbf7b0effb9f46ff Mon Sep 17 00:00:00 2001 From: AnarchistHoneybun Date: Thu, 27 Jun 2024 21:58:25 +0530 Subject: [PATCH 1/2] standard formatting --- kupyna512/src/lib.rs | 12 ++++------ kupyna512/src/sub_units/mod.rs | 6 +---- kupyna512/src/sub_units/t_xor_plus/mod.rs | 3 +-- .../src/sub_units/t_xor_plus/tests/mod.rs | 2 +- kupyna512/src/tests/blocking_tests.rs | 22 +++++++++---------- kupyna512/src/tests/full_test.rs | 3 +-- kupyna512/src/tests/mod.rs | 4 ++-- kupyna512/src/tests/padding_tests.rs | 16 +++++++------- 8 files changed, 28 insertions(+), 40 deletions(-) diff --git a/kupyna512/src/lib.rs b/kupyna512/src/lib.rs index b52159ef..f692a931 100644 --- a/kupyna512/src/lib.rs +++ b/kupyna512/src/lib.rs @@ -1,11 +1,10 @@ +mod sub_units; #[cfg(test)] mod tests; -mod sub_units; const STATE_SIZE: usize = 1024; const HASH_SIZE: usize = 512; - fn pad_message(message: &[u8], msg_len_bits: usize, state_size: usize) -> Vec { let round_msg_len = message.len() * 8; let d = @@ -61,10 +60,10 @@ pub fn hash(message: Vec, length: Option) -> Result, &'static return Err("Message length is less than the provided length"); } - let mut trimmed_message = message[..(len/8)].to_vec(); + let mut trimmed_message = message[..(len / 8)].to_vec(); if len % 8 != 0 { - let extra_byte = message[len/8]; + let extra_byte = message[len / 8]; let extra_bits = len % 8; let mask = 0xFF << (8 - extra_bits); trimmed_message.push(extra_byte & mask); @@ -72,7 +71,6 @@ pub fn hash(message: Vec, length: Option) -> Result, &'static message = trimmed_message; message_length = len; - } else { message_length = message.len() * 8; } @@ -81,14 +79,12 @@ pub fn hash(message: Vec, length: Option) -> Result, &'static let blocks = divide_into_blocks(&padded_message, STATE_SIZE); - let mut init_vector: Vec = vec![0; STATE_SIZE/8]; + let mut init_vector: Vec = vec![0; STATE_SIZE / 8]; init_vector[0] = 0x80; // set the first bit of this init vector to high - let fin_vector = sub_units::plant(blocks, &init_vector); let hash = truncate(&fin_vector, HASH_SIZE); Ok(hash) } - diff --git a/kupyna512/src/sub_units/mod.rs b/kupyna512/src/sub_units/mod.rs index beeae736..9d865116 100644 --- a/kupyna512/src/sub_units/mod.rs +++ b/kupyna512/src/sub_units/mod.rs @@ -1,6 +1,6 @@ mod t_xor_plus; -use t_xor_plus::{t_xor_l, t_plus_l}; +use t_xor_plus::{t_plus_l, t_xor_l}; const ROUNDS: usize = 14; @@ -9,7 +9,6 @@ fn xor_bytes(a: &[u8], b: &[u8]) -> Vec { } fn silo(message_block: &[u8], prev_vector: &[u8]) -> Vec { - let m_xor_p = xor_bytes(message_block, prev_vector); let t_xor_mp = t_xor_l(&m_xor_p, ROUNDS); @@ -19,11 +18,9 @@ fn silo(message_block: &[u8], prev_vector: &[u8]) -> Vec { let return_vector = xor_bytes(&(xor_bytes(&t_xor_mp, &t_plus_m)), prev_vector); return_vector - } pub(crate) fn plant(message_blocks: Vec<&[u8]>, init_vector: &[u8]) -> Vec { - let mut last_vector = init_vector.to_vec(); for block in message_blocks { @@ -40,4 +37,3 @@ fn finalize(ult_processed_block: &[u8]) -> Vec { let final_state = xor_bytes(ult_processed_block, &t_xor_ult_processed_block); final_state } - diff --git a/kupyna512/src/sub_units/t_xor_plus/mod.rs b/kupyna512/src/sub_units/t_xor_plus/mod.rs index bf385edf..5a5ae3c7 100644 --- a/kupyna512/src/sub_units/t_xor_plus/mod.rs +++ b/kupyna512/src/sub_units/t_xor_plus/mod.rs @@ -13,7 +13,6 @@ type Matrix = [[u8; COLS]; ROWS]; use tables::{MDS_MATRIX, SBOXES}; - pub(crate) fn block_to_matrix(block: &[u8]) -> Matrix { let mut matrix = [[0u8; COLS]; ROWS]; for i in 0..ROWS { @@ -159,4 +158,4 @@ pub fn t_plus_l(block: &[u8], rounds: usize) -> Vec { state = mix_columns(state); } matrix_to_block(state) -} \ No newline at end of file +} diff --git a/kupyna512/src/sub_units/t_xor_plus/tests/mod.rs b/kupyna512/src/sub_units/t_xor_plus/tests/mod.rs index 24971c08..318c06d7 100644 --- a/kupyna512/src/sub_units/t_xor_plus/tests/mod.rs +++ b/kupyna512/src/sub_units/t_xor_plus/tests/mod.rs @@ -1,2 +1,2 @@ +mod test_individual_layers; mod test_xor_plus; -mod test_individual_layers; \ No newline at end of file diff --git a/kupyna512/src/tests/blocking_tests.rs b/kupyna512/src/tests/blocking_tests.rs index bb257df9..0aade34d 100644 --- a/kupyna512/src/tests/blocking_tests.rs +++ b/kupyna512/src/tests/blocking_tests.rs @@ -1,5 +1,4 @@ -use crate::{pad_message, divide_into_blocks}; - +use crate::{divide_into_blocks, pad_message}; const STATE_SIZE_512: usize = 1024; @@ -9,7 +8,7 @@ fn block_test_0() { let expected_block_count = 1; - let padded_message = pad_message(&message,0, STATE_SIZE_512); + let padded_message = pad_message(&message, 0, STATE_SIZE_512); let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512); @@ -28,7 +27,7 @@ fn block_test_8() { let expected_block_count = 1; - let padded_message = pad_message(&message,8, STATE_SIZE_512); + let padded_message = pad_message(&message, 8, STATE_SIZE_512); let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512); assert_eq!(blocks.len(), expected_block_count); @@ -52,7 +51,7 @@ fn block_test_512() { let expected_block_count = 1; - let padded_message = pad_message(&message,512, STATE_SIZE_512); + let padded_message = pad_message(&message, 512, STATE_SIZE_512); let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512); @@ -77,7 +76,7 @@ fn block_test_510() { let expected_block_count = 1; - let padded_message = pad_message(&message,510, STATE_SIZE_512); + let padded_message = pad_message(&message, 510, STATE_SIZE_512); let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512); @@ -88,7 +87,6 @@ fn block_test_510() { for block in blocks { assert_eq!(block.len(), block_len); } - } #[test] @@ -107,7 +105,7 @@ fn block_test_1024() { let expected_block_count = 2; - let padded_message = pad_message(&message,1024, STATE_SIZE_512); + let padded_message = pad_message(&message, 1024, STATE_SIZE_512); let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512); @@ -145,7 +143,7 @@ fn block_test_2048() { let expected_block_count = 3; - let padded_message = pad_message(&message,2048, STATE_SIZE_512); + let padded_message = pad_message(&message, 2048, STATE_SIZE_512); let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512); @@ -178,7 +176,7 @@ fn block_test_1536() { let expected_block_count = 2; - let padded_message = pad_message(&message,1536, STATE_SIZE_512); + let padded_message = pad_message(&message, 1536, STATE_SIZE_512); let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512); @@ -204,7 +202,7 @@ fn block_test_655() { let expected_block_count = 1; - let padded_message = pad_message(&message,655, STATE_SIZE_512); + let padded_message = pad_message(&message, 655, STATE_SIZE_512); let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512); @@ -215,4 +213,4 @@ fn block_test_655() { for block in blocks { assert_eq!(block.len(), block_len); } -} \ No newline at end of file +} diff --git a/kupyna512/src/tests/full_test.rs b/kupyna512/src/tests/full_test.rs index 33297ad6..8019adc8 100644 --- a/kupyna512/src/tests/full_test.rs +++ b/kupyna512/src/tests/full_test.rs @@ -1,8 +1,7 @@ - #[test] fn full_test() { let message = b"Hello, World!".to_vec(); let _message_length = 0; let _hash_code = crate::hash(message, None).unwrap(); -} \ No newline at end of file +} diff --git a/kupyna512/src/tests/mod.rs b/kupyna512/src/tests/mod.rs index 8d3aa901..0159900c 100644 --- a/kupyna512/src/tests/mod.rs +++ b/kupyna512/src/tests/mod.rs @@ -1,5 +1,5 @@ -mod padding_tests; mod blocking_tests; mod hashing_tests; +mod padding_tests; -mod full_test; \ No newline at end of file +mod full_test; diff --git a/kupyna512/src/tests/padding_tests.rs b/kupyna512/src/tests/padding_tests.rs index 95415088..f358f98a 100644 --- a/kupyna512/src/tests/padding_tests.rs +++ b/kupyna512/src/tests/padding_tests.rs @@ -18,7 +18,7 @@ fn pad_test_0() { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ]; - let padded_message = pad_message(&message,0, STATE_SIZE_512); + let padded_message = pad_message(&message, 0, STATE_SIZE_512); assert_eq!(padded_message, expected_output); } @@ -39,7 +39,7 @@ fn pad_test_8() { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ]; - let padded_message = pad_message(&message,8, STATE_SIZE_512); + let padded_message = pad_message(&message, 8, STATE_SIZE_512); assert_eq!(padded_message, expected_output); } @@ -66,7 +66,7 @@ fn pad_test_510() { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ]; - let padded_message = pad_message(&message,510, STATE_SIZE_512); + let padded_message = pad_message(&message, 510, STATE_SIZE_512); assert_eq!(padded_message, expected_output); } @@ -93,7 +93,7 @@ fn pad_test_512() { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ]; - let padded_message = pad_message(&message,512, STATE_SIZE_512); + let padded_message = pad_message(&message, 512, STATE_SIZE_512); assert_eq!(padded_message, expected_output); } @@ -133,7 +133,7 @@ fn pad_test_1024() { 0x00, ]; - let padded_message = pad_message(&message,1024, STATE_SIZE_512); + let padded_message = pad_message(&message, 1024, STATE_SIZE_512); assert_eq!(padded_message, expected_output); } @@ -190,7 +190,7 @@ fn pad_test_2048() { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ]; - let padded_message = pad_message(&message,2048, STATE_SIZE_512); + let padded_message = pad_message(&message, 2048, STATE_SIZE_512); assert_eq!(padded_message, expected_output); } @@ -234,7 +234,7 @@ fn pad_test_1536() { 0x00, ]; - let padded_message = pad_message(&message,1536, STATE_SIZE_512); + let padded_message = pad_message(&message, 1536, STATE_SIZE_512); assert_eq!(padded_message, expected_output); } @@ -262,7 +262,7 @@ fn pad_test_655() { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ]; - let padded_message = pad_message(&message,655, STATE_SIZE_512); + let padded_message = pad_message(&message, 655, STATE_SIZE_512); assert_eq!(padded_message, expected_output); } From fa5c07723cfac54420aed2f1ad7deb7c2a2ab7d3 Mon Sep 17 00:00:00 2001 From: AnarchistHoneybun Date: Thu, 27 Jun 2024 22:47:53 +0530 Subject: [PATCH 2/2] modified to address clippy warnings --- kupyna512/src/sub_units/mod.rs | 13 ++++--------- kupyna512/src/sub_units/t_xor_plus/mod.rs | 10 +++++----- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/kupyna512/src/sub_units/mod.rs b/kupyna512/src/sub_units/mod.rs index 9d865116..9a2df880 100644 --- a/kupyna512/src/sub_units/mod.rs +++ b/kupyna512/src/sub_units/mod.rs @@ -13,11 +13,9 @@ fn silo(message_block: &[u8], prev_vector: &[u8]) -> Vec { let t_xor_mp = t_xor_l(&m_xor_p, ROUNDS); - let t_plus_m = t_plus_l(&message_block, ROUNDS); + let t_plus_m = t_plus_l(message_block, ROUNDS); - let return_vector = xor_bytes(&(xor_bytes(&t_xor_mp, &t_plus_m)), prev_vector); - - return_vector + xor_bytes(&(xor_bytes(&t_xor_mp, &t_plus_m)), prev_vector) } pub(crate) fn plant(message_blocks: Vec<&[u8]>, init_vector: &[u8]) -> Vec { @@ -27,13 +25,10 @@ pub(crate) fn plant(message_blocks: Vec<&[u8]>, init_vector: &[u8]) -> Vec { last_vector = silo(block, &last_vector); } - let last_vector = finalize(&last_vector); - - last_vector + finalize(&last_vector) } fn finalize(ult_processed_block: &[u8]) -> Vec { let t_xor_ult_processed_block = t_xor_l(ult_processed_block, ROUNDS); - let final_state = xor_bytes(ult_processed_block, &t_xor_ult_processed_block); - final_state + xor_bytes(ult_processed_block, &t_xor_ult_processed_block) } diff --git a/kupyna512/src/sub_units/t_xor_plus/mod.rs b/kupyna512/src/sub_units/t_xor_plus/mod.rs index 5a5ae3c7..2c99f2c5 100644 --- a/kupyna512/src/sub_units/t_xor_plus/mod.rs +++ b/kupyna512/src/sub_units/t_xor_plus/mod.rs @@ -34,9 +34,9 @@ fn matrix_to_block(matrix: Matrix) -> Vec { } pub(crate) fn add_constant_xor(mut state: Matrix, round: usize) -> Matrix { - for j in 0..ROWS { + for (j, row) in state.iter_mut().enumerate().take(ROWS) { let constant = ((j * 0x10) ^ round) as u8; - state[j][0] ^= constant; + row[0] ^= constant; } state } @@ -58,8 +58,8 @@ pub(crate) fn add_constant_plus(mut state: Matrix, round: usize) -> Matrix { pub(crate) fn s_box_layer(mut state: Matrix) -> Matrix { for i in 0..COLS { - for j in 0..ROWS { - state[j][i] = SBOXES[i % 4][state[j][i] as usize]; + for row in state.iter_mut() { + row[i] = SBOXES[i % 4][row[i] as usize]; } } state @@ -69,7 +69,7 @@ pub(crate) fn rotate_rows(mut state: Matrix) -> Matrix { let mut temp = [0u8; ROWS]; let mut shift: i32 = -1; for i in 0..COLS { - if (i == COLS - 1) && true { + if i == COLS - 1 { shift = 11; } else { shift += 1;