Skip to content

Commit

Permalink
Scaffold verifier integration-test
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriianChestnykh committed Jan 20, 2025
1 parent 1901186 commit 8365d4a
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// Copyright 2017 Christian Reitwiessner
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Check failure on line 3 in contracts/lib/groth16-verifiers/Groth16VerifierLinkedMultiQuery10Wrapper.sol

View workflow job for this annotation

GitHub Actions / solhint

Line length must be no more than 120 but current length is 435
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

Check failure on line 4 in contracts/lib/groth16-verifiers/Groth16VerifierLinkedMultiQuery10Wrapper.sol

View workflow job for this annotation

GitHub Actions / solhint

Line length must be no more than 120 but current length is 129
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Check failure on line 5 in contracts/lib/groth16-verifiers/Groth16VerifierLinkedMultiQuery10Wrapper.sol

View workflow job for this annotation

GitHub Actions / solhint

Line length must be no more than 120 but current length is 463
//
// 2019 OKIMS
// ported to solidity 0.6
// fixed linter warnings
// added requiere error messages
//
//
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.10;

import {IGroth16Verifier} from "../../interfaces/IGroth16Verifier.sol";
import {Groth16VerifierLinkedMultiQuery10} from "./Groth16VerifierLinkedMultiQuery10.sol";

contract Groth16VerifierLinkedMultiQuery10Wrapper is
Groth16VerifierLinkedMultiQuery10,
IGroth16Verifier
{
/**
* @dev Number of public signals for atomic mtp circuit
*/
uint constant PUBSIGNALS_LENGTH = 22;

/**
* @dev Verify the circuit with the groth16 proof π=([πa]1,[πb]2,[πc]1).
* @param a πa element of the groth16 proof.
* @param b πb element of the groth16 proof.
* @param c πc element of the groth16 proof.
* @param input Public inputs of the circuit.
* @return r true if the proof is valid.
*/
function verify(
uint256[2] calldata a,
uint256[2][2] calldata b,
uint256[2] calldata c,
uint256[] calldata input
) public view returns (bool r) {
uint[PUBSIGNALS_LENGTH] memory pubSignals;

require(input.length == PUBSIGNALS_LENGTH, "expected array length is 22");

for (uint256 i = 0; i < PUBSIGNALS_LENGTH; i++) {
pubSignals[i] = input[i];
}

return this.verifyProof(a, b, c, pubSignals);
}
}
62 changes: 49 additions & 13 deletions contracts/validators/LinkedMultiQueryValidator.sol
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;

import {ERC165} from "@openzeppelin/contracts/utils/introspection/ERC165.sol";
import {ICircuitValidator} from "../interfaces/ICircuitValidator.sol";
import {IGroth16Verifier} from "../interfaces/IGroth16Verifier.sol";
import {IRequestValidator} from "../interfaces/IRequestValidator.sol";
import {IState} from "../interfaces/IState.sol";
import {Ownable2StepUpgradeable} from "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
import {Initializable} from "../.deps/npm/@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

error WrongCircuitID(string circuitID);
error InvalidQueryHash(uint256 expectedQueryHash, uint256 actualQueryHash);
error InvalidGroupID(uint256 groupID);
error TooManyQueries(uint256 operatorCount);
error InvalidGroth16Proof();

contract LinkedMultiQueryValidator is IRequestValidator, Initializable {
contract LinkedMultiQueryValidator is Ownable2StepUpgradeable, IRequestValidator, ERC165 {
// This should be limited to the real number of queries in which operator != 0
struct Query {
uint256[] claimPathKey;
Expand Down Expand Up @@ -51,13 +51,6 @@ contract LinkedMultiQueryValidator is IRequestValidator, Initializable {
}
}

function getRequestParams(
bytes calldata params
) external view override returns (IRequestValidator.RequestParams memory) {
Query memory query = abi.decode(params, (Query));
return IRequestValidator.RequestParams(query.groupID, query.verifierID, 0);
}

struct PubSignals {
uint256 linkID;
uint256 merklized;
Expand All @@ -69,16 +62,39 @@ contract LinkedMultiQueryValidator is IRequestValidator, Initializable {
string internal constant CIRCUIT_ID = "linkedMultiQuery10";
uint256 internal constant QUERIES_COUNT = 10;

/**
* @dev Returns the version of the contract
* @return The version of the contract
*/
function version() external view override returns (string memory) {
return VERSION;
}

function initialize(address _groth16VerifierContractAddr) public initializer {
/**
* @dev Initialize the contract
* @param _groth16VerifierContractAddr Address of the verifier contract
* @param _stateContractAddr Address of the state contract
* @param owner Owner of the contract
*/
function initialize(
address _groth16VerifierContractAddr,
address _stateContractAddr,
address owner
) public initializer {
LinkedMultiQueryValidatorStorage storage $ = _getLinkedMultiQueryValidatorStorage();
$._supportedCircuits[CIRCUIT_ID] = IGroth16Verifier(_groth16VerifierContractAddr);
$._supportedCircuitIds.push(CIRCUIT_ID);
}

/**
* @dev Verify the proof with the supported method informed in the request query data
* packed as bytes and that the proof was generated by the sender.
* @param proof Proof packed as bytes to verify.
* @param data Request query data of the credential to verify.
* @param sender Sender of the proof.
* @param state State contract to get identities and gist states to check.
* @return Array of response fields as result.
*/
function verify(
bytes calldata proof,
bytes calldata data,
Expand Down Expand Up @@ -109,6 +125,28 @@ contract LinkedMultiQueryValidator is IRequestValidator, Initializable {
return _getResponseFields(pubSignals, query);
}

/**
* @dev Decodes special request parameters from the request params
* do be used by upper level clients of this contract.
* @param params Request parameters packed as bytes.
* @return Special request parameters extracted from the request data.
*/
function getRequestParams(
bytes calldata params
) external view override returns (IRequestValidator.RequestParams memory) {
Query memory query = abi.decode(params, (Query));
return IRequestValidator.RequestParams(query.groupID, query.verifierID, 0);
}

/**
* @dev See {IERC165-supportsInterface}.
*/
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return
interfaceId == type(IRequestValidator).interfaceId ||
super.supportsInterface(interfaceId);
}

function _checkGroupId(uint256 groupID) internal pure {
if (groupID == 0) {
revert InvalidGroupID(groupID);
Expand All @@ -126,9 +164,7 @@ contract LinkedMultiQueryValidator is IRequestValidator, Initializable {
}
}

function _parsePubSignals(
uint256[] memory inputs
) internal pure returns (PubSignals memory) {
function _parsePubSignals(uint256[] memory inputs) internal pure returns (PubSignals memory) {
uint256[QUERIES_COUNT] memory opsOutput;
uint256[QUERIES_COUNT] memory queryHashes;
PubSignals memory pubSignals = PubSignals({
Expand Down
23 changes: 19 additions & 4 deletions helpers/DeployHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
CredentialAtomicQueryV3ValidatorProxyModule,
UniversalVerifierProxyModule,
AuthV2ValidatorProxyModule,
UniversalVerifierMultiQueryProxyModule,
} from "../ignition";
import { chainIdInfoMap, contractsInfo } from "./constants";
import {
Expand All @@ -24,11 +23,12 @@ import {
} from "./helperUtils";
import { MCPaymentProxyModule } from "../ignition/modules/mcPayment";
import { AuthV2ValidatorForAuthProxyModule } from "../ignition/modules/authV2ValidatorForAuth";
import { LinkedMultiQueryProxyModule } from "../ignition/modules/linkedMultiQuery";

const SMT_MAX_DEPTH = 64;

export type Groth16VerifierType = "mtpV2" | "sigV2" | "v3" | "authV2";
export type ValidatorType = "mtpV2" | "sigV2" | "v3" | "authV2" | "authV2_forAuth";
export type Groth16VerifierType = "mtpV2" | "sigV2" | "v3" | "authV2" | "lmk10";
export type ValidatorType = "mtpV2" | "sigV2" | "v3" | "authV2" | "authV2_forAuth" | "lmk";

export class DeployHelper {
constructor(
Expand Down Expand Up @@ -575,6 +575,9 @@ export class DeployHelper {
case "authV2_forAuth":
groth16VerifierType = "authV2";
break;
case "lmk":
groth16VerifierType = "lmk10";
break;
}
return groth16VerifierType;
}
Expand All @@ -594,6 +597,9 @@ export class DeployHelper {
case "authV2":
g16VerifierContractWrapperName = contractsInfo.GROTH16_VERIFIER_AUTH_V2.name;
break;
case "lmk10":
g16VerifierContractWrapperName = contractsInfo.GROTH16_VERIFIER_LINKED_MULTI_QUERY10.name;
break;
}
return g16VerifierContractWrapperName;
}
Expand All @@ -615,9 +621,12 @@ export class DeployHelper {
break;
case "v3":
verification = contractsInfo.GROTH16_VERIFIER_V3.verificationOpts;
break;
case "authV2":
verification = contractsInfo.GROTH16_VERIFIER_AUTH_V2.verificationOpts;
break;
case "lmk10":
verification = contractsInfo.GROTH16_VERIFIER_LINKED_MULTI_QUERY10.verificationOpts;
}
return verification;
}
Expand All @@ -639,6 +648,7 @@ export class DeployHelper {
break;
case "v3":
verification = contractsInfo.VALIDATOR_V3.verificationOpts;
break;
case "authV2":
verification = contractsInfo.VALIDATOR_AUTH_V2.verificationOpts;
break;
Expand Down Expand Up @@ -713,6 +723,8 @@ export class DeployHelper {
case "authV2_forAuth":
validatorContractName = "AuthV2Validator_forAuth";
break;
case "lmk":
validatorContractName = "LinkedMultiQueryValidator";
}

let validator;
Expand All @@ -734,7 +746,10 @@ export class DeployHelper {
validatorModule = AuthV2ValidatorProxyModule;
break;
case "authV2_forAuth":
validatorContractName = AuthV2ValidatorForAuthProxyModule;
validatorModule = AuthV2ValidatorForAuthProxyModule;
break;
case "lmk":
validatorModule = LinkedMultiQueryProxyModule;
break;
}

Expand Down
27 changes: 27 additions & 0 deletions helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,22 @@ export const contractsInfo = Object.freeze({
libraries: {},
},
},
VALIDATOR_LINKED_MULTI_QUERY: {
name: "LinkedMultiQueryValidator",
version: "1.0.0-beta",
unifiedAddress: "",
create2Calldata: ethers.hexlify(ethers.toUtf8Bytes("iden3.create2.LinkedMultiQueryValidator")),
verificationOpts: {
constructorArgsImplementation: [],
constructorArgsProxy: [
"0x56fF81aBB5cdaC478bF236db717e4976b2ff841e",
"0xae15d2023a76174a940cbb2b7f44012c728b9d74",
"0x6964656e332e637265617465322e556e6976657273616c5665726966696572",
],
constructorArgsProxyAdmin: ["0xAe15d2023A76174a940cbb2b7f44012c728b9d74"],
libraries: {},
},
},
VALIDATOR_AUTH_V2: {
name: "AuthV2Validator",
version: "1.0.0",
Expand Down Expand Up @@ -343,6 +359,17 @@ export const contractsInfo = Object.freeze({
libraries: {},
},
},
GROTH16_VERIFIER_LINKED_MULTI_QUERY10: {
name: "Groth16VerifierLinkedMultiQuery10Wrapper",
unifiedAddress: "",
create2Calldata: "",
verificationOpts: {
contract:
"contracts/lib/groth16-verifiers/Groth16VerifierLinkedMultiQueryWrapper.sol:Groth16VerifierLinkedMultiQueryWrapper",
constructorArgsImplementation: [],
libraries: {},
},
},
GROTH16_VERIFIER_AUTH_V2: {
name: "Groth16VerifierAuthV2Wrapper",
unifiedAddress: "",
Expand Down
22 changes: 22 additions & 0 deletions ignition/modules/linkedMultiQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";
import { contractsInfo } from "../../helpers/constants";

export const LinkedMultiQueryProxyModule = buildModule("LinkedMultiQueryModule", (m) => {
const proxyAdminOwner = m.getAccount(0);

// This contract is supposed to be deployed to the same address across many networks,
// so the first implementation address is a dummy contract that does nothing but accepts any calldata.
// Therefore, it is a mechanism to deploy TransparentUpgradeableProxy contract
// with constant constructor arguments, so predictable init bytecode and predictable CREATE2 address.
// Subsequent upgrades are supposed to switch this proxy to the real implementation.

const proxy = m.contract("TransparentUpgradeableProxy", [
contractsInfo.CREATE2_ADDRESS_ANCHOR.unifiedAddress,
proxyAdminOwner,
contractsInfo.VALIDATOR_LINKED_MULTI_QUERY.create2Calldata,
]);

const proxyAdminAddress = m.readEventArgument(proxy, "AdminChanged", "newAdmin");
const proxyAdmin = m.contractAt("ProxyAdmin", proxyAdminAddress);
return { proxyAdmin, proxy };
});
55 changes: 55 additions & 0 deletions test/integration-tests/integration-verifier.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// An integration test with a MultiRequest
// The multiRequest has a single group with two requests inside
// One request is based on V3 validator
// Another one is based on LinkedMultiQuery validator

import { ethers } from "hardhat";
import { DeployHelper } from "../../helpers/DeployHelper";
import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers";

describe("Verifier Integration test", function () {
let verifier, verifierLib, v3Validator, lmkValidator;

async function deployContractsFixture() {
const verifierLib = await ethers.deployContract("VerifierLib");
const verifier = await ethers.deployContract("VerifierTestWrapper", [], {
libraries: { VerifierLib: await verifierLib.getAddress() },
});

const deployHelper = await DeployHelper.initialize(null, true);
const { state } = await deployHelper.deployStateWithLibraries([]);
await verifier.initialize(await state.getAddress());

const authValidator = await ethers.deployContract("AuthV2Validator_forAuth");

const authType = {
authType: "authV2",
validator: await authValidator.getAddress(),
params: "0x",
};
await verifier.setAuthType(authType);

const { validator: v3Validator } = await deployHelper.deployValidatorContractsWithVerifiers(
"v3",
await state.getAddress(),
);
const { validator: lmkValidator } = await deployHelper.deployValidatorContractsWithVerifiers(
"lmk",
await state.getAddress(),
);

return { verifier, verifierLib, v3Validator, lmkValidator };
}

beforeEach(async () => {
({ verifier, verifierLib, v3Validator, lmkValidator } =
await loadFixture(deployContractsFixture));
});

it("Should verify", async function () {
console.log("Verifier address: ", await verifier.getAddress());
console.log("VerifierLib address: ", await verifierLib.getAddress());
console.log("V3Validator address: ", await v3Validator.getAddress());
console.log("LinkedMultiQueryValidator address: ", await lmkValidator.getAddress());
});
});

0 comments on commit 8365d4a

Please sign in to comment.