make hash functions take a location pointer

This commit is contained in:
Dmitry Vagner 2023-02-16 16:59:51 -08:00
parent a6ffb4b3c3
commit da7a8879c4
4 changed files with 33 additions and 28 deletions

View File

@ -1,14 +1,15 @@
global blake2b:
// stack: virt, num_bytes, retdest
POP
// stack: num_bytes, retdest
DUP1
// stack: num_bytes, num_bytes, retdest
DUP2
// stack: num_bytes, virt, num_bytes, retdest
%add_const(127)
%div_const(128)
// stack: num_blocks = ceil(num_bytes / 128), num_bytes, retdest
%mstore_kernel_general(0)
// stack: num_bytes, retdest
%mstore_kernel_general(1)
// stack: num_blocks = ceil(num_bytes / 128), virt, num_bytes, retdest
DUP2
// stack: virt, num_blocks, virt, num_bytes, retdest
%mstore_kernel_general
// stack: virt, num_bytes, retdest
%add_const(1)
%mstore_kernel_general
// stack: retdest
%jump(blake2b_compression)

View File

@ -6,13 +6,14 @@
/// STATE, count, _buffer = ripemd_update(STATE, count, _buffer, padlength(len(input)), bytes = [0x80]+[0]*63)
/// STATE, count, _buffer = ripemd_update(STATE, count, _buffer, 8, bytes = size(len(_input)))
/// return process(STATE)
///
/// ripemd is called on
/// // stack: length
///
/// The hardcoded memory structure, where each register is only a byte, is given as follows
/// { 0-63: buffer, 64-71: bytes(8*len(_input)), 72-135: [0x80]+[0]*63 }
///
/// ripemd_update receives and return the stack in the form:
/// stack: STATE, count, length, virt
/// where virt is the virtual address of the bytes argument
///
global ripemd:
// stack: virt, length
@ -52,7 +53,7 @@ global ripemd:
%stack (virt, length) -> ( 0, length, virt, ripemd_1, ripemd_2, process)
// stack: count = 0, length, virt, ripemd_1, ripemd_2, process
%stack () -> (0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0)
// stack: 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, count, length, virt, LABELS
// stack: STATE, count, length, virt, LABELS
%jump(ripemd_update)
ripemd_1:

View File

@ -1,16 +1,19 @@
global sha2:
// stack: virt, length
POP
// stack: length
%mstore_kernel_general(0)
// stack: virt, num_bytes, retdest
SWAP1
// stack: num_bytes, virt, retdest
DUP2
// stack: virt, num_bytes, virt, retdest
%mstore_kernel_general
// stack: virt, retdest
// Precodition: input is in memory, starting at 0 of kernel general segment, of the form
// Precodition: input is in memory, starting at virt of kernel general segment, of the form
// num_bytes, x[0], x[1], ..., x[num_bytes - 1]
// Postcodition: output is in memory, starting at 0, of the form
// num_blocks, block0[0], ..., block0[63], block1[0], ..., blocklast[63]
global sha2_pad:
// stack: retdest
PUSH 0
// stack: virt, retdest
%mload_kernel_general
// stack: num_bytes, retdest
// STEP 1: append 1

View File

@ -41,18 +41,18 @@ fn make_random_input() -> Vec<u8> {
fn make_interpreter_setup(
message: Vec<u8>,
hash_fn_label: &str,
hash_input_virt: usize,
hash_input_virt: (usize, usize),
) -> InterpreterMemoryInitialization {
InterpreterMemoryInitialization {
label: hash_fn_label.to_string(),
stack: vec![
U256::from(hash_input_virt),
U256::from(hash_input_virt.0),
U256::from(message.len()),
U256::from(0xdeadbeefu32),
],
segment: KernelGeneral,
memory: vec![(
hash_input_virt,
hash_input_virt.1,
message.iter().map(|&x| U256::from(x as u32)).collect(),
)],
}
@ -64,7 +64,7 @@ fn combine_u256s(hi: U256, lo: U256) -> U512 {
fn prepare_test<T>(
hash_fn_label: &str,
hash_input_virt: usize,
hash_input_virt: (usize, usize),
standard_implementation: &dyn Fn(Vec<u8>) -> T,
) -> Result<(T, Vec<U256>)> {
// Make the input.
@ -84,7 +84,7 @@ fn prepare_test<T>(
fn test_hash_256(
hash_fn_label: &str,
hash_input_virt: usize,
hash_input_virt: (usize, usize),
standard_implementation: &dyn Fn(Vec<u8>) -> U256,
) -> Result<()> {
let (expected, result_stack) =
@ -101,7 +101,7 @@ fn test_hash_256(
fn test_hash_512(
hash_fn_label: &str,
hash_input_virt: usize,
hash_input_virt: (usize, usize),
standard_implementation: &dyn Fn(Vec<u8>) -> U512,
) -> Result<()> {
let (expected, result_stack) =
@ -118,15 +118,15 @@ fn test_hash_512(
#[test]
fn test_blake2b() -> Result<()> {
test_hash_512("blake2b", 2, &blake2b)
test_hash_512("blake2b", (0,2), &blake2b)
}
#[test]
fn test_ripemd() -> Result<()> {
test_hash_256("ripemd", 200, &ripemd)
test_hash_256("ripemd", (200, 200), &ripemd)
}
#[test]
fn test_sha2() -> Result<()> {
test_hash_256("sha2", 1, &sha2)
test_hash_256("sha2", (0, 1), &sha2)
}