Utils

Git Source

Author: Matter Labs

Common utilities used in ZKsync system contracts

Note: security-contact: security@matterlabs.dev

State Variables

IS_CONSTRUCTOR_BYTECODE_HASH_BIT_MASK

Bit mask of bytecode hash "isConstructor" marker

bytes32 internal constant IS_CONSTRUCTOR_BYTECODE_HASH_BIT_MASK =
  0x00ff000000000000000000000000000000000000000000000000000000000000;

SET_IS_CONSTRUCTOR_MARKER_BIT_MASK

Bit mask to set the "isConstructor" marker in the bytecode hash

bytes32 internal constant SET_IS_CONSTRUCTOR_MARKER_BIT_MASK =
  0x0001000000000000000000000000000000000000000000000000000000000000;

MAX_BYTECODE_LENGTH

uint256 internal constant MAX_BYTECODE_LENGTH = (2 ** 16) - 1;

Functions

safeCastToU128

function safeCastToU128(uint256 _x) internal pure returns (uint128);

safeCastToU32

function safeCastToU32(uint256 _x) internal pure returns (uint32);

safeCastToU24

function safeCastToU24(uint256 _x) internal pure returns (uint24);

isCodeHashEVM

function isCodeHashEVM(bytes32 _bytecodeHash) internal pure returns (bool);

Returns

NameTypeDescription
<none>boolIf this bytecode hash for EVM contract or not

bytecodeLenInBytes

function bytecodeLenInBytes(bytes32 _bytecodeHash)
  internal
  pure
  returns (uint256 codeLengthInBytes);

Returns

NameTypeDescription
codeLengthInBytesuint256The bytecode length in bytes

bytecodeLenInWords

function bytecodeLenInWords(bytes32 _bytecodeHash)
  internal
  pure
  returns (uint256 codeLengthInWords);

Returns

NameTypeDescription
codeLengthInWordsuint256The bytecode length in machine words

isContractConstructed

Denotes whether bytecode hash corresponds to a contract that already constructed

function isContractConstructed(bytes32 _bytecodeHash)
  internal
  pure
  returns (bool);

isContractConstructing

Denotes whether bytecode hash corresponds to a contract that is on constructor or has already been constructed

function isContractConstructing(bytes32 _bytecodeHash)
  internal
  pure
  returns (bool);

constructingBytecodeHash

Sets "isConstructor" flag to TRUE for the bytecode hash

function constructingBytecodeHash(bytes32 _bytecodeHash)
  internal
  pure
  returns (bytes32);

Parameters

NameTypeDescription
_bytecodeHashbytes32The bytecode hash for which it is needed to set the constructing flag

Returns

NameTypeDescription
<none>bytes32The bytecode hash with "isConstructor" flag set to TRUE

constructedBytecodeHash

Sets "isConstructor" flag to FALSE for the bytecode hash

function constructedBytecodeHash(bytes32 _bytecodeHash)
  internal
  pure
  returns (bytes32);

Parameters

NameTypeDescription
_bytecodeHashbytes32The bytecode hash for which it is needed to set the constructing flag

Returns

NameTypeDescription
<none>bytes32The bytecode hash with "isConstructor" flag set to FALSE

hashL2Bytecode

Validate the bytecode format and calculate its hash.

function hashL2Bytecode(bytes calldata _bytecode)
  internal
  view
  returns (bytes32 hashedBytecode);

Parameters

NameTypeDescription
_bytecodebytesThe bytecode to hash.

Returns

NameTypeDescription
hashedBytecodebytes32The 32-byte hash of the bytecode. Note: The function reverts the execution if the bytecode has non expected format: - Bytecode bytes length is not a multiple of 32 - Bytecode bytes length is not less than 2^21 bytes (2^16 words) - Bytecode words length is not odd

hashEVMBytecode

Validate the bytecode format and calculate its hash.

function hashEVMBytecode(
  uint256 _evmBytecodeLen,
  bytes calldata _paddedBytecode
) internal view returns (bytes32 hashedEVMBytecode);

Parameters

NameTypeDescription
_evmBytecodeLenuint256The length of original EVM bytecode in bytes
_paddedBytecodebytesThe padded EVM bytecode to hash.

Returns

NameTypeDescription
hashedEVMBytecodebytes32The 32-byte hash of the EVM bytecode. Note: The function reverts the execution if the bytecode has non expected format: - Bytecode bytes length is not a multiple of 32 - Bytecode bytes length is greater than 2^16 - 1 bytes - Bytecode words length is not odd

getNewAddressCreate2EVM

Calculates the address of a deployed contract via create2 on the EVM

function getNewAddressCreate2EVM(
  address _sender,
  bytes32 _salt,
  bytes32 _bytecodeHash
) internal pure returns (address newAddress);

Parameters

NameTypeDescription
_senderaddressThe account that deploys the contract.
_saltbytes32The create2 salt.
_bytecodeHashbytes32The hash of the init code of the new contract.

Returns

NameTypeDescription
newAddressaddressThe derived address of the account.

getNewAddressCreateEVM

Calculates the address of a deployed contract via create

function getNewAddressCreateEVM(address _sender, uint256 _senderNonce)
  internal
  pure
  returns (address newAddress);

Parameters

NameTypeDescription
_senderaddressThe account that deploys the contract.
_senderNonceuint256The deploy nonce of the sender's account.