-
Notifications
You must be signed in to change notification settings - Fork 24
/
Utils.sol
91 lines (75 loc) · 4.06 KB
/
Utils.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import "./EfficientCall.sol";
/**
* @author Matter Labs
* @dev Common utilities used in zkSync system contracts
*/
library Utils {
/// @dev Bit mask of bytecode hash "isConstructor" marker
bytes32 constant IS_CONSTRUCTOR_BYTECODE_HASH_BIT_MASK =
0x00ff000000000000000000000000000000000000000000000000000000000000;
/// @dev Bit mask to set the "isConstructor" marker in the bytecode hash
bytes32 constant SET_IS_CONSTRUCTOR_MARKER_BIT_MASK =
0x0001000000000000000000000000000000000000000000000000000000000000;
function safeCastToU128(uint256 _x) internal pure returns (uint128) {
require(_x <= type(uint128).max, "Overflow");
return uint128(_x);
}
function safeCastToU32(uint256 _x) internal pure returns (uint32) {
require(_x <= type(uint32).max, "Overflow");
return uint32(_x);
}
function safeCastToU24(uint256 _x) internal pure returns (uint24) {
require(_x <= type(uint24).max, "Overflow");
return uint24(_x);
}
/// @return codeLength The bytecode length in bytes
function bytecodeLenInBytes(bytes32 _bytecodeHash) internal pure returns (uint256 codeLength) {
codeLength = bytecodeLenInWords(_bytecodeHash) << 5; // _bytecodeHash * 32
}
/// @return codeLengthInWords The bytecode length in machine words
function bytecodeLenInWords(bytes32 _bytecodeHash) internal pure returns (uint256 codeLengthInWords) {
unchecked {
codeLengthInWords = uint256(uint8(_bytecodeHash[2])) * 256 + uint256(uint8(_bytecodeHash[3]));
}
}
/// @notice 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) {
return _bytecodeHash[1] == 0x01;
}
/// @notice Sets "isConstructor" flag to TRUE for the bytecode hash
/// @param _bytecodeHash The bytecode hash for which it is needed to set the constructing flag
/// @return The bytecode hash with "isConstructor" flag set to TRUE
function constructingBytecodeHash(bytes32 _bytecodeHash) internal pure returns (bytes32) {
// Clear the "isConstructor" marker and set it to 0x01.
return constructedBytecodeHash(_bytecodeHash) | SET_IS_CONSTRUCTOR_MARKER_BIT_MASK;
}
/// @notice Sets "isConstructor" flag to FALSE for the bytecode hash
/// @param _bytecodeHash The bytecode hash for which it is needed to set the constructing flag
/// @return The bytecode hash with "isConstructor" flag set to FALSE
function constructedBytecodeHash(bytes32 _bytecodeHash) internal pure returns (bytes32) {
return _bytecodeHash & ~IS_CONSTRUCTOR_BYTECODE_HASH_BIT_MASK;
}
/// @notice Validate the bytecode format and calculate its hash.
/// @param _bytecode The bytecode to hash.
/// @return hashedBytecode 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
function hashL2Bytecode(bytes calldata _bytecode) internal view returns (bytes32 hashedBytecode) {
// Note that the length of the bytecode must be provided in 32-byte words.
require(_bytecode.length % 32 == 0, "po");
uint256 bytecodeLenInWords = _bytecode.length / 32;
require(bytecodeLenInWords < 2 ** 16, "pp"); // bytecode length must be less than 2^16 words
require(bytecodeLenInWords % 2 == 1, "pr"); // bytecode length in words must be odd
hashedBytecode =
EfficientCall.sha(_bytecode) &
0x00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
// Setting the version of the hash
hashedBytecode = (hashedBytecode | bytes32(uint256(1 << 248)));
// Setting the length
hashedBytecode = hashedBytecode | bytes32(bytecodeLenInWords << 224);
}
}