feat: verify proof using test verifier

This commit is contained in:
rymnc 2022-11-28 10:32:53 +05:30
parent 4ab3a6f2fb
commit e53e225493
No known key found for this signature in database
GPG Key ID: C740033EE3F41EBD
7 changed files with 93 additions and 5 deletions

View File

@ -7,10 +7,30 @@ import "@semaphore-protocol/contracts/base/SemaphoreCore.sol";
import "@semaphore-protocol/contracts/base/SemaphoreConstants.sol"; import "@semaphore-protocol/contracts/base/SemaphoreConstants.sol";
contract InterepTest is IInterep, SemaphoreCore { contract InterepTest is IInterep, SemaphoreCore {
/// @dev Gets a tree depth and returns its verifier address.
mapping(uint8 => IVerifier) public verifiers;
mapping(uint256 => Group) public groups; mapping(uint256 => Group) public groups;
/// @dev mimics https://github.com/interep-project/contracts/blob/main/contracts/Interep.sol but ignores the verification mechanism /// @dev Checks if there is a verifier for the given tree depth.
constructor() {} /// @param depth: Depth of the tree.
modifier onlySupportedDepth(uint8 depth) {
require(
address(verifiers[depth]) != address(0),
"Interep: tree depth is not supported"
);
_;
}
/// @dev Initializes the Semaphore verifiers used to verify the user's ZK proofs.
/// @param _verifiers: List of Semaphore verifiers (address and related Merkle tree depth).
constructor(Verifier[] memory _verifiers) {
for (uint8 i = 0; i < _verifiers.length; i++) {
verifiers[_verifiers[i].merkleTreeDepth] = IVerifier(
_verifiers[i].contractAddress
);
}
}
/// @dev See {IInterep-updateGroups}. /// @dev See {IInterep-updateGroups}.
function updateGroups(Group[] calldata _groups) external override { function updateGroups(Group[] calldata _groups) external override {
@ -50,11 +70,33 @@ contract InterepTest is IInterep, SemaphoreCore {
); );
} }
/// @dev See {IInterep-verifyProof}.
function verifyProof( function verifyProof(
uint256 groupId, uint256 groupId,
bytes32 signal, bytes32 signal,
uint256 nullifierHash, uint256 nullifierHash,
uint256 externalNullifier, uint256 externalNullifier,
uint256[8] calldata proof uint256[8] calldata proof
) external override {} ) external override {
uint256 root = getRoot(groupId);
uint8 depth = getDepth(groupId);
require(depth != 0, "Interep: group does not exist");
IVerifier verifier = verifiers[depth];
_verifyProof(
signal,
root,
nullifierHash,
externalNullifier,
proof,
verifier
);
// TODO: check if the nullifier is not used before
// _saveNullifierHash(nullifierHash);
emit ProofVerified(groupId, signal);
}
} }

View File

@ -1,3 +1,4 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.15; pragma solidity 0.8.15;
import {IPoseidonHasher} from "./PoseidonHasher.sol"; import {IPoseidonHasher} from "./PoseidonHasher.sol";

View File

@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
contract VerifierTest {
function verifyProof(
uint[2] memory a,
uint[2][2] memory b,
uint[2] memory c,
uint[4] memory input
) public view {}
}

View File

@ -0,0 +1,24 @@
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";
import { isDevNet } from "../common";
const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const { deployments, getUnnamedAccounts } = hre;
const { deploy } = deployments;
const [deployer] = await getUnnamedAccounts();
await deploy("VerifierTest", {
from: deployer,
log: true,
});
};
export default func;
func.tags = ["VerifierTest"];
// skip when running on mainnet
func.skip = async (hre: HardhatRuntimeEnvironment) => {
if (isDevNet(hre.network.name)) {
return false;
}
return true;
};

View File

@ -1,6 +1,6 @@
import { HardhatRuntimeEnvironment } from "hardhat/types"; import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types"; import { DeployFunction } from "hardhat-deploy/types";
import { getGroups, isDevNet } from "../common"; import { getGroups, isDevNet, merkleTreeDepth } from "../common";
const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) { const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const { deployments, getUnnamedAccounts } = hre; const { deployments, getUnnamedAccounts } = hre;
@ -8,10 +8,19 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const [deployer] = await getUnnamedAccounts(); const [deployer] = await getUnnamedAccounts();
const verifierAddress = (await deployments.get("VerifierTest")).address;
const interepTest = await deploy("InterepTest", { const interepTest = await deploy("InterepTest", {
from: deployer, from: deployer,
log: true, log: true,
args: [], args: [
[
{
contractAddress: verifierAddress,
merkleTreeDepth,
},
],
],
}); });
const contract = await hre.ethers.getContractAt( const contract = await hre.ethers.getContractAt(
@ -24,6 +33,7 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
}; };
export default func; export default func;
func.tags = ["InterepTest"]; func.tags = ["InterepTest"];
func.dependencies = ["VerifierTest"];
// skip when running on mainnet // skip when running on mainnet
func.skip = async (hre: HardhatRuntimeEnvironment) => { func.skip = async (hre: HardhatRuntimeEnvironment) => {
if (isDevNet(hre.network.name)) { if (isDevNet(hre.network.name)) {