Utils
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
Name | Type | Description |
---|---|---|
<none> | bool | If this bytecode hash for EVM contract or not |
bytecodeLenInBytes
function bytecodeLenInBytes(bytes32 _bytecodeHash)
internal
pure
returns (uint256 codeLengthInBytes);
Returns
Name | Type | Description |
---|---|---|
codeLengthInBytes | uint256 | The bytecode length in bytes |
bytecodeLenInWords
function bytecodeLenInWords(bytes32 _bytecodeHash)
internal
pure
returns (uint256 codeLengthInWords);
Returns
Name | Type | Description |
---|---|---|
codeLengthInWords | uint256 | The 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
Name | Type | Description |
---|---|---|
_bytecodeHash | bytes32 | The bytecode hash for which it is needed to set the constructing flag |
Returns
Name | Type | Description |
---|---|---|
<none> | bytes32 | The 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
Name | Type | Description |
---|---|---|
_bytecodeHash | bytes32 | The bytecode hash for which it is needed to set the constructing flag |
Returns
Name | Type | Description |
---|---|---|
<none> | bytes32 | The 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
Name | Type | Description |
---|---|---|
_bytecode | bytes | The bytecode to hash. |
Returns
Name | Type | Description |
---|---|---|
hashedBytecode | bytes32 | The 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
Name | Type | Description |
---|---|---|
_evmBytecodeLen | uint256 | The length of original EVM bytecode in bytes |
_paddedBytecode | bytes | The padded EVM bytecode to hash. |
Returns
Name | Type | Description |
---|---|---|
hashedEVMBytecode | bytes32 | The 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
Name | Type | Description |
---|---|---|
_sender | address | The account that deploys the contract. |
_salt | bytes32 | The create2 salt. |
_bytecodeHash | bytes32 | The hash of the init code of the new contract. |
Returns
Name | Type | Description |
---|---|---|
newAddress | address | The 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
Name | Type | Description |
---|---|---|
_sender | address | The account that deploys the contract. |
_senderNonce | uint256 | The deploy nonce of the sender's account. |