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

281 lines
11 KiB
NASM
Raw Normal View History

2022-12-01 21:42:58 -08:00
global blake2b_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-12-01 21:42:58 -08:00
%blake2b_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-12-01 21:49:06 -08:00
// Store the hash values.
2022-12-01 21:42:58 -08:00
%blake2b_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-12-01 21:49:06 -08:00
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
2022-12-01 21:49:06 -08:00
// Calculate t counter value.
2022-11-29 16:22:23 -08:00
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-12-13 14:42:45 -08:00
%block_size
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-12-13 14:42:45 -08:00
%block_size
2022-11-29 16:22:23 -08:00
%add_const(2)
2022-11-30 17:46:11 -08:00
// stack: cur_block_start_byte, t, cur_block, is_last_block, retdest
2022-12-01 21:49:06 -08:00
// Copy the message from the input space to the message working space.
2022-12-01 21:42:58 -08:00
%blake2b_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-12-12 16:38:40 -08:00
%mload_kernel_general_u64_LE
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-12-01 21:42:58 -08:00
%blake2b_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-12-01 21:49:06 -08:00
// Store the initial 16 values of the internal state.
2022-12-01 21:42:58 -08:00
%blake2b_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-12-02 08:10:18 -08:00
// First eight words of the internal state: current hash value 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-12-02 08:10:18 -08:00
// Next four values of the internal state: first four IV values.
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,...
2022-12-01 21:42:58 -08:00
%blake2b_iv
2022-11-07 14:30:14 -08:00
// 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-12-02 08:10:18 -08:00
// Last four values of the internal state: last four IV values, XOR'd with
// the values (t % 2**64, t >> 64, invert_if, 0).
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,...
2022-12-01 21:42:58 -08:00
%blake2b_iv
2022-11-07 14:30:14 -08:00
// 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-12-01 21:42:58 -08:00
%blake2b_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-12-01 21:49:06 -08:00
// Run 12 rounds of G functions.
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-12-01 21:42:58 -08:00
%call_blake2b_g_function(0, 4, 8, 12, 0, 1)
%call_blake2b_g_function(1, 5, 9, 13, 2, 3)
%call_blake2b_g_function(2, 6, 10, 14, 4, 5)
%call_blake2b_g_function(3, 7, 11, 15, 6, 7)
%call_blake2b_g_function(0, 5, 10, 15, 8, 9)
%call_blake2b_g_function(1, 6, 11, 12, 10, 11)
%call_blake2b_g_function(2, 7, 8, 13, 12, 13)
%call_blake2b_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-12-01 21:49:06 -08:00
// Finalize hash value.
2022-11-30 17:46:11 -08:00
// stack: cur_block, retdest
2022-12-01 21:42:58 -08:00
%blake2b_generate_new_hash_value(7)
%blake2b_generate_new_hash_value(6)
%blake2b_generate_new_hash_value(5)
%blake2b_generate_new_hash_value(4)
%blake2b_generate_new_hash_value(3)
%blake2b_generate_new_hash_value(2)
%blake2b_generate_new_hash_value(1)
%blake2b_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
2022-12-01 21:49:06 -08:00
// Invert the bytes of each hash value.
2022-12-12 17:29:56 -08:00
%reverse_bytes_u64
2022-12-12 16:38:40 -08:00
// stack: h_0'', h_1', h_2', h_3', h_4', h_5', h_6', h_7', cur_block + 1, retdest
2022-11-30 17:46:11 -08:00
SWAP1
2022-12-12 16:38:40 -08:00
// stack: h_1', h_0'', h_2', h_3', h_4', h_5', h_6', h_7', cur_block + 1, retdest
2022-12-12 17:29:56 -08:00
%reverse_bytes_u64
2022-12-12 16:38:40 -08:00
// stack: h_1'', h_0'', h_2', h_3', h_4', h_5', h_6', h_7', cur_block + 1, retdest
2022-11-30 17:46:11 -08:00
SWAP2
2022-12-12 16:38:40 -08:00
// stack: h_2', h_0'', h_1'', h_3', h_4', h_5', h_6', h_7', cur_block + 1, retdest
2022-12-12 17:29:56 -08:00
%reverse_bytes_u64
2022-12-12 16:38:40 -08:00
// stack: h_2'', h_0'', h_1'', h_3', h_4', h_5', h_6', h_7', cur_block + 1, retdest
2022-11-30 17:46:11 -08:00
SWAP3
2022-12-12 16:38:40 -08:00
// stack: h_3', h_0'', h_1'', h_2'', h_4', h_5', h_6', h_7', cur_block + 1, retdest
2022-12-12 17:29:56 -08:00
%reverse_bytes_u64
2022-12-12 16:38:40 -08:00
// stack: h_3'', h_0'', h_1'', h_2'', h_4', h_5', h_6', h_7', cur_block + 1, retdest
2022-11-30 17:46:11 -08:00
SWAP4
2022-12-12 16:38:40 -08:00
// stack: h_4', h_0'', h_1'', h_2'', h_3'', h_5', h_6', h_7', cur_block + 1, retdest
2022-12-12 17:29:56 -08:00
%reverse_bytes_u64
2022-12-12 16:38:40 -08:00
// stack: h_4'', h_0'', h_1'', h_2'', h_3'', h_5', h_6', h_7', cur_block + 1, retdest
2022-11-30 17:46:11 -08:00
SWAP5
2022-12-12 16:38:40 -08:00
// stack: h_5', h_0'', h_1'', h_2'', h_3'', h_4'', h_6', h_7', cur_block + 1, retdest
2022-12-12 17:29:56 -08:00
%reverse_bytes_u64
2022-12-12 16:38:40 -08:00
// stack: h_5'', h_0'', h_1'', h_2'', h_3'', h_4'', h_6', h_7', cur_block + 1, retdest
2022-11-30 17:46:11 -08:00
SWAP6
2022-12-12 16:38:40 -08:00
// stack: h_6', h_0'', h_1'', h_2'', h_3'', h_4'', h_5'', h_7', cur_block + 1, retdest
2022-12-12 17:29:56 -08:00
%reverse_bytes_u64
2022-12-12 16:38:40 -08:00
// stack: h_6'', h_0'', h_1'', h_2'', h_3'', h_4'', h_5'', h_7', cur_block + 1, retdest
2022-11-30 17:46:11 -08:00
SWAP7
2022-12-12 16:38:40 -08:00
// stack: h_7', h_0'', h_1'', h_2'', h_3'', h_4'', h_5'', h_6'', cur_block + 1, retdest
2022-12-12 17:29:56 -08:00
%reverse_bytes_u64
2022-12-12 16:38:40 -08:00
// stack: h_7'', h_0'', h_1'', h_2'', h_3'', h_4'', h_5'', h_6'', cur_block + 1, retdest
%stack (h_7, h_s: 7) -> (h_s, h_7)
// stack: h_0'', h_1'', h_2'', h_3'', h_4'', h_5'', h_6'', h_7'', cur_block + 1, retdest
2022-12-01 21:49:06 -08:00
// Combine hash values.
2022-12-12 17:36:21 -08:00
%u64s_to_u256
2022-12-12 16:38:40 -08:00
// stack: h_0'' || h_1'' || h_2'' || h_3'', h_4'', h_5'', h_6'', h_7'', cur_block + 1, retdest
2022-11-30 17:46:11 -08:00
%stack (first, second: 4, cur) -> (second, first)
2022-12-12 16:38:40 -08:00
// stack: h_4'', h_5'', h_6'', h_7'', h_0'' || h_1'' || h_2'' || h_3'', retdest
2022-12-12 17:36:21 -08:00
%u64s_to_u256
2022-12-12 16:38:40 -08:00
// stack: hash_second = h_4'' || h_5'' || h_6'' || h_7'', hash_first = h_0'' || h_1'' || h_2'' || h_3'', retdest
2022-11-29 16:22:23 -08:00
%stack (second, first, ret) -> (ret, second, first)
2022-11-16 14:23:15 -08:00
// stack: retdest, hash_first, hash_second
2022-12-02 08:10:18 -08:00
JUMP