Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Improve some calls to %mstore_rlp (0xPolygonZero#1452)
Browse files Browse the repository at this point in the history
* Improve some calls to mstore_rlp

* Remove comment
  • Loading branch information
Nashtare authored Jan 9, 2024
1 parent 95c83ad commit 54a1313
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 57 deletions.
3 changes: 1 addition & 2 deletions evm/src/cpu/kernel/asm/memory/core.asm
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,7 @@
%mstore_u32
%endmacro

// Store a single byte to @SEGMENT_RLP_RAW.
%macro mstore_rlp
%macro swap_mstore
// stack: addr, value
SWAP1
MSTORE_GENERAL
Expand Down
12 changes: 6 additions & 6 deletions evm/src/cpu/kernel/asm/mpt/hash/hash.asm
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ global encode_node_branch:

// No value; append the empty string (0x80).
// stack: value_ptr, rlp_pos', rlp_start, encode_value, cur_len, retdest
%stack (value_ptr, rlp_pos, rlp_start, encode_value) -> (rlp_pos, 0x80, rlp_pos, rlp_start)
%mstore_rlp
%stack (value_ptr, rlp_pos, rlp_start, encode_value) -> (0x80, rlp_pos, rlp_pos, rlp_start)
MSTORE_GENERAL
// stack: rlp_pos', rlp_start, cur_len, retdest
%increment
// stack: rlp_pos'', rlp_start, cur_len, retdest
Expand Down Expand Up @@ -192,9 +192,9 @@ encode_node_branch_prepend_prefix:
SWAP1 DUP1 %sub_const(32) %jumpi(%%unpack)
// Otherwise, result is a hash, and we need to add the prefix 0x80 + 32 = 160.
// stack: result_len, result, cur_len, rlp_pos, rlp_start, node_payload_ptr, encode_value, retdest
DUP4 // rlp_pos
PUSH 160
DUP5 // rlp_pos
%mstore_rlp
MSTORE_GENERAL
SWAP3 %increment SWAP3 // rlp_pos += 1
%%unpack:
%stack (result_len, result, cur_len, rlp_pos, rlp_start, node_payload_ptr, encode_value, retdest)
Expand Down Expand Up @@ -233,9 +233,9 @@ encode_node_extension_after_hex_prefix:
// If result_len != 32, result is raw RLP, with an appropriate RLP prefix already.
DUP4 %sub_const(32) %jumpi(encode_node_extension_unpack)
// Otherwise, result is a hash, and we need to add the prefix 0x80 + 32 = 160.
DUP1 // rlp_pos
PUSH 160
DUP2 // rlp_pos
%mstore_rlp
MSTORE_GENERAL
%increment // rlp_pos += 1
encode_node_extension_unpack:
%stack (rlp_pos, rlp_start, result, result_len, node_payload_ptr, cur_len)
Expand Down
4 changes: 2 additions & 2 deletions evm/src/cpu/kernel/asm/mpt/hash/hash_trie_specific.asm
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ encode_nonzero_receipt_type:
// stack: rlp_receipt_len, txn_type, rlp_addr, value_ptr, retdest
DUP3 %encode_rlp_multi_byte_string_prefix
// stack: rlp_addr, txn_type, old_rlp_addr, value_ptr, retdest
DUP2 DUP2
%mstore_rlp
DUP1 DUP3
MSTORE_GENERAL
%increment
// stack: rlp_addr, txn_type, old_rlp_addr, value_ptr, retdest
%stack (rlp_addr, txn_type, old_rlp_addr, value_ptr, retdest) -> (rlp_addr, value_ptr, retdest)
Expand Down
12 changes: 6 additions & 6 deletions evm/src/cpu/kernel/asm/mpt/hex_prefix.asm
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ first_byte:
// stack: rlp_addr, num_nibbles, packed_nibbles, terminated, retdest
// get the first nibble, if num_nibbles is odd, or zero otherwise
SWAP2
// stack: packed_nibbles, num_nibbbles, rlp_addr, terminated, retdest
// stack: packed_nibbles, num_nibbles, rlp_addr, terminated, retdest
DUP2 DUP1
%mod_const(2)
// stack: parity, num_nibbles, packed_nibbles, num_nibbles, rlp_addr, terminated, retdest
Expand All @@ -50,7 +50,7 @@ first_byte:
ADD
// stack: first_byte, rlp_addr, retdest
DUP2
%mstore_rlp
%swap_mstore
%increment
// stack: rlp_addr', retdest
SWAP1
Expand Down Expand Up @@ -88,7 +88,7 @@ rlp_header_medium:
// stack: hp_len, rlp_addr, num_nibbles, packed_nibbles, terminated, retdest
%add_const(0x80) // value = 0x80 + hp_len
DUP2
%mstore_rlp
%swap_mstore
// stack: rlp_addr, num_nibbles, packed_nibbles, terminated, retdest
// rlp_addr += 1
%increment
Expand All @@ -108,14 +108,14 @@ rlp_header_large:
// In practice hex-prefix length will never exceed 256, so the length of the
// length will always be 1 byte in this case.

DUP2 // rlp_addr
PUSH 0xb8 // value = 0xb7 + len_of_len = 0xb8
DUP3
%mstore_rlp
MSTORE_GENERAL
// stack: rlp_addr, value, hp_len, i, rlp_addr, num_nibbles, packed_nibbles, terminated, retdest

// stack: hp_len, rlp_addr, num_nibbles, packed_nibbles, terminated, retdest
DUP2 %increment
%mstore_rlp
%swap_mstore

// stack: rlp_addr, num_nibbles, packed_nibbles, terminated, retdest
// rlp_addr += 2
Expand Down
4 changes: 2 additions & 2 deletions evm/src/cpu/kernel/asm/mpt/util.asm
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
%endmacro

%macro initialize_rlp_segment
PUSH 0x80
PUSH @ENCODED_EMPTY_NODE_POS
%mstore_rlp
PUSH 0x80
MSTORE_GENERAL
%endmacro

%macro alloc_rlp_block
Expand Down
72 changes: 33 additions & 39 deletions evm/src/cpu/kernel/asm/rlp/encode.asm
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ global encode_rlp_256:
// RLP-encode a fixed-length string with the given byte length. Assumes string < 2^(8 * len).
global encode_rlp_fixed:
// stack: len, rlp_addr, string, retdest
DUP1
DUP2
DUP2
%add_const(0x80)
// stack: first_byte, len, rlp_addr, string, retdest
DUP3
// stack: rlp_addr, first_byte, len, rlp_addr, string, retdest
%mstore_rlp
// stack: first_byte, rlp_addr, len, rlp_addr, string, retdest
MSTORE_GENERAL
// stack: len, rlp_addr, string, retdest
SWAP1
%increment // increment rlp_addr
Expand All @@ -51,19 +50,17 @@ encode_rlp_fixed_finish:
// I.e. writes encode(encode(string). Assumes string < 2^(8 * len).
global doubly_encode_rlp_fixed:
// stack: len, rlp_addr, string, retdest
DUP1
DUP2
DUP2
%add_const(0x81)
// stack: first_byte, len, rlp_addr, string, retdest
DUP3
// stack: rlp_addr, first_byte, len, rlp_addr, string, retdest
%mstore_rlp
// stack: first_byte, rlp_addr, len, rlp_addr, string, retdest
MSTORE_GENERAL
// stack: len, rlp_addr, string, retdest
DUP1
DUP2 %increment
DUP2
%add_const(0x80)
// stack: second_byte, len, original_rlp_addr, string, retdest
DUP3 %increment
// stack: rlp_addr', second_byte, len, rlp_addr, string, retdest
%mstore_rlp
// stack: second_byte, rlp_addr', len, original_rlp_addr, string, retdest
MSTORE_GENERAL
// stack: len, rlp_addr, string, retdest
SWAP1
%add_const(2) // advance past the two prefix bytes
Expand All @@ -87,11 +84,10 @@ global encode_rlp_multi_byte_string_prefix:
%jumpi(encode_rlp_multi_byte_string_prefix_large)
// Medium case; prefix is 0x80 + str_len.
// stack: rlp_addr, str_len, retdest
SWAP1 %add_const(0x80)
// stack: prefix, rlp_addr, retdest
DUP2
// stack: rlp_addr, prefix, rlp_addr, retdest
%mstore_rlp
DUP1
SWAP2 %add_const(0x80)
// stack: prefix, rlp_addr, rlp_addr, retdest
MSTORE_GENERAL
// stack: rlp_addr, retdest
%increment
// stack: rlp_addr', retdest
Expand All @@ -104,12 +100,11 @@ encode_rlp_multi_byte_string_prefix_large:
%num_bytes
// stack: len_of_len, rlp_addr, str_len, retdest
SWAP1
DUP2 // len_of_len
DUP1 // rlp_addr
DUP3 // len_of_len
%add_const(0xb7)
// stack: first_byte, rlp_addr, len_of_len, str_len, retdest
DUP2
// stack: rlp_addr, first_byte, rlp_addr, len_of_len, str_len, retdest
%mstore_rlp
// stack: first_byte, rlp_addr, rlp_addr, len_of_len, str_len, retdest
MSTORE_GENERAL
// stack: rlp_addr, len_of_len, str_len, retdest
%increment
// stack: rlp_addr', len_of_len, str_len, retdest
Expand All @@ -132,12 +127,11 @@ global encode_rlp_list_prefix:
%jumpi(encode_rlp_list_prefix_large)
// Small case: prefix is just 0xc0 + length.
// stack: rlp_addr, payload_len, retdest
SWAP1
DUP1
SWAP2
%add_const(0xc0)
// stack: prefix, rlp_addr, retdest
DUP2
// stack: rlp_addr, prefix, rlp_addr, retdest
%mstore_rlp
// stack: prefix, rlp_addr, rlp_addr, retdest
MSTORE_GENERAL
// stack: rlp_addr, retdest
%increment
SWAP1
Expand All @@ -147,10 +141,10 @@ encode_rlp_list_prefix_large:
// stack: rlp_addr, payload_len, retdest
DUP2 %num_bytes
// stack: len_of_len, rlp_addr, payload_len, retdest
DUP1 %add_const(0xf7)
// stack: first_byte, len_of_len, rlp_addr, payload_len, retdest
DUP3 // rlp_addr
%mstore_rlp
DUP2
DUP2 %add_const(0xf7)
// stack: first_byte, rlp_addr, len_of_len, rlp_addr, payload_len, retdest
MSTORE_GENERAL
// stack: len_of_len, rlp_addr, payload_len, retdest
SWAP1 %increment
// stack: rlp_addr', len_of_len, payload_len, retdest
Expand Down Expand Up @@ -184,10 +178,10 @@ global prepend_rlp_list_prefix:

// If we got here, we have a small list, so we prepend 0xc0 + len at rlp_address 8.
// stack: payload_len, end_rlp_addr, start_rlp_addr, retdest
DUP1 %add_const(0xc0)
// stack: prefix_byte, payload_len, end_rlp_addr, start_rlp_addr, retdest
DUP4 %decrement // offset of prefix
%mstore_rlp
DUP3 %decrement // offset of prefix
DUP2 %add_const(0xc0)
// stack: prefix_byte, start_rlp_addr-1, payload_len, end_rlp_addr, start_rlp_addr, retdest
MSTORE_GENERAL
// stack: payload_len, end_rlp_addr, start_rlp_addr, retdest
%increment
// stack: rlp_len, end_rlp_addr, start_rlp_addr, retdest
Expand All @@ -207,7 +201,7 @@ prepend_rlp_list_prefix_big:
DUP5 %decrement // start_rlp_addr - 1
SUB
// stack: prefix_start_rlp_addr, len_of_len, payload_len, end_rlp_addr, start_rlp_addr, retdest
DUP2 %add_const(0xf7) DUP2 %mstore_rlp // rlp[prefix_start_rlp_addr] = 0xf7 + len_of_len
DUP2 %add_const(0xf7) DUP2 %swap_mstore // rlp[prefix_start_rlp_addr] = 0xf7 + len_of_len
// stack: prefix_start_rlp_addr, len_of_len, payload_len, end_rlp_addr, start_rlp_addr, retdest
DUP1 %increment // start_len_rlp_addr = prefix_start_rlp_addr + 1
%stack (start_len_rlp_addr, prefix_start_rlp_addr, len_of_len, payload_len, end_rlp_addr, start_rlp_addr, retdest)
Expand Down

0 comments on commit 54a1313

Please sign in to comment.