From 0c919443f991f9aa001d75489bdd36f14d7ad7d5 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Mon, 7 Nov 2022 15:47:17 -0800 Subject: [PATCH] progress --- .../cpu/kernel/asm/hash/blake/compression.asm | 48 +++++++--- .../cpu/kernel/asm/hash/blake/g_functions.asm | 26 +++++- .../cpu/kernel/asm/hash/blake/store_pad.asm | 89 ------------------- 3 files changed, 59 insertions(+), 104 deletions(-) delete mode 100644 evm/src/cpu/kernel/asm/hash/blake/store_pad.asm diff --git a/evm/src/cpu/kernel/asm/hash/blake/compression.asm b/evm/src/cpu/kernel/asm/hash/blake/compression.asm index e805f923..c00c7554 100644 --- a/evm/src/cpu/kernel/asm/hash/blake/compression.asm +++ b/evm/src/cpu/kernel/asm/hash/blake/compression.asm @@ -1,9 +1,18 @@ -%macro blake_compression_internal_state_addr +// We put the message schedule in memory starting at 64 * num_blocks + 2. +%macro message_schedule_addr_from_num_blocks + +%macro blake_internal_state_addr PUSH 0 + // stack: 0 + %mload_kernel_general + // stack: num_blocks + %mul_const(128) + // stack: num_bytes %endmacro -%macro blake_compression_message_addr - PUSH 16 +%macro blake_message_addr + %blake_internal_state_addr + %add_const(16) %endmacro global blake_compression: @@ -61,17 +70,28 @@ global blake_compression: %blake_compression_message_addr // stack: addr, m_0, ..., m_15 %rep 16 - + SWAP1 + DUP2 + %mstore_kernel_general + %increment %endrep + // stack: (empty) + %blake_compression_internal_state_addr + // stack: start PUSH 0 - // stack: round=0, m_0, ..., m_15 -compression_loop: - // stack: round, m_0, ..., m_15 - PUSH 0 - DUP2 - // stack: round, 0, round, m_0, ..., m_15 - %blake_permutation - // stack: s[0], round, m_0, ..., m_15 + // stack: round=0, start + %rep 12 + // stack: round, start + %call_blake_g_function(0, 4, 8, 12, 0, 1) + %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) + // stack: round, start + %increment + // stack: round + 1, start + %endrep - - diff --git a/evm/src/cpu/kernel/asm/hash/blake/g_functions.asm b/evm/src/cpu/kernel/asm/hash/blake/g_functions.asm index 67cf6740..5b3943bb 100644 --- a/evm/src/cpu/kernel/asm/hash/blake/g_functions.asm +++ b/evm/src/cpu/kernel/asm/hash/blake/g_functions.asm @@ -1,4 +1,4 @@ -global blake_g_function: +%macro blake_g_function // Function to mix two input words, x and y, into the four words indexed by a, b, c, d (which // are in the range 0..16) in the internal state. // The internal state is stored in memory starting at the address start. @@ -92,3 +92,27 @@ global blake_g_function: %mstore_kernel_general ADD %mstore_kernel_general +%endmacro + +%macro call_blake_g_function(a, b, c, d, x_idx, y_idx) + // stack: round, start + PUSH $y_idx + DUP2 + // stack: round, y_idx, round, start + %blake_permutation + // stack: s[y_idx], round, start + PUSH $x_idx + DUP3 + // stack: round, 2, s[y_idx], round, start + %blake_permutation + // stack: s[x_idx], s[y_idx], round, start + %stack (ss: 2, r, s) -> (ss, s, r, s) + // stack: s[x_idx], 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 + %blake_g_function + // stack: round, start +%endmacro \ No newline at end of file diff --git a/evm/src/cpu/kernel/asm/hash/blake/store_pad.asm b/evm/src/cpu/kernel/asm/hash/blake/store_pad.asm deleted file mode 100644 index 7594eb81..00000000 --- a/evm/src/cpu/kernel/asm/hash/blake/store_pad.asm +++ /dev/null @@ -1,89 +0,0 @@ -global sha2: - %jump(sha2_store) - -global sha2_store: - // stack: num_bytes, x[0], x[1], ..., x[num_bytes - 1], retdest - DUP1 - // stack: num_bytes, num_bytes, x[0], x[1], ..., x[num_bytes - 1], retdest - PUSH 0 - // stack: addr=0, num_bytes, num_bytes, x[0], x[1], ..., x[num_bytes - 1], retdest - %mstore_kernel_general - // stack: num_bytes, x[0], x[1], ..., x[num_bytes - 1], retdest - PUSH 1 - // stack: addr=1, counter=num_bytes, x[0], x[1], x[2], ... , x[num_bytes-1], retdest -store_loop: - // stack: addr, counter, x[num_bytes-counter], ... , x[num_bytes-1], retdest - DUP2 - // stack: counter, addr, counter, x[num_bytes-counter], ... , x[num_bytes-1], retdest - ISZERO - %jumpi(store_end) - // stack: addr, counter, x[num_bytes-counter], ... , x[num_bytes-1], retdest - %stack (addr, counter, val) -> (addr, val, counter, addr) - // stack: addr, x[num_bytes-counter], counter, addr, ... , x[num_bytes-1], retdest - %mstore_kernel_general - // stack: counter, addr, ... , x[num_bytes-1], retdest - %decrement - // stack: counter-1, addr, ... , x[num_bytes-1], retdest - SWAP1 - // stack: addr, counter-1, ... , x[num_bytes-1], retdest - %increment - // stack: addr+1, counter-1, ... , x[num_bytes-1], retdest - %jump(store_loop) -store_end: - // stack: addr, counter, retdest - %pop2 - // stack: retdest - %jump(sha2_pad) - -// Precodition: input is in memory, starting at 0 of kernel general segment, of the form -// num_bytes, x[0], x[1], ..., x[num_bytes - 1] -// Postcodition: output is in memory, starting at 0, of the form -// num_blocks, block0[0], ..., block0[63], block1[0], ..., blocklast[63] -global sha2_pad: - // stack: retdest - PUSH 0 - %mload_kernel_general - // stack: num_bytes, retdest - // STEP 1: append 1 - // insert 128 (= 1 << 7) at x[num_bytes+1] - // stack: num_bytes, retdest - PUSH 1 - PUSH 7 - SHL - // stack: 128, num_bytes, retdest - DUP2 - // stack: num_bytes, 128, num_bytes, retdest - %increment - // stack: num_bytes+1, 128, num_bytes, retdest - %mstore_kernel_general - // stack: num_bytes, retdest - // STEP 2: calculate num_blocks := (num_bytes+8)//64 + 1 - DUP1 - // stack: num_bytes, num_bytes, retdest - %add_const(8) - %div_const(64) - - %increment - // stack: num_blocks = (num_bytes+8)//64 + 1, num_bytes, retdest - // STEP 3: calculate length := num_bytes*8 - SWAP1 - // stack: num_bytes, num_blocks, retdest - PUSH 8 - MUL - // stack: length = num_bytes*8, num_blocks, retdest - // STEP 4: write length to x[num_blocks*64-7..num_blocks*64] - DUP2 - // stack: num_blocks, length, num_blocks, retdest - PUSH 64 - MUL - // stack: last_addr = num_blocks*64, length, num_blocks, retdest - %sha2_write_length - // stack: num_blocks, retdest - DUP1 - // stack: num_blocks, num_blocks, retdest - // STEP 5: write num_blocks to x[0] - PUSH 0 - %mstore_kernel_general - // stack: num_blocks, retdest - %message_schedule_addr_from_num_blocks - %jump(sha2_gen_all_message_schedules)