Skip to content

Commit

Permalink
proof-producer: memory expansion tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oclaw committed Mar 5, 2025
1 parent d2c579d commit e10e0ed
Show file tree
Hide file tree
Showing 55 changed files with 338 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <nil/blueprint/zkevm/zkevm_word.hpp>
#include <nil/blueprint/zkevm_bbf/types/opcode.hpp>
#include <nil/blueprint/zkevm_bbf/opcodes/dummy.hpp>

namespace nil {
namespace blueprint {
namespace bbf{

template<typename FieldType>
using zkevm_address_operation = zkevm_dummy_operation<FieldType>;
} // namespace bbf
} // namespace blueprint
} // namespace nil
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
#include <nil/blueprint/zkevm_bbf/opcodes/gas.hpp>
#include <nil/blueprint/zkevm_bbf/opcodes/pc.hpp>
#include <nil/blueprint/zkevm_bbf/opcodes/msize.hpp>
#include "nil/blueprint/zkevm_bbf/opcodes/logx.hpp"
#include <nil/blueprint/zkevm_bbf/opcodes/logx.hpp>
#include <nil/blueprint/zkevm_bbf/opcodes/address.hpp>

namespace nil {
namespace blueprint {
Expand Down Expand Up @@ -700,13 +701,14 @@ namespace nil {
opcodes[zkevm_opcode::CODECOPY] = std::make_shared<zkevm_codecopy_operation<BlueprintFieldType>>();
opcodes[zkevm_opcode::MCOPY] = std::make_shared<zkevm_mcopy_operation<BlueprintFieldType>>();
opcodes[zkevm_opcode::RETURNDATACOPY] = std::make_shared<zkevm_returndatacopy_operation<BlueprintFieldType>>();

// not implemented yet opcodes

opcodes[zkevm_opcode::RETURNDATASIZE] = std::make_shared<zkevm_returndatasize_operation<BlueprintFieldType>>();
opcodes[zkevm_opcode::CALL] = std::make_shared<zkevm_call_operation<BlueprintFieldType>>();
opcodes[zkevm_opcode::GAS] = std::make_shared<zkevm_gas_operation<BlueprintFieldType>>();
opcodes[zkevm_opcode::STATICCALL] = std::make_shared<zkevm_staticcall_operation<BlueprintFieldType>>();
opcodes[zkevm_opcode::ADDRESS] = std::make_shared<zkevm_address_operation<BlueprintFieldType>>();

// // DUP
opcodes[zkevm_opcode::DUP1] = std::make_shared<zkevm_dupx_operation<BlueprintFieldType>>(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace nil {

const std::size_t max_copy_rows = 30000;
const std::size_t max_rw_rows = 60000;
const std::size_t max_keccak_blocks = 100;
const std::size_t max_keccak_blocks = 25;
const std::size_t max_bytecode_rows = 20000;
const std::size_t max_total_rows = 150000;
const std::size_t max_mpt_rows = 30;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,156 @@ contract Uint256CornerCaseTests {
}
}
}

contract MemoryGasTest {
/**
* @dev Internal helper to expand memory by `size` bytes.
* Forces memory allocation by storing a word at the end.
*/
function _expandMemory(uint256 size) internal pure {
assembly {
// Current free memory pointer (https://docs.soliditylang.org/en/latest/internals/layout_in_memory.html)
let ptr := mload(0x40)

// Move it `size` bytes forward
let newPtr := add(ptr, size)

// Store something at newPtr to force allocation
mstore(newPtr, 0)

// Update the free memory pointer
mstore(0x40, add(newPtr, 0x20))
}
}

/**
* @notice Expands memory, then copies call data into a new bytes array.
*/
function testCalldatacopy(
uint256 memSize,
uint256 start,
uint256 len
) external pure returns (bytes memory) {
_expandMemory(memSize);

bytes memory result = new bytes(len);
assembly {
let dest := add(result, 0x20)
calldatacopy(dest, start, len)
}
return result;
}

/**
* @notice Expands memory, then copies code from this contract’s bytecode.
*/
function testCodecopy(
uint256 memSize,
uint256 codeOffset,
uint256 len
) external pure returns (bytes memory) {
_expandMemory(memSize);

bytes memory result = new bytes(len);
assembly {
let dest := add(result, 0x20)
codecopy(dest, codeOffset, len)
}
return result;
}

/**
* @notice Expands memory, then demonstrates a naive memory-to-memory copy in assembly.
*/
function testMemCopy(
uint256 memSize,
uint256 src,
uint256 len
) external pure returns (bytes memory) {
_expandMemory(memSize);

// We'll create a new buffer for demonstration
bytes memory result = new bytes(len);
assembly {
mcopy(result, src, len)
}
return result;
}


function _returnSomeData() external pure returns (bytes memory) {
return "Hello, I am your return data!";
}

function testReturndatacopy(
uint256 memSize,
uint256 offset,
uint256 len
) external returns (bytes memory) {
_expandMemory(memSize);

// Call this contract’s own _returnSomeData()
// by encoding its selector and arguments
(bool success, ) = address(this).call(
abi.encodeWithSelector(this._returnSomeData.selector)
);

require(success, "Sub-call failed.");

// Copy from the return data buffer into new memory
bytes memory ret = new bytes(len);
assembly {
let dest := add(ret, 0x20)
returndatacopy(dest, offset, len)
}

// 3) Return the copied slice
return ret;
}

function testMload(
uint256 memSize,
uint256 slot
) external pure returns (uint256 val) {
_expandMemory(memSize);

assembly {
val := mload(slot)
}
}

/**
* @notice Expands memory, then demonstrates mstore by writing and reading at `slot`.
*/
function testMstore(
uint256 memSize,
uint256 slot,
uint256 value
) external pure returns (uint256) {
_expandMemory(memSize);

uint256 val;
assembly {
mstore(slot, value)
val := mload(slot)
}
return val;
}

/**
* @notice Expands memory, then demonstrates mstore8 by writing a single byte at `slot`.
*/
function testMstore8(
uint256 memSize,
uint256 slot,
uint8 value
) external pure returns (uint8 storedByte) {
_expandMemory(memSize);

assembly {
mstore8(slot, value)
// mload returns 32 bytes; isolate the lowest-order byte
storedByte := byte(0, mload(slot))
}
}
}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��˼�ᕓE@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��˼�ᕓE@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��˼�ᕓE"@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�ϫ��c@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�ϫ��c@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�ϫ��c"@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��������@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��������@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��������"@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�����ŗ�"@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�����ŗ�"@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�����ŗ�""@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ʖ�Ԟ���B@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ʖ�Ԟ���B@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ʖ�Ԟ���B"@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��Ȇ����H@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��Ȇ����H@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��Ȇ����H"@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�����‰Q@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�����‰Q@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�����‰Q"@a7854f32091eca8f7c93df94688ae61f24f16d1c8c695dcd3315b9d3cad12ad9
Binary file not shown.
Binary file not shown.
80 changes: 80 additions & 0 deletions proof-producer/tests/bin/proof-producer/resources/traces/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,86 @@ nild run --http-port 8529 # should be run in another terminal (or with &) and st
prover trace corner_cases/division_by_zero/div_by_zero 1 $block_hash
```


### Memory expansion tests

#### CALLDATACOPY
```bash
solc -o . --bin --abi contracts/tracer_data.sol --overwrite --no-cbor-metadata --metadata-hash none
nil_block_generator init
nil_block_generator add-contract --contract-name MemoryGasTest --contract-path MemoryGasTest
nil_block_generator call-contract --contract-name MemoryGasTest --method testCalldatacopy --count 1 --args "600 0 32"
nil_block_generator get-block
nild run --http-port 8529 # should be run in another terminal (or with &) and stopped after collecting the traces with prover
prover trace memory_expansion/calldatacopy/mem_expand_calldatacopy 1 $block_hash
```

#### CODECOPY
```bash
solc -o . --bin --abi contracts/tracer_data.sol --overwrite --no-cbor-metadata --metadata-hash none
nil_block_generator init
nil_block_generator add-contract --contract-name MemoryGasTest --contract-path MemoryGasTest
nil_block_generator call-contract --contract-name MemoryGasTest --method testCodecopy --count 1 --args "700 0 32"
nil_block_generator get-block
nild run --http-port 8529 # should be run in another terminal (or with &) and stopped after collecting the traces with prover
prover trace memory_expansion/codecopy/mem_expand_codecopy 1 $block_hash
```

#### MLOAD
```bash
solc -o . --bin --abi contracts/tracer_data.sol --overwrite --no-cbor-metadata --metadata-hash none
nil_block_generator init
nil_block_generator add-contract --contract-name MemoryGasTest --contract-path MemoryGasTest
nil_block_generator call-contract --contract-name MemoryGasTest --method testMload --count 1 --args "1000 128"
nil_block_generator get-block
nild run --http-port 8529 # should be run in another terminal (or with &) and stopped after collecting the traces with prover
prover trace memory_expansion/mload/mem_expand_mload 1 $block_hash
```

#### MSTORE
```bash
solc -o . --bin --abi contracts/tracer_data.sol --overwrite --no-cbor-metadata --metadata-hash none
nil_block_generator init
nil_block_generator add-contract --contract-name MemoryGasTest --contract-path MemoryGasTest
nil_block_generator call-contract --contract-name MemoryGasTest --method testMstore --count 1 --args "1100 160 999"
nil_block_generator get-block
nild run --http-port 8529 # should be run in another terminal (or with &) and stopped after collecting the traces with prover
prover trace memory_expansion/mstore/mem_expand_mstore 1 $block_hash
```

#### MSTORE8
```bash
solc -o . --bin --abi contracts/tracer_data.sol --overwrite --no-cbor-metadata --metadata-hash none
nil_block_generator init
nil_block_generator add-contract --contract-name MemoryGasTest --contract-path MemoryGasTest
nil_block_generator call-contract --contract-name MemoryGasTest --method testMstore8 --count 1 --args "1200 192 255"
nil_block_generator get-block
nild run --http-port 8529 # should be run in another terminal (or with &) and stopped after collecting the traces with prover
prover trace memory_expansion/mstore8/mem_expand_mstore8 1 $block_hash
```

#### RETURNDATACOPY
```bash
solc -o . --bin --abi contracts/tracer_data.sol --overwrite --no-cbor-metadata --metadata-hash none
nil_block_generator init
nil_block_generator add-contract --contract-name MemoryGasTest --contract-path MemoryGasTest
nil_block_generator call-contract --contract-name MemoryGasTest --method testReturndatacopy --count 1 --args "900 0 32"
nil_block_generator get-block
nild run --http-port 8529 # should be run in another terminal (or with &) and stopped after collecting the traces with prover
prover trace memory_expansion/returndatacopy/mem_expand_returndatacopy 1 $block_hash
```

#### MCOPY
```bash
solc -o . --bin --abi contracts/tracer_data.sol --overwrite --no-cbor-metadata --metadata-hash none
nil_block_generator init
nil_block_generator add-contract --contract-name MemoryGasTest --contract-path MemoryGasTest
nil_block_generator call-contract --contract-name MemoryGasTest --method testMemCopy --count 1 --args "900 0 20"
nil_block_generator get-block
nild run --http-port 8529 # should be run in another terminal (or with &) and stopped after collecting the traces with prover
prover trace memory_expansion/mcopy/mcopy 1 $block_hash
```

### broken index
```bash
solc -o . --bin --abi contracts/tracer_data.sol --overwrite --no-cbor-metadata --metadata-hash none
Expand Down
Loading

0 comments on commit e10e0ed

Please sign in to comment.