From 41ce8e94b986e235129850fbf35b02a88fdd7078 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Fri, 23 Sep 2022 10:39:49 -0700 Subject: [PATCH] FIRST UNIT TEST PASSED! --- evm/src/cpu/kernel/asm/ripemd/compression.asm | 2 +- evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 6 ++-- evm/src/cpu/kernel/asm/ripemd/update.asm | 6 ++-- evm/src/cpu/kernel/asm/util/basic_macros.asm | 32 ++++++++----------- evm/src/cpu/kernel/tests/ripemd.rs | 9 ++---- 5 files changed, 23 insertions(+), 32 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index d525b068..851586b7 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -99,7 +99,7 @@ mix: SWAP1 // stack: VR, RD, o0, o1, o2, o3, o4 POP - // stack: RD, o0, o1, o2, o3, o4 + // stack: RD, o0, o1, o2, o3, o4 JUMP diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm index 29971038..8112f20f 100644 --- a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -59,10 +59,11 @@ ripemd_2: global process: // stack: a , b, c, d, e, count, length, virt %reverse_bytes_u32 + %shl_const(128) // stack: a', b, c, d, e, VARS SWAP1 %reverse_bytes_u32 - %shl_const(32) + %shl_const(96) OR // stack: b' a', c, d, e, VARS SWAP1 @@ -72,12 +73,11 @@ global process: // stack: c' b' a', d, e, VARS SWAP1 %reverse_bytes_u32 - %shl_const(96) + %shl_const(32) OR // stack: d' c' b' a', e, VARS SWAP1 %reverse_bytes_u32 - %shl_const(96) OR // stack: e' d' c' b' a', VARS %stack (result, VARS: 3) -> (0xdeadbeef, result) diff --git a/evm/src/cpu/kernel/asm/ripemd/update.asm b/evm/src/cpu/kernel/asm/ripemd/update.asm index fccec845..a5132d7c 100644 --- a/evm/src/cpu/kernel/asm/ripemd/update.asm +++ b/evm/src/cpu/kernel/asm/ripemd/update.asm @@ -35,7 +35,7 @@ global ripemd_update: ISZERO // stack: Q, STATE, 0, shift, need, have, count, length, virt, retdest %stack (Q, STATE: 5, i, shift, need, have) -> (have, Q, Q, STATE, i, shift, need, have) - %eq_const(0) + %gt_const(0) AND // stack: P, Q, STATE, 0, shift, need, have, count, length, virt, retdest %jumpi(update_1) @@ -91,8 +91,8 @@ update_1a: /// cond -= 64 update_2: - // stack: STATE, 0, shift, need, have, count, length, virt, retdest - %stack (STATE: 5, i, shift, need, have, count, length) -> (length, shift, STATE, shift, need, have, count, length) + // stack: STATE, shift, need, have, count, length, virt, retdest + %stack (STATE: 5, shift, need, have, count, length) -> (length, shift, STATE, shift, need, have, count, length) SUB %ge_const(64) // stack: cond, STATE, shift, need, have, count, length, virt, retdest diff --git a/evm/src/cpu/kernel/asm/util/basic_macros.asm b/evm/src/cpu/kernel/asm/util/basic_macros.asm index b078f133..edf63359 100644 --- a/evm/src/cpu/kernel/asm/util/basic_macros.asm +++ b/evm/src/cpu/kernel/asm/util/basic_macros.asm @@ -258,44 +258,38 @@ DUP1 %and_const(0xFF) // stack: d, abcd - PUSH 0xFF - DUP1 - SWAP2 - // stack: abcd, d, 0xFF, d + %stack (d, abcd) -> (abcd, d, 0x100, d) + // stack: abcd, d, 0x100, d SUB DIV // stack: abc, d DUP1 %and_const(0xFF) - // stack: c, abcd, d - PUSH 0xFF - DUP1 - SWAP2 - // stack: abc, c, 0xFF, c, d + // stack: c, abc, d + %stack (c, abc) -> (abc, c, 0x100, c) + // stack: abc, c, 0x100, c, d SUB DIV // stack: ab, c, d DUP1 %and_const(0xFF) // stack: b, ab, c, d - PUSH 0xFF - DUP1 - SWAP2 - // stack: ab, b, 0xFF, b, c, d + %stack (b, ab) -> (ab, b, 0x100, b) + // stack: ab, b, 0x100, b, c, d SUB DIV // stack: a, b, c, d SWAP1 - %mul_const(0x100) - ADD + %shl_const(8) + OR // stack: ba, c, d SWAP1 - %mul_const(0x10000) - ADD + %shl_const(16) + OR // stack: cba, d SWAP1 - %mul_const(0x1000000) - ADD + %shl_const(24) + OR // stack: dcba %endmacro diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index f8019cdc..80c46a54 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -6,11 +6,8 @@ use crate::cpu::kernel::interpreter::run_with_kernel; #[test] fn test_ripemd() -> Result<()> { - let expected = "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc"; - // let expected: Vec<&str> = vec!["10271CF7", "1B2C699C", "EBDCBB56", "65289D5B", "BC8D70B3"]; - println!("{:#?}", expected); - - // let input: Vec = vec![0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0, 0, 0xdeadbeef]; + let expected = "f71c27109c692c1b56bbdceb5b9d2865b3708dbc"; + println!("{:#}", expected); let input: Vec = vec![ 26, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, @@ -23,7 +20,7 @@ fn test_ripemd() -> Result<()> { let actual: Vec = stack_output .stack() .iter() - .map(|&x| format!("{:X}", x)) + .map(|&x| format!("{:x}", x)) .rev() .collect(); println!("{:#?}", actual);