133 lines
3.4 KiB
Rust
Raw Normal View History

2022-07-26 11:02:12 -07:00
use anyhow::Result;
2022-11-16 14:23:15 -08:00
use blake2::Blake2b512;
use ethereum_types::{U256, U512};
2022-07-26 11:02:12 -07:00
use rand::{thread_rng, Rng};
2022-10-03 15:30:17 -07:00
use ripemd::{Digest, Ripemd160};
use sha2::Sha256;
2022-07-26 11:02:12 -07:00
2023-02-16 15:01:22 -08:00
use crate::cpu::kernel::interpreter::{
run_interpreter_with_memory, InterpreterMemoryInitialization,
};
2023-02-15 18:18:26 -08:00
use crate::memory::segments::Segment::KernelGeneral;
2022-08-18 16:22:43 -07:00
2023-02-15 18:18:26 -08:00
/// Standard Blake2b implementation.
fn blake2b(input: Vec<u8>) -> U512 {
let mut hasher = Blake2b512::new();
2022-10-03 14:08:09 -07:00
hasher.update(input);
2023-02-15 18:18:26 -08:00
U512::from(&hasher.finalize()[..])
2022-10-03 14:07:21 -07:00
}
2022-09-25 20:13:04 -07:00
2022-10-03 15:30:17 -07:00
/// Standard RipeMD implementation.
fn ripemd(input: Vec<u8>) -> U256 {
let mut hasher = Ripemd160::new();
hasher.update(input);
U256::from(&hasher.finalize()[..])
}
2023-02-15 18:18:26 -08:00
/// Standard Sha2 implementation.
fn sha2(input: Vec<u8>) -> U256 {
let mut hasher = Sha256::new();
2022-11-16 14:23:15 -08:00
hasher.update(input);
2023-02-15 18:18:26 -08:00
U256::from(&hasher.finalize()[..])
2022-12-13 10:08:30 -08:00
}
2022-10-05 12:11:10 -07:00
fn make_random_input() -> Vec<u8> {
2022-09-19 10:31:55 -07:00
// Generate a random message, between 0 and 9999 bytes.
2022-10-05 09:45:38 -07:00
let mut rng = thread_rng();
2022-11-30 17:46:11 -08:00
let num_bytes = rng.gen_range(0..10000);
2022-10-05 12:11:10 -07:00
(0..num_bytes).map(|_| rng.gen()).collect()
2022-10-05 09:45:38 -07:00
}
2022-09-19 10:32:52 -07:00
2023-02-15 18:47:33 -08:00
fn make_interpreter_setup(
message: Vec<u8>,
hash_fn_label: &str,
hash_input_virt: (usize, usize),
2023-02-16 15:01:22 -08:00
) -> InterpreterMemoryInitialization {
InterpreterMemoryInitialization {
2023-02-15 18:47:33 -08:00
label: hash_fn_label.to_string(),
stack: vec![
U256::from(hash_input_virt.0),
2023-02-15 18:47:33 -08:00
U256::from(message.len()),
U256::from(0xdeadbeefu32),
],
segment: KernelGeneral,
memory: vec![(
hash_input_virt.1,
2023-02-15 18:47:33 -08:00
message.iter().map(|&x| U256::from(x as u32)).collect(),
)],
}
}
2022-11-30 17:54:54 -08:00
fn combine_u256s(hi: U256, lo: U256) -> U512 {
2023-02-15 18:50:26 -08:00
U512::from(lo) + (U512::from(hi) << 256)
2022-11-30 17:54:54 -08:00
}
fn prepare_test<T>(
2022-11-30 17:47:41 -08:00
hash_fn_label: &str,
hash_input_virt: (usize, usize),
2022-11-30 17:54:54 -08:00
standard_implementation: &dyn Fn(Vec<u8>) -> T,
2023-02-15 18:26:05 -08:00
) -> Result<(T, Vec<U256>)> {
2022-10-05 12:11:10 -07:00
// Make the input.
2023-02-15 18:47:33 -08:00
let message = make_random_input();
2022-11-14 12:33:14 -08:00
2022-10-05 09:45:38 -07:00
// Hash the message using a standard implementation.
2023-02-15 18:47:33 -08:00
let expected = standard_implementation(message.clone());
2023-02-15 18:18:26 -08:00
// Load the message into the kernel.
2023-02-15 18:47:33 -08:00
let interpreter_setup = make_interpreter_setup(message, hash_fn_label, hash_input_virt);
2023-02-15 18:18:26 -08:00
2023-02-15 18:26:05 -08:00
// Run the interpeter
2023-02-16 15:01:22 -08:00
let result = run_interpreter_with_memory(interpreter_setup).unwrap();
2023-02-15 18:26:05 -08:00
2023-02-15 18:47:33 -08:00
Ok((expected, result.stack().to_vec()))
2022-11-30 17:54:54 -08:00
}
fn test_hash_256(
hash_fn_label: &str,
hash_input_virt: (usize, usize),
2022-11-30 17:54:54 -08:00
standard_implementation: &dyn Fn(Vec<u8>) -> U256,
) -> Result<()> {
2023-02-15 18:47:33 -08:00
let (expected, result_stack) =
2023-02-15 18:26:05 -08:00
prepare_test(hash_fn_label, hash_input_virt, standard_implementation).unwrap();
2022-11-30 17:54:54 -08:00
2022-11-29 16:22:23 -08:00
// Extract the final output.
2023-02-15 18:47:33 -08:00
let actual = result_stack[0];
2022-11-29 16:22:23 -08:00
// Check that the result is correct.
2023-02-15 18:47:33 -08:00
assert_eq!(expected, actual);
2022-11-29 16:22:23 -08:00
Ok(())
}
2022-11-30 17:47:41 -08:00
fn test_hash_512(
hash_fn_label: &str,
hash_input_virt: (usize, usize),
2022-11-30 17:47:41 -08:00
standard_implementation: &dyn Fn(Vec<u8>) -> U512,
) -> Result<()> {
2023-02-15 18:47:33 -08:00
let (expected, result_stack) =
2023-02-15 18:26:05 -08:00
prepare_test(hash_fn_label, hash_input_virt, standard_implementation).unwrap();
2022-10-05 10:10:45 -07:00
2022-10-05 12:11:10 -07:00
// Extract the final output.
2023-02-15 18:47:33 -08:00
let actual = combine_u256s(result_stack[0], result_stack[1]);
2022-09-09 12:31:29 -07:00
2022-09-19 10:31:55 -07:00
// Check that the result is correct.
2023-02-15 18:47:33 -08:00
assert_eq!(expected, actual);
2022-08-18 16:21:52 -07:00
2022-07-26 11:02:12 -07:00
Ok(())
}
2022-10-03 14:07:21 -07:00
2023-02-15 19:11:22 -08:00
#[test]
fn test_blake2b() -> Result<()> {
test_hash_512("blake2b", (0,2), &blake2b)
2023-02-15 19:11:22 -08:00
}
2022-10-03 15:30:17 -07:00
2022-11-29 16:22:23 -08:00
#[test]
fn test_ripemd() -> Result<()> {
test_hash_256("ripemd", (200, 200), &ripemd)
2022-11-29 16:22:23 -08:00
}
2022-12-13 10:08:30 -08:00
2023-02-15 19:00:52 -08:00
#[test]
fn test_sha2() -> Result<()> {
test_hash_256("sha2", (0, 1), &sha2)
2023-02-15 19:00:52 -08:00
}