Merge pull request #759 from mir-protocol/ripeFIX

Fix RipeMD padlength issue
This commit is contained in:
Dima V 2022-10-05 12:21:39 -07:00 committed by GitHub
commit 7ccc673368
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 19 deletions

View File

@ -88,20 +88,20 @@ global process:
/// def padlength(length): /// def padlength(length):
/// t = length % 64 /// t = length % 64
/// return 56 + 64*(t > 47) - t /// return 56 + 64*(t > 55) - t
%macro padlength %macro padlength
// stack: count // stack: count
%mod_const(64) %mod_const(64)
// stack: t = count % 64 // stack: t = count % 64
PUSH 47 PUSH 55
DUP2 DUP2
// stack: t , 47 , t // stack: t , 55 , t
GT GT
// stack: t > 47 , t // stack: t > 55 , t
%mul_const(64) %mul_const(64)
%add_const(56) %add_const(56)
// stack: 56 + 64*(t > 47), t // stack: 56 + 64*(t > 55), t
SUB SUB
%endmacro %endmacro

View File

@ -23,31 +23,59 @@ fn ripemd(input: Vec<u8>) -> U256 {
U256::from(&hasher.finalize()[..]) U256::from(&hasher.finalize()[..])
} }
fn test_hash(hash_fn_label: &str, standard_implementation: &dyn Fn(Vec<u8>) -> U256) -> Result<()> { fn make_random_input() -> Vec<u8> {
let kernel = combined_kernel();
let mut rng = thread_rng();
// Generate a random message, between 0 and 9999 bytes. // Generate a random message, between 0 and 9999 bytes.
let mut rng = thread_rng();
let num_bytes = rng.gen_range(0..10000); let num_bytes = rng.gen_range(0..10000);
let message: Vec<u8> = (0..num_bytes).map(|_| rng.gen()).collect(); (0..num_bytes).map(|_| rng.gen()).collect()
}
// Hash the message using a standard implementation. fn make_custom_input() -> Vec<u8> {
let expected = standard_implementation(message.clone()); // Hardcode a custom message
vec![
86, 124, 206, 245, 74, 57, 250, 43, 60, 30, 254, 43, 143, 144, 242, 215, 13, 103, 237, 61,
90, 105, 123, 250, 189, 181, 110, 192, 227, 57, 145, 46, 221, 238, 7, 181, 146, 111, 209,
150, 31, 157, 229, 126, 206, 105, 37, 17,
]
}
// Load the message onto the stack. fn make_input_stack(message: Vec<u8>) -> Vec<U256> {
let mut initial_stack = vec![U256::from(num_bytes)]; let mut initial_stack = vec![U256::from(message.len())];
let bytes: Vec<U256> = message.iter().map(|&x| U256::from(x as u32)).collect(); let bytes: Vec<U256> = message.iter().map(|&x| U256::from(x as u32)).collect();
initial_stack.extend(bytes); initial_stack.extend(bytes);
initial_stack.push(U256::from_str("0xdeadbeef").unwrap()); initial_stack.push(U256::from_str("0xdeadbeef").unwrap());
initial_stack.reverse(); initial_stack.reverse();
initial_stack
}
fn test_hash(hash_fn_label: &str, standard_implementation: &dyn Fn(Vec<u8>) -> U256) -> Result<()> {
// Make the input.
let message_random = make_random_input();
let message_custom = make_custom_input();
// Hash the message using a standard implementation.
let expected_random = standard_implementation(message_random.clone());
let expected_custom = standard_implementation(message_custom.clone());
// Load the message onto the stack.
let initial_stack_random = make_input_stack(message_random);
let initial_stack_custom = make_input_stack(message_custom);
// Make the kernel.
let kernel = combined_kernel();
let kernel_function = kernel.global_labels[hash_fn_label];
// Run the kernel code. // Run the kernel code.
let kernel_function = kernel.global_labels[hash_fn_label]; let result_random = run_with_kernel(&kernel, kernel_function, initial_stack_random)?;
let result = run_with_kernel(&kernel, kernel_function, initial_stack)?; let result_custom = run_with_kernel(&kernel, kernel_function, initial_stack_custom)?;
let actual = result.stack()[0];
// Extract the final output.
let actual_random = result_random.stack()[0];
let actual_custom = result_custom.stack()[0];
// Check that the result is correct. // Check that the result is correct.
assert_eq!(expected, actual); assert_eq!(expected_random, actual_random);
assert_eq!(expected_custom, actual_custom);
Ok(()) Ok(())
} }