Skip to content

Commit

Permalink
Merge branch 'dvush-new-sc' into vb-new-contracts-improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
vladbochok authored Dec 11, 2020
2 parents cae1f6a + 9c16d26 commit a6a31d8
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 99 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
!package.json
!Cargo.lock
!Cargo.toml
!contracts/build
!contracts/artifacts
!infrastructure/explorer/index.html
!infrastructure/explorer/dist
!infrastructure/fee-seller
Expand Down
24 changes: 12 additions & 12 deletions contracts/contracts/Bytes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ library Bytes {
// Copies 'len' lower bytes from 'self' into a new 'bytes memory'.
// Returns the newly created 'bytes memory'. The returned bytes will be of length 'len'.
function toBytesFromUIntTruncated(uint256 self, uint8 byteLength) private pure returns (bytes memory bts) {
require(byteLength <= 32, "bt211");
require(byteLength <= 32, "a");
bts = new bytes(byteLength);
// Even though the bytes will allocate a full word, we don't want
// any potential garbage bytes in there.
Expand All @@ -53,7 +53,7 @@ library Bytes {
// NOTE: theoretically possible overflow of (_start + 20)
function bytesToAddress(bytes memory self, uint256 _start) internal pure returns (address addr) {
uint256 offset = _start + 20;
require(self.length >= offset, "bta11");
require(self.length >= offset, "b");
assembly {
addr := mload(add(self, offset))
}
Expand All @@ -63,7 +63,7 @@ library Bytes {
// NOTE: that bytes1..32 is stored in the beginning of the word unlike other primitive types
// NOTE: theoretically possible overflow of (_start + 20)
function bytesToBytes20(bytes memory self, uint256 _start) internal pure returns (bytes20 r) {
require(self.length >= (_start + 20), "btb20");
require(self.length >= (_start + 20), "c");
assembly {
r := mload(add(add(self, 0x20), _start))
}
Expand All @@ -73,7 +73,7 @@ library Bytes {
// NOTE: theoretically possible overflow of (_start + 0x2)
function bytesToUInt16(bytes memory _bytes, uint256 _start) internal pure returns (uint16 r) {
uint256 offset = _start + 0x2;
require(_bytes.length >= offset, "btu02");
require(_bytes.length >= offset, "d");
assembly {
r := mload(add(_bytes, offset))
}
Expand All @@ -83,7 +83,7 @@ library Bytes {
// NOTE: theoretically possible overflow of (_start + 0x3)
function bytesToUInt24(bytes memory _bytes, uint256 _start) internal pure returns (uint24 r) {
uint256 offset = _start + 0x3;
require(_bytes.length >= offset, "btu03");
require(_bytes.length >= offset, "e");
assembly {
r := mload(add(_bytes, offset))
}
Expand All @@ -92,7 +92,7 @@ library Bytes {
// NOTE: theoretically possible overflow of (_start + 0x4)
function bytesToUInt32(bytes memory _bytes, uint256 _start) internal pure returns (uint32 r) {
uint256 offset = _start + 0x4;
require(_bytes.length >= offset, "btu04");
require(_bytes.length >= offset, "f");
assembly {
r := mload(add(_bytes, offset))
}
Expand All @@ -101,7 +101,7 @@ library Bytes {
// NOTE: theoretically possible overflow of (_start + 0x10)
function bytesToUInt128(bytes memory _bytes, uint256 _start) internal pure returns (uint128 r) {
uint256 offset = _start + 0x10;
require(_bytes.length >= offset, "btu16");
require(_bytes.length >= offset, "g");
assembly {
r := mload(add(_bytes, offset))
}
Expand All @@ -111,7 +111,7 @@ library Bytes {
// NOTE: theoretically possible overflow of (_start + 0x14)
function bytesToUInt160(bytes memory _bytes, uint256 _start) internal pure returns (uint160 r) {
uint256 offset = _start + 0x14;
require(_bytes.length >= offset, "btu20");
require(_bytes.length >= offset, "h");
assembly {
r := mload(add(_bytes, offset))
}
Expand All @@ -120,7 +120,7 @@ library Bytes {
// NOTE: theoretically possible overflow of (_start + 0x20)
function bytesToBytes32(bytes memory _bytes, uint256 _start) internal pure returns (bytes32 r) {
uint256 offset = _start + 0x20;
require(_bytes.length >= offset, "btb32");
require(_bytes.length >= offset, "i");
assembly {
r := mload(add(_bytes, offset))
}
Expand All @@ -135,7 +135,7 @@ library Bytes {
uint256 _start,
uint256 _length
) internal pure returns (bytes memory) {
require(_bytes.length >= (_start + _length), "bse11"); // bytes length is less then start byte + length bytes
require(_bytes.length >= (_start + _length), "j"); // bytes length is less then start byte + length bytes

bytes memory tempBytes = new bytes(_length);

Expand Down Expand Up @@ -234,8 +234,8 @@ library Bytes {

/// Trim bytes into single word
function trim(bytes memory _data, uint256 _new_length) internal pure returns (uint256 r) {
require(_new_length <= 0x20, "trm10"); // new_length is longer than word
require(_data.length >= _new_length, "trm11"); // data is to short
require(_new_length <= 0x20, "k"); // new_length is longer than word
require(_data.length >= _new_length, "l"); // data is to short

uint256 a;
assembly {
Expand Down
3 changes: 3 additions & 0 deletions contracts/contracts/Config.sol
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,7 @@ contract Config {
/// @dev Maximum available error between real commit block timestamp and analog used in the verifier (in seconds)
/// @dev Must be used cause miner's `block.timestamp` value can differ on some small value (as we know - 15 seconds)
uint256 constant COMMIT_TIMESTAMP_APPROXIMATION_DELTA = 15 minutes;

/// @dev Bit mask to apply for verifier public input before verifying.
uint256 constant INPUT_MASK = $$(~uint256(0) >> 3);
}
10 changes: 5 additions & 5 deletions contracts/contracts/Governance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ contract Governance is Config {
/// @param _token Token address
function addToken(address _token) external {
requireGovernor(msg.sender);
require(tokenIds[_token] == 0, "gan11"); // token exists
require(totalTokens < MAX_AMOUNT_OF_REGISTERED_TOKENS, "gan12"); // no free identifiers for tokens
require(tokenIds[_token] == 0, "bz"); // token exists
require(totalTokens < MAX_AMOUNT_OF_REGISTERED_TOKENS, "ca"); // no free identifiers for tokens

totalTokens++;
uint16 newTokenId = totalTokens; // it is not `totalTokens - 1` because tokenId = 0 is reserved for eth
Expand Down Expand Up @@ -103,13 +103,13 @@ contract Governance is Config {
/// @notice Check if specified address is is governor
/// @param _address Address to check
function requireGovernor(address _address) public view {
require(_address == networkGovernor, "grr11"); // only by governor
require(_address == networkGovernor, "cb"); // only by governor
}

/// @notice Checks if validator is active
/// @param _address Validator address
function requireActiveValidator(address _address) external view {
require(validators[_address], "grr21"); // validator is not active
require(validators[_address], "cc"); // validator is not active
}

/// @notice Validate token id (must be less than or equal to total tokens amount)
Expand All @@ -124,7 +124,7 @@ contract Governance is Config {
/// @return tokens id
function validateTokenAddress(address _tokenAddr) external view returns (uint16) {
uint16 tokenId = tokenIds[_tokenAddr];
require(tokenId != 0, "gvs11"); // 0 is not a valid token
require(tokenId != 0, "cd"); // 0 is not a valid token
return tokenId;
}
}
4 changes: 2 additions & 2 deletions contracts/contracts/Operations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ library Operations {
(offset, parsed.amount) = Bytes.readUInt128(_data, offset); // amount
(offset, parsed.owner) = Bytes.readAddress(_data, offset); // owner

require(offset == PACKED_DEPOSIT_PUBDATA_BYTES, "rdp10"); // reading invalid deposit pubdata size
require(offset == PACKED_DEPOSIT_PUBDATA_BYTES, "m"); // reading invalid deposit pubdata size
}

/// Serialize deposit pubdata
Expand Down Expand Up @@ -111,7 +111,7 @@ library Operations {
(offset, parsed.tokenId) = Bytes.readUInt16(_data, offset); // tokenId
(offset, parsed.amount) = Bytes.readUInt128(_data, offset); // amount

require(offset == PACKED_FULL_EXIT_PUBDATA_BYTES, "rfp10"); // reading invalid full exit pubdata size
require(offset == PACKED_FULL_EXIT_PUBDATA_BYTES, "n"); // reading invalid full exit pubdata size
}

function writeFullExitPubdata(FullExit memory op) internal pure returns (bytes memory buf) {
Expand Down
2 changes: 1 addition & 1 deletion contracts/contracts/ReentrancyGuard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ contract ReentrancyGuard {
}

// On the first call to nonReentrant, _notEntered will be true
require(notEntered, "ReentrancyGuard: reentrant call");
require(notEntered, "o");

// Any calls to nonReentrant after this point will fail
assembly {
Expand Down
10 changes: 5 additions & 5 deletions contracts/contracts/SafeCast.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ library SafeCast {
* - input must fit into 128 bits
*/
function toUint128(uint256 value) internal pure returns (uint128) {
require(value < 2**128, "SafeCast: value doesn't fit in 128 bits");
require(value < 2**128, "p");
return uint128(value);
}

Expand All @@ -46,7 +46,7 @@ library SafeCast {
* - input must fit into 64 bits
*/
function toUint64(uint256 value) internal pure returns (uint64) {
require(value < 2**64, "SafeCast: value doesn't fit in 64 bits");
require(value < 2**64, "q");
return uint64(value);
}

Expand All @@ -61,7 +61,7 @@ library SafeCast {
* - input must fit into 32 bits
*/
function toUint32(uint256 value) internal pure returns (uint32) {
require(value < 2**32, "SafeCast: value doesn't fit in 32 bits");
require(value < 2**32, "r");
return uint32(value);
}

Expand All @@ -76,7 +76,7 @@ library SafeCast {
* - input must fit into 16 bits
*/
function toUint16(uint256 value) internal pure returns (uint16) {
require(value < 2**16, "SafeCast: value doesn't fit in 16 bits");
require(value < 2**16, "s");
return uint16(value);
}

Expand All @@ -91,7 +91,7 @@ library SafeCast {
* - input must fit into 8 bits.
*/
function toUint8(uint256 value) internal pure returns (uint8) {
require(value < 2**8, "SafeCast: value doesn't fit in 8 bits");
require(value < 2**8, "t");
return uint8(value);
}
}
10 changes: 5 additions & 5 deletions contracts/contracts/SafeMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ library SafeMath {
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
require(c >= a, "u");

return c;
}
Expand Down Expand Up @@ -61,7 +61,7 @@ library SafeMath {
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
require(b <= a, errorMessage);
require(b <= a, "v");
uint256 c = a - b;

return c;
Expand All @@ -85,7 +85,7 @@ library SafeMath {
}

uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
require(c / a == b, "w");

return c;
}
Expand Down Expand Up @@ -124,7 +124,7 @@ library SafeMath {
string memory errorMessage
) internal pure returns (uint256) {
// Solidity only automatically asserts when dividing by 0
require(b > 0, errorMessage);
require(b > 0, "x");
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold

Expand Down Expand Up @@ -164,7 +164,7 @@ library SafeMath {
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
require(b != 0, errorMessage);
require(b != 0, "y");
return a % b;
}
}
10 changes: 5 additions & 5 deletions contracts/contracts/SafeMathUInt128.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ library SafeMathUInt128 {
*/
function add(uint128 a, uint128 b) internal pure returns (uint128) {
uint128 c = a + b;
require(c >= a, "SafeMath: addition overflow");
require(c >= a, "z");

return c;
}
Expand Down Expand Up @@ -61,7 +61,7 @@ library SafeMathUInt128 {
uint128 b,
string memory errorMessage
) internal pure returns (uint128) {
require(b <= a, errorMessage);
require(b <= a, "aa");
uint128 c = a - b;

return c;
Expand All @@ -85,7 +85,7 @@ library SafeMathUInt128 {
}

uint128 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
require(c / a == b, "ab");

return c;
}
Expand Down Expand Up @@ -124,7 +124,7 @@ library SafeMathUInt128 {
string memory errorMessage
) internal pure returns (uint128) {
// Solidity only automatically asserts when dividing by 0
require(b > 0, errorMessage);
require(b > 0, "ac");
uint128 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold

Expand Down Expand Up @@ -164,7 +164,7 @@ library SafeMathUInt128 {
uint128 b,
string memory errorMessage
) internal pure returns (uint128) {
require(b != 0, errorMessage);
require(b != 0, "ad");
return a % b;
}
}
2 changes: 1 addition & 1 deletion contracts/contracts/Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ library Utils {
pure
returns (address)
{
require(_signature.length == 65, "ves10"); // incorrect signature length
require(_signature.length == 65, "ae"); // incorrect signature length

bytes32 signR;
bytes32 signS;
Expand Down
8 changes: 4 additions & 4 deletions contracts/contracts/Verifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;

import "./KeysWithPlonkVerifier.sol";
import "./Config.sol";

// Hardcoded constants to avoid accessing store
contract Verifier is KeysWithPlonkVerifier {
contract Verifier is KeysWithPlonkVerifier, Config {
function initialize(bytes calldata) external {}

/// @notice Verifier contract upgrade. Can be external because Proxy contract intercepts illegal calls of this function.
Expand All @@ -30,12 +31,11 @@ contract Verifier is KeysWithPlonkVerifier {
}
return true;
}
// #endif
for (uint256 i = 0; i < _individual_vks_inputs.length; ++i) {
uint256 commitment = _individual_vks_inputs[i];
uint256 mask = (~uint256(0)) >> 3;
_individual_vks_inputs[i] = uint256(commitment) & mask;
_individual_vks_inputs[i] = uint256(commitment) & INPUT_MASK;
}
// #endif
VerificationKey memory vk = getVkAggregated(uint32(_vkIndexes.length));

uint256 treeRoot = blockProof ? VK_TREE_ROOT : VK_EXIT_TREE_ROOT;
Expand Down
Loading

0 comments on commit a6a31d8

Please sign in to comment.