fix arg order for memory version

This commit is contained in:
Dmitry Vagner 2022-10-03 13:13:10 -07:00
parent 4d8f618fd2
commit 2f97ad4416
3 changed files with 32 additions and 31 deletions

View File

@ -1,18 +1,20 @@
/// Variables beginning with _ are in memory
///
/// def ripemd160(_input):
/// state, count, _buffer = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0], 0, [0]*64
/// state, count, _buffer = ripemd_update(state, count, _buffer, len(input) , bytes = _input )
/// 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)
/// STATE, count, _buffer = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0], 0, [0]*64
/// STATE, count, _buffer = ripemd_update(STATE, count, _buffer, len(input) , bytes = _input )
/// 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 a stack with ADDR and length
/// ripemd_update will receive and return the stack in the form:
/// ripemd_stack is called on a stack with length, followed by the input bytes
///
/// 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_alt:
global ripemd_stack:
// stack: length, INPUT
%stack (length) -> (64, length, 0x80, 63, length, length)
// stack: 64, length, 0x80, 63, length, length, INPUT
@ -24,8 +26,8 @@ global ripemd_alt:
global ripemd:
// stack: ADDR, length
%stack (ADDR: 3, length) -> (64, length, 0x80, 63, ADDR, length, length)
// stack: 64, length, 0x80, 63, ADDR, length, length
%stack (ADDR: 3, length) -> (64, length, 0x80, 63, length, ADDR, length)
// stack: 64, length, 0x80, 63, length, ADDR, length
%jump(ripemd_storage) // stores the following into memory
// init _buffer at virt 0 [consumes 64]
// store _size at virt 64 [consumes length]

View File

@ -28,14 +28,15 @@ store_size:
%jump(store_padding)
store_padding:
// stack: i (init 63)
// stack: i [init 63], length
%store_zeros(136, store_padding)
// stack: length
DUP1
%jumpi(store_input_alt)
%jumpi(store_input_stack)
POP
%jump(ripemd_init)
store_input_alt:
store_input_stack:
// stack: rem, length, REM_INP
%stack (rem, length, head) -> (length, rem, 136, head, rem, length)
SUB
@ -46,38 +47,36 @@ store_input_alt:
%sub_const(1)
DUP1
// stack: rem - 1, rem - 1, length, REM_INP
%jumpi(store_input_alt)
%jumpi(store_input_stack)
// stack: 0, length
POP
%jump(ripemd_init)
store_input:
// stack: ADDR , rem , length
DUP3
DUP3
DUP3
// stack: rem , ADDR , length
DUP4
DUP4
DUP4
MLOAD_GENERAL
// stack: byte, ADDR , rem , length
DUP5
// stack: byte, rem , ADDR , length
DUP2
DUP7
SUB
%add_const(136)
// stack: offset, byte, ADDR , rem , length
// stack: offset, byte, rem , ADDR , length
%mstore_kernel_general
// stack: ADDR , rem , length
SWAP2
%add_const(1)
SWAP2
// stack: ADDR + 1, rem , length
SWAP3
// stack: rem , ADDR , length
%sub_const(1)
// stack: rem-1, ADDR , length
SWAP3
// stack: ADDR + 1, rem - 1, length
DUP4
%add_const(1)
SWAP3
// stack: rem-1, ADDR+1, length
DUP2
%jumpi(store_input)
// stack: ADDR , 0 , length
// stack: 0 , ADDR , length
%pop4
// stack: length
// stack: length
%jump(ripemd_init)
%macro store_zeros(N, label)

View File

@ -43,7 +43,7 @@ fn test_ripemd() -> Result<()> {
let expected = U256::from(y);
let kernel = combined_kernel();
let initial_offset = kernel.global_labels["ripemd_alt"];
let initial_offset = kernel.global_labels["ripemd_stack"];
let initial_stack: Vec<U256> = input.iter().map(|&x| U256::from(x as u8)).rev().collect();
let final_stack: Vec<U256> = run_with_kernel(&kernel, initial_offset, initial_stack)?
.stack()