Skip to content

Commit

Permalink
remove VerifierLib and move functionality to Verifier
Browse files Browse the repository at this point in the history
  • Loading branch information
daveroga committed Jan 23, 2025
1 parent 45c5edc commit 539badf
Show file tree
Hide file tree
Showing 18 changed files with 82 additions and 199 deletions.
73 changes: 0 additions & 73 deletions contracts/lib/VerifierLib.sol

This file was deleted.

72 changes: 67 additions & 5 deletions contracts/verifiers/Verifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {ContextUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/Cont
import {IRequestValidator} from "../interfaces/IRequestValidator.sol";
import {IAuthValidator} from "../interfaces/IAuthValidator.sol";
import {IState} from "../interfaces/IState.sol";
import {VerifierLib} from "../lib/VerifierLib.sol";
import {IVerifier} from "../interfaces/IVerifier.sol";
import {GenesisUtils} from "../lib/GenesisUtils.sol";

Expand All @@ -20,6 +19,7 @@ error MetadataNotSupportedYet();
error MultiRequestIdAlreadyExists(uint256 multiRequestId);
error MultiRequestIdNotFound(uint256 multiRequestId);
error NullifierSessionIDAlreadyExists(uint256 nullifierSessionID);
error ResponseFieldAlreadyExists(string responseFieldName);
error RequestIdAlreadyExists(uint256 requestId);
error RequestIdNotFound(uint256 requestId);
error RequestIdNotValid();
Expand Down Expand Up @@ -47,7 +47,7 @@ abstract contract Verifier is IVerifier, ContextUpgradeable {
struct VerifierStorage {
// Information about requests
// solhint-disable-next-line
mapping(uint256 requestId => mapping(address sender => VerifierLib.Proof)) _proofs;
mapping(uint256 requestId => mapping(address sender => Proof)) _proofs;
mapping(uint256 requestId => IVerifier.RequestData) _requests;
uint256[] _requestIds;
IState _state;
Expand All @@ -64,13 +64,45 @@ abstract contract Verifier is IVerifier, ContextUpgradeable {
uint256 _verifierID;
}

/**
* @dev Struct to store proof and associated data
*/
struct Proof {
bool isVerified;
mapping(string key => uint256 inputValue) storageFields;
string validatorVersion;
// TODO: discuss if we need this field
// uint256 blockNumber;
uint256 blockTimestamp;
// This empty reserved space is put in place to allow future versions
// (see https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable#storage-gaps)
string[] keys;
// introduce artificial shift + 1 to avoid 0 index
mapping(string key => uint256 keyIndex) keyIndexes;
uint256[44] __gap;
}

/**
* @dev Struct to store auth proof and associated data
*/
struct AuthProof {
bool isVerified;
mapping(string key => uint256 inputValue) storageFields;
string validatorVersion;
uint256 blockTimestamp;
// This empty reserved space is put in place to allow future versions
// (see https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable#storage-gaps)
uint256[45] __gap;
}

// solhint-disable-next-line
// keccak256(abi.encode(uint256(keccak256("iden3.storage.Verifier")) -1 )) & ~bytes32(uint256(0xff));
bytes32 internal constant VerifierStorageLocation =
0x11369addde4aae8af30dcf56fa25ad3d864848d3201d1e9197f8b4da18a51a00;

bytes2 internal constant VerifierIdType = 0x01A1;
using VerifierLib for VerifierStorage;


Check failure on line 105 in contracts/verifiers/Verifier.sol

View workflow job for this annotation

GitHub Actions / solhint

Delete ⏎⏎···

/**
* @dev Modifier to check if the request exists
Expand Down Expand Up @@ -323,7 +355,7 @@ abstract contract Verifier is IVerifier, ContextUpgradeable {
// Check if userID from authResponse is the same as the one in the signals
_checkUserIDMatch(userIDFromAuthResponse, signals);

$.writeProofResults(response.requestId, sender, signals);
_writeProofResults(response.requestId, sender, signals);

if (response.metadata.length > 0) {
revert MetadataNotSupportedYet();
Expand Down Expand Up @@ -537,7 +569,7 @@ abstract contract Verifier is IVerifier, ContextUpgradeable {
uint256 requestId
) public view checkRequestExistence(requestId, true) returns (IVerifier.RequestStatus memory) {
VerifierStorage storage s = _getVerifierStorage();
VerifierLib.Proof storage proof = s._proofs[requestId][sender];
Proof storage proof = s._proofs[requestId][sender];

return
IVerifier.RequestStatus(
Expand Down Expand Up @@ -855,4 +887,34 @@ abstract contract Verifier is IVerifier, ContextUpgradeable {
VerifierStorage storage $ = _getVerifierStorage();
return $._requests[requestId];
}

/**

Check failure on line 891 in contracts/verifiers/Verifier.sol

View workflow job for this annotation

GitHub Actions / solhint

Delete ·
* @dev Writes proof results.
* @param requestId The request ID of the proof
* @param sender The address of the sender of the proof
* @param responseFields The array of response fields of the proof
*/
function _writeProofResults(
uint256 requestId,
address sender,
IRequestValidator.ResponseField[] memory responseFields
) internal {
VerifierStorage storage s = _getVerifierStorage();
Proof storage proof = s._proofs[requestId][sender];

// We only keep only 1 proof now without history. Prepared for the future if needed.
for (uint256 i = 0; i < responseFields.length; i++) {
proof.storageFields[responseFields[i].name] = responseFields[i].value;
if (proof.keyIndexes[responseFields[i].name] == 0) {
proof.keys.push(responseFields[i].name);
proof.keyIndexes[responseFields[i].name] = proof.keys.length;
} else {
revert ResponseFieldAlreadyExists(responseFields[i].name);
}
}

proof.isVerified = true;
proof.validatorVersion = s._requests[requestId].validator.version();
proof.blockTimestamp = block.timestamp;
}
}
19 changes: 0 additions & 19 deletions helpers/DeployHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,17 +442,6 @@ export class DeployHelper {
return stateLibWrapper;
}

async deployVerifierLib(): Promise<Contract> {
const contractName = "VerifierLib";

const verifierLib = await ethers.deployContract(contractName);
await verifierLib.waitForDeployment();

Logger.success(`${contractName} deployed to: ${await verifierLib.getAddress()}`);

return verifierLib;
}

async deployBinarySearchTestWrapper(): Promise<Contract> {
this.log("deploying poseidons...");
const [poseidon2Elements, poseidon3Elements] = await deployPoseidons([2, 3]);
Expand Down Expand Up @@ -860,7 +849,6 @@ export class DeployHelper {

async upgradeUniversalVerifier(
verifierAddress: string,
verifierLibAddr: string,
verifierContractName = contractsInfo.UNIVERSAL_VERIFIER.name,
): Promise<Contract> {
this.log("======== Verifier: upgrade started ========");
Expand All @@ -869,9 +857,6 @@ export class DeployHelper {
this.log("upgrading verifier...");
const VerifierFactory = await ethers.getContractFactory(verifierContractName, {
signer: proxyAdminOwner,
libraries: {
VerifierLib: verifierLibAddr,
},
});

this.log("upgrading proxy...");
Expand Down Expand Up @@ -915,7 +900,6 @@ export class DeployHelper {
async deployUniversalVerifier(
owner: SignerWithAddress | undefined,
stateAddr: string,
verifierLibAddr: string,
deployStrategy: "basic" | "create2" = "basic",
): Promise<Contract> {
if (!owner) {
Expand All @@ -925,9 +909,6 @@ export class DeployHelper {
contractsInfo.UNIVERSAL_VERIFIER.name,
{
signer: owner,
libraries: {
VerifierLib: verifierLibAddr,
},
},
);
const Create2AddressAnchorFactory = await ethers.getContractFactory(
Expand Down
6 changes: 1 addition & 5 deletions helpers/UniversalVerifierContractMigrationHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,9 @@ export class UniversalVerifierContractMigrationHelper extends ContractMigrationS
}

@log
async upgradeContract(
universalVerifierContract: Contract,
opts: { verifierLibAddress: string },
): Promise<any> {
async upgradeContract(universalVerifierContract: Contract): Promise<any> {
return await this._universalVerifierDeployHelper.upgradeUniversalVerifier(
await universalVerifierContract.getAddress(),
opts.verifierLibAddress,
);
}
}
10 changes: 1 addition & 9 deletions helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,15 +390,7 @@ export const contractsInfo = Object.freeze({
libraries: {},
},
},
VERIFIER_LIB: {
name: "VerifierLib",
unifiedAddress: "",
create2Address: "",
verificationOpts: {
constructorArgsImplementation: [],
libraries: {},
},
},

EMBEDDED_VERIFIER_WRAPPER: {
name: "EmbeddedVerifierWrapper",
unifiedAddress: "",
Expand Down
5 changes: 0 additions & 5 deletions ignition/modules/libraries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,3 @@ export const SpongePoseidonModule = buildModule("SpongePoseidonModule", (m) => {
});
return { spongePoseidon };
});

export const VerifierLibModule = buildModule("VerifierLibModule", (m) => {
const verifierLib = m.contract("VerifierLib");
return { verifierLib };
});
9 changes: 1 addition & 8 deletions scripts/deploy/deployCrossChainVerifierWithRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,8 @@ async function main() {

const { validator: validatorV3 } = await deployHelper.deployValidatorContractsWithVerifiers("v3");

// ##################### VerifierLib deploy #####################
const verifierLib = await deployHelper.deployVerifierLib();

// ##################### Universal Verifier deploy #####################
const verifier = await deployHelper.deployUniversalVerifier(
undefined,
await state.getAddress(),
await verifierLib.getAddress(),
);
const verifier = await deployHelper.deployUniversalVerifier(undefined, await state.getAddress());

const addToWhiteList1 = await verifier.addValidatorToWhitelist(await validatorSig.getAddress());
await addToWhiteList1.wait();
Expand Down
21 changes: 0 additions & 21 deletions scripts/deploy/deployUniversalVerifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,9 @@ async function main() {
"./scripts/deployments_output/temp_deployments_output.json",
);

let verifierLib = await tmpContractDeployments.getContract(contractsInfo.VERIFIER_LIB.name);
if (verifierLib) {
Logger.warning(
`${contractsInfo.VERIFIER_LIB.name} found already deployed to: ${await verifierLib?.getAddress()}`,
);
} else {
verifierLib = await deployHelper.deployVerifierLib();
const tx = await verifierLib.deploymentTransaction();
await waitNotToInterfereWithHardhatIgnition(tx);
tmpContractDeployments.addContract(
contractsInfo.VERIFIER_LIB.name,
await verifierLib.getAddress(),
);
await verifyContract(
await verifierLib.getAddress(),
contractsInfo.VERIFIER_LIB.verificationOpts,
);
}

const universalVerifier = await deployHelper.deployUniversalVerifier(
undefined,
stateContractAddress,
await verifierLib.getAddress(),
deployStrategy,
);
tmpContractDeployments.remove();
Expand All @@ -67,7 +47,6 @@ async function main() {
const outputJson = {
proxyAdminOwnerAddress: await signer.getAddress(),
universalVerifier: await universalVerifier.getAddress(),
verifierLib: await verifierLib.getAddress(),
state: stateContractAddress,
network: networkName,
chainId,
Expand Down
5 changes: 0 additions & 5 deletions scripts/maintenance/checkContractsVerificationManually.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { contractsInfo } from "../../helpers/constants";

async function main() {
const StateLibAddress: string = "<put-your-contract-address>";
const VerifierLibAddress: string = "<put-your-contract-address>";
const StateAddress: string = "<put-your-contract-address>";

if (StateAddress.includes("0x")) {
Expand All @@ -13,10 +12,6 @@ async function main() {
if (StateLibAddress.includes("0x")) {
await verifyContract(StateLibAddress, contractsInfo.STATE_LIB.verificationOpts);
}

if (VerifierLibAddress.includes("0x")) {
await verifyContract(VerifierLibAddress, contractsInfo.VERIFIER_LIB.verificationOpts);
}
}

main()
Expand Down
8 changes: 0 additions & 8 deletions scripts/upgrade/verifiers/embedded-verifier-upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,9 @@ async function main() {
const verifierRequestsCountBeforeUpgrade = await verifierContract.getZKPRequestsCount();
console.log("Owner Address Before Upgrade: ", verifierOwnerAddressBeforeUpgrade);

const verifierLib = await deployerHelper.deployVerifierLib();

// **** Upgrade Embedded Verifier ****
const verifierFactory = await ethers.getContractFactory(
contractsInfo.EMBEDDED_VERIFIER_WRAPPER.name,
{
libraries: {
VerifierLib: await verifierLib.getAddress(),
},
},
);

try {
Expand Down Expand Up @@ -100,7 +93,6 @@ async function main() {
const outputJson = {
proxyAdminOwnerAddress: await signer.getAddress(),
verifierContract: await verifierContract.getAddress(),
verifierLib: await verifierLib.getAddress(),
state: stateContractAddress,
network: network,
chainId,
Expand Down
Loading

0 comments on commit 539badf

Please sign in to comment.