From 84273889b5e86c603c56e70469ed44176814904a Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Thu, 11 Aug 2022 17:50:23 -0700 Subject: [PATCH] many fixes --- evm/src/cpu/kernel/asm/sha2/memory.asm | 16 +++--- evm/src/cpu/kernel/asm/sha2/sha2.asm | 56 ++++++++++---------- evm/src/cpu/kernel/asm/sha2/write_length.asm | 31 +++++------ evm/src/cpu/kernel/tests/sha2.rs | 5 +- 4 files changed, 51 insertions(+), 57 deletions(-) diff --git a/evm/src/cpu/kernel/asm/sha2/memory.asm b/evm/src/cpu/kernel/asm/sha2/memory.asm index 3b86d9b0..018abef8 100644 --- a/evm/src/cpu/kernel/asm/sha2/memory.asm +++ b/evm/src/cpu/kernel/asm/sha2/memory.asm @@ -43,54 +43,54 @@ %macro mload_kernel_general_u256 // stack: offset DUP1 - %mload_kernel_code_u32 + %mload_kernel_general_u32 // stack: c_7, offset %shl_const(32) // stack: c7 << 32, offset DUP2 - %increment + %add_const(4) %mload_kernel_general_u32 OR // stack: (c_7 << 32) | c_6, offset %shl_const(32) // stack: ((c_7 << 32) | c_6) << 32, offset DUP2 - %add_const(2) + %add_const(8) %mload_kernel_general_u32 OR // stack: (c_7 << 64) | (c_6 << 32) | c_5, offset %shl_const(32) // stack: ((c_7 << 64) | (c_6 << 32) | c_5) << 32, offset DUP2 - %add_const(3) + %add_const(12) %mload_kernel_general_u32 OR // stack: (c_7 << 96) | (c_6 << 64) | (c_5 << 32) | c_4, offset %shl_const(32) // stack: ((c_7 << 96) | (c_6 << 64) | (c_5 << 32) | c_4) << 32, offset DUP2 - %add_const(4) + %add_const(16) %mload_kernel_general_u32 OR // stack: (c_7 << 128) | (c_6 << 96) | (c_5 << 64) | (c_4 << 32) | c_3, offset %shl_const(32) // stack: ((c_7 << 128) | (c_6 << 96) | (c_5 << 64) | (c_4 << 32) | c_3) << 32, offset DUP2 - %add_const(5) + %add_const(20) %mload_kernel_general_u32 OR // stack: (c_7 << 160) | (c_6 << 128) | (c_5 << 96) | (c_4 << 64) | (c_3 << 32) | c_2, offset %shl_const(32) // stack: ((c_7 << 160) | (c_6 << 128) | (c_5 << 96) | (c_4 << 64) | (c_3 << 32) | c_2) << 32, offset DUP2 - %add_const(6) + %add_const(24) %mload_kernel_general_u32 OR // stack: (c_7 << 192) | (c_6 << 160) | (c_5 << 128) | (c_4 << 96) | (c_3 << 64) | (c_2 << 32) | c_1, offset %shl_const(32) // stack: ((c_7 << 192) | (c_6 << 160) | (c_5 << 128) | (c_4 << 96) | (c_3 << 64) | (c_2 << 32) | c_1) << 32, offset DUP2 - %add_const(7) + %add_const(28) %mload_kernel_general_u32 OR // stack: (c_7 << 224) | (c_6 << 192) | (c_5 << 160) | (c_4 << 128) | (c_3 << 96) | (c_2 << 64) | (c_1 << 32) | c_0, offset diff --git a/evm/src/cpu/kernel/asm/sha2/sha2.asm b/evm/src/cpu/kernel/asm/sha2/sha2.asm index 368ea587..1bee6699 100644 --- a/evm/src/cpu/kernel/asm/sha2/sha2.asm +++ b/evm/src/cpu/kernel/asm/sha2/sha2.asm @@ -51,15 +51,16 @@ global sha2_pad: %mload_kernel_general // stack: num_bytes, retdest // STEP 1: append 1 - // insert 128 (= 1 << 7) at x[num_bytes] + // insert 128 (= 1 << 7) at x[num_bytes+1] // stack: num_bytes, retdest push 1 push 7 - swap1 // TODO: remove once SHR implementation is fixed 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 @@ -70,20 +71,18 @@ global sha2_pad: %increment // stack: num_blocks = (num_bytes+8)//64 + 1, num_bytes, retdest - // STEP 3: calculate length := num_bytes*8+1 + // STEP 3: calculate length := num_bytes*8 swap1 // stack: num_bytes, num_blocks, retdest push 8 mul - %increment - // stack: length = num_bytes*8+1, num_blocks, retdest - // STEP 4: write length to x[num_blocks*64-8..num_blocks*64-1] + // 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 - %decrement - // stack: last_addr = num_blocks*64-1, length, num_blocks, retdest + // stack: last_addr = num_blocks*64, length, num_blocks, retdest %sha2_write_length // stack: num_blocks, retdest // STEP 5: write num_blocks to x[0] @@ -114,6 +113,7 @@ global sha2_gen_message_schedule_from_block: %mload_kernel_general_u256 // stack: block[1], block[0], output_addr, retdest swap2 + STOP // stack: output_addr, block[0], block[1], retdest push 8 // stack: counter=8, output_addr, block[0], block[1], retdest @@ -123,10 +123,9 @@ sha2_gen_message_schedule_from_block_0_loop: // stack: counter, output_addr, block[0], block[1], retdest swap2 // stack: block[0], output_addr, counter, block[1], retdest - push 15 push 1 - //shl - STOP + push 32 + shl // stack: 1 << 32, block[0], output_addr, counter, block[1], retdest dup2 dup2 @@ -138,21 +137,21 @@ sha2_gen_message_schedule_from_block_0_loop: swap2 // stack: block[0], 1 << 32, block[0] % (1 << 32), output_addr, counter, block[1], retdest div - // stack: block[0] // (1 << 32), block[0] % (1 << 32), output_addr, counter, block[1], retdest + // stack: block[0] >> 32, block[0] % (1 << 32), output_addr, counter, block[1], retdest swap1 - // stack: block[0] % (1 << 32), block[0] // (1 << 32), output_addr, counter, block[1], retdest + // stack: block[0] % (1 << 32), block[0] >> 32, output_addr, counter, block[1], retdest dup3 - // stack: output_addr, block[0] % (1 << 32), block[0] // (1 << 32), output_addr, counter, block[1], retdest + // stack: output_addr, block[0] % (1 << 32), block[0] >> 32, output_addr, counter, block[1], retdest %mstore_kernel_general_u32 - // stack: block[0] // (1 << 32), output_addr, counter, block[1], retdest + // stack: block[0] >> 32, output_addr, counter, block[1], retdest swap1 - // stack: output_addr, block[0] // (1 << 32), counter, block[1], retdest + // stack: output_addr, block[0] >> 32, counter, block[1], retdest %add_const(4) - // stack: output_addr + 4, block[0] // (1 << 32), counter, block[1], retdest + // stack: output_addr + 4, block[0] >> 32, counter, block[1], retdest swap1 - // stack: block[0] // (1 << 32), output_addr + 4, counter, block[1], retdest + // stack: block[0] >> 32, output_addr + 4, counter, block[1], retdest swap2 - // stack: counter, output_addr + 4, block[0] // (1 << 32), block[1], retdest + // stack: counter, output_addr + 4, block[0] >> 32, block[1], retdest %decrement dup1 iszero @@ -177,7 +176,6 @@ sha2_gen_message_schedule_from_block_1_loop: // stack: block[1], output_addr, counter, block[0], retdest push 1 push 32 - swap1 // TODO: remove once SHL implementation is fixed shl // stack: 1 << 32, block[1], output_addr, counter, block[0], retdest dup2 @@ -190,21 +188,21 @@ sha2_gen_message_schedule_from_block_1_loop: swap2 // stack: block[1], 1 << 32, block[1] % (1 << 32), output_addr, counter, block[0], retdest div - // stack: block[1] // (1 << 32), block[1] % (1 << 32), output_addr, counter, block[0], retdest + // stack: block[1] >> 32, block[1] % (1 << 32), output_addr, counter, block[0], retdest swap1 - // stack: block[1] % (1 << 32), block[1] // (1 << 32), output_addr, counter, block[0], retdest + // stack: block[1] % (1 << 32), block[1] >> 32, output_addr, counter, block[0], retdest dup3 - // stack: output_addr, block[1] % (1 << 32), block[1] // (1 << 32), output_addr, counter, block[0], retdest + // stack: output_addr, block[1] % (1 << 32), block[1] >> 32, output_addr, counter, block[0], retdest %mstore_kernel_general_u32 - // stack: block[1] // (1 << 32), output_addr, counter, block[0], retdest + // stack: block[1] >> 32, output_addr, counter, block[0], retdest swap1 - // stack: output_addr, block[1] // (1 << 32), counter, block[0], retdest + // stack: output_addr, block[1] >> 32, counter, block[0], retdest %add_const(4) - // stack: output_addr + 4, block[1] // (1 << 32), counter, block[0], retdest + // stack: output_addr + 4, block[1] >> 32, counter, block[0], retdest swap1 - // stack: block[1] // (1 << 32), output_addr + 4, counter, block[0], retdest + // stack: block[1] >> 32, output_addr + 4, counter, block[0], retdest swap2 - // stack: counter, output_addr + 4, block[1] // (1 << 32), block[0], retdest + // stack: counter, output_addr + 4, block[1] >> 32, block[0], retdest %decrement dup1 iszero @@ -307,7 +305,7 @@ sha2_gen_message_schedule_remaining_end: JUMP // Precodition: memory, starting at 0, contains num_blocks, block0[0], ..., block0[63], block1[0], ..., blocklast[63] - stack contains output_addr +// stack contains output_addr // Postcondition: global sha2_gen_all_message_schedules: JUMPDEST diff --git a/evm/src/cpu/kernel/asm/sha2/write_length.asm b/evm/src/cpu/kernel/asm/sha2/write_length.asm index 368572e4..7474cd0e 100644 --- a/evm/src/cpu/kernel/asm/sha2/write_length.asm +++ b/evm/src/cpu/kernel/asm/sha2/write_length.asm @@ -4,7 +4,6 @@ // stack: length, last_addr push 1 push 8 - swap1 // TODO: remove once SHL implementation is fixed shl // stack: 1 << 8, length, last_addr @@ -21,10 +20,9 @@ // stack: 1 << 8, length, last_addr dup1 // stack: 1 << 8, 1 << 8, length, last_addr - dup2 + dup3 // stack: length, 1 << 8, 1 << 8, length, last_addr push 8 - swap1 // TODO: remove once SHR implementation is fixed shr // stack: length >> 8, 1 << 8, 1 << 8, length, last_addr mod @@ -40,10 +38,9 @@ // stack: 1 << 8, length, last_addr dup1 // stack: 1 << 8, 1 << 8, length, last_addr - dup2 + dup3 // stack: length, 1 << 8, 1 << 8, length, last_addr push 16 - swap1 // TODO: remove once SHR implementation is fixed shr // stack: length >> 16, 1 << 8, 1 << 8, length, last_addr mod @@ -59,10 +56,9 @@ // stack: 1 << 8, length, last_addr dup1 // stack: 1 << 8, 1 << 8, length, last_addr - dup2 + dup3 // stack: length, 1 << 8, 1 << 8, length, last_addr push 24 - swap1 // TODO: remove once SHR implementation is fixed shr // stack: length >> 24, 1 << 8, 1 << 8, length, last_addr mod @@ -72,16 +68,15 @@ push 3 swap1 sub - // stack: last_addr - 1, (length >> 24) % (1 << 8), 1 << 8, length, last_addr + // stack: last_addr - 3, (length >> 24) % (1 << 8), 1 << 8, length, last_addr %mstore_kernel_general // stack: 1 << 8, length, last_addr dup1 // stack: 1 << 8, 1 << 8, length, last_addr - dup2 + dup3 // stack: length, 1 << 8, 1 << 8, length, last_addr push 32 - swap1 // TODO: remove once SHR implementation is fixed shr // stack: length >> 32, 1 << 8, 1 << 8, length, last_addr mod @@ -91,13 +86,13 @@ push 4 swap1 sub - // stack: last_addr - 1, (length >> 32) % (1 << 8), 1 << 8, length, last_addr + // stack: last_addr - 4, (length >> 32) % (1 << 8), 1 << 8, length, last_addr %mstore_kernel_general // stack: 1 << 8, length, last_addr dup1 // stack: 1 << 8, 1 << 8, length, last_addr - dup2 + dup3 // stack: length, 1 << 8, 1 << 8, length, last_addr push 40 shr @@ -109,16 +104,15 @@ push 5 swap1 sub - // stack: last_addr - 1, (length >> 40) % (1 << 8), 1 << 8, length, last_addr + // stack: last_addr - 5, (length >> 40) % (1 << 8), 1 << 8, length, last_addr %mstore_kernel_general // stack: 1 << 8, length, last_addr dup1 // stack: 1 << 8, 1 << 8, length, last_addr - dup2 + dup3 // stack: length, 1 << 8, 1 << 8, length, last_addr push 48 - swap1 // TODO: remove once SHR implementation is fixed shr // stack: length >> 48, 1 << 8, 1 << 8, length, last_addr mod @@ -128,16 +122,15 @@ push 6 swap1 sub - // stack: last_addr - 1, (length >> 48) % (1 << 8), 1 << 8, length, last_addr + // stack: last_addr - 6, (length >> 48) % (1 << 8), 1 << 8, length, last_addr %mstore_kernel_general // stack: 1 << 8, length, last_addr dup1 // stack: 1 << 8, 1 << 8, length, last_addr - dup2 + dup3 // stack: length, 1 << 8, 1 << 8, length, last_addr push 56 - swap1 // TODO: remove once SHR implementation is fixed shr // stack: length >> 56, 1 << 8, 1 << 8, length, last_addr mod @@ -147,7 +140,7 @@ push 7 swap1 sub - // stack: last_addr - 1, (length >> 56) % (1 << 8), 1 << 8, length, last_addr + // stack: last_addr - 7, (length >> 56) % (1 << 8), 1 << 8, length, last_addr %mstore_kernel_general %pop3 // stack: (empty) diff --git a/evm/src/cpu/kernel/tests/sha2.rs b/evm/src/cpu/kernel/tests/sha2.rs index 7b931a91..a123c9da 100644 --- a/evm/src/cpu/kernel/tests/sha2.rs +++ b/evm/src/cpu/kernel/tests/sha2.rs @@ -28,6 +28,9 @@ fn test_sha2_store() -> Result<()> { dbg!(num_bytes); dbg!(bytes.clone()); + let num_bytes = 3; + let bytes: Vec = vec![97.into(), 98.into(), 99.into()]; + let mut store_initial_stack = vec![U256::from(num_bytes)]; store_initial_stack.extend(bytes); store_initial_stack.push(U256::from_str("0xdeadbeef").unwrap()); @@ -48,7 +51,7 @@ fn test_sha2_store() -> Result<()> { let mem = memory_after_storing.context_memory[0].segments[Segment::KernelGeneral as usize] .content .clone(); - dbg!(&mem[0..66]); + dbg!(&mem[0..65]); dbg!(&mem[100..353]);