2022-09-18 11:05:00 -07:00
|
|
|
/// Variables beginning with _ are in memory
|
2022-09-18 10:20:25 -07:00
|
|
|
///
|
|
|
|
|
/// def ripemd160(_input):
|
|
|
|
|
/// state, count, _buffer = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0], 0, [0]*64
|
2022-09-19 12:09:57 -07:00
|
|
|
/// 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)))
|
2022-09-18 10:20:25 -07:00
|
|
|
/// return process(state)
|
2022-09-19 12:09:57 -07:00
|
|
|
///
|
2022-09-18 11:05:00 -07:00
|
|
|
/// ripemd is called on a stack with ADDR and length
|
|
|
|
|
/// ripemd_update will receive and return the stack in the form:
|
2022-09-19 18:11:07 -07:00
|
|
|
/// stack: STATE, count, length, virt
|
|
|
|
|
/// where virt is the virtual address of the bytes argument
|
2022-09-18 11:05:00 -07:00
|
|
|
|
2022-09-19 19:04:22 -07:00
|
|
|
global ripemd_alt:
|
|
|
|
|
// stack: length, INPUT
|
|
|
|
|
%stack (length) -> (64, length, 0x80, 63, length, length)
|
|
|
|
|
// stack: 64, length, 0x80, 63, length, length, INPUT
|
|
|
|
|
%jump(ripemd_storage) // stores the following into memory
|
|
|
|
|
// init _buffer at virt 0 [consumes 64]
|
|
|
|
|
// store _size at virt 64 [consumes length]
|
|
|
|
|
// store _padding at virt 72 [consumes 0x80, 63]
|
|
|
|
|
// store _input at virt 136 [consumes length]
|
|
|
|
|
|
2022-09-18 10:20:25 -07:00
|
|
|
global ripemd:
|
2022-09-18 11:05:00 -07:00
|
|
|
// stack: ADDR, length
|
2022-09-19 19:04:22 -07:00
|
|
|
%stack (a, b, c, length) -> (64, length, 0x80, 63, a, b, c, length, length)
|
2022-09-19 12:09:57 -07:00
|
|
|
// stack: 64, length, 0x80, 63, a, b, c, length, length
|
|
|
|
|
%jump(ripemd_storage) // stores the following into memory
|
2022-09-19 18:11:07 -07:00
|
|
|
// init _buffer at virt 0 [consumes 64]
|
|
|
|
|
// store _size at virt 64 [consumes length]
|
|
|
|
|
// store _padding at virt 72 [consumes 0x80, 63]
|
|
|
|
|
// store _input at virt 136 [consumes ADDR, length]
|
2022-09-20 10:29:43 -07:00
|
|
|
global ripemd_init:
|
2022-09-18 10:20:25 -07:00
|
|
|
// stack: length
|
2022-09-19 18:11:07 -07:00
|
|
|
%stack (length) -> ( 0, length, 136, ripemd_1, ripemd_2, process)
|
|
|
|
|
// stack: count = 0, length, virt = 136, ripemd_1, ripemd_2, process
|
2022-09-20 10:11:45 -07:00
|
|
|
%stack (c, l, o, l1, l2, l3) -> (0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, c, l, o, l1, l2, l3)
|
2022-09-19 18:11:07 -07:00
|
|
|
// stack: 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, count, length, virt, *labels
|
2022-09-18 10:20:25 -07:00
|
|
|
%jump(ripemd_update)
|
|
|
|
|
ripemd_1:
|
2022-09-19 18:11:07 -07:00
|
|
|
// stack: STATE, count, length , virt, *labels
|
2022-09-18 11:05:00 -07:00
|
|
|
DUP7
|
2022-09-19 18:11:07 -07:00
|
|
|
// stack: length, STATE, count, length , virt, *labels
|
2022-09-18 10:20:25 -07:00
|
|
|
%padlength
|
2022-09-19 18:11:07 -07:00
|
|
|
// stack: padlength, STATE, count, length , virt, *labels
|
2022-09-18 10:20:25 -07:00
|
|
|
SWAP7
|
|
|
|
|
POP
|
2022-09-19 18:11:07 -07:00
|
|
|
// stack: STATE, count, length = padlength, virt, *labels
|
|
|
|
|
%stack (a, b, c, d, e, count, length, virt) -> (a, b, c, d, e, count, length, 72)
|
2022-09-18 10:20:25 -07:00
|
|
|
%jump(ripemd_update)
|
|
|
|
|
ripemd_2:
|
2022-09-19 18:11:07 -07:00
|
|
|
// stack: STATE, count, length, virt, *labels
|
|
|
|
|
%stack (a, b, c, d, e, count, length, virt) -> (a, b, c, d, e, count, 8, 64)
|
|
|
|
|
// stack: STATE, count, length, virt, *labels
|
2022-09-18 10:20:25 -07:00
|
|
|
%jump(ripemd_update)
|
2022-09-16 10:12:21 -07:00
|
|
|
process:
|
2022-09-19 18:11:07 -07:00
|
|
|
// stack: a , b, c, d, e, count, length, virt
|
2022-09-16 10:12:21 -07:00
|
|
|
%flip_bytes_u32
|
2022-09-18 10:20:25 -07:00
|
|
|
// stack: a', b, c, d, e, *vars
|
2022-09-16 10:12:21 -07:00
|
|
|
SWAP1
|
2022-09-20 10:11:45 -07:00
|
|
|
%flip_bytes_u32
|
2022-09-16 10:12:21 -07:00
|
|
|
%shl_const(32)
|
|
|
|
|
OR
|
2022-09-18 10:20:25 -07:00
|
|
|
// stack: b' a', c, d, e, *vars
|
2022-09-16 10:12:21 -07:00
|
|
|
SWAP1
|
2022-09-20 10:11:45 -07:00
|
|
|
%flip_bytes_u32
|
2022-09-16 10:12:21 -07:00
|
|
|
%shl_const(64)
|
|
|
|
|
OR
|
2022-09-18 10:20:25 -07:00
|
|
|
// stack: c' b' a', d, e, *vars
|
2022-09-16 10:12:21 -07:00
|
|
|
SWAP1
|
2022-09-20 10:11:45 -07:00
|
|
|
%flip_bytes_u32
|
2022-09-16 10:12:21 -07:00
|
|
|
%shl_const(96)
|
|
|
|
|
OR
|
2022-09-18 10:20:25 -07:00
|
|
|
// stack: d' c' b' a', e, *vars
|
2022-09-16 10:12:21 -07:00
|
|
|
SWAP1
|
2022-09-20 10:11:45 -07:00
|
|
|
%flip_bytes_u32
|
2022-09-16 10:12:21 -07:00
|
|
|
%shl_const(96)
|
|
|
|
|
OR
|
2022-09-18 10:20:25 -07:00
|
|
|
// stack: e' d' c' b' a', *vars
|
2022-09-20 10:11:45 -07:00
|
|
|
%stack (result, x, y, z) -> (result)
|
2022-09-18 10:20:25 -07:00
|
|
|
// stack: result
|
2022-09-16 19:18:26 -07:00
|
|
|
|
|
|
|
|
|
2022-09-18 11:05:00 -07:00
|
|
|
/// def padlength(length):
|
|
|
|
|
/// x = 56 - length % 64
|
2022-09-16 19:18:26 -07:00
|
|
|
/// return x + 64*(x < 9)
|
|
|
|
|
|
|
|
|
|
%macro padlength
|
2022-09-18 11:05:00 -07:00
|
|
|
// stack: count
|
2022-09-16 19:18:26 -07:00
|
|
|
%mod_const(64)
|
2022-09-18 11:05:00 -07:00
|
|
|
// stack: count % 64
|
2022-09-16 19:18:26 -07:00
|
|
|
PUSH 56
|
|
|
|
|
SUB
|
2022-09-18 11:05:00 -07:00
|
|
|
// stack: x = 56 - count % 64
|
2022-09-16 19:18:26 -07:00
|
|
|
DUP1
|
|
|
|
|
%lt_const(9)
|
|
|
|
|
// stack: x < 9 , x
|
|
|
|
|
%mul_const(64)
|
|
|
|
|
// stack: 64*(x < 9) , x
|
|
|
|
|
ADD
|
|
|
|
|
// stack: 64*(x < 9) + x
|
2022-09-18 10:20:25 -07:00
|
|
|
%endmacro
|