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

Commit

Permalink
Zero ptr keccak test
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonD3 committed Oct 20, 2023
1 parent 2664215 commit 10570f6
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
79 changes: 79 additions & 0 deletions contracts/test-contracts/KeccakTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;
pragma abicoder v2;

import "../libraries/SystemContractsCaller.sol";
import "../Constants.sol";

contract KeccakTest {
// Just some computation-heavy function, it will be used to test out of gas
function infiniteFuction(uint256 n) pure public returns (uint256 sumOfSquares) {
for(uint i = 0; i < n; i++) {
sumOfSquares += i * i;
}
}

function _loadFarCallABIIntoActivePtr(
uint256 _gas
) private view {
uint256 farCallAbi = SystemContractsCaller.getFarCallABIWithEmptyFatPointer(
uint32(_gas),
// Only rollup is supported for now
0,
CalldataForwardingMode.ForwardFatPointer,
false,
false
);
_ptrPackIntoActivePtr(farCallAbi);
}

function _loadReturnDataIntoActivePtr() internal {
address callAddr = LOAD_LATEST_RETURNDATA_INTO_ACTIVE_PTR_CALL_ADDRESS;
assembly {
pop(staticcall(0, callAddr, 0, 0xFFFF, 0, 0))
}
}

function _ptrPackIntoActivePtr(uint256 _farCallAbi) internal view {
address callAddr = PTR_PACK_INTO_ACTIVE_CALL_ADDRESS;
assembly {
pop(staticcall(_farCallAbi, callAddr, 0, 0xFFFF, 0, 0))
}
}

function rawCallByRef(address _address) internal returns (bool success) {
address callAddr = RAW_FAR_CALL_BY_REF_CALL_ADDRESS;
assembly {
success := call(_address, callAddr, 0, 0, 0xFFFF, 0, 0)
}
}

function zeroPointerTest() external {
try this.infiniteFuction{gas: 1000000}(1000000) returns (uint256 sumOfSquares) {
revert("The transaction should have failed");
} catch {}

_loadReturnDataIntoActivePtr();
_loadFarCallABIIntoActivePtr(1000000);
bool success = rawCallByRef(KECCAK256_SYSTEM_CONTRACT);
require(success, "The call to keccak should have succeeded");

uint256 returndataSize = 0;
assembly {
returndataSize := returndatasize()
}
require(returndataSize == 32, "The return data size should be 32 bytes");

bytes32 result;
assembly {
let ptr := mload(0x40)
returndatacopy(ptr, 0, 32)
result := mload(ptr)
// Clearing the pointer just in case
mstore(ptr, 0)
}

require(result == bytes32(0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470), "The result is not correct");
}
}
14 changes: 14 additions & 0 deletions test/Keccak256.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { KeccakTest } from '../typechain-types';
import { deployContract } from './shared/utils';

describe('Keccak256 tests', function () {
let keccakTest: KeccakTest;

before(async () => {
keccakTest = (await deployContract('KeccakTest')) as KeccakTest
});

it('zero pointer test', async () => {
await keccakTest.zeroPointerTest()
});
});

0 comments on commit 10570f6

Please sign in to comment.