This commit is contained in:
Nicholas Ward 2022-11-10 15:24:08 -08:00
parent 1367f9bcca
commit 54a2e96473
3 changed files with 74 additions and 23 deletions

View File

@ -45,6 +45,7 @@ pub(crate) fn combined_kernel() -> Kernel {
include_str!("asm/hash/blake/ops.asm"),
include_str!("asm/hash/blake/permutations.asm"),
include_str!("asm/hash/blake/store.asm"),
include_str!("asm/hash/blake/util.asm"),
include_str!("asm/hash/ripemd/box.asm"),
include_str!("asm/hash/ripemd/compression.asm"),
include_str!("asm/hash/ripemd/constants.asm"),

View File

@ -1,4 +1,4 @@
%macro blake_initial_state
%macro blake_initial_hash_value
%blake_iv_i(7)
%blake_iv_i(6)
%blake_iv_i(5)
@ -13,7 +13,7 @@
// stack: IV_0 ^ params, IV_1, IV_2, IV_3, IV_4, IV_5, IV_6, IV_7
%endmacro
%macro blake_internal_state_addr
%macro blake_hash_value_addr
PUSH 0
// stack: 0
%mload_kernel_general
@ -23,30 +23,81 @@
// stack: num_bytes+1
%endmacro
%macro blake_internal_state_addr
%blake_hash_value_addr
%add_const(8)
%endmacro
%macro blake_message_addr
%blake_internal_state_addr
%add_const(16)
%endmacro
global blake_compression:
%blake_initial_state
// stack: t_0, t_1, h_0, h_1, h_2, h_3, h_4, h_5, h_6, h_7
// stack: retdest
%stack () -> (0, 0, 0)
// stack: cur_block = 0, t_0 = 0, t_1 = 0, h_0, h_1, h_2, h_3, h_4, h_5, h_6, h_7
// stack: cur_block = 0, t_0 = 0, t_1 = 0, retdest
// TODO: load %blake_initial_hash_value and store to blake_hash_value_addr
// stack: h_0, ..., h_7, t_0, t_1, f_0, f_1, m_0, ..., m_15
compression_loop:
// stack: cur_block, t_0, t_1, retdest
PUSH 0
%mload_kernel_general
// stack: num_blocks, cur_block, t_0, t_1, retdest
%decrement
// stack: num_blocks - 1, cur_block, t_0, t_1, retdest
DUP2
// stack: cur_block, num_blocks - 1, cur_block, t_0, t_1, retdest
EQ
// stack: is_last_block, cur_block, t_0, t_1, retdest
SWAP1
// stack: cur_block, is_last_block, t_0, t_1, retdest
%mul_const(128)
%increment
// stack: cur_block_start_byte, is_last_block, t_0, t_1, retdest
%blake_message_addr
// stack: message_addr, cur_block_start_byte, is_last_block, t_0, t_1, retdest
%rep 16
// stack: cur_message_addr, cur_block_byte, ...
DUP2
// stack: cur_block_byte, cur_message_addr, cur_block_byte, ...
%mload_blake_word
// stack: m_i, cur_message_addr, cur_block_byte, ...
DUP2
// stack: cur_message_addr, m_i, cur_message_addr, cur_block_byte, ...
%mstore_kernel_general
// stack: cur_message_addr, cur_block_byte, ...
%increment
// stack: cur_message_addr + 1, cur_block_byte, ...
SWAP1
// stack: cur_block_byte, cur_message_addr + 1, ...
%add_const(64)
// stack: cur_block_byte + 64, cur_message_addr + 1, ...
SWAP1
// stack: cur_message_addr + 1, cur_block_byte + 64, ...
%endrep
// stack: end_message_addr, end_block_start_byte, is_last_block, t_0, t_1, retdest
POP
POP
// stack: is_last_block, t_0, t_1, retdest
%mul_const(0xFFFFFFFF)
%stack (l, t0, t1) -> (t0, t1, l, 0)
// stack: t_0, t_1, invert_if_last_block, 0, retdest
// TODO: LOAD from %blake_hash_value_addr
// stack: h_0, ..., h_7, t_0, t_1, invert_if_last_block, 0, retdest
%blake_internal_state_addr
// stack: start, h_0, ..., h_7, t_0, t_1, f_0, f_1, m_0, ..., m_15
// stack: start, h_0, ..., h_7, t_0, t_1, invert_if_last_block, 0, retdest
// First eight words of compression state: current state h_0, ..., h_7.
%rep 8
SWAP1
DUP2
%mstore_kernel_general
%increment
%endrep
// stack: start + 8, t_0, t_1, f_0, f_1, m_0, ..., m_15
// stack: start + 8, t_0, t_1, invert_if_last_block, 0, retdest
PUSH 0
// stack: 0, start + 8, t_0, t_1, f_0, f_1, m_0, ..., m_15
// stack: 0, start + 8, t_0, t_1, invert_if_last_block, 0, retdest
%rep 4
// stack: i, loc, ...
DUP2
@ -82,18 +133,9 @@ global blake_compression:
SWAP1
// stack: i + 1, loc + 1, next_val,...
%endrep
// stack: 8, loc + 16, m_0, ..., m_15
// stack: 8, loc + 16
POP
POP
// stack: m_0, ..., m_15
%blake_message_addr
// stack: addr, m_0, ..., m_15
%rep 16
SWAP1
DUP2
%mstore_kernel_general
%increment
%endrep
// stack: (empty)
%blake_internal_state_addr
// stack: start

View File

@ -101,18 +101,26 @@
// stack: round, y_idx, round, start
%blake_permutation
// stack: s[y_idx], round, start
%blake_message_addr
ADD
%mload_blake_word
// stack: m[s[y_idx]], round, start
PUSH $x_idx
DUP3
// stack: round, 2, s[y_idx], round, start
// stack: round, 2, m[s[y_idx]], round, start
%blake_permutation
// stack: s[x_idx], s[y_idx], round, start
// stack: s[x_idx], m[s[y_idx]], round, start
%blake_message_addr
ADD
%mload_blake_word
// stack: m[s[x_idx]], m[s[y_idx]], round, start
%stack (ss: 2, r, s) -> (ss, s, r, s)
// stack: s[x_idx], s[y_idx], start, round, start
// stack: m[s[x_idx]], m[s[y_idx]], start, round, start
PUSH $d
PUSH $c
PUSH $b
PUSH $a
// stack: a, b, c, d, s[x_idx], s[y_idx], start, round, start
// stack: a, b, c, d, m[s[x_idx]], m[s[y_idx]], start, round, start
%blake_g_function
// stack: round, start
%endmacro