diff --git a/evm/src/cpu/kernel/asm/sha2/sha2.asm b/evm/src/cpu/kernel/asm/sha2/sha2.asm index 2d3b0c53..e1e74038 100644 --- a/evm/src/cpu/kernel/asm/sha2/sha2.asm +++ b/evm/src/cpu/kernel/asm/sha2/sha2.asm @@ -40,37 +40,6 @@ sha2_store_end: //JUMP %jump(sha2_pad) -//global test_sha2_read: -// JUMPDEST -// // stack: retdest -// push 0 -// // stack: 0, retdest -// %mload_kernel_general -// // stack: counter=num_bytes, retdest -//test_sha2_read_loop: -// JUMPDEST -// // stack: counter, retdest, [stack] -// dup1 -// // stack: addr=counter, counter, retdest, [stack] -// %mload_kernel_general -// // stack: value, counter, retdest, [stack] -// swap2 -// // stack: retdest, counter, value, [stack] -// swap1 -// // stack: counter, retdest, value, [stack] -// %decrement -// // stack: counter-1, retdest, value, [stack] -// dup1 -// iszero -// %jumpi(test_sha2_read_end) -// %jump(test_sha2_read_loop) -//test_sha2_read_end: -// // stack: counter=0, retdest, [stack] -// JUMPDEST -// pop -// // stack: retdest, [stack] -// JUMP - // 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 @@ -84,8 +53,9 @@ global sha2_pad: // STEP 1: append 1 // insert 128 (= 1 << 7) at x[num_bytes] // stack: num_bytes, retdest - push 1 + // TODO: these should be in the other order once SHL implementation is fixed push 7 + push 1 shl // stack: 128, num_bytes, retdest dup2 @@ -126,7 +96,7 @@ global sha2_pad: %jump(sha2_gen_message_schedule_from_block) // Precodition: stack contains address of one message block, followed by output address -// Postcondition: 256 addresses starting at given output address, contain 32-bit chunks +// Postcondition: 256 bytes starting at given output address contain the 64 32-bit chunks // of message schedule (in four-byte increments) global sha2_gen_message_schedule_from_block: JUMPDEST @@ -153,8 +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 1 + // TODO: these should be in the other order once SHL implementation is fixed push 32 + push 1 shl // stack: 1 << 32, block[0], output_addr, counter, block[1], retdest dup2 @@ -204,8 +175,9 @@ sha2_gen_message_schedule_from_block_1_loop: // stack: counter, output_addr, block[1], block[0], retdest swap2 // stack: block[1], output_addr, counter, block[0], retdest - push 1 + // TODO: these should be in the other order once SHL implementation is fixed push 32 + push 1 shl // stack: 1 << 32, block[1], output_addr, counter, block[0], retdest dup2 diff --git a/evm/src/cpu/kernel/asm/sha2/write_length.asm b/evm/src/cpu/kernel/asm/sha2/write_length.asm index c79ceb06..ecc20fe6 100644 --- a/evm/src/cpu/kernel/asm/sha2/write_length.asm +++ b/evm/src/cpu/kernel/asm/sha2/write_length.asm @@ -2,8 +2,9 @@ // stack: last_addr, length swap1 // stack: length, last_addr - push 1 + // TODO: these should be in the other order once SHL implementation is fixed push 8 + push 1 shl // stack: 1 << 8, length, last_addr @@ -13,7 +14,7 @@ // stack: length, 1 << 8, 1 << 8, length, last_addr mod // stack: length % (1 << 8), 1 << 8, length, last_addr - dup3 + dup4 // stack: last_addr, length % (1 << 8), 1 << 8, length, last_addr %mstore_kernel_general @@ -23,11 +24,12 @@ dup2 // 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 // stack: (length >> 8) % (1 << 8), 1 << 8, length, last_addr - dup3 + dup4 // stack: last_addr, (length >> 8) % (1 << 8), 1 << 8, length, last_addr push 1 swap1 @@ -41,11 +43,12 @@ dup2 // 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 // stack: (length >> 16) % (1 << 8), 1 << 8, length, last_addr - dup3 + dup4 // stack: last_addr, (length >> 16) % (1 << 8), 1 << 8, length, last_addr push 2 swap1 @@ -59,11 +62,12 @@ dup2 // 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 // stack: (length >> 24) % (1 << 8), 1 << 8, length, last_addr - dup3 + dup4 // stack: last_addr, (length >> 24) % (1 << 8), 1 << 8, length, last_addr push 3 swap1 @@ -77,11 +81,12 @@ dup2 // 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 // stack: (length >> 32) % (1 << 8), 1 << 8, length, last_addr - dup3 + dup4 // stack: last_addr, (length >> 32) % (1 << 8), 1 << 8, length, last_addr push 4 swap1 @@ -99,7 +104,7 @@ // stack: length >> 40, 1 << 8, 1 << 8, length, last_addr mod // stack: (length >> 40) % (1 << 8), 1 << 8, length, last_addr - dup3 + dup4 // stack: last_addr, (length >> 40) % (1 << 8), 1 << 8, length, last_addr push 5 swap1 @@ -113,11 +118,12 @@ dup2 // 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 // stack: (length >> 48) % (1 << 8), 1 << 8, length, last_addr - dup3 + dup4 // stack: last_addr, (length >> 48) % (1 << 8), 1 << 8, length, last_addr push 6 swap1 @@ -131,11 +137,12 @@ dup2 // 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 // stack: (length >> 56) % (1 << 8), 1 << 8, length, last_addr - dup3 + dup4 // stack: last_addr, (length >> 56) % (1 << 8), 1 << 8, length, last_addr push 7 swap1 diff --git a/evm/src/cpu/kernel/interpreter.rs b/evm/src/cpu/kernel/interpreter.rs index 090230f5..72a76950 100644 --- a/evm/src/cpu/kernel/interpreter.rs +++ b/evm/src/cpu/kernel/interpreter.rs @@ -20,7 +20,7 @@ type F = GoldilocksField; /// Halt interpreter execution whenever a jump to this offset is done. const DEFAULT_HALT_OFFSET: usize = 0xdeadbeef; -#[derive(Debug)] +#[derive(Clone, Debug)] pub(crate) struct InterpreterMemory { pub(crate) context_memory: Vec, } diff --git a/evm/src/cpu/kernel/tests/sha2.rs b/evm/src/cpu/kernel/tests/sha2.rs index 2399b2ee..c610e94f 100644 --- a/evm/src/cpu/kernel/tests/sha2.rs +++ b/evm/src/cpu/kernel/tests/sha2.rs @@ -6,6 +6,7 @@ use rand::{thread_rng, Rng}; use crate::cpu::kernel::aggregator::combined_kernel; use crate::cpu::kernel::interpreter::run; +use crate::memory::segments::Segment; #[test] fn test_sha2_store() -> Result<()> { @@ -39,17 +40,15 @@ fn test_sha2_store() -> Result<()> { store_initial_stack, &kernel.prover_inputs, )?; + let stack_after_storing = after_storing.stack(); dbg!(stack_after_storing.clone()); + let memory_after_storing = after_storing.memory; - dbg!(memory_after_storing); + let mem = memory_after_storing.context_memory[0].segments[Segment::KernelGeneral as usize].content.clone(); + dbg!(&mem[0..66]); - // let load_initial_stack = vec![U256::from_str("0xdeadbeef").unwrap()]; - // let stack_after_loading = run(&kernel.code, test_sha2_read, load_initial_stack)?.stack; - // dbg!(stack_after_loading); - - // let expected_stack = todo!(); - // assert_eq!(stack_with_kernel, expected_stack); + // dbg!(&mem[100..353]); Ok(()) } diff --git a/evm/src/generation/memory.rs b/evm/src/generation/memory.rs index 5e2919a4..944b42a6 100644 --- a/evm/src/generation/memory.rs +++ b/evm/src/generation/memory.rs @@ -22,13 +22,13 @@ impl Default for MemoryState { } } -#[derive(Default, Debug)] +#[derive(Clone, Default, Debug)] pub(crate) struct MemoryContextState { /// The content of each memory segment. pub segments: [MemorySegmentState; Segment::COUNT], } -#[derive(Default, Debug)] +#[derive(Clone, Default, Debug)] pub(crate) struct MemorySegmentState { pub content: Vec, }