Skip to content

Commit

Permalink
initialize storage requests uint64 to uint256
Browse files Browse the repository at this point in the history
  • Loading branch information
daveroga committed Nov 7, 2024
1 parent 9482e0f commit 2577ce2
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 6 deletions.
7 changes: 7 additions & 0 deletions contracts/verifiers/UniversalVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ contract UniversalVerifier is
_setState(state);
}

/**
* @dev Initializes the ZKP requests from previous format
*/
function initializeRequests() public onlyOwner {
__ZKPVerifierBase_init_requests();
}

/// @dev Sets ZKP Request Owner address
/// @param requestId The ID of the ZKP request
/// @param requestOwner ZKP Request Owner address
Expand Down
70 changes: 69 additions & 1 deletion contracts/verifiers/ZKPVerifierBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ abstract contract ZKPVerifierBase is IZKPVerifier, ContextUpgradeable {
// We should increase the index by 1 when writing to the mapping
// This is to avoid unimbiguous with 0 proof position in the _proof array
mapping(address user => mapping(uint256 requestId => mapping(uint256 issuerId => uint256 _indexInProofs))) _proofsByIssuers;

Check failure on line 50 in contracts/verifiers/ZKPVerifierBase.sol

View workflow job for this annotation

GitHub Actions / solhint

Line length must be no more than 120 but current length is 132
bool _initializedRequests;
}

// keccak256(abi.encode(uint256(keccak256("iden3.storage.ZKPVerifier.ProofType1")) - 1)) & ~bytes32(uint256(0xff));
Expand All @@ -62,6 +63,14 @@ abstract contract ZKPVerifierBase is IZKPVerifier, ContextUpgradeable {
mapping(string key => bytes) metadata;
}

/**
* @dev Modifier to protect an initialization of requests function so that it can only be invoked if not initialized
*/
modifier onlyNotInitializedRequests() {
require(!_getZKPVerifierStorageProofType1()._initializedRequests, "Requests already initialized");

Check failure on line 70 in contracts/verifiers/ZKPVerifierBase.sol

View workflow job for this annotation

GitHub Actions / solhint

Replace !_getZKPVerifierStorageProofType1()._initializedRequests,·"Requests·already·initialized" with ⏎············!_getZKPVerifierStorageProofType1()._initializedRequests,⏎············"Requests·already·initialized"⏎········
_;
}

// 32 bytes (in Big Endian): 31-0x00(not used), 30-0x01(requestType), 29..8-0x00(not used), 7..0 requestId
function setZKPRequestV3(
uint256 requestId,
Expand Down Expand Up @@ -135,7 +144,7 @@ abstract contract ZKPVerifierBase is IZKPVerifier, ContextUpgradeable {
returns (ZKPVerifierStorageProofType1 storage $)
{
assembly {
$.slot := ZKPVerifierStorageLocation
$.slot := ZKPVerifierStorageProofType1Location
}
}

Expand All @@ -154,6 +163,65 @@ abstract contract ZKPVerifierBase is IZKPVerifier, ContextUpgradeable {
_setState(state);
}

function _initializeRequests() internal onlyNotInitializedRequests {
ZKPVerifierStorage storage s = _getZKPVerifierStorage();
uint256[] storage requestIds = s._requestIds;

uint256 slot;
uint256 len;
assembly {
slot := requestIds.slot
len := sload(requestIds.slot)
}
uint256 lenSlots = 0;
if (len > 0) {
lenSlots = (len / 4) + 1; // 4 uint64 in 1 slot
}
bytes32 location = keccak256(abi.encode(slot));

// Copy all the requestIds to a new array copyRequestIds
// accessing the storage directly in assembly because of the dynamic array uint64 in previous version
uint256[] memory copyRequestIds = new uint256[](len);
uint256 index = 0;

for (uint256 currentSlot = 0; currentSlot < lenSlots; currentSlot++) {
uint256 v0; uint256 v1; uint256 v2; uint256 v3;

assembly{
let valueSlot := sload(add(location, currentSlot)) // all the slot 256 bits
v0 := and(0xffffffff, valueSlot)
v1 := and(0xffffffff, shr(64, valueSlot))
v2 := and(0xffffffff, shr(128, valueSlot))
v3 := and(0xffffffff, shr(192, valueSlot))
}

copyRequestIds[index] = v0;
index++;
if ((currentSlot < lenSlots - 1) || (currentSlot + 1)*4 % len < 3) {
copyRequestIds[index] = v1;
index++;
}
if ((currentSlot < lenSlots - 1) || (currentSlot + 1)*4 % len < 2) {
copyRequestIds[index] = v2;
index++;
}
if ((currentSlot < lenSlots - 1) || (currentSlot + 1)*4 % len == 0) {
copyRequestIds[index] = v3;
index++;
}
}


for (uint256 i = 0; i < copyRequestIds.length; i++) {
s._requestIds[i] = copyRequestIds[i];
}
}

function __ZKPVerifierBase_init_requests() internal onlyNotInitializedRequests {
_initializeRequests();
_getZKPVerifierStorageProofType1()._initializedRequests = true;
}

/**
* @dev Max return array length for request queries
*/
Expand Down
21 changes: 16 additions & 5 deletions scripts/upgrade/verifiers/universal-verifier-upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ async function main() {
proxyAdminOwnerSigner,
);

const universalVerifierContract = await universalVerifierMigrationHelper.getInitContract({
// Get contract with previous ABI
let universalVerifierContract = await universalVerifierMigrationHelper.getInitContract({
contractNameOrAbi: previousUniversalVerifierArtifact.abi,
address: universalVerifierAddress,
});
Expand All @@ -125,11 +126,15 @@ async function main() {

await verifyContract(await verifierLib.getAddress(), contractsInfo.VERIFIER_LIB.verificationOpts);

// Upgrade and get upgraded contract with new ABI
// **** Upgrade Universal Verifier ****
await universalVerifierMigrationHelper.upgradeContract(universalVerifierContract, {
verifierLibAddress: await verifierLib.getAddress(),
verifierLibReqType1Address: await verifierLibReqType1.getAddress(),
});
universalVerifierContract = await universalVerifierMigrationHelper.upgradeContract(
universalVerifierContract,
{
verifierLibAddress: await verifierLib.getAddress(),
verifierLibReqType1Address: await verifierLibReqType1.getAddress(),
},
);
// ************************
console.log("Checking data after upgrade");

Expand All @@ -138,6 +143,12 @@ async function main() {
contractsInfo.UNIVERSAL_VERIFIER.verificationOpts,
);

// Initialize requests in ZKPVerifierStorage after upgrade uint64[] _requestIds -> uint256[] _requestIds;
const txRequestsUpgrade = await universalVerifierContract
.connect(universalVerifierOwnerSigner)
.initializeRequests();
await txRequestsUpgrade.wait();

const dataAfterUpgrade =
await universalVerifierMigrationHelper.getDataFromContract(universalVerifierContract);
universalVerifierMigrationHelper.checkData(dataBeforeUpgrade, dataAfterUpgrade);
Expand Down

0 comments on commit 2577ce2

Please sign in to comment.