From 21e64777550c1395e88bbfc1673bfe5b5f6ec505 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Wed, 27 Jul 2022 14:34:00 -0400 Subject: [PATCH 01/97] subroutines draft --- evm/src/cpu/kernel/asm/ripemd/subroutines.asm | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 evm/src/cpu/kernel/asm/ripemd/subroutines.asm diff --git a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm new file mode 100644 index 00000000..dfa4ea94 --- /dev/null +++ b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm @@ -0,0 +1,133 @@ +global Rol: + jumpdest + // stack: 2, 1 + dup2 + // stack: 2, 1, 1 + dup1 + // stack: 2, 2, 1, 1 + swap2 + // stack: 1, 2, 2, 1 + push 32 + // stack: 32, 1, 2, 2, 1 + sub + // stack: 31, 2, 2, 1 + swap1 + // stack: 2, 31, 2, 1 + shr + // stack: 0, 2, 1 + swap2 + // stack: 1, 2, 0 + swap1 + // stack: 2, 1, 0 + shl + // stack: 4, 0 + push 4294967295 + // stack: 4294967295, 4, 0 + and + // stack: 4, 0 + or + // stack: 4 + jump + +global F0: + jumpdest + // stack: x, y, z + xor + // stack: x ^ y, z + xor + // stack: x ^ y ^ z + jump + + +global F1: + jumpdest + // stack: 1, 2, 3 + dup1 + // stack: 1, 1, 2, 3 + swap2 + // stack: 2, 1, 1, 3 + and + // stack: 0, 1, 3 + swap2 + // stack: 3, 1, 0 + swap1 + // stack: 1, 3, 0 + not + // stack: -2, 3, 0 + push 0x100000000 + // stack: 4294967296, -2, 3, 0 + swap1 + // stack: -2, 4294967296, 3, 0 + mod + // stack: 4294967294, 3, 0 + and + // stack: 2, 0 + or + // stack: 2 + jump + + +global F2: + jumpdest + // stack: 1, 2, 3 + swap1 + // stack: 2, 1, 3 + not + // stack: -3, 1, 3 + push 0x100000000 + // stack: 4294967296, -3, 1, 3 + swap1 + // stack: -3, 4294967296, 1, 3 + mod + // stack: 4294967293, 1, 3 + or + // stack: 4294967293, 3 + xor + // stack: 4294967294 + jump + + +global F3: + jumpdest + // stack: 1, 2, 3 + dup3 + // stack: 1, 2, 3, 3 + swap3 + // stack: 3, 2, 3, 1 + not + // stack: -4, 2, 3, 1 + push 0x100000000 + // stack: 4294967296, -4, 2, 3, 1 + swap1 + // stack: -4, 4294967296, 2, 3, 1 + mod + // stack: 4294967292, 2, 3, 1 + and + // stack: 0, 3, 1 + swap2 + // stack: 1, 3, 0 + and + // stack: 1, 0 + or + // stack: 1 + jump + + +global F4: + jumpdest + // stack: 1, 2, 3 + swap2 + // stack: 3, 2, 1 + not + // stack: -4, 2, 1 + push 0x100000000 + // stack: 4294967296, -4, 2, 1 + swap1 + // stack: -4, 4294967296, 2, 1 + mod + // stack: 4294967292, 2, 1 + or + // stack: 4294967294, 1 + xor + // stack: 4294967295 + jump \ No newline at end of file From d25693d77c36980948163e61249d7118305e622b Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Fri, 29 Jul 2022 13:24:11 -0400 Subject: [PATCH 02/97] update dups, write out stack states --- evm/src/cpu/kernel/asm/ripemd/subroutines.asm | 141 ++++++++---------- 1 file changed, 64 insertions(+), 77 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm index dfa4ea94..ce91e1bc 100644 --- a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm +++ b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm @@ -1,133 +1,120 @@ global Rol: jumpdest - // stack: 2, 1 - dup2 - // stack: 2, 1, 1 + // stack: n, x, retdest + swap1 + // stack: x, n, retdest dup1 - // stack: 2, 2, 1, 1 - swap2 - // stack: 1, 2, 2, 1 + // stack: x, x, n, retdest + dup3 + // stack: n, x, x, n, retdest push 32 - // stack: 32, 1, 2, 2, 1 + // stack: 32, n, x, x, n, retdest sub - // stack: 31, 2, 2, 1 + // stack: 32-n, x, x, n, retdest swap1 - // stack: 2, 31, 2, 1 + // stack: x, 32-n, x, n, retdest shr - // stack: 0, 2, 1 + // stack: x << (32-n), x, n, retdest swap2 - // stack: 1, 2, 0 + // stack: n, x, x << (32-n), retdest swap1 - // stack: 2, 1, 0 + // stack: x, n, x << (32-n), retdest shl - // stack: 4, 0 - push 4294967295 - // stack: 4294967295, 4, 0 + // stack: x >> n, x << (32-n), retdest + push 0xffffffff + // stack: 0xffffffff, (x >> n), x << (32-n), retdest and - // stack: 4, 0 + // stack: (x >> n) & 0xffffffff, x << (32-n), retdest or - // stack: 4 + // stack: ((x >> n) & 0xffffffff) | (x << (32-n)), retdest + swap1 + // stack: retdest, ((x >> n) & 0xffffffff) | (x << (32-n)) jump + global F0: jumpdest - // stack: x, y, z + // stack: x, y, z, retdest xor - // stack: x ^ y, z + // stack: x ^ y, z, retdest xor - // stack: x ^ y ^ z + // stack: x ^ y ^ z, retdest + swap1 + // stack: retdest, x ^ y ^ z jump global F1: jumpdest - // stack: 1, 2, 3 + // stack: x, y, z, retdest dup1 - // stack: 1, 1, 2, 3 + // stack: x, x, y, z, retdest swap2 - // stack: 2, 1, 1, 3 + // stack: y, x, x, z, retdest and - // stack: 0, 1, 3 + // stack: y & x, x, z, retdest swap2 - // stack: 3, 1, 0 + // stack: z, x, y & x, retdest swap1 - // stack: 1, 3, 0 - not - // stack: -2, 3, 0 - push 0x100000000 - // stack: 4294967296, -2, 3, 0 - swap1 - // stack: -2, 4294967296, 3, 0 - mod - // stack: 4294967294, 3, 0 + // stack: x, z, y & x, retdest + %not_u32 + // stack: ~x, z, y & x, retdest and - // stack: 2, 0 + // stack: ~x & z, y & x, retdest or - // stack: 2 + // stack: (~x & z) | (y & x), retdest + swap1 + // stack: retdest, (~x & z) | (y & x) jump global F2: jumpdest - // stack: 1, 2, 3 + // stack: x, y, z, retdest swap1 - // stack: 2, 1, 3 - not - // stack: -3, 1, 3 - push 0x100000000 - // stack: 4294967296, -3, 1, 3 - swap1 - // stack: -3, 4294967296, 1, 3 - mod - // stack: 4294967293, 1, 3 + // stack: y, x, z, retdest + %not_u32 + // stack: ~y, x, z, retdest or - // stack: 4294967293, 3 + // stack: ~y | x, z, retdest xor - // stack: 4294967294 + // stack: (~y | x) ^ z, retdest + swap1 + // stack: retdest, (~y | x) ^ z jump global F3: jumpdest - // stack: 1, 2, 3 + // stack: x, y, z, retdest dup3 - // stack: 1, 2, 3, 3 - swap3 - // stack: 3, 2, 3, 1 - not - // stack: -4, 2, 3, 1 - push 0x100000000 - // stack: 4294967296, -4, 2, 3, 1 - swap1 - // stack: -4, 4294967296, 2, 3, 1 - mod - // stack: 4294967292, 2, 3, 1 + // stack: z, x, y, z, retdest and - // stack: 0, 3, 1 + // stack: z & x, y, z, retdest swap2 - // stack: 1, 3, 0 + // stack: z, y, z & x, retdest + %not_u32 + // stack: ~z, y, z & x, retdest and - // stack: 1, 0 + // stack: ~z & y, z & x, retdest or - // stack: 1 + // stack: (~z & y) | (z & x), retdest + swap1 + // stack: retdest, (~z & y) | (z & x) jump global F4: jumpdest - // stack: 1, 2, 3 + // stack: x, y, z, retdest swap2 - // stack: 3, 2, 1 - not - // stack: -4, 2, 1 - push 0x100000000 - // stack: 4294967296, -4, 2, 1 - swap1 - // stack: -4, 4294967296, 2, 1 - mod - // stack: 4294967292, 2, 1 + // stack: z, y, x, retdest + %not_u32 + // stack: ~z, y, x, retdest or - // stack: 4294967294, 1 + // stack: ~z | y, x, retdest xor - // stack: 4294967295 - jump \ No newline at end of file + // stack: (~z | y) ^ x, retdest + swap1 + // stack: retdest, (~z | y) ^ x + jump From 7e0334fccebc93506904882b8aca5705112cbda7 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Sat, 30 Jul 2022 14:21:06 -0400 Subject: [PATCH 03/97] fix shift direction --- evm/src/cpu/kernel/asm/ripemd/subroutines.asm | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm index ce91e1bc..263dfcd0 100644 --- a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm +++ b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm @@ -14,21 +14,21 @@ global Rol: swap1 // stack: x, 32-n, x, n, retdest shr - // stack: x << (32-n), x, n, retdest + // stack: x >> (32-n), x, n, retdest swap2 - // stack: n, x, x << (32-n), retdest + // stack: n, x, x >> (32-n), retdest swap1 - // stack: x, n, x << (32-n), retdest + // stack: x, n, x >> (32-n), retdest shl - // stack: x >> n, x << (32-n), retdest + // stack: x << n, x >> (32-n), retdest push 0xffffffff - // stack: 0xffffffff, (x >> n), x << (32-n), retdest + // stack: 0xffffffff, (x << n), x >> (32-n), retdest and - // stack: (x >> n) & 0xffffffff, x << (32-n), retdest + // stack: (x << n) & 0xffffffff, x >> (32-n), retdest or - // stack: ((x >> n) & 0xffffffff) | (x << (32-n)), retdest + // stack: ((x << n) & 0xffffffff) | (x >> (32-n)), retdest swap1 - // stack: retdest, ((x >> n) & 0xffffffff) | (x << (32-n)) + // stack: retdest, ((x << n) & 0xffffffff) | (x >> (32-n)) jump From cf637115970fc9e392dc9e6c638e76aecaca32e0 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Sat, 30 Jul 2022 14:52:39 -0400 Subject: [PATCH 04/97] add u32 macros --- evm/src/cpu/kernel/asm/basic_macros.asm | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/evm/src/cpu/kernel/asm/basic_macros.asm b/evm/src/cpu/kernel/asm/basic_macros.asm index e266b2cb..f265c69f 100644 --- a/evm/src/cpu/kernel/asm/basic_macros.asm +++ b/evm/src/cpu/kernel/asm/basic_macros.asm @@ -188,3 +188,19 @@ mul // stack: x^2 %endmacro + +%macro not_32 + // stack: x + push 0xffffffff + // stack: 0xffffffff, x + sub + // stack: 0xffffffff - x +%endmacro + +%macro u32 + // stack: x + push 0xffffffff + // stack: 0xffffffff, x + and + // stack: 0xffffffff & x +%endmacro \ No newline at end of file From 93d25859b252e50d8cdc59a233a21bafa04da332 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 8 Aug 2022 10:51:01 -0400 Subject: [PATCH 05/97] merge --- evm/src/cpu/kernel/asm/basic_macros.asm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evm/src/cpu/kernel/asm/basic_macros.asm b/evm/src/cpu/kernel/asm/basic_macros.asm index f265c69f..9583409c 100644 --- a/evm/src/cpu/kernel/asm/basic_macros.asm +++ b/evm/src/cpu/kernel/asm/basic_macros.asm @@ -203,4 +203,4 @@ // stack: 0xffffffff, x and // stack: 0xffffffff & x -%endmacro \ No newline at end of file +%endmacro From 463384545292548348ec9fc6e6b35cf3c6f5d828 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 8 Aug 2022 10:57:39 -0400 Subject: [PATCH 06/97] fix merge conflict --- evm/src/cpu/kernel/asm/util/basic_macros.asm | 33 ++++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/evm/src/cpu/kernel/asm/util/basic_macros.asm b/evm/src/cpu/kernel/asm/util/basic_macros.asm index 8876fd1e..06283bb3 100644 --- a/evm/src/cpu/kernel/asm/util/basic_macros.asm +++ b/evm/src/cpu/kernel/asm/util/basic_macros.asm @@ -194,22 +194,6 @@ // stack: x^2 %endmacro -<<<<<<< HEAD:evm/src/cpu/kernel/asm/basic_macros.asm -%macro not_32 - // stack: x - push 0xffffffff - // stack: 0xffffffff, x - sub - // stack: 0xffffffff - x -%endmacro - -%macro u32 - // stack: x - push 0xffffffff - // stack: 0xffffffff, x - and - // stack: 0xffffffff & x -======= %macro min // stack: x, y DUP2 @@ -230,5 +214,20 @@ // stack: x > y, x, y %select_bool // stack: max ->>>>>>> 65a20bcd8a5a4040f86b7425817b98daecc05a78:evm/src/cpu/kernel/asm/util/basic_macros.asm %endmacro + +%macro not_32 + // stack: x + push 0xffffffff + // stack: 0xffffffff, x + sub + // stack: 0xffffffff - x +%endmacro + +%macro u32 + // stack: x + push 0xffffffff + // stack: 0xffffffff, x + and + // stack: 0xffffffff & x + \ No newline at end of file From 48f17e484ba41538f1ab766a0b214f3f732ab040 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 8 Aug 2022 11:04:05 -0400 Subject: [PATCH 07/97] space --- evm/src/cpu/kernel/asm/util/basic_macros.asm | 1 - 1 file changed, 1 deletion(-) diff --git a/evm/src/cpu/kernel/asm/util/basic_macros.asm b/evm/src/cpu/kernel/asm/util/basic_macros.asm index 06283bb3..01594ca6 100644 --- a/evm/src/cpu/kernel/asm/util/basic_macros.asm +++ b/evm/src/cpu/kernel/asm/util/basic_macros.asm @@ -230,4 +230,3 @@ // stack: 0xffffffff, x and // stack: 0xffffffff & x - \ No newline at end of file From 26574f8b1eb5ad3d43ef4cf96a3380a5d9021ab9 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Tue, 9 Aug 2022 14:28:32 -0700 Subject: [PATCH 08/97] swap shift orders --- evm/src/cpu/kernel/asm/ripemd/subroutines.asm | 4 ---- 1 file changed, 4 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm index 263dfcd0..05cf0c15 100644 --- a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm +++ b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm @@ -11,14 +11,10 @@ global Rol: // stack: 32, n, x, x, n, retdest sub // stack: 32-n, x, x, n, retdest - swap1 - // stack: x, 32-n, x, n, retdest shr // stack: x >> (32-n), x, n, retdest swap2 // stack: n, x, x >> (32-n), retdest - swap1 - // stack: x, n, x >> (32-n), retdest shl // stack: x << n, x >> (32-n), retdest push 0xffffffff From f9efc3aedee5069c8a96fa12c79b94ca2860b5d8 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Tue, 9 Aug 2022 15:11:18 -0700 Subject: [PATCH 09/97] draft implementation --- .../asm/ripemd/{iterate.asm => hashloop.asm} | 0 evm/src/cpu/kernel/asm/ripemd/subroutines.asm | 84 +++++++++++++++++-- 2 files changed, 79 insertions(+), 5 deletions(-) rename evm/src/cpu/kernel/asm/ripemd/{iterate.asm => hashloop.asm} (100%) diff --git a/evm/src/cpu/kernel/asm/ripemd/iterate.asm b/evm/src/cpu/kernel/asm/ripemd/hashloop.asm similarity index 100% rename from evm/src/cpu/kernel/asm/ripemd/iterate.asm rename to evm/src/cpu/kernel/asm/ripemd/hashloop.asm diff --git a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm index 05cf0c15..1590e515 100644 --- a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm +++ b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm @@ -1,3 +1,62 @@ +/// Note that Fj, Kj last for 16 iterations, but sj, rj update each call +/// +/// def R(a, b, c, d, e, Fj, Kj, sj, rj, X): +/// a = u32(ROL(sj, u32(Fj(b, c, d) + a + X[rj] + Kj)) + e) +/// c = ROL(10, c) +/// return e, a, b, c, d + +global R: + jumpdest + // stack: a, b, c, d, e, Fj, Kj, retdest + dup4 + // stack: d, a, b, c, d, e, Fj, Kj, retdest + dup4 + // stack: c, d, a, b, c, d, e, Fj, Kj, retdest + dup4 + // stack: b, c, d, a, b, c, d, e, Fj, Kj, retdest + dup9 + // stack: Fj, b, c, d, a, b, c, d, e, Fj, Kj, retdest + jump---------------------------------------------------------------------------TODO + // stack: Fj(b, c, d), a, b, c, d, e, Fj, Kj, retdest + add + // stack: Fj(b, c, d) + a, b, c, d, e, Fj, Kj, retdest + push X[rj]---------------------------------------------------------------------TODO + // stack: X[rj], Fj(b, c, d) + a, b, c, d, e, Fj, Kj, retdest + add + // stack: X[rj] + Fj(b, c, d) + a, b, c, d, e, Fj, Kj, retdest + dup7 + // stack: Kj, X[rj] + Fj(b, c, d) + a, b, c, d, e, Fj, Kj, retdest + add + // stack: Kj + X[rj] + Fj(b, c, d) + a, b, c, d, e, Fj, Kj, retdest + %u32 + // stack: Kj + X[rj] + Fj(b, c, d) + a, b, c, d, e, Fj, Kj, retdest + push sj------------------------------------------------------------------------TODO + // stack: sj, Kj + X[rj] + Fj(b, c, d) + a, b, c, d, e, Fj, Kj, retdest + push ROL + // stack: ROL, sj, Kj + X[rj] + Fj(b, c, d) + a, b, c, d, e, Fj, Kj, retdest + jump---------------------------------------------------------------------------TODO + // stack: ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, c, d, e, Fj, Kj, retdest + dup5 + // stack: e, ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, c, d, e, Fj, Kj, retdest + add + // stack: e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, c, d, e, Fj, Kj, retdest + %u32 + // stack: e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, c, d, e, Fj, Kj, retdest + swap2 + // stack: c, b, e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), d, e, Fj, Kj, retdest + push 10 + // stack: 10, c, b, e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), d, e, Fj, Kj, retdest + push ROL + // stack: ROL, 10, c, b, e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), d, e, Fj, Kj, retdest + jump---------------------------------------------------------------------------TODO + // stack: ROL(10, c), b, e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), d, e, Fj, Kj, retdest + %stack (c, b, a, d, e) -> (e, a, b, c, d) + // stack: e, e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, ROL(10, c), d, e, Fj, Kj, retdest + + +/// def ROL(n, x): +/// return (u32(x << n)) | (x >> (32 - n)) + global Rol: jumpdest // stack: n, x, retdest @@ -28,6 +87,9 @@ global Rol: jump +/// def F0(x, y, z): +/// return x ^ y ^ z + global F0: jumpdest // stack: x, y, z, retdest @@ -40,6 +102,9 @@ global F0: jump +/// def F1(x, y, z): +/// return (x & y) | (u32(~x) & z) + global F1: jumpdest // stack: x, y, z, retdest @@ -53,7 +118,7 @@ global F1: // stack: z, x, y & x, retdest swap1 // stack: x, z, y & x, retdest - %not_u32 + %not_32 // stack: ~x, z, y & x, retdest and // stack: ~x & z, y & x, retdest @@ -64,22 +129,28 @@ global F1: jump +/// def F2(x, y, z): +/// return (x | u32(~y)) ^ z + global F2: jumpdest // stack: x, y, z, retdest swap1 // stack: y, x, z, retdest - %not_u32 + %not_32 // stack: ~y, x, z, retdest or // stack: ~y | x, z, retdest xor - // stack: (~y | x) ^ z, retdest + // stack: (~y | x) ^ z, retdest swap1 // stack: retdest, (~y | x) ^ z jump +/// def F3(x, y, z): +/// return (x & z) | (u32(~z) & y) + global F3: jumpdest // stack: x, y, z, retdest @@ -89,7 +160,7 @@ global F3: // stack: z & x, y, z, retdest swap2 // stack: z, y, z & x, retdest - %not_u32 + %not_32 // stack: ~z, y, z & x, retdest and // stack: ~z & y, z & x, retdest @@ -100,12 +171,15 @@ global F3: jump +/// def F4(x, y, z): +/// return x ^ (y | u32(~z)) + global F4: jumpdest // stack: x, y, z, retdest swap2 // stack: z, y, x, retdest - %not_u32 + %not_32 // stack: ~z, y, x, retdest or // stack: ~z | y, x, retdest From 03b4d0a9da3478a8b2d416105dc21e31e1e344fb Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Tue, 23 Aug 2022 12:03:06 -0700 Subject: [PATCH 10/97] all but memory --- evm/src/cpu/kernel/asm/ripemd/hashloop.asm | 168 +++++++++++++++++- evm/src/cpu/kernel/asm/ripemd/subroutines.asm | 58 +----- evm/src/cpu/kernel/asm/util/basic_macros.asm | 21 ++- 3 files changed, 182 insertions(+), 65 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/hashloop.asm b/evm/src/cpu/kernel/asm/ripemd/hashloop.asm index b91624ce..242b7575 100644 --- a/evm/src/cpu/kernel/asm/ripemd/hashloop.asm +++ b/evm/src/cpu/kernel/asm/ripemd/hashloop.asm @@ -1,2 +1,166 @@ -global R: - \ No newline at end of file +/// Variables beginning with _ are not maintained on the stack +/// Note that state takes up 5 stack slots + + +/// def hash(state, _block): +/// +/// stateL = state +/// stateL = loop(stateL) +/// +/// stateR = state +/// stateR = loop(stateR) +/// +/// state = mix(state, stateL, stateR) + +global hash: + jumpdest + // stack: *state, retdest + push switch push 5 push 16 push K0 push F0 + // stack: F0, K0, 16, 5, switch, *state, retdest + dup10 dup10 dup10 dup10 dup10 + // stack: *state, F0, K0, 16, 5, switch, *state, retdest + %jump(loop) +switch: + jumpdest + // stack: *stateL, *state, retdest + push mix push 5 push 16 + // stack: F0, K0, 16, 5, mix, *stateL, *state, retdest + dup15 dup15 dup15 dup15 dup15 + // stack: *state, F0, K0, 16, 5, mix, *stateL, *state, retdest + %jump(loop) + + +/// def loop(*state, Fj, Kj): +/// while n: +/// while i: +/// R(*state, Fj, Kj) +/// i -= 1 +/// i = 16 +/// n -= 1 +/// F = Fs[n] +/// K = Ks[n] + +loop: + jumpdest + // stack: *stack, Fj, Kj, 16, n, retdest + push 1 dup9 sub swap8 + // stack: n, *stack, Fj, Kj, 16, n-1, retdest + %jumpi(cycle) + // stack: *stack, Fj, Kj, 16, -1, retdest + %stack (a, b, c, d, e, f, k, i, n, ret) -> (ret, a, b, c, d, e) + // stack: retdest, *stack + jump +cycle: + jumpdest + // stack: *stack, Fj, Kj, i, n, retdest + push 1 dup9 sub swap8 + // stack: i, *stack, Fj, Kj, i-1, n, retdest + %jumpi(R) + // stack: *stack, Fj, Kj, -1, n, retdest + swap5 pop push Fj swap5 ---------------------------------------------------------------------TODO + // stack: *stack, Fj, Kj 16, n, retdest + swap6 pop push Kj swap6 ---------------------------------------------------------------------TODO + // stack: *stack, Fj, Kj 16, n, retdest + swap7 pop push 16 swap7 + // stack: *stack, Fj, Kj 16, n, retdest + %jump(loop) + + +/// def R(a, b, c, d, e, Fj, Kj, _sj, _rj, _X): +/// a = u32(ROL(sj, u32(Fj(b, c, d) + a + X[rj] + Kj)) + e) +/// c = ROL(10, c) +/// return e, a, b, c, d, Fj, Kj + +R: + jumpdest + // stack: a, b, c, d, e, Fj, Kj + push after_F dup5 dup5 dup5 dup10 + // stack: Fj, b, c, d, after_F, a, b, c, d, e, Fj, Kj + jump +after_F: + // stack: Fj(b, c, d), a, b, c, d, e, Fj, Kj + add + // stack: Fj(b, c, d) + a, b, c, d, e, Fj, Kj + push X[rj]---------------------------------------------------------------------TODO + // stack: X[rj], Fj(b, c, d) + a, b, c, d, e, Fj, Kj + add + // stack: X[rj] + Fj(b, c, d) + a, b, c, d, e, Fj, Kj + dup7 + // stack: Kj, X[rj] + Fj(b, c, d) + a, b, c, d, e, Fj, Kj + add %u32 + // stack: Kj + X[rj] + Fj(b, c, d) + a, b, c, d, e, Fj, Kj + push sj------------------------------------------------------------------------TODO + // stack: sj, Kj + X[rj] + Fj(b, c, d) + a, b, c, d, e, Fj, Kj + %jump(ROL) + // stack: ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, c, d, e, Fj, Kj + dup5 + // stack: e, ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, c, d, e, Fj, Kj + add %u32 + // stack: e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, c, d, e, Fj, Kj + swap1 + // stack: b, e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), c, d, e, Fj, Kj + swap2 + // stack: c, e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, d, e, Fj, Kj + push 10 + // stack: 10, c, b, e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), d, e, Fj, Kj + %jump(ROL) + // stack: ROL(10, c), e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, d, e, Fj, Kj + swap4 + // stack: d, e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, ROL(10,c), e, Fj, Kj + swap5 + // stack: e, e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, ROL(10, c), d, Fj, Kj + %jump(cycle) + + +/// def mix(*stateR, *stateL, *state): +/// return [ +/// u32(state[1] + stateL[2] + stateR[3]), +/// u32(state[2] + stateL[3] + stateR[4]), +/// u32(state[3] + stateL[4] + stateR[0]), +/// u32(state[4] + stateL[0] + stateR[1]), +/// u32(state[0] + stateL[1] + stateR[2]) +/// ] +/// +/// Note that we denote state[i], stateL[i], stateR[i] by si, li, ri + +mix: + jumpdest + // stack: r0, r1, r2, r3, r4, l0, l1, l2, l3, l4, s0, s1, s2, s3, s4, retdest + swap10 + // stack: s0, r1, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, retdest + swap1 + // stack: r1, s0, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, retdest + swap6 + // stack: l1, s0, r2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, retdest + %add3_32 + // stack: s0+l1+r2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, retdest + swap13 + // stack: retdest, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, s0+l1+r2 + swap11 + // stack: s3, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, retdest, s4, s0+l1+r2 + swap10 + // stack: s2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s3, retdest, s4, s0+l1+r2 + swap1 + // stack: r3, s2, r4, l0, r1, l2, l3, l4, r0, s1, s3, retdest, s4, s0+l1+r2 + swap6 + // stack: l3, s2, r4, l0, r1, l2, r3, l4, r0, s1, s3, retdest, s4, s0+l1+r2 + %add3_32 + // stack: s2+l3+r4, l0, r1, l2, r3, l4, r0, s1, s3, retdest, s4, s0+l1+r2 + swap8 + // stack: s3, l0, r1, l2, r3, l4, r0, s1, s2+l3+r4, retdest, s4, s0+l1+r2 + swap10 + // stack: s4, l0, r1, l2, r3, l4, r0, s1, s2+l3+r4, retdest, s3, s0+l1+r2 + %add3_32 + // stack: s4+l0+r1, l2, r3, l4, r0, s1, s2+l3+r4, retdest, s3, s0+l1+r2 + swap8 + // stack: s3, l2, r3, l4, r0, s1, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2 + swap5 + // stack: s1, l2, r3, l4, r0, s3, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2 + %add3_32 + // stack: s1+l2+r3, l4, r0, s3, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2 + swap3 + // stack: s3, l4, r0, s1+l2+r3, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2 + %add3_32 + // stack: s3+l4+r0, s1+l2+r3, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2 + swap3 + // stack: retdest, s1+l2+r3, s2+l3+r4, s3+l4+r0, s4+l0+r1, s0+l1+r2 diff --git a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm index 1590e515..ab34a8d6 100644 --- a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm +++ b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm @@ -1,63 +1,7 @@ -/// Note that Fj, Kj last for 16 iterations, but sj, rj update each call -/// -/// def R(a, b, c, d, e, Fj, Kj, sj, rj, X): -/// a = u32(ROL(sj, u32(Fj(b, c, d) + a + X[rj] + Kj)) + e) -/// c = ROL(10, c) -/// return e, a, b, c, d - -global R: - jumpdest - // stack: a, b, c, d, e, Fj, Kj, retdest - dup4 - // stack: d, a, b, c, d, e, Fj, Kj, retdest - dup4 - // stack: c, d, a, b, c, d, e, Fj, Kj, retdest - dup4 - // stack: b, c, d, a, b, c, d, e, Fj, Kj, retdest - dup9 - // stack: Fj, b, c, d, a, b, c, d, e, Fj, Kj, retdest - jump---------------------------------------------------------------------------TODO - // stack: Fj(b, c, d), a, b, c, d, e, Fj, Kj, retdest - add - // stack: Fj(b, c, d) + a, b, c, d, e, Fj, Kj, retdest - push X[rj]---------------------------------------------------------------------TODO - // stack: X[rj], Fj(b, c, d) + a, b, c, d, e, Fj, Kj, retdest - add - // stack: X[rj] + Fj(b, c, d) + a, b, c, d, e, Fj, Kj, retdest - dup7 - // stack: Kj, X[rj] + Fj(b, c, d) + a, b, c, d, e, Fj, Kj, retdest - add - // stack: Kj + X[rj] + Fj(b, c, d) + a, b, c, d, e, Fj, Kj, retdest - %u32 - // stack: Kj + X[rj] + Fj(b, c, d) + a, b, c, d, e, Fj, Kj, retdest - push sj------------------------------------------------------------------------TODO - // stack: sj, Kj + X[rj] + Fj(b, c, d) + a, b, c, d, e, Fj, Kj, retdest - push ROL - // stack: ROL, sj, Kj + X[rj] + Fj(b, c, d) + a, b, c, d, e, Fj, Kj, retdest - jump---------------------------------------------------------------------------TODO - // stack: ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, c, d, e, Fj, Kj, retdest - dup5 - // stack: e, ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, c, d, e, Fj, Kj, retdest - add - // stack: e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, c, d, e, Fj, Kj, retdest - %u32 - // stack: e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, c, d, e, Fj, Kj, retdest - swap2 - // stack: c, b, e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), d, e, Fj, Kj, retdest - push 10 - // stack: 10, c, b, e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), d, e, Fj, Kj, retdest - push ROL - // stack: ROL, 10, c, b, e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), d, e, Fj, Kj, retdest - jump---------------------------------------------------------------------------TODO - // stack: ROL(10, c), b, e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), d, e, Fj, Kj, retdest - %stack (c, b, a, d, e) -> (e, a, b, c, d) - // stack: e, e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, ROL(10, c), d, e, Fj, Kj, retdest - - /// def ROL(n, x): /// return (u32(x << n)) | (x >> (32 - n)) -global Rol: +global ROL: jumpdest // stack: n, x, retdest swap1 diff --git a/evm/src/cpu/kernel/asm/util/basic_macros.asm b/evm/src/cpu/kernel/asm/util/basic_macros.asm index 01594ca6..5b88c51f 100644 --- a/evm/src/cpu/kernel/asm/util/basic_macros.asm +++ b/evm/src/cpu/kernel/asm/util/basic_macros.asm @@ -216,6 +216,13 @@ // stack: max %endmacro +%macro u32 + // stack: x + push 0xffffffff + // stack: 0xffffffff, x + and + // stack: 0xffffffff & x + %macro not_32 // stack: x push 0xffffffff @@ -224,9 +231,11 @@ // stack: 0xffffffff - x %endmacro -%macro u32 - // stack: x - push 0xffffffff - // stack: 0xffffffff, x - and - // stack: 0xffffffff & x +%macro add3_32 + // stack: x, y, z + add + // stack: x+y, z + add + // stack: x+y+z + %u32 +%endmacro From ca230010292d40ffe20b1a2b5d95831582f9648e Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Tue, 23 Aug 2022 16:40:31 -0700 Subject: [PATCH 11/97] add constants --- evm/src/cpu/kernel/asm/ripemd/constants.asm | 126 ++++++++++++++++++ .../asm/ripemd/{hashloop.asm => hash.asm} | 24 ++-- 2 files changed, 139 insertions(+), 11 deletions(-) create mode 100644 evm/src/cpu/kernel/asm/ripemd/constants.asm rename evm/src/cpu/kernel/asm/ripemd/{hashloop.asm => hash.asm} (91%) diff --git a/evm/src/cpu/kernel/asm/ripemd/constants.asm b/evm/src/cpu/kernel/asm/ripemd/constants.asm new file mode 100644 index 00000000..ad20de9c --- /dev/null +++ b/evm/src/cpu/kernel/asm/ripemd/constants.asm @@ -0,0 +1,126 @@ +// %macro load_K + +K_data: + // Left + BYTES 0x00, 0x00, 0x00, 0x00 + BYTES 0x5A, 0x82, 0x79, 0x99 + BYTES 0x6E, 0xD9, 0xEB, 0xA1 + BYTES 0x8F, 0x1B, 0xBC, 0xDC + BYTES 0xA9, 0x53, 0xFD, 0x4E + // Right + BYTES 0x50, 0xA2, 0x8B, 0xE6 + BYTES 0x5C, 0x4D, 0xD1, 0x24 + BYTES 0x6D, 0x70, 0x3E, 0xF3 + BYTES 0x7A, 0x6D, 0x76, 0xE9 + BYTES 0x00, 0x00, 0x00, 0x00 + +// %macro load_s + +s_data: + // Left Round 1 + BYTES 11, 14, 15, 12 + BYTES 05, 08, 07, 09 + BYTES 11, 13, 14, 15 + BYTES 06, 07, 09, 08 + // Left Round 2 + BYTES 07, 06, 08, 13 + BYTES 11, 09, 07, 15 + BYTES 07, 12, 15, 09 + BYTES 11, 07, 13, 12 + // Left Round 3 + BYTES 11, 13, 06, 07 + BYTES 14, 09, 13, 15 + BYTES 14, 08, 13, 06 + BYTES 05, 12, 07, 05 + // Left Round 4 + BYTES 11, 12, 14, 15 + BYTES 14, 15, 09, 08 + BYTES 09, 14, 05, 06 + BYTES 08, 06, 05, 12 + // Left Round 5 + BYTES 09, 15, 05, 11 + BYTES 06, 08, 13, 12 + BYTES 05, 12, 13, 14 + BYTES 11, 08, 05, 06 + + // Right Round 1 + BYTES 08, 09, 09, 11 + BYTES 13, 15, 15, 05 + BYTES 07, 07, 08, 11 + BYTES 14, 14, 12, 06 + // Right Round 2 + BYTES 09, 13, 15, 07 + BYTES 12, 08, 09, 11 + BYTES 07, 07, 12, 07 + BYTES 06, 15, 13, 11 + // Right Round 3 + BYTES 09, 07, 15, 11 + BYTES 08, 06, 06, 14 + BYTES 12, 13, 05, 14 + BYTES 13, 13, 07, 05 + // Right Round 4 + BYTES 15, 05, 08, 11 + BYTES 14, 14, 06, 14 + BYTES 06, 09, 12, 09 + BYTES 12, 05, 15, 08 + // Right Round 5 + BYTES 08, 05, 12, 09 + BYTES 12, 05, 14, 06 + BYTES 08, 13, 06, 05 + BYTES 15, 13, 11, 11 + + +// %macro load_r + +r_data: + // Left Round 0 + BYTES 00, 01, 02, 03 + BYTES 04, 05, 06, 07 + BYTES 08, 09, 10, 11 + BYTES 12, 13, 14, 15 + // Left Round 1 + BYTES 07, 04, 13, 01 + BYTES 10, 06, 15, 03 + BYTES 12, 00, 09, 05 + BYTES 02, 14, 11, 08 + // Left Round 2 + BYTES 03, 10, 14, 04 + BYTES 09, 15, 08, 01 + BYTES 02, 07, 00, 06 + BYTES 13, 11, 05, 12 + // Left Round 3 + BYTES 01, 09, 11, 10 + BYTES 00, 08, 12, 04 + BYTES 13, 03, 07, 15 + BYTES 14, 05, 06, 02 + // Left Round 4 + BYTES 04, 00, 05, 09 + BYTES 07, 12, 02, 10 + BYTES 14, 01, 03, 08 + BYTES 11, 06, 15, 13 + // Right Round 0 + BYTES 05, 14, 07, 00 + BYTES 09, 02, 11, 04 + BYTES 13, 06, 15, 08 + BYTES 01, 10, 03, 12 + // Right Round 1 + BYTES 06, 11, 03, 07 + BYTES 00, 13, 05, 10 + BYTES 14, 15, 08, 12 + BYTES 04, 09, 01, 02 + // Right Round 2 + BYTES 15, 05, 01, 03 + BYTES 07, 14, 06, 09 + BYTES 11, 08, 12, 02 + BYTES 10, 00, 04, 13 + // Right Round 3 + BYTES 08, 06, 04, 01 + BYTES 03, 11, 15, 00 + BYTES 05, 12, 02, 13 + BYTES 09, 07, 10, 14 + // Right Round 4 + BYTES 12, 15, 10, 04 + BYTES 01, 05, 08, 07 + BYTES 06, 02, 13, 14 + BYTES 00, 03, 09, 11 + \ No newline at end of file diff --git a/evm/src/cpu/kernel/asm/ripemd/hashloop.asm b/evm/src/cpu/kernel/asm/ripemd/hash.asm similarity index 91% rename from evm/src/cpu/kernel/asm/ripemd/hashloop.asm rename to evm/src/cpu/kernel/asm/ripemd/hash.asm index 242b7575..2f7cf77d 100644 --- a/evm/src/cpu/kernel/asm/ripemd/hashloop.asm +++ b/evm/src/cpu/kernel/asm/ripemd/hash.asm @@ -42,30 +42,31 @@ switch: loop: jumpdest - // stack: *stack, Fj, Kj, 16, n, retdest + // stack: *state, Fj, Kj, 16, n, retdest push 1 dup9 sub swap8 - // stack: n, *stack, Fj, Kj, 16, n-1, retdest + // stack: n, *state, Fj, Kj, 16, n-1, retdest %jumpi(cycle) - // stack: *stack, Fj, Kj, 16, -1, retdest + // stack: *state, Fj, Kj, 16, -1, retdest %stack (a, b, c, d, e, f, k, i, n, ret) -> (ret, a, b, c, d, e) - // stack: retdest, *stack + // stack: retdest, *state jump cycle: jumpdest - // stack: *stack, Fj, Kj, i, n, retdest + // stack: *state, Fj, Kj, i, n, retdest push 1 dup9 sub swap8 - // stack: i, *stack, Fj, Kj, i-1, n, retdest + // stack: i, *state, Fj, Kj, i-1, n, retdest %jumpi(R) - // stack: *stack, Fj, Kj, -1, n, retdest + // stack: *state, Fj, Kj, -1, n, retdest swap5 pop push Fj swap5 ---------------------------------------------------------------------TODO - // stack: *stack, Fj, Kj 16, n, retdest + // stack: *state, Fj, Kj 16, n, retdest swap6 pop push Kj swap6 ---------------------------------------------------------------------TODO - // stack: *stack, Fj, Kj 16, n, retdest + // stack: *state, Fj, Kj 16, n, retdest swap7 pop push 16 swap7 - // stack: *stack, Fj, Kj 16, n, retdest + // stack: *state, Fj, Kj 16, n, retdest %jump(loop) - +/// Note that a, b, c, d, e represent *state +/// /// def R(a, b, c, d, e, Fj, Kj, _sj, _rj, _X): /// a = u32(ROL(sj, u32(Fj(b, c, d) + a + X[rj] + Kj)) + e) /// c = ROL(10, c) @@ -164,3 +165,4 @@ mix: // stack: s3+l4+r0, s1+l2+r3, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2 swap3 // stack: retdest, s1+l2+r3, s2+l3+r4, s3+l4+r0, s4+l0+r1, s0+l1+r2 + jump From 3e671155cbc18e3f027386dced36291b552672de Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 25 Aug 2022 15:38:18 -0700 Subject: [PATCH 12/97] all but blocks --- evm/src/cpu/kernel/asm/ripemd/constants.asm | 63 +++-- evm/src/cpu/kernel/asm/ripemd/hash.asm | 258 +++++++++++------- evm/src/cpu/kernel/asm/ripemd/subroutines.asm | 134 ++++----- 3 files changed, 265 insertions(+), 190 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/constants.asm b/evm/src/cpu/kernel/asm/ripemd/constants.asm index ad20de9c..42997b7e 100644 --- a/evm/src/cpu/kernel/asm/ripemd/constants.asm +++ b/evm/src/cpu/kernel/asm/ripemd/constants.asm @@ -1,4 +1,10 @@ -// %macro load_K +%macro load_K + // stack: rnd + %mul_const(4) push K_data add + // stack: K_data + 4*rnd + %mload_kernel_code_u32 + // stack: K +%end_macro K_data: // Left @@ -14,20 +20,27 @@ K_data: BYTES 0x7A, 0x6D, 0x76, 0xE9 BYTES 0x00, 0x00, 0x00, 0x00 -// %macro load_s -s_data: +%macro load_s + // stack: box + push S_data add + // stack: S_data + box + %mload_kernel_code + // stack: s +%end_macro + +S_data: // Left Round 1 BYTES 11, 14, 15, 12 BYTES 05, 08, 07, 09 BYTES 11, 13, 14, 15 BYTES 06, 07, 09, 08 - // Left Round 2 + // Left Round 2 BYTES 07, 06, 08, 13 BYTES 11, 09, 07, 15 BYTES 07, 12, 15, 09 BYTES 11, 07, 13, 12 - // Left Round 3 + // Left Round 3 BYTES 11, 13, 06, 07 BYTES 14, 09, 13, 15 BYTES 14, 08, 13, 06 @@ -37,7 +50,7 @@ s_data: BYTES 14, 15, 09, 08 BYTES 09, 14, 05, 06 BYTES 08, 06, 05, 12 - // Left Round 5 + // Left Round 5 BYTES 09, 15, 05, 11 BYTES 06, 08, 13, 12 BYTES 05, 12, 13, 14 @@ -48,77 +61,83 @@ s_data: BYTES 13, 15, 15, 05 BYTES 07, 07, 08, 11 BYTES 14, 14, 12, 06 - // Right Round 2 + // Right Round 2 BYTES 09, 13, 15, 07 BYTES 12, 08, 09, 11 BYTES 07, 07, 12, 07 BYTES 06, 15, 13, 11 - // Right Round 3 + // Right Round 3 BYTES 09, 07, 15, 11 BYTES 08, 06, 06, 14 BYTES 12, 13, 05, 14 BYTES 13, 13, 07, 05 - // Right Round 4 + // Right Round 4 BYTES 15, 05, 08, 11 BYTES 14, 14, 06, 14 BYTES 06, 09, 12, 09 BYTES 12, 05, 15, 08 - // Right Round 5 + // Right Round 5 BYTES 08, 05, 12, 09 BYTES 12, 05, 14, 06 BYTES 08, 13, 06, 05 BYTES 15, 13, 11, 11 -// %macro load_r +%macro load_r + // stack: box + push R_data add + // stack: R_data + box + %mload_kernel_code + // stack: r +%end_macro -r_data: - // Left Round 0 +R_data: + // Left Round 1 BYTES 00, 01, 02, 03 BYTES 04, 05, 06, 07 BYTES 08, 09, 10, 11 BYTES 12, 13, 14, 15 - // Left Round 1 + // Left Round 2 BYTES 07, 04, 13, 01 BYTES 10, 06, 15, 03 BYTES 12, 00, 09, 05 BYTES 02, 14, 11, 08 - // Left Round 2 + // Left Round 3 BYTES 03, 10, 14, 04 BYTES 09, 15, 08, 01 BYTES 02, 07, 00, 06 BYTES 13, 11, 05, 12 - // Left Round 3 + // Left Round 4 BYTES 01, 09, 11, 10 BYTES 00, 08, 12, 04 BYTES 13, 03, 07, 15 BYTES 14, 05, 06, 02 - // Left Round 4 + // Left Round 5 BYTES 04, 00, 05, 09 BYTES 07, 12, 02, 10 BYTES 14, 01, 03, 08 BYTES 11, 06, 15, 13 - // Right Round 0 + // Right Round 1 BYTES 05, 14, 07, 00 BYTES 09, 02, 11, 04 BYTES 13, 06, 15, 08 BYTES 01, 10, 03, 12 - // Right Round 1 + // Right Round 2 BYTES 06, 11, 03, 07 BYTES 00, 13, 05, 10 BYTES 14, 15, 08, 12 BYTES 04, 09, 01, 02 - // Right Round 2 + // Right Round 3 BYTES 15, 05, 01, 03 BYTES 07, 14, 06, 09 BYTES 11, 08, 12, 02 BYTES 10, 00, 04, 13 - // Right Round 3 + // Right Round 4 BYTES 08, 06, 04, 01 BYTES 03, 11, 15, 00 BYTES 05, 12, 02, 13 BYTES 09, 07, 10, 14 - // Right Round 4 + // Right Round 5 BYTES 12, 15, 10, 04 BYTES 01, 05, 08, 07 BYTES 06, 02, 13, 14 diff --git a/evm/src/cpu/kernel/asm/ripemd/hash.asm b/evm/src/cpu/kernel/asm/ripemd/hash.asm index 2f7cf77d..85d12f0d 100644 --- a/evm/src/cpu/kernel/asm/ripemd/hash.asm +++ b/evm/src/cpu/kernel/asm/ripemd/hash.asm @@ -10,120 +10,35 @@ /// stateR = state /// stateR = loop(stateR) /// -/// state = mix(state, stateL, stateR) +/// return mix(state, stateL, stateR) +/// +/// +/// def mix(*stateR, *stateL, *state): +/// return +/// u32(state[1] + stateL[2] + stateR[3]), +/// u32(state[2] + stateL[3] + stateR[4]), +/// u32(state[3] + stateL[4] + stateR[0]), +/// u32(state[4] + stateL[0] + stateR[1]), +/// u32(state[0] + stateL[1] + stateR[2]) +/// +/// In mix, we denote state[i], stateL[i], stateR[i] by si, li, ri global hash: jumpdest // stack: *state, retdest - push switch push 5 push 16 push K0 push F0 - // stack: F0, K0, 16, 5, switch, *state, retdest - dup10 dup10 dup10 dup10 dup10 - // stack: *state, F0, K0, 16, 5, switch, *state, retdest + push switch push 0 push 5 push 16 push 0 push 0 + // stack: 0, 0, 16, 5, 1, switch, *state, retdest + dup11 dup11 dup11 dup11 dup11 + // stack: *state, 0, 0, 16, 5, 1, switch, *state, retdest %jump(loop) switch: jumpdest // stack: *stateL, *state, retdest - push mix push 5 push 16 - // stack: F0, K0, 16, 5, mix, *stateL, *state, retdest - dup15 dup15 dup15 dup15 dup15 - // stack: *state, F0, K0, 16, 5, mix, *stateL, *state, retdest + push mix push 1 push 5 push 16 push 0 push 0 + // stack: 0, 0, 16, 5, 0, mix, *stateL, *state, retdest + dup16 dup16 dup16 dup16 dup16 + // stack: *state, 0, 0, 16, 5, 0, mix, *stateL, *state, retdest %jump(loop) - - -/// def loop(*state, Fj, Kj): -/// while n: -/// while i: -/// R(*state, Fj, Kj) -/// i -= 1 -/// i = 16 -/// n -= 1 -/// F = Fs[n] -/// K = Ks[n] - -loop: - jumpdest - // stack: *state, Fj, Kj, 16, n, retdest - push 1 dup9 sub swap8 - // stack: n, *state, Fj, Kj, 16, n-1, retdest - %jumpi(cycle) - // stack: *state, Fj, Kj, 16, -1, retdest - %stack (a, b, c, d, e, f, k, i, n, ret) -> (ret, a, b, c, d, e) - // stack: retdest, *state - jump -cycle: - jumpdest - // stack: *state, Fj, Kj, i, n, retdest - push 1 dup9 sub swap8 - // stack: i, *state, Fj, Kj, i-1, n, retdest - %jumpi(R) - // stack: *state, Fj, Kj, -1, n, retdest - swap5 pop push Fj swap5 ---------------------------------------------------------------------TODO - // stack: *state, Fj, Kj 16, n, retdest - swap6 pop push Kj swap6 ---------------------------------------------------------------------TODO - // stack: *state, Fj, Kj 16, n, retdest - swap7 pop push 16 swap7 - // stack: *state, Fj, Kj 16, n, retdest - %jump(loop) - -/// Note that a, b, c, d, e represent *state -/// -/// def R(a, b, c, d, e, Fj, Kj, _sj, _rj, _X): -/// a = u32(ROL(sj, u32(Fj(b, c, d) + a + X[rj] + Kj)) + e) -/// c = ROL(10, c) -/// return e, a, b, c, d, Fj, Kj - -R: - jumpdest - // stack: a, b, c, d, e, Fj, Kj - push after_F dup5 dup5 dup5 dup10 - // stack: Fj, b, c, d, after_F, a, b, c, d, e, Fj, Kj - jump -after_F: - // stack: Fj(b, c, d), a, b, c, d, e, Fj, Kj - add - // stack: Fj(b, c, d) + a, b, c, d, e, Fj, Kj - push X[rj]---------------------------------------------------------------------TODO - // stack: X[rj], Fj(b, c, d) + a, b, c, d, e, Fj, Kj - add - // stack: X[rj] + Fj(b, c, d) + a, b, c, d, e, Fj, Kj - dup7 - // stack: Kj, X[rj] + Fj(b, c, d) + a, b, c, d, e, Fj, Kj - add %u32 - // stack: Kj + X[rj] + Fj(b, c, d) + a, b, c, d, e, Fj, Kj - push sj------------------------------------------------------------------------TODO - // stack: sj, Kj + X[rj] + Fj(b, c, d) + a, b, c, d, e, Fj, Kj - %jump(ROL) - // stack: ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, c, d, e, Fj, Kj - dup5 - // stack: e, ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, c, d, e, Fj, Kj - add %u32 - // stack: e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, c, d, e, Fj, Kj - swap1 - // stack: b, e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), c, d, e, Fj, Kj - swap2 - // stack: c, e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, d, e, Fj, Kj - push 10 - // stack: 10, c, b, e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), d, e, Fj, Kj - %jump(ROL) - // stack: ROL(10, c), e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, d, e, Fj, Kj - swap4 - // stack: d, e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, ROL(10,c), e, Fj, Kj - swap5 - // stack: e, e + ROL(sj, Kj + X[rj] + Fj(b, c, d) + a), b, ROL(10, c), d, Fj, Kj - %jump(cycle) - - -/// def mix(*stateR, *stateL, *state): -/// return [ -/// u32(state[1] + stateL[2] + stateR[3]), -/// u32(state[2] + stateL[3] + stateR[4]), -/// u32(state[3] + stateL[4] + stateR[0]), -/// u32(state[4] + stateL[0] + stateR[1]), -/// u32(state[0] + stateL[1] + stateR[2]) -/// ] -/// -/// Note that we denote state[i], stateL[i], stateR[i] by si, li, ri - mix: jumpdest // stack: r0, r1, r2, r3, r4, l0, l1, l2, l3, l4, s0, s1, s2, s3, s4, retdest @@ -166,3 +81,136 @@ mix: swap3 // stack: retdest, s1+l2+r3, s2+l3+r4, s3+l4+r0, s4+l0+r1, s0+l1+r2 jump + + +/// def loop(*state): +/// while rounds: +/// update_round_vars() +/// round(*state, F, K, rounds, sides) +/// +/// def update_round_vars(): +/// F = load_F(sides, rounds) +/// K = load_K(sides, rounds) +/// +/// def round(*state, rounds, sides): +/// while boxes: +/// box(*state, F, K) +/// boxes -= 1 +/// boxes = 16 +/// rounds -= 1 + + +loop: + jumpdest + // stack: *state, F, K, 16, rounds, sides, retdest + dup9 + // stack: round, *state, F, K, 16, rounds, sides, retdest + %jumpi(update_round_vars) + // stack: *state, F, K, 16, 0, sides, retdest + %stack (a, b, c, d, e, F, K, boxes, rounds, sides, retdest) -> (retdest, a, b, c, d, e) + // stack: retdest, *state + jump +update_round_vars: + jumpdest + // stack: *state, F , K , 16, rounds, sides, retdest + dup10 dup10 %get_round up1 + // stack: rnd, rnd, *state, F , K , 16, rounds, sides, retdest + swap7 pop %load_F swap6 + // stack: rnd, rnd, *state, F', K , 16, rounds, sides, retdest + swap8 pop %load_K swap7 pop + // stack: *state, F', K', 16, rounds, sides, retdest + %jump(round) +round: + jumpdest + // stack: *state, F, K, boxes, rounds , sides, retdest + dup8 + // stack: boxes, *state, F, K, boxes, rounds , sides, retdest + %jumpi(box) + // stack: *state, F, K, 0, rounds , sides, retdest + swap7 pop push 16 swap7 + // stack: *state, F, K, 16, rounds , sides, retdest + push 1 dup10 sub swap9 pop + // stack: *state, F, K, 16, rounds-1, sides, retdest + %jump(loop) + + +/// Note that we unpack *state to a, b, c, d, e +/// All additions are u32 +/// +/// def box(a, b, c, d, e, F, K): +/// +/// box = get_box_index(sides, rounds, boxes) +/// a += F(b, c, d) +/// r = load_r(box) +/// x = load_block(r) +/// a += x + K +/// s = load_s(box) +/// a = ROL(s, a) +/// a += e +/// c = ROL(10, c) +/// +/// return e, a, b, c, d, F, K + + +box: + jumpdest + // stack: a, b, c, d, e, F, K, boxes, rounds, sides + push after_F dup5 dup5 dup5 dup10 + // stack: F, b, c, d, pre_rol, a, b, c, d, e, F, K, boxes, rounds, sides + jump +pre_rol: + jumpdest + // stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides + add + // stack: a, b, c, d, e, F, K, boxes, rounds, sides + %get_box + // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides + dup1 %load_r + // stack: r, box, a, b, c, d, e, F, K, boxes, rounds, sides + %load_X ------------------------------------------------------------------------TODO + // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides + swap1 swap2 + // stack: a, x, box, b, c, d, e, F, K, boxes, rounds, sides + add dup8 add %u32 + // stack: a, box, b, c, d, e, F, K, boxes, rounds, sides + push mid_rol swap2 + // stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides + %load_s + // stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides + %jump(ROL) +mid_rol: + jumpdest + // stack: a, b, c, d, e, F, K, boxes, rounds, sides + dup5 + // stack: e, a, b, c, d, e, F, K, boxes, rounds, sides + add %u32 + // stack: a, b, c, d, e, F, K, boxes, rounds, sides + %stack (a, b, c) -> (10, c, post_rol, a, b) + // stack: 10, c, post_rol, b, a, d, e, F, K, boxes, rounds, sides + %jump(ROL) +post_rol: + jumpdest + // stack: c, a, b, d, e, F, K, boxes, rounds, sides + swap4 + // stack: d, a, b, c, e, F, K, boxes, rounds, sides + swap5 + // stack: e, a, b, c, d, F, K, boxes, rounds, sides + swap7 push 1 swap1 sub swap7 + // stack: e, a, b, c, d, F, K, boxes-1, rounds, sides + %jump(round) + + +%macro get_round + // stack: sides, rounds + %mul_const(5) push 10 sub sub + // stack: 10 - 5*sides - rounds +%end_macro + + +%macro get_box + // stack: *7_args, boxes, rounds, sides + dup10 %mul_const(80) dup10 %mul_const(16) dup10 + // stack: boxes , 16*rounds , 80*sides, *7_args, boxes, rounds, sides + push 160 sub sub sub + // stack: 160 - boxes - 16*rounds - 80*sides, *7_args, boxes, rounds, sides +%end_macro diff --git a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm index ab34a8d6..dab8c4bf 100644 --- a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm +++ b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm @@ -1,34 +1,52 @@ -/// def ROL(n, x): +/// def rol(n, x): /// return (u32(x << n)) | (x >> (32 - n)) -global ROL: +global rol: jumpdest - // stack: n, x, retdest - swap1 - // stack: x, n, retdest - dup1 - // stack: x, x, n, retdest - dup3 - // stack: n, x, x, n, retdest - push 32 - // stack: 32, n, x, x, n, retdest - sub - // stack: 32-n, x, x, n, retdest + // stack: n, x, retdest + swap1 dup1 dup3 + // stack: n, x, x, n, retdest + push 32 sub + // stack: 32-n, x, x, n, retdest shr - // stack: x >> (32-n), x, n, retdest + // stack: x >> (32-n), x, n, retdest swap2 - // stack: n, x, x >> (32-n), retdest + // stack: n, x, x >> (32-n), retdest shl - // stack: x << n, x >> (32-n), retdest - push 0xffffffff - // stack: 0xffffffff, (x << n), x >> (32-n), retdest - and - // stack: (x << n) & 0xffffffff, x >> (32-n), retdest + // stack: x << n, x >> (32-n), retdest + %u32 + // stack: u32(x << n), x >> (32-n), retdest or - // stack: ((x << n) & 0xffffffff) | (x >> (32-n)), retdest - swap1 - // stack: retdest, ((x << n) & 0xffffffff) | (x >> (32-n)) - jump + // stack: u32(x << n) | (x >> (32-n)), retdest + swap1 jump + + +%macro load_F: + push 0 + %this_F(0,F0) + %this_F(1,F1) + %this_F(2,F2) + %this_F(3,F3) + %this_F(4,F4) + %this_F(5,F4) + %this_F(6,F3) + %this_F(7,F2) + %this_F(8,F1) + %this_F(9,F0) +%endmacro + + +%macro this_F(i, F) + // stack: acc, rnd + dup2 + // stack: rnd, acc, rnd + %eq_const(i) + // stack: rnd==i, acc, j + %mul_const(result) + // stack: (rnd==i)*F, acc, rnd + add + acc + (rnd==j)*result, rnd +%endmacro /// def F0(x, y, z): @@ -36,14 +54,12 @@ global ROL: global F0: jumpdest - // stack: x, y, z, retdest + // stack: x , y , z, retdest xor - // stack: x ^ y, z, retdest + // stack: x ^ y , z, retdest xor // stack: x ^ y ^ z, retdest - swap1 - // stack: retdest, x ^ y ^ z - jump + swap1 jump /// def F1(x, y, z): @@ -51,26 +67,24 @@ global F0: global F1: jumpdest - // stack: x, y, z, retdest + // stack: x, y, z, retdest dup1 - // stack: x, x, y, z, retdest + // stack: x, x, y, z, retdest swap2 - // stack: y, x, x, z, retdest + // stack: y, x, x, z, retdest and - // stack: y & x, x, z, retdest + // stack: y & x, x, z, retdest swap2 - // stack: z, x, y & x, retdest + // stack: z, x, y & x, retdest swap1 - // stack: x, z, y & x, retdest + // stack: x, z, y & x, retdest %not_32 - // stack: ~x, z, y & x, retdest + // stack: ~x, z, y & x, retdest and - // stack: ~x & z, y & x, retdest + // stack: ~x & z, y & x, retdest or // stack: (~x & z) | (y & x), retdest - swap1 - // stack: retdest, (~x & z) | (y & x) - jump + swap1 jump /// def F2(x, y, z): @@ -78,18 +92,16 @@ global F1: global F2: jumpdest - // stack: x, y, z, retdest + // stack: x, y, z, retdest swap1 - // stack: y, x, z, retdest + // stack: y, x, z, retdest %not_32 - // stack: ~y, x, z, retdest + // stack: ~y, x, z, retdest or - // stack: ~y | x, z, retdest + // stack: ~y | x, z, retdest xor // stack: (~y | x) ^ z, retdest - swap1 - // stack: retdest, (~y | x) ^ z - jump + swap1 jump /// def F3(x, y, z): @@ -97,22 +109,20 @@ global F2: global F3: jumpdest - // stack: x, y, z, retdest + // stack: x, y, z, retdest dup3 - // stack: z, x, y, z, retdest + // stack: z, x, y, z, retdest and - // stack: z & x, y, z, retdest + // stack: z & x, y, z, retdest swap2 - // stack: z, y, z & x, retdest + // stack: z, y, z & x, retdest %not_32 - // stack: ~z, y, z & x, retdest + // stack: ~z, y, z & x, retdest and - // stack: ~z & y, z & x, retdest + // stack: ~z & y, z & x, retdest or // stack: (~z & y) | (z & x), retdest - swap1 - // stack: retdest, (~z & y) | (z & x) - jump + swap1 jump /// def F4(x, y, z): @@ -120,15 +130,13 @@ global F3: global F4: jumpdest - // stack: x, y, z, retdest + // stack: x, y, z, retdest swap2 - // stack: z, y, x, retdest + // stack: z, y, x, retdest %not_32 - // stack: ~z, y, x, retdest + // stack: ~z, y, x, retdest or - // stack: ~z | y, x, retdest + // stack: ~z | y, x, retdest xor // stack: (~z | y) ^ x, retdest - swap1 - // stack: retdest, (~z | y) ^ x - jump + swap1 jump From e6e6099cec3b34625b533c5c623cab1797056501 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 25 Aug 2022 16:18:45 -0700 Subject: [PATCH 13/97] finished hash loop --- evm/src/cpu/kernel/asm/ripemd/hash.asm | 18 +++++++++--------- evm/src/cpu/kernel/asm/ripemd/subroutines.asm | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/hash.asm b/evm/src/cpu/kernel/asm/ripemd/hash.asm index 85d12f0d..c15dec3e 100644 --- a/evm/src/cpu/kernel/asm/ripemd/hash.asm +++ b/evm/src/cpu/kernel/asm/ripemd/hash.asm @@ -113,9 +113,9 @@ loop: update_round_vars: jumpdest // stack: *state, F , K , 16, rounds, sides, retdest - dup10 dup10 %get_round up1 + dup10 dup10 %get_round dup1 // stack: rnd, rnd, *state, F , K , 16, rounds, sides, retdest - swap7 pop %load_F swap6 + swap7 pop %push_F swap7 // stack: rnd, rnd, *state, F', K , 16, rounds, sides, retdest swap8 pop %load_K swap7 pop // stack: *state, F', K', 16, rounds, sides, retdest @@ -139,7 +139,7 @@ round: /// /// def box(a, b, c, d, e, F, K): /// -/// box = get_box_index(sides, rounds, boxes) +/// box = get_box(sides, rounds, boxes) /// a += F(b, c, d) /// r = load_r(box) /// x = load_block(r) @@ -163,11 +163,11 @@ pre_rol: // stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides add // stack: a, b, c, d, e, F, K, boxes, rounds, sides - %get_box + %get_box_from_stack // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides dup1 %load_r // stack: r, box, a, b, c, d, e, F, K, boxes, rounds, sides - %load_X ------------------------------------------------------------------------TODO + %mload_kernel(@SEGMENT_KERNEL_GENERAL) // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides swap1 swap2 // stack: a, x, box, b, c, d, e, F, K, boxes, rounds, sides @@ -190,11 +190,11 @@ mid_rol: %jump(ROL) post_rol: jumpdest - // stack: c, a, b, d, e, F, K, boxes, rounds, sides + // stack: c, a, b, d, e, F, K, boxes , rounds, sides swap4 - // stack: d, a, b, c, e, F, K, boxes, rounds, sides + // stack: d, a, b, c, e, F, K, boxes , rounds, sides swap5 - // stack: e, a, b, c, d, F, K, boxes, rounds, sides + // stack: e, a, b, c, d, F, K, boxes , rounds, sides swap7 push 1 swap1 sub swap7 // stack: e, a, b, c, d, F, K, boxes-1, rounds, sides %jump(round) @@ -207,7 +207,7 @@ post_rol: %end_macro -%macro get_box +%macro get_box_from_stack // stack: *7_args, boxes, rounds, sides dup10 %mul_const(80) dup10 %mul_const(16) dup10 // stack: boxes , 16*rounds , 80*sides, *7_args, boxes, rounds, sides diff --git a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm index dab8c4bf..0620a1de 100644 --- a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm +++ b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm @@ -21,7 +21,7 @@ global rol: swap1 jump -%macro load_F: +%macro push_F: push 0 %this_F(0,F0) %this_F(1,F1) @@ -42,10 +42,10 @@ global rol: // stack: rnd, acc, rnd %eq_const(i) // stack: rnd==i, acc, j - %mul_const(result) + %mul_const(F) // stack: (rnd==i)*F, acc, rnd add - acc + (rnd==j)*result, rnd + acc + (rnd==j)*F, rnd %endmacro From 7ed78c22a314d23ee5cbff0aa975b7552c1a1f0e Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 8 Sep 2022 15:47:18 -0700 Subject: [PATCH 14/97] minor errors --- evm/src/cpu/kernel/asm/ripemd/hash.asm | 6 +++--- evm/src/cpu/kernel/asm/util/basic_macros.asm | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/hash.asm b/evm/src/cpu/kernel/asm/ripemd/hash.asm index c15dec3e..c3489a5b 100644 --- a/evm/src/cpu/kernel/asm/ripemd/hash.asm +++ b/evm/src/cpu/kernel/asm/ripemd/hash.asm @@ -155,7 +155,7 @@ round: box: jumpdest // stack: a, b, c, d, e, F, K, boxes, rounds, sides - push after_F dup5 dup5 dup5 dup10 + push pre_rol dup5 dup5 dup5 dup10 // stack: F, b, c, d, pre_rol, a, b, c, d, e, F, K, boxes, rounds, sides jump pre_rol: @@ -204,7 +204,7 @@ post_rol: // stack: sides, rounds %mul_const(5) push 10 sub sub // stack: 10 - 5*sides - rounds -%end_macro +%endmacro %macro get_box_from_stack @@ -213,4 +213,4 @@ post_rol: // stack: boxes , 16*rounds , 80*sides, *7_args, boxes, rounds, sides push 160 sub sub sub // stack: 160 - boxes - 16*rounds - 80*sides, *7_args, boxes, rounds, sides -%end_macro +%endmacro diff --git a/evm/src/cpu/kernel/asm/util/basic_macros.asm b/evm/src/cpu/kernel/asm/util/basic_macros.asm index 5b88c51f..edac2b75 100644 --- a/evm/src/cpu/kernel/asm/util/basic_macros.asm +++ b/evm/src/cpu/kernel/asm/util/basic_macros.asm @@ -222,6 +222,7 @@ // stack: 0xffffffff, x and // stack: 0xffffffff & x +%endmacro %macro not_32 // stack: x From 3bfb994cbd74d9c906f8e2c34f83f954ebb0439f Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 8 Sep 2022 19:35:37 -0700 Subject: [PATCH 15/97] fix errors --- evm/src/cpu/kernel/asm/ripemd/hash.asm | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/hash.asm b/evm/src/cpu/kernel/asm/ripemd/hash.asm index c3489a5b..e0840657 100644 --- a/evm/src/cpu/kernel/asm/ripemd/hash.asm +++ b/evm/src/cpu/kernel/asm/ripemd/hash.asm @@ -145,9 +145,9 @@ round: /// x = load_block(r) /// a += x + K /// s = load_s(box) -/// a = ROL(s, a) +/// a = rol(s, a) /// a += e -/// c = ROL(10, c) +/// c = rol(10, c) /// /// return e, a, b, c, d, F, K @@ -177,7 +177,7 @@ pre_rol: // stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides %load_s // stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides - %jump(ROL) + %jump(rol) mid_rol: jumpdest // stack: a, b, c, d, e, F, K, boxes, rounds, sides @@ -185,15 +185,15 @@ mid_rol: // stack: e, a, b, c, d, e, F, K, boxes, rounds, sides add %u32 // stack: a, b, c, d, e, F, K, boxes, rounds, sides - %stack (a, b, c) -> (10, c, post_rol, a, b) + SWAP1 SWAP2 PUSH post_rol SWAP1 PUSH 10 // stack: 10, c, post_rol, b, a, d, e, F, K, boxes, rounds, sides - %jump(ROL) + %jump(rol) post_rol: jumpdest // stack: c, a, b, d, e, F, K, boxes , rounds, sides - swap4 + swap3 // stack: d, a, b, c, e, F, K, boxes , rounds, sides - swap5 + swap4 // stack: e, a, b, c, d, F, K, boxes , rounds, sides swap7 push 1 swap1 sub swap7 // stack: e, a, b, c, d, F, K, boxes-1, rounds, sides From 9561dceb391e4a2c00196f9f2f43dca5f87ce340 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 8 Sep 2022 20:16:51 -0700 Subject: [PATCH 16/97] fix --- evm/src/cpu/kernel/asm/ripemd/hash.asm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/hash.asm b/evm/src/cpu/kernel/asm/ripemd/hash.asm index e0840657..30b1935e 100644 --- a/evm/src/cpu/kernel/asm/ripemd/hash.asm +++ b/evm/src/cpu/kernel/asm/ripemd/hash.asm @@ -26,7 +26,7 @@ global hash: jumpdest // stack: *state, retdest - push switch push 0 push 5 push 16 push 0 push 0 + push switch push 1 push 5 push 16 push 0 push 0 // stack: 0, 0, 16, 5, 1, switch, *state, retdest dup11 dup11 dup11 dup11 dup11 // stack: *state, 0, 0, 16, 5, 1, switch, *state, retdest @@ -113,7 +113,7 @@ loop: update_round_vars: jumpdest // stack: *state, F , K , 16, rounds, sides, retdest - dup10 dup10 %get_round dup1 + dup9 dup11 %get_round dup1 // stack: rnd, rnd, *state, F , K , 16, rounds, sides, retdest swap7 pop %push_F swap7 // stack: rnd, rnd, *state, F', K , 16, rounds, sides, retdest From 64e9f6f0fbd5c3cbad6dfe655303663ee46df382 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Fri, 9 Sep 2022 17:38:36 -0700 Subject: [PATCH 17/97] final error and formatting --- evm/src/cpu/kernel/asm/ripemd/hash.asm | 169 +++++++++++++++++-------- 1 file changed, 115 insertions(+), 54 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/hash.asm b/evm/src/cpu/kernel/asm/ripemd/hash.asm index 30b1935e..5afd65a4 100644 --- a/evm/src/cpu/kernel/asm/ripemd/hash.asm +++ b/evm/src/cpu/kernel/asm/ripemd/hash.asm @@ -24,63 +24,81 @@ /// In mix, we denote state[i], stateL[i], stateR[i] by si, li, ri global hash: - jumpdest + JUMPDEST // stack: *state, retdest - push switch push 1 push 5 push 16 push 0 push 0 + PUSH switch + PUSH 1 + PUSH 5 + PUSH 16 + PUSH 0 + PUSH 0 // stack: 0, 0, 16, 5, 1, switch, *state, retdest - dup11 dup11 dup11 dup11 dup11 + DUP11 + DUP11 + DUP11 + DUP11 + DUP11 // stack: *state, 0, 0, 16, 5, 1, switch, *state, retdest %jump(loop) switch: - jumpdest + JUMPDEST // stack: *stateL, *state, retdest - push mix push 1 push 5 push 16 push 0 push 0 + PUSH mix + PUSH 0 + PUSH 5 + PUSH 16 + PUSH 0 + PUSH 0 // stack: 0, 0, 16, 5, 0, mix, *stateL, *state, retdest - dup16 dup16 dup16 dup16 dup16 + DUP16 + DUP16 + DUP16 + DUP16 + DUP16 // stack: *state, 0, 0, 16, 5, 0, mix, *stateL, *state, retdest %jump(loop) mix: - jumpdest + JUMPDEST // stack: r0, r1, r2, r3, r4, l0, l1, l2, l3, l4, s0, s1, s2, s3, s4, retdest - swap10 + SWAP10 // stack: s0, r1, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, retdest - swap1 + SWAP1 // stack: r1, s0, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, retdest - swap6 + SWAP6 // stack: l1, s0, r2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, retdest %add3_32 // stack: s0+l1+r2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, retdest - swap13 + SWAP13 // stack: retdest, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, s0+l1+r2 - swap11 + SWAP11 // stack: s3, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, retdest, s4, s0+l1+r2 - swap10 + SWAP10 // stack: s2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s3, retdest, s4, s0+l1+r2 - swap1 + SWAP1 // stack: r3, s2, r4, l0, r1, l2, l3, l4, r0, s1, s3, retdest, s4, s0+l1+r2 - swap6 + SWAP6 // stack: l3, s2, r4, l0, r1, l2, r3, l4, r0, s1, s3, retdest, s4, s0+l1+r2 %add3_32 // stack: s2+l3+r4, l0, r1, l2, r3, l4, r0, s1, s3, retdest, s4, s0+l1+r2 - swap8 + SWAP8 // stack: s3, l0, r1, l2, r3, l4, r0, s1, s2+l3+r4, retdest, s4, s0+l1+r2 - swap10 + SWAP10 // stack: s4, l0, r1, l2, r3, l4, r0, s1, s2+l3+r4, retdest, s3, s0+l1+r2 %add3_32 // stack: s4+l0+r1, l2, r3, l4, r0, s1, s2+l3+r4, retdest, s3, s0+l1+r2 - swap8 + SWAP8 // stack: s3, l2, r3, l4, r0, s1, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2 - swap5 + SWAP5 // stack: s1, l2, r3, l4, r0, s3, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2 %add3_32 // stack: s1+l2+r3, l4, r0, s3, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2 - swap3 + SWAP3 // stack: s3, l4, r0, s1+l2+r3, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2 %add3_32 // stack: s3+l4+r0, s1+l2+r3, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2 - swap3 + SWAP3 // stack: retdest, s1+l2+r3, s2+l3+r4, s3+l4+r0, s4+l0+r1, s0+l1+r2 - jump + JUMP /// def loop(*state): @@ -101,35 +119,52 @@ mix: loop: - jumpdest + JUMPDEST // stack: *state, F, K, 16, rounds, sides, retdest - dup9 + DUP9 // stack: round, *state, F, K, 16, rounds, sides, retdest %jumpi(update_round_vars) // stack: *state, F, K, 16, 0, sides, retdest %stack (a, b, c, d, e, F, K, boxes, rounds, sides, retdest) -> (retdest, a, b, c, d, e) // stack: retdest, *state - jump + JUMP update_round_vars: - jumpdest + JUMPDEST // stack: *state, F , K , 16, rounds, sides, retdest - dup9 dup11 %get_round dup1 + DUP9 + DUP11 + %get_round + DUP1 // stack: rnd, rnd, *state, F , K , 16, rounds, sides, retdest - swap7 pop %push_F swap7 + SWAP7 + POP + %push_F + SWAP7 // stack: rnd, rnd, *state, F', K , 16, rounds, sides, retdest - swap8 pop %load_K swap7 pop + SWAP8 + POP + %load_K + SWAP7 + POP // stack: *state, F', K', 16, rounds, sides, retdest %jump(round) round: - jumpdest + JUMPDEST // stack: *state, F, K, boxes, rounds , sides, retdest - dup8 + DUP8 // stack: boxes, *state, F, K, boxes, rounds , sides, retdest %jumpi(box) // stack: *state, F, K, 0, rounds , sides, retdest - swap7 pop push 16 swap7 + SWAP7 + POP + PUSH 16 + SWAP7 // stack: *state, F, K, 16, rounds , sides, retdest - push 1 dup10 sub swap9 pop + PUSH 1 + DUP10 + SUB + SWAP9 + POP // stack: *state, F, K, 16, rounds-1, sides, retdest %jump(loop) @@ -153,64 +188,90 @@ round: box: - jumpdest + JUMPDEST // stack: a, b, c, d, e, F, K, boxes, rounds, sides - push pre_rol dup5 dup5 dup5 dup10 + PUSH pre_rol + DUP5 + DUP5 + DUP5 + DUP10 // stack: F, b, c, d, pre_rol, a, b, c, d, e, F, K, boxes, rounds, sides - jump + JUMP pre_rol: - jumpdest + JUMPDEST // stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides - add + ADD // stack: a, b, c, d, e, F, K, boxes, rounds, sides %get_box_from_stack // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides - dup1 %load_r + DUP1 + %load_r // stack: r, box, a, b, c, d, e, F, K, boxes, rounds, sides %mload_kernel(@SEGMENT_KERNEL_GENERAL) // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides - swap1 swap2 + SWAP1 + SWAP2 // stack: a, x, box, b, c, d, e, F, K, boxes, rounds, sides - add dup8 add %u32 + ADD + DUP8 + ADD + %u32 // stack: a, box, b, c, d, e, F, K, boxes, rounds, sides - push mid_rol swap2 + PUSH mid_rol + SWAP2 // stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides %load_s // stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides %jump(rol) mid_rol: - jumpdest + JUMPDEST // stack: a, b, c, d, e, F, K, boxes, rounds, sides - dup5 + DUP5 // stack: e, a, b, c, d, e, F, K, boxes, rounds, sides - add %u32 + ADD + %u32 // stack: a, b, c, d, e, F, K, boxes, rounds, sides - SWAP1 SWAP2 PUSH post_rol SWAP1 PUSH 10 + SWAP1 + SWAP2 + PUSH post_rol + SWAP1 + PUSH 10 // stack: 10, c, post_rol, b, a, d, e, F, K, boxes, rounds, sides %jump(rol) post_rol: - jumpdest + JUMPDEST // stack: c, a, b, d, e, F, K, boxes , rounds, sides - swap3 + SWAP3 // stack: d, a, b, c, e, F, K, boxes , rounds, sides - swap4 + SWAP4 // stack: e, a, b, c, d, F, K, boxes , rounds, sides - swap7 push 1 swap1 sub swap7 + SWAP7 + PUSH 1 + SWAP1 + SUB + SWAP7 // stack: e, a, b, c, d, F, K, boxes-1, rounds, sides %jump(round) %macro get_round // stack: sides, rounds - %mul_const(5) push 10 sub sub + %mul_const(5) PUSH 10 sub sub // stack: 10 - 5*sides - rounds %endmacro %macro get_box_from_stack // stack: *7_args, boxes, rounds, sides - dup10 %mul_const(80) dup10 %mul_const(16) dup10 + DUP10 + %mul_const(80) + DUP10 + %mul_const(16) + DUP10 // stack: boxes , 16*rounds , 80*sides, *7_args, boxes, rounds, sides - push 160 sub sub sub - // stack: 160 - boxes - 16*rounds - 80*sides, *7_args, boxes, rounds, sides + PUSH 176 + SUB + SUB + SUB + // stack: 176 - boxes - 16*rounds - 80*sides, *7_args, boxes, rounds, sides %endmacro From cf80668ce370dc44ecfd647d87d7482bcb34bcc3 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Fri, 9 Sep 2022 17:42:49 -0700 Subject: [PATCH 18/97] formatting --- evm/src/cpu/kernel/asm/ripemd/subroutines.asm | 95 ++++++++++--------- evm/src/cpu/kernel/asm/util/basic_macros.asm | 12 +-- 2 files changed, 58 insertions(+), 49 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm index 0620a1de..3193a2e2 100644 --- a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm +++ b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm @@ -2,27 +2,31 @@ /// return (u32(x << n)) | (x >> (32 - n)) global rol: - jumpdest + JUMPDEST // stack: n, x, retdest - swap1 dup1 dup3 + SWAP1 + DUP1 + DUP3 // stack: n, x, x, n, retdest - push 32 sub + PUSH 32 + SUB // stack: 32-n, x, x, n, retdest - shr + SHR // stack: x >> (32-n), x, n, retdest - swap2 + SWAP2 // stack: n, x, x >> (32-n), retdest - shl + SHL // stack: x << n, x >> (32-n), retdest %u32 // stack: u32(x << n), x >> (32-n), retdest - or + OR // stack: u32(x << n) | (x >> (32-n)), retdest - swap1 jump + SWAP1 + JUMP -%macro push_F: - push 0 +%macro PUSH_F + PUSH 0 %this_F(0,F0) %this_F(1,F1) %this_F(2,F2) @@ -38,14 +42,14 @@ global rol: %macro this_F(i, F) // stack: acc, rnd - dup2 + DUP2 // stack: rnd, acc, rnd %eq_const(i) // stack: rnd==i, acc, j %mul_const(F) // stack: (rnd==i)*F, acc, rnd - add - acc + (rnd==j)*F, rnd + ADD + // stack: acc + (rnd==j)*F, rnd %endmacro @@ -53,90 +57,95 @@ global rol: /// return x ^ y ^ z global F0: - jumpdest + JUMPDEST // stack: x , y , z, retdest - xor + XOR // stack: x ^ y , z, retdest - xor + XOR // stack: x ^ y ^ z, retdest - swap1 jump + SWAP1 + JUMP /// def F1(x, y, z): /// return (x & y) | (u32(~x) & z) global F1: - jumpdest + JUMPDEST // stack: x, y, z, retdest - dup1 + DUP1 // stack: x, x, y, z, retdest - swap2 + SWAP2 // stack: y, x, x, z, retdest - and + AND // stack: y & x, x, z, retdest - swap2 + SWAP2 // stack: z, x, y & x, retdest - swap1 + SWAP1 // stack: x, z, y & x, retdest %not_32 // stack: ~x, z, y & x, retdest - and + AND // stack: ~x & z, y & x, retdest - or + OR // stack: (~x & z) | (y & x), retdest - swap1 jump + SWAP1 + JUMP /// def F2(x, y, z): /// return (x | u32(~y)) ^ z global F2: - jumpdest + JUMPDEST // stack: x, y, z, retdest - swap1 + SWAP1 // stack: y, x, z, retdest %not_32 // stack: ~y, x, z, retdest - or + OR // stack: ~y | x, z, retdest - xor + XOR // stack: (~y | x) ^ z, retdest - swap1 jump + SWAP1 + JUMP /// def F3(x, y, z): /// return (x & z) | (u32(~z) & y) global F3: - jumpdest + JUMPDEST // stack: x, y, z, retdest - dup3 + DUP3 // stack: z, x, y, z, retdest - and + AND // stack: z & x, y, z, retdest - swap2 + SWAP2 // stack: z, y, z & x, retdest %not_32 // stack: ~z, y, z & x, retdest - and + AND // stack: ~z & y, z & x, retdest - or + OR // stack: (~z & y) | (z & x), retdest - swap1 jump + SWAP1 + JUMP /// def F4(x, y, z): /// return x ^ (y | u32(~z)) global F4: - jumpdest + JUMPDEST // stack: x, y, z, retdest - swap2 + SWAP2 // stack: z, y, x, retdest %not_32 // stack: ~z, y, x, retdest - or + OR // stack: ~z | y, x, retdest - xor + XOR // stack: (~z | y) ^ x, retdest - swap1 jump + SWAP1 + JUMP diff --git a/evm/src/cpu/kernel/asm/util/basic_macros.asm b/evm/src/cpu/kernel/asm/util/basic_macros.asm index edac2b75..1cea8deb 100644 --- a/evm/src/cpu/kernel/asm/util/basic_macros.asm +++ b/evm/src/cpu/kernel/asm/util/basic_macros.asm @@ -218,25 +218,25 @@ %macro u32 // stack: x - push 0xffffffff + PUSH 0xffffffff // stack: 0xffffffff, x - and + AND // stack: 0xffffffff & x %endmacro %macro not_32 // stack: x - push 0xffffffff + PUSH 0xffffffff // stack: 0xffffffff, x - sub + SUB // stack: 0xffffffff - x %endmacro %macro add3_32 // stack: x, y, z - add + ADD // stack: x+y, z - add + ADD // stack: x+y+z %u32 %endmacro From b08a5772d3f8c48b47ffe4c6e0adf50bacc20bd9 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Tue, 13 Sep 2022 14:32:41 -0700 Subject: [PATCH 19/97] allow offset variable --- evm/src/cpu/kernel/asm/ripemd/hash.asm | 184 +++++++++++++------------ 1 file changed, 99 insertions(+), 85 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/hash.asm b/evm/src/cpu/kernel/asm/ripemd/hash.asm index 5afd65a4..b22ae5b1 100644 --- a/evm/src/cpu/kernel/asm/ripemd/hash.asm +++ b/evm/src/cpu/kernel/asm/ripemd/hash.asm @@ -15,89 +15,96 @@ /// /// def mix(*stateR, *stateL, *state): /// return -/// u32(state[1] + stateL[2] + stateR[3]), -/// u32(state[2] + stateL[3] + stateR[4]), -/// u32(state[3] + stateL[4] + stateR[0]), -/// u32(state[4] + stateL[0] + stateR[1]), -/// u32(state[0] + stateL[1] + stateR[2]) +/// u32(s1 + l2 + r3), +/// u32(s2 + l3 + r4), +/// u32(s3 + l4 + r0), +/// u32(s4 + l0 + r1), +/// u32(s0 + l1 + r2) /// -/// In mix, we denote state[i], stateL[i], stateR[i] by si, li, ri +/// where si, li, ri, oi, OS, RD respectively denote +/// state[i], stateL[i], stateR[i], output[i], offset, retdest global hash: JUMPDEST - // stack: *state, retdest - PUSH switch + // stack: *state, offset, retdest + PUSH switch + DUP7 PUSH 1 PUSH 5 PUSH 16 PUSH 0 PUSH 0 - // stack: 0, 0, 16, 5, 1, switch, *state, retdest - DUP11 - DUP11 - DUP11 - DUP11 - DUP11 - // stack: *state, 0, 0, 16, 5, 1, switch, *state, retdest + // stack: 0, 0, 16, 5, 1, offset, switch, *state, offset, retdest + DUP12 + DUP12 + DUP12 + DUP12 + DUP12 + // stack: *state, 0, 0, 16, 5, 1, offset, switch, *state, offset, retdest %jump(loop) switch: JUMPDEST - // stack: *stateL, *state, retdest - PUSH mix - PUSH 0 + // stack: *stateL, *state, offset, retdest + PUSH mix + DUP12 + PUSH 0 PUSH 5 PUSH 16 PUSH 0 PUSH 0 - // stack: 0, 0, 16, 5, 0, mix, *stateL, *state, retdest - DUP16 - DUP16 - DUP16 - DUP16 - DUP16 - // stack: *state, 0, 0, 16, 5, 0, mix, *stateL, *state, retdest + // stack: 0, 0, 16, 5, 0, offset, mix, *stateL, *state, offset, retdest + DUP17 + DUP17 + DUP17 + DUP17 + DUP17 + // stack: *state, 0, 0, 16, 5, 0, offset, mix, *stateL, *state, offset, retdest %jump(loop) mix: JUMPDEST - // stack: r0, r1, r2, r3, r4, l0, l1, l2, l3, l4, s0, s1, s2, s3, s4, retdest + // stack: r0, r1, r2, r3, r4, l0, l1, l2, l3, l4, s0, s1, s2, s3, s4, OS, RD SWAP10 - // stack: s0, r1, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, retdest + // stack: s0, r1, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, OS, RD SWAP1 - // stack: r1, s0, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, retdest + // stack: r1, s0, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, OS, RD SWAP6 - // stack: l1, s0, r2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, retdest + // stack: l1, s0, r2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, OS, RD %add3_32 - // stack: s0+l1+r2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, retdest - SWAP13 - // stack: retdest, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, s0+l1+r2 + // stack: o4, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, OS, RD + SWAP14 + // stack: RD, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, OS, o4 SWAP11 - // stack: s3, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, retdest, s4, s0+l1+r2 + // stack: s3, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, RD, s4, OS, o4 SWAP10 - // stack: s2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s3, retdest, s4, s0+l1+r2 + // stack: s2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s3, RD, s4, OS, o4 SWAP1 - // stack: r3, s2, r4, l0, r1, l2, l3, l4, r0, s1, s3, retdest, s4, s0+l1+r2 + // stack: r3, s2, r4, l0, r1, l2, l3, l4, r0, s1, s3, RD, s4, OS, o4 SWAP6 - // stack: l3, s2, r4, l0, r1, l2, r3, l4, r0, s1, s3, retdest, s4, s0+l1+r2 + // stack: l3, s2, r4, l0, r1, l2, r3, l4, r0, s1, s3, RD, s4, OS, o4 %add3_32 - // stack: s2+l3+r4, l0, r1, l2, r3, l4, r0, s1, s3, retdest, s4, s0+l1+r2 - SWAP8 - // stack: s3, l0, r1, l2, r3, l4, r0, s1, s2+l3+r4, retdest, s4, s0+l1+r2 + // stack: o1, l0, r1, l2, r3, l4, r0, s1, s3, RD, s4, OS, o4 + SWAP9 + // stack: RD, l0, r1, l2, r3, l4, r0, s1, s3, o1, s4, OS, o4 SWAP10 - // stack: s4, l0, r1, l2, r3, l4, r0, s1, s2+l3+r4, retdest, s3, s0+l1+r2 + // stack: s4, l0, r1, l2, r3, l4, r0, s1, s3, o1, RD, OS, o4 %add3_32 - // stack: s4+l0+r1, l2, r3, l4, r0, s1, s2+l3+r4, retdest, s3, s0+l1+r2 - SWAP8 - // stack: s3, l2, r3, l4, r0, s1, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2 + // stack: o3, l2, r3, l4, r0, s1, s3, o1, RD, OS, o4 + SWAP9 + // stack: OS, l2, r3, l4, r0, s1, s3, o1, RD, o3, o4 SWAP5 - // stack: s1, l2, r3, l4, r0, s3, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2 + // stack: s1, l2, r3, l4, r0, OS, s3, o1, RD, o3, o4 %add3_32 - // stack: s1+l2+r3, l4, r0, s3, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2 - SWAP3 - // stack: s3, l4, r0, s1+l2+r3, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2 - %add3_32 - // stack: s3+l4+r0, s1+l2+r3, s2+l3+r4, retdest, s4+l0+r1, s0+l1+r2 - SWAP3 - // stack: retdest, s1+l2+r3, s2+l3+r4, s3+l4+r0, s4+l0+r1, s0+l1+r2 + // stack: o0, l4, r0, OS, s3, o1, RD, o3, o4 + SWAP4 + // stack: s3, l4, r0, OS, o0, o1, RD, o3, o4 + %add3_32 + // stack: o2, OS, o0, o1, RD, o3, o4 + SWAP4 + // stack: RD, OS, o0, o1, o2, o3, o4 + SWAP1 + // stack: OS, RD, o0, o1, o2, o3, o4 + POP + // stack: RD, o0, o1, o2, o3, o4 JUMP @@ -120,52 +127,52 @@ mix: loop: JUMPDEST - // stack: *state, F, K, 16, rounds, sides, retdest + // stack: *state, F, K, 16, rounds, sides, offset, retdest DUP9 - // stack: round, *state, F, K, 16, rounds, sides, retdest + // stack: round, *state, F, K, 16, rounds, sides, offset, retdest %jumpi(update_round_vars) - // stack: *state, F, K, 16, 0, sides, retdest - %stack (a, b, c, d, e, F, K, boxes, rounds, sides, retdest) -> (retdest, a, b, c, d, e) + // stack: *state, F, K, 16, 0, sides, offset, retdest + %stack (a, b, c, d, e, F, K, boxes, rounds, sides, offset, retdest) -> (retdest, a, b, c, d, e) // stack: retdest, *state JUMP update_round_vars: JUMPDEST - // stack: *state, F , K , 16, rounds, sides, retdest + // stack: *state, F , K , 16, rounds, sides, offset, retdest DUP9 DUP11 %get_round DUP1 - // stack: rnd, rnd, *state, F , K , 16, rounds, sides, retdest + // stack: rnd, rnd, *state, F , K , 16, rounds, sides, offset, retdest SWAP7 POP %push_F SWAP7 - // stack: rnd, rnd, *state, F', K , 16, rounds, sides, retdest + // stack: rnd, rnd, *state, F', K , 16, rounds, sides, offset, retdest SWAP8 POP %load_K SWAP7 POP - // stack: *state, F', K', 16, rounds, sides, retdest + // stack: *state, F', K', 16, rounds, sides, offset, retdest %jump(round) round: JUMPDEST - // stack: *state, F, K, boxes, rounds , sides, retdest + // stack: *state, F, K, boxes, rounds , sides, offset, retdest DUP8 - // stack: boxes, *state, F, K, boxes, rounds , sides, retdest + // stack: boxes, *state, F, K, boxes, rounds , sides, offset, retdest %jumpi(box) - // stack: *state, F, K, 0, rounds , sides, retdest + // stack: *state, F, K, 0, rounds , sides, offset, retdest SWAP7 POP PUSH 16 SWAP7 - // stack: *state, F, K, 16, rounds , sides, retdest + // stack: *state, F, K, 16, rounds , sides, offset, retdest PUSH 1 DUP10 SUB SWAP9 POP - // stack: *state, F, K, 16, rounds-1, sides, retdest + // stack: *state, F, K, 16, rounds-1, sides, offset, retdest %jump(loop) @@ -189,68 +196,69 @@ round: box: JUMPDEST - // stack: a, b, c, d, e, F, K, boxes, rounds, sides + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, offset PUSH pre_rol DUP5 DUP5 DUP5 DUP10 - // stack: F, b, c, d, pre_rol, a, b, c, d, e, F, K, boxes, rounds, sides + // stack: F, b, c, d, pre_rol, a, b, c, d, e, F, K, boxes, rounds, sides, offset JUMP pre_rol: JUMPDEST - // stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides + // stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides, offset ADD - // stack: a, b, c, d, e, F, K, boxes, rounds, sides + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, offset %get_box_from_stack - // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides - DUP1 + // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, offset + DUP12 + DUP2 %load_r - // stack: r, box, a, b, c, d, e, F, K, boxes, rounds, sides - %mload_kernel(@SEGMENT_KERNEL_GENERAL) - // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides + // stack: r, offset, box, a, b, c, d, e, F, K, boxes, rounds, sides, offset + %load_block + // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, offset SWAP1 SWAP2 - // stack: a, x, box, b, c, d, e, F, K, boxes, rounds, sides + // stack: a, x, box, b, c, d, e, F, K, boxes, rounds, sides, offset ADD DUP8 ADD %u32 - // stack: a, box, b, c, d, e, F, K, boxes, rounds, sides + // stack: a, box, b, c, d, e, F, K, boxes, rounds, sides, offset PUSH mid_rol SWAP2 - // stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides + // stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, offset %load_s - // stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides + // stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, offset %jump(rol) mid_rol: JUMPDEST - // stack: a, b, c, d, e, F, K, boxes, rounds, sides + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, offset DUP5 - // stack: e, a, b, c, d, e, F, K, boxes, rounds, sides + // stack: e, a, b, c, d, e, F, K, boxes, rounds, sides, offset ADD %u32 - // stack: a, b, c, d, e, F, K, boxes, rounds, sides + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, offset SWAP1 SWAP2 PUSH post_rol SWAP1 PUSH 10 - // stack: 10, c, post_rol, b, a, d, e, F, K, boxes, rounds, sides + // stack: 10, c, post_rol, b, a, d, e, F, K, boxes, rounds, sides, offset %jump(rol) post_rol: JUMPDEST - // stack: c, a, b, d, e, F, K, boxes , rounds, sides + // stack: c, a, b, d, e, F, K, boxes , rounds, sides, offset SWAP3 - // stack: d, a, b, c, e, F, K, boxes , rounds, sides + // stack: d, a, b, c, e, F, K, boxes , rounds, sides, offset SWAP4 - // stack: e, a, b, c, d, F, K, boxes , rounds, sides + // stack: e, a, b, c, d, F, K, boxes , rounds, sides, offset SWAP7 PUSH 1 SWAP1 SUB SWAP7 - // stack: e, a, b, c, d, F, K, boxes-1, rounds, sides + // stack: e, a, b, c, d, F, K, boxes-1, rounds, sides, offset %jump(round) @@ -275,3 +283,9 @@ post_rol: SUB // stack: 176 - boxes - 16*rounds - 80*sides, *7_args, boxes, rounds, sides %endmacro + + +%macro load_block + // stack: r, offset + %mload_kernel(@SEGMENT_KERNEL_GENERAL) +%endmacro \ No newline at end of file From e86c7034bcf5175fe96d1042464e13758ceca179 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 15 Sep 2022 09:21:19 -0700 Subject: [PATCH 20/97] load little endian --- evm/src/cpu/kernel/asm/memory/core.asm | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/evm/src/cpu/kernel/asm/memory/core.asm b/evm/src/cpu/kernel/asm/memory/core.asm index 2c896345..55f88493 100644 --- a/evm/src/cpu/kernel/asm/memory/core.asm +++ b/evm/src/cpu/kernel/asm/memory/core.asm @@ -85,6 +85,32 @@ // stack: (((((c_3 << 8) | c_2) << 8) | c_1) << 8) | c_0 %endmacro + +// Load a little-endian u32, consisting of 4 bytes (c_0, c_1, c_2, c_3), +// from kernel code. +%macro mload_kernel_code_u32 + // stack: offset + DUP1 + %mload_kernel_code + // stack: c_0, offset + DUP2 + %add_const(1) + %mload_kernel_code + %shl_const(8) + // stack: c0 | (c1 << 8), offset + DUP2 + %add_const(2) + %mload_kernel_code + %shl_const(16) + // stack: c0 | (c1 << 8) | (c2 << 16), offset + DUP2 + %add_const(3) + %mload_kernel_code + %shl_const(24) + // stack: c0 | (c1 << 8) | (c2 << 16) | (c3 << 24), offset +%endmacro + + // Store a single byte to kernel code. %macro mstore_kernel_code // stack: offset, value From 8049a5dabb554e2273ea0d61f9926e209af9f347 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 15 Sep 2022 09:22:22 -0700 Subject: [PATCH 21/97] consume offset --- evm/src/cpu/kernel/asm/memory/core.asm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/evm/src/cpu/kernel/asm/memory/core.asm b/evm/src/cpu/kernel/asm/memory/core.asm index 55f88493..401fcc04 100644 --- a/evm/src/cpu/kernel/asm/memory/core.asm +++ b/evm/src/cpu/kernel/asm/memory/core.asm @@ -103,11 +103,11 @@ %mload_kernel_code %shl_const(16) // stack: c0 | (c1 << 8) | (c2 << 16), offset - DUP2 + SWAP1 %add_const(3) %mload_kernel_code %shl_const(24) - // stack: c0 | (c1 << 8) | (c2 << 16) | (c3 << 24), offset + // stack: c0 | (c1 << 8) | (c2 << 16) | (c3 << 24) %endmacro From d8ac2ced748fe35e11506b02f00e510c6d5cdb9a Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 15 Sep 2022 09:24:50 -0700 Subject: [PATCH 22/97] diff name --- evm/src/cpu/kernel/asm/memory/core.asm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evm/src/cpu/kernel/asm/memory/core.asm b/evm/src/cpu/kernel/asm/memory/core.asm index 401fcc04..d4d9b5be 100644 --- a/evm/src/cpu/kernel/asm/memory/core.asm +++ b/evm/src/cpu/kernel/asm/memory/core.asm @@ -88,7 +88,7 @@ // Load a little-endian u32, consisting of 4 bytes (c_0, c_1, c_2, c_3), // from kernel code. -%macro mload_kernel_code_u32 +%macro mload_kernel_code_little_u32 // stack: offset DUP1 %mload_kernel_code From f09aec1b6c3d4a08cf493540b937e67b6a07e3e5 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 15 Sep 2022 09:27:51 -0700 Subject: [PATCH 23/97] scale indices by 4 --- evm/src/cpu/kernel/asm/ripemd/constants.asm | 84 ++++++++++----------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/constants.asm b/evm/src/cpu/kernel/asm/ripemd/constants.asm index 42997b7e..0cea39b5 100644 --- a/evm/src/cpu/kernel/asm/ripemd/constants.asm +++ b/evm/src/cpu/kernel/asm/ripemd/constants.asm @@ -92,54 +92,54 @@ S_data: %end_macro R_data: + // Left Round 0 + BYTES 00, 04, 08, 12 + BYTES 16, 20, 24, 28 + BYTES 32, 36, 40, 44 + BYTES 48, 52, 56, 60 // Left Round 1 - BYTES 00, 01, 02, 03 - BYTES 04, 05, 06, 07 - BYTES 08, 09, 10, 11 - BYTES 12, 13, 14, 15 + BYTES 28, 16, 52, 04 + BYTES 40, 24, 60, 12 + BYTES 48, 00, 36, 20 + BYTES 08, 56, 44, 32 // Left Round 2 - BYTES 07, 04, 13, 01 - BYTES 10, 06, 15, 03 - BYTES 12, 00, 09, 05 - BYTES 02, 14, 11, 08 + BYTES 12, 40, 56, 16 + BYTES 36, 60, 32, 04 + BYTES 08, 28, 00, 24 + BYTES 52, 44, 20, 48 // Left Round 3 - BYTES 03, 10, 14, 04 - BYTES 09, 15, 08, 01 - BYTES 02, 07, 00, 06 - BYTES 13, 11, 05, 12 + BYTES 04, 36, 44, 40 + BYTES 00, 32, 48, 16 + BYTES 52, 12, 28, 60 + BYTES 56, 20, 24, 08 // Left Round 4 - BYTES 01, 09, 11, 10 - BYTES 00, 08, 12, 04 - BYTES 13, 03, 07, 15 - BYTES 14, 05, 06, 02 - // Left Round 5 - BYTES 04, 00, 05, 09 - BYTES 07, 12, 02, 10 - BYTES 14, 01, 03, 08 - BYTES 11, 06, 15, 13 + BYTES 16, 00, 20, 36 + BYTES 28, 48, 08, 40 + BYTES 56, 04, 12, 32 + BYTES 44, 24, 60, 52 + // Right Round 0 + BYTES 20, 56, 28, 00 + BYTES 36, 08, 44, 16 + BYTES 52, 24, 60, 32 + BYTES 04, 40, 12, 48 // Right Round 1 - BYTES 05, 14, 07, 00 - BYTES 09, 02, 11, 04 - BYTES 13, 06, 15, 08 - BYTES 01, 10, 03, 12 + BYTES 24, 44, 12, 28 + BYTES 00, 52, 20, 40 + BYTES 56, 60, 32, 48 + BYTES 16, 36, 04, 08 // Right Round 2 - BYTES 06, 11, 03, 07 - BYTES 00, 13, 05, 10 - BYTES 14, 15, 08, 12 - BYTES 04, 09, 01, 02 + BYTES 60, 20, 04, 12 + BYTES 28, 56, 24, 36 + BYTES 44, 32, 48, 08 + BYTES 40, 00, 16, 52 // Right Round 3 - BYTES 15, 05, 01, 03 - BYTES 07, 14, 06, 09 - BYTES 11, 08, 12, 02 - BYTES 10, 00, 04, 13 + BYTES 32, 24, 16, 04 + BYTES 12, 44, 60, 00 + BYTES 20, 48, 08, 52 + BYTES 36, 28, 40, 56 // Right Round 4 - BYTES 08, 06, 04, 01 - BYTES 03, 11, 15, 00 - BYTES 05, 12, 02, 13 - BYTES 09, 07, 10, 14 - // Right Round 5 - BYTES 12, 15, 10, 04 - BYTES 01, 05, 08, 07 - BYTES 06, 02, 13, 14 - BYTES 00, 03, 09, 11 + BYTES 48, 60, 40, 16 + BYTES 04, 20, 32, 28 + BYTES 24, 08, 52, 56 + BYTES 00, 12, 36, 44 \ No newline at end of file From 145eb5865bea29f5ff1989ac0e9244dc434e77af Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 15 Sep 2022 11:02:03 -0700 Subject: [PATCH 24/97] flip bytes of a u32 macro --- evm/src/cpu/kernel/asm/util/basic_macros.asm | 77 ++++++++++++++++++-- 1 file changed, 72 insertions(+), 5 deletions(-) diff --git a/evm/src/cpu/kernel/asm/util/basic_macros.asm b/evm/src/cpu/kernel/asm/util/basic_macros.asm index 1cea8deb..56001dc1 100644 --- a/evm/src/cpu/kernel/asm/util/basic_macros.asm +++ b/evm/src/cpu/kernel/asm/util/basic_macros.asm @@ -140,6 +140,13 @@ // stack: input >= c, ... %endmacro +%macro and_const(c) + // stack: x + PUSH $c + AND + // stack: c & x +%endmacro + %macro consume_gas_const(c) PUSH $c CONSUME_GAS @@ -217,11 +224,7 @@ %endmacro %macro u32 - // stack: x - PUSH 0xffffffff - // stack: 0xffffffff, x - AND - // stack: 0xffffffff & x + %and_const(0xffffffff) %endmacro %macro not_32 @@ -240,3 +243,67 @@ // stack: x+y+z %u32 %endmacro + +// given u32 bytestring abcd return dcba +%macro flip_bytes_u32 + // stack: abcd + DUP1 + %and_const(0xFF) + // stack: d, abcd + PUSH 0xFF + DUP1 + SWAP2 + // stack: abcd, d, 0xFF, d + SUB + DIV + // stack: abc, d + DUP1 + %and_const(0xFF) + // stack: c, abcd, d + PUSH 0xFF + DUP1 + SWAP2 + // stack: abc, c, 0xFF, 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 + SUB + DIV + // stack: a, b, c, d + SWAP1 + %mul_const(0x100) + ADD + // stack: ba, c, d + SWAP1 + %mul_const(0x10000) + ADD + // stack: cba, d + SWAP1 + %mul_const(0x1000000) + ADD + // stack: dcba +%endmacro + + + + + + + + + +def flip_bytes(x): + acc = 0 + while x: + acc <<= 8 + d = x % (1<<8) + acc += d + x = (x - d)//(1<<8) + return acc \ No newline at end of file From 32cf13da61c27ffb3c4375c9ebf6c687f5d97244 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Fri, 16 Sep 2022 10:12:21 -0700 Subject: [PATCH 25/97] update --- .../asm/ripemd/{hash.asm => compression.asm} | 0 evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 40 +++++++++++++++++++ 2 files changed, 40 insertions(+) rename evm/src/cpu/kernel/asm/ripemd/{hash.asm => compression.asm} (100%) create mode 100644 evm/src/cpu/kernel/asm/ripemd/ripemd.asm diff --git a/evm/src/cpu/kernel/asm/ripemd/hash.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm similarity index 100% rename from evm/src/cpu/kernel/asm/ripemd/hash.asm rename to evm/src/cpu/kernel/asm/ripemd/compression.asm diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm new file mode 100644 index 00000000..588fa21d --- /dev/null +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -0,0 +1,40 @@ +global ripemd: + JUMPDEST + // stack: retdest + PUSH 0xC3D2E1F0 + PUSH 0x10325476 + PUSH 0x98BADCFE + PUSH 0xEFCDAB89 + PUSH 0x67452301 + // stack: 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, retdest + +process: + JUMPDEST + // stack: a , b, c, d, e, count, retdest + %flip_bytes_u32 + // stack: a', b, c, d, e, count, retdest + SWAP1 + %flip_bytes_32 + %shl_const(32) + OR + // stack: b' a', c, d, e, count, retdest + SWAP1 + %flip_bytes_32 + %shl_const(64) + OR + // stack: c' b' a', d, e, count, retdest + SWAP1 + %flip_bytes_32 + %shl_const(96) + OR + // stack: d' c' b' a', e, count, retdest + SWAP1 + %flip_bytes_32 + %shl_const(96) + OR + // stack: e' d' c' b' a', count, retdest + SWAP2 + SWAP1 + POP + // stack: retdest, e'd'c'b'a' + JUMP From 51639687478f2d506b3e0414e36919a80232a6c4 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Fri, 16 Sep 2022 10:31:30 -0700 Subject: [PATCH 26/97] remove jumpdests / fix name --- evm/src/cpu/kernel/asm/memory/core.asm | 2 +- evm/src/cpu/kernel/asm/ripemd/subroutines.asm | 20 +++++++------------ 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/evm/src/cpu/kernel/asm/memory/core.asm b/evm/src/cpu/kernel/asm/memory/core.asm index d4d9b5be..3700d7c8 100644 --- a/evm/src/cpu/kernel/asm/memory/core.asm +++ b/evm/src/cpu/kernel/asm/memory/core.asm @@ -88,7 +88,7 @@ // Load a little-endian u32, consisting of 4 bytes (c_0, c_1, c_2, c_3), // from kernel code. -%macro mload_kernel_code_little_u32 +%macro mload_kernel_code_LE_u32 // stack: offset DUP1 %mload_kernel_code diff --git a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm index 3193a2e2..86f81e1b 100644 --- a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm +++ b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm @@ -2,7 +2,6 @@ /// return (u32(x << n)) | (x >> (32 - n)) global rol: - JUMPDEST // stack: n, x, retdest SWAP1 DUP1 @@ -56,8 +55,7 @@ global rol: /// def F0(x, y, z): /// return x ^ y ^ z -global F0: - JUMPDEST +global F0: // stack: x , y , z, retdest XOR // stack: x ^ y , z, retdest @@ -70,8 +68,7 @@ global F0: /// def F1(x, y, z): /// return (x & y) | (u32(~x) & z) -global F1: - JUMPDEST +global F1: // stack: x, y, z, retdest DUP1 // stack: x, x, y, z, retdest @@ -97,7 +94,6 @@ global F1: /// return (x | u32(~y)) ^ z global F2: - JUMPDEST // stack: x, y, z, retdest SWAP1 // stack: y, x, z, retdest @@ -114,8 +110,7 @@ global F2: /// def F3(x, y, z): /// return (x & z) | (u32(~z) & y) -global F3: - JUMPDEST +global F3: // stack: x, y, z, retdest DUP3 // stack: z, x, y, z, retdest @@ -137,14 +132,13 @@ global F3: /// return x ^ (y | u32(~z)) global F4: - JUMPDEST - // stack: x, y, z, retdest + // stack: x, y, z, retdest SWAP2 - // stack: z, y, x, retdest + // stack: z, y, x, retdest %not_32 - // stack: ~z, y, x, retdest + // stack: ~z, y, x, retdest OR - // stack: ~z | y, x, retdest + // stack: ~z | y, x, retdest XOR // stack: (~z | y) ^ x, retdest SWAP1 From fa69f2a7fb769659d58a71e1d9c3851f3ce94c56 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Fri, 16 Sep 2022 19:18:26 -0700 Subject: [PATCH 27/97] remove jumpdests / add macro --- evm/src/cpu/kernel/asm/memory/core.asm | 2 +- evm/src/cpu/kernel/asm/ripemd/compression.asm | 150 ++++++++---------- evm/src/cpu/kernel/asm/ripemd/constants.asm | 50 +++--- evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 23 +++ evm/src/cpu/kernel/asm/ripemd/subroutines.asm | 46 +++--- 5 files changed, 140 insertions(+), 131 deletions(-) diff --git a/evm/src/cpu/kernel/asm/memory/core.asm b/evm/src/cpu/kernel/asm/memory/core.asm index 3700d7c8..ac02fb12 100644 --- a/evm/src/cpu/kernel/asm/memory/core.asm +++ b/evm/src/cpu/kernel/asm/memory/core.asm @@ -88,7 +88,7 @@ // Load a little-endian u32, consisting of 4 bytes (c_0, c_1, c_2, c_3), // from kernel code. -%macro mload_kernel_code_LE_u32 +%macro mload_kernel_code_u32_LE // stack: offset DUP1 %mload_kernel_code diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index b22ae5b1..bf322502 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -21,12 +21,11 @@ /// u32(s4 + l0 + r1), /// u32(s0 + l1 + r2) /// -/// where si, li, ri, oi, OS, RD respectively denote -/// state[i], stateL[i], stateR[i], output[i], offset, retdest +/// where si, li, ri, oi, BL, RD respectively denote +/// state[i], stateL[i], stateR[i], output[i], block, retdest global hash: - JUMPDEST - // stack: *state, offset, retdest + // stack: *state, block, retdest PUSH switch DUP7 PUSH 1 @@ -34,17 +33,16 @@ global hash: PUSH 16 PUSH 0 PUSH 0 - // stack: 0, 0, 16, 5, 1, offset, switch, *state, offset, retdest + // stack: 0, 0, 16, 5, 1, block, switch, *state, block, retdest DUP12 DUP12 DUP12 DUP12 DUP12 - // stack: *state, 0, 0, 16, 5, 1, offset, switch, *state, offset, retdest + // stack: *state, 0, 0, 16, 5, 1, block, switch, *state, block, retdest %jump(loop) switch: - JUMPDEST - // stack: *stateL, *state, offset, retdest + // stack: *stateL, *state, block, retdest PUSH mix DUP12 PUSH 0 @@ -52,57 +50,56 @@ switch: PUSH 16 PUSH 0 PUSH 0 - // stack: 0, 0, 16, 5, 0, offset, mix, *stateL, *state, offset, retdest + // stack: 0, 0, 16, 5, 0, block, mix, *stateL, *state, block, retdest DUP17 DUP17 DUP17 DUP17 DUP17 - // stack: *state, 0, 0, 16, 5, 0, offset, mix, *stateL, *state, offset, retdest + // stack: *state, 0, 0, 16, 5, 0, block, mix, *stateL, *state, block, retdest %jump(loop) mix: - JUMPDEST - // stack: r0, r1, r2, r3, r4, l0, l1, l2, l3, l4, s0, s1, s2, s3, s4, OS, RD + // stack: r0, r1, r2, r3, r4, l0, l1, l2, l3, l4, s0, s1, s2, s3, s4, BL, RD SWAP10 - // stack: s0, r1, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, OS, RD + // stack: s0, r1, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, BL, RD SWAP1 - // stack: r1, s0, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, OS, RD + // stack: r1, s0, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, BL, RD SWAP6 - // stack: l1, s0, r2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, OS, RD + // stack: l1, s0, r2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, BL, RD %add3_32 - // stack: o4, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, OS, RD + // stack: o4, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, BL, RD SWAP14 - // stack: RD, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, OS, o4 + // stack: RD, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, BL, o4 SWAP11 - // stack: s3, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, RD, s4, OS, o4 + // stack: s3, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, RD, s4, BL, o4 SWAP10 - // stack: s2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s3, RD, s4, OS, o4 + // stack: s2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s3, RD, s4, BL, o4 SWAP1 - // stack: r3, s2, r4, l0, r1, l2, l3, l4, r0, s1, s3, RD, s4, OS, o4 + // stack: r3, s2, r4, l0, r1, l2, l3, l4, r0, s1, s3, RD, s4, BL, o4 SWAP6 - // stack: l3, s2, r4, l0, r1, l2, r3, l4, r0, s1, s3, RD, s4, OS, o4 + // stack: l3, s2, r4, l0, r1, l2, r3, l4, r0, s1, s3, RD, s4, BL, o4 %add3_32 - // stack: o1, l0, r1, l2, r3, l4, r0, s1, s3, RD, s4, OS, o4 + // stack: o1, l0, r1, l2, r3, l4, r0, s1, s3, RD, s4, BL, o4 SWAP9 - // stack: RD, l0, r1, l2, r3, l4, r0, s1, s3, o1, s4, OS, o4 + // stack: RD, l0, r1, l2, r3, l4, r0, s1, s3, o1, s4, BL, o4 SWAP10 - // stack: s4, l0, r1, l2, r3, l4, r0, s1, s3, o1, RD, OS, o4 + // stack: s4, l0, r1, l2, r3, l4, r0, s1, s3, o1, RD, BL, o4 %add3_32 - // stack: o3, l2, r3, l4, r0, s1, s3, o1, RD, OS, o4 + // stack: o3, l2, r3, l4, r0, s1, s3, o1, RD, BL, o4 SWAP9 - // stack: OS, l2, r3, l4, r0, s1, s3, o1, RD, o3, o4 + // stack: BL, l2, r3, l4, r0, s1, s3, o1, RD, o3, o4 SWAP5 - // stack: s1, l2, r3, l4, r0, OS, s3, o1, RD, o3, o4 + // stack: s1, l2, r3, l4, r0, BL, s3, o1, RD, o3, o4 %add3_32 - // stack: o0, l4, r0, OS, s3, o1, RD, o3, o4 + // stack: o0, l4, r0, BL, s3, o1, RD, o3, o4 SWAP4 - // stack: s3, l4, r0, OS, o0, o1, RD, o3, o4 + // stack: s3, l4, r0, BL, o0, o1, RD, o3, o4 %add3_32 - // stack: o2, OS, o0, o1, RD, o3, o4 + // stack: o2, BL, o0, o1, RD, o3, o4 SWAP4 - // stack: RD, OS, o0, o1, o2, o3, o4 + // stack: RD, BL, o0, o1, o2, o3, o4 SWAP1 - // stack: OS, RD, o0, o1, o2, o3, o4 + // stack: BL, RD, o0, o1, o2, o3, o4 POP // stack: RD, o0, o1, o2, o3, o4 JUMP @@ -114,8 +111,8 @@ mix: /// round(*state, F, K, rounds, sides) /// /// def update_round_vars(): -/// F = load_F(sides, rounds) -/// K = load_K(sides, rounds) +/// F = load(F)(sides, rounds) +/// K = load(K)(sides, rounds) /// /// def round(*state, rounds, sides): /// while boxes: @@ -125,54 +122,51 @@ mix: /// rounds -= 1 -loop: - JUMPDEST - // stack: *state, F, K, 16, rounds, sides, offset, retdest +loop: + // stack: *state, F, K, 16, rounds, sides, block, retdest DUP9 - // stack: round, *state, F, K, 16, rounds, sides, offset, retdest + // stack: round, *state, F, K, 16, rounds, sides, block, retdest %jumpi(update_round_vars) - // stack: *state, F, K, 16, 0, sides, offset, retdest - %stack (a, b, c, d, e, F, K, boxes, rounds, sides, offset, retdest) -> (retdest, a, b, c, d, e) + // stack: *state, F, K, 16, 0, sides, block, retdest + %stack (a, b, c, d, e, F, K, boxes, rounds, sides, block, retdest) -> (retdest, a, b, c, d, e) // stack: retdest, *state JUMP update_round_vars: - JUMPDEST - // stack: *state, F , K , 16, rounds, sides, offset, retdest + // stack: *state, F , K , 16, rounds, sides, block, retdest DUP9 DUP11 %get_round DUP1 - // stack: rnd, rnd, *state, F , K , 16, rounds, sides, offset, retdest + // stack: rnd, rnd, *state, F , K , 16, rounds, sides, block, retdest SWAP7 POP %push_F SWAP7 - // stack: rnd, rnd, *state, F', K , 16, rounds, sides, offset, retdest + // stack: rnd, rnd, *state, F', K , 16, rounds, sides, block, retdest SWAP8 POP - %load_K + %load_u32(K_data) SWAP7 POP - // stack: *state, F', K', 16, rounds, sides, offset, retdest + // stack: *state, F', K', 16, rounds, sides, block, retdest %jump(round) round: - JUMPDEST - // stack: *state, F, K, boxes, rounds , sides, offset, retdest + // stack: *state, F, K, boxes, rounds , sides, block, retdest DUP8 - // stack: boxes, *state, F, K, boxes, rounds , sides, offset, retdest + // stack: boxes, *state, F, K, boxes, rounds , sides, block, retdest %jumpi(box) - // stack: *state, F, K, 0, rounds , sides, offset, retdest + // stack: *state, F, K, 0, rounds , sides, block, retdest SWAP7 POP PUSH 16 SWAP7 - // stack: *state, F, K, 16, rounds , sides, offset, retdest + // stack: *state, F, K, 16, rounds , sides, block, retdest PUSH 1 DUP10 SUB SWAP9 POP - // stack: *state, F, K, 16, rounds-1, sides, offset, retdest + // stack: *state, F, K, 16, rounds-1, sides, block, retdest %jump(loop) @@ -195,70 +189,66 @@ round: box: - JUMPDEST - // stack: a, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, block PUSH pre_rol DUP5 DUP5 DUP5 DUP10 - // stack: F, b, c, d, pre_rol, a, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: F, b, c, d, pre_rol, a, b, c, d, e, F, K, boxes, rounds, sides, block JUMP pre_rol: - JUMPDEST - // stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides, block ADD - // stack: a, b, c, d, e, F, K, boxes, rounds, sides, offset - %get_box_from_stack - // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, block + %get_box + // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, block DUP12 DUP2 - %load_r - // stack: r, offset, box, a, b, c, d, e, F, K, boxes, rounds, sides, offset + %load_byte(R_data) + // stack: r, block, box, a, b, c, d, e, F, K, boxes, rounds, sides, block %load_block - // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, block SWAP1 SWAP2 - // stack: a, x, box, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: a, x, box, b, c, d, e, F, K, boxes, rounds, sides, block ADD DUP8 ADD %u32 - // stack: a, box, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: a, box, b, c, d, e, F, K, boxes, rounds, sides, block PUSH mid_rol SWAP2 - // stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, offset - %load_s - // stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, block + %load_byte(S_data) + // stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, block %jump(rol) mid_rol: - JUMPDEST - // stack: a, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, block DUP5 - // stack: e, a, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: e, a, b, c, d, e, F, K, boxes, rounds, sides, block ADD %u32 - // stack: a, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, block SWAP1 SWAP2 PUSH post_rol SWAP1 PUSH 10 - // stack: 10, c, post_rol, b, a, d, e, F, K, boxes, rounds, sides, offset + // stack: 10, c, post_rol, b, a, d, e, F, K, boxes, rounds, sides, block %jump(rol) post_rol: - JUMPDEST - // stack: c, a, b, d, e, F, K, boxes , rounds, sides, offset + // stack: c, a, b, d, e, F, K, boxes , rounds, sides, block SWAP3 - // stack: d, a, b, c, e, F, K, boxes , rounds, sides, offset + // stack: d, a, b, c, e, F, K, boxes , rounds, sides, block SWAP4 - // stack: e, a, b, c, d, F, K, boxes , rounds, sides, offset + // stack: e, a, b, c, d, F, K, boxes , rounds, sides, block SWAP7 PUSH 1 SWAP1 SUB SWAP7 - // stack: e, a, b, c, d, F, K, boxes-1, rounds, sides, offset + // stack: e, a, b, c, d, F, K, boxes-1, rounds, sides, block %jump(round) @@ -269,7 +259,7 @@ post_rol: %endmacro -%macro get_box_from_stack +%macro get_box // stack: *7_args, boxes, rounds, sides DUP10 %mul_const(80) @@ -286,6 +276,6 @@ post_rol: %macro load_block - // stack: r, offset + // stack: r, block %mload_kernel(@SEGMENT_KERNEL_GENERAL) %endmacro \ No newline at end of file diff --git a/evm/src/cpu/kernel/asm/ripemd/constants.asm b/evm/src/cpu/kernel/asm/ripemd/constants.asm index 0cea39b5..6d0c5b0e 100644 --- a/evm/src/cpu/kernel/asm/ripemd/constants.asm +++ b/evm/src/cpu/kernel/asm/ripemd/constants.asm @@ -1,9 +1,11 @@ -%macro load_K +%macro load_u32(loc) // stack: rnd - %mul_const(4) push K_data add - // stack: K_data + 4*rnd + %mul_const(4) + push $loc + ADD + // stack: loc + 4*rnd %mload_kernel_code_u32 - // stack: K + // stack: u32 %end_macro K_data: @@ -21,76 +23,70 @@ K_data: BYTES 0x00, 0x00, 0x00, 0x00 -%macro load_s +%macro load_byte(loc) // stack: box - push S_data add - // stack: S_data + box + PUSH $loc + ADD + // stack: loc + box %mload_kernel_code - // stack: s -%end_macro + // stack: u8 +%endmacro + S_data: - // Left Round 1 + // Left Round 0 BYTES 11, 14, 15, 12 BYTES 05, 08, 07, 09 BYTES 11, 13, 14, 15 BYTES 06, 07, 09, 08 - // Left Round 2 + // Left Round 1 BYTES 07, 06, 08, 13 BYTES 11, 09, 07, 15 BYTES 07, 12, 15, 09 BYTES 11, 07, 13, 12 - // Left Round 3 + // Left Round 2 BYTES 11, 13, 06, 07 BYTES 14, 09, 13, 15 BYTES 14, 08, 13, 06 BYTES 05, 12, 07, 05 - // Left Round 4 + // Left Round 3 BYTES 11, 12, 14, 15 BYTES 14, 15, 09, 08 BYTES 09, 14, 05, 06 BYTES 08, 06, 05, 12 - // Left Round 5 + // Left Round 4 BYTES 09, 15, 05, 11 BYTES 06, 08, 13, 12 BYTES 05, 12, 13, 14 BYTES 11, 08, 05, 06 - // Right Round 1 + // Right Round 0 BYTES 08, 09, 09, 11 BYTES 13, 15, 15, 05 BYTES 07, 07, 08, 11 BYTES 14, 14, 12, 06 - // Right Round 2 + // Right Round 1 BYTES 09, 13, 15, 07 BYTES 12, 08, 09, 11 BYTES 07, 07, 12, 07 BYTES 06, 15, 13, 11 - // Right Round 3 + // Right Round 2 BYTES 09, 07, 15, 11 BYTES 08, 06, 06, 14 BYTES 12, 13, 05, 14 BYTES 13, 13, 07, 05 - // Right Round 4 + // Right Round 3 BYTES 15, 05, 08, 11 BYTES 14, 14, 06, 14 BYTES 06, 09, 12, 09 BYTES 12, 05, 15, 08 - // Right Round 5 + // Right Round 4 BYTES 08, 05, 12, 09 BYTES 12, 05, 14, 06 BYTES 08, 13, 06, 05 BYTES 15, 13, 11, 11 -%macro load_r - // stack: box - push R_data add - // stack: R_data + box - %mload_kernel_code - // stack: r -%end_macro - R_data: // Left Round 0 BYTES 00, 04, 08, 12 diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm index 588fa21d..0a89cdca 100644 --- a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -38,3 +38,26 @@ process: POP // stack: retdest, e'd'c'b'a' JUMP + + +/// def padlength(count): +/// x = 56 - (count // 8) % 64 +/// return x + 64*(x < 9) + +%macro padlength + // stack: count + %div_const(8) + // stack: count // 8 + %mod_const(64) + // stack: (count // 8) % 64 + PUSH 56 + SUB + // stack: x = 56 - (count // 8) % 64 + DUP1 + %lt_const(9) + // stack: x < 9 , x + %mul_const(64) + // stack: 64*(x < 9) , x + ADD + // stack: 64*(x < 9) + x +%endmacro \ No newline at end of file diff --git a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm index 86f81e1b..54877521 100644 --- a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm +++ b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm @@ -40,15 +40,15 @@ global rol: %macro this_F(i, F) - // stack: acc, rnd + // stack: acc, rnd DUP2 - // stack: rnd, acc, rnd - %eq_const(i) - // stack: rnd==i, acc, j - %mul_const(F) - // stack: (rnd==i)*F, acc, rnd + // stack: rnd , acc, rnd + %eq_const($i) + // stack: rnd==i , acc, j + %mul_const($F) + // stack: (rnd==i)*F , acc, rnd ADD - // stack: acc + (rnd==j)*F, rnd + // stack: (rnd==j)*F + acc, rnd %endmacro @@ -71,19 +71,19 @@ global F0: global F1: // stack: x, y, z, retdest DUP1 - // stack: x, x, y, z, retdest + // stack: x, x, y, z, retdest SWAP2 - // stack: y, x, x, z, retdest + // stack: y, x, x, z, retdest AND // stack: y & x, x, z, retdest SWAP2 - // stack: z, x, y & x, retdest + // stack: z, x, y & x , retdest SWAP1 - // stack: x, z, y & x, retdest + // stack: x, z, y & x , retdest %not_32 - // stack: ~x, z, y & x, retdest + // stack: ~x, z, y & x , retdest AND - // stack: ~x & z, y & x, retdest + // stack: ~x & z , y & x , retdest OR // stack: (~x & z) | (y & x), retdest SWAP1 @@ -94,13 +94,13 @@ global F1: /// return (x | u32(~y)) ^ z global F2: - // stack: x, y, z, retdest + // stack: x , y, z, retdest SWAP1 - // stack: y, x, z, retdest + // stack: y , x, z, retdest %not_32 - // stack: ~y, x, z, retdest + // stack: ~y , x , z, retdest OR - // stack: ~y | x, z, retdest + // stack: ~y | x , z, retdest XOR // stack: (~y | x) ^ z, retdest SWAP1 @@ -111,17 +111,17 @@ global F2: /// return (x & z) | (u32(~z) & y) global F3: - // stack: x, y, z, retdest + // stack: x, y , z , retdest DUP3 - // stack: z, x, y, z, retdest + // stack: z , x, y , z , retdest AND - // stack: z & x, y, z, retdest + // stack: z & x, y , z , retdest SWAP2 - // stack: z, y, z & x, retdest + // stack: z, y, z & x , retdest %not_32 - // stack: ~z, y, z & x, retdest + // stack: ~z , y, z & x , retdest AND - // stack: ~z & y, z & x, retdest + // stack: ~z & y, z & x , retdest OR // stack: (~z & y) | (z & x), retdest SWAP1 From ffbb6fc6178e59029192ad4726c1f451cfad8407 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Sun, 18 Sep 2022 10:20:25 -0700 Subject: [PATCH 28/97] outer function --- evm/src/cpu/kernel/asm/ripemd/compression.asm | 30 ++++---- evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 72 +++++++++++++------ .../cpu/kernel/asm/ripemd/storage_macros.asm | 0 evm/src/cpu/kernel/asm/ripemd/update.asm | 0 4 files changed, 62 insertions(+), 40 deletions(-) create mode 100644 evm/src/cpu/kernel/asm/ripemd/storage_macros.asm create mode 100644 evm/src/cpu/kernel/asm/ripemd/update.asm diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index bf322502..99694d00 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -2,7 +2,7 @@ /// Note that state takes up 5 stack slots -/// def hash(state, _block): +/// def compression(state, _block): /// /// stateL = state /// stateL = loop(stateL) @@ -24,7 +24,7 @@ /// where si, li, ri, oi, BL, RD respectively denote /// state[i], stateL[i], stateR[i], output[i], block, retdest -global hash: +global compression: // stack: *state, block, retdest PUSH switch DUP7 @@ -42,7 +42,7 @@ global hash: // stack: *state, 0, 0, 16, 5, 1, block, switch, *state, block, retdest %jump(loop) switch: - // stack: *stateL, *state, block, retdest + // stack: *stateL, *state, block, retdest PUSH mix DUP12 PUSH 0 @@ -50,7 +50,7 @@ switch: PUSH 16 PUSH 0 PUSH 0 - // stack: 0, 0, 16, 5, 0, block, mix, *stateL, *state, block, retdest + // stack: 0, 0, 16, 5, 0, block, mix, *stateL, *state, block, retdest DUP17 DUP17 DUP17 @@ -177,10 +177,10 @@ round: /// /// box = get_box(sides, rounds, boxes) /// a += F(b, c, d) -/// r = load_r(box) +/// r = load_byte(r)(box) /// x = load_block(r) /// a += x + K -/// s = load_s(box) +/// s = load_byte(s)(box) /// a = rol(s, a) /// a += e /// c = rol(10, c) @@ -198,15 +198,15 @@ box: // stack: F, b, c, d, pre_rol, a, b, c, d, e, F, K, boxes, rounds, sides, block JUMP pre_rol: - // stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides, block + // stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides, block ADD - // stack: a, b, c, d, e, F, K, boxes, rounds, sides, block + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, block %get_box - // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, block - DUP12 - DUP2 + // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, block + DUP1 %load_byte(R_data) - // stack: r, block, box, a, b, c, d, e, F, K, boxes, rounds, sides, block + DUP13 + // stack: block, r, box, a, b, c, d, e, F, K, boxes, rounds, sides, block %load_block // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, block SWAP1 @@ -273,9 +273,3 @@ post_rol: SUB // stack: 176 - boxes - 16*rounds - 80*sides, *7_args, boxes, rounds, sides %endmacro - - -%macro load_block - // stack: r, block - %mload_kernel(@SEGMENT_KERNEL_GENERAL) -%endmacro \ No newline at end of file diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm index 0a89cdca..bb3d6476 100644 --- a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -1,43 +1,71 @@ -global ripemd: - JUMPDEST - // stack: retdest - PUSH 0xC3D2E1F0 - PUSH 0x10325476 - PUSH 0x98BADCFE - PUSH 0xEFCDAB89 - PUSH 0x67452301 - // stack: 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, retdest +/// Variables beginning with _ are in memory and not on the stack +/// ripemd_update will receive and return the stack in the form: +/// stack: *state, count, length, offset +/// where offset is the virtual address of its final positional argument +/// +/// def ripemd160(_input): +/// state, count, _buffer = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0], 0, [0]*64 +/// state, count, _buffer = ripemd_update(state, count, _buffer, len(_input), _input) +/// _padding = [0x80]+[0]*63 +/// _size = get_size(count) +/// state, count, _buffer = ripemd_update(state, count, _buffer, padlength(count), _padding) +/// state, count, _buffer = ripemd_update(state, count, _buffer, 8, _size) +/// return process(state) +global ripemd: + // stack: ADDR, length + %store_buffer // store _buffer at location 0 + %store_input // store _input at location 64 + // stack: length + %stack (length) -> ( 0, length, 64, ripemd_1, ripemd_2, process) + // stack: count = 0, length, offset = 64, ripemd_1, ripemd_2, process + %stack (c, l, o, l1, l2, l3) -> (0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, c, l, o, l1, l2, l3) + // stack: 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, count, length, offset, *labels + %jump(ripemd_update) +ripemd_1: + // stack: *state, count, length, offset, *labels + DUP6 + DUP1 + // stack: count, count, *state, count, length, offset, *labels + %store_padding // store _padding at location 64 + %store_size // store _size at location 128 [note: consumes count] + %padlength + // stack: padlength, *state, count, length, offset, *labels + SWAP7 + POP + // stack: *state, count, length, offset, *labels + %jump(ripemd_update) +ripemd_2: + // stack: *state, count, length, offset, *labels + %stack (a, b, c, d, e, count, length, offset) -> (a, b, c, d, e, count, 8, 128) + // stack: *state, count, length, offset, *labels + %jump(ripemd_update) process: - JUMPDEST - // stack: a , b, c, d, e, count, retdest + // stack: a , b, c, d, e, count, length, offset %flip_bytes_u32 - // stack: a', b, c, d, e, count, retdest + // stack: a', b, c, d, e, *vars SWAP1 %flip_bytes_32 %shl_const(32) OR - // stack: b' a', c, d, e, count, retdest + // stack: b' a', c, d, e, *vars SWAP1 %flip_bytes_32 %shl_const(64) OR - // stack: c' b' a', d, e, count, retdest + // stack: c' b' a', d, e, *vars SWAP1 %flip_bytes_32 %shl_const(96) OR - // stack: d' c' b' a', e, count, retdest + // stack: d' c' b' a', e, *vars SWAP1 %flip_bytes_32 %shl_const(96) OR - // stack: e' d' c' b' a', count, retdest - SWAP2 - SWAP1 - POP - // stack: retdest, e'd'c'b'a' - JUMP + // stack: e' d' c' b' a', *vars + %stack (result, x, y, z) -> result + // stack: result /// def padlength(count): @@ -60,4 +88,4 @@ process: // stack: 64*(x < 9) , x ADD // stack: 64*(x < 9) + x -%endmacro \ No newline at end of file +%endmacro diff --git a/evm/src/cpu/kernel/asm/ripemd/storage_macros.asm b/evm/src/cpu/kernel/asm/ripemd/storage_macros.asm new file mode 100644 index 00000000..e69de29b diff --git a/evm/src/cpu/kernel/asm/ripemd/update.asm b/evm/src/cpu/kernel/asm/ripemd/update.asm new file mode 100644 index 00000000..e69de29b From e1a1b05b13a2f5f44446f175fa30920853cf9354 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Sun, 18 Sep 2022 11:05:00 -0700 Subject: [PATCH 29/97] update skeleton --- evm/src/cpu/kernel/asm/ripemd/compression.asm | 4 +- .../cpu/kernel/asm/ripemd/memory_macros.asm | 22 +++++++ evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 60 +++++++++---------- .../cpu/kernel/asm/ripemd/storage_macros.asm | 0 4 files changed, 54 insertions(+), 32 deletions(-) create mode 100644 evm/src/cpu/kernel/asm/ripemd/memory_macros.asm delete mode 100644 evm/src/cpu/kernel/asm/ripemd/storage_macros.asm diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index 99694d00..b14b0e72 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -1,4 +1,4 @@ -/// Variables beginning with _ are not maintained on the stack +/// _block is stored in memory and its address block remains on the stack /// Note that state takes up 5 stack slots @@ -207,7 +207,7 @@ pre_rol: %load_byte(R_data) DUP13 // stack: block, r, box, a, b, c, d, e, F, K, boxes, rounds, sides, block - %load_block + %load_from_block // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, block SWAP1 SWAP2 diff --git a/evm/src/cpu/kernel/asm/ripemd/memory_macros.asm b/evm/src/cpu/kernel/asm/ripemd/memory_macros.asm new file mode 100644 index 00000000..1c35506b --- /dev/null +++ b/evm/src/cpu/kernel/asm/ripemd/memory_macros.asm @@ -0,0 +1,22 @@ +%macro load_from_block + // stack: block, r + ADD + // stack: offset = block + r +%endmacro + +%macro init_buffer + +%endmacro + +%macro store_input + // stack: ADDR +%endmacro + +%macro store_padding +%endmacro + +%macro store_size + // stack: length + %shl_const(3) + // stack: length +%endmacro diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm index bb3d6476..58a51224 100644 --- a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -1,43 +1,45 @@ -/// Variables beginning with _ are in memory and not on the stack -/// ripemd_update will receive and return the stack in the form: -/// stack: *state, count, length, offset -/// where offset is the virtual address of its final positional argument +/// Variables beginning with _ are in memory /// /// def ripemd160(_input): /// state, count, _buffer = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0], 0, [0]*64 -/// state, count, _buffer = ripemd_update(state, count, _buffer, len(_input), _input) -/// _padding = [0x80]+[0]*63 -/// _size = get_size(count) -/// state, count, _buffer = ripemd_update(state, count, _buffer, padlength(count), _padding) -/// state, count, _buffer = ripemd_update(state, count, _buffer, 8, _size) +/// state, count, _buffer = ripemd_update(state, count, _buffer, len(input) , _input ) +/// state, count, _buffer = ripemd_update(state, count, _buffer, padlength(len(input)), [0x80]+[0]*63) +/// state, count, _buffer = ripemd_update(state, count, _buffer, 8, size(len(_input))) /// return process(state) +/// ripemd is called on a stack with ADDR and length +/// ripemd_update will receive and return the stack in the form: +/// stack: *state, count, length, offset +/// where offset is the virtual address of its final positional argument + global ripemd: - // stack: ADDR, length - %store_buffer // store _buffer at location 0 - %store_input // store _input at location 64 + // stack: ADDR, length + DUP4 + // stack: length, ADDR, length + %init_buffer // init _buffer at offset 0 + %store_size // store _size at offset 64 [consumes length] + %store_padding // store _padding at offset 72 + %store_input // store _input at offset 136 [consumes ADDR] // stack: length - %stack (length) -> ( 0, length, 64, ripemd_1, ripemd_2, process) - // stack: count = 0, length, offset = 64, ripemd_1, ripemd_2, process + %stack (length) -> ( 0, length, 136, ripemd_1, ripemd_2, process) + // stack: count = 0, length, offset = 136, ripemd_1, ripemd_2, process %stack (c, l, o, l1, l2, l3) -> (0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, c, l, o, l1, l2, l3) // stack: 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, count, length, offset, *labels %jump(ripemd_update) ripemd_1: - // stack: *state, count, length, offset, *labels - DUP6 - DUP1 - // stack: count, count, *state, count, length, offset, *labels - %store_padding // store _padding at location 64 - %store_size // store _size at location 128 [note: consumes count] + // stack: *state, count, length , offset, *labels + DUP7 + // stack: length, *state, count, length , offset, *labels %padlength - // stack: padlength, *state, count, length, offset, *labels + // stack: padlength, *state, count, length , offset, *labels SWAP7 POP - // stack: *state, count, length, offset, *labels + // stack: *state, count, length = padlength, offset, *labels + %stack (a, b, c, d, e, count, length, offset) -> (a, b, c, d, e, count, length, 72) %jump(ripemd_update) ripemd_2: // stack: *state, count, length, offset, *labels - %stack (a, b, c, d, e, count, length, offset) -> (a, b, c, d, e, count, 8, 128) + %stack (a, b, c, d, e, count, length, offset) -> (a, b, c, d, e, count, 8, 64) // stack: *state, count, length, offset, *labels %jump(ripemd_update) process: @@ -68,19 +70,17 @@ process: // stack: result -/// def padlength(count): -/// x = 56 - (count // 8) % 64 +/// def padlength(length): +/// x = 56 - length % 64 /// return x + 64*(x < 9) %macro padlength - // stack: count - %div_const(8) - // stack: count // 8 + // stack: count %mod_const(64) - // stack: (count // 8) % 64 + // stack: count % 64 PUSH 56 SUB - // stack: x = 56 - (count // 8) % 64 + // stack: x = 56 - count % 64 DUP1 %lt_const(9) // stack: x < 9 , x diff --git a/evm/src/cpu/kernel/asm/ripemd/storage_macros.asm b/evm/src/cpu/kernel/asm/ripemd/storage_macros.asm deleted file mode 100644 index e69de29b..00000000 From 1783f9716597c510b35067b04d8a6eea801a6129 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 19 Sep 2022 10:26:46 -0700 Subject: [PATCH 30/97] add ripeMD segment --- evm/src/memory/segments.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/evm/src/memory/segments.rs b/evm/src/memory/segments.rs index 48f9136b..28e371d4 100644 --- a/evm/src/memory/segments.rs +++ b/evm/src/memory/segments.rs @@ -24,6 +24,8 @@ pub(crate) enum Segment { TxnData = 9, /// Raw RLP data. RlpRaw = 10, + /// Ripe MD Blocks + RipeMD = 11, } impl Segment { @@ -42,6 +44,7 @@ impl Segment { Self::TxnFields, Self::TxnData, Self::RlpRaw, + Self::RipeMD ] } @@ -59,6 +62,7 @@ impl Segment { Segment::TxnFields => "SEGMENT_NORMALIZED_TXN", Segment::TxnData => "SEGMENT_TXN_DATA", Segment::RlpRaw => "SEGMENT_RLP_RAW", + Segment::RipeMD => "SEGMENT_RIPEMD" } } @@ -76,6 +80,7 @@ impl Segment { Segment::TxnFields => 256, Segment::TxnData => 256, Segment::RlpRaw => 8, + Segment::RipeMD => 256, } } } From c45785f4a59c8144188b19deb0b696faa84ef30b Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 19 Sep 2022 10:27:26 -0700 Subject: [PATCH 31/97] change to 8 bit words --- evm/src/memory/segments.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evm/src/memory/segments.rs b/evm/src/memory/segments.rs index 28e371d4..b725211c 100644 --- a/evm/src/memory/segments.rs +++ b/evm/src/memory/segments.rs @@ -80,7 +80,7 @@ impl Segment { Segment::TxnFields => 256, Segment::TxnData => 256, Segment::RlpRaw => 8, - Segment::RipeMD => 256, + Segment::RipeMD => 8, } } } From e151be552214117543fd01bcd97db8d2f30fd367 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 19 Sep 2022 12:09:57 -0700 Subject: [PATCH 32/97] ripemd storage --- evm/src/cpu/kernel/asm/memory/core.asm | 26 ---- evm/src/cpu/kernel/asm/ripemd/compression.asm | 90 ++++++------ evm/src/cpu/kernel/asm/ripemd/constants.asm | 9 +- evm/src/cpu/kernel/asm/ripemd/memory.asm | 138 ++++++++++++++++++ .../cpu/kernel/asm/ripemd/memory_macros.asm | 22 --- evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 24 +-- 6 files changed, 200 insertions(+), 109 deletions(-) create mode 100644 evm/src/cpu/kernel/asm/ripemd/memory.asm delete mode 100644 evm/src/cpu/kernel/asm/ripemd/memory_macros.asm diff --git a/evm/src/cpu/kernel/asm/memory/core.asm b/evm/src/cpu/kernel/asm/memory/core.asm index ac02fb12..2c896345 100644 --- a/evm/src/cpu/kernel/asm/memory/core.asm +++ b/evm/src/cpu/kernel/asm/memory/core.asm @@ -85,32 +85,6 @@ // stack: (((((c_3 << 8) | c_2) << 8) | c_1) << 8) | c_0 %endmacro - -// Load a little-endian u32, consisting of 4 bytes (c_0, c_1, c_2, c_3), -// from kernel code. -%macro mload_kernel_code_u32_LE - // stack: offset - DUP1 - %mload_kernel_code - // stack: c_0, offset - DUP2 - %add_const(1) - %mload_kernel_code - %shl_const(8) - // stack: c0 | (c1 << 8), offset - DUP2 - %add_const(2) - %mload_kernel_code - %shl_const(16) - // stack: c0 | (c1 << 8) | (c2 << 16), offset - SWAP1 - %add_const(3) - %mload_kernel_code - %shl_const(24) - // stack: c0 | (c1 << 8) | (c2 << 16) | (c3 << 24) -%endmacro - - // Store a single byte to kernel code. %macro mstore_kernel_code // stack: offset, value diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index b14b0e72..544d417c 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -1,8 +1,8 @@ -/// _block is stored in memory and its address block remains on the stack +/// _offset is stored in memory and its address offset remains on the stack /// Note that state takes up 5 stack slots -/// def compression(state, _block): +/// def compression(state, _offset): /// /// stateL = state /// stateL = loop(stateL) @@ -22,10 +22,10 @@ /// u32(s0 + l1 + r2) /// /// where si, li, ri, oi, BL, RD respectively denote -/// state[i], stateL[i], stateR[i], output[i], block, retdest +/// state[i], stateL[i], stateR[i], output[i], offset, retdest global compression: - // stack: *state, block, retdest + // stack: *state, offset, retdest PUSH switch DUP7 PUSH 1 @@ -33,16 +33,16 @@ global compression: PUSH 16 PUSH 0 PUSH 0 - // stack: 0, 0, 16, 5, 1, block, switch, *state, block, retdest + // stack: 0, 0, 16, 5, 1, offset, switch, *state, offset, retdest DUP12 DUP12 DUP12 DUP12 DUP12 - // stack: *state, 0, 0, 16, 5, 1, block, switch, *state, block, retdest + // stack: *state, 0, 0, 16, 5, 1, offset, switch, *state, offset, retdest %jump(loop) switch: - // stack: *stateL, *state, block, retdest + // stack: *stateL, *state, offset, retdest PUSH mix DUP12 PUSH 0 @@ -50,13 +50,13 @@ switch: PUSH 16 PUSH 0 PUSH 0 - // stack: 0, 0, 16, 5, 0, block, mix, *stateL, *state, block, retdest + // stack: 0, 0, 16, 5, 0, offset, mix, *stateL, *state, offset, retdest DUP17 DUP17 DUP17 DUP17 DUP17 - // stack: *state, 0, 0, 16, 5, 0, block, mix, *stateL, *state, block, retdest + // stack: *state, 0, 0, 16, 5, 0, offset, mix, *stateL, *state, offset, retdest %jump(loop) mix: // stack: r0, r1, r2, r3, r4, l0, l1, l2, l3, l4, s0, s1, s2, s3, s4, BL, RD @@ -123,50 +123,50 @@ mix: loop: - // stack: *state, F, K, 16, rounds, sides, block, retdest + // stack: *state, F, K, 16, rounds, sides, offset, retdest DUP9 - // stack: round, *state, F, K, 16, rounds, sides, block, retdest + // stack: round, *state, F, K, 16, rounds, sides, offset, retdest %jumpi(update_round_vars) - // stack: *state, F, K, 16, 0, sides, block, retdest - %stack (a, b, c, d, e, F, K, boxes, rounds, sides, block, retdest) -> (retdest, a, b, c, d, e) + // stack: *state, F, K, 16, 0, sides, offset, retdest + %stack (a, b, c, d, e, F, K, boxes, rounds, sides, offset, retdest) -> (retdest, a, b, c, d, e) // stack: retdest, *state JUMP update_round_vars: - // stack: *state, F , K , 16, rounds, sides, block, retdest + // stack: *state, F , K , 16, rounds, sides, offset, retdest DUP9 DUP11 %get_round DUP1 - // stack: rnd, rnd, *state, F , K , 16, rounds, sides, block, retdest + // stack: rnd, rnd, *state, F , K , 16, rounds, sides, offset, retdest SWAP7 POP %push_F SWAP7 - // stack: rnd, rnd, *state, F', K , 16, rounds, sides, block, retdest + // stack: rnd, rnd, *state, F', K , 16, rounds, sides, offset, retdest SWAP8 POP - %load_u32(K_data) + %load_K SWAP7 POP - // stack: *state, F', K', 16, rounds, sides, block, retdest + // stack: *state, F', K', 16, rounds, sides, offset, retdest %jump(round) round: - // stack: *state, F, K, boxes, rounds , sides, block, retdest + // stack: *state, F, K, boxes, rounds , sides, offset, retdest DUP8 - // stack: boxes, *state, F, K, boxes, rounds , sides, block, retdest + // stack: boxes, *state, F, K, boxes, rounds , sides, offset, retdest %jumpi(box) - // stack: *state, F, K, 0, rounds , sides, block, retdest + // stack: *state, F, K, 0, rounds , sides, offset, retdest SWAP7 POP PUSH 16 SWAP7 - // stack: *state, F, K, 16, rounds , sides, block, retdest + // stack: *state, F, K, 16, rounds , sides, offset, retdest PUSH 1 DUP10 SUB SWAP9 POP - // stack: *state, F, K, 16, rounds-1, sides, block, retdest + // stack: *state, F, K, 16, rounds-1, sides, offset, retdest %jump(loop) @@ -178,7 +178,7 @@ round: /// box = get_box(sides, rounds, boxes) /// a += F(b, c, d) /// r = load_byte(r)(box) -/// x = load_block(r) +/// x = load_offset(r) /// a += x + K /// s = load_byte(s)(box) /// a = rol(s, a) @@ -189,66 +189,67 @@ round: box: - // stack: a, b, c, d, e, F, K, boxes, rounds, sides, block + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, offset PUSH pre_rol DUP5 DUP5 DUP5 DUP10 - // stack: F, b, c, d, pre_rol, a, b, c, d, e, F, K, boxes, rounds, sides, block + // stack: F, b, c, d, pre_rol, a, b, c, d, e, F, K, boxes, rounds, sides, offset JUMP pre_rol: - // stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides, block + // stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides, offset ADD - // stack: a, b, c, d, e, F, K, boxes, rounds, sides, block + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, offset %get_box - // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, block + // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, offset DUP1 %load_byte(R_data) DUP13 - // stack: block, r, box, a, b, c, d, e, F, K, boxes, rounds, sides, block - %load_from_block - // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, block + ADD + // stack: offset + r, box, a, b, c, d, e, F, K, boxes, rounds, sides, offset + %load_u32_from_block + // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, offset SWAP1 SWAP2 - // stack: a, x, box, b, c, d, e, F, K, boxes, rounds, sides, block + // stack: a, x, box, b, c, d, e, F, K, boxes, rounds, sides, offset ADD DUP8 ADD %u32 - // stack: a, box, b, c, d, e, F, K, boxes, rounds, sides, block + // stack: a, box, b, c, d, e, F, K, boxes, rounds, sides, offset PUSH mid_rol SWAP2 - // stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, block + // stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, offset %load_byte(S_data) - // stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, block + // stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, offset %jump(rol) mid_rol: - // stack: a, b, c, d, e, F, K, boxes, rounds, sides, block + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, offset DUP5 - // stack: e, a, b, c, d, e, F, K, boxes, rounds, sides, block + // stack: e, a, b, c, d, e, F, K, boxes, rounds, sides, offset ADD %u32 - // stack: a, b, c, d, e, F, K, boxes, rounds, sides, block + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, offset SWAP1 SWAP2 PUSH post_rol SWAP1 PUSH 10 - // stack: 10, c, post_rol, b, a, d, e, F, K, boxes, rounds, sides, block + // stack: 10, c, post_rol, b, a, d, e, F, K, boxes, rounds, sides, offset %jump(rol) post_rol: - // stack: c, a, b, d, e, F, K, boxes , rounds, sides, block + // stack: c, a, b, d, e, F, K, boxes , rounds, sides, offset SWAP3 - // stack: d, a, b, c, e, F, K, boxes , rounds, sides, block + // stack: d, a, b, c, e, F, K, boxes , rounds, sides, offset SWAP4 - // stack: e, a, b, c, d, F, K, boxes , rounds, sides, block + // stack: e, a, b, c, d, F, K, boxes , rounds, sides, offset SWAP7 PUSH 1 SWAP1 SUB SWAP7 - // stack: e, a, b, c, d, F, K, boxes-1, rounds, sides, block + // stack: e, a, b, c, d, F, K, boxes-1, rounds, sides, offset %jump(round) @@ -258,7 +259,6 @@ post_rol: // stack: 10 - 5*sides - rounds %endmacro - %macro get_box // stack: *7_args, boxes, rounds, sides DUP10 diff --git a/evm/src/cpu/kernel/asm/ripemd/constants.asm b/evm/src/cpu/kernel/asm/ripemd/constants.asm index 6d0c5b0e..69b75e86 100644 --- a/evm/src/cpu/kernel/asm/ripemd/constants.asm +++ b/evm/src/cpu/kernel/asm/ripemd/constants.asm @@ -1,11 +1,10 @@ -%macro load_u32(loc) +%macro load_K // stack: rnd %mul_const(4) - push $loc - ADD - // stack: loc + 4*rnd + %add_const(K_data) + // stack: K_data + 4*rnd %mload_kernel_code_u32 - // stack: u32 + // stack: K %end_macro K_data: diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm new file mode 100644 index 00000000..0a170eb0 --- /dev/null +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -0,0 +1,138 @@ +ripemd_storage: // starts by initializing buffer + // stack: i (init 64) + %store_zeros(64, ripemd_storage) + // stack: + %jump(store_size) + +store_size: + // stack: length + %shl_const(3) + // stack: abcdefgh + %extract_and_store_byte(64) + // stack: abcdefg + %extract_and_store_byte(65) + // stack: abcdef + %extract_and_store_byte(66) + // stack: abcde + %extract_and_store_byte(67) + // stack: abcd + %extract_and_store_byte(68) + // stack: abc + %extract_and_store_byte(69) + // stack: ab + %extract_and_store_byte(70) + // stack: a + %mstore_ripemd(71) + // stack: 0x80 // padding has 0x80 in first position and zeros elsewhere + %mstore_ripemd(72) // store first padding term here so as to avoid extra label + %jump(store_padding) + +store_padding: + // stack: i (init 63) + %store_zeros(136, store_padding) + %jump(store_input) + +store_input: + // stack: ADDR , rem , length + DUP3 + DUP3 + DUP3 + MLOAD_GENERAL + // stack: byte, ADDR , rem , length + DUP5 + DUP7 + SUB + %add_const(136) + // stack: offset, byte, ADDR , rem , length + %mstore_ripemd + // stack: ADDR , rem , length + SWAP2 + %add_const(1) + SWAP2 + // stack: ADDR + 1, rem , length + SWAP3 + %sub_const(1) + SWAP3 + // stack: ADDR + 1, rem - 1, length + DUP4 + %jumpi(store_input) + // stack: ADDR , 0 , length + %pop4 + // stack: length + %jump(ripemd_init) + + +%macro store_zeros(N, label) + // stack: i + %stack (i) -> ($N, i, 0, i) + SUB + // stack: offset = N-i, 0, i + %mstore_ripemd + // stack: i + %sub_const(1) + DUP1 + // stack: i-1, i-1 + %jumpi($label) + // stack: 0 + POP +%endmacro + +%macro extract_and_store_byte(offset) + // stack: xsy + PUSH 0x100 + DUP2 + MOD + // stack: y, xsy + %stack (y, xsy) -> (xsy, y, 0x100, y) + // stack: xsy, y, 0x100, y + SUB + DIV + SWAP1 + // stack: y, xs + %mstore_ripemd($offset) + // stack: xs +%endmacro + +%macro mstore_ripemd_offset($offset) + // stack: value + PUSH $offset + // stack: offset, value + %mstore_kernel(RipeMD) + // stack: +%endmacro + +%macro mstore_ripemd + // stack: offset, value + %mstore_kernel(RipeMD) + // stack: +%endmacro + +%macro mload_ripemd + %mload_kernel(RipeMD) +%endmacro + +// Load LE u32 from 4 contiguous bytes a, b, c, d in RipeMD segment +%macro load_u32_from_block + // stack: offset + DUP1 + %mload_ripemd + // stack: a, offset + DUP2 + %add_const(1) + %mload_ripemd + %shl_const(8) + OR + // stack: a | (b << 8), offset + DUP2 + %add_const(2) + %mload_ripemd + %shl_const(16) + OR + // stack: a | (b << 8) | (c << 16), offset + SWAP1 + %add_const(3) + %mload_ripemd + %shl_const(24) + OR + // stack: a | (b << 8) | (c << 16) | (d << 24) +%endmacro diff --git a/evm/src/cpu/kernel/asm/ripemd/memory_macros.asm b/evm/src/cpu/kernel/asm/ripemd/memory_macros.asm deleted file mode 100644 index 1c35506b..00000000 --- a/evm/src/cpu/kernel/asm/ripemd/memory_macros.asm +++ /dev/null @@ -1,22 +0,0 @@ -%macro load_from_block - // stack: block, r - ADD - // stack: offset = block + r -%endmacro - -%macro init_buffer - -%endmacro - -%macro store_input - // stack: ADDR -%endmacro - -%macro store_padding -%endmacro - -%macro store_size - // stack: length - %shl_const(3) - // stack: length -%endmacro diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm index 58a51224..46db0c2d 100644 --- a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -2,24 +2,26 @@ /// /// def ripemd160(_input): /// state, count, _buffer = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0], 0, [0]*64 -/// state, count, _buffer = ripemd_update(state, count, _buffer, len(input) , _input ) -/// state, count, _buffer = ripemd_update(state, count, _buffer, padlength(len(input)), [0x80]+[0]*63) -/// state, count, _buffer = ripemd_update(state, count, _buffer, 8, size(len(_input))) +/// state, count, _buffer = ripemd_update(state, count, _buffer, len(input) , bytes = _input ) +/// state, count, _buffer = ripemd_update(state, count, _buffer, padlength(len(input)), bytes = [0x80]+[0]*63) +/// state, count, _buffer = ripemd_update(state, count, _buffer, 8, bytes = size(len(_input))) /// return process(state) - +/// /// ripemd is called on a stack with ADDR and length /// ripemd_update will receive and return the stack in the form: /// stack: *state, count, length, offset -/// where offset is the virtual address of its final positional argument +/// where offset is the virtual address of the bytes argument global ripemd: // stack: ADDR, length - DUP4 - // stack: length, ADDR, length - %init_buffer // init _buffer at offset 0 - %store_size // store _size at offset 64 [consumes length] - %store_padding // store _padding at offset 72 - %store_input // store _input at offset 136 [consumes ADDR] + $stack (a, b, c, length) -> (64, length, 0x80, 63, a, b, c, length, length) + // stack: 64, length, 0x80, 63, a, b, c, length, length + %jump(ripemd_storage) // stores the following into memory + // init _buffer at offset 0 [consumes 64] + // store _size at offset 64 [consumes length] + // store _padding at offset 72 [consumes 0x80, 63] + // store _input at offset 136 [consumes ADDR, length] +ripemd_init: // stack: length %stack (length) -> ( 0, length, 136, ripemd_1, ripemd_2, process) // stack: count = 0, length, offset = 136, ripemd_1, ripemd_2, process From 6193827733b76cf2a49cc26aaac03e93b3fe3e65 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 19 Sep 2022 18:11:07 -0700 Subject: [PATCH 33/97] finish?? --- evm/src/cpu/kernel/asm/ripemd/compression.asm | 144 ++++++++--------- evm/src/cpu/kernel/asm/ripemd/constants.asm | 2 +- evm/src/cpu/kernel/asm/ripemd/memory.asm | 23 ++- evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 36 ++--- evm/src/cpu/kernel/asm/ripemd/update.asm | 151 ++++++++++++++++++ evm/src/memory/segments.rs | 4 +- 6 files changed, 261 insertions(+), 99 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index 544d417c..fa68a9d7 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -1,8 +1,8 @@ -/// _offset is stored in memory and its address offset remains on the stack -/// Note that state takes up 5 stack slots +/// _block is stored in memory and its address virt remains on the stack +/// Note that STATE takes up 5 stack slots -/// def compression(state, _offset): +/// def compress(state, _block): /// /// stateL = state /// stateL = loop(stateL) @@ -13,7 +13,7 @@ /// return mix(state, stateL, stateR) /// /// -/// def mix(*stateR, *stateL, *state): +/// def mix(stateR, stateL, state): /// return /// u32(s1 + l2 + r3), /// u32(s2 + l3 + r4), @@ -21,11 +21,11 @@ /// u32(s4 + l0 + r1), /// u32(s0 + l1 + r2) /// -/// where si, li, ri, oi, BL, RD respectively denote -/// state[i], stateL[i], stateR[i], output[i], offset, retdest +/// where si, li, ri, oi, OF, RD respectively denote +/// state[i], stateL[i], stateR[i], output[i], virt, retdest -global compression: - // stack: *state, offset, retdest +global compress: + // stack: STATE, virt, retdest PUSH switch DUP7 PUSH 1 @@ -33,16 +33,16 @@ global compression: PUSH 16 PUSH 0 PUSH 0 - // stack: 0, 0, 16, 5, 1, offset, switch, *state, offset, retdest + // stack: 0, 0, 16, 5, 1, virt, switch, STATE, virt, retdest DUP12 DUP12 DUP12 DUP12 DUP12 - // stack: *state, 0, 0, 16, 5, 1, offset, switch, *state, offset, retdest + // stack: STATE, 0, 0, 16, 5, 1, virt, switch, STATE, virt, retdest %jump(loop) switch: - // stack: *stateL, *state, offset, retdest + // stack: STATEL, STATE, virt, retdest PUSH mix DUP12 PUSH 0 @@ -50,127 +50,127 @@ switch: PUSH 16 PUSH 0 PUSH 0 - // stack: 0, 0, 16, 5, 0, offset, mix, *stateL, *state, offset, retdest + // stack: 0, 0, 16, 5, 0, virt, mix, STATEL, STATE, virt, retdest DUP17 DUP17 DUP17 DUP17 DUP17 - // stack: *state, 0, 0, 16, 5, 0, offset, mix, *stateL, *state, offset, retdest + // stack: STATE, 0, 0, 16, 5, 0, virt, mix, STATEL, STATE, virt, retdest %jump(loop) mix: - // stack: r0, r1, r2, r3, r4, l0, l1, l2, l3, l4, s0, s1, s2, s3, s4, BL, RD + // stack: r0, r1, r2, r3, r4, l0, l1, l2, l3, l4, s0, s1, s2, s3, s4, VR, RD SWAP10 - // stack: s0, r1, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, BL, RD + // stack: s0, r1, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, VR, RD SWAP1 - // stack: r1, s0, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, BL, RD + // stack: r1, s0, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, VR, RD SWAP6 - // stack: l1, s0, r2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, BL, RD + // stack: l1, s0, r2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, VR, RD %add3_32 - // stack: o4, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, BL, RD + // stack: o4, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, VR, RD SWAP14 - // stack: RD, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, BL, o4 + // stack: RD, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, VR, o4 SWAP11 - // stack: s3, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, RD, s4, BL, o4 + // stack: s3, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, RD, s4, VR, o4 SWAP10 - // stack: s2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s3, RD, s4, BL, o4 + // stack: s2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s3, RD, s4, VR, o4 SWAP1 - // stack: r3, s2, r4, l0, r1, l2, l3, l4, r0, s1, s3, RD, s4, BL, o4 + // stack: r3, s2, r4, l0, r1, l2, l3, l4, r0, s1, s3, RD, s4, VR, o4 SWAP6 - // stack: l3, s2, r4, l0, r1, l2, r3, l4, r0, s1, s3, RD, s4, BL, o4 + // stack: l3, s2, r4, l0, r1, l2, r3, l4, r0, s1, s3, RD, s4, VR, o4 %add3_32 - // stack: o1, l0, r1, l2, r3, l4, r0, s1, s3, RD, s4, BL, o4 + // stack: o1, l0, r1, l2, r3, l4, r0, s1, s3, RD, s4, VR, o4 SWAP9 - // stack: RD, l0, r1, l2, r3, l4, r0, s1, s3, o1, s4, BL, o4 + // stack: RD, l0, r1, l2, r3, l4, r0, s1, s3, o1, s4, VR, o4 SWAP10 - // stack: s4, l0, r1, l2, r3, l4, r0, s1, s3, o1, RD, BL, o4 + // stack: s4, l0, r1, l2, r3, l4, r0, s1, s3, o1, RD, VR, o4 %add3_32 - // stack: o3, l2, r3, l4, r0, s1, s3, o1, RD, BL, o4 + // stack: o3, l2, r3, l4, r0, s1, s3, o1, RD, VR, o4 SWAP9 - // stack: BL, l2, r3, l4, r0, s1, s3, o1, RD, o3, o4 + // stack: VR, l2, r3, l4, r0, s1, s3, o1, RD, o3, o4 SWAP5 - // stack: s1, l2, r3, l4, r0, BL, s3, o1, RD, o3, o4 + // stack: s1, l2, r3, l4, r0, VR, s3, o1, RD, o3, o4 %add3_32 - // stack: o0, l4, r0, BL, s3, o1, RD, o3, o4 + // stack: o0, l4, r0, VR, s3, o1, RD, o3, o4 SWAP4 - // stack: s3, l4, r0, BL, o0, o1, RD, o3, o4 + // stack: s3, l4, r0, VR, o0, o1, RD, o3, o4 %add3_32 - // stack: o2, BL, o0, o1, RD, o3, o4 + // stack: o2, VR, o0, o1, RD, o3, o4 SWAP4 - // stack: RD, BL, o0, o1, o2, o3, o4 + // stack: RD, VR, o0, o1, o2, o3, o4 SWAP1 - // stack: BL, RD, o0, o1, o2, o3, o4 + // stack: VR, RD, o0, o1, o2, o3, o4 POP // stack: RD, o0, o1, o2, o3, o4 JUMP -/// def loop(*state): +/// def loop(STATE): /// while rounds: /// update_round_vars() -/// round(*state, F, K, rounds, sides) +/// round(STATE, F, K, rounds, sides) /// /// def update_round_vars(): /// F = load(F)(sides, rounds) /// K = load(K)(sides, rounds) /// -/// def round(*state, rounds, sides): +/// def round(STATE, rounds, sides): /// while boxes: -/// box(*state, F, K) +/// box(STATE, F, K) /// boxes -= 1 /// boxes = 16 /// rounds -= 1 loop: - // stack: *state, F, K, 16, rounds, sides, offset, retdest + // stack: STATE, F, K, 16, rounds, sides, virt, retdest DUP9 - // stack: round, *state, F, K, 16, rounds, sides, offset, retdest + // stack: round, STATE, F, K, 16, rounds, sides, virt, retdest %jumpi(update_round_vars) - // stack: *state, F, K, 16, 0, sides, offset, retdest - %stack (a, b, c, d, e, F, K, boxes, rounds, sides, offset, retdest) -> (retdest, a, b, c, d, e) - // stack: retdest, *state + // stack: STATE, F, K, 16, 0, sides, virt, retdest + %stack (a, b, c, d, e, F, K, boxes, rounds, sides, virt, retdest) -> (retdest, a, b, c, d, e) + // stack: retdest, STATE JUMP update_round_vars: - // stack: *state, F , K , 16, rounds, sides, offset, retdest + // stack: STATE, F , K , 16, rounds, sides, virt, retdest DUP9 DUP11 %get_round DUP1 - // stack: rnd, rnd, *state, F , K , 16, rounds, sides, offset, retdest + // stack: rnd, rnd, STATE, F , K , 16, rounds, sides, virt, retdest SWAP7 POP %push_F SWAP7 - // stack: rnd, rnd, *state, F', K , 16, rounds, sides, offset, retdest + // stack: rnd, rnd, STATE, F', K , 16, rounds, sides, virt, retdest SWAP8 POP %load_K SWAP7 POP - // stack: *state, F', K', 16, rounds, sides, offset, retdest + // stack: STATE, F', K', 16, rounds, sides, virt, retdest %jump(round) round: - // stack: *state, F, K, boxes, rounds , sides, offset, retdest + // stack: STATE, F, K, boxes, rounds , sides, virt, retdest DUP8 - // stack: boxes, *state, F, K, boxes, rounds , sides, offset, retdest + // stack: boxes, STATE, F, K, boxes, rounds , sides, virt, retdest %jumpi(box) - // stack: *state, F, K, 0, rounds , sides, offset, retdest + // stack: STATE, F, K, 0, rounds , sides, virt, retdest SWAP7 POP PUSH 16 SWAP7 - // stack: *state, F, K, 16, rounds , sides, offset, retdest + // stack: STATE, F, K, 16, rounds , sides, virt, retdest PUSH 1 DUP10 SUB SWAP9 POP - // stack: *state, F, K, 16, rounds-1, sides, offset, retdest + // stack: STATE, F, K, 16, rounds-1, sides, virt, retdest %jump(loop) -/// Note that we unpack *state to a, b, c, d, e +/// Note that we unpack STATE to a, b, c, d, e /// All additions are u32 /// /// def box(a, b, c, d, e, F, K): @@ -189,67 +189,67 @@ round: box: - // stack: a, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, virt PUSH pre_rol DUP5 DUP5 DUP5 DUP10 - // stack: F, b, c, d, pre_rol, a, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: F, b, c, d, pre_rol, a, b, c, d, e, F, K, boxes, rounds, sides, virt JUMP pre_rol: - // stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides, virt ADD - // stack: a, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, virt %get_box - // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, virt DUP1 %load_byte(R_data) DUP13 ADD - // stack: offset + r, box, a, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: virt + r, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt %load_u32_from_block - // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt SWAP1 SWAP2 - // stack: a, x, box, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: a, x, box, b, c, d, e, F, K, boxes, rounds, sides, virt ADD DUP8 ADD %u32 - // stack: a, box, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: a, box, b, c, d, e, F, K, boxes, rounds, sides, virt PUSH mid_rol SWAP2 - // stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, virt %load_byte(S_data) - // stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, virt %jump(rol) mid_rol: - // stack: a, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, virt DUP5 - // stack: e, a, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: e, a, b, c, d, e, F, K, boxes, rounds, sides, virt ADD %u32 - // stack: a, b, c, d, e, F, K, boxes, rounds, sides, offset + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, virt SWAP1 SWAP2 PUSH post_rol SWAP1 PUSH 10 - // stack: 10, c, post_rol, b, a, d, e, F, K, boxes, rounds, sides, offset + // stack: 10, c, post_rol, b, a, d, e, F, K, boxes, rounds, sides, virt %jump(rol) post_rol: - // stack: c, a, b, d, e, F, K, boxes , rounds, sides, offset + // stack: c, a, b, d, e, F, K, boxes , rounds, sides, virt SWAP3 - // stack: d, a, b, c, e, F, K, boxes , rounds, sides, offset + // stack: d, a, b, c, e, F, K, boxes , rounds, sides, virt SWAP4 - // stack: e, a, b, c, d, F, K, boxes , rounds, sides, offset + // stack: e, a, b, c, d, F, K, boxes , rounds, sides, virt SWAP7 PUSH 1 SWAP1 SUB SWAP7 - // stack: e, a, b, c, d, F, K, boxes-1, rounds, sides, offset + // stack: e, a, b, c, d, F, K, boxes-1, rounds, sides, virt %jump(round) diff --git a/evm/src/cpu/kernel/asm/ripemd/constants.asm b/evm/src/cpu/kernel/asm/ripemd/constants.asm index 69b75e86..b99df72a 100644 --- a/evm/src/cpu/kernel/asm/ripemd/constants.asm +++ b/evm/src/cpu/kernel/asm/ripemd/constants.asm @@ -28,7 +28,7 @@ K_data: ADD // stack: loc + box %mload_kernel_code - // stack: u8 + // stack: byte %endmacro diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm index 0a170eb0..bc0e8a48 100644 --- a/evm/src/cpu/kernel/asm/ripemd/memory.asm +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -97,32 +97,32 @@ store_input: // stack: value PUSH $offset // stack: offset, value - %mstore_kernel(RipeMD) + %mstore_kernel(SEGMENT_RIPEMD) // stack: %endmacro %macro mstore_ripemd // stack: offset, value - %mstore_kernel(RipeMD) + %mstore_kernel(SEGMENT_RIPEMD) // stack: %endmacro %macro mload_ripemd - %mload_kernel(RipeMD) + %mload_kernel(SEGMENT_RIPEMD) %endmacro -// Load LE u32 from 4 contiguous bytes a, b, c, d in RipeMD segment +// Load LE u32 from 4 contiguous bytes a, b, c, d in SEGMENT_RIPEMD %macro load_u32_from_block // stack: offset DUP1 %mload_ripemd - // stack: a, offset + // stack: a , offset DUP2 %add_const(1) %mload_ripemd %shl_const(8) OR - // stack: a | (b << 8), offset + // stack: a | (b << 8) , offset DUP2 %add_const(2) %mload_ripemd @@ -136,3 +136,14 @@ store_input: OR // stack: a | (b << 8) | (c << 16) | (d << 24) %endmacro + +// set offset i to offset j in SEGMENT_RIPEMD +%macro mupdate_ripemd + // stack: j, i + %mload_ripemd + // stack: x, i + SWAP1 + %mstore_ripemd + // stack: +%endmacro + \ No newline at end of file diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm index 46db0c2d..fb6777c7 100644 --- a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -9,43 +9,43 @@ /// /// ripemd is called on a stack with ADDR and length /// ripemd_update will receive and return the stack in the form: -/// stack: *state, count, length, offset -/// where offset is the virtual address of the bytes argument +/// stack: STATE, count, length, virt +/// where virt is the virtual address of the bytes argument global ripemd: // stack: ADDR, length $stack (a, b, c, length) -> (64, length, 0x80, 63, a, b, c, length, length) // stack: 64, length, 0x80, 63, a, b, c, length, length %jump(ripemd_storage) // stores the following into memory - // init _buffer at offset 0 [consumes 64] - // store _size at offset 64 [consumes length] - // store _padding at offset 72 [consumes 0x80, 63] - // store _input at offset 136 [consumes ADDR, length] + // init _buffer at virt 0 [consumes 64] + // store _size at virt 64 [consumes length] + // store _padding at virt 72 [consumes 0x80, 63] + // store _input at virt 136 [consumes ADDR, length] ripemd_init: // stack: length - %stack (length) -> ( 0, length, 136, ripemd_1, ripemd_2, process) - // stack: count = 0, length, offset = 136, ripemd_1, ripemd_2, process + %stack (length) -> ( 0, length, 136, ripemd_1, ripemd_2, process) + // stack: count = 0, length, virt = 136, ripemd_1, ripemd_2, process %stack (c, l, o, l1, l2, l3) -> (0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, c, l, o, l1, l2, l3) - // stack: 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, count, length, offset, *labels + // stack: 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, count, length, virt, *labels %jump(ripemd_update) ripemd_1: - // stack: *state, count, length , offset, *labels + // stack: STATE, count, length , virt, *labels DUP7 - // stack: length, *state, count, length , offset, *labels + // stack: length, STATE, count, length , virt, *labels %padlength - // stack: padlength, *state, count, length , offset, *labels + // stack: padlength, STATE, count, length , virt, *labels SWAP7 POP - // stack: *state, count, length = padlength, offset, *labels - %stack (a, b, c, d, e, count, length, offset) -> (a, b, c, d, e, count, length, 72) + // stack: STATE, count, length = padlength, virt, *labels + %stack (a, b, c, d, e, count, length, virt) -> (a, b, c, d, e, count, length, 72) %jump(ripemd_update) ripemd_2: - // stack: *state, count, length, offset, *labels - %stack (a, b, c, d, e, count, length, offset) -> (a, b, c, d, e, count, 8, 64) - // stack: *state, count, length, offset, *labels + // stack: STATE, count, length, virt, *labels + %stack (a, b, c, d, e, count, length, virt) -> (a, b, c, d, e, count, 8, 64) + // stack: STATE, count, length, virt, *labels %jump(ripemd_update) process: - // stack: a , b, c, d, e, count, length, offset + // stack: a , b, c, d, e, count, length, virt %flip_bytes_u32 // stack: a', b, c, d, e, *vars SWAP1 diff --git a/evm/src/cpu/kernel/asm/ripemd/update.asm b/evm/src/cpu/kernel/asm/ripemd/update.asm index e69de29b..59b476a3 100644 --- a/evm/src/cpu/kernel/asm/ripemd/update.asm +++ b/evm/src/cpu/kernel/asm/ripemd/update.asm @@ -0,0 +1,151 @@ +/// ripemd_update will receive and return the stack in the form: +/// stack: STATE, count, length, virt +/// +/// def ripemd_update(state, count, buffer, length, bytestring): +/// have = (count // 8) % 64 +/// need = 64 - have +/// shift = 0 +/// P = length >= need and have +/// Q = length >= need +/// if P: +/// update_1() +/// if Q: +/// update_2() +/// R = length - shift > 0 +/// if R: +/// buffer_update(virt + shift, have, length - shift) +/// +/// return state, count + 8*length, buffer + + +global ripemd_update: + // stack: STATE, count, length, virt, retdest + %stack (a, b, c, d, e, count, length, virt) -> (count, 8, 64, a, b, c, d, e, count, length, virt) + DIV + MOD + // stack: have, STATE, count, length, virt, retdest + DUP1 + PUSH 64 + SUB + PUSH 0 + // stack: shift, need, have, STATE, count, length, virt, retdest + %stack (shift, need, have, a, b, c, d, e, count, length) -> (length, need, a, b, c, d, e, 0, shift, need, have, count, length) + // stack: length, need, STATE, 0, shift, need, have, count, length, virt, retdest + LT + NOT + // stack: Q, STATE, 0, shift, need, have, count, length, virt, retdest + %stack (Q, STATE, 0, shift, need, have) -> (have, Q, Q, STATE, 0, shift, need, have) + AND + // stack: P, Q, STATE, 0, shift, need, have, count, length, virt, retdest + %jumpi(update_1) + // stack: Q, STATE, 0, shift, need, have, count, length, virt, retdest + %jumpi(update_2) +final_update: + // stack: shift, need, have, STATE, count, length, virt, retdest + %stack (shift, need, have, a, b, c, d, e, count, length) -> (length, shift, return_step, shift, need, have, a, b, c, d, e, count, length) + SUB + // stack: ARGS, shift, need, have, STATE, count, length, virt, retdest + %stack (a, r, shift, need, have, a, b, c, d, e, count, length, virt) -> (shift, virt, have, a, r, shift, need, have, a, b, c, d, e, count, length, virt) + ADD + // stack: ARGS, shift, need, have, STATE, count, length, virt, retdest + PUSH 0 + DUP4 + GT + // stack: R, ARGS, shift, need, have, STATE, count, length, virt, retdest + %jumpi(buffer_update) + // stack: ARGS, shift, need, have, STATE, count, length, virt, retdest + *pop3 + JUMP +return_step: + // stack: shift, need, have, STATE, count, length, virt, retdest + SWAP8 + DUP10 + %mul_const(8) + ADD + SWAP8 + // stack: shift, need, have, STATE, count += 8*length, length, virt, retdest + %stack (shift, need, have, a, b, c, d, e, count, length, virt, retdest) -> (retdest, a, b, c, d, e, count, length, virt) + JUMP + + +/// def update_1(): +/// buffer_update(virt, have, need) +/// shift = need +/// have = 0 +/// state = compress(state, buffer) + +update_1: + // stack: Q, STATE, 0, shift, need, have, count, length, virt, retdest + %stack (Q, a, b, c, d, e, 0, shift, need, have, count, length, virt) -> (virt, have, need, update_1a, a, b, c, d, e, 0, shift, need, have, count, length, virt) + %jump(buffer_update) +update_1a: + // stack: STATE, 0, shift, need, have, count, length, virt, retdest + %stack (a, b, c, d, e, 0, shift, need, have) -> (a, b, c, d, e, 0, update_2, need, need, 0) + // stack: STATE, 0, update_2, shift, need, have, count, length, virt, retdest + %jump(compress) + +/// def update_2(): +/// cond = length - shift - 64 +/// while cond >= 0: +/// state = compress(state, bytestring[shift:]) +/// shift += 64 +/// cond -= 64 + +update_2: + // stack: STATE, shift, need, have, count, length, virt, retdest + %stack (a, b, c, d, e, shift, need, have, count, length) -> (length, shift, a, b, c, d, e, shift, need, have, count, length) + SUB + SUB + // stack: cond, STATE, shift, need, have, count, length, virt, retdest + DUP12 + DUP8 + ADD + // stack: offset, cond, STATE, shift, need, have, count, length, virt, retdest + %stack (offset, cond, a, b, c, d, e) -> (cond, 0, a, b, c, d, e, offset, compression_loop, cond) + LT + NOT + // cond >= 0, STATE, offset, compression_loop, cond, shift, need, have, count, length, virt, retdest + %jumpi(compress) +compression_loop: + // stack: STATE, offset , cond , shift, need, have, count, length, virt, retdest + SWAP5 + %add_const(64) + SWAP5 + SWAP6 + %sub_const(64) + SWAP6 + SWAP7 + %add_const(64) + SWAP7 + // stack: STATE, offset+64, cond-64, shift+64, need, have, count, length, virt, retdest + %stack (a, b, c, d, e, offset, cond, shift) -> (cond, 0, a, b, c, d, e, offset, compression_loop, cond, shift) + %jumpi(compress) + // stack: STATE, offset , label, cond , shift , need, have, count, length, virt, retdest + %stack (a, b, c, d, e, offset, label, cond, shift, need, have, count, length, virt, retdest) -> (shift, need, have, a, b, c, d, e, count, length, virt, retdest) + %jump(final_update) + + +/// def buffer_update(get, set, times): +/// for i in range(times): +/// buffer[set+i] = bytestring[get+i] + +buffer_update: + // stack: get , set , times , retdest + DUP2 + DUP2 + // stack: get, set, get , set , times , retdest + %mupdate_ripemd + // stack: get , set , times , retdest + %add_const(1) + SWAP1 + %add_const(1) + SWAP1 + SWAP2 + %sub_const(1) + SWAP2 + // stack: get+1, set+1, times-1, retdest + DUP3 + %jumpi(buffer_update) + // stack: get , set , 0 , retdest + %pop3 + JUMP diff --git a/evm/src/memory/segments.rs b/evm/src/memory/segments.rs index b725211c..93ef8514 100644 --- a/evm/src/memory/segments.rs +++ b/evm/src/memory/segments.rs @@ -29,7 +29,7 @@ pub(crate) enum Segment { } impl Segment { - pub(crate) const COUNT: usize = 11; + pub(crate) const COUNT: usize = 12; pub(crate) fn all() -> [Self; Self::COUNT] { [ @@ -44,7 +44,7 @@ impl Segment { Self::TxnFields, Self::TxnData, Self::RlpRaw, - Self::RipeMD + Self::RipeMD, ] } From aa862b76621298507ce48a5efd8f0ae720c3ea7f Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 19 Sep 2022 18:15:22 -0700 Subject: [PATCH 34/97] delete needless --- evm/src/cpu/kernel/asm/util/basic_macros.asm | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/evm/src/cpu/kernel/asm/util/basic_macros.asm b/evm/src/cpu/kernel/asm/util/basic_macros.asm index d1b7bff3..bac1381c 100644 --- a/evm/src/cpu/kernel/asm/util/basic_macros.asm +++ b/evm/src/cpu/kernel/asm/util/basic_macros.asm @@ -258,6 +258,7 @@ %u32 %endmacro + // given u32 bytestring abcd return dcba %macro flip_bytes_u32 // stack: abcd @@ -304,20 +305,3 @@ ADD // stack: dcba %endmacro - - - - - - - - - -def flip_bytes(x): - acc = 0 - while x: - acc <<= 8 - d = x % (1<<8) - acc += d - x = (x - d)//(1<<8) - return acc \ No newline at end of file From 76d50f336c59619812c68eeb52de78f9995b8ab0 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 19 Sep 2022 18:38:43 -0700 Subject: [PATCH 35/97] duplicate macro, test skeleton --- evm/src/cpu/kernel/asm/util/basic_macros.asm | 7 ----- evm/src/cpu/kernel/tests/ripemd.rs | 27 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 evm/src/cpu/kernel/tests/ripemd.rs diff --git a/evm/src/cpu/kernel/asm/util/basic_macros.asm b/evm/src/cpu/kernel/asm/util/basic_macros.asm index bac1381c..232cb055 100644 --- a/evm/src/cpu/kernel/asm/util/basic_macros.asm +++ b/evm/src/cpu/kernel/asm/util/basic_macros.asm @@ -154,13 +154,6 @@ // stack: input >= c, ... %endmacro -%macro and_const(c) - // stack: x - PUSH $c - AND - // stack: c & x -%endmacro - %macro consume_gas_const(c) PUSH $c CONSUME_GAS diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs new file mode 100644 index 00000000..0c90b430 --- /dev/null +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -0,0 +1,27 @@ +use std::str::FromStr; + +use anyhow::Result; +use ethereum_types::U256; +use rand::{thread_rng, Rng}; +use sha2::{Digest, Sha256}; + +use crate::cpu::kernel::aggregator::combined_kernel; +use crate::cpu::kernel::interpreter::run; + +#[test] +fn test_ripemd() -> Result<()> { + let kernel = combined_kernel(); + let ripemd = kernel.global_labels["ripemd"]; + + let mut initial_stack = vec![U256::from(num_bytes)]; + initial_stack.extend(bytes); + + let after_ripemd = run(&kernel.code, ripemd, initial_stack, &kernel.prover_inputs)?; + let result = after_ripemd.stack()[1]; + let actual = format!("{:X}", result); + + EXPECTED = "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc" + assert_eq!(EXPECTED, actual); + + Ok(()) +} From a6f72ee31586bacb033f015c23c8777604a84987 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 19 Sep 2022 19:04:22 -0700 Subject: [PATCH 36/97] alt api for testing --- evm/src/cpu/kernel/asm/ripemd/constants.asm | 1 - evm/src/cpu/kernel/asm/ripemd/memory.asm | 20 +++++++++++++++- evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 12 +++++++++- evm/src/cpu/kernel/tests/mod.rs | 1 + evm/src/cpu/kernel/tests/ripemd.rs | 26 +++++++++++---------- 5 files changed, 45 insertions(+), 15 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/constants.asm b/evm/src/cpu/kernel/asm/ripemd/constants.asm index b99df72a..659ab47a 100644 --- a/evm/src/cpu/kernel/asm/ripemd/constants.asm +++ b/evm/src/cpu/kernel/asm/ripemd/constants.asm @@ -137,4 +137,3 @@ R_data: BYTES 04, 20, 32, 28 BYTES 24, 08, 52, 56 BYTES 00, 12, 36, 44 - \ No newline at end of file diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm index bc0e8a48..50169500 100644 --- a/evm/src/cpu/kernel/asm/ripemd/memory.asm +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -30,7 +30,25 @@ store_size: store_padding: // stack: i (init 63) %store_zeros(136, store_padding) - %jump(store_input) + %jump(store_input_alt) + + +store_input_alt: + // stack: rem, length, REM_INP + %stack (rem, length, head) -> (length, rem, 136, head, rem, length) + SUB + ADD + // stack: offset, byte, rem, length, REM_INP + %mstore_ripemd + // stack: rem, length, REM_INP + %sub_const(1) + DUP1 + // stack: rem - 1, rem - 1, length, REM_INP + %jumpi(store_input_alt) + // stack: 0, length + POP + %jump(ripemd_init) + store_input: // stack: ADDR , rem , length diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm index fb6777c7..dc5dd7b8 100644 --- a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -12,9 +12,19 @@ /// stack: STATE, count, length, virt /// where virt is the virtual address of the bytes argument +global ripemd_alt: + // stack: length, INPUT + %stack (length) -> (64, length, 0x80, 63, length, length) + // stack: 64, length, 0x80, 63, length, length, INPUT + %jump(ripemd_storage) // stores the following into memory + // init _buffer at virt 0 [consumes 64] + // store _size at virt 64 [consumes length] + // store _padding at virt 72 [consumes 0x80, 63] + // store _input at virt 136 [consumes length] + global ripemd: // stack: ADDR, length - $stack (a, b, c, length) -> (64, length, 0x80, 63, a, b, c, length, length) + %stack (a, b, c, length) -> (64, length, 0x80, 63, a, b, c, length, length) // stack: 64, length, 0x80, 63, a, b, c, length, length %jump(ripemd_storage) // stores the following into memory // init _buffer at virt 0 [consumes 64] diff --git a/evm/src/cpu/kernel/tests/mod.rs b/evm/src/cpu/kernel/tests/mod.rs index 925db56f..7917c385 100644 --- a/evm/src/cpu/kernel/tests/mod.rs +++ b/evm/src/cpu/kernel/tests/mod.rs @@ -3,6 +3,7 @@ mod curve_ops; mod ecrecover; mod exp; mod packing; +mod ripemd; mod rlp; mod transaction_parsing; diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index 0c90b430..6c711612 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -1,9 +1,4 @@ -use std::str::FromStr; - use anyhow::Result; -use ethereum_types::U256; -use rand::{thread_rng, Rng}; -use sha2::{Digest, Sha256}; use crate::cpu::kernel::aggregator::combined_kernel; use crate::cpu::kernel::interpreter::run; @@ -11,17 +6,24 @@ use crate::cpu::kernel::interpreter::run; #[test] fn test_ripemd() -> Result<()> { let kernel = combined_kernel(); - let ripemd = kernel.global_labels["ripemd"]; + let ripemd = kernel.global_labels["ripemd_alt"]; - let mut initial_stack = vec![U256::from(num_bytes)]; - initial_stack.extend(bytes); + let initial_stack = vec![ + 0x61, 0x62, 0x63, 0x64, + 0x65, 0x66, 0x67, 0x68, + 0x69, 0x6a, 0x6b, 0x6c, + 0x6d, 0x6e, 0x6f, 0x70, + 0x71, 0x72, 0x73, 0x74, + 0x75, 0x76, 0x77, 0x78, + 0x79, 0x7a + ]; - let after_ripemd = run(&kernel.code, ripemd, initial_stack, &kernel.prover_inputs)?; - let result = after_ripemd.stack()[1]; + let hashed = run(&kernel.code, ripemd, initial_stack, &kernel.prover_inputs)?; + let result = hashed.stack()[1]; let actual = format!("{:X}", result); - EXPECTED = "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc" - assert_eq!(EXPECTED, actual); + let expected = "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc"; + assert_eq!(expected, actual); Ok(()) } From 5d25683b1319c41cfe8b2a8889676b8c67064024 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 19 Sep 2022 19:06:48 -0700 Subject: [PATCH 37/97] test running --- evm/src/cpu/kernel/tests/ripemd.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index 6c711612..17c212bc 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -1,4 +1,5 @@ use anyhow::Result; +use ethereum_types::U256; use crate::cpu::kernel::aggregator::combined_kernel; use crate::cpu::kernel::interpreter::run; @@ -8,7 +9,7 @@ fn test_ripemd() -> Result<()> { let kernel = combined_kernel(); let ripemd = kernel.global_labels["ripemd_alt"]; - let initial_stack = vec![ + let input = vec![ 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, @@ -18,6 +19,7 @@ fn test_ripemd() -> Result<()> { 0x79, 0x7a ]; + let initial_stack = input.iter().map(|&x| U256::from(x as u32)).collect(); let hashed = run(&kernel.code, ripemd, initial_stack, &kernel.prover_inputs)?; let result = hashed.stack()[1]; let actual = format!("{:X}", result); From e36c17b6533b9355702b127803d505008acf1ad3 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 19 Sep 2022 19:12:38 -0700 Subject: [PATCH 38/97] test --- evm/src/cpu/kernel/tests/ripemd.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index 17c212bc..0d0dee5a 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -24,7 +24,9 @@ fn test_ripemd() -> Result<()> { let result = hashed.stack()[1]; let actual = format!("{:X}", result); + println!("{}", actual); let expected = "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc"; + println!("{}", expected); assert_eq!(expected, actual); Ok(()) From c127f80bfa956601df50371a07511582d73ce11a Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Tue, 20 Sep 2022 10:00:40 -0700 Subject: [PATCH 39/97] add to include files --- evm/src/cpu/kernel/aggregator.rs | 6 ++++++ evm/src/cpu/kernel/tests/ripemd.rs | 12 ++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/evm/src/cpu/kernel/aggregator.rs b/evm/src/cpu/kernel/aggregator.rs index dda006e6..115679ea 100644 --- a/evm/src/cpu/kernel/aggregator.rs +++ b/evm/src/cpu/kernel/aggregator.rs @@ -38,6 +38,12 @@ pub(crate) fn combined_kernel() -> Kernel { include_str!("asm/memory/metadata.asm"), include_str!("asm/memory/packing.asm"), include_str!("asm/memory/txn_fields.asm"), + include_str!("asm/ripemd/compression.asm"), + include_str!("asm/ripemd/constants.asm"), + include_str!("asm/ripemd/memory.asm"), + include_str!("asm/ripemd/ripemd.asm"), + include_str!("asm/ripemd/subroutines.asm"), + include_str!("asm/ripemd/update.asm"), include_str!("asm/rlp/encode.asm"), include_str!("asm/rlp/decode.asm"), include_str!("asm/rlp/read_to_memory.asm"), diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index 0d0dee5a..4bff5a40 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -1,3 +1,4 @@ +use std::env; use anyhow::Result; use ethereum_types::U256; @@ -6,10 +7,16 @@ use crate::cpu::kernel::interpreter::run; #[test] fn test_ripemd() -> Result<()> { + + env::set_var("RUST_BACKTRACE", "1"); + + let expected = "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc"; + println!("{}", expected); + let kernel = combined_kernel(); let ripemd = kernel.global_labels["ripemd_alt"]; - let input = vec![ + let input: Vec = vec![ 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, @@ -23,10 +30,7 @@ fn test_ripemd() -> Result<()> { let hashed = run(&kernel.code, ripemd, initial_stack, &kernel.prover_inputs)?; let result = hashed.stack()[1]; let actual = format!("{:X}", result); - println!("{}", actual); - let expected = "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc"; - println!("{}", expected); assert_eq!(expected, actual); Ok(()) From 9f1e97edc4586428144ed594f026b6ae7a33c396 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Tue, 20 Sep 2022 10:11:45 -0700 Subject: [PATCH 40/97] fix bugs --- evm/src/cpu/kernel/aggregator.rs | 8 ++++---- evm/src/cpu/kernel/asm/ripemd/memory.asm | 16 ++++++++-------- evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 12 ++++++------ 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/evm/src/cpu/kernel/aggregator.rs b/evm/src/cpu/kernel/aggregator.rs index 115679ea..7d624b7a 100644 --- a/evm/src/cpu/kernel/aggregator.rs +++ b/evm/src/cpu/kernel/aggregator.rs @@ -38,12 +38,12 @@ pub(crate) fn combined_kernel() -> Kernel { include_str!("asm/memory/metadata.asm"), include_str!("asm/memory/packing.asm"), include_str!("asm/memory/txn_fields.asm"), - include_str!("asm/ripemd/compression.asm"), - include_str!("asm/ripemd/constants.asm"), + // include_str!("asm/ripemd/compression.asm"), + // include_str!("asm/ripemd/constants.asm"), include_str!("asm/ripemd/memory.asm"), include_str!("asm/ripemd/ripemd.asm"), - include_str!("asm/ripemd/subroutines.asm"), - include_str!("asm/ripemd/update.asm"), + // include_str!("asm/ripemd/subroutines.asm"), + // include_str!("asm/ripemd/update.asm"), include_str!("asm/rlp/encode.asm"), include_str!("asm/rlp/decode.asm"), include_str!("asm/rlp/read_to_memory.asm"), diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm index 50169500..b9bfb422 100644 --- a/evm/src/cpu/kernel/asm/ripemd/memory.asm +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -22,9 +22,9 @@ store_size: // stack: ab %extract_and_store_byte(70) // stack: a - %mstore_ripemd(71) - // stack: 0x80 // padding has 0x80 in first position and zeros elsewhere - %mstore_ripemd(72) // store first padding term here so as to avoid extra label + %mstore_ripemd_offset(71) + // stack: 0x80 // padding has 0x80 in first position and zeros elsewhere + %mstore_ripemd_offset(72) // store first padding term here so as to avoid extra label %jump(store_padding) store_padding: @@ -107,26 +107,26 @@ store_input: DIV SWAP1 // stack: y, xs - %mstore_ripemd($offset) + %mstore_ripemd_offset($offset) // stack: xs %endmacro -%macro mstore_ripemd_offset($offset) +%macro mstore_ripemd_offset(offset) // stack: value PUSH $offset // stack: offset, value - %mstore_kernel(SEGMENT_RIPEMD) + %mstore_kernel(@SEGMENT_RIPEMD) // stack: %endmacro %macro mstore_ripemd // stack: offset, value - %mstore_kernel(SEGMENT_RIPEMD) + %mstore_kernel(@SEGMENT_RIPEMD) // stack: %endmacro %macro mload_ripemd - %mload_kernel(SEGMENT_RIPEMD) + %mload_kernel(@SEGMENT_RIPEMD) %endmacro // Load LE u32 from 4 contiguous bytes a, b, c, d in SEGMENT_RIPEMD diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm index dc5dd7b8..1a126d9b 100644 --- a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -35,7 +35,7 @@ ripemd_init: // stack: length %stack (length) -> ( 0, length, 136, ripemd_1, ripemd_2, process) // stack: count = 0, length, virt = 136, ripemd_1, ripemd_2, process - %stack (c, l, o, l1, l2, l3) -> (0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, c, l, o, l1, l2, l3) + %stack (c, l, o, l1, l2, l3) -> (0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, c, l, o, l1, l2, l3) // stack: 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, count, length, virt, *labels %jump(ripemd_update) ripemd_1: @@ -59,26 +59,26 @@ process: %flip_bytes_u32 // stack: a', b, c, d, e, *vars SWAP1 - %flip_bytes_32 + %flip_bytes_u32 %shl_const(32) OR // stack: b' a', c, d, e, *vars SWAP1 - %flip_bytes_32 + %flip_bytes_u32 %shl_const(64) OR // stack: c' b' a', d, e, *vars SWAP1 - %flip_bytes_32 + %flip_bytes_u32 %shl_const(96) OR // stack: d' c' b' a', e, *vars SWAP1 - %flip_bytes_32 + %flip_bytes_u32 %shl_const(96) OR // stack: e' d' c' b' a', *vars - %stack (result, x, y, z) -> result + %stack (result, x, y, z) -> (result) // stack: result From 172bde80285e5725e769985acf9ea1e5b9bb6d20 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Tue, 20 Sep 2022 10:29:43 -0700 Subject: [PATCH 41/97] everything is parsing --- evm/src/cpu/kernel/aggregator.rs | 8 ++++---- evm/src/cpu/kernel/asm/ripemd/constants.asm | 2 +- evm/src/cpu/kernel/asm/ripemd/memory.asm | 6 ++++-- evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 2 +- evm/src/cpu/kernel/asm/ripemd/subroutines.asm | 2 +- evm/src/cpu/kernel/asm/ripemd/update.asm | 8 ++++---- 6 files changed, 15 insertions(+), 13 deletions(-) diff --git a/evm/src/cpu/kernel/aggregator.rs b/evm/src/cpu/kernel/aggregator.rs index 7d624b7a..115679ea 100644 --- a/evm/src/cpu/kernel/aggregator.rs +++ b/evm/src/cpu/kernel/aggregator.rs @@ -38,12 +38,12 @@ pub(crate) fn combined_kernel() -> Kernel { include_str!("asm/memory/metadata.asm"), include_str!("asm/memory/packing.asm"), include_str!("asm/memory/txn_fields.asm"), - // include_str!("asm/ripemd/compression.asm"), - // include_str!("asm/ripemd/constants.asm"), + include_str!("asm/ripemd/compression.asm"), + include_str!("asm/ripemd/constants.asm"), include_str!("asm/ripemd/memory.asm"), include_str!("asm/ripemd/ripemd.asm"), - // include_str!("asm/ripemd/subroutines.asm"), - // include_str!("asm/ripemd/update.asm"), + include_str!("asm/ripemd/subroutines.asm"), + include_str!("asm/ripemd/update.asm"), include_str!("asm/rlp/encode.asm"), include_str!("asm/rlp/decode.asm"), include_str!("asm/rlp/read_to_memory.asm"), diff --git a/evm/src/cpu/kernel/asm/ripemd/constants.asm b/evm/src/cpu/kernel/asm/ripemd/constants.asm index 659ab47a..004ae846 100644 --- a/evm/src/cpu/kernel/asm/ripemd/constants.asm +++ b/evm/src/cpu/kernel/asm/ripemd/constants.asm @@ -5,7 +5,7 @@ // stack: K_data + 4*rnd %mload_kernel_code_u32 // stack: K -%end_macro +%endmacro K_data: // Left diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm index b9bfb422..1a5ededd 100644 --- a/evm/src/cpu/kernel/asm/ripemd/memory.asm +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -1,4 +1,4 @@ -ripemd_storage: // starts by initializing buffer +global ripemd_storage: // starts by initializing buffer // stack: i (init 64) %store_zeros(64, ripemd_storage) // stack: @@ -82,7 +82,9 @@ store_input: %macro store_zeros(N, label) // stack: i - %stack (i) -> ($N, i, 0, i) + PUSH 0 + DUP2 + PUSH $N SUB // stack: offset = N-i, 0, i %mstore_ripemd diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm index 1a126d9b..103da104 100644 --- a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -31,7 +31,7 @@ global ripemd: // store _size at virt 64 [consumes length] // store _padding at virt 72 [consumes 0x80, 63] // store _input at virt 136 [consumes ADDR, length] -ripemd_init: +global ripemd_init: // stack: length %stack (length) -> ( 0, length, 136, ripemd_1, ripemd_2, process) // stack: count = 0, length, virt = 136, ripemd_1, ripemd_2, process diff --git a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm index 54877521..0b855233 100644 --- a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm +++ b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm @@ -24,7 +24,7 @@ global rol: JUMP -%macro PUSH_F +%macro push_F PUSH 0 %this_F(0,F0) %this_F(1,F1) diff --git a/evm/src/cpu/kernel/asm/ripemd/update.asm b/evm/src/cpu/kernel/asm/ripemd/update.asm index 59b476a3..87f022ab 100644 --- a/evm/src/cpu/kernel/asm/ripemd/update.asm +++ b/evm/src/cpu/kernel/asm/ripemd/update.asm @@ -34,7 +34,7 @@ global ripemd_update: LT NOT // stack: Q, STATE, 0, shift, need, have, count, length, virt, retdest - %stack (Q, STATE, 0, shift, need, have) -> (have, Q, Q, STATE, 0, shift, need, have) + %stack (Q, a, b, c, d, e, i, shift, need, have) -> (have, Q, Q, a, b, c, d, e, i, shift, need, have) AND // stack: P, Q, STATE, 0, shift, need, have, count, length, virt, retdest %jumpi(update_1) @@ -54,7 +54,7 @@ final_update: // stack: R, ARGS, shift, need, have, STATE, count, length, virt, retdest %jumpi(buffer_update) // stack: ARGS, shift, need, have, STATE, count, length, virt, retdest - *pop3 + %pop3 JUMP return_step: // stack: shift, need, have, STATE, count, length, virt, retdest @@ -76,11 +76,11 @@ return_step: update_1: // stack: Q, STATE, 0, shift, need, have, count, length, virt, retdest - %stack (Q, a, b, c, d, e, 0, shift, need, have, count, length, virt) -> (virt, have, need, update_1a, a, b, c, d, e, 0, shift, need, have, count, length, virt) + %stack (Q, a, b, c, d, e, i, shift, need, have, count, length, virt) -> (virt, have, need, update_1a, a, b, c, d, e, i, shift, need, have, count, length, virt) %jump(buffer_update) update_1a: // stack: STATE, 0, shift, need, have, count, length, virt, retdest - %stack (a, b, c, d, e, 0, shift, need, have) -> (a, b, c, d, e, 0, update_2, need, need, 0) + %stack (a, b, c, d, e, i, shift, need, have) -> (a, b, c, d, e, i, update_2, need, need, 0) // stack: STATE, 0, update_2, shift, need, have, count, length, virt, retdest %jump(compress) From bb968bd725680574567609f6d0907d26336f9514 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Tue, 20 Sep 2022 21:40:22 -0700 Subject: [PATCH 42/97] it runs (but forever...) --- evm/src/cpu/kernel/asm/ripemd/compression.asm | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index fa68a9d7..7f807e36 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -42,21 +42,8 @@ global compress: // stack: STATE, 0, 0, 16, 5, 1, virt, switch, STATE, virt, retdest %jump(loop) switch: - // stack: STATEL, STATE, virt, retdest - PUSH mix - DUP12 - PUSH 0 - PUSH 5 - PUSH 16 - PUSH 0 - PUSH 0 - // stack: 0, 0, 16, 5, 0, virt, mix, STATEL, STATE, virt, retdest - DUP17 - DUP17 - DUP17 - DUP17 - DUP17 - // stack: STATE, 0, 0, 16, 5, 0, virt, mix, STATEL, STATE, virt, retdest + // stack: STATEL, STATE, virt, retdest + %stack (STATEL: 5, STATE: 5, virt) -> (STATE, 0, 0, 16, 5, 0, virt, mix, STATEL, STATE, virt) %jump(loop) mix: // stack: r0, r1, r2, r3, r4, l0, l1, l2, l3, l4, s0, s1, s2, s3, s4, VR, RD From 3b0dda77f2c9e71596222d1a92baa50621746589 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Wed, 21 Sep 2022 12:54:19 -0700 Subject: [PATCH 43/97] fix test --- evm/src/cpu/kernel/tests/ripemd.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index 4bff5a40..152547b3 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -17,13 +17,13 @@ fn test_ripemd() -> Result<()> { let ripemd = kernel.global_labels["ripemd_alt"]; let input: Vec = vec![ - 0x61, 0x62, 0x63, 0x64, - 0x65, 0x66, 0x67, 0x68, - 0x69, 0x6a, 0x6b, 0x6c, - 0x6d, 0x6e, 0x6f, 0x70, - 0x71, 0x72, 0x73, 0x74, - 0x75, 0x76, 0x77, 0x78, - 0x79, 0x7a + 26 , 0x61, 0x62, + 0x63, 0x64, 0x65, 0x66, + 0x67, 0x68, 0x69, 0x6a, + 0x6b, 0x6c, 0x6d, 0x6e, + 0x6f, 0x70, 0x71, 0x72, + 0x73, 0x74, 0x75, 0x76, + 0x77, 0x78, 0x79, 0x7a ]; let initial_stack = input.iter().map(|&x| U256::from(x as u32)).collect(); From 6cac2d79f87b5b1da1fa54e0c465efb8c2c6034d Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Wed, 21 Sep 2022 13:42:13 -0700 Subject: [PATCH 44/97] use blocks --- evm/src/cpu/kernel/asm/ripemd/compression.asm | 2 +- evm/src/cpu/kernel/asm/ripemd/memory.asm | 10 ++--- evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 31 ++++++++------- evm/src/cpu/kernel/asm/ripemd/subroutines.asm | 2 +- evm/src/cpu/kernel/asm/ripemd/update.asm | 38 +++++++++---------- evm/src/cpu/kernel/tests/ripemd.rs | 37 ++++++++++++------ 6 files changed, 67 insertions(+), 53 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index 7f807e36..b6eaf63e 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -115,7 +115,7 @@ loop: // stack: round, STATE, F, K, 16, rounds, sides, virt, retdest %jumpi(update_round_vars) // stack: STATE, F, K, 16, 0, sides, virt, retdest - %stack (a, b, c, d, e, F, K, boxes, rounds, sides, virt, retdest) -> (retdest, a, b, c, d, e) + %stack (STATE: 5, F, K, boxes, rounds, sides, virt, retdest) -> (retdest, STATE) // stack: retdest, STATE JUMP update_round_vars: diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm index 1a5ededd..c1dff4a5 100644 --- a/evm/src/cpu/kernel/asm/ripemd/memory.asm +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -1,8 +1,8 @@ global ripemd_storage: // starts by initializing buffer - // stack: i (init 64) + // stack: i [init: 64] %store_zeros(64, ripemd_storage) - // stack: - %jump(store_size) + // stack: + // %jump(store_size) store_size: // stack: length @@ -82,9 +82,7 @@ store_input: %macro store_zeros(N, label) // stack: i - PUSH 0 - DUP2 - PUSH $N + %stack (i) -> ($N, i, 0, i) SUB // stack: offset = N-i, 0, i %mstore_ripemd diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm index 103da104..25422a26 100644 --- a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -15,7 +15,8 @@ global ripemd_alt: // stack: length, INPUT %stack (length) -> (64, length, 0x80, 63, length, length) - // stack: 64, length, 0x80, 63, length, length, INPUT + // stack: 64, length, 0x80, 63, length, length, INPUT + %jump(ripemd_storage) // stores the following into memory // init _buffer at virt 0 [consumes 64] // store _size at virt 64 [consumes length] @@ -23,36 +24,38 @@ global ripemd_alt: // store _input at virt 136 [consumes length] global ripemd: - // stack: ADDR, length - %stack (a, b, c, length) -> (64, length, 0x80, 63, a, b, c, length, length) - // stack: 64, length, 0x80, 63, a, b, c, length, length + // stack: ADDR, length + %stack (ADDR: 3, length) -> (64, length, 0x80, 63, ADDR, length, length) + // stack: 64, length, 0x80, 63, ADDR, length, length %jump(ripemd_storage) // stores the following into memory // init _buffer at virt 0 [consumes 64] // store _size at virt 64 [consumes length] // store _padding at virt 72 [consumes 0x80, 63] // store _input at virt 136 [consumes ADDR, length] + global ripemd_init: // stack: length %stack (length) -> ( 0, length, 136, ripemd_1, ripemd_2, process) // stack: count = 0, length, virt = 136, ripemd_1, ripemd_2, process - %stack (c, l, o, l1, l2, l3) -> (0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, c, l, o, l1, l2, l3) - // stack: 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, count, length, virt, *labels + %stack (ARGS: 3, LABELS: 3) -> (0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, ARGS, LABELS) + // stack: 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, count, length, virt, LABELS %jump(ripemd_update) ripemd_1: - // stack: STATE, count, length , virt, *labels + // stack: STATE, count, length , virt , LABELS DUP7 - // stack: length, STATE, count, length , virt, *labels + // stack: length, STATE, count, length , virt , LABELS %padlength - // stack: padlength, STATE, count, length , virt, *labels + // stack: padlength, STATE, count, length , virt , LABELS SWAP7 POP - // stack: STATE, count, length = padlength, virt, *labels - %stack (a, b, c, d, e, count, length, virt) -> (a, b, c, d, e, count, length, 72) + // stack: STATE, count, length = padlength, virt , LABELS + %stack (STATE: 5, count, length, virt) -> (STATE, count, length, 72) + // STATE, count, length , virt = 72, LABELS %jump(ripemd_update) ripemd_2: - // stack: STATE, count, length, virt, *labels - %stack (a, b, c, d, e, count, length, virt) -> (a, b, c, d, e, count, 8, 64) - // stack: STATE, count, length, virt, *labels + // stack: STATE, count, length , virt , LABELS + %stack (STATE: 5, count, length, virt) -> (STATE, count, 8, 64) + // stack: STATE, count, length = 8, virt = 64, LABELS %jump(ripemd_update) process: // stack: a , b, c, d, e, count, length, virt diff --git a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm index 0b855233..b7a97a2a 100644 --- a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm +++ b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm @@ -141,5 +141,5 @@ global F4: // stack: ~z | y, x, retdest XOR // stack: (~z | y) ^ x, retdest - SWAP1 + SWAP1 JUMP diff --git a/evm/src/cpu/kernel/asm/ripemd/update.asm b/evm/src/cpu/kernel/asm/ripemd/update.asm index 87f022ab..82d078c8 100644 --- a/evm/src/cpu/kernel/asm/ripemd/update.asm +++ b/evm/src/cpu/kernel/asm/ripemd/update.asm @@ -20,7 +20,7 @@ global ripemd_update: // stack: STATE, count, length, virt, retdest - %stack (a, b, c, d, e, count, length, virt) -> (count, 8, 64, a, b, c, d, e, count, length, virt) + %stack (STATE: 5, count, length, virt) -> (count, 8, 64, STATE, count, length, virt) DIV MOD // stack: have, STATE, count, length, virt, retdest @@ -28,13 +28,13 @@ global ripemd_update: PUSH 64 SUB PUSH 0 - // stack: shift, need, have, STATE, count, length, virt, retdest - %stack (shift, need, have, a, b, c, d, e, count, length) -> (length, need, a, b, c, d, e, 0, shift, need, have, count, length) - // stack: length, need, STATE, 0, shift, need, have, count, length, virt, retdest + // stack: shift, need, have, STATE, count, length, virt, retdest + %stack (shift, need, have, STATE: 5, count, length) -> (length, need, STATE, 0, shift, need, have, count, length) + // stack: length, need, STATE, 0, shift, need, have, count, length, virt, retdest LT NOT // stack: Q, STATE, 0, shift, need, have, count, length, virt, retdest - %stack (Q, a, b, c, d, e, i, shift, need, have) -> (have, Q, Q, a, b, c, d, e, i, shift, need, have) + %stack (Q, STATE: 5, i, shift, need, have) -> (have, Q, Q, STATE, i, shift, need, have) AND // stack: P, Q, STATE, 0, shift, need, have, count, length, virt, retdest %jumpi(update_1) @@ -42,18 +42,18 @@ global ripemd_update: %jumpi(update_2) final_update: // stack: shift, need, have, STATE, count, length, virt, retdest - %stack (shift, need, have, a, b, c, d, e, count, length) -> (length, shift, return_step, shift, need, have, a, b, c, d, e, count, length) + %stack (shift, need, have, STATE: 5, count, length) -> (length, shift, return_step, shift, need, have, STATE, count, length) SUB - // stack: ARGS, shift, need, have, STATE, count, length, virt, retdest - %stack (a, r, shift, need, have, a, b, c, d, e, count, length, virt) -> (shift, virt, have, a, r, shift, need, have, a, b, c, d, e, count, length, virt) + // stack: ARGS, shift, need, have, STATE, count, length, virt, retdest + %stack (ARGS: 2, shift, need, have, STATE: 5, count, length, virt) -> (shift, virt, have, ARGS: 2, shift, need, have, STATE, count, length, virt) ADD - // stack: ARGS, shift, need, have, STATE, count, length, virt, retdest + // stack: ARGS: 4, shift, need, have, STATE, count, length, virt, retdest PUSH 0 DUP4 GT - // stack: R, ARGS, shift, need, have, STATE, count, length, virt, retdest + // stack: R, ARGS, shift, need, have, STATE, count, length, virt, retdest %jumpi(buffer_update) - // stack: ARGS, shift, need, have, STATE, count, length, virt, retdest + // stack: ARGS, shift, need, have, STATE, count, length, virt, retdest %pop3 JUMP return_step: @@ -64,7 +64,7 @@ return_step: ADD SWAP8 // stack: shift, need, have, STATE, count += 8*length, length, virt, retdest - %stack (shift, need, have, a, b, c, d, e, count, length, virt, retdest) -> (retdest, a, b, c, d, e, count, length, virt) + %stack (shift, need, have, STATE: 5, count, length, virt, retdest) -> (retdest, STATE, count, length, virt) JUMP @@ -76,12 +76,12 @@ return_step: update_1: // stack: Q, STATE, 0, shift, need, have, count, length, virt, retdest - %stack (Q, a, b, c, d, e, i, shift, need, have, count, length, virt) -> (virt, have, need, update_1a, a, b, c, d, e, i, shift, need, have, count, length, virt) + %stack (Q, STATE: 5, i, shift, need, have, count, length, virt) -> (virt, have, need, update_1a, STATE, i, shift, need, have, count, length, virt) %jump(buffer_update) update_1a: // stack: STATE, 0, shift, need, have, count, length, virt, retdest - %stack (a, b, c, d, e, i, shift, need, have) -> (a, b, c, d, e, i, update_2, need, need, 0) - // stack: STATE, 0, update_2, shift, need, have, count, length, virt, retdest + %stack (STATE: 5, i, shift, need, have) -> (STATE, i, update_2, need, need, 0) + // stack: STATE, 0, update_2, shift = need, need, have = 0, count, length, virt, retdest %jump(compress) /// def update_2(): @@ -93,7 +93,7 @@ update_1a: update_2: // stack: STATE, shift, need, have, count, length, virt, retdest - %stack (a, b, c, d, e, shift, need, have, count, length) -> (length, shift, a, b, c, d, e, shift, need, have, count, length) + %stack (STATE: 5, shift, need, have, count, length) -> (length, shift, STATE, shift, need, have, count, length) SUB SUB // stack: cond, STATE, shift, need, have, count, length, virt, retdest @@ -101,7 +101,7 @@ update_2: DUP8 ADD // stack: offset, cond, STATE, shift, need, have, count, length, virt, retdest - %stack (offset, cond, a, b, c, d, e) -> (cond, 0, a, b, c, d, e, offset, compression_loop, cond) + %stack (offset, cond, STATE: 5) -> (cond, 0, STATE, offset, compression_loop, cond) LT NOT // cond >= 0, STATE, offset, compression_loop, cond, shift, need, have, count, length, virt, retdest @@ -118,10 +118,10 @@ compression_loop: %add_const(64) SWAP7 // stack: STATE, offset+64, cond-64, shift+64, need, have, count, length, virt, retdest - %stack (a, b, c, d, e, offset, cond, shift) -> (cond, 0, a, b, c, d, e, offset, compression_loop, cond, shift) + %stack (STATE: 5, offset, cond, shift) -> (cond, 0, STATE, offset, compression_loop, cond, shift) %jumpi(compress) // stack: STATE, offset , label, cond , shift , need, have, count, length, virt, retdest - %stack (a, b, c, d, e, offset, label, cond, shift, need, have, count, length, virt, retdest) -> (shift, need, have, a, b, c, d, e, count, length, virt, retdest) + %stack (STATE: 5, offset, label, cond, shift, need, have, count, length, virt, retdest) -> (shift, need, have, STATE, count, length, virt, retdest) %jump(final_update) diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index 152547b3..d62f3105 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -1,4 +1,4 @@ -use std::env; +// use std::env; use anyhow::Result; use ethereum_types::U256; @@ -8,13 +8,12 @@ use crate::cpu::kernel::interpreter::run; #[test] fn test_ripemd() -> Result<()> { - env::set_var("RUST_BACKTRACE", "1"); + // env::set_var("RUST_BACKTRACE", "1"); - let expected = "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc"; - println!("{}", expected); + // let expected = "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc"; + // println!("{}", expected); let kernel = combined_kernel(); - let ripemd = kernel.global_labels["ripemd_alt"]; let input: Vec = vec![ 26 , 0x61, 0x62, @@ -23,15 +22,29 @@ fn test_ripemd() -> Result<()> { 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, - 0x77, 0x78, 0x79, 0x7a + 0x77, 0x78, 0x79, 0x7a, ]; - let initial_stack = input.iter().map(|&x| U256::from(x as u32)).collect(); - let hashed = run(&kernel.code, ripemd, initial_stack, &kernel.prover_inputs)?; - let result = hashed.stack()[1]; - let actual = format!("{:X}", result); - println!("{}", actual); - assert_eq!(expected, actual); + // let input: Vec = vec![0xabcd, 0x1234, 0x6789]; + let stack_init = input.iter().map(|&x| U256::from(x as u32)).collect(); + + let ripemd = kernel.global_labels["F0"]; + let hashed = run( + &kernel.code, + ripemd, + stack_init, + &kernel.prover_inputs)?; + let result = hashed.stack(); + + for term in input { + println!("{:X}", term); + } + + for term in result { + println!("{:X}", term); + } + + // assert_eq!(expected, actual); Ok(()) } From 98d9e6cae223b9d42a7746dd755d941f496d9efb Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Wed, 21 Sep 2022 16:34:34 -0700 Subject: [PATCH 45/97] parse error --- evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 1 + evm/src/cpu/kernel/asm/ripemd/update.asm | 2 +- evm/src/cpu/kernel/tests/ripemd.rs | 12 +++++------- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm index 25422a26..cdf8d12c 100644 --- a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -17,6 +17,7 @@ global ripemd_alt: %stack (length) -> (64, length, 0x80, 63, length, length) // stack: 64, length, 0x80, 63, length, length, INPUT + %jump(0xdeadbeef) %jump(ripemd_storage) // stores the following into memory // init _buffer at virt 0 [consumes 64] // store _size at virt 64 [consumes length] diff --git a/evm/src/cpu/kernel/asm/ripemd/update.asm b/evm/src/cpu/kernel/asm/ripemd/update.asm index 82d078c8..edfb64b3 100644 --- a/evm/src/cpu/kernel/asm/ripemd/update.asm +++ b/evm/src/cpu/kernel/asm/ripemd/update.asm @@ -45,7 +45,7 @@ final_update: %stack (shift, need, have, STATE: 5, count, length) -> (length, shift, return_step, shift, need, have, STATE, count, length) SUB // stack: ARGS, shift, need, have, STATE, count, length, virt, retdest - %stack (ARGS: 2, shift, need, have, STATE: 5, count, length, virt) -> (shift, virt, have, ARGS: 2, shift, need, have, STATE, count, length, virt) + %stack (ARGS: 2, shift, need, have, STATE: 5, count, length, virt) -> (shift, virt, have, ARGS, shift, need, have, STATE, count, length, virt) ADD // stack: ARGS: 4, shift, need, have, STATE, count, length, virt, retdest PUSH 0 diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index d62f3105..ed75cc9e 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -24,17 +24,15 @@ fn test_ripemd() -> Result<()> { 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, ]; - - // let input: Vec = vec![0xabcd, 0x1234, 0x6789]; let stack_init = input.iter().map(|&x| U256::from(x as u32)).collect(); - let ripemd = kernel.global_labels["F0"]; - let hashed = run( + let stack_result = run( &kernel.code, - ripemd, + kernel.global_labels["ripemd_alt"], stack_init, - &kernel.prover_inputs)?; - let result = hashed.stack(); + &kernel.prover_inputs + )?; + let result = stack_result.stack(); for term in input { println!("{:X}", term); From 94390c3cc7fee18f1260637f7bada019134a8bc3 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Wed, 21 Sep 2022 16:40:27 -0700 Subject: [PATCH 46/97] fix --- evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 17 ++++++++--------- evm/src/cpu/kernel/tests/ripemd.rs | 23 ++++++----------------- 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm index cdf8d12c..156dc496 100644 --- a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -16,8 +16,6 @@ global ripemd_alt: // stack: length, INPUT %stack (length) -> (64, length, 0x80, 63, length, length) // stack: 64, length, 0x80, 63, length, length, INPUT - - %jump(0xdeadbeef) %jump(ripemd_storage) // stores the following into memory // init _buffer at virt 0 [consumes 64] // store _size at virt 64 [consumes length] @@ -61,29 +59,30 @@ ripemd_2: process: // stack: a , b, c, d, e, count, length, virt %flip_bytes_u32 - // stack: a', b, c, d, e, *vars + // stack: a', b, c, d, e, VARS SWAP1 %flip_bytes_u32 %shl_const(32) OR - // stack: b' a', c, d, e, *vars + // stack: b' a', c, d, e, VARS SWAP1 %flip_bytes_u32 %shl_const(64) OR - // stack: c' b' a', d, e, *vars + // stack: c' b' a', d, e, VARS SWAP1 %flip_bytes_u32 %shl_const(96) OR - // stack: d' c' b' a', e, *vars + // stack: d' c' b' a', e, VARS SWAP1 %flip_bytes_u32 %shl_const(96) OR - // stack: e' d' c' b' a', *vars - %stack (result, x, y, z) -> (result) - // stack: result + // stack: e' d' c' b' a', VARS + %stack (result, VARS: 3) -> (0xdeadbeef, result) + // stack: 0xdeadbeef, result + JUMP /// def padlength(length): diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index ed75cc9e..e5a35bbc 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -1,4 +1,3 @@ -// use std::env; use anyhow::Result; use ethereum_types::U256; @@ -7,11 +6,8 @@ use crate::cpu::kernel::interpreter::run; #[test] fn test_ripemd() -> Result<()> { - - // env::set_var("RUST_BACKTRACE", "1"); - - // let expected = "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc"; - // println!("{}", expected); + let expected = "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc"; + println!("{}", expected); let kernel = combined_kernel(); @@ -32,17 +28,10 @@ fn test_ripemd() -> Result<()> { stack_init, &kernel.prover_inputs )?; - let result = stack_result.stack(); - - for term in input { - println!("{:X}", term); - } - - for term in result { - println!("{:X}", term); - } - - // assert_eq!(expected, actual); + let result = stack_result.stack()[1]; + let actual = format!("{}", result); + println!("{}", actual); + assert_eq!(expected, actual); Ok(()) } From 4783a90c949cfa2c4d07453f763c4e3c2042936b Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Wed, 21 Sep 2022 16:45:47 -0700 Subject: [PATCH 47/97] tweak --- evm/src/cpu/kernel/tests/ripemd.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index e5a35bbc..60b2dbb8 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -20,7 +20,9 @@ fn test_ripemd() -> Result<()> { 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, ]; - let stack_init = input.iter().map(|&x| U256::from(x as u32)).collect(); + let mut stack_init:Vec = input.iter().map(|&x| U256::from(x as u32)).collect(); + stack_init.push(U256::from_str("0xdeadbeef").unwrap()); + let stack_result = run( &kernel.code, @@ -29,7 +31,7 @@ fn test_ripemd() -> Result<()> { &kernel.prover_inputs )?; let result = stack_result.stack()[1]; - let actual = format!("{}", result); + let actual = format!("{:X}", result); println!("{}", actual); assert_eq!(expected, actual); From 28f980744293aa5c8bddaa0e3a3a04c567204574 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Wed, 21 Sep 2022 16:49:46 -0700 Subject: [PATCH 48/97] 0xdeadbeef --- evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 2 +- evm/src/cpu/kernel/tests/ripemd.rs | 46 +++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm index 156dc496..917ef937 100644 --- a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -80,7 +80,7 @@ process: %shl_const(96) OR // stack: e' d' c' b' a', VARS - %stack (result, VARS: 3) -> (0xdeadbeef, result) + %stack (result, VARS: 3, fin) -> (fin, result) // stack: 0xdeadbeef, result JUMP diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index 60b2dbb8..845c665f 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -1,3 +1,4 @@ +use std::str::FromStr; use anyhow::Result; use ethereum_types::U256; @@ -12,7 +13,7 @@ fn test_ripemd() -> Result<()> { let kernel = combined_kernel(); let input: Vec = vec![ - 26 , 0x61, 0x62, + 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, @@ -24,6 +25,7 @@ fn test_ripemd() -> Result<()> { stack_init.push(U256::from_str("0xdeadbeef").unwrap()); + let stack_result = run( &kernel.code, kernel.global_labels["ripemd_alt"], @@ -37,3 +39,45 @@ fn test_ripemd() -> Result<()> { Ok(()) } + + +// use std::str::FromStr; + +// use anyhow::Result; +// use ethereum_types::U256; +// use rand::{thread_rng, Rng}; +// use sha2::{Digest, Sha256}; + +// use crate::cpu::kernel::aggregator::combined_kernel; +// use crate::cpu::kernel::interpreter::run; + +// #[test] +// fn test_sha2() -> Result<()> { +// let kernel = combined_kernel(); +// let sha2 = kernel.global_labels["sha2"]; + +// let mut rng = thread_rng(); + +// let num_bytes = rng.gen_range(0..10000); +// let message: Vec = (0..num_bytes).map(|_| rng.gen()).collect(); + +// let mut hasher = Sha256::new(); +// hasher.update(message.clone()); +// let expected = format!("{:X}", hasher.finalize()); + +// let bytes: Vec = message.iter().map(|&x| U256::from(x as u32)).collect(); + +// let mut initial_stack = vec![U256::from(num_bytes)]; +// initial_stack.extend(bytes); +// initial_stack.push(U256::from_str("0xdeadbeef").unwrap()); +// initial_stack.reverse(); + +// let after_sha2 = run(&kernel.code, sha2, initial_stack, &kernel.prover_inputs)?; +// let stack_after_sha2 = after_sha2.stack(); +// let result = stack_after_sha2[1]; +// let actual = format!("{:X}", result); + +// assert_eq!(expected, actual); + +// Ok(()) +// } From aa5537a960e8b93357e0014f347ee71d676046df Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Wed, 21 Sep 2022 19:37:09 -0700 Subject: [PATCH 49/97] fake test --- evm/src/cpu/kernel/tests/ripemd.rs | 71 +++++++++++++++++++----------- 1 file changed, 45 insertions(+), 26 deletions(-) diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index 845c665f..b87de8cb 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -1,41 +1,60 @@ -use std::str::FromStr; use anyhow::Result; use ethereum_types::U256; +use rand::{thread_rng, Rng}; use crate::cpu::kernel::aggregator::combined_kernel; -use crate::cpu::kernel::interpreter::run; +use crate::cpu::kernel::interpreter::{run, run_with_kernel}; #[test] fn test_ripemd() -> Result<()> { - let expected = "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc"; - println!("{}", expected); - + // Make sure we can parse and assemble the entire kernel. let kernel = combined_kernel(); + let exp = kernel.global_labels["exp"]; + let mut rng = thread_rng(); + let a = U256([0; 4].map(|_| rng.gen())); + let b = U256([0; 4].map(|_| rng.gen())); - let input: Vec = vec![ - 0x61, 0x62, - 0x63, 0x64, 0x65, 0x66, - 0x67, 0x68, 0x69, 0x6a, - 0x6b, 0x6c, 0x6d, 0x6e, - 0x6f, 0x70, 0x71, 0x72, - 0x73, 0x74, 0x75, 0x76, - 0x77, 0x78, 0x79, 0x7a, - ]; - let mut stack_init:Vec = input.iter().map(|&x| U256::from(x as u32)).collect(); - stack_init.push(U256::from_str("0xdeadbeef").unwrap()); + // Random input + let initial_stack = vec![0xDEADBEEFu32.into(), b, a]; + let stack_with_kernel = run_with_kernel(&kernel, exp, initial_stack)? + .stack() + .to_vec(); + let initial_stack = vec![b, a]; + let code = [0xa, 0x63, 0xde, 0xad, 0xbe, 0xef, 0x56]; // EXP, PUSH4 deadbeef, JUMP + let stack_with_opcode = run(&code, 0, initial_stack, &kernel.prover_inputs)? + .stack() + .to_vec(); + assert_eq!(stack_with_kernel, stack_with_opcode); + + // let expected = "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc"; + // println!("{}", expected); + + // let kernel = combined_kernel(); + + // let input: Vec = vec![ + // 0x61, 0x62, + // 0x63, 0x64, 0x65, 0x66, + // 0x67, 0x68, 0x69, 0x6a, + // 0x6b, 0x6c, 0x6d, 0x6e, + // 0x6f, 0x70, 0x71, 0x72, + // 0x73, 0x74, 0x75, 0x76, + // 0x77, 0x78, 0x79, 0x7a, + // ]; + // let mut stack_init:Vec = input.iter().map(|&x| U256::from(x as u32)).collect(); + // stack_init.push(U256::from_str("0xdeadbeef").unwrap()); - let stack_result = run( - &kernel.code, - kernel.global_labels["ripemd_alt"], - stack_init, - &kernel.prover_inputs - )?; - let result = stack_result.stack()[1]; - let actual = format!("{:X}", result); - println!("{}", actual); - assert_eq!(expected, actual); + // let stack_result = run( + // &kernel.code, + // kernel.global_labels["ripemd_alt"], + // stack_init, + // &kernel.prover_inputs + // )?; + // let result = stack_result.stack()[1]; + // let actual = format!("{:X}", result); + // println!("{}", actual); + // assert_eq!(expected, actual); Ok(()) } From a25aea8e2ecd6e8ad0abbf9901dd4a3ce6e3ca13 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Wed, 21 Sep 2022 20:03:11 -0700 Subject: [PATCH 50/97] Nick's comments --- evm/src/cpu/kernel/asm/memory/core.asm | 9 ++++ evm/src/cpu/kernel/asm/ripemd/compression.asm | 36 ++++++---------- evm/src/cpu/kernel/asm/ripemd/constants.asm | 10 ----- evm/src/cpu/kernel/asm/ripemd/memory.asm | 1 - evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 10 ++--- evm/src/cpu/kernel/asm/ripemd/subroutines.asm | 43 ++++++++++++------- evm/src/cpu/kernel/asm/util/basic_macros.asm | 6 +-- 7 files changed, 56 insertions(+), 59 deletions(-) diff --git a/evm/src/cpu/kernel/asm/memory/core.asm b/evm/src/cpu/kernel/asm/memory/core.asm index 73bafbee..3ecfa240 100644 --- a/evm/src/cpu/kernel/asm/memory/core.asm +++ b/evm/src/cpu/kernel/asm/memory/core.asm @@ -62,6 +62,15 @@ // stack: value %endmacro +%macro mload_kernel_code_shift(label) + // stack: shift + PUSH $label + ADD + // stack: label + shift + %mload_kernel_code + // stack: byte +%endmacro + // Load a big-endian u32, consisting of 4 bytes (c_3, c_2, c_1, c_0), // from kernel code. %macro mload_kernel_code_u32 diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index b6eaf63e..6fb54f6c 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -25,21 +25,9 @@ /// state[i], stateL[i], stateR[i], output[i], virt, retdest global compress: - // stack: STATE, virt, retdest - PUSH switch - DUP7 - PUSH 1 - PUSH 5 - PUSH 16 - PUSH 0 - PUSH 0 - // stack: 0, 0, 16, 5, 1, virt, switch, STATE, virt, retdest - DUP12 - DUP12 - DUP12 - DUP12 - DUP12 - // stack: STATE, 0, 0, 16, 5, 1, virt, switch, STATE, virt, retdest + // stack: STATE, virt, retdest + %stack (STATE: 5, virt) -> (STATE, 0, 0, 16, 5, 1, virt, switch, STATE, virt) + // stack: STATE, 0, 0, 16, 5, 1, virt, switch, STATE, virt, retdest %jump(loop) switch: // stack: STATEL, STATE, virt, retdest @@ -53,7 +41,7 @@ mix: // stack: r1, s0, r2, r3, r4, l0, l1, l2, l3, l4, r0, s1, s2, s3, s4, VR, RD SWAP6 // stack: l1, s0, r2, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, VR, RD - %add3_32 + %add3_u32 // stack: o4, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, VR, RD SWAP14 // stack: RD, r3, r4, l0, r1, l2, l3, l4, r0, s1, s2, s3, s4, VR, o4 @@ -65,23 +53,23 @@ mix: // stack: r3, s2, r4, l0, r1, l2, l3, l4, r0, s1, s3, RD, s4, VR, o4 SWAP6 // stack: l3, s2, r4, l0, r1, l2, r3, l4, r0, s1, s3, RD, s4, VR, o4 - %add3_32 + %add3_u32 // stack: o1, l0, r1, l2, r3, l4, r0, s1, s3, RD, s4, VR, o4 SWAP9 // stack: RD, l0, r1, l2, r3, l4, r0, s1, s3, o1, s4, VR, o4 SWAP10 // stack: s4, l0, r1, l2, r3, l4, r0, s1, s3, o1, RD, VR, o4 - %add3_32 + %add3_u32 // stack: o3, l2, r3, l4, r0, s1, s3, o1, RD, VR, o4 SWAP9 // stack: VR, l2, r3, l4, r0, s1, s3, o1, RD, o3, o4 SWAP5 // stack: s1, l2, r3, l4, r0, VR, s3, o1, RD, o3, o4 - %add3_32 + %add3_u32 // stack: o0, l4, r0, VR, s3, o1, RD, o3, o4 SWAP4 // stack: s3, l4, r0, VR, o0, o1, RD, o3, o4 - %add3_32 + %add3_u32 // stack: o2, VR, o0, o1, RD, o3, o4 SWAP4 // stack: RD, VR, o0, o1, o2, o3, o4 @@ -164,10 +152,10 @@ round: /// /// box = get_box(sides, rounds, boxes) /// a += F(b, c, d) -/// r = load_byte(r)(box) +/// r = load(r)(box) /// x = load_offset(r) /// a += x + K -/// s = load_byte(s)(box) +/// s = load(s)(box) /// a = rol(s, a) /// a += e /// c = rol(10, c) @@ -191,7 +179,7 @@ pre_rol: %get_box // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, virt DUP1 - %load_byte(R_data) + %mload_kernel_code_shift(R_data) DUP13 ADD // stack: virt + r, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt @@ -208,7 +196,7 @@ pre_rol: PUSH mid_rol SWAP2 // stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, virt - %load_byte(S_data) + %mload_kernel_code_shift(S_data) // stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, virt %jump(rol) mid_rol: diff --git a/evm/src/cpu/kernel/asm/ripemd/constants.asm b/evm/src/cpu/kernel/asm/ripemd/constants.asm index 004ae846..622a9943 100644 --- a/evm/src/cpu/kernel/asm/ripemd/constants.asm +++ b/evm/src/cpu/kernel/asm/ripemd/constants.asm @@ -22,16 +22,6 @@ K_data: BYTES 0x00, 0x00, 0x00, 0x00 -%macro load_byte(loc) - // stack: box - PUSH $loc - ADD - // stack: loc + box - %mload_kernel_code - // stack: byte -%endmacro - - S_data: // Left Round 0 BYTES 11, 14, 15, 12 diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm index c1dff4a5..49678837 100644 --- a/evm/src/cpu/kernel/asm/ripemd/memory.asm +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -164,4 +164,3 @@ store_input: %mstore_ripemd // stack: %endmacro - \ No newline at end of file diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm index 917ef937..4ba955f0 100644 --- a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -58,25 +58,25 @@ ripemd_2: %jump(ripemd_update) process: // stack: a , b, c, d, e, count, length, virt - %flip_bytes_u32 + %reverse_bytes_u32 // stack: a', b, c, d, e, VARS SWAP1 - %flip_bytes_u32 + %reverse_bytes_u32 %shl_const(32) OR // stack: b' a', c, d, e, VARS SWAP1 - %flip_bytes_u32 + %reverse_bytes_u32 %shl_const(64) OR // stack: c' b' a', d, e, VARS SWAP1 - %flip_bytes_u32 + %reverse_bytes_u32 %shl_const(96) OR // stack: d' c' b' a', e, VARS SWAP1 - %flip_bytes_u32 + %reverse_bytes_u32 %shl_const(96) OR // stack: e' d' c' b' a', VARS diff --git a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm index b7a97a2a..8e1181ae 100644 --- a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm +++ b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm @@ -23,19 +23,30 @@ global rol: SWAP1 JUMP +// def push_F(rnd): +// Fs = [F0, F1, F2, F3, F4, F4, F3, F2, F1, F0] +// acc = 0 +// for i, F in enumerate(Fs): +// acc += (i==rnd)*F +// return acc, rnd +// +// the macro %this_F(i,F) enacts +// acc += (i==rnd)*F %macro push_F - PUSH 0 - %this_F(0,F0) - %this_F(1,F1) - %this_F(2,F2) - %this_F(3,F3) - %this_F(4,F4) - %this_F(5,F4) - %this_F(6,F3) - %this_F(7,F2) - %this_F(8,F1) - %this_F(9,F0) + // stack: rnd + PUSH 0 + %this_F(0,F0) + %this_F(1,F1) + %this_F(2,F2) + %this_F(3,F3) + %this_F(4,F4) + %this_F(5,F4) + %this_F(6,F3) + %this_F(7,F2) + %this_F(8,F1) + %this_F(9,F0) + // stack: F, rnd %endmacro @@ -44,7 +55,7 @@ global rol: DUP2 // stack: rnd , acc, rnd %eq_const($i) - // stack: rnd==i , acc, j + // stack: rnd==i , acc, rnd %mul_const($F) // stack: (rnd==i)*F , acc, rnd ADD @@ -80,7 +91,7 @@ global F1: // stack: z, x, y & x , retdest SWAP1 // stack: x, z, y & x , retdest - %not_32 + %not_u32 // stack: ~x, z, y & x , retdest AND // stack: ~x & z , y & x , retdest @@ -97,7 +108,7 @@ global F2: // stack: x , y, z, retdest SWAP1 // stack: y , x, z, retdest - %not_32 + %not_u32 // stack: ~y , x , z, retdest OR // stack: ~y | x , z, retdest @@ -118,7 +129,7 @@ global F3: // stack: z & x, y , z , retdest SWAP2 // stack: z, y, z & x , retdest - %not_32 + %not_u32 // stack: ~z , y, z & x , retdest AND // stack: ~z & y, z & x , retdest @@ -135,7 +146,7 @@ global F4: // stack: x, y, z, retdest SWAP2 // stack: z, y, x, retdest - %not_32 + %not_u32 // stack: ~z, y, x, retdest OR // stack: ~z | y, x, retdest diff --git a/evm/src/cpu/kernel/asm/util/basic_macros.asm b/evm/src/cpu/kernel/asm/util/basic_macros.asm index 232cb055..8eb60647 100644 --- a/evm/src/cpu/kernel/asm/util/basic_macros.asm +++ b/evm/src/cpu/kernel/asm/util/basic_macros.asm @@ -234,7 +234,7 @@ %and_const(0xffffffff) %endmacro -%macro not_32 +%macro not_u32 // stack: x PUSH 0xffffffff // stack: 0xffffffff, x @@ -242,7 +242,7 @@ // stack: 0xffffffff - x %endmacro -%macro add3_32 +%macro add3_u32 // stack: x, y, z ADD // stack: x+y, z @@ -253,7 +253,7 @@ // given u32 bytestring abcd return dcba -%macro flip_bytes_u32 +%macro reverse_bytes_u32 // stack: abcd DUP1 %and_const(0xFF) From cfa01865178ee64277951bc0e5dd4ad38140e19a Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Wed, 21 Sep 2022 20:06:41 -0700 Subject: [PATCH 51/97] Nick's comments --- evm/src/cpu/kernel/asm/ripemd/compression.asm | 2 +- evm/src/cpu/kernel/asm/ripemd/subroutines.asm | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index 6fb54f6c..6c5c254f 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -115,7 +115,7 @@ update_round_vars: // stack: rnd, rnd, STATE, F , K , 16, rounds, sides, virt, retdest SWAP7 POP - %push_F + %push_f SWAP7 // stack: rnd, rnd, STATE, F', K , 16, rounds, sides, virt, retdest SWAP8 diff --git a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm index 8e1181ae..cc4cab29 100644 --- a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm +++ b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm @@ -23,7 +23,7 @@ global rol: SWAP1 JUMP -// def push_F(rnd): +// def push_f(rnd): // Fs = [F0, F1, F2, F3, F4, F4, F3, F2, F1, F0] // acc = 0 // for i, F in enumerate(Fs): @@ -33,7 +33,7 @@ global rol: // the macro %this_F(i,F) enacts // acc += (i==rnd)*F -%macro push_F +%macro push_f // stack: rnd PUSH 0 %this_F(0,F0) From 937955788f4b074298dd337fecf12af914dcc84f Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Wed, 21 Sep 2022 20:18:18 -0700 Subject: [PATCH 52/97] cargo format --- evm/src/cpu/kernel/tests/ripemd.rs | 19 ++++++++----------- evm/src/memory/segments.rs | 4 ++-- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index b87de8cb..734b7709 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -32,34 +32,31 @@ fn test_ripemd() -> Result<()> { // let kernel = combined_kernel(); // let input: Vec = vec![ - // 0x61, 0x62, - // 0x63, 0x64, 0x65, 0x66, - // 0x67, 0x68, 0x69, 0x6a, - // 0x6b, 0x6c, 0x6d, 0x6e, - // 0x6f, 0x70, 0x71, 0x72, - // 0x73, 0x74, 0x75, 0x76, + // 0x61, 0x62, + // 0x63, 0x64, 0x65, 0x66, + // 0x67, 0x68, 0x69, 0x6a, + // 0x6b, 0x6c, 0x6d, 0x6e, + // 0x6f, 0x70, 0x71, 0x72, + // 0x73, 0x74, 0x75, 0x76, // 0x77, 0x78, 0x79, 0x7a, // ]; // let mut stack_init:Vec = input.iter().map(|&x| U256::from(x as u32)).collect(); // stack_init.push(U256::from_str("0xdeadbeef").unwrap()); - - // let stack_result = run( - // &kernel.code, + // &kernel.code, // kernel.global_labels["ripemd_alt"], // stack_init, // &kernel.prover_inputs // )?; // let result = stack_result.stack()[1]; // let actual = format!("{:X}", result); - // println!("{}", actual); + // println!("{}", actual); // assert_eq!(expected, actual); Ok(()) } - // use std::str::FromStr; // use anyhow::Result; diff --git a/evm/src/memory/segments.rs b/evm/src/memory/segments.rs index 5bfe0f99..5b3725d7 100644 --- a/evm/src/memory/segments.rs +++ b/evm/src/memory/segments.rs @@ -37,7 +37,7 @@ pub(crate) enum Segment { /// See also `StateTrieCheckpointPointer`. StorageTrieCheckpointPointers = 14, /// RipeMD storage - /// + /// RipeMd = 15, } @@ -83,7 +83,7 @@ impl Segment { Segment::StorageTrieAddresses => "SEGMENT_STORAGE_TRIE_ADDRS", Segment::StorageTriePointers => "SEGMENT_STORAGE_TRIE_PTRS", Segment::StorageTrieCheckpointPointers => "SEGMENT_STORAGE_TRIE_CHECKPOINT_PTRS", - Segment::RipeMd => "SEGMENT_RIPEMD" + Segment::RipeMd => "SEGMENT_RIPEMD", } } From 9506c647de8eadc22b5527fbf84c9a2fef808c6b Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 22 Sep 2022 08:38:23 -0700 Subject: [PATCH 53/97] fix macro --- evm/src/cpu/kernel/asm/ripemd/compression.asm | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index 6c5c254f..f92df818 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -230,7 +230,10 @@ post_rol: %macro get_round // stack: sides, rounds - %mul_const(5) PUSH 10 sub sub + %mul_const(5) + PUSH 10 + SUB + SUB // stack: 10 - 5*sides - rounds %endmacro From 65b393cb95ab46a2d841b51437c6af29fb6db8c7 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 22 Sep 2022 09:16:55 -0700 Subject: [PATCH 54/97] assembler is running --- evm/src/cpu/kernel/asm/ripemd/compression.asm | 7 +++++-- evm/src/cpu/kernel/asm/ripemd/constants.asm | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index f92df818..b1199a21 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -26,12 +26,15 @@ global compress: // stack: STATE, virt, retdest - %stack (STATE: 5, virt) -> (STATE, 0, 0, 16, 5, 1, virt, switch, STATE, virt) + %stack (x) -> (0, 0, 16, 5, 1, x) + %stack (CONSTS: 5, virt, switch, STATE: 5) -> (STATE, CONSTS, virt, switch, STATE) // stack: STATE, 0, 0, 16, 5, 1, virt, switch, STATE, virt, retdest %jump(loop) switch: // stack: STATEL, STATE, virt, retdest - %stack (STATEL: 5, STATE: 5, virt) -> (STATE, 0, 0, 16, 5, 0, virt, mix, STATEL, STATE, virt) + %stack (STATEL: 5, STATE: 5) -> (STATE, STATEL, STATE) + %stack (STATE: 5) -> (STATE, 0, 0, 16, 5, 0) + // %stack (STATEL: 5, STATE: 5, virt) -> (STATE, 0, 0, 16, 5, 0, virt, mix, STATEL, STATE, virt) %jump(loop) mix: // stack: r0, r1, r2, r3, r4, l0, l1, l2, l3, l4, s0, s1, s2, s3, s4, VR, RD diff --git a/evm/src/cpu/kernel/asm/ripemd/constants.asm b/evm/src/cpu/kernel/asm/ripemd/constants.asm index 622a9943..0b0865e5 100644 --- a/evm/src/cpu/kernel/asm/ripemd/constants.asm +++ b/evm/src/cpu/kernel/asm/ripemd/constants.asm @@ -7,7 +7,7 @@ // stack: K %endmacro -K_data: +global K_data: // Left BYTES 0x00, 0x00, 0x00, 0x00 BYTES 0x5A, 0x82, 0x79, 0x99 @@ -22,7 +22,7 @@ K_data: BYTES 0x00, 0x00, 0x00, 0x00 -S_data: +global S_data: // Left Round 0 BYTES 11, 14, 15, 12 BYTES 05, 08, 07, 09 @@ -76,7 +76,7 @@ S_data: BYTES 15, 13, 11, 11 -R_data: +global R_data: // Left Round 0 BYTES 00, 04, 08, 12 BYTES 16, 20, 24, 28 From 7ee583551c3e339cc1cd563aac7baad4eecceb89 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 22 Sep 2022 09:27:11 -0700 Subject: [PATCH 55/97] test is running, but failing --- evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 4 +- evm/src/cpu/kernel/tests/ripemd.rs | 113 ++++++----------------- 2 files changed, 29 insertions(+), 88 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm index 4ba955f0..693fde53 100644 --- a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -80,9 +80,9 @@ process: %shl_const(96) OR // stack: e' d' c' b' a', VARS - %stack (result, VARS: 3, fin) -> (fin, result) + %stack (result, VARS: 3) -> (0xdeadbeef, result) // stack: 0xdeadbeef, result - JUMP + JUMP /// def padlength(length): diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index 734b7709..7cb34da0 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -1,99 +1,40 @@ use anyhow::Result; use ethereum_types::U256; -use rand::{thread_rng, Rng}; use crate::cpu::kernel::aggregator::combined_kernel; -use crate::cpu::kernel::interpreter::{run, run_with_kernel}; +use crate::cpu::kernel::interpreter::run_with_kernel; #[test] fn test_ripemd() -> Result<()> { - // Make sure we can parse and assemble the entire kernel. + let expected = "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc"; + println!("{}", expected); + let kernel = combined_kernel(); - let exp = kernel.global_labels["exp"]; - let mut rng = thread_rng(); - let a = U256([0; 4].map(|_| rng.gen())); - let b = U256([0; 4].map(|_| rng.gen())); - // Random input - let initial_stack = vec![0xDEADBEEFu32.into(), b, a]; - let stack_with_kernel = run_with_kernel(&kernel, exp, initial_stack)? - .stack() - .to_vec(); - let initial_stack = vec![b, a]; - let code = [0xa, 0x63, 0xde, 0xad, 0xbe, 0xef, 0x56]; // EXP, PUSH4 deadbeef, JUMP - let stack_with_opcode = run(&code, 0, initial_stack, &kernel.prover_inputs)? - .stack() - .to_vec(); - assert_eq!(stack_with_kernel, stack_with_opcode); + let input: Vec = vec![ + 26, 0x61, 0x62, + 0x63, 0x64, 0x65, 0x66, + 0x67, 0x68, 0x69, 0x6a, + 0x6b, 0x6c, 0x6d, 0x6e, + 0x6f, 0x70, 0x71, 0x72, + 0x73, 0x74, 0x75, 0x76, + 0x77, 0x78, 0x79, 0x7a, + ]; + let stack_init:Vec = input + .iter() + .map(|&x| U256::from(x as u32)) + .rev() + .collect(); - // let expected = "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc"; - // println!("{}", expected); - - // let kernel = combined_kernel(); - - // let input: Vec = vec![ - // 0x61, 0x62, - // 0x63, 0x64, 0x65, 0x66, - // 0x67, 0x68, 0x69, 0x6a, - // 0x6b, 0x6c, 0x6d, 0x6e, - // 0x6f, 0x70, 0x71, 0x72, - // 0x73, 0x74, 0x75, 0x76, - // 0x77, 0x78, 0x79, 0x7a, - // ]; - // let mut stack_init:Vec = input.iter().map(|&x| U256::from(x as u32)).collect(); - // stack_init.push(U256::from_str("0xdeadbeef").unwrap()); - - // let stack_result = run( - // &kernel.code, - // kernel.global_labels["ripemd_alt"], - // stack_init, - // &kernel.prover_inputs - // )?; - // let result = stack_result.stack()[1]; - // let actual = format!("{:X}", result); - // println!("{}", actual); - // assert_eq!(expected, actual); + let stack_result = run_with_kernel( + &kernel, + kernel.global_labels["ripemd_alt"], + stack_init, + )?; + let result = stack_result.stack()[0]; + let actual = format!("{:X}", result); + println!("{}", actual); + assert_eq!(expected, actual); Ok(()) } - -// use std::str::FromStr; - -// use anyhow::Result; -// use ethereum_types::U256; -// use rand::{thread_rng, Rng}; -// use sha2::{Digest, Sha256}; - -// use crate::cpu::kernel::aggregator::combined_kernel; -// use crate::cpu::kernel::interpreter::run; - -// #[test] -// fn test_sha2() -> Result<()> { -// let kernel = combined_kernel(); -// let sha2 = kernel.global_labels["sha2"]; - -// let mut rng = thread_rng(); - -// let num_bytes = rng.gen_range(0..10000); -// let message: Vec = (0..num_bytes).map(|_| rng.gen()).collect(); - -// let mut hasher = Sha256::new(); -// hasher.update(message.clone()); -// let expected = format!("{:X}", hasher.finalize()); - -// let bytes: Vec = message.iter().map(|&x| U256::from(x as u32)).collect(); - -// let mut initial_stack = vec![U256::from(num_bytes)]; -// initial_stack.extend(bytes); -// initial_stack.push(U256::from_str("0xdeadbeef").unwrap()); -// initial_stack.reverse(); - -// let after_sha2 = run(&kernel.code, sha2, initial_stack, &kernel.prover_inputs)?; -// let stack_after_sha2 = after_sha2.stack(); -// let result = stack_after_sha2[1]; -// let actual = format!("{:X}", result); - -// assert_eq!(expected, actual); - -// Ok(()) -// } From 3e371e5486a4f003a973faff788924a64867578b Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 22 Sep 2022 09:31:42 -0700 Subject: [PATCH 56/97] style --- evm/src/cpu/kernel/asm/ripemd/compression.asm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index b1199a21..215539c3 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -241,16 +241,16 @@ post_rol: %endmacro %macro get_box - // stack: *7_args, boxes, rounds, sides + // stack: ARGS: 7, boxes, rounds, sides DUP10 %mul_const(80) DUP10 %mul_const(16) DUP10 - // stack: boxes , 16*rounds , 80*sides, *7_args, boxes, rounds, sides + // stack: boxes , 16*rounds , 80*sides, ARGS: 7, boxes, rounds, sides PUSH 176 SUB SUB SUB - // stack: 176 - boxes - 16*rounds - 80*sides, *7_args, boxes, rounds, sides + // stack: 176 - boxes - 16*rounds - 80*sides, ARGS: 7, boxes, rounds, sides %endmacro From 81675e6eda789a69f1e6e10718962a0e3eda8bd5 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 22 Sep 2022 09:32:06 -0700 Subject: [PATCH 57/97] cargo format --- evm/src/cpu/kernel/tests/ripemd.rs | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index 7cb34da0..43157a9e 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -12,25 +12,12 @@ fn test_ripemd() -> Result<()> { let kernel = combined_kernel(); let input: Vec = vec![ - 26, 0x61, 0x62, - 0x63, 0x64, 0x65, 0x66, - 0x67, 0x68, 0x69, 0x6a, - 0x6b, 0x6c, 0x6d, 0x6e, - 0x6f, 0x70, 0x71, 0x72, - 0x73, 0x74, 0x75, 0x76, - 0x77, 0x78, 0x79, 0x7a, + 26, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, + 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, ]; - let stack_init:Vec = input - .iter() - .map(|&x| U256::from(x as u32)) - .rev() - .collect(); + let stack_init: Vec = input.iter().map(|&x| U256::from(x as u32)).rev().collect(); - let stack_result = run_with_kernel( - &kernel, - kernel.global_labels["ripemd_alt"], - stack_init, - )?; + let stack_result = run_with_kernel(&kernel, kernel.global_labels["ripemd_alt"], stack_init)?; let result = stack_result.stack()[0]; let actual = format!("{:X}", result); println!("{}", actual); From f45d6593f9a51100a9de2f0b099c4824306c91bd Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 22 Sep 2022 09:40:40 -0700 Subject: [PATCH 58/97] fix constants --- evm/src/cpu/kernel/asm/memory/core.asm | 11 ++++++++++- evm/src/cpu/kernel/asm/ripemd/compression.asm | 9 +++++---- evm/src/cpu/kernel/asm/ripemd/constants.asm | 9 --------- evm/src/cpu/kernel/tests/ripemd.rs | 2 +- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/evm/src/cpu/kernel/asm/memory/core.asm b/evm/src/cpu/kernel/asm/memory/core.asm index 3ecfa240..0fc85106 100644 --- a/evm/src/cpu/kernel/asm/memory/core.asm +++ b/evm/src/cpu/kernel/asm/memory/core.asm @@ -62,7 +62,7 @@ // stack: value %endmacro -%macro mload_kernel_code_shift(label) +%macro mload_kernel_code_label(label) // stack: shift PUSH $label ADD @@ -71,6 +71,15 @@ // stack: byte %endmacro +%macro mload_kernel_code_label_u32(label) + // stack: shift + PUSH $label + ADD + // stack: label + shift + %mload_kernel_code_u32 + // stack: byte +%endmacro + // Load a big-endian u32, consisting of 4 bytes (c_3, c_2, c_1, c_0), // from kernel code. %macro mload_kernel_code_u32 diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index 215539c3..d62d6857 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -26,7 +26,7 @@ global compress: // stack: STATE, virt, retdest - %stack (x) -> (0, 0, 16, 5, 1, x) + %stack (stack) -> (0, 0, 16, 5, 1, stack) %stack (CONSTS: 5, virt, switch, STATE: 5) -> (STATE, CONSTS, virt, switch, STATE) // stack: STATE, 0, 0, 16, 5, 1, virt, switch, STATE, virt, retdest %jump(loop) @@ -123,7 +123,8 @@ update_round_vars: // stack: rnd, rnd, STATE, F', K , 16, rounds, sides, virt, retdest SWAP8 POP - %load_K + %mul_const(4) + %mload_kernel_code_label_u32(K_data) SWAP7 POP // stack: STATE, F', K', 16, rounds, sides, virt, retdest @@ -182,7 +183,7 @@ pre_rol: %get_box // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, virt DUP1 - %mload_kernel_code_shift(R_data) + %mload_kernel_code_label(R_data) DUP13 ADD // stack: virt + r, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt @@ -199,7 +200,7 @@ pre_rol: PUSH mid_rol SWAP2 // stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, virt - %mload_kernel_code_shift(S_data) + %mload_kernel_code_label(S_data) // stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, virt %jump(rol) mid_rol: diff --git a/evm/src/cpu/kernel/asm/ripemd/constants.asm b/evm/src/cpu/kernel/asm/ripemd/constants.asm index 0b0865e5..18bf3595 100644 --- a/evm/src/cpu/kernel/asm/ripemd/constants.asm +++ b/evm/src/cpu/kernel/asm/ripemd/constants.asm @@ -1,12 +1,3 @@ -%macro load_K - // stack: rnd - %mul_const(4) - %add_const(K_data) - // stack: K_data + 4*rnd - %mload_kernel_code_u32 - // stack: K -%endmacro - global K_data: // Left BYTES 0x00, 0x00, 0x00, 0x00 diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index 43157a9e..c5ddf96b 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -12,7 +12,7 @@ fn test_ripemd() -> Result<()> { let kernel = combined_kernel(); let input: Vec = vec![ - 26, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, + 0x1a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, ]; let stack_init: Vec = input.iter().map(|&x| U256::from(x as u32)).rev().collect(); From 7342b9658355e052e6ce4940305c92134867a8d3 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 22 Sep 2022 10:34:32 -0700 Subject: [PATCH 59/97] test compress --- evm/src/cpu/kernel/asm/ripemd/compression.asm | 18 ++++++------- evm/src/cpu/kernel/asm/ripemd/constants.asm | 19 +++++++++++++ evm/src/cpu/kernel/tests/ripemd.rs | 27 ++++++++++--------- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index d62d6857..c8cbd020 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -177,31 +177,31 @@ box: // stack: F, b, c, d, pre_rol, a, b, c, d, e, F, K, boxes, rounds, sides, virt JUMP pre_rol: - // stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides, virt + // stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides, virt ADD - // stack: a, b, c, d, e, F, K, boxes, rounds, sides, virt + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, virt %get_box - // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, virt + // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, virt DUP1 %mload_kernel_code_label(R_data) DUP13 ADD // stack: virt + r, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt - %load_u32_from_block - // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt + %mload_kernel_code_label_u32(Input_Block) // %load_u32_from_block + // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt SWAP1 SWAP2 - // stack: a, x, box, b, c, d, e, F, K, boxes, rounds, sides, virt + // stack: a, x, box, b, c, d, e, F, K, boxes, rounds, sides, virt ADD DUP8 ADD %u32 - // stack: a, box, b, c, d, e, F, K, boxes, rounds, sides, virt + // stack: a, box, b, c, d, e, F, K, boxes, rounds, sides, virt PUSH mid_rol SWAP2 - // stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, virt + // stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, virt %mload_kernel_code_label(S_data) - // stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, virt + // stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, virt %jump(rol) mid_rol: // stack: a, b, c, d, e, F, K, boxes, rounds, sides, virt diff --git a/evm/src/cpu/kernel/asm/ripemd/constants.asm b/evm/src/cpu/kernel/asm/ripemd/constants.asm index 18bf3595..6fb33490 100644 --- a/evm/src/cpu/kernel/asm/ripemd/constants.asm +++ b/evm/src/cpu/kernel/asm/ripemd/constants.asm @@ -118,3 +118,22 @@ global R_data: BYTES 04, 20, 32, 28 BYTES 24, 08, 52, 56 BYTES 00, 12, 36, 44 + + +global Input_Block: + BYTES 0x61, 0x62, 0x63, 0x64 + BYTES 0x65, 0x66, 0x67, 0x68 + BYTES 0x69, 0x6a, 0x6b, 0x6c + BYTES 0x6d, 0x6e, 0x6f, 0x70 + BYTES 0x71, 0x72, 0x73, 0x74 + BYTES 0x75, 0x76, 0x77, 0x78 + BYTES 0x79, 0x7a, 0x80, 0x00 + BYTES 0x00, 0x00, 0x00, 0x00 + BYTES 0x00, 0x00, 0x00, 0x00 + BYTES 0x00, 0x00, 0x00, 0x00 + BYTES 0x00, 0x00, 0x00, 0x00 + BYTES 0x00, 0x00, 0x00, 0x00 + BYTES 0x00, 0x00, 0x00, 0x00 + BYTES 0x00, 0x00, 0x00, 0x00 + BYTES 0xd0, 0x00, 0x00, 0x00 + BYTES 0x00, 0x00, 0x00, 0x00 \ No newline at end of file diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index c5ddf96b..acdb235b 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -4,23 +4,24 @@ use ethereum_types::U256; use crate::cpu::kernel::aggregator::combined_kernel; use crate::cpu::kernel::interpreter::run_with_kernel; + #[test] fn test_ripemd() -> Result<()> { - let expected = "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc"; - println!("{}", expected); + // 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 input: Vec = vec![ + // 0x1a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, + // 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, + // ]; let kernel = combined_kernel(); - - let input: Vec = vec![ - 0x1a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, - 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, - ]; - let stack_init: Vec = input.iter().map(|&x| U256::from(x as u32)).rev().collect(); - - let stack_result = run_with_kernel(&kernel, kernel.global_labels["ripemd_alt"], stack_init)?; - let result = stack_result.stack()[0]; - let actual = format!("{:X}", result); - println!("{}", actual); + let stack_input: Vec = input.iter().map(|&x| U256::from(x as u32)).rev().collect(); + let stack_output = run_with_kernel(&kernel, kernel.global_labels["compress"], stack_input)?; + let actual: Vec = stack_output.stack().iter().map(|&x| format!("{:#?}", x)).collect(); + println!("{:#?}", actual); assert_eq!(expected, actual); Ok(()) From 0dfd1b64507ae2b3488a5d4bfe048825880a8701 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 22 Sep 2022 23:19:21 -0700 Subject: [PATCH 60/97] fix stack manipulation --- evm/src/cpu/kernel/asm/ripemd/compression.asm | 38 +++++++++++++++---- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index c8cbd020..67757738 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -25,16 +25,38 @@ /// state[i], stateL[i], stateR[i], output[i], virt, retdest global compress: - // stack: STATE, virt, retdest - %stack (stack) -> (0, 0, 16, 5, 1, stack) - %stack (CONSTS: 5, virt, switch, STATE: 5) -> (STATE, CONSTS, virt, switch, STATE) - // stack: STATE, 0, 0, 16, 5, 1, virt, switch, STATE, virt, retdest + // stack: STATE, virt, retdest + PUSH switch + DUP7 + PUSH 1 + PUSH 5 + PUSH 16 + PUSH 0 + PUSH 0 + // stack: 0, 0, 16, 5, 1, virt, switch, STATE, virt, retdest + DUP12 + DUP12 + DUP12 + DUP12 + DUP12 + // stack: STATE, 0, 0, 16, 5, 1, virt, switch, STATE, virt, retdest %jump(loop) switch: - // stack: STATEL, STATE, virt, retdest - %stack (STATEL: 5, STATE: 5) -> (STATE, STATEL, STATE) - %stack (STATE: 5) -> (STATE, 0, 0, 16, 5, 0) - // %stack (STATEL: 5, STATE: 5, virt) -> (STATE, 0, 0, 16, 5, 0, virt, mix, STATEL, STATE, virt) + // stack: STATEL, STATE, virt, retdest + PUSH mix + DUP12 + PUSH 0 + PUSH 5 + PUSH 16 + // stack: 16, 5, 0, virt, mix, STATEL, STATE, virt, retdest + DUP15 + DUP15 + DUP15 + DUP15 + DUP15 + // stack: STATE, 16, 5, 0, virt, mix, STATEL, STATE, virt, retdest + %stack (STATE: 5) -> (STATE, 0, 0) + // stack: STATE, 0, 0, 16, 5, 0, virt, mix, STATEL, STATE, virt, retdest %jump(loop) mix: // stack: r0, r1, r2, r3, r4, l0, l1, l2, l3, l4, s0, s1, s2, s3, s4, VR, RD From 4a5ddfda0a4c81d72afcc32f6300296e7f38c327 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 22 Sep 2022 23:33:43 -0700 Subject: [PATCH 61/97] shr --- evm/src/cpu/kernel/asm/memory/core.asm | 2 +- evm/src/cpu/kernel/interpreter.rs | 8 +++++++- evm/src/cpu/kernel/tests/ripemd.rs | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/evm/src/cpu/kernel/asm/memory/core.asm b/evm/src/cpu/kernel/asm/memory/core.asm index 0fc85106..963e88ea 100644 --- a/evm/src/cpu/kernel/asm/memory/core.asm +++ b/evm/src/cpu/kernel/asm/memory/core.asm @@ -77,7 +77,7 @@ ADD // stack: label + shift %mload_kernel_code_u32 - // stack: byte + // stack: u32 %endmacro // Load a big-endian u32, consisting of 4 bytes (c_3, c_2, c_1, c_0), diff --git a/evm/src/cpu/kernel/interpreter.rs b/evm/src/cpu/kernel/interpreter.rs index 64d70529..e36b1e66 100644 --- a/evm/src/cpu/kernel/interpreter.rs +++ b/evm/src/cpu/kernel/interpreter.rs @@ -211,7 +211,7 @@ impl<'a> Interpreter<'a> { 0x19 => self.run_not(), // "NOT", 0x1a => self.run_byte(), // "BYTE", 0x1b => self.run_shl(), // "SHL", - 0x1c => todo!(), // "SHR", + 0x1c => self.run_shr(), // "SHR", 0x1d => todo!(), // "SAR", 0x20 => self.run_keccak256(), // "KECCAK256", 0x30 => todo!(), // "ADDRESS", @@ -412,6 +412,12 @@ impl<'a> Interpreter<'a> { self.push(x << shift); } + fn run_shr(&mut self) { + let shift = self.pop(); + let value = self.pop(); + self.push(value >> shift); + } + fn run_keccak256(&mut self) { let offset = self.pop().as_usize(); let size = self.pop().as_usize(); diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index acdb235b..73c329a8 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -20,7 +20,7 @@ fn test_ripemd() -> Result<()> { let kernel = combined_kernel(); let stack_input: Vec = input.iter().map(|&x| U256::from(x as u32)).rev().collect(); let stack_output = run_with_kernel(&kernel, kernel.global_labels["compress"], stack_input)?; - let actual: Vec = stack_output.stack().iter().map(|&x| format!("{:#?}", x)).collect(); + let actual: Vec = stack_output.stack().iter().map(|&x| format!("{:X}", x)).collect(); println!("{:#?}", actual); assert_eq!(expected, actual); From bbeac10bffeedf7dd78c005fa5a5c97e3996ccd1 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Fri, 23 Sep 2022 00:15:44 -0700 Subject: [PATCH 62/97] compression test works! --- evm/src/cpu/kernel/asm/memory/core.asm | 30 +++++++++++++++++++ evm/src/cpu/kernel/asm/ripemd/compression.asm | 12 ++++---- evm/src/cpu/kernel/asm/ripemd/constants.asm | 3 +- evm/src/cpu/kernel/asm/ripemd/memory.asm | 1 + evm/src/cpu/kernel/tests/ripemd.rs | 3 +- 5 files changed, 40 insertions(+), 9 deletions(-) diff --git a/evm/src/cpu/kernel/asm/memory/core.asm b/evm/src/cpu/kernel/asm/memory/core.asm index 963e88ea..264d9439 100644 --- a/evm/src/cpu/kernel/asm/memory/core.asm +++ b/evm/src/cpu/kernel/asm/memory/core.asm @@ -110,6 +110,36 @@ // stack: (((((c_3 << 8) | c_2) << 8) | c_1) << 8) | c_0 %endmacro +// Load LE u32 at given label from 4 bytes (a, b, c, d) +%macro mload_kernel_code_u32_LE(label) + // stack: offset + PUSH $label + ADD + // stack: offset + DUP1 + %mload_kernel_code + // stack: a , offset + DUP2 + %add_const(1) + %mload_kernel_code + %shl_const(8) + OR + // stack: a | (b << 8) , offset + DUP2 + %add_const(2) + %mload_kernel_code + %shl_const(16) + OR + // stack: a | (b << 8) | (c << 16), offset + SWAP1 + %add_const(3) + %mload_kernel_code + %shl_const(24) + OR + // stack: a | (b << 8) | (c << 16) | (d << 24) +%endmacro + + // Store a single byte to kernel code. %macro mstore_kernel_code // stack: offset, value diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index 67757738..ff616952 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -1,7 +1,5 @@ /// _block is stored in memory and its address virt remains on the stack /// Note that STATE takes up 5 stack slots - - /// def compress(state, _block): /// /// stateL = state @@ -25,7 +23,7 @@ /// state[i], stateL[i], stateR[i], output[i], virt, retdest global compress: - // stack: STATE, virt, retdest + // stack: STATE, virt, retdest PUSH switch DUP7 PUSH 1 @@ -204,12 +202,12 @@ pre_rol: // stack: a, b, c, d, e, F, K, boxes, rounds, sides, virt %get_box // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, virt - DUP1 + DUP12 + DUP2 %mload_kernel_code_label(R_data) - DUP13 ADD - // stack: virt + r, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt - %mload_kernel_code_label_u32(Input_Block) // %load_u32_from_block + // stack: virt + r, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt + %mload_kernel_code_u32_LE(Input_Block) // %load_u32_from_block // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt SWAP1 SWAP2 diff --git a/evm/src/cpu/kernel/asm/ripemd/constants.asm b/evm/src/cpu/kernel/asm/ripemd/constants.asm index 6fb33490..1ce49c28 100644 --- a/evm/src/cpu/kernel/asm/ripemd/constants.asm +++ b/evm/src/cpu/kernel/asm/ripemd/constants.asm @@ -136,4 +136,5 @@ global Input_Block: BYTES 0x00, 0x00, 0x00, 0x00 BYTES 0x00, 0x00, 0x00, 0x00 BYTES 0xd0, 0x00, 0x00, 0x00 - BYTES 0x00, 0x00, 0x00, 0x00 \ No newline at end of file + BYTES 0x00, 0x00, 0x00, 0x00 + \ No newline at end of file diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm index 49678837..2a259215 100644 --- a/evm/src/cpu/kernel/asm/ripemd/memory.asm +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -155,6 +155,7 @@ store_input: // stack: a | (b << 8) | (c << 16) | (d << 24) %endmacro + // set offset i to offset j in SEGMENT_RIPEMD %macro mupdate_ripemd // stack: j, i diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index 73c329a8..dd530eb3 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -12,6 +12,7 @@ fn test_ripemd() -> Result<()> { println!("{:#?}", expected); let input: Vec = vec![0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0, 0, 0xdeadbeef]; + // let input: Vec = vec![ // 0x1a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, // 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, @@ -20,7 +21,7 @@ fn test_ripemd() -> Result<()> { let kernel = combined_kernel(); let stack_input: Vec = input.iter().map(|&x| U256::from(x as u32)).rev().collect(); let stack_output = run_with_kernel(&kernel, kernel.global_labels["compress"], stack_input)?; - let actual: Vec = stack_output.stack().iter().map(|&x| format!("{:X}", x)).collect(); + let actual: Vec = stack_output.stack().iter().map(|&x| format!("{:X}", x)).rev().collect(); println!("{:#?}", actual); assert_eq!(expected, actual); From c6b62eaf2fefce28be30c5fa9d6e5d89c1579b01 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Fri, 23 Sep 2022 08:05:14 -0700 Subject: [PATCH 63/97] fix update --- evm/src/cpu/kernel/asm/ripemd/compression.asm | 3 ++- evm/src/cpu/kernel/asm/ripemd/memory.asm | 4 +-- evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 1 + evm/src/cpu/kernel/asm/ripemd/update.asm | 18 ++++++------- evm/src/cpu/kernel/asm/util/basic_macros.asm | 4 +++ evm/src/cpu/kernel/tests/ripemd.rs | 26 +++++++++++-------- 6 files changed, 33 insertions(+), 23 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index ff616952..d525b068 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -207,7 +207,8 @@ pre_rol: %mload_kernel_code_label(R_data) ADD // stack: virt + r, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt - %mload_kernel_code_u32_LE(Input_Block) // %load_u32_from_block + // %mload_kernel_code_u32_LE(Input_Block) + %load_u32_from_block // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt SWAP1 SWAP2 diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm index 2a259215..1e93d809 100644 --- a/evm/src/cpu/kernel/asm/ripemd/memory.asm +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -2,7 +2,7 @@ global ripemd_storage: // starts by initializing buffer // stack: i [init: 64] %store_zeros(64, ripemd_storage) // stack: - // %jump(store_size) + %jump(store_size) store_size: // stack: length @@ -42,7 +42,7 @@ store_input_alt: %mstore_ripemd // stack: rem, length, REM_INP %sub_const(1) - DUP1 + DUP1 // stack: rem - 1, rem - 1, length, REM_INP %jumpi(store_input_alt) // stack: 0, length diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm index 693fde53..a33a605c 100644 --- a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -40,6 +40,7 @@ global ripemd_init: // stack: 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, count, length, virt, LABELS %jump(ripemd_update) ripemd_1: + %break // stack: STATE, count, length , virt , LABELS DUP7 // stack: length, STATE, count, length , virt , LABELS diff --git a/evm/src/cpu/kernel/asm/ripemd/update.asm b/evm/src/cpu/kernel/asm/ripemd/update.asm index edfb64b3..2fdeab6c 100644 --- a/evm/src/cpu/kernel/asm/ripemd/update.asm +++ b/evm/src/cpu/kernel/asm/ripemd/update.asm @@ -11,7 +11,7 @@ /// update_1() /// if Q: /// update_2() -/// R = length - shift > 0 +/// R = length > shift /// if R: /// buffer_update(virt + shift, have, length - shift) /// @@ -32,24 +32,24 @@ global ripemd_update: %stack (shift, need, have, STATE: 5, count, length) -> (length, need, STATE, 0, shift, need, have, count, length) // stack: length, need, STATE, 0, shift, need, have, count, length, virt, retdest LT - NOT + 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) + %stack (Q, STATE: 5, i, shift, need, have) -> (have, Q, Q, STATE, i, shift, need, have) + %eq_const(0) AND // stack: P, Q, STATE, 0, shift, need, have, count, length, virt, retdest %jumpi(update_1) // stack: Q, STATE, 0, shift, need, have, count, length, virt, retdest %jumpi(update_2) final_update: - // stack: shift, need, have, STATE, count, length, virt, retdest + // stack: shift, need, have, STATE, count, length, virt, retdest %stack (shift, need, have, STATE: 5, count, length) -> (length, shift, return_step, shift, need, have, STATE, count, length) SUB // stack: ARGS, shift, need, have, STATE, count, length, virt, retdest %stack (ARGS: 2, shift, need, have, STATE: 5, count, length, virt) -> (shift, virt, have, ARGS, shift, need, have, STATE, count, length, virt) ADD // stack: ARGS: 4, shift, need, have, STATE, count, length, virt, retdest - PUSH 0 - DUP4 + %stack (ARGS: 4, shift, need, have, STATE, count, length) -> (length, shift, ARGS, shift, need, have, STATE, count, length) GT // stack: R, ARGS, shift, need, have, STATE, count, length, virt, retdest %jumpi(buffer_update) @@ -57,7 +57,7 @@ final_update: %pop3 JUMP return_step: - // stack: shift, need, have, STATE, count, length, virt, retdest + // stack: shift, need, have, STATE, count , length, virt, retdest SWAP8 DUP10 %mul_const(8) @@ -103,7 +103,7 @@ update_2: // stack: offset, cond, STATE, shift, need, have, count, length, virt, retdest %stack (offset, cond, STATE: 5) -> (cond, 0, STATE, offset, compression_loop, cond) LT - NOT + ISZERO // cond >= 0, STATE, offset, compression_loop, cond, shift, need, have, count, length, virt, retdest %jumpi(compress) compression_loop: @@ -129,7 +129,7 @@ compression_loop: /// for i in range(times): /// buffer[set+i] = bytestring[get+i] -buffer_update: +buffer_update: // stack: get , set , times , retdest DUP2 DUP2 diff --git a/evm/src/cpu/kernel/asm/util/basic_macros.asm b/evm/src/cpu/kernel/asm/util/basic_macros.asm index 8eb60647..b078f133 100644 --- a/evm/src/cpu/kernel/asm/util/basic_macros.asm +++ b/evm/src/cpu/kernel/asm/util/basic_macros.asm @@ -298,3 +298,7 @@ ADD // stack: dcba %endmacro + +%macro break + %jump(0xdeadbeef) +%endmacro \ No newline at end of file diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index dd530eb3..f8019cdc 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -4,26 +4,30 @@ use ethereum_types::U256; use crate::cpu::kernel::aggregator::combined_kernel; 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"]; + 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 input: Vec = vec![0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0, 0, 0xdeadbeef]; - // let input: Vec = vec![ - // 0x1a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, - // 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, - // ]; + let input: Vec = vec![ + 26, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, + 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a + ]; let kernel = combined_kernel(); let stack_input: Vec = input.iter().map(|&x| U256::from(x as u32)).rev().collect(); - let stack_output = run_with_kernel(&kernel, kernel.global_labels["compress"], stack_input)?; - let actual: Vec = stack_output.stack().iter().map(|&x| format!("{:X}", x)).rev().collect(); + let stack_output = run_with_kernel(&kernel, kernel.global_labels["ripemd_alt"], stack_input)?; + let actual: Vec = stack_output + .stack() + .iter() + .map(|&x| format!("{:X}", x)) + .rev() + .collect(); println!("{:#?}", actual); - assert_eq!(expected, actual); + assert_eq!(expected, actual[0]); Ok(()) } From 24606decce51ffa476667119d473f0ab8dacf1cb Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Fri, 23 Sep 2022 09:27:09 -0700 Subject: [PATCH 64/97] full run but fails --- evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 3 +- evm/src/cpu/kernel/asm/ripemd/update.asm | 51 ++++++++++++------------ evm/src/cpu/kernel/interpreter.rs | 1 + 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm index a33a605c..29971038 100644 --- a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -40,7 +40,6 @@ global ripemd_init: // stack: 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, count, length, virt, LABELS %jump(ripemd_update) ripemd_1: - %break // stack: STATE, count, length , virt , LABELS DUP7 // stack: length, STATE, count, length , virt , LABELS @@ -57,7 +56,7 @@ ripemd_2: %stack (STATE: 5, count, length, virt) -> (STATE, count, 8, 64) // stack: STATE, count, length = 8, virt = 64, LABELS %jump(ripemd_update) -process: +global process: // stack: a , b, c, d, e, count, length, virt %reverse_bytes_u32 // stack: a', b, c, d, e, VARS diff --git a/evm/src/cpu/kernel/asm/ripemd/update.asm b/evm/src/cpu/kernel/asm/ripemd/update.asm index 2fdeab6c..fccec845 100644 --- a/evm/src/cpu/kernel/asm/ripemd/update.asm +++ b/evm/src/cpu/kernel/asm/ripemd/update.asm @@ -42,29 +42,29 @@ global ripemd_update: // stack: Q, STATE, 0, shift, need, have, count, length, virt, retdest %jumpi(update_2) final_update: - // stack: shift, need, have, STATE, count, length, virt, retdest - %stack (shift, need, have, STATE: 5, count, length) -> (length, shift, return_step, shift, need, have, STATE, count, length) + // stack: STATE, 0, shift, need, have, count, length, virt, retdest + %stack (STATE: 5, i, shift, need, have, count, length) -> (length, shift, return_step, STATE, 0, shift, need, have, count, length) SUB - // stack: ARGS, shift, need, have, STATE, count, length, virt, retdest - %stack (ARGS: 2, shift, need, have, STATE: 5, count, length, virt) -> (shift, virt, have, ARGS, shift, need, have, STATE, count, length, virt) + // stack: ARGS: 2, STATE, 0, shift, need, have, count, length, virt, retdest + %stack (ARGS: 2, STATE: 5, i, shift, need, have, count, length, virt) -> (shift, virt, have, ARGS, STATE, 0, shift, need, have, count, length, virt) ADD - // stack: ARGS: 4, shift, need, have, STATE, count, length, virt, retdest - %stack (ARGS: 4, shift, need, have, STATE, count, length) -> (length, shift, ARGS, shift, need, have, STATE, count, length) + // stack: ARGS: 4, STATE, 0, shift, need, have, count, length, virt, retdest + %stack (ARGS: 4, STATE: 5, i, shift, need, have, count, length) -> (length, shift, ARGS, STATE, 0, shift, need, have, count, length) GT - // stack: R, ARGS, shift, need, have, STATE, count, length, virt, retdest + // stack: R, ARGS: 4, STATE, 0, shift, need, have, count, length, virt, retdest %jumpi(buffer_update) - // stack: ARGS, shift, need, have, STATE, count, length, virt, retdest + // stack: ARGS: 4, STATE, 0, shift, need, have, count, length, virt, retdest %pop3 JUMP return_step: - // stack: shift, need, have, STATE, count , length, virt, retdest - SWAP8 - DUP10 + // stack: STATE, 0, shift, need, have, count, length, virt, retdest + SWAP9 + DUP11 %mul_const(8) ADD - SWAP8 - // stack: shift, need, have, STATE, count += 8*length, length, virt, retdest - %stack (shift, need, have, STATE: 5, count, length, virt, retdest) -> (retdest, STATE, count, length, virt) + SWAP9 + // stack: STATE, 0, shift, need, have, count, length, virt, retdest + %stack (STATE: 5, i, shift, need, have, count, length, virt, retdest) -> (retdest, STATE, count, length, virt) JUMP @@ -79,33 +79,32 @@ update_1: %stack (Q, STATE: 5, i, shift, need, have, count, length, virt) -> (virt, have, need, update_1a, STATE, i, shift, need, have, count, length, virt) %jump(buffer_update) update_1a: - // stack: STATE, 0, shift, need, have, count, length, virt, retdest + // stack: STATE, 0, shift, need, have, count, length, virt, retdest %stack (STATE: 5, i, shift, need, have) -> (STATE, i, update_2, need, need, 0) // stack: STATE, 0, update_2, shift = need, need, have = 0, count, length, virt, retdest %jump(compress) /// def update_2(): -/// cond = length - shift - 64 -/// while cond >= 0: +/// while length - shift >= 64: /// state = compress(state, bytestring[shift:]) /// shift += 64 /// cond -= 64 update_2: - // 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) + // 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) SUB - SUB - // stack: cond, STATE, shift, need, have, count, length, virt, retdest + %ge_const(64) + // stack: cond, STATE, shift, need, have, count, length, virt, retdest DUP12 DUP8 ADD - // stack: offset, cond, STATE, shift, need, have, count, length, virt, retdest - %stack (offset, cond, STATE: 5) -> (cond, 0, STATE, offset, compression_loop, cond) - LT - ISZERO - // cond >= 0, STATE, offset, compression_loop, cond, shift, need, have, count, length, virt, retdest + // stack: offset, cond, STATE, shift, need, have, count, length, virt, retdest + %stack (offset, cond, STATE: 5) -> (cond, STATE, offset, compression_loop) + // stack: cond, STATE, offset, compression_loop, shift, need, have, count, length, virt, retdest %jumpi(compress) + %stack (STATE:5, offset, compression_loop) -> (STATE, offset) + %jump(final_update) compression_loop: // stack: STATE, offset , cond , shift, need, have, count, length, virt, retdest SWAP5 diff --git a/evm/src/cpu/kernel/interpreter.rs b/evm/src/cpu/kernel/interpreter.rs index e36b1e66..40e9cc18 100644 --- a/evm/src/cpu/kernel/interpreter.rs +++ b/evm/src/cpu/kernel/interpreter.rs @@ -282,6 +282,7 @@ impl<'a> Interpreter<'a> { 0xff => todo!(), // "SELFDESTRUCT", _ => bail!("Unrecognized opcode {}.", opcode), }; + println!("{:X}", opcode); Ok(()) } From 41ce8e94b986e235129850fbf35b02a88fdd7078 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Fri, 23 Sep 2022 10:39:49 -0700 Subject: [PATCH 65/97] 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); From 3cceede41232a6157f01f0392a4f7c65a3076d63 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Fri, 23 Sep 2022 10:52:05 -0700 Subject: [PATCH 66/97] format, stack macro, remove prints --- evm/src/cpu/kernel/asm/ripemd/compression.asm | 9 ++------- evm/src/cpu/kernel/interpreter.rs | 1 - evm/src/cpu/kernel/tests/ripemd.rs | 14 ++++++-------- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index 851586b7..dfd57bc3 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -240,13 +240,8 @@ mid_rol: %jump(rol) post_rol: // stack: c, a, b, d, e, F, K, boxes , rounds, sides, virt - SWAP3 - // stack: d, a, b, c, e, F, K, boxes , rounds, sides, virt - SWAP4 - // stack: e, a, b, c, d, F, K, boxes , rounds, sides, virt - SWAP7 - PUSH 1 - SWAP1 + %stack (c, a, b, d, e, F, K, boxes) -> (boxes, 1, a, b, c, d, F, K, e) + // stack: boxes, 1, a, b, c, d, F, K, e, rounds, sides, virt SUB SWAP7 // stack: e, a, b, c, d, F, K, boxes-1, rounds, sides, virt diff --git a/evm/src/cpu/kernel/interpreter.rs b/evm/src/cpu/kernel/interpreter.rs index 40e9cc18..e36b1e66 100644 --- a/evm/src/cpu/kernel/interpreter.rs +++ b/evm/src/cpu/kernel/interpreter.rs @@ -282,7 +282,6 @@ impl<'a> Interpreter<'a> { 0xff => todo!(), // "SELFDESTRUCT", _ => bail!("Unrecognized opcode {}.", opcode), }; - println!("{:X}", opcode); Ok(()) } diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index 80c46a54..d418fa5a 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -6,25 +6,23 @@ use crate::cpu::kernel::interpreter::run_with_kernel; #[test] fn test_ripemd() -> Result<()> { - let expected = "f71c27109c692c1b56bbdceb5b9d2865b3708dbc"; - println!("{:#}", expected); - let input: Vec = vec![ - 26, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, - 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a + 26, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, + 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, ]; let kernel = combined_kernel(); let stack_input: Vec = input.iter().map(|&x| U256::from(x as u32)).rev().collect(); let stack_output = run_with_kernel(&kernel, kernel.global_labels["ripemd_alt"], stack_input)?; - let actual: Vec = stack_output + let actual: String = stack_output .stack() .iter() .map(|&x| format!("{:x}", x)) .rev() .collect(); - println!("{:#?}", actual); - assert_eq!(expected, actual[0]); + + let expected = "f71c27109c692c1b56bbdceb5b9d2865b3708dbc"; + assert_eq!(expected, actual); Ok(()) } From 58b6771cba71d239b013a4680e1d699a9a7a129f Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Fri, 23 Sep 2022 12:13:18 -0700 Subject: [PATCH 67/97] fancy new stack macro --- evm/src/cpu/kernel/asm/ripemd/compression.asm | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index dfd57bc3..328a5074 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -26,11 +26,7 @@ global compress: // stack: STATE, virt, retdest PUSH switch DUP7 - PUSH 1 - PUSH 5 - PUSH 16 - PUSH 0 - PUSH 0 + %stack () -> (0, 0, 16, 5, 1) // stack: 0, 0, 16, 5, 1, virt, switch, STATE, virt, retdest DUP12 DUP12 From 36c4b480175ad0472db7b8f09399ed27381413b7 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Fri, 23 Sep 2022 13:28:47 -0700 Subject: [PATCH 68/97] better test API --- evm/src/cpu/kernel/tests/ripemd.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index d418fa5a..1dd57022 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -4,25 +4,28 @@ use ethereum_types::U256; use crate::cpu::kernel::aggregator::combined_kernel; use crate::cpu::kernel::interpreter::run_with_kernel; +fn make_input(word: &str) -> Vec { + let mut bytes: Vec = vec![word.len().try_into().unwrap()]; + bytes.append(&mut word.as_bytes().to_vec()); + bytes +} + #[test] fn test_ripemd() -> Result<()> { - let input: Vec = vec![ - 26, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, - 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, - ]; + + let input: Vec = make_input("abcdefghijklmnopqrstuvwxyz"); + let expected = "f71c27109c692c1b56bbdceb5b9d2865b3708dbc"; let kernel = combined_kernel(); - let stack_input: Vec = input.iter().map(|&x| U256::from(x as u32)).rev().collect(); - let stack_output = run_with_kernel(&kernel, kernel.global_labels["ripemd_alt"], stack_input)?; - let actual: String = stack_output + let label = kernel.global_labels["ripemd_alt"]; + let stack_input: Vec = input.iter().map(|&x| U256::from(x as u8)).rev().collect(); + let output: String = run_with_kernel(&kernel, label, stack_input)? .stack() .iter() .map(|&x| format!("{:x}", x)) .rev() .collect(); - - let expected = "f71c27109c692c1b56bbdceb5b9d2865b3708dbc"; - assert_eq!(expected, actual); + assert_eq!(output, expected); Ok(()) } From abcb93d0a86857bb0fcf393da2ac49f3f503a6fc Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Fri, 23 Sep 2022 13:48:11 -0700 Subject: [PATCH 69/97] assert as U256 --- evm/src/cpu/kernel/tests/ripemd.rs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index 1dd57022..a976b8cc 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -5,26 +5,20 @@ use crate::cpu::kernel::aggregator::combined_kernel; use crate::cpu::kernel::interpreter::run_with_kernel; fn make_input(word: &str) -> Vec { - let mut bytes: Vec = vec![word.len().try_into().unwrap()]; - bytes.append(&mut word.as_bytes().to_vec()); - bytes + let mut bytes: Vec = vec![word.len().try_into().unwrap()]; + bytes.append(&mut word.as_bytes().to_vec()); + bytes } #[test] fn test_ripemd() -> Result<()> { - - let input: Vec = make_input("abcdefghijklmnopqrstuvwxyz"); - let expected = "f71c27109c692c1b56bbdceb5b9d2865b3708dbc"; + let input: Vec = make_input("a"); + let expected = U256::from("0x0bdc9d2d256b3ee9daae347be6f4dc835a467ffe"); let kernel = combined_kernel(); let label = kernel.global_labels["ripemd_alt"]; let stack_input: Vec = input.iter().map(|&x| U256::from(x as u8)).rev().collect(); - let output: String = run_with_kernel(&kernel, label, stack_input)? - .stack() - .iter() - .map(|&x| format!("{:x}", x)) - .rev() - .collect(); + let output: U256 = run_with_kernel(&kernel, label, stack_input)?.stack()[0]; assert_eq!(output, expected); Ok(()) From 85b88a1cde027a834f299dda8a31fdb2582e7feb Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Fri, 23 Sep 2022 17:46:53 -0700 Subject: [PATCH 70/97] fix padlength overflow error --- evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 19 +++++++++---------- evm/src/cpu/kernel/tests/ripemd.rs | 4 ++-- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm index 8112f20f..d0afb6d0 100644 --- a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -92,15 +92,14 @@ global process: %macro padlength // stack: count %mod_const(64) - // stack: count % 64 - PUSH 56 - SUB - // stack: x = 56 - count % 64 - DUP1 - %lt_const(9) - // stack: x < 9 , x + // stack: t = count % 64 + PUSH 47 + DUP2 + // stack: t , 47 , t + GT + // stack: t > 47 , t %mul_const(64) - // stack: 64*(x < 9) , x - ADD - // stack: 64*(x < 9) + x + %add_const(56) + // stack: 56 + 64*(t > 47), t + SUB %endmacro diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index a976b8cc..3eca4506 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -12,8 +12,8 @@ fn make_input(word: &str) -> Vec { #[test] fn test_ripemd() -> Result<()> { - let input: Vec = make_input("a"); - let expected = U256::from("0x0bdc9d2d256b3ee9daae347be6f4dc835a467ffe"); + let input: Vec = make_input("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); + let expected = U256::from("0xb0e20b6e3116640286ed3a87a5713079b21f5189"); let kernel = combined_kernel(); let label = kernel.global_labels["ripemd_alt"]; From 705b4a432f37a5f855a2f37df236ad750eb92de0 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Fri, 23 Sep 2022 17:56:28 -0700 Subject: [PATCH 71/97] fix length zero input issue --- evm/src/cpu/kernel/asm/ripemd/memory.asm | 4 ++-- evm/src/cpu/kernel/tests/ripemd.rs | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm index 1e93d809..e3d3141a 100644 --- a/evm/src/cpu/kernel/asm/ripemd/memory.asm +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -30,8 +30,8 @@ store_size: store_padding: // stack: i (init 63) %store_zeros(136, store_padding) - %jump(store_input_alt) - + %jumpi(store_input_alt) + %jump(ripemd_init) store_input_alt: // stack: rem, length, REM_INP diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index 3eca4506..bdd2b4af 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -4,6 +4,7 @@ use ethereum_types::U256; use crate::cpu::kernel::aggregator::combined_kernel; use crate::cpu::kernel::interpreter::run_with_kernel; + fn make_input(word: &str) -> Vec { let mut bytes: Vec = vec![word.len().try_into().unwrap()]; bytes.append(&mut word.as_bytes().to_vec()); @@ -12,8 +13,8 @@ fn make_input(word: &str) -> Vec { #[test] fn test_ripemd() -> Result<()> { - let input: Vec = make_input("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); - let expected = U256::from("0xb0e20b6e3116640286ed3a87a5713079b21f5189"); + let input: Vec = make_input(""); + let expected = U256::from("0x9c1185a5c5e9fc54612808977ee8f548b2258d31"); let kernel = combined_kernel(); let label = kernel.global_labels["ripemd_alt"]; From 4850c338b430a50a7d05c3df58aeab48a2b6ab7e Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 26 Sep 2022 07:34:57 -0700 Subject: [PATCH 72/97] clean up --- evm/src/cpu/kernel/asm/ripemd/memory.asm | 2 +- evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 4 ++-- evm/src/cpu/kernel/asm/ripemd/update.asm | 8 ++++---- evm/src/cpu/kernel/tests/ripemd.rs | 17 +++++++++++++---- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm index e3d3141a..c5188b16 100644 --- a/evm/src/cpu/kernel/asm/ripemd/memory.asm +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -30,7 +30,7 @@ store_size: store_padding: // stack: i (init 63) %store_zeros(136, store_padding) - %jumpi(store_input_alt) + %jump(store_input_alt) %jump(ripemd_init) store_input_alt: diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm index d0afb6d0..6c3c0397 100644 --- a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -36,8 +36,8 @@ global ripemd_init: // stack: length %stack (length) -> ( 0, length, 136, ripemd_1, ripemd_2, process) // stack: count = 0, length, virt = 136, ripemd_1, ripemd_2, process - %stack (ARGS: 3, LABELS: 3) -> (0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, ARGS, LABELS) - // stack: 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, count, length, virt, LABELS + %stack () -> (0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0) + // stack: 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, count, length, virt, LABELS %jump(ripemd_update) ripemd_1: // stack: STATE, count, length , virt , LABELS diff --git a/evm/src/cpu/kernel/asm/ripemd/update.asm b/evm/src/cpu/kernel/asm/ripemd/update.asm index a5132d7c..03d1c5c5 100644 --- a/evm/src/cpu/kernel/asm/ripemd/update.asm +++ b/evm/src/cpu/kernel/asm/ripemd/update.asm @@ -91,19 +91,19 @@ update_1a: /// cond -= 64 update_2: - // stack: STATE, shift, need, have, count, length, virt, retdest + // 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 + // stack: cond, STATE, shift, need, have, count, length, virt, retdest DUP12 DUP8 ADD - // stack: offset, cond, STATE, shift, need, have, count, length, virt, retdest + // stack: offset, cond, STATE, shift, need, have, count, length, virt, retdest %stack (offset, cond, STATE: 5) -> (cond, STATE, offset, compression_loop) // stack: cond, STATE, offset, compression_loop, shift, need, have, count, length, virt, retdest %jumpi(compress) - %stack (STATE:5, offset, compression_loop) -> (STATE, offset) + %stack (STATE: 5, offset, compression_loop) -> (STATE, offset) %jump(final_update) compression_loop: // stack: STATE, offset , cond , shift, need, have, count, length, virt, retdest diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index bdd2b4af..5a70b8ba 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -13,14 +13,23 @@ fn make_input(word: &str) -> Vec { #[test] fn test_ripemd() -> Result<()> { - let input: Vec = make_input(""); - let expected = U256::from("0x9c1185a5c5e9fc54612808977ee8f548b2258d31"); + // let input: Vec = make_input("12345678901234567890123456789012345678901234567890123456789012345678901234567890"); + // let expected = U256::from("0x9b752e45573d4b39f4dbd3323cab82bf63326bfb"); + + let input: Vec = make_input("abcdefghijklmnopqrstuvwxyz"); + let expected = U256::from("0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc"); + let kernel = combined_kernel(); let label = kernel.global_labels["ripemd_alt"]; let stack_input: Vec = input.iter().map(|&x| U256::from(x as u8)).rev().collect(); - let output: U256 = run_with_kernel(&kernel, label, stack_input)?.stack()[0]; - assert_eq!(output, expected); + let stack_output: Vec = run_with_kernel(&kernel, label, stack_input)?.stack().to_vec(); + + let read_out: Vec = stack_output.iter().map(|x| format!("{:x}", x)).rev().collect(); + println!("{:x?}", read_out); + + let actual = stack_output[0]; + assert_eq!(actual, expected); Ok(()) } From 9f5e5654523bbe84d4e89d59757f0bbe5c64046d Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 26 Sep 2022 10:32:54 -0700 Subject: [PATCH 73/97] fix update2 --- evm/src/cpu/kernel/asm/ripemd/update.asm | 52 ++++++++++++------------ evm/src/cpu/kernel/tests/ripemd.rs | 4 +- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/update.asm b/evm/src/cpu/kernel/asm/ripemd/update.asm index 03d1c5c5..8620b7aa 100644 --- a/evm/src/cpu/kernel/asm/ripemd/update.asm +++ b/evm/src/cpu/kernel/asm/ripemd/update.asm @@ -29,42 +29,42 @@ global ripemd_update: SUB PUSH 0 // stack: shift, need, have, STATE, count, length, virt, retdest - %stack (shift, need, have, STATE: 5, count, length) -> (length, need, STATE, 0, shift, need, have, count, length) - // stack: length, need, STATE, 0, shift, need, have, count, length, virt, retdest + %stack (shift, need, have, STATE: 5, count, length) -> (length, need, STATE, shift, need, have, count, length) + // stack: length, need, STATE, shift, need, have, count, length, virt, retdest LT 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) + // stack: Q, STATE, shift, need, have, count, length, virt, retdest + %stack (Q, STATE: 5, shift, need, have) -> (have, Q, Q, STATE, shift, need, have) %gt_const(0) AND - // stack: P, Q, STATE, 0, shift, need, have, count, length, virt, retdest + // stack: P, Q, STATE, shift, need, have, count, length, virt, retdest %jumpi(update_1) - // stack: Q, STATE, 0, shift, need, have, count, length, virt, retdest + // stack: Q, STATE, shift, need, have, count, length, virt, retdest %jumpi(update_2) final_update: - // stack: STATE, 0, shift, need, have, count, length, virt, retdest - %stack (STATE: 5, i, shift, need, have, count, length) -> (length, shift, return_step, STATE, 0, shift, need, have, count, length) + // stack: STATE, shift, need, have, count, length, virt, retdest + %stack (STATE: 5, shift, need, have, count, length) -> (length, shift, return_step, STATE, shift, need, have, count, length) SUB - // stack: ARGS: 2, STATE, 0, shift, need, have, count, length, virt, retdest - %stack (ARGS: 2, STATE: 5, i, shift, need, have, count, length, virt) -> (shift, virt, have, ARGS, STATE, 0, shift, need, have, count, length, virt) + // stack: ARGS: 2, STATE, shift, need, have, count, length, virt, retdest + %stack (ARGS: 2, STATE: 5, shift, need, have, count, length, virt) -> (shift, virt, have, ARGS, STATE, shift, need, have, count, length, virt) ADD - // stack: ARGS: 4, STATE, 0, shift, need, have, count, length, virt, retdest - %stack (ARGS: 4, STATE: 5, i, shift, need, have, count, length) -> (length, shift, ARGS, STATE, 0, shift, need, have, count, length) + // stack: ARGS: 4, STATE, shift, need, have, count, length, virt, retdest + %stack (ARGS: 4, STATE: 5, shift, need, have, count, length) -> (length, shift, ARGS, STATE, shift, need, have, count, length) GT - // stack: R, ARGS: 4, STATE, 0, shift, need, have, count, length, virt, retdest + // stack: R, ARGS: 4, STATE, shift, need, have, count, length, virt, retdest %jumpi(buffer_update) - // stack: ARGS: 4, STATE, 0, shift, need, have, count, length, virt, retdest + // stack: ARGS: 4, STATE, shift, need, have, count, length, virt, retdest %pop3 JUMP return_step: - // stack: STATE, 0, shift, need, have, count, length, virt, retdest - SWAP9 - DUP11 + // stack: STATE, shift, need, have, count, length, virt, retdest + SWAP8 + DUP10 %mul_const(8) ADD - SWAP9 - // stack: STATE, 0, shift, need, have, count, length, virt, retdest - %stack (STATE: 5, i, shift, need, have, count, length, virt, retdest) -> (retdest, STATE, count, length, virt) + SWAP8 + // stack: STATE, shift, need, have, count, length, virt, retdest + %stack (STATE: 5, shift, need, have, count, length, virt, retdest) -> (retdest, STATE, count, length, virt) JUMP @@ -75,13 +75,13 @@ return_step: /// state = compress(state, buffer) update_1: - // stack: Q, STATE, 0, shift, need, have, count, length, virt, retdest - %stack (Q, STATE: 5, i, shift, need, have, count, length, virt) -> (virt, have, need, update_1a, STATE, i, shift, need, have, count, length, virt) + // stack: Q, STATE, shift, need, have, count, length, virt, retdest + %stack (Q, STATE: 5, shift, need, have, count, length, virt) -> (virt, have, need, update_1a, STATE, shift, need, have, count, length, virt) %jump(buffer_update) update_1a: - // stack: STATE, 0, shift, need, have, count, length, virt, retdest - %stack (STATE: 5, i, shift, need, have) -> (STATE, i, update_2, need, need, 0) - // stack: STATE, 0, update_2, shift = need, need, have = 0, count, length, virt, retdest + // stack: STATE, shift, need, have, count, length, virt, retdest + %stack (STATE: 5, shift, need, have) -> (STATE, 0, update_2, need, need, 0) + // stack: STATE, 0, update_2, shift = need, need, have = 0, count, length, virt, retdest %jump(compress) /// def update_2(): @@ -103,7 +103,7 @@ update_2: %stack (offset, cond, STATE: 5) -> (cond, STATE, offset, compression_loop) // stack: cond, STATE, offset, compression_loop, shift, need, have, count, length, virt, retdest %jumpi(compress) - %stack (STATE: 5, offset, compression_loop) -> (STATE, offset) + %stack (STATE: 5, offset, compression_loop) -> (STATE) %jump(final_update) compression_loop: // stack: STATE, offset , cond , shift, need, have, count, length, virt, retdest diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index 5a70b8ba..66842e5f 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -16,8 +16,8 @@ fn test_ripemd() -> Result<()> { // let input: Vec = make_input("12345678901234567890123456789012345678901234567890123456789012345678901234567890"); // let expected = U256::from("0x9b752e45573d4b39f4dbd3323cab82bf63326bfb"); - let input: Vec = make_input("abcdefghijklmnopqrstuvwxyz"); - let expected = U256::from("0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc"); + let input: Vec = make_input("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); + let expected = U256::from("0xb0e20b6e3116640286ed3a87a5713079b21f5189"); let kernel = combined_kernel(); From 01b310d013cc118bfa91b9af700b7727cf6657c8 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 26 Sep 2022 10:53:47 -0700 Subject: [PATCH 74/97] all small inputs work --- evm/src/cpu/kernel/asm/ripemd/memory.asm | 4 +++- evm/src/cpu/kernel/tests/ripemd.rs | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm index c5188b16..f8338dcc 100644 --- a/evm/src/cpu/kernel/asm/ripemd/memory.asm +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -30,7 +30,9 @@ store_size: store_padding: // stack: i (init 63) %store_zeros(136, store_padding) - %jump(store_input_alt) + DUP1 + %jumpi(store_input_alt) + POP %jump(ripemd_init) store_input_alt: diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index 66842e5f..db3348eb 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -19,6 +19,9 @@ fn test_ripemd() -> Result<()> { let input: Vec = make_input("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); let expected = U256::from("0xb0e20b6e3116640286ed3a87a5713079b21f5189"); + // let input: Vec = make_input(""); + // let expected = U256::from("0x9c1185a5c5e9fc54612808977ee8f548b2258d31"); + let kernel = combined_kernel(); let label = kernel.global_labels["ripemd_alt"]; From 9b59d02dadfc8379c1d6f75e1154b09c3965c084 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 26 Sep 2022 19:37:16 -0700 Subject: [PATCH 75/97] loop thru official ripemd tests --- evm/src/cpu/kernel/tests/ripemd.rs | 57 +++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index db3348eb..d614b157 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -4,7 +4,6 @@ use ethereum_types::U256; use crate::cpu::kernel::aggregator::combined_kernel; use crate::cpu::kernel::interpreter::run_with_kernel; - fn make_input(word: &str) -> Vec { let mut bytes: Vec = vec![word.len().try_into().unwrap()]; bytes.append(&mut word.as_bytes().to_vec()); @@ -15,24 +14,48 @@ fn make_input(word: &str) -> Vec { fn test_ripemd() -> Result<()> { // let input: Vec = make_input("12345678901234567890123456789012345678901234567890123456789012345678901234567890"); // let expected = U256::from("0x9b752e45573d4b39f4dbd3323cab82bf63326bfb"); + let reference = vec![ + ("", "0x9c1185a5c5e9fc54612808977ee8f548b2258d31"), + ("a", "0x0bdc9d2d256b3ee9daae347be6f4dc835a467ffe"), + ("abc", "0x8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"), + ( + "message digest", + "0x5d0689ef49d2fae572b881b123a85ffa21595f36", + ), + ( + "abcdefghijklmnopqrstuvwxyz", + "0xf71c27109c692c1b56bbdceb5b9d2865b3708dbc", + ), + ( + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + "0x12a053384a9c0c88e405a06c27dcf49ada62eb2b", + ), + ( + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + "0xb0e20b6e3116640286ed3a87a5713079b21f5189", + ), + ]; - let input: Vec = make_input("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); - let expected = U256::from("0xb0e20b6e3116640286ed3a87a5713079b21f5189"); + for (x, y) in reference { + let input: Vec = make_input(x); + let expected = U256::from(y); + let kernel = combined_kernel(); + let label = kernel.global_labels["ripemd_alt"]; + let stack_input: Vec = input.iter().map(|&x| U256::from(x as u8)).rev().collect(); + let stack_output: Vec = run_with_kernel(&kernel, label, stack_input)? + .stack() + .to_vec(); + + let actual = stack_output[0]; + assert_eq!(actual, expected); + } + Ok(()) + + // let input: Vec = make_input("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); + // let expected = U256::from("0xb0e20b6e3116640286ed3a87a5713079b21f5189"); // let input: Vec = make_input(""); // let expected = U256::from("0x9c1185a5c5e9fc54612808977ee8f548b2258d31"); - - - let kernel = combined_kernel(); - let label = kernel.global_labels["ripemd_alt"]; - let stack_input: Vec = input.iter().map(|&x| U256::from(x as u8)).rev().collect(); - let stack_output: Vec = run_with_kernel(&kernel, label, stack_input)?.stack().to_vec(); - - let read_out: Vec = stack_output.iter().map(|x| format!("{:x}", x)).rev().collect(); - println!("{:x?}", read_out); - - let actual = stack_output[0]; - assert_eq!(actual, expected); - - Ok(()) + // let read_out: Vec = stack_output.iter().map(|x| format!("{:x}", x)).rev().collect(); + // println!("{:x?}", read_out); } From 29b2552facd2e79f74f11eb8e8f074080b4b4720 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 26 Sep 2022 20:17:41 -0700 Subject: [PATCH 76/97] majorly simplify update2 --- evm/src/cpu/kernel/asm/ripemd/update.asm | 47 ++++++++---------------- evm/src/cpu/kernel/tests/ripemd.rs | 25 ++++++------- 2 files changed, 28 insertions(+), 44 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/update.asm b/evm/src/cpu/kernel/asm/ripemd/update.asm index 8620b7aa..a9ef166a 100644 --- a/evm/src/cpu/kernel/asm/ripemd/update.asm +++ b/evm/src/cpu/kernel/asm/ripemd/update.asm @@ -85,43 +85,28 @@ update_1a: %jump(compress) /// def update_2(): -/// while length - shift >= 64: -/// state = compress(state, bytestring[shift:]) +/// while length >= shift + 64: /// shift += 64 -/// cond -= 64 +/// state = compress(state, bytestring[shift-64:]) update_2: - // 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 - DUP12 - DUP8 + // stack: STATE, shift, need, have, count, length, virt, retdest + %stack (STATE: 5, shift, need, have, count, length) -> (64, shift, length, STATE, shift, need, have, count, length) ADD - // stack: offset, cond, STATE, shift, need, have, count, length, virt, retdest - %stack (offset, cond, STATE: 5) -> (cond, STATE, offset, compression_loop) - // stack: cond, STATE, offset, compression_loop, shift, need, have, count, length, virt, retdest - %jumpi(compress) - %stack (STATE: 5, offset, compression_loop) -> (STATE) - %jump(final_update) -compression_loop: - // stack: STATE, offset , cond , shift, need, have, count, length, virt, retdest + GT + // stack: cond, STATE, shift, need, have, count, length, virt, retdest + %jumpi(final_update) SWAP5 %add_const(64) - SWAP5 - SWAP6 - %sub_const(64) - SWAP6 - SWAP7 - %add_const(64) - SWAP7 - // stack: STATE, offset+64, cond-64, shift+64, need, have, count, length, virt, retdest - %stack (STATE: 5, offset, cond, shift) -> (cond, 0, STATE, offset, compression_loop, cond, shift) - %jumpi(compress) - // stack: STATE, offset , label, cond , shift , need, have, count, length, virt, retdest - %stack (STATE: 5, offset, label, cond, shift, need, have, count, length, virt, retdest) -> (shift, need, have, STATE, count, length, virt, retdest) - %jump(final_update) + SWAP5 + %stack (STATE: 5, shift) -> (shift, 64, STATE) + DUP14 + ADD + SUB + // stack: offset, STATE, shift, need, have, count, length, virt, retdest + %stack (offset, STATE: 5) -> (STATE, offset, update_2) + // stack: STATE, offset, update_2, shift, need, have, count, length, virt, retdest + %jump(compress) /// def buffer_update(get, set, times): diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index d614b157..8741103f 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -12,8 +12,6 @@ fn make_input(word: &str) -> Vec { #[test] fn test_ripemd() -> Result<()> { - // let input: Vec = make_input("12345678901234567890123456789012345678901234567890123456789012345678901234567890"); - // let expected = U256::from("0x9b752e45573d4b39f4dbd3323cab82bf63326bfb"); let reference = vec![ ("", "0x9c1185a5c5e9fc54612808977ee8f548b2258d31"), ("a", "0x0bdc9d2d256b3ee9daae347be6f4dc835a467ffe"), @@ -34,6 +32,10 @@ fn test_ripemd() -> Result<()> { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "0xb0e20b6e3116640286ed3a87a5713079b21f5189", ), + // ( + // "12345678901234567890123456789012345678901234567890123456789012345678901234567890", + // "0x9b752e45573d4b39f4dbd3323cab82bf63326bfb", + // ) ]; for (x, y) in reference { @@ -41,21 +43,18 @@ fn test_ripemd() -> Result<()> { let expected = U256::from(y); let kernel = combined_kernel(); - let label = kernel.global_labels["ripemd_alt"]; - let stack_input: Vec = input.iter().map(|&x| U256::from(x as u8)).rev().collect(); - let stack_output: Vec = run_with_kernel(&kernel, label, stack_input)? + let initial_offset = kernel.global_labels["ripemd_alt"]; + let initial_stack: Vec = input.iter().map(|&x| U256::from(x as u8)).rev().collect(); + let final_stack: Vec = run_with_kernel(&kernel, initial_offset, initial_stack)? .stack() .to_vec(); - let actual = stack_output[0]; + let actual = final_stack[0]; + + let read_out: Vec = final_stack.iter().map(|x| format!("{:x}", x)).rev().collect(); + println!("{:x?}", read_out); + assert_eq!(actual, expected); } Ok(()) - - // let input: Vec = make_input("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); - // let expected = U256::from("0xb0e20b6e3116640286ed3a87a5713079b21f5189"); - // let input: Vec = make_input(""); - // let expected = U256::from("0x9c1185a5c5e9fc54612808977ee8f548b2258d31"); - // let read_out: Vec = stack_output.iter().map(|x| format!("{:x}", x)).rev().collect(); - // println!("{:x?}", read_out); } From f3e1a73ebe246666f892a5a7f94d48e8d1c905ee Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 26 Sep 2022 20:28:29 -0700 Subject: [PATCH 77/97] all reference tests are working! --- evm/src/cpu/kernel/asm/ripemd/update.asm | 4 ++-- evm/src/cpu/kernel/tests/ripemd.rs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/update.asm b/evm/src/cpu/kernel/asm/ripemd/update.asm index a9ef166a..89284ccd 100644 --- a/evm/src/cpu/kernel/asm/ripemd/update.asm +++ b/evm/src/cpu/kernel/asm/ripemd/update.asm @@ -99,8 +99,8 @@ update_2: SWAP5 %add_const(64) SWAP5 - %stack (STATE: 5, shift) -> (shift, 64, STATE) - DUP14 + %stack (STATE: 5, shift) -> (shift, 64, STATE, shift) + DUP13 ADD SUB // stack: offset, STATE, shift, need, have, count, length, virt, retdest diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index 8741103f..d354237b 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -32,10 +32,10 @@ fn test_ripemd() -> Result<()> { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "0xb0e20b6e3116640286ed3a87a5713079b21f5189", ), - // ( - // "12345678901234567890123456789012345678901234567890123456789012345678901234567890", - // "0x9b752e45573d4b39f4dbd3323cab82bf63326bfb", - // ) + ( + "12345678901234567890123456789012345678901234567890123456789012345678901234567890", + "0x9b752e45573d4b39f4dbd3323cab82bf63326bfb", + ) ]; for (x, y) in reference { From fc761f7c20814bb4d10280bf502960bbf6f851c3 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Tue, 27 Sep 2022 00:38:41 -0700 Subject: [PATCH 78/97] SHR --- evm/src/cpu/kernel/interpreter.rs | 2 +- evm/src/cpu/kernel/tests/ripemd.rs | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/evm/src/cpu/kernel/interpreter.rs b/evm/src/cpu/kernel/interpreter.rs index 066a1360..b2ba563f 100644 --- a/evm/src/cpu/kernel/interpreter.rs +++ b/evm/src/cpu/kernel/interpreter.rs @@ -226,7 +226,7 @@ impl<'a> Interpreter<'a> { 0x19 => self.run_not(), // "NOT", 0x1a => self.run_byte(), // "BYTE", 0x1b => self.run_shl(), // "SHL", - 0x1c => todo!(), // "SHR", + 0x1c => self.run_shr(), // "SHR", 0x1d => todo!(), // "SAR", 0x20 => self.run_keccak256(), // "KECCAK256", 0x30 => todo!(), // "ADDRESS", diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index d354237b..3da46c42 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -48,13 +48,11 @@ fn test_ripemd() -> Result<()> { let final_stack: Vec = run_with_kernel(&kernel, initial_offset, initial_stack)? .stack() .to_vec(); - let actual = final_stack[0]; - - let read_out: Vec = final_stack.iter().map(|x| format!("{:x}", x)).rev().collect(); - println!("{:x?}", read_out); - assert_eq!(actual, expected); + + // let read_out: Vec = final_stack.iter().map(|x| format!("{:x}", x)).rev().collect(); + // println!("{:x?}", read_out); } Ok(()) } From bfb1f54ce31f3b18cc8607e440f102ca643b4266 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Tue, 27 Sep 2022 09:33:04 -0700 Subject: [PATCH 79/97] format --- evm/src/cpu/kernel/tests/ripemd.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index 3da46c42..79e93d4c 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -35,7 +35,7 @@ fn test_ripemd() -> Result<()> { ( "12345678901234567890123456789012345678901234567890123456789012345678901234567890", "0x9b752e45573d4b39f4dbd3323cab82bf63326bfb", - ) + ), ]; for (x, y) in reference { From 71d8384a0f0c209aa0e4e21e4e0254a030bfd93c Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Tue, 27 Sep 2022 22:17:48 -0700 Subject: [PATCH 80/97] Nick's comments --- evm/src/cpu/kernel/asm/ripemd/compression.asm | 7 ++----- evm/src/cpu/kernel/asm/ripemd/constants.asm | 20 ------------------- evm/src/cpu/kernel/asm/util/basic_macros.asm | 2 +- evm/src/memory/segments.rs | 1 - 4 files changed, 3 insertions(+), 27 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index 328a5074..5ce5f347 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -19,7 +19,7 @@ /// u32(s4 + l0 + r1), /// u32(s0 + l1 + r2) /// -/// where si, li, ri, oi, OF, RD respectively denote +/// where si, li, ri, oi, VR, RD respectively denote /// state[i], stateL[i], stateR[i], output[i], virt, retdest global compress: @@ -39,9 +39,7 @@ switch: // stack: STATEL, STATE, virt, retdest PUSH mix DUP12 - PUSH 0 - PUSH 5 - PUSH 16 + %stack () -> (16, 5, 0) // stack: 16, 5, 0, virt, mix, STATEL, STATE, virt, retdest DUP15 DUP15 @@ -203,7 +201,6 @@ pre_rol: %mload_kernel_code_label(R_data) ADD // stack: virt + r, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt - // %mload_kernel_code_u32_LE(Input_Block) %load_u32_from_block // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt SWAP1 diff --git a/evm/src/cpu/kernel/asm/ripemd/constants.asm b/evm/src/cpu/kernel/asm/ripemd/constants.asm index 1ce49c28..18bf3595 100644 --- a/evm/src/cpu/kernel/asm/ripemd/constants.asm +++ b/evm/src/cpu/kernel/asm/ripemd/constants.asm @@ -118,23 +118,3 @@ global R_data: BYTES 04, 20, 32, 28 BYTES 24, 08, 52, 56 BYTES 00, 12, 36, 44 - - -global Input_Block: - BYTES 0x61, 0x62, 0x63, 0x64 - BYTES 0x65, 0x66, 0x67, 0x68 - BYTES 0x69, 0x6a, 0x6b, 0x6c - BYTES 0x6d, 0x6e, 0x6f, 0x70 - BYTES 0x71, 0x72, 0x73, 0x74 - BYTES 0x75, 0x76, 0x77, 0x78 - BYTES 0x79, 0x7a, 0x80, 0x00 - BYTES 0x00, 0x00, 0x00, 0x00 - BYTES 0x00, 0x00, 0x00, 0x00 - BYTES 0x00, 0x00, 0x00, 0x00 - BYTES 0x00, 0x00, 0x00, 0x00 - BYTES 0x00, 0x00, 0x00, 0x00 - BYTES 0x00, 0x00, 0x00, 0x00 - BYTES 0x00, 0x00, 0x00, 0x00 - BYTES 0xd0, 0x00, 0x00, 0x00 - BYTES 0x00, 0x00, 0x00, 0x00 - \ No newline at end of file diff --git a/evm/src/cpu/kernel/asm/util/basic_macros.asm b/evm/src/cpu/kernel/asm/util/basic_macros.asm index edf63359..3027d338 100644 --- a/evm/src/cpu/kernel/asm/util/basic_macros.asm +++ b/evm/src/cpu/kernel/asm/util/basic_macros.asm @@ -295,4 +295,4 @@ %macro break %jump(0xdeadbeef) -%endmacro \ No newline at end of file +%endmacro diff --git a/evm/src/memory/segments.rs b/evm/src/memory/segments.rs index 5b3725d7..f32e0f5e 100644 --- a/evm/src/memory/segments.rs +++ b/evm/src/memory/segments.rs @@ -37,7 +37,6 @@ pub(crate) enum Segment { /// See also `StateTrieCheckpointPointer`. StorageTrieCheckpointPointers = 14, /// RipeMD storage - /// RipeMd = 15, } From 8f18f815875a92835e2156d1a82e59065ec7ae2c Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Tue, 27 Sep 2022 22:20:32 -0700 Subject: [PATCH 81/97] unused macro --- evm/src/cpu/kernel/asm/memory/core.asm | 30 -------------------------- 1 file changed, 30 deletions(-) diff --git a/evm/src/cpu/kernel/asm/memory/core.asm b/evm/src/cpu/kernel/asm/memory/core.asm index 264d9439..963e88ea 100644 --- a/evm/src/cpu/kernel/asm/memory/core.asm +++ b/evm/src/cpu/kernel/asm/memory/core.asm @@ -110,36 +110,6 @@ // stack: (((((c_3 << 8) | c_2) << 8) | c_1) << 8) | c_0 %endmacro -// Load LE u32 at given label from 4 bytes (a, b, c, d) -%macro mload_kernel_code_u32_LE(label) - // stack: offset - PUSH $label - ADD - // stack: offset - DUP1 - %mload_kernel_code - // stack: a , offset - DUP2 - %add_const(1) - %mload_kernel_code - %shl_const(8) - OR - // stack: a | (b << 8) , offset - DUP2 - %add_const(2) - %mload_kernel_code - %shl_const(16) - OR - // stack: a | (b << 8) | (c << 16), offset - SWAP1 - %add_const(3) - %mload_kernel_code - %shl_const(24) - OR - // stack: a | (b << 8) | (c << 16) | (d << 24) -%endmacro - - // Store a single byte to kernel code. %macro mstore_kernel_code // stack: offset, value From 7202f403c685009efb8dea1d67109846826f27f8 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Wed, 28 Sep 2022 00:00:58 -0700 Subject: [PATCH 82/97] spacing --- evm/src/cpu/kernel/asm/ripemd/compression.asm | 9 +++------ evm/src/cpu/kernel/asm/ripemd/constants.asm | 9 +++------ evm/src/cpu/kernel/asm/ripemd/memory.asm | 2 -- evm/src/cpu/kernel/asm/ripemd/ripemd.asm | 4 ++-- 4 files changed, 8 insertions(+), 16 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index 5ce5f347..bee9c6e8 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -113,7 +113,6 @@ mix: /// boxes = 16 /// rounds -= 1 - loop: // stack: STATE, F, K, 16, rounds, sides, virt, retdest DUP9 @@ -138,7 +137,7 @@ update_round_vars: SWAP8 POP %mul_const(4) - %mload_kernel_code_label_u32(K_data) + %mload_kernel_code_label_u32(k_data) SWAP7 POP // stack: STATE, F', K', 16, rounds, sides, virt, retdest @@ -180,7 +179,6 @@ round: /// /// return e, a, b, c, d, F, K - box: // stack: a, b, c, d, e, F, K, boxes, rounds, sides, virt PUSH pre_rol @@ -198,7 +196,7 @@ pre_rol: // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, virt DUP12 DUP2 - %mload_kernel_code_label(R_data) + %mload_kernel_code_label(r_data) ADD // stack: virt + r, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt %load_u32_from_block @@ -214,7 +212,7 @@ pre_rol: PUSH mid_rol SWAP2 // stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, virt - %mload_kernel_code_label(S_data) + %mload_kernel_code_label(s_data) // stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, virt %jump(rol) mid_rol: @@ -240,7 +238,6 @@ post_rol: // stack: e, a, b, c, d, F, K, boxes-1, rounds, sides, virt %jump(round) - %macro get_round // stack: sides, rounds %mul_const(5) diff --git a/evm/src/cpu/kernel/asm/ripemd/constants.asm b/evm/src/cpu/kernel/asm/ripemd/constants.asm index 18bf3595..7a8959fe 100644 --- a/evm/src/cpu/kernel/asm/ripemd/constants.asm +++ b/evm/src/cpu/kernel/asm/ripemd/constants.asm @@ -1,4 +1,4 @@ -global K_data: +global k_data: // Left BYTES 0x00, 0x00, 0x00, 0x00 BYTES 0x5A, 0x82, 0x79, 0x99 @@ -12,8 +12,7 @@ global K_data: BYTES 0x7A, 0x6D, 0x76, 0xE9 BYTES 0x00, 0x00, 0x00, 0x00 - -global S_data: +global s_data: // Left Round 0 BYTES 11, 14, 15, 12 BYTES 05, 08, 07, 09 @@ -39,7 +38,6 @@ global S_data: BYTES 06, 08, 13, 12 BYTES 05, 12, 13, 14 BYTES 11, 08, 05, 06 - // Right Round 0 BYTES 08, 09, 09, 11 BYTES 13, 15, 15, 05 @@ -66,8 +64,7 @@ global S_data: BYTES 08, 13, 06, 05 BYTES 15, 13, 11, 11 - -global R_data: +global r_data: // Left Round 0 BYTES 00, 04, 08, 12 BYTES 16, 20, 24, 28 diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm index f8338dcc..88cd6a59 100644 --- a/evm/src/cpu/kernel/asm/ripemd/memory.asm +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -51,7 +51,6 @@ store_input_alt: POP %jump(ripemd_init) - store_input: // stack: ADDR , rem , length DUP3 @@ -81,7 +80,6 @@ store_input: // stack: length %jump(ripemd_init) - %macro store_zeros(N, label) // stack: i %stack (i) -> ($N, i, 0, i) diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm index 6c3c0397..c1494b59 100644 --- a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm +++ b/evm/src/cpu/kernel/asm/ripemd/ripemd.asm @@ -86,8 +86,8 @@ global process: /// def padlength(length): -/// x = 56 - length % 64 -/// return x + 64*(x < 9) +/// t = length % 64 +/// return 56 + 64*(t > 47) - t %macro padlength // stack: count From e1cdd8248a43e4c816a8bca929c676f26ba47786 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 29 Sep 2022 06:46:23 -0700 Subject: [PATCH 83/97] spacing --- evm/src/cpu/kernel/asm/ripemd/memory.asm | 1 - evm/src/cpu/kernel/asm/ripemd/subroutines.asm | 8 +------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm index 88cd6a59..fa842535 100644 --- a/evm/src/cpu/kernel/asm/ripemd/memory.asm +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -155,7 +155,6 @@ store_input: // stack: a | (b << 8) | (c << 16) | (d << 24) %endmacro - // set offset i to offset j in SEGMENT_RIPEMD %macro mupdate_ripemd // stack: j, i diff --git a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm index cc4cab29..1558ecf5 100644 --- a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm +++ b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm @@ -30,7 +30,7 @@ global rol: // acc += (i==rnd)*F // return acc, rnd // -// the macro %this_F(i,F) enacts +// %this_F(i,F) enacts // acc += (i==rnd)*F %macro push_f @@ -49,7 +49,6 @@ global rol: // stack: F, rnd %endmacro - %macro this_F(i, F) // stack: acc, rnd DUP2 @@ -62,7 +61,6 @@ global rol: // stack: (rnd==j)*F + acc, rnd %endmacro - /// def F0(x, y, z): /// return x ^ y ^ z @@ -75,7 +73,6 @@ global F0: SWAP1 JUMP - /// def F1(x, y, z): /// return (x & y) | (u32(~x) & z) @@ -100,7 +97,6 @@ global F1: SWAP1 JUMP - /// def F2(x, y, z): /// return (x | u32(~y)) ^ z @@ -117,7 +113,6 @@ global F2: SWAP1 JUMP - /// def F3(x, y, z): /// return (x & z) | (u32(~z) & y) @@ -138,7 +133,6 @@ global F3: SWAP1 JUMP - /// def F4(x, y, z): /// return x ^ (y | u32(~z)) From abff9788261d6da3dafeaa24ec33693319ea21e7 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 29 Sep 2022 13:58:49 -0700 Subject: [PATCH 84/97] Daniel's comments --- evm/src/cpu/kernel/asm/memory/core.asm | 6 ++++-- evm/src/cpu/kernel/asm/ripemd/compression.asm | 15 +++++---------- evm/src/cpu/kernel/asm/ripemd/subroutines.asm | 2 +- evm/src/cpu/kernel/asm/util/basic_macros.asm | 15 +++++---------- 4 files changed, 15 insertions(+), 23 deletions(-) diff --git a/evm/src/cpu/kernel/asm/memory/core.asm b/evm/src/cpu/kernel/asm/memory/core.asm index 963e88ea..6c7d9b40 100644 --- a/evm/src/cpu/kernel/asm/memory/core.asm +++ b/evm/src/cpu/kernel/asm/memory/core.asm @@ -72,10 +72,12 @@ %endmacro %macro mload_kernel_code_label_u32(label) - // stack: shift + // stack: u32_shift + %mul_const(4) + // stack: byte_shift PUSH $label ADD - // stack: label + shift + // stack: label + byte_shift %mload_kernel_code_u32 // stack: u32 %endmacro diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index bee9c6e8..bdeb010b 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -135,8 +135,7 @@ update_round_vars: SWAP7 // stack: rnd, rnd, STATE, F', K , 16, rounds, sides, virt, retdest SWAP8 - POP - %mul_const(4) + POP %mload_kernel_code_label_u32(k_data) SWAP7 POP @@ -207,7 +206,7 @@ pre_rol: ADD DUP8 ADD - %u32 + %as_u32 // stack: a, box, b, c, d, e, F, K, boxes, rounds, sides, virt PUSH mid_rol SWAP2 @@ -220,14 +219,10 @@ mid_rol: DUP5 // stack: e, a, b, c, d, e, F, K, boxes, rounds, sides, virt ADD - %u32 + %as_u32 // stack: a, b, c, d, e, F, K, boxes, rounds, sides, virt - SWAP1 - SWAP2 - PUSH post_rol - SWAP1 - PUSH 10 - // stack: 10, c, post_rol, b, a, d, e, F, K, boxes, rounds, sides, virt + %stack (a, b, c) -> (10, c, post_rol, a, b) + // stack: 10, c, post_rol, a, b, d, e, F, K, boxes, rounds, sides, virt %jump(rol) post_rol: // stack: c, a, b, d, e, F, K, boxes , rounds, sides, virt diff --git a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm index 1558ecf5..6cc3db17 100644 --- a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm +++ b/evm/src/cpu/kernel/asm/ripemd/subroutines.asm @@ -16,7 +16,7 @@ global rol: // stack: n, x, x >> (32-n), retdest SHL // stack: x << n, x >> (32-n), retdest - %u32 + %as_u32 // stack: u32(x << n), x >> (32-n), retdest OR // stack: u32(x << n) | (x >> (32-n)), retdest diff --git a/evm/src/cpu/kernel/asm/util/basic_macros.asm b/evm/src/cpu/kernel/asm/util/basic_macros.asm index 3027d338..ef2f6844 100644 --- a/evm/src/cpu/kernel/asm/util/basic_macros.asm +++ b/evm/src/cpu/kernel/asm/util/basic_macros.asm @@ -230,7 +230,7 @@ // stack: max %endmacro -%macro u32 +%macro as_u32 %and_const(0xffffffff) %endmacro @@ -243,15 +243,14 @@ %endmacro %macro add3_u32 - // stack: x, y, z + // stack: x , y , z ADD - // stack: x+y, z + // stack: x + y , z ADD - // stack: x+y+z - %u32 + // stack: x + y + z + %as_u32 %endmacro - // given u32 bytestring abcd return dcba %macro reverse_bytes_u32 // stack: abcd @@ -292,7 +291,3 @@ OR // stack: dcba %endmacro - -%macro break - %jump(0xdeadbeef) -%endmacro From d6b3522b3464a3bad111208568f4732077258f0e Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Thu, 29 Sep 2022 16:21:10 -0700 Subject: [PATCH 85/97] reverse bytes via BYTE code --- evm/src/cpu/kernel/asm/ripemd/memory.asm | 8 ++-- evm/src/cpu/kernel/asm/util/basic_macros.asm | 47 +++++++------------- 2 files changed, 21 insertions(+), 34 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm index fa842535..eb9c83f7 100644 --- a/evm/src/cpu/kernel/asm/ripemd/memory.asm +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -22,9 +22,9 @@ store_size: // stack: ab %extract_and_store_byte(70) // stack: a - %mstore_ripemd_offset(71) + %mstore_ripemd(71) // stack: 0x80 // padding has 0x80 in first position and zeros elsewhere - %mstore_ripemd_offset(72) // store first padding term here so as to avoid extra label + %mstore_ripemd(72) // store first padding term here so as to avoid extra label %jump(store_padding) store_padding: @@ -107,11 +107,11 @@ store_input: DIV SWAP1 // stack: y, xs - %mstore_ripemd_offset($offset) + %mstore_ripemd($offset) // stack: xs %endmacro -%macro mstore_ripemd_offset(offset) +%macro mstore_ripemd(offset) // stack: value PUSH $offset // stack: offset, value diff --git a/evm/src/cpu/kernel/asm/util/basic_macros.asm b/evm/src/cpu/kernel/asm/util/basic_macros.asm index ef2f6844..14d402c7 100644 --- a/evm/src/cpu/kernel/asm/util/basic_macros.asm +++ b/evm/src/cpu/kernel/asm/util/basic_macros.asm @@ -255,39 +255,26 @@ %macro reverse_bytes_u32 // stack: abcd DUP1 - %and_const(0xFF) - // stack: d, abcd - %stack (d, abcd) -> (abcd, d, 0x100, d) - // stack: abcd, d, 0x100, d - SUB - DIV - // stack: abc, d - DUP1 - %and_const(0xFF) - // 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 - %stack (b, ab) -> (ab, b, 0x100, b) - // stack: ab, b, 0x100, b, c, d - SUB - DIV - // stack: a, b, c, d - SWAP1 + PUSH 28 + BYTE + // stack: a, abcd + DUP2 + PUSH 29 + BYTE %shl_const(8) - OR - // stack: ba, c, d - SWAP1 + // stack: b0, a, abcd + DUP3 + PUSH 30 + BYTE %shl_const(16) - OR - // stack: cba, d - SWAP1 + // stack: c00, b0, a, abcd + SWAP3 + PUSH 31 + BYTE %shl_const(24) + // stack: d000, b0, a, c00 + OR + OR OR // stack: dcba %endmacro From cb88dadd291d0efe8f2c51ea99754e12b10fe4b5 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 3 Oct 2022 12:21:43 -0700 Subject: [PATCH 86/97] clean up macros, remove ripemd segment --- evm/src/cpu/kernel/aggregator.rs | 4 ++-- evm/src/cpu/kernel/asm/memory/core.asm | 24 +++++++++---------- evm/src/cpu/kernel/asm/ripemd/compression.asm | 6 ++--- .../ripemd/{subroutines.asm => functions.asm} | 0 .../asm/ripemd/{ripemd.asm => main.asm} | 0 evm/src/cpu/kernel/asm/ripemd/memory.asm | 10 ++++---- evm/src/memory/segments.rs | 7 +----- 7 files changed, 23 insertions(+), 28 deletions(-) rename evm/src/cpu/kernel/asm/ripemd/{subroutines.asm => functions.asm} (100%) rename evm/src/cpu/kernel/asm/ripemd/{ripemd.asm => main.asm} (100%) diff --git a/evm/src/cpu/kernel/aggregator.rs b/evm/src/cpu/kernel/aggregator.rs index ae728f09..c11180e3 100644 --- a/evm/src/cpu/kernel/aggregator.rs +++ b/evm/src/cpu/kernel/aggregator.rs @@ -41,9 +41,9 @@ pub(crate) fn combined_kernel() -> Kernel { include_str!("asm/memory/txn_fields.asm"), include_str!("asm/ripemd/compression.asm"), include_str!("asm/ripemd/constants.asm"), + include_str!("asm/ripemd/functions.asm"), + include_str!("asm/ripemd/main.asm"), include_str!("asm/ripemd/memory.asm"), - include_str!("asm/ripemd/ripemd.asm"), - include_str!("asm/ripemd/subroutines.asm"), include_str!("asm/ripemd/update.asm"), include_str!("asm/rlp/encode.asm"), include_str!("asm/rlp/decode.asm"), diff --git a/evm/src/cpu/kernel/asm/memory/core.asm b/evm/src/cpu/kernel/asm/memory/core.asm index cfce6f6e..2bf4ae6f 100644 --- a/evm/src/cpu/kernel/asm/memory/core.asm +++ b/evm/src/cpu/kernel/asm/memory/core.asm @@ -62,7 +62,7 @@ // stack: value %endmacro -%macro mload_kernel_code_label(label) +%macro mload_kernel_code(label) // stack: shift PUSH $label ADD @@ -71,17 +71,6 @@ // stack: byte %endmacro -%macro mload_kernel_code_label_u32(label) - // stack: u32_shift - %mul_const(4) - // stack: byte_shift - PUSH $label - ADD - // stack: label + byte_shift - %mload_kernel_code_u32 - // stack: u32 -%endmacro - // Load a big-endian u32, consisting of 4 bytes (c_3, c_2, c_1, c_0), // from kernel code. %macro mload_kernel_code_u32 @@ -112,6 +101,17 @@ // stack: (((((c_3 << 8) | c_2) << 8) | c_1) << 8) | c_0 %endmacro +%macro mload_kernel_code_u32(label) + // stack: u32_shift + %mul_const(4) + // stack: byte_shift + PUSH $label + ADD + // stack: label + byte_shift + %mload_kernel_code_u32 + // stack: u32 +%endmacro + // Store a single byte to kernel code. %macro mstore_kernel_code // stack: offset, value diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index bdeb010b..a1535e30 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -136,7 +136,7 @@ update_round_vars: // stack: rnd, rnd, STATE, F', K , 16, rounds, sides, virt, retdest SWAP8 POP - %mload_kernel_code_label_u32(k_data) + %mload_kernel_code_u32(k_data) SWAP7 POP // stack: STATE, F', K', 16, rounds, sides, virt, retdest @@ -195,7 +195,7 @@ pre_rol: // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, virt DUP12 DUP2 - %mload_kernel_code_label(r_data) + %mload_kernel_code(r_data) ADD // stack: virt + r, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt %load_u32_from_block @@ -211,7 +211,7 @@ pre_rol: PUSH mid_rol SWAP2 // stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, virt - %mload_kernel_code_label(s_data) + %mload_kernel_code(s_data) // stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, virt %jump(rol) mid_rol: diff --git a/evm/src/cpu/kernel/asm/ripemd/subroutines.asm b/evm/src/cpu/kernel/asm/ripemd/functions.asm similarity index 100% rename from evm/src/cpu/kernel/asm/ripemd/subroutines.asm rename to evm/src/cpu/kernel/asm/ripemd/functions.asm diff --git a/evm/src/cpu/kernel/asm/ripemd/ripemd.asm b/evm/src/cpu/kernel/asm/ripemd/main.asm similarity index 100% rename from evm/src/cpu/kernel/asm/ripemd/ripemd.asm rename to evm/src/cpu/kernel/asm/ripemd/main.asm diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm index eb9c83f7..19bcf78d 100644 --- a/evm/src/cpu/kernel/asm/ripemd/memory.asm +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -115,21 +115,21 @@ store_input: // stack: value PUSH $offset // stack: offset, value - %mstore_kernel(@SEGMENT_RIPEMD) + %mstore_kernel(@SEGMENT_KERNEL_GENERAL) // stack: %endmacro %macro mstore_ripemd // stack: offset, value - %mstore_kernel(@SEGMENT_RIPEMD) + %mstore_kernel(@SEGMENT_KERNEL_GENERAL) // stack: %endmacro %macro mload_ripemd - %mload_kernel(@SEGMENT_RIPEMD) + %mload_kernel(@SEGMENT_KERNEL_GENERAL) %endmacro -// Load LE u32 from 4 contiguous bytes a, b, c, d in SEGMENT_RIPEMD +// Load LE u32 from 4 contiguous bytes a, b, c, d %macro load_u32_from_block // stack: offset DUP1 @@ -155,7 +155,7 @@ store_input: // stack: a | (b << 8) | (c << 16) | (d << 24) %endmacro -// set offset i to offset j in SEGMENT_RIPEMD +// set offset i to offset j in memory %macro mupdate_ripemd // stack: j, i %mload_ripemd diff --git a/evm/src/memory/segments.rs b/evm/src/memory/segments.rs index f32e0f5e..0a0b6245 100644 --- a/evm/src/memory/segments.rs +++ b/evm/src/memory/segments.rs @@ -36,12 +36,10 @@ pub(crate) enum Segment { /// `StorageTriePointers` with `StorageTrieCheckpointPointers`. /// See also `StateTrieCheckpointPointer`. StorageTrieCheckpointPointers = 14, - /// RipeMD storage - RipeMd = 15, } impl Segment { - pub(crate) const COUNT: usize = 16; + pub(crate) const COUNT: usize = 15; pub(crate) fn all() -> [Self; Self::COUNT] { [ @@ -60,7 +58,6 @@ impl Segment { Self::StorageTrieAddresses, Self::StorageTriePointers, Self::StorageTrieCheckpointPointers, - Self::RipeMd, ] } @@ -82,7 +79,6 @@ impl Segment { Segment::StorageTrieAddresses => "SEGMENT_STORAGE_TRIE_ADDRS", Segment::StorageTriePointers => "SEGMENT_STORAGE_TRIE_PTRS", Segment::StorageTrieCheckpointPointers => "SEGMENT_STORAGE_TRIE_CHECKPOINT_PTRS", - Segment::RipeMd => "SEGMENT_RIPEMD", } } @@ -104,7 +100,6 @@ impl Segment { Segment::StorageTrieAddresses => 160, Segment::StorageTriePointers => 32, Segment::StorageTrieCheckpointPointers => 32, - Segment::RipeMd => 8, } } } From 1e7c33e83ce9c3cbc5ffe508e11b402be8e75452 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 3 Oct 2022 12:35:49 -0700 Subject: [PATCH 87/97] extraxt box into own module --- evm/src/cpu/kernel/aggregator.rs | 1 + evm/src/cpu/kernel/asm/ripemd/box.asm | 96 ++++++++++++++ evm/src/cpu/kernel/asm/ripemd/compression.asm | 122 ++---------------- 3 files changed, 109 insertions(+), 110 deletions(-) create mode 100644 evm/src/cpu/kernel/asm/ripemd/box.asm diff --git a/evm/src/cpu/kernel/aggregator.rs b/evm/src/cpu/kernel/aggregator.rs index c11180e3..c967dc3c 100644 --- a/evm/src/cpu/kernel/aggregator.rs +++ b/evm/src/cpu/kernel/aggregator.rs @@ -39,6 +39,7 @@ pub(crate) fn combined_kernel() -> Kernel { include_str!("asm/memory/metadata.asm"), include_str!("asm/memory/packing.asm"), include_str!("asm/memory/txn_fields.asm"), + include_str!("asm/ripemd/box.asm"), include_str!("asm/ripemd/compression.asm"), include_str!("asm/ripemd/constants.asm"), include_str!("asm/ripemd/functions.asm"), diff --git a/evm/src/cpu/kernel/asm/ripemd/box.asm b/evm/src/cpu/kernel/asm/ripemd/box.asm new file mode 100644 index 00000000..81e89c1f --- /dev/null +++ b/evm/src/cpu/kernel/asm/ripemd/box.asm @@ -0,0 +1,96 @@ +/// Note that we unpack STATE: 5 to a, b, c, d, e +/// All additions are u32 +/// +/// def box(a, b, c, d, e, F, K): +/// +/// box = get_box(sides, rounds, boxes) +/// a += F(b, c, d) +/// r = load(r)(box) +/// x = load_offset(r) +/// a += x + K +/// s = load(s)(box) +/// a = rol(s, a) +/// a += e +/// c = rol(10, c) +/// +/// return e, a, b, c, d, F, K + +global box: + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, virt + PUSH pre_rol + DUP5 + DUP5 + DUP5 + DUP10 + // stack: F, b, c, d, pre_rol, a, b, c, d, e, F, K, boxes, rounds, sides, virt + JUMP +pre_rol: + // stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides, virt + ADD + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, virt + %get_box + // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, virt + DUP12 + DUP2 + %mload_kernel_code(r_data) + ADD + // stack: virt + r, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt + %load_u32_from_block + // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt + SWAP1 + SWAP2 + // stack: a, x, box, b, c, d, e, F, K, boxes, rounds, sides, virt + ADD + DUP8 + ADD + %as_u32 + // stack: a, box, b, c, d, e, F, K, boxes, rounds, sides, virt + PUSH mid_rol + SWAP2 + // stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, virt + %mload_kernel_code(s_data) + // stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, virt + %jump(rol) +mid_rol: + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, virt + DUP5 + // stack: e, a, b, c, d, e, F, K, boxes, rounds, sides, virt + ADD + %as_u32 + // stack: a, b, c, d, e, F, K, boxes, rounds, sides, virt + %stack (a, b, c) -> (10, c, post_rol, a, b) + // stack: 10, c, post_rol, a, b, d, e, F, K, boxes, rounds, sides, virt + %jump(rol) +post_rol: + // stack: c, a, b, d, e, F, K, boxes , rounds, sides, virt + %stack (c, a, b, d, e, F, K, boxes) -> (boxes, 1, a, b, c, d, F, K, e) + // stack: boxes, 1, a, b, c, d, F, K, e, rounds, sides, virt + SUB + SWAP7 + // stack: e, a, b, c, d, F, K, boxes-1, rounds, sides, virt + %jump(round) + + +%macro get_round + // stack: sides, rounds + %mul_const(5) + PUSH 10 + SUB + SUB + // stack: 10 - 5*sides - rounds +%endmacro + +%macro get_box + // stack: ARGS: 7, boxes, rounds, sides + DUP10 + %mul_const(80) + DUP10 + %mul_const(16) + DUP10 + // stack: boxes , 16*rounds , 80*sides, ARGS: 7, boxes, rounds, sides + PUSH 176 + SUB + SUB + SUB + // stack: 176 - boxes - 16*rounds - 80*sides, ARGS: 7, boxes, rounds, sides +%endmacro diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index a1535e30..a83bf832 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -1,17 +1,16 @@ -/// _block is stored in memory and its address virt remains on the stack -/// Note that STATE takes up 5 stack slots -/// def compress(state, _block): +/// _block is stored in memory: its address virt stays on the stack +/// def compress(STATE: 5, _block): /// -/// stateL = state -/// stateL = loop(stateL) +/// STATEL = STATE +/// STATEL = loop(STATEL) /// -/// stateR = state -/// stateR = loop(stateR) +/// STATER = state +/// STATER = loop(STATER) /// -/// return mix(state, stateL, stateR) +/// return mix(STATER, STATEL, STATE) /// /// -/// def mix(stateR, stateL, state): +/// def mix(STATER, STATEL, STATE): /// return /// u32(s1 + l2 + r3), /// u32(s2 + l3 + r4), @@ -20,7 +19,7 @@ /// u32(s0 + l1 + r2) /// /// where si, li, ri, oi, VR, RD respectively denote -/// state[i], stateL[i], stateR[i], output[i], virt, retdest +/// STATE[i], STATEL[i], STATER[i], OUTPUT[i], virt, retdest global compress: // stack: STATE, virt, retdest @@ -97,10 +96,10 @@ mix: JUMP -/// def loop(STATE): +/// def loop(STATE: 5): /// while rounds: /// update_round_vars() -/// round(STATE, F, K, rounds, sides) +/// round(STATE: 5, F, K, rounds, sides) /// /// def update_round_vars(): /// F = load(F)(sides, rounds) @@ -141,7 +140,7 @@ update_round_vars: POP // stack: STATE, F', K', 16, rounds, sides, virt, retdest %jump(round) -round: +global round: // stack: STATE, F, K, boxes, rounds , sides, virt, retdest DUP8 // stack: boxes, STATE, F, K, boxes, rounds , sides, virt, retdest @@ -159,100 +158,3 @@ round: POP // stack: STATE, F, K, 16, rounds-1, sides, virt, retdest %jump(loop) - - -/// Note that we unpack STATE to a, b, c, d, e -/// All additions are u32 -/// -/// def box(a, b, c, d, e, F, K): -/// -/// box = get_box(sides, rounds, boxes) -/// a += F(b, c, d) -/// r = load(r)(box) -/// x = load_offset(r) -/// a += x + K -/// s = load(s)(box) -/// a = rol(s, a) -/// a += e -/// c = rol(10, c) -/// -/// return e, a, b, c, d, F, K - -box: - // stack: a, b, c, d, e, F, K, boxes, rounds, sides, virt - PUSH pre_rol - DUP5 - DUP5 - DUP5 - DUP10 - // stack: F, b, c, d, pre_rol, a, b, c, d, e, F, K, boxes, rounds, sides, virt - JUMP -pre_rol: - // stack: F(b, c, d), a, b, c, d, e, F, K, boxes, rounds, sides, virt - ADD - // stack: a, b, c, d, e, F, K, boxes, rounds, sides, virt - %get_box - // stack: box, a, b, c, d, e, F, K, boxes, rounds, sides, virt - DUP12 - DUP2 - %mload_kernel_code(r_data) - ADD - // stack: virt + r, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt - %load_u32_from_block - // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt - SWAP1 - SWAP2 - // stack: a, x, box, b, c, d, e, F, K, boxes, rounds, sides, virt - ADD - DUP8 - ADD - %as_u32 - // stack: a, box, b, c, d, e, F, K, boxes, rounds, sides, virt - PUSH mid_rol - SWAP2 - // stack: box, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, virt - %mload_kernel_code(s_data) - // stack: s, a, mid_rol, b, c, d, e, F, K, boxes, rounds, sides, virt - %jump(rol) -mid_rol: - // stack: a, b, c, d, e, F, K, boxes, rounds, sides, virt - DUP5 - // stack: e, a, b, c, d, e, F, K, boxes, rounds, sides, virt - ADD - %as_u32 - // stack: a, b, c, d, e, F, K, boxes, rounds, sides, virt - %stack (a, b, c) -> (10, c, post_rol, a, b) - // stack: 10, c, post_rol, a, b, d, e, F, K, boxes, rounds, sides, virt - %jump(rol) -post_rol: - // stack: c, a, b, d, e, F, K, boxes , rounds, sides, virt - %stack (c, a, b, d, e, F, K, boxes) -> (boxes, 1, a, b, c, d, F, K, e) - // stack: boxes, 1, a, b, c, d, F, K, e, rounds, sides, virt - SUB - SWAP7 - // stack: e, a, b, c, d, F, K, boxes-1, rounds, sides, virt - %jump(round) - -%macro get_round - // stack: sides, rounds - %mul_const(5) - PUSH 10 - SUB - SUB - // stack: 10 - 5*sides - rounds -%endmacro - -%macro get_box - // stack: ARGS: 7, boxes, rounds, sides - DUP10 - %mul_const(80) - DUP10 - %mul_const(16) - DUP10 - // stack: boxes , 16*rounds , 80*sides, ARGS: 7, boxes, rounds, sides - PUSH 176 - SUB - SUB - SUB - // stack: 176 - boxes - 16*rounds - 80*sides, ARGS: 7, boxes, rounds, sides -%endmacro From 4d8f618fd2ed109593bd509ec3461907037c2d92 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 3 Oct 2022 12:45:53 -0700 Subject: [PATCH 88/97] put macros in more general module --- evm/src/cpu/kernel/asm/memory/core.asm | 57 ++++++++++++++++++++ evm/src/cpu/kernel/asm/ripemd/box.asm | 2 +- evm/src/cpu/kernel/asm/ripemd/memory.asm | 66 +++--------------------- evm/src/cpu/kernel/asm/ripemd/update.asm | 2 +- 4 files changed, 65 insertions(+), 62 deletions(-) diff --git a/evm/src/cpu/kernel/asm/memory/core.asm b/evm/src/cpu/kernel/asm/memory/core.asm index 2bf4ae6f..417dc738 100644 --- a/evm/src/cpu/kernel/asm/memory/core.asm +++ b/evm/src/cpu/kernel/asm/memory/core.asm @@ -125,3 +125,60 @@ %mstore_kernel(@SEGMENT_RLP_RAW) // stack: (empty) %endmacro + +%macro mstore_kernel_general + // stack: offset, value + %mstore_kernel(@SEGMENT_KERNEL_GENERAL) + // stack: +%endmacro + +%macro mstore_kernel_general(offset) + // stack: value + PUSH $offset + // stack: offset, value + %mstore_kernel_general + // stack: +%endmacro + +%macro mload_kernel_general + // stack: + %mload_kernel(@SEGMENT_KERNEL_GENERAL) + // stack: byte +%endmacro + +// Load a big-endian u32, consisting of 4 bytes (c_0, c_1, c_2, c_3), +// from kernel general. +%macro mload_kernel_general_u32 + // stack: offset + DUP1 + %mload_kernel_general + // stack: c0 , offset + DUP2 + %add_const(1) + %mload_kernel_general + %shl_const(8) + OR + // stack: c0 | (c1 << 8) , offset + DUP2 + %add_const(2) + %mload_kernel_general + %shl_const(16) + OR + // stack: c0 | (c1 << 8) | (c2 << 16), offset + SWAP1 + %add_const(3) + %mload_kernel_general + %shl_const(24) + OR + // stack: c0 | (c1 << 8) | (c2 << 16) | (c3 << 24) +%endmacro + +// set offset i to offset j in kernel general +%macro mupdate_kernel_general + // stack: j, i + %mload_kernel_general + // stack: x, i + SWAP1 + %mstore_kernel_general + // stack: +%endmacro diff --git a/evm/src/cpu/kernel/asm/ripemd/box.asm b/evm/src/cpu/kernel/asm/ripemd/box.asm index 81e89c1f..5894f219 100644 --- a/evm/src/cpu/kernel/asm/ripemd/box.asm +++ b/evm/src/cpu/kernel/asm/ripemd/box.asm @@ -35,7 +35,7 @@ pre_rol: %mload_kernel_code(r_data) ADD // stack: virt + r, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt - %load_u32_from_block + %mload_kernel_general_u32 // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt SWAP1 SWAP2 diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm index 19bcf78d..03004859 100644 --- a/evm/src/cpu/kernel/asm/ripemd/memory.asm +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -22,9 +22,9 @@ store_size: // stack: ab %extract_and_store_byte(70) // stack: a - %mstore_ripemd(71) + %mstore_kernel_general(71) // stack: 0x80 // padding has 0x80 in first position and zeros elsewhere - %mstore_ripemd(72) // store first padding term here so as to avoid extra label + %mstore_kernel_general(72) // store first padding term here so as to avoid extra label %jump(store_padding) store_padding: @@ -41,7 +41,7 @@ store_input_alt: SUB ADD // stack: offset, byte, rem, length, REM_INP - %mstore_ripemd + %mstore_kernel_general // stack: rem, length, REM_INP %sub_const(1) DUP1 @@ -63,7 +63,7 @@ store_input: SUB %add_const(136) // stack: offset, byte, ADDR , rem , length - %mstore_ripemd + %mstore_kernel_general // stack: ADDR , rem , length SWAP2 %add_const(1) @@ -85,7 +85,7 @@ store_input: %stack (i) -> ($N, i, 0, i) SUB // stack: offset = N-i, 0, i - %mstore_ripemd + %mstore_kernel_general // stack: i %sub_const(1) DUP1 @@ -107,60 +107,6 @@ store_input: DIV SWAP1 // stack: y, xs - %mstore_ripemd($offset) + %mstore_kernel_general($offset) // stack: xs %endmacro - -%macro mstore_ripemd(offset) - // stack: value - PUSH $offset - // stack: offset, value - %mstore_kernel(@SEGMENT_KERNEL_GENERAL) - // stack: -%endmacro - -%macro mstore_ripemd - // stack: offset, value - %mstore_kernel(@SEGMENT_KERNEL_GENERAL) - // stack: -%endmacro - -%macro mload_ripemd - %mload_kernel(@SEGMENT_KERNEL_GENERAL) -%endmacro - -// Load LE u32 from 4 contiguous bytes a, b, c, d -%macro load_u32_from_block - // stack: offset - DUP1 - %mload_ripemd - // stack: a , offset - DUP2 - %add_const(1) - %mload_ripemd - %shl_const(8) - OR - // stack: a | (b << 8) , offset - DUP2 - %add_const(2) - %mload_ripemd - %shl_const(16) - OR - // stack: a | (b << 8) | (c << 16), offset - SWAP1 - %add_const(3) - %mload_ripemd - %shl_const(24) - OR - // stack: a | (b << 8) | (c << 16) | (d << 24) -%endmacro - -// set offset i to offset j in memory -%macro mupdate_ripemd - // stack: j, i - %mload_ripemd - // stack: x, i - SWAP1 - %mstore_ripemd - // stack: -%endmacro diff --git a/evm/src/cpu/kernel/asm/ripemd/update.asm b/evm/src/cpu/kernel/asm/ripemd/update.asm index 89284ccd..df52d175 100644 --- a/evm/src/cpu/kernel/asm/ripemd/update.asm +++ b/evm/src/cpu/kernel/asm/ripemd/update.asm @@ -118,7 +118,7 @@ buffer_update: DUP2 DUP2 // stack: get, set, get , set , times , retdest - %mupdate_ripemd + %mupdate_kernel_general // stack: get , set , times , retdest %add_const(1) SWAP1 From 2f97ad4416fd5b5db4b2a3d403af862dc4634ef4 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 3 Oct 2022 13:13:10 -0700 Subject: [PATCH 89/97] fix arg order for memory version --- evm/src/cpu/kernel/asm/ripemd/main.asm | 20 ++++++------ evm/src/cpu/kernel/asm/ripemd/memory.asm | 41 ++++++++++++------------ evm/src/cpu/kernel/tests/ripemd.rs | 2 +- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/main.asm b/evm/src/cpu/kernel/asm/ripemd/main.asm index c1494b59..66c409be 100644 --- a/evm/src/cpu/kernel/asm/ripemd/main.asm +++ b/evm/src/cpu/kernel/asm/ripemd/main.asm @@ -1,18 +1,20 @@ /// Variables beginning with _ are in memory /// /// def ripemd160(_input): -/// state, count, _buffer = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0], 0, [0]*64 -/// state, count, _buffer = ripemd_update(state, count, _buffer, len(input) , bytes = _input ) -/// state, count, _buffer = ripemd_update(state, count, _buffer, padlength(len(input)), bytes = [0x80]+[0]*63) -/// state, count, _buffer = ripemd_update(state, count, _buffer, 8, bytes = size(len(_input))) -/// return process(state) +/// STATE, count, _buffer = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0], 0, [0]*64 +/// STATE, count, _buffer = ripemd_update(STATE, count, _buffer, len(input) , bytes = _input ) +/// STATE, count, _buffer = ripemd_update(STATE, count, _buffer, padlength(len(input)), bytes = [0x80]+[0]*63) +/// STATE, count, _buffer = ripemd_update(STATE, count, _buffer, 8, bytes = size(len(_input))) +/// return process(STATE) /// /// ripemd is called on a stack with ADDR and length -/// ripemd_update will receive and return the stack in the form: +/// ripemd_stack is called on a stack with length, followed by the input bytes +/// +/// ripemd_update receives and return the stack in the form: /// stack: STATE, count, length, virt /// where virt is the virtual address of the bytes argument -global ripemd_alt: +global ripemd_stack: // stack: length, INPUT %stack (length) -> (64, length, 0x80, 63, length, length) // stack: 64, length, 0x80, 63, length, length, INPUT @@ -24,8 +26,8 @@ global ripemd_alt: global ripemd: // stack: ADDR, length - %stack (ADDR: 3, length) -> (64, length, 0x80, 63, ADDR, length, length) - // stack: 64, length, 0x80, 63, ADDR, length, length + %stack (ADDR: 3, length) -> (64, length, 0x80, 63, length, ADDR, length) + // stack: 64, length, 0x80, 63, length, ADDR, length %jump(ripemd_storage) // stores the following into memory // init _buffer at virt 0 [consumes 64] // store _size at virt 64 [consumes length] diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm index 03004859..69b434df 100644 --- a/evm/src/cpu/kernel/asm/ripemd/memory.asm +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -28,14 +28,15 @@ store_size: %jump(store_padding) store_padding: - // stack: i (init 63) + // stack: i [init 63], length %store_zeros(136, store_padding) + // stack: length DUP1 - %jumpi(store_input_alt) + %jumpi(store_input_stack) POP %jump(ripemd_init) -store_input_alt: +store_input_stack: // stack: rem, length, REM_INP %stack (rem, length, head) -> (length, rem, 136, head, rem, length) SUB @@ -46,38 +47,36 @@ store_input_alt: %sub_const(1) DUP1 // stack: rem - 1, rem - 1, length, REM_INP - %jumpi(store_input_alt) + %jumpi(store_input_stack) // stack: 0, length POP %jump(ripemd_init) store_input: - // stack: ADDR , rem , length - DUP3 - DUP3 - DUP3 + // stack: rem , ADDR , length + DUP4 + DUP4 + DUP4 MLOAD_GENERAL - // stack: byte, ADDR , rem , length - DUP5 + // stack: byte, rem , ADDR , length + DUP2 DUP7 SUB %add_const(136) - // stack: offset, byte, ADDR , rem , length + // stack: offset, byte, rem , ADDR , length %mstore_kernel_general - // stack: ADDR , rem , length - SWAP2 - %add_const(1) - SWAP2 - // stack: ADDR + 1, rem , length - SWAP3 + // stack: rem , ADDR , length %sub_const(1) + // stack: rem-1, ADDR , length SWAP3 - // stack: ADDR + 1, rem - 1, length - DUP4 + %add_const(1) + SWAP3 + // stack: rem-1, ADDR+1, length + DUP2 %jumpi(store_input) - // stack: ADDR , 0 , length + // stack: 0 , ADDR , length %pop4 - // stack: length + // stack: length %jump(ripemd_init) %macro store_zeros(N, label) diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index 79e93d4c..de8ebfb0 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -43,7 +43,7 @@ fn test_ripemd() -> Result<()> { let expected = U256::from(y); let kernel = combined_kernel(); - let initial_offset = kernel.global_labels["ripemd_alt"]; + let initial_offset = kernel.global_labels["ripemd_stack"]; let initial_stack: Vec = input.iter().map(|&x| U256::from(x as u8)).rev().collect(); let final_stack: Vec = run_with_kernel(&kernel, initial_offset, initial_stack)? .stack() From 32b8b3599b82de77f0b4a5305999072a84eec437 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 3 Oct 2022 13:17:29 -0700 Subject: [PATCH 90/97] move buffer_update to memory module --- evm/src/cpu/kernel/asm/ripemd/memory.asm | 26 ++++++++++++++++++++++++ evm/src/cpu/kernel/asm/ripemd/update.asm | 26 ------------------------ 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm index 69b434df..e070c235 100644 --- a/evm/src/cpu/kernel/asm/ripemd/memory.asm +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -79,6 +79,32 @@ store_input: // stack: length %jump(ripemd_init) +/// def buffer_update(get, set, times): +/// for i in range(times): +/// buffer[set+i] = bytestring[get+i] + +global buffer_update: + // stack: get , set , times , retdest + DUP2 + DUP2 + // stack: get, set, get , set , times , retdest + %mupdate_kernel_general + // stack: get , set , times , retdest + %add_const(1) + SWAP1 + %add_const(1) + SWAP1 + SWAP2 + %sub_const(1) + SWAP2 + // stack: get+1, set+1, times-1, retdest + DUP3 + %jumpi(buffer_update) + // stack: get , set , 0 , retdest + %pop3 + JUMP + + %macro store_zeros(N, label) // stack: i %stack (i) -> ($N, i, 0, i) diff --git a/evm/src/cpu/kernel/asm/ripemd/update.asm b/evm/src/cpu/kernel/asm/ripemd/update.asm index df52d175..41fecd40 100644 --- a/evm/src/cpu/kernel/asm/ripemd/update.asm +++ b/evm/src/cpu/kernel/asm/ripemd/update.asm @@ -107,29 +107,3 @@ update_2: %stack (offset, STATE: 5) -> (STATE, offset, update_2) // stack: STATE, offset, update_2, shift, need, have, count, length, virt, retdest %jump(compress) - - -/// def buffer_update(get, set, times): -/// for i in range(times): -/// buffer[set+i] = bytestring[get+i] - -buffer_update: - // stack: get , set , times , retdest - DUP2 - DUP2 - // stack: get, set, get , set , times , retdest - %mupdate_kernel_general - // stack: get , set , times , retdest - %add_const(1) - SWAP1 - %add_const(1) - SWAP1 - SWAP2 - %sub_const(1) - SWAP2 - // stack: get+1, set+1, times-1, retdest - DUP3 - %jumpi(buffer_update) - // stack: get , set , 0 , retdest - %pop3 - JUMP From 12695447122c108d5496482d5b9b5ca95047507c Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 3 Oct 2022 13:20:06 -0700 Subject: [PATCH 91/97] minor --- evm/src/cpu/kernel/asm/ripemd/memory.asm | 4 ++-- evm/src/cpu/kernel/asm/ripemd/update.asm | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm index e070c235..4bb9c1b6 100644 --- a/evm/src/cpu/kernel/asm/ripemd/memory.asm +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -72,7 +72,7 @@ store_input: %add_const(1) SWAP3 // stack: rem-1, ADDR+1, length - DUP2 + DUP1 %jumpi(store_input) // stack: 0 , ADDR , length %pop4 @@ -103,7 +103,7 @@ global buffer_update: // stack: get , set , 0 , retdest %pop3 JUMP - + %macro store_zeros(N, label) // stack: i diff --git a/evm/src/cpu/kernel/asm/ripemd/update.asm b/evm/src/cpu/kernel/asm/ripemd/update.asm index 41fecd40..a0c3ef68 100644 --- a/evm/src/cpu/kernel/asm/ripemd/update.asm +++ b/evm/src/cpu/kernel/asm/ripemd/update.asm @@ -17,7 +17,6 @@ /// /// return state, count + 8*length, buffer - global ripemd_update: // stack: STATE, count, length, virt, retdest %stack (STATE: 5, count, length, virt) -> (count, 8, 64, STATE, count, length, virt) From 681b6e31e79f37ea1c15125b77bb65d515fd905a Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 3 Oct 2022 14:01:47 -0700 Subject: [PATCH 92/97] minor --- evm/src/cpu/kernel/asm/ripemd/functions.asm | 24 ++++++++++----------- evm/src/cpu/kernel/asm/ripemd/memory.asm | 4 ++-- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/evm/src/cpu/kernel/asm/ripemd/functions.asm b/evm/src/cpu/kernel/asm/ripemd/functions.asm index 6cc3db17..ac111215 100644 --- a/evm/src/cpu/kernel/asm/ripemd/functions.asm +++ b/evm/src/cpu/kernel/asm/ripemd/functions.asm @@ -30,26 +30,26 @@ global rol: // acc += (i==rnd)*F // return acc, rnd // -// %this_F(i,F) enacts +// %this_f(i,F) enacts // acc += (i==rnd)*F %macro push_f // stack: rnd PUSH 0 - %this_F(0,F0) - %this_F(1,F1) - %this_F(2,F2) - %this_F(3,F3) - %this_F(4,F4) - %this_F(5,F4) - %this_F(6,F3) - %this_F(7,F2) - %this_F(8,F1) - %this_F(9,F0) + %this_f(0,F0) + %this_f(1,F1) + %this_f(2,F2) + %this_f(3,F3) + %this_f(4,F4) + %this_f(5,F4) + %this_f(6,F3) + %this_f(7,F2) + %this_f(8,F1) + %this_f(9,F0) // stack: F, rnd %endmacro -%macro this_F(i, F) +%macro this_f(i, F) // stack: acc, rnd DUP2 // stack: rnd , acc, rnd diff --git a/evm/src/cpu/kernel/asm/ripemd/memory.asm b/evm/src/cpu/kernel/asm/ripemd/memory.asm index 4bb9c1b6..6c684eb1 100644 --- a/evm/src/cpu/kernel/asm/ripemd/memory.asm +++ b/evm/src/cpu/kernel/asm/ripemd/memory.asm @@ -23,8 +23,8 @@ store_size: %extract_and_store_byte(70) // stack: a %mstore_kernel_general(71) - // stack: 0x80 // padding has 0x80 in first position and zeros elsewhere - %mstore_kernel_general(72) // store first padding term here so as to avoid extra label + // stack: 0x80 // padding has 0x80 in first position and zeros elsewhere + %mstore_kernel_general(72) // store first padding term here so as to avoid extra label %jump(store_padding) store_padding: From 6e5fe43c7e175a47209b5c4d45d439f490fda8c8 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 3 Oct 2022 14:31:56 -0700 Subject: [PATCH 93/97] delete duplicates --- evm/src/cpu/kernel/asm/memory/core.asm | 40 -------------------------- 1 file changed, 40 deletions(-) diff --git a/evm/src/cpu/kernel/asm/memory/core.asm b/evm/src/cpu/kernel/asm/memory/core.asm index 5d7980a5..96caef89 100644 --- a/evm/src/cpu/kernel/asm/memory/core.asm +++ b/evm/src/cpu/kernel/asm/memory/core.asm @@ -257,13 +257,6 @@ // stack: (empty) %endmacro -// Store a single byte to kernel general memory. -%macro mstore_kernel_general - // stack: offset, value - %mstore_kernel(@SEGMENT_KERNEL_GENERAL) - // stack: (empty) -%endmacro - // Store a big-endian u32, consisting of 4 bytes (c_3, c_2, c_1, c_0), // to kernel code. %macro mstore_kernel_code_u32 @@ -299,39 +292,6 @@ // stack: %endmacro -%macro mload_kernel_general - // stack: - %mload_kernel(@SEGMENT_KERNEL_GENERAL) - // stack: byte -%endmacro - -// Load a big-endian u32, consisting of 4 bytes (c_0, c_1, c_2, c_3), -// from kernel general. -%macro mload_kernel_general_u32 - // stack: offset - DUP1 - %mload_kernel_general - // stack: c0 , offset - DUP2 - %add_const(1) - %mload_kernel_general - %shl_const(8) - OR - // stack: c0 | (c1 << 8) , offset - DUP2 - %add_const(2) - %mload_kernel_general - %shl_const(16) - OR - // stack: c0 | (c1 << 8) | (c2 << 16), offset - SWAP1 - %add_const(3) - %mload_kernel_general - %shl_const(24) - OR - // stack: c0 | (c1 << 8) | (c2 << 16) | (c3 << 24) -%endmacro - // set offset i to offset j in kernel general %macro mupdate_kernel_general // stack: j, i From 54885fefa0c795fb6ab0cbddb96c33132b80a4de Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 3 Oct 2022 14:57:04 -0700 Subject: [PATCH 94/97] fix merge problem --- evm/src/cpu/kernel/aggregator.rs | 17 +++---- evm/src/cpu/kernel/asm/memory/core.asm | 48 ++++++++++++++++++- evm/src/cpu/kernel/asm/ripemd/box.asm | 2 +- evm/src/cpu/kernel/asm/ripemd/compression.asm | 1 - 4 files changed, 54 insertions(+), 14 deletions(-) diff --git a/evm/src/cpu/kernel/aggregator.rs b/evm/src/cpu/kernel/aggregator.rs index 18bc5dc6..c1bd0b3d 100644 --- a/evm/src/cpu/kernel/aggregator.rs +++ b/evm/src/cpu/kernel/aggregator.rs @@ -39,16 +39,6 @@ pub(crate) fn combined_kernel() -> Kernel { include_str!("asm/memory/metadata.asm"), include_str!("asm/memory/packing.asm"), include_str!("asm/memory/txn_fields.asm"), - include_str!("asm/ripemd/box.asm"), - include_str!("asm/ripemd/compression.asm"), - include_str!("asm/ripemd/constants.asm"), - include_str!("asm/ripemd/functions.asm"), - include_str!("asm/ripemd/main.asm"), - include_str!("asm/ripemd/memory.asm"), - include_str!("asm/ripemd/update.asm"), - include_str!("asm/rlp/encode.asm"), - include_str!("asm/rlp/decode.asm"), - include_str!("asm/rlp/read_to_memory.asm"), include_str!("asm/mpt/hash.asm"), include_str!("asm/mpt/hash_trie_specific.asm"), include_str!("asm/mpt/hex_prefix.asm"), @@ -58,6 +48,13 @@ pub(crate) fn combined_kernel() -> Kernel { include_str!("asm/mpt/storage_write.asm"), include_str!("asm/mpt/util.asm"), include_str!("asm/mpt/write.asm"), + include_str!("asm/ripemd/box.asm"), + include_str!("asm/ripemd/compression.asm"), + include_str!("asm/ripemd/constants.asm"), + include_str!("asm/ripemd/functions.asm"), + include_str!("asm/ripemd/main.asm"), + include_str!("asm/ripemd/memory.asm"), + include_str!("asm/ripemd/update.asm"), include_str!("asm/rlp/encode.asm"), include_str!("asm/rlp/decode.asm"), include_str!("asm/rlp/read_to_memory.asm"), diff --git a/evm/src/cpu/kernel/asm/memory/core.asm b/evm/src/cpu/kernel/asm/memory/core.asm index 96caef89..7ffd904a 100644 --- a/evm/src/cpu/kernel/asm/memory/core.asm +++ b/evm/src/cpu/kernel/asm/memory/core.asm @@ -55,8 +55,7 @@ // stack: (empty) %endmacro -// Load a big-endian u32, consisting of 4 bytes (c_3, c_2, c_1, c_0), -// from the kernel. +// Load from the kernel a big-endian u32, consisting of 4 bytes (c_3, c_2, c_1, c_0) %macro mload_kernel_u32(segment) // stack: offset DUP1 @@ -85,6 +84,32 @@ // stack: (((((c_3 << 8) | c_2) << 8) | c_1) << 8) | c_0 %endmacro +// Load from the kernel a little-endian u32, consisting of 4 bytes (c_0, c_1, c_2, c_3) +%macro mload_kernel_u32_LE(segment) + // stack: offset + DUP1 + %mload_kernel($segment) + // stack: c0 , offset + DUP2 + %add_const(1) + %mload_kernel($segment) + %shl_const(8) + OR + // stack: c0 | (c1 << 8) , offset + DUP2 + %add_const(2) + %mload_kernel($segment) + %shl_const(16) + OR + // stack: c0 | (c1 << 8) | (c2 << 16), offset + SWAP1 + %add_const(3) + %mload_kernel($segment) + %shl_const(24) + OR + // stack: c0 | (c1 << 8) | (c2 << 16) | (c3 << 24) +%endmacro + // Load a u256 (big-endian) from the kernel. %macro mload_kernel_u256(segment) // stack: offset @@ -228,6 +253,17 @@ // stack: value %endmacro +%macro mload_kernel_code_u32(label) + // stack: u32_shift + %mul_const(4) + // stack: byte_shift + PUSH $label + ADD + // stack: offset + %mload_kernel_u32(@SEGMENT_CODE) + // stack: value +%endmacro + // Load a big-endian u32, consisting of 4 bytes (c_3, c_2, c_1, c_0), // from kernel general memory. %macro mload_kernel_general_u32 @@ -236,6 +272,14 @@ // stack: value %endmacro +// Load a little-endian u32, consisting of 4 bytes (c_3, c_2, c_1, c_0), +// from kernel general memory. +%macro mload_kernel_general_u32_LE + // stack: offset + %mload_kernel_u32_LE(@SEGMENT_KERNEL_GENERAL) + // stack: value +%endmacro + // Load a u256 (big-endian) from kernel code. %macro mload_kernel_code_u256 // stack: offset diff --git a/evm/src/cpu/kernel/asm/ripemd/box.asm b/evm/src/cpu/kernel/asm/ripemd/box.asm index 5894f219..d60d9b8c 100644 --- a/evm/src/cpu/kernel/asm/ripemd/box.asm +++ b/evm/src/cpu/kernel/asm/ripemd/box.asm @@ -35,7 +35,7 @@ pre_rol: %mload_kernel_code(r_data) ADD // stack: virt + r, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt - %mload_kernel_general_u32 + %mload_kernel_general_u32_LE // stack: x, box, a, b, c, d, e, F, K, boxes, rounds, sides, virt SWAP1 SWAP2 diff --git a/evm/src/cpu/kernel/asm/ripemd/compression.asm b/evm/src/cpu/kernel/asm/ripemd/compression.asm index b0cd7b7d..a83bf832 100644 --- a/evm/src/cpu/kernel/asm/ripemd/compression.asm +++ b/evm/src/cpu/kernel/asm/ripemd/compression.asm @@ -135,7 +135,6 @@ update_round_vars: // stack: rnd, rnd, STATE, F', K , 16, rounds, sides, virt, retdest SWAP8 POP - %mul_const(4) %mload_kernel_code_u32(k_data) SWAP7 POP From 1475cddb3db522ee6dc178e12b414f62a2ec2093 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 3 Oct 2022 15:01:59 -0700 Subject: [PATCH 95/97] rearrange --- evm/src/cpu/kernel/asm/memory/core.asm | 28 +++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/evm/src/cpu/kernel/asm/memory/core.asm b/evm/src/cpu/kernel/asm/memory/core.asm index 7ffd904a..2ec27d0e 100644 --- a/evm/src/cpu/kernel/asm/memory/core.asm +++ b/evm/src/cpu/kernel/asm/memory/core.asm @@ -238,13 +238,6 @@ // stack: byte %endmacro -// Load a single byte from kernel general memory. -%macro mload_kernel_general - // stack: offset - %mload_kernel(@SEGMENT_KERNEL_GENERAL) - // stack: value -%endmacro - // Load a big-endian u32, consisting of 4 bytes (c_3, c_2, c_1, c_0), // from kernel code. %macro mload_kernel_code_u32 @@ -264,6 +257,13 @@ // stack: value %endmacro +// Load a single byte from kernel general memory. +%macro mload_kernel_general + // stack: offset + %mload_kernel(@SEGMENT_KERNEL_GENERAL) + // stack: value +%endmacro + // Load a big-endian u32, consisting of 4 bytes (c_3, c_2, c_1, c_0), // from kernel general memory. %macro mload_kernel_general_u32 @@ -308,13 +308,6 @@ %mstore_kernel_u32(@SEGMENT_CODE) %endmacro -// Store a big-endian u32, consisting of 4 bytes (c_3, c_2, c_1, c_0), -// to kernel general memory. -%macro mstore_kernel_general_u32 - // stack: offset, value - %mstore_kernel_u32(@SEGMENT_KERNEL_GENERAL) -%endmacro - // Store a single byte to @SEGMENT_RLP_RAW. %macro mstore_rlp // stack: offset, value @@ -336,6 +329,13 @@ // stack: %endmacro +// Store a big-endian u32, consisting of 4 bytes (c_3, c_2, c_1, c_0), +// to kernel general memory. +%macro mstore_kernel_general_u32 + // stack: offset, value + %mstore_kernel_u32(@SEGMENT_KERNEL_GENERAL) +%endmacro + // set offset i to offset j in kernel general %macro mupdate_kernel_general // stack: j, i From 53014b732f1fc00bd73658e492bcba0cac86ab57 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 3 Oct 2022 15:30:17 -0700 Subject: [PATCH 96/97] almost done --- evm/Cargo.toml | 1 + evm/src/cpu/kernel/asm/ripemd/main.asm | 2 +- evm/src/cpu/kernel/tests/hash.rs | 15 ++++++++++++++- evm/src/cpu/kernel/tests/ripemd.rs | 23 +++++++++++++---------- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/evm/Cargo.toml b/evm/Cargo.toml index 5ee3b1ff..24e5c661 100644 --- a/evm/Cargo.toml +++ b/evm/Cargo.toml @@ -22,6 +22,7 @@ pest = "2.1.3" pest_derive = "2.1.0" rand = "0.8.5" rand_chacha = "0.3.1" +ripemd = "0.1.3" rlp = "0.5.1" serde = { version = "1.0.144", features = ["derive"] } sha2 = "0.10.2" diff --git a/evm/src/cpu/kernel/asm/ripemd/main.asm b/evm/src/cpu/kernel/asm/ripemd/main.asm index 66c409be..f9b28c94 100644 --- a/evm/src/cpu/kernel/asm/ripemd/main.asm +++ b/evm/src/cpu/kernel/asm/ripemd/main.asm @@ -82,7 +82,7 @@ global process: %reverse_bytes_u32 OR // stack: e' d' c' b' a', VARS - %stack (result, VARS: 3) -> (0xdeadbeef, result) + %stack (result, VARS: 3, retdest) -> (retdest, result) // stack: 0xdeadbeef, result JUMP diff --git a/evm/src/cpu/kernel/tests/hash.rs b/evm/src/cpu/kernel/tests/hash.rs index 3acdce2b..67ba64b2 100644 --- a/evm/src/cpu/kernel/tests/hash.rs +++ b/evm/src/cpu/kernel/tests/hash.rs @@ -3,7 +3,8 @@ use std::str::FromStr; use anyhow::Result; use ethereum_types::U256; use rand::{thread_rng, Rng}; -use sha2::{Digest, Sha256}; +use ripemd::{Digest, Ripemd160}; +use sha2::Sha256; use crate::cpu::kernel::aggregator::combined_kernel; use crate::cpu::kernel::interpreter::run_with_kernel; @@ -15,6 +16,13 @@ fn sha2(input: Vec) -> U256 { U256::from(&hasher.finalize()[..]) } +/// Standard RipeMD implementation. +fn ripemd(input: Vec) -> U256 { + let mut hasher = Ripemd160::new(); + hasher.update(input); + U256::from(&hasher.finalize()[..]) +} + fn test_hash(hash_fn_label: &str, standard_implementation: &dyn Fn(Vec) -> U256) -> Result<()> { let kernel = combined_kernel(); let mut rng = thread_rng(); @@ -48,3 +56,8 @@ fn test_hash(hash_fn_label: &str, standard_implementation: &dyn Fn(Vec) -> U fn test_sha2() -> Result<()> { test_hash("sha2", &sha2) } + +#[test] +fn test_ripemd() -> Result<()> { + test_hash("ripemd_stack", &ripemd) +} diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index de8ebfb0..d0240cf2 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -1,21 +1,24 @@ use anyhow::Result; use ethereum_types::U256; +use itertools::Itertools; use crate::cpu::kernel::aggregator::combined_kernel; use crate::cpu::kernel::interpreter::run_with_kernel; -fn make_input(word: &str) -> Vec { - let mut bytes: Vec = vec![word.len().try_into().unwrap()]; - bytes.append(&mut word.as_bytes().to_vec()); - bytes +fn make_input(word: &str) -> Vec { + let mut input: Vec = vec![word.len().try_into().unwrap()]; + input.append(&mut word.as_bytes().iter().map(|&x| x as u32).collect_vec()); + input.push(u32::from_str_radix("deadbeef", 16).unwrap()); + dbg!(input.clone()); + input } -#[test] -fn test_ripemd() -> Result<()> { +// #[test] +fn test_ripemd_reference() -> Result<()> { let reference = vec![ - ("", "0x9c1185a5c5e9fc54612808977ee8f548b2258d31"), - ("a", "0x0bdc9d2d256b3ee9daae347be6f4dc835a467ffe"), - ("abc", "0x8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"), + // ("", "0x9c1185a5c5e9fc54612808977ee8f548b2258d31"), + // ("a", "0x0bdc9d2d256b3ee9daae347be6f4dc835a467ffe"), + // ("abc", "0x8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"), ( "message digest", "0x5d0689ef49d2fae572b881b123a85ffa21595f36", @@ -39,7 +42,7 @@ fn test_ripemd() -> Result<()> { ]; for (x, y) in reference { - let input: Vec = make_input(x); + let input: Vec = make_input(x); let expected = U256::from(y); let kernel = combined_kernel(); From 95128cbbf57c67755a7b3b4f80ea2abbb77a10a3 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 3 Oct 2022 15:32:47 -0700 Subject: [PATCH 97/97] done --- evm/src/cpu/kernel/tests/ripemd.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/evm/src/cpu/kernel/tests/ripemd.rs b/evm/src/cpu/kernel/tests/ripemd.rs index d0240cf2..6123c336 100644 --- a/evm/src/cpu/kernel/tests/ripemd.rs +++ b/evm/src/cpu/kernel/tests/ripemd.rs @@ -9,16 +9,15 @@ fn make_input(word: &str) -> Vec { let mut input: Vec = vec![word.len().try_into().unwrap()]; input.append(&mut word.as_bytes().iter().map(|&x| x as u32).collect_vec()); input.push(u32::from_str_radix("deadbeef", 16).unwrap()); - dbg!(input.clone()); input } -// #[test] +#[test] fn test_ripemd_reference() -> Result<()> { let reference = vec![ - // ("", "0x9c1185a5c5e9fc54612808977ee8f548b2258d31"), - // ("a", "0x0bdc9d2d256b3ee9daae347be6f4dc835a467ffe"), - // ("abc", "0x8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"), + ("", "0x9c1185a5c5e9fc54612808977ee8f548b2258d31"), + ("a", "0x0bdc9d2d256b3ee9daae347be6f4dc835a467ffe"), + ("abc", "0x8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"), ( "message digest", "0x5d0689ef49d2fae572b881b123a85ffa21595f36", @@ -47,15 +46,12 @@ fn test_ripemd_reference() -> Result<()> { let kernel = combined_kernel(); let initial_offset = kernel.global_labels["ripemd_stack"]; - let initial_stack: Vec = input.iter().map(|&x| U256::from(x as u8)).rev().collect(); + let initial_stack: Vec = input.iter().map(|&x| U256::from(x as u32)).rev().collect(); let final_stack: Vec = run_with_kernel(&kernel, initial_offset, initial_stack)? .stack() .to_vec(); let actual = final_stack[0]; assert_eq!(actual, expected); - - // let read_out: Vec = final_stack.iter().map(|x| format!("{:x}", x)).rev().collect(); - // println!("{:x?}", read_out); } Ok(()) }