plonky2/evm/src/cpu/kernel/asm/hash/blake/compression.asm

263 lines
9.0 KiB
NASM
Raw Normal View History

2022-11-03 16:01:08 -07:00
global blake_compression:
2022-11-10 15:24:08 -08:00
// stack: retdest
2022-11-29 16:22:23 -08:00
PUSH 0
// stack: cur_block = 0, retdest
2022-11-14 12:33:14 -08:00
%blake_initial_hash_value
2022-12-01 20:09:11 -08:00
compression_loop:
2022-11-29 16:22:23 -08:00
// stack: h_0, ..., h_7, cur_block, retdest
2022-11-14 12:33:14 -08:00
%blake_hash_value_addr
2022-11-29 16:22:23 -08:00
// stack: addr, h_0, ..., h_7, cur_block, retdest
2022-11-14 12:33:14 -08:00
%rep 8
2022-11-15 14:46:45 -08:00
SWAP1
2022-11-14 12:33:14 -08:00
DUP2
%mstore_kernel_general
%increment
%endrep
2022-11-29 16:22:23 -08:00
// stack: addr, cur_block, retdest
2022-11-14 12:33:14 -08:00
POP
2022-11-29 16:22:23 -08:00
// stack: cur_block, retdest
2022-11-10 15:24:08 -08:00
PUSH 0
%mload_kernel_general
2022-11-29 16:22:23 -08:00
// stack: num_blocks, cur_block, retdest
2022-11-10 15:24:08 -08:00
%decrement
2022-11-29 16:22:23 -08:00
// stack: num_blocks - 1, cur_block, retdest
2022-11-10 15:24:08 -08:00
DUP2
2022-11-29 16:22:23 -08:00
// stack: cur_block, num_blocks - 1, cur_block, retdest
2022-11-10 15:24:08 -08:00
EQ
2022-11-29 16:22:23 -08:00
// stack: is_last_block, cur_block, retdest
2022-11-10 15:24:08 -08:00
SWAP1
2022-11-29 16:22:23 -08:00
// stack: cur_block, is_last_block, retdest
PUSH 1
%mload_kernel_general
// stack: num_bytes, cur_block, is_last_block, retdest
DUP3
// stack: is_last_block, num_bytes, cur_block, is_last_block, retdest
MUL
// stack: is_last_block * num_bytes, cur_block, is_last_block, retdest
DUP2
// stack: cur_block, is_last_block * num_bytes, cur_block, is_last_block, retdest
2022-11-30 17:46:11 -08:00
%increment
2022-11-10 15:24:08 -08:00
%mul_const(128)
2022-11-30 17:46:11 -08:00
// stack: (cur_block + 1) * 128, is_last_block * num_bytes, cur_block, is_last_block, retdest
2022-11-29 16:22:23 -08:00
DUP4
2022-11-30 17:46:11 -08:00
// stack: is_last_block, (cur_block + 1) * 128, is_last_block * num_bytes, cur_block, is_last_block, retdest
2022-11-29 16:22:23 -08:00
ISZERO
2022-11-30 17:46:11 -08:00
// stack: not_last_block, (cur_block + 1) * 128, is_last_block * num_bytes, cur_block, is_last_block, retdest
2022-11-29 16:22:23 -08:00
MUL
2022-11-30 17:46:11 -08:00
// stack: not_last_block * ((cur_block + 1) * 128), is_last_block * num_bytes, cur_block, is_last_block, retdest
2022-11-29 16:22:23 -08:00
ADD
2022-11-30 17:46:11 -08:00
// stack: t = not_last_block * ((cur_block + 1) * 128) + is_last_block * num_bytes, cur_block, is_last_block, retdest
2022-11-29 16:22:23 -08:00
SWAP1
// stack: cur_block, t, is_last_block, retdest
2022-11-30 17:46:11 -08:00
DUP1
// stack: cur_block, cur_block, t, is_last_block, retdest
2022-11-29 16:22:23 -08:00
%mul_const(128)
%add_const(2)
2022-11-30 17:46:11 -08:00
// stack: cur_block_start_byte, t, cur_block, is_last_block, retdest
2022-11-10 15:24:08 -08:00
%blake_message_addr
2022-11-30 17:46:11 -08:00
// stack: message_addr, cur_block_start_byte, t, cur_block, is_last_block, retdest
2022-11-10 15:24:08 -08:00
%rep 16
// stack: cur_message_addr, cur_block_byte, ...
DUP2
// stack: cur_block_byte, cur_message_addr, cur_block_byte, ...
2022-11-29 16:12:58 -08:00
%mload_blake_word_from_bytes
2022-11-10 15:24:08 -08:00
// 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, ...
2022-11-29 16:22:23 -08:00
%add_const(8)
// stack: cur_block_byte + 8, cur_message_addr + 1, ...
2022-11-10 15:24:08 -08:00
SWAP1
2022-11-29 16:22:23 -08:00
// stack: cur_message_addr + 1, cur_block_byte + 8, ...
2022-11-10 15:24:08 -08:00
%endrep
2022-11-30 17:46:11 -08:00
// stack: end_message_addr, end_block_start_byte, t, cur_block, is_last_block, retdest
2022-11-10 15:24:08 -08:00
POP
POP
2022-11-30 17:46:11 -08:00
// stack: t, cur_block, is_last_block, retdest
2022-11-29 16:22:23 -08:00
SWAP1
2022-11-30 17:46:11 -08:00
// stack: cur_block, t, is_last_block, retdest
SWAP2
// stack: is_last_block, t, cur_block, retdest
2022-11-16 14:23:15 -08:00
%mul_const(0xFFFFFFFFFFFFFFFF)
2022-11-30 17:46:11 -08:00
// stack: invert_if_last_block, t, cur_block, retdest
2022-11-14 12:33:14 -08:00
%blake_hash_value_addr
2022-11-16 14:23:15 -08:00
%add_const(7)
2022-11-14 12:33:14 -08:00
%rep 8
// stack: addr, ...
DUP1
// stack: addr, addr, ...
%mload_kernel_general
// stack: val, addr, ...
SWAP1
// stack: addr, val, ...
2022-11-16 14:23:15 -08:00
%decrement
2022-11-14 12:33:14 -08:00
%endrep
2022-11-30 17:46:11 -08:00
// stack: addr, h_0, ..., h_7, invert_if_last_block, t, cur_block, retdest
2022-11-15 14:46:45 -08:00
POP
2022-11-30 17:46:11 -08:00
// stack: h_0, ..., h_7, invert_if_last_block, t, cur_block, retdest
2022-11-09 13:48:56 -08:00
%blake_internal_state_addr
2022-11-30 17:46:11 -08:00
// stack: start, h_0, ..., h_7, invert_if_last_block, t, cur_block, retdest
2022-11-10 15:24:08 -08:00
// First eight words of compression state: current state h_0, ..., h_7.
2022-11-04 15:27:34 -07:00
%rep 8
SWAP1
DUP2
%mstore_kernel_general
%increment
%endrep
2022-11-30 17:46:11 -08:00
// stack: start + 8, invert_if_last_block, t, cur_block, retdest
2022-11-04 15:27:34 -07:00
PUSH 0
2022-11-30 17:46:11 -08:00
// stack: 0, start + 8, invert_if_last_block, t, cur_block, retdest
2022-11-04 15:27:34 -07:00
%rep 4
2022-11-07 14:30:14 -08:00
// stack: i, loc, ...
DUP2
DUP2
// stack: i, loc, i, loc,...
%blake_iv
// stack: IV_i, loc, i, loc,...
SWAP1
// stack: loc, IV_i, i, loc,...
%mstore_kernel_general
// stack: i, loc,...
%increment
SWAP1
%increment
SWAP1
// stack: i + 1, loc + 1,...
%endrep
2022-11-30 17:46:11 -08:00
// stack: 4, start + 12, invert_if_last_block, t, cur_block, retdest
%stack (i, loc, inv, last, t) -> (t, t, i, loc, inv, last)
// stack: t, t, 4, start + 12, invert_if_last_block, cur_block, retdest
2022-11-29 16:22:23 -08:00
%shr_const(64)
2022-11-30 17:46:11 -08:00
// stack: t >> 64, t, 4, start + 12, invert_if_last_block, cur_block, retdest
2022-11-29 16:22:23 -08:00
SWAP1
2022-11-30 17:46:11 -08:00
// stack: t, t >> 64, 4, start + 12, invert_if_last_block, cur_block, retdest
2022-11-29 16:22:23 -08:00
PUSH 1
%shl_const(64)
2022-11-30 17:46:11 -08:00
// stack: 1 << 64, t, t >> 64, 4, start + 12, invert_if_last_block, cur_block, retdest
2022-11-29 16:22:23 -08:00
SWAP1
MOD
2022-11-30 17:46:11 -08:00
// stack: t_lo = t % (1 << 64), t_hi = t >> 64, 4, start + 12, invert_if_last_block, cur_block, retdest
2022-11-29 16:22:23 -08:00
%stack (t_lo, t_hi, i, loc, inv) -> (i, loc, t_lo, t_hi, inv, 0)
2022-11-30 17:46:11 -08:00
// stack: 4, start + 12, t_lo, t_hi, invert_if_last_block, 0, cur_block, retdest
2022-11-29 16:22:23 -08:00
// XOR the values (t % 2**64, t >> 64, invert_if, 0) into the last four IV values.
2022-11-07 14:30:14 -08:00
%rep 4
2022-11-15 14:46:45 -08:00
// stack: i, loc, val, next_val,...
2022-11-07 14:30:14 -08:00
%stack (i, loc, val) -> (i, val, loc, i, loc)
// stack: i, val, loc, i, loc, next_val,...
%blake_iv
// stack: IV_i, val, loc, i, loc, next_val,...
XOR
// stack: val ^ IV_i, loc, i, loc, next_val,...
SWAP1
// stack: loc, val ^ IV_i, i, loc, next_val,...
%mstore_kernel_general
// stack: i, loc, next_val,...
%increment
SWAP1
%increment
SWAP1
// stack: i + 1, loc + 1, next_val,...
%endrep
2022-11-30 17:46:11 -08:00
// stack: 8, loc + 16, cur_block, retdest
2022-11-07 14:30:14 -08:00
POP
POP
2022-11-30 17:46:11 -08:00
// stack: cur_block, retdest
2022-11-09 13:48:56 -08:00
%blake_internal_state_addr
2022-11-30 17:46:11 -08:00
// stack: start, cur_block, retdest
2022-11-07 14:30:14 -08:00
PUSH 0
2022-11-30 17:46:11 -08:00
// stack: round=0, start, cur_block, retdest
2022-11-29 16:12:58 -08:00
%rep 12
2022-11-30 17:46:11 -08:00
// stack: round, start, cur_block, retdest
2022-11-07 15:47:17 -08:00
%call_blake_g_function(0, 4, 8, 12, 0, 1)
2022-11-29 16:12:58 -08:00
%call_blake_g_function(1, 5, 9, 13, 2, 3)
%call_blake_g_function(2, 6, 10, 14, 4, 5)
%call_blake_g_function(3, 7, 11, 15, 6, 7)
%call_blake_g_function(0, 5, 10, 15, 8, 9)
%call_blake_g_function(1, 6, 11, 12, 10, 11)
%call_blake_g_function(2, 7, 8, 13, 12, 13)
%call_blake_g_function(3, 4, 9, 14, 14, 15)
2022-11-30 17:46:11 -08:00
// stack: round, start, cur_block, retdest
2022-11-07 15:47:17 -08:00
%increment
2022-11-30 17:46:11 -08:00
// stack: round + 1, start, cur_block, retdest
2022-11-07 15:47:17 -08:00
%endrep
2022-11-30 17:46:11 -08:00
// stack: 12, start, cur_block, retdest
2022-11-15 14:46:45 -08:00
POP
POP
2022-11-30 17:46:11 -08:00
// stack: cur_block, retdest
2022-11-15 14:46:45 -08:00
%blake_generate_new_hash_value(7)
%blake_generate_new_hash_value(6)
%blake_generate_new_hash_value(5)
%blake_generate_new_hash_value(4)
%blake_generate_new_hash_value(3)
%blake_generate_new_hash_value(2)
%blake_generate_new_hash_value(1)
%blake_generate_new_hash_value(0)
2022-11-30 17:46:11 -08:00
// stack: h_0', h_1', h_2', h_3', h_4', h_5', h_6', h_7', cur_block, retdest
DUP9
// stack: cur_block, h_0', h_1', h_2', h_3', h_4', h_5', h_6', h_7', cur_block, retdest
%increment
// stack: cur_block + 1, h_0', h_1', h_2', h_3', h_4', h_5', h_6', h_7', cur_block, retdest
SWAP9
// stack: cur_block, h_0', h_1', h_2', h_3', h_4', h_5', h_6', h_7', cur_block + 1, retdest
%increment
// stack: cur_block + 1, h_0', h_1', h_2', h_3', h_4', h_5', h_6', h_7', cur_block + 1, retdest
PUSH 0
%mload_kernel_general
// stack: num_blocks, cur_block + 1, h_0', h_1', h_2', h_3', h_4', h_5', h_6', h_7', cur_block + 1, retdest
EQ
// stack: last_block, h_0', h_1', h_2', h_3', h_4', h_5', h_6', h_7', cur_block + 1, retdest
2022-12-01 20:09:11 -08:00
%jumpi(compression_end)
%jump(compression_loop)
compression_end:
2022-11-30 17:46:11 -08:00
// stack: h_0', h_1', h_2', h_3', h_4', h_5', h_6', h_7', cur_block + 1, retdest
PUSH 0
// stack: dummy=0, h_0', h_1', h_2', h_3', h_4', h_5', h_6', h_7', cur_block + 1, retdest
SWAP1
%invert_bytes_blake_word
SWAP1
SWAP2
2022-11-29 16:22:23 -08:00
%invert_bytes_blake_word
2022-11-30 17:46:11 -08:00
SWAP2
SWAP3
%invert_bytes_blake_word
SWAP3
SWAP4
%invert_bytes_blake_word
SWAP4
SWAP5
%invert_bytes_blake_word
SWAP5
SWAP6
%invert_bytes_blake_word
SWAP6
SWAP7
%invert_bytes_blake_word
SWAP7
SWAP8
%invert_bytes_blake_word
SWAP8
POP
2022-11-16 14:23:15 -08:00
%shl_const(64)
2022-11-15 15:40:56 -08:00
OR
2022-11-16 14:23:15 -08:00
%shl_const(64)
2022-11-15 15:40:56 -08:00
OR
2022-11-16 14:23:15 -08:00
%shl_const(64)
2022-11-15 15:40:56 -08:00
OR
2022-11-30 17:46:11 -08:00
// stack: h_0' || h_1' || h_2' || h_3', h_4', h_5', h_6', h_7', cur_block + 1, retdest
%stack (first, second: 4, cur) -> (second, first)
2022-11-16 14:23:15 -08:00
// stack: h_4', h_5', h_6', h_7', h_0' || h_1' || h_2' || h_3', retdest
%shl_const(64)
2022-11-15 15:40:56 -08:00
OR
2022-11-16 14:23:15 -08:00
%shl_const(64)
2022-11-15 15:40:56 -08:00
OR
2022-11-16 14:23:15 -08:00
%shl_const(64)
2022-11-15 15:40:56 -08:00
OR
2022-11-29 16:22:23 -08:00
// stack: hash_second = h_4' || h_5' || h_6' || h_7', hash_first = h_0' || h_1' || h_2' || h_3', retdest
%stack (second, first, ret) -> (ret, second, first)
2022-11-16 14:23:15 -08:00
// stack: retdest, hash_first, hash_second
2022-11-15 15:36:15 -08:00
JUMP