Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into feat/cancun
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashtare committed May 20, 2024
2 parents afb9633 + bde253b commit c610b93
Show file tree
Hide file tree
Showing 13 changed files with 265 additions and 171 deletions.
4 changes: 2 additions & 2 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
* @muursh @Nashtare
/evm_arithmetization/ @wborgeaud @muursh @Nashtare
* @muursh @Nashtare @cpubot
/evm_arithmetization/ @wborgeaud @muursh @Nashtare @cpubot
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Changed
- Add a few QoL useability functions to the interface ([#169](https://github.com/0xPolygonZero/zk_evm/pull/169))

## [0.3.1] - 2024-04-22

Expand Down
44 changes: 26 additions & 18 deletions evm_arithmetization/src/cpu/kernel/asm/core/access_lists.asm
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,22 @@ global init_access_lists:
POP
%endmacro

// Multiply the ptr at the top of the stack by 2
// and abort if 2*ptr - @SEGMENT_ACCESSED_ADDRESSES >= @GLOBAL_METADATA_ACCESSED_ADDRESSES_LEN
// In this way ptr must be pointing to the begining of a node.
// Multiply the value at the top of the stack, denoted by ptr/2, by 2
// and abort if ptr/2 >= mem[@GLOBAL_METADATA_ACCESSED_ADDRESSES_LEN]/2
// In this way 2*ptr/2 must be pointing to the begining of a node.
%macro get_valid_addr_ptr
// stack: ptr
// stack: ptr/2
DUP1
// stack: ptr/2, ptr/2
%mload_global_metadata(@GLOBAL_METADATA_ACCESSED_ADDRESSES_LEN)
// @GLOBAL_METADATA_ACCESSED_ADDRESSES_LEN must be an even number because
// both @SEGMENT_ACCESSED_ADDRESSES and the unscaled access addresses list len
// must be even numbers
%div_const(2)
// stack: scaled_len/2, ptr/2, ptr/2
%assert_gt
%mul_const(2)
PUSH @SEGMENT_ACCESSED_ADDRESSES
DUP2
SUB
%assert_lt_const(@GLOBAL_METADATA_ACCESSED_ADDRESSES_LEN)
// stack: 2*ptr
// stack: ptr
%endmacro


Expand Down Expand Up @@ -205,17 +210,20 @@ global remove_accessed_addresses:
// stack: cold_access, value_ptr
%endmacro

// Multiply the ptr at the top of the stack by 4
// and abort if 4*ptr - SEGMENT_ACCESSED_STORAGE_KEYS >= @GLOBAL_METADATA_ACCESSED_STORAGE_KEYS_LEN
// In this way ptr must be pointing to the beginning of a node.
// Multiply the ptr at the top of the stack, denoted by ptr/4, by 4
// and abort if ptr/4 >= @GLOBAL_METADATA_ACCESSED_STORAGE_KEYS_LEN/4
// In this way 4*ptr/4 be pointing to the beginning of a node.
%macro get_valid_storage_ptr
// stack: ptr
// stack: ptr/4
DUP1
%mload_global_metadata(@GLOBAL_METADATA_ACCESSED_STORAGE_KEYS_LEN)
// By construction, both @SEGMENT_ACCESSED_STORAGE_KEYS and the unscaled list len
// must be multiples of 4
%div_const(4)
// stack: scaled_len/4, ptr/4, ptr/4
%assert_gt
%mul_const(4)
PUSH @SEGMENT_ACCESSED_STORAGE_KEYS
DUP2
SUB
%assert_lt_const(@GLOBAL_METADATA_ACCESSED_STORAGE_KEYS_LEN)
// stack: 2*ptr
// stack: ptr
%endmacro

/// Inserts the storage key into the access list if it is not already present.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ compression_loop:
%mload_kernel_code_u32
// stack: K[i], W[i], a[i], b[i], c[i], d[i], e[i], f[i], g[i], h[i], num_blocks, scratch_space_addr, message_schedule_addr, i, a[0]..h[0], retdest
DUP10
DUP10
DUP10
DUP10
// stack: e[i], f[i], g[i], h[i], K[i], W[i], a[i], b[i], c[i], d[i], e[i], f[i], g[i], h[i], num_blocks, scratch_space_addr, message_schedule_addr, i, a[0]..h[0], retdest
DUP8
DUP11
DUP11
DUP11
// stack: e[i], f[i], g[i], e[i], h[i], K[i], W[i], a[i], b[i], c[i], d[i], e[i], f[i], g[i], h[i], num_blocks, scratch_space_addr, message_schedule_addr, i, a[0]..h[0], retdest
%sha2_temp_word1
// stack: T1[i], a[i], b[i], c[i], d[i], e[i], f[i], g[i], h[i], num_blocks, scratch_space_addr, message_schedule_addr, i, a[0]..h[0], retdest
DUP4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ gen_message_schedule_from_block:
// stack: block_addr, output_addr, retdest
DUP1
// stack: block_addr, block_addr, output_addr, retdest
%add_const(32)
// stack: block_addr + 32, block_addr, output_addr, retdest
SWAP1
// stack: block_addr, block_addr + 32, output_addr, retdest
%mload_u256
// stack: block[0], block_addr + 32, output_addr, retdest
// stack: block[0], block_addr, output_addr, retdest
SWAP1
// stack: block_addr, block[0], output_addr, retdest
%add_const(32)
// stack: block_addr + 32, block[0], output_addr, retdest
%mload_u256
// stack: block[1], block[0], output_addr, retdest
Expand All @@ -45,27 +43,22 @@ gen_message_schedule_from_block_0_loop:
// stack: output_addr, block[0] % (1 << 32), block[0] >> 32, output_addr, counter, block[1], retdest
%mstore_u32
// stack: block[0] >> 32, output_addr, counter, block[1], retdest
SWAP1
// stack: output_addr, block[0] >> 32, counter, block[1], retdest
%sub_const(4)
// stack: output_addr - 4, block[0] >> 32, counter, block[1], retdest
SWAP1
// stack: block[0] >> 32, output_addr - 4, counter, block[1], retdest
%stack (block0_shifted, output_addr, counter) -> (output_addr, 4, 1, counter, block0_shifted)
SUB
// stack: output_addr - 4, 1, counter, block[0] >> 32, block[1], retdest
SWAP2
// stack: counter, output_addr - 4, block[0] >> 32, block[1], retdest
%decrement
SUB
// stack: counter - 1, output_addr - 4, block[0] >> 32, block[1], retdest
DUP1
%jumpi(gen_message_schedule_from_block_0_loop)
gen_message_schedule_from_block_0_end:
// stack: old counter=0, output_addr, block[0], block[1], retdest
POP
// stack: output_addr, block[0], block[1], retdest
%stack (out, b0, b1) -> (out, 8, b1, b0)
// stack: output_addr, counter=8, block[1], block[0], retdest
%add_const(64)
// stack: output_addr + 64, counter, block[1], block[0], retdest
SWAP1
// stack: counter, output_addr + 64, block[1], block[0], retdest
// stack: output_addr + 64, block[0], block[1], retdest
%stack (out, b0, b1) -> (8, out, b1, b0)
// stack: counter=8, output_addr + 64, block[1], block[0], retdest
gen_message_schedule_from_block_1_loop:
// Split the second half (256 bits) of the block into the next eight (32-bit) chunks of the message sdchedule.
// stack: counter, output_addr, block[1], block[0], retdest
Expand All @@ -83,29 +76,22 @@ gen_message_schedule_from_block_1_loop:
// stack: output_addr, block[1] % (1 << 32), block[1] >> 32, output_addr, counter, block[0], retdest
%mstore_u32
// stack: block[1] >> 32, output_addr, counter, block[0], retdest
SWAP1
// stack: output_addr, block[1] >> 32, counter, block[0], retdest
%sub_const(4)
// stack: output_addr - 4, block[1] >> 32, counter, block[0], retdest
SWAP1
// stack: block[1] >> 32, output_addr - 4, counter, block[0], retdest
%stack (block1_shifted, output_addr, counter) -> (output_addr, 4, 1, counter, block1_shifted)
SUB
// stack: output_addr - 4, 1, counter, block[1] >> 32, block[0], retdest
SWAP2
// stack: counter, output_addr - 4, block[1] >> 32, block[0], retdest
%decrement
SUB
// stack: counter - 1, output_addr - 4, block[1] >> 32, block[0], retdest
DUP1
%jumpi(gen_message_schedule_from_block_1_loop)
gen_message_schedule_from_block_1_end:
// stack: old counter=0, output_addr, block[1], block[0], retdest
POP
// stack: output_addr, block[0], block[1], retdest
PUSH 48
// stack: counter=48, output_addr, block[0], block[1], retdest
SWAP1
// stack: output_addr, counter, block[0], block[1], retdest
%add_const(36)
// stack: output_addr + 36, counter, block[0], block[1], retdest
SWAP1
// stack: counter, output_addr + 36, block[0], block[1], retdest
// stack: output_addr + 36, block[0], block[1], retdest
PUSH 48
// stack: counter=48, output_addr + 36, block[0], block[1], retdest
gen_message_schedule_remaining_loop:
// Generate the next 48 chunks of the message schedule, one at a time, from prior chunks.
// stack: counter, output_addr, block[0], block[1], retdest
Expand Down Expand Up @@ -153,9 +139,10 @@ gen_message_schedule_remaining_loop:
// stack: output_addr, x[output_addr - 16*4], sigma_0(x[output_addr - 15*4]), x[output_addr - 7*4], sigma_1(x[output_addr - 2*4]), counter, block[0], block[1], retdest
SWAP4
// stack: sigma_1(x[output_addr - 2*4]), x[output_addr - 16*4], sigma_0(x[output_addr - 15*4]), x[output_addr - 7*4], output_addr, counter, block[0], block[1], retdest
%add_u32
%add_u32
%add_u32
ADD
ADD
ADD
%as_u32
// stack: sigma_1(x[output_addr - 2*4]) + x[output_addr - 16*4] + sigma_0(x[output_addr - 15*4]) + x[output_addr - 7*4], output_addr, counter, block[0], block[1], retdest
DUP2
// stack: output_addr, sigma_1(x[output_addr - 2*4]) + x[output_addr - 16*4] + sigma_0(x[output_addr - 15*4]) + x[output_addr - 7*4], output_addr, counter, block[0], block[1], retdest
Expand All @@ -174,20 +161,22 @@ gen_message_schedule_remaining_end:
%pop4
JUMP

// Precodition: memory, starting at 0, contains num_blocks, block0[0], ..., block0[63], block1[0], ..., blocklast[63]
// Precondition: memory, starting at 0, contains num_blocks, block0[0], ..., block0[63], block1[0], ..., blocklast[63]
// stack contains output_addr
// Postcondition: starting at output_addr, set of 256 bytes per block
// each contains the 64 32-bit chunks of the message schedule for that block (in four-byte increments)
global sha2_gen_all_message_schedules:
// stack: output_addr, retdest
DUP1
// stack: output_addr, output_addr, retdest
%mload_current_general_no_offset
// stack: num_blocks, output_addr, output_addr, retdest
PUSH 1
// stack: cur_offset = 1, counter = num_blocks, output_addr, output_addr, retdest
%build_current_general_address
// stack: cur_addr, counter, output_addr, output_addr, retdest
%build_current_general_address_no_offset
DUP1
// stack: base_addr, base_addr, output_addr, output_addr, retdest
MLOAD_GENERAL
// stack: num_blocks, base_addr, output_addr, output_addr, retdest
SWAP1
%increment
// stack: cur_addr (offset = 1), counter = num_blocks, output_addr, output_addr, retdest
gen_all_message_schedules_loop:
// stack: cur_addr, counter, cur_output_addr, output_addr, retdest
PUSH gen_all_message_schedules_loop_end
Expand Down
Loading

0 comments on commit c610b93

Please sign in to comment.