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)