2022-07-26 11:02:12 -07:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-11-04 13:55:13 +01:00
|
|
|
use crate::cpu::kernel::aggregator::KERNEL;
|
|
|
|
|
use crate::cpu::kernel::interpreter::run_interpreter;
|
2022-08-18 16:22:43 -07:00
|
|
|
|
2022-10-03 14:07:21 -07:00
|
|
|
/// Standard Sha2 implementation.
|
|
|
|
|
fn sha2(input: Vec<u8>) -> U256 {
|
|
|
|
|
let mut hasher = Sha256::new();
|
2022-10-03 14:08:09 -07:00
|
|
|
hasher.update(input);
|
2022-10-03 14:07:21 -07:00
|
|
|
U256::from(&hasher.finalize()[..])
|
|
|
|
|
}
|
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()[..])
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-16 14:23:15 -08:00
|
|
|
/// Standard Blake2b implementation.
|
|
|
|
|
fn blake2b(input: Vec<u8>) -> U512 {
|
|
|
|
|
let mut hasher = Blake2b512::new();
|
|
|
|
|
hasher.update(input);
|
|
|
|
|
U512::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-15 14:46:45 -08:00
|
|
|
let num_bytes = rng.gen_range(0..25);
|
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
|
|
|
|
2022-10-05 12:11:10 -07:00
|
|
|
fn make_custom_input() -> Vec<u8> {
|
2022-10-05 09:45:38 -07:00
|
|
|
// Hardcode a custom message
|
2022-10-05 12:11:10 -07:00
|
|
|
vec![
|
2022-11-16 14:23:15 -08:00
|
|
|
1, 2, 3
|
2022-10-05 12:11:10 -07:00
|
|
|
]
|
2022-10-05 09:45:38 -07:00
|
|
|
}
|
2022-09-19 10:32:52 -07:00
|
|
|
|
2022-10-05 12:11:10 -07:00
|
|
|
fn make_input_stack(message: Vec<u8>) -> Vec<U256> {
|
|
|
|
|
let mut initial_stack = vec![U256::from(message.len())];
|
2022-09-19 10:31:55 -07:00
|
|
|
let bytes: Vec<U256> = message.iter().map(|&x| U256::from(x as u32)).collect();
|
2022-08-23 15:27:20 -07:00
|
|
|
initial_stack.extend(bytes);
|
|
|
|
|
initial_stack.push(U256::from_str("0xdeadbeef").unwrap());
|
|
|
|
|
initial_stack.reverse();
|
2022-10-05 09:45:38 -07:00
|
|
|
initial_stack
|
|
|
|
|
}
|
2022-08-08 11:37:35 -07:00
|
|
|
|
2022-11-16 14:23:15 -08:00
|
|
|
fn test_hash(hash_fn_label: &str, standard_implementation: &dyn Fn(Vec<u8>) -> U512) -> Result<()> {
|
2022-10-05 12:11:10 -07:00
|
|
|
// Make the input.
|
2022-11-16 14:23:15 -08:00
|
|
|
// let message_random = make_random_input();
|
2022-11-15 15:36:15 -08:00
|
|
|
let message_custom = make_custom_input();
|
2022-11-14 12:33:14 -08:00
|
|
|
|
2022-11-16 14:23:15 -08:00
|
|
|
// dbg!(message_random.clone());
|
2022-10-05 09:45:38 -07:00
|
|
|
|
|
|
|
|
// Hash the message using a standard implementation.
|
2022-11-16 14:23:15 -08:00
|
|
|
// // let expected_random = standard_implementation(message_random.clone());
|
2022-11-15 15:36:15 -08:00
|
|
|
let expected_custom = standard_implementation(message_custom.clone());
|
2022-10-05 09:45:38 -07:00
|
|
|
|
2022-11-16 14:23:15 -08:00
|
|
|
dbg!(expected_custom);
|
|
|
|
|
|
2022-10-05 09:45:38 -07:00
|
|
|
// Load the message onto the stack.
|
2022-11-16 14:23:15 -08:00
|
|
|
// // let initial_stack_random = make_input_stack(message_random);
|
2022-11-15 15:36:15 -08:00
|
|
|
let initial_stack_custom = make_input_stack(message_custom);
|
2022-11-14 12:33:14 -08:00
|
|
|
|
2022-11-16 14:23:15 -08:00
|
|
|
// dbg!(initial_stack_random.clone());
|
2022-10-05 09:45:38 -07:00
|
|
|
|
|
|
|
|
// Make the kernel.
|
2022-11-04 13:55:13 +01:00
|
|
|
let kernel_function = KERNEL.global_labels[hash_fn_label];
|
2022-10-05 09:45:38 -07:00
|
|
|
|
|
|
|
|
// Run the kernel code.
|
2022-11-16 14:23:15 -08:00
|
|
|
// // let result_random = run_interpreter(kernel_function, initial_stack_random)?;
|
2022-11-15 15:36:15 -08:00
|
|
|
let result_custom = run_interpreter(kernel_function, initial_stack_custom)?;
|
2022-11-14 12:33:14 -08:00
|
|
|
|
2022-11-16 14:23:15 -08:00
|
|
|
dbg!(result_custom.stack());
|
2022-10-05 10:10:45 -07:00
|
|
|
|
2022-10-05 12:11:10 -07:00
|
|
|
// Extract the final output.
|
2022-11-16 14:23:15 -08:00
|
|
|
// let actual_random = result_random.stack()[0];
|
|
|
|
|
let actual_custom_first = result_custom.stack()[0];
|
|
|
|
|
let actual_custom_second = result_custom.stack()[1];
|
|
|
|
|
let mut actual_custom = U512::from(actual_custom_first);
|
|
|
|
|
actual_custom *= U512::from_big_endian(&[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
|
|
|
|
|
actual_custom += U512::from(actual_custom_second);
|
|
|
|
|
|
|
|
|
|
dbg!(actual_custom);
|
2022-09-09 12:31:29 -07:00
|
|
|
|
2022-09-19 10:31:55 -07:00
|
|
|
// Check that the result is correct.
|
2022-11-14 12:33:14 -08:00
|
|
|
// assert_eq!(expected_random, actual_random);
|
2022-11-16 14:23:15 -08:00
|
|
|
// assert_eq!(expected_custom, actual_custom);
|
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
|
|
|
|
2022-11-16 14:23:15 -08:00
|
|
|
// #[test]
|
|
|
|
|
// fn test_sha2() -> Result<()> {
|
|
|
|
|
// test_hash("sha2", &sha2)
|
|
|
|
|
// }
|
2022-10-03 15:30:17 -07:00
|
|
|
|
2022-11-16 14:23:15 -08:00
|
|
|
// #[test]
|
|
|
|
|
// fn test_ripemd() -> Result<()> {
|
|
|
|
|
// test_hash("ripemd_stack", &ripemd)
|
|
|
|
|
// }
|
2022-12-13 10:08:30 -08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_blake() -> Result<()> {
|
2022-11-16 14:23:15 -08:00
|
|
|
test_hash("blake", &blake2b)
|
2022-12-13 10:08:30 -08:00
|
|
|
}
|