Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing tests in sha2 #603

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions sha2/src/core_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,47 @@ impl SerializableState for Sha512VarCore {
Ok(Self { state, block_len })
}
}

#[cfg(test)]
mod tests {
use crate::core_api::Array;
use crate::core_api::U128;
use crate::{Sha256VarCore, Sha512VarCore};
use digest::core_api::Block;
use digest::core_api::UpdateCore;
use digest::core_api::VariableOutputCore;

#[test]
fn test_update_blocks_after_blocks() {
let mut core = Sha256VarCore::new(32).unwrap();
let block = Block::<Sha256VarCore>::default();
core.update_blocks(&[block, block]);
core.update_blocks(&[block]);
assert_eq!(core.block_len, 3);
}

#[test]
fn test_sha256_var_core_new_valid_224() {
assert!(Sha256VarCore::new(28).is_ok());
assert!(Sha256VarCore::new(32).is_ok());
assert!(Sha256VarCore::new(0).is_err());
}

#[test]
fn update_blocks_increases_block_length() {
let mut core = Sha512VarCore::new(64).unwrap();
let initial_block_len = core.block_len;
let block = Array::<u8, U128>::default();
let blocks = [block; 1];
core.update_blocks(&blocks);
assert_eq!(core.block_len, initial_block_len + blocks.len() as u128);
}

#[test]
fn sha512_var_core_new_valid_output_sizes() {
assert!(Sha512VarCore::new(28).is_ok());
assert!(Sha512VarCore::new(32).is_ok());
assert!(Sha512VarCore::new(48).is_ok());
assert!(Sha512VarCore::new(64).is_ok());
}
}
16 changes: 16 additions & 0 deletions sha2/src/sha256/soft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,19 @@ pub fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
sha256_digest_block_u32(state, &block_u32);
}
}

#[cfg(test)]
mod tests {
use crate::sha256::soft::schedule;

#[test]
fn schedule_test() {
let v0: [u32; 4] = [0, 0, 0, 0];
let v1: [u32; 4] = [0, 0, 0, 0];
let v2: [u32; 4] = [0, 0, 0, 0];
let v3: [u32; 4] = [0, 0, 0, 0];
let expected: [u32; 4] = [0, 0, 0, 0];
let result = schedule(v0, v1, v2, v3);
assert_eq!(result, expected);
}
}
61 changes: 61 additions & 0 deletions sha2/src/sha512/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,64 @@ const SHA512_ROUNDS_NUM: usize = 80;
const SHA512_HASH_BYTE_LEN: usize = 64;
const SHA512_HASH_WORDS_NUM: usize = SHA512_HASH_BYTE_LEN / size_of::<u64>();
const SHA512_BLOCK_WORDS_NUM: usize = SHA512_BLOCK_BYTE_LEN / size_of::<u64>();

#[cfg(test)]
mod tests {
use crate::sha512::x86::sha512_compress_x86_64_avx2;

#[test]
fn test_sha512_compress_x86_64_avx2() {
CXWorks marked this conversation as resolved.
Show resolved Hide resolved
unsafe {
let mut state = [
0x6a09e667f3bcc908,
0xbb67ae8584caa73b,
0x3c6ef372fe94f82b,
0xa54ff53a5f1d36f1,
0x510e527fade682d1,
0x9b05688c2b3e6c1f,
0x1f83d9abfb41bd6b,
0x5be0cd19137e2179,
];
let blocks = [[0u8; 128], [0u8; 128]];
sha512_compress_x86_64_avx2(&mut state, &blocks);
assert_eq!(
state,
[
0x1F6A2DE220B43EBC,
0xF052C997D2B28419,
0x8ACEEC8F1A7F8421,
0xD16BD3C52FAC3460,
0x08F8C96E9029BC30,
0x284D3E4C0EDDC462,
0x0261B89DD3808F39,
0x43044E2B40D0899D
]
);
let mut state = [
0x6a09e667f3bcc908,
0xbb67ae8584caa73b,
0x3c6ef372fe94f82b,
0xa54ff53a5f1d36f1,
0x510e527fade682d1,
0x9b05688c2b3e6c1f,
0x1f83d9abfb41bd6b,
0x5be0cd19137e2179,
];
let blocks = [[0u8; 128]];
sha512_compress_x86_64_avx2(&mut state, &blocks);
assert_eq!(
state,
[
0xCF7881D5774ACBE8,
0x533362E0FBC78070,
0x0267639D87460EDA,
0x3086CB40E85931B0,
0x717DC95288A023A3,
0x96BAB2C14CE0B5E0,
0x6FC4FE04EAE33E0B,
0x91F4D80CBD668BEE
]
);
}
}
}
Loading