From e2c61b7845c23bb795f9ea92c068f46c5d5f1f8b Mon Sep 17 00:00:00 2001 From: vbasiuk Date: Mon, 13 Nov 2023 17:25:52 +0200 Subject: [PATCH 1/3] Refactor GenesisUtils: separate primitive utils --- contracts/lib/ClaimBuilder.sol | 28 ++---- contracts/lib/GenesisUtils.sol | 89 +++---------------- contracts/lib/PrimitiveTypeUtils.sol | 125 +++++++++++++++++++++++++++ contracts/package.json | 52 +++++------ 4 files changed, 171 insertions(+), 123 deletions(-) create mode 100644 contracts/lib/PrimitiveTypeUtils.sol diff --git a/contracts/lib/ClaimBuilder.sol b/contracts/lib/ClaimBuilder.sol index 2b9ac84d..cda76489 100644 --- a/contracts/lib/ClaimBuilder.sol +++ b/contracts/lib/ClaimBuilder.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-3.0 pragma solidity 0.8.16; -import {BytesLib} from "solidity-bytes-utils/contracts/BytesLib.sol"; +import {PrimitiveTypeUtils} from "../lib/PrimitiveTypeUtils.sol"; import {GenesisUtils} from "../lib/GenesisUtils.sol"; library ClaimBuilder { @@ -65,8 +65,8 @@ library ClaimBuilder { uint32 flags; // Schema - bytes memory cutSchema = BytesLib.slice( - GenesisUtils.uint256ToBytes(GenesisUtils.reverse(c.schemaHash)), + bytes memory cutSchema = PrimitiveTypeUtils.slice( + PrimitiveTypeUtils.uint256ToBytes(PrimitiveTypeUtils.reverse(c.schemaHash)), 0, 16 ); @@ -125,19 +125,19 @@ library ClaimBuilder { require(c.merklizedRoot == 0, "merklizedRoot should be 0 for non merklized claim"); } - bytes memory claim0 = BytesLib.concat( + bytes memory claim0 = PrimitiveTypeUtils.concat( cutSchema, // 128 bits - abi.encodePacked(reverse(flags)) // 32 bits + abi.encodePacked(PrimitiveTypeUtils.reverse32(flags)) // 32 bits ); - bytes memory claim02 = BytesLib.concat( - abi.encodePacked(reverse(c.version)), // 32 bits + bytes memory claim02 = PrimitiveTypeUtils.concat( + abi.encodePacked(PrimitiveTypeUtils.reverse32(c.version)), // 32 bits abi.encodePacked(empty64) ); - claim0 = BytesLib.concat(claim0, claim02); + claim0 = PrimitiveTypeUtils.concat(claim0, claim02); - claim[0] = GenesisUtils.reverse(uint256(bytes32(claim0))); + claim[0] = PrimitiveTypeUtils.reverse(uint256(bytes32(claim0))); // claim[1] was written before @@ -154,14 +154,4 @@ library ClaimBuilder { return claim; } - - function reverse(uint32 input) internal pure returns (uint32 v) { - v = input; - - // swap bytes - v = ((v & 0xFF00FF00) >> 8) | ((v & 0x00FF00FF) << 8); - - // swap 2-byte long pairs - v = (v >> 16) | (v << 16); - } } diff --git a/contracts/lib/GenesisUtils.sol b/contracts/lib/GenesisUtils.sol index 7eaf656e..f7267f00 100644 --- a/contracts/lib/GenesisUtils.sol +++ b/contracts/lib/GenesisUtils.sol @@ -1,59 +1,9 @@ // SPDX-License-Identifier: GPL-3.0 pragma solidity 0.8.16; -import {BytesLib} from "solidity-bytes-utils/contracts/BytesLib.sol"; +import {PrimitiveTypeUtils} from "./PrimitiveTypeUtils.sol"; library GenesisUtils { - /** - * @dev uint256ToBytes - */ - function uint256ToBytes(uint256 x) internal pure returns (bytes memory b) { - b = new bytes(32); - assembly { - mstore(add(b, 32), x) - } - } - - /** - * @dev reverse - */ - function reverse(uint256 input) internal pure returns (uint256 v) { - v = input; - - // swap bytes - v = - ((v & 0xFF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00) >> 8) | - ((v & 0x00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF) << 8); - - // swap 2-byte long pairs - v = - ((v & 0xFFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000) >> 16) | - ((v & 0x0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF) << 16); - - // swap 4-byte long pairs - v = - ((v & 0xFFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000) >> 32) | - ((v & 0x00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF) << 32); - - // swap 8-byte long pairs - v = - ((v & 0xFFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF0000000000000000) >> 64) | - ((v & 0x0000000000000000FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF) << 64); - - // swap 16-byte long pairs - v = (v >> 128) | (v << 128); - } - - /** - * @dev reverse uint16 - */ - function reverse16(uint16 input) internal pure returns (uint16 v) { - v = input; - - // swap bytes - v = (v >> 8) | (v << 8); - } - /** * @dev sum */ @@ -65,21 +15,11 @@ library GenesisUtils { } } - /** - * @dev compareStrings - */ - function compareStrings(string memory a, string memory b) internal pure returns (bool) { - if (bytes(a).length != bytes(b).length) { - return false; - } - return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b)))); - } - /** * @dev isGenesisState */ function isGenesisState(uint256 id, uint256 idState) internal pure returns (bool) { - bytes2 idType = bytes2(uint256ToBytes(reverse(id))); + bytes2 idType = bytes2(PrimitiveTypeUtils.uint256ToBytes(PrimitiveTypeUtils.reverse(id))); uint256 computedId = calcIdFromGenesisState(idType, idState); return id == computedId; } @@ -91,21 +31,23 @@ library GenesisUtils { bytes2 idType, uint256 idState ) internal pure returns (uint256) { - bytes memory userStateB1 = uint256ToBytes(reverse(idState)); + bytes memory userStateB1 = PrimitiveTypeUtils.uint256ToBytes( + PrimitiveTypeUtils.reverse(idState) + ); - bytes memory cutState = BytesLib.slice(userStateB1, userStateB1.length - 27, 27); + bytes memory cutState = PrimitiveTypeUtils.slice(userStateB1, userStateB1.length - 27, 27); - bytes memory beforeChecksum = BytesLib.concat(abi.encodePacked(idType), cutState); + bytes memory beforeChecksum = PrimitiveTypeUtils.concat(abi.encodePacked(idType), cutState); require(beforeChecksum.length == 29, "Checksum requires 29 length array"); - uint16 checksum = reverse16(sum(beforeChecksum)); + uint16 checksum = PrimitiveTypeUtils.reverse16(sum(beforeChecksum)); bytes memory checkSumBytes = abi.encodePacked(checksum); - bytes memory idBytes = BytesLib.concat(beforeChecksum, checkSumBytes); + bytes memory idBytes = PrimitiveTypeUtils.concat(beforeChecksum, checkSumBytes); require(idBytes.length == 31, "idBytes requires 31 length array"); - return reverse(toUint256(idBytes)); + return PrimitiveTypeUtils.reverse(PrimitiveTypeUtils.toUint256(idBytes)); } /** @@ -114,15 +56,6 @@ library GenesisUtils { function calcIdFromEthAddress(bytes2 idType, address caller) internal pure returns (uint256) { uint256 addr = uint256(uint160(caller)); - return calcIdFromGenesisState(idType, reverse(addr)); - } - - /** - * @dev toUint256 - */ - function toUint256(bytes memory _bytes) internal pure returns (uint256 value) { - assembly { - value := mload(add(_bytes, 0x20)) - } + return calcIdFromGenesisState(idType, PrimitiveTypeUtils.reverse(addr)); } } diff --git a/contracts/lib/PrimitiveTypeUtils.sol b/contracts/lib/PrimitiveTypeUtils.sol new file mode 100644 index 00000000..72eaf550 --- /dev/null +++ b/contracts/lib/PrimitiveTypeUtils.sol @@ -0,0 +1,125 @@ +// SPDX-License-Identifier: GPL-3.0 +pragma solidity 0.8.16; + +import {BytesLib} from "solidity-bytes-utils/contracts/BytesLib.sol"; + +library PrimitiveTypeUtils { + /** + * @dev uint256ToBytes + */ + function uint256ToBytes(uint256 x) internal pure returns (bytes memory b) { + b = new bytes(32); + assembly { + mstore(add(b, 32), x) + } + } + + /** + * @dev reverse + */ + function reverse(uint256 input) internal pure returns (uint256 v) { + v = input; + + // swap bytes + v = + ((v & 0xFF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00) >> 8) | + ((v & 0x00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF) << 8); + + // swap 2-byte long pairs + v = + ((v & 0xFFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000) >> 16) | + ((v & 0x0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF) << 16); + + // swap 4-byte long pairs + v = + ((v & 0xFFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000) >> 32) | + ((v & 0x00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF) << 32); + + // swap 8-byte long pairs + v = + ((v & 0xFFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF0000000000000000) >> 64) | + ((v & 0x0000000000000000FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF) << 64); + + // swap 16-byte long pairs + v = (v >> 128) | (v << 128); + } + + /** + * @dev reverse uint16 + */ + function reverse16(uint16 input) internal pure returns (uint16 v) { + v = input; + + // swap bytes + v = (v >> 8) | (v << 8); + } + + /** + * @dev reverse uint32 + */ + function reverse32(uint32 input) internal pure returns (uint32 v) { + v = input; + + // swap bytes + v = ((v & 0xFF00FF00) >> 8) | ((v & 0x00FF00FF) << 8); + + // swap 2-byte long pairs + v = (v >> 16) | (v << 16); + } + + /** + * @dev compareStrings + */ + function compareStrings(string memory a, string memory b) internal pure returns (bool) { + if (bytes(a).length != bytes(b).length) { + return false; + } + return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b)))); + } + + /** + * @dev toUint256 + */ + function toUint256(bytes memory bys) internal pure returns (uint256 value) { + assembly { + value := mload(add(bys, 0x20)) + } + } + + /** + * @dev bytesToAddress + */ + function bytesToAddress(bytes memory bys) internal pure returns (address addr) { + assembly { + addr := mload(add(bys, 20)) + } + } + + /** + * @dev int256ToAddress + */ + function int256ToAddress(uint256 input) internal pure returns (address) { + return bytesToAddress(uint256ToBytes(reverse(input))); + } + + /** + * @dev concat + */ + function concat( + bytes memory preBytes, + bytes memory postBytes + ) internal pure returns (bytes memory) { + return BytesLib.concat(preBytes, postBytes); + } + + /** + * @dev slice + */ + function slice( + bytes memory bys, + uint256 start, + uint256 length + ) internal pure returns (bytes memory) { + return BytesLib.slice(bys, start, length); + } +} diff --git a/contracts/package.json b/contracts/package.json index 39a1ce2c..da01a368 100644 --- a/contracts/package.json +++ b/contracts/package.json @@ -1,28 +1,28 @@ { - "name": "@iden3/contracts", - "description": "Smart Contract library for Solidity", - "version": "1.3.0", - "files": [ - "**/*.sol", - "/build/contracts/*.json", - "!test-helpers" - ], - "repository": { - "type": "git", - "url": "https://github.com/iden3/contracts.git" - }, - "keywords": [ - "solidity", - "smart", - "contracts" - ], - "author": "iden3", - "license": "GPL-3.0", - "bugs": { - "url": "https://github.com/iden3/contracts/issues" - }, - "dependencies": { - "@openzeppelin/contracts": "^4.7.3", - "@openzeppelin/contracts-upgradeable": "^4.8.2" - } + "name": "@iden3/contracts", + "description": "Smart Contract library for Solidity", + "version": "1.3.1", + "files": [ + "**/*.sol", + "/build/contracts/*.json", + "!test-helpers" + ], + "repository": { + "type": "git", + "url": "https://github.com/iden3/contracts.git" + }, + "keywords": [ + "solidity", + "smart", + "contracts" + ], + "author": "iden3", + "license": "GPL-3.0", + "bugs": { + "url": "https://github.com/iden3/contracts/issues" + }, + "dependencies": { + "@openzeppelin/contracts": "^4.7.3", + "@openzeppelin/contracts-upgradeable": "^4.8.2" + } } From 4d0452c14269a956a4fa048618f0d407881853e8 Mon Sep 17 00:00:00 2001 From: vbasiuk Date: Mon, 13 Nov 2023 17:32:32 +0200 Subject: [PATCH 2/3] fix global import --- contracts/identitytreestore/IdentityTreeStore.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/identitytreestore/IdentityTreeStore.sol b/contracts/identitytreestore/IdentityTreeStore.sol index 3f94692d..02ca94ee 100644 --- a/contracts/identitytreestore/IdentityTreeStore.sol +++ b/contracts/identitytreestore/IdentityTreeStore.sol @@ -5,7 +5,7 @@ import {ReverseHashLib} from "../lib/ReverseHashLib.sol"; import {PoseidonUnit2L, PoseidonUnit3L} from "../lib/Poseidon.sol"; import {IState} from "../interfaces/IState.sol"; import {IOnchainCredentialStatusResolver} from "../interfaces/IOnchainCredentialStatusResolver.sol"; -import "../interfaces/IRHSStorage.sol"; +import {IRHSStorage} from "../interfaces/IRHSStorage.sol"; contract IdentityTreeStore is IOnchainCredentialStatusResolver, IRHSStorage { /** From 94f5904fbe8483b6bf7c72ef1a700cad2d5a3f1d Mon Sep 17 00:00:00 2001 From: vbasiuk Date: Mon, 13 Nov 2023 18:53:01 +0200 Subject: [PATCH 3/3] update package-lock file --- contracts/package-lock.json | 48 ++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/contracts/package-lock.json b/contracts/package-lock.json index 50b64545..705ad276 100644 --- a/contracts/package-lock.json +++ b/contracts/package-lock.json @@ -1,27 +1,27 @@ { - "name": "@iden3/contracts", - "version": "1.2.0", - "lockfileVersion": 3, - "requires": true, - "packages": { - "": { - "name": "@iden3/contracts", - "version": "1.2.0", - "license": "GPL-3.0", - "dependencies": { - "@openzeppelin/contracts": "^4.7.3", - "@openzeppelin/contracts-upgradeable": "^4.8.2" - } - }, - "node_modules/@openzeppelin/contracts": { - "version": "4.9.2", - "resolved": "https://registry.npmjs.org/@openzeppelin/contracts/-/contracts-4.9.2.tgz", - "integrity": "sha512-mO+y6JaqXjWeMh9glYVzVu8HYPGknAAnWyxTRhGeckOruyXQMNnlcW6w/Dx9ftLeIQk6N+ZJFuVmTwF7lEIFrg==" - }, - "node_modules/@openzeppelin/contracts-upgradeable": { - "version": "4.9.2", - "resolved": "https://registry.npmjs.org/@openzeppelin/contracts-upgradeable/-/contracts-upgradeable-4.9.2.tgz", - "integrity": "sha512-siviV3PZV/fHfPaoIC51rf1Jb6iElkYWnNYZ0leO23/ukXuvOyoC/ahy8jqiV7g+++9Nuo3n/rk5ajSN/+d/Sg==" - } + "name": "@iden3/contracts", + "version": "1.3.1", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "@iden3/contracts", + "version": "1.3.1", + "license": "GPL-3.0", + "dependencies": { + "@openzeppelin/contracts": "^4.7.3", + "@openzeppelin/contracts-upgradeable": "^4.8.2" + } + }, + "node_modules/@openzeppelin/contracts": { + "version": "4.9.2", + "resolved": "https://registry.npmjs.org/@openzeppelin/contracts/-/contracts-4.9.2.tgz", + "integrity": "sha512-mO+y6JaqXjWeMh9glYVzVu8HYPGknAAnWyxTRhGeckOruyXQMNnlcW6w/Dx9ftLeIQk6N+ZJFuVmTwF7lEIFrg==" + }, + "node_modules/@openzeppelin/contracts-upgradeable": { + "version": "4.9.2", + "resolved": "https://registry.npmjs.org/@openzeppelin/contracts-upgradeable/-/contracts-upgradeable-4.9.2.tgz", + "integrity": "sha512-siviV3PZV/fHfPaoIC51rf1Jb6iElkYWnNYZ0leO23/ukXuvOyoC/ahy8jqiV7g+++9Nuo3n/rk5ajSN/+d/Sg==" } + } }