From ce510ada509a3842922b9b79207bffe754fb98af Mon Sep 17 00:00:00 2001 From: Jia Liu Date: Mon, 18 Mar 2024 16:27:44 +0000 Subject: [PATCH 1/3] split verifier and verifying key contracts --- .env.example | 1 + README.md | 3 ++- contracts/zkdvrf.sol | 8 +++++--- scripts/deploy.ts | 15 ++++++++++++--- test/zkdvrf.spec.ts | 6 ++++-- 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/.env.example b/.env.example index 982c554..c591ea9 100644 --- a/.env.example +++ b/.env.example @@ -2,5 +2,6 @@ RPC_URL=http://localhost:7545 PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 DEPLOY_NO_HELPERS=false # optional HALO2V=0x26Aa5a7c4CA7D0F81943ea9CbDf97D80c560D6Fa # optional +HALO2VK=0x63311f167b6B07fd0D3d83310c16512701B4Cb2d # optional GPP=0xbB9a8f4c3662b6EF4b512E5f358289d1Db63fc81 # optional PSRAND=0xAecFC1cc68dD9664F2fCF5f2958d5277c5385123 # optional \ No newline at end of file diff --git a/README.md b/README.md index 701c31e..7a3f3d3 100644 --- a/README.md +++ b/README.md @@ -199,7 +199,8 @@ To deploy the Zkdvrf contracts on-chain- RPC_URL = PRIVATE_KEY = DEPLOY_NO_HELPERS = # optional -HALO2V = # optional +HALO2V = # optional +HALO2VK = # optional GPP = # optional PSRAND = # optional ``` diff --git a/contracts/zkdvrf.sol b/contracts/zkdvrf.sol index b75c0cc..bc169e9 100644 --- a/contracts/zkdvrf.sol +++ b/contracts/zkdvrf.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; -import {Halo2Verifier} from "./Halo2Verifier-3-5-g2.sol"; +import {Halo2Verifier} from "./Halo2Verifier.sol"; import {GlobalPublicParams} from "./GlobalPublicParams.sol"; import {Pairing} from "./libs/Pairing.sol"; import {IPseudoRand} from "./IPseudoRand.sol"; @@ -51,6 +51,7 @@ contract zkdvrf is Ownable { Status public contractPhase; address public halo2Verifier; + address public halo2VerifyingKey; address public globalPublicParams; address public pseudoRand; @@ -62,12 +63,13 @@ contract zkdvrf is Ownable { mapping (uint256 => uint32) public roundSubmissionCount; mapping (uint256 => IPseudoRand.PseudoRandom) public roundToRandom; - constructor(address halo2VerifierAddress, address globalPublicParamsAddress, address pseudoRandAddress, uint256 minDeposit) Ownable(msg.sender) { + constructor(address halo2VerifierAddress, address halo2VerifyingKeyAddress, address globalPublicParamsAddress, address pseudoRandAddress, uint256 minDeposit) Ownable(msg.sender) { require (halo2VerifierAddress != address(0) && globalPublicParamsAddress != address(0) && pseudoRandAddress != address(0), "Cannot be zero addresses"); memberCount = 5; threshold = 3; ppLength = 7 * memberCount + 14; halo2Verifier = halo2VerifierAddress; + halo2VerifyingKey = halo2VerifyingKeyAddress; globalPublicParams = globalPublicParamsAddress; pseudoRand = pseudoRandAddress; minNodeDeposit = minDeposit; @@ -121,7 +123,7 @@ contract zkdvrf is Ownable { require(contractPhase == Status.Nidkg, "Contract not in NIDKG phase"); require(!addrToNode[msg.sender].statusPP, "Node already submitted"); require(checkPublicParams(pp), "Invalid public parameters"); - require(Halo2Verifier(halo2Verifier).verifyProof(zkProof, pp), "SNARK proof verification failed"); + require(Halo2Verifier(halo2Verifier).verifyProof(halo2VerifyingKey, zkProof, pp), "SNARK proof verification failed"); addrToNode[msg.sender].statusPP = true; diff --git a/scripts/deploy.ts b/scripts/deploy.ts index 2fcbc1d..895b89c 100644 --- a/scripts/deploy.ts +++ b/scripts/deploy.ts @@ -9,15 +9,23 @@ async function main() { const deployNoHelpers = process.env.DEPLOY_NO_HELPERS === 'true' let halo2VerifierAddress + let halo2VerifyingKeyAddress let globalPublicParamsAddress let pseudoRandAddress if (deployNoHelpers) { - halo2VerifierAddress = process.env.HALO2V + halo2VerifierAddress = process.env.HALO2V + halo2VerifyingKeyAddress = process.env.HALO2VK globalPublicParamsAddress = process.env.GPP pseudoRandAddress = process.env.PSRAND } else { - const Halo2Verifier = await ethers.getContractFactory('contracts/Halo2Verifier-3-5-g2.sol:Halo2Verifier') + const Halo2VerifyingKey = await ethers.getContractFactory('contracts/Halo2VerifyingKey-3-5-18-g2.sol:Halo2VerifyingKey') + const halo2VerifyingKey = await Halo2VerifyingKey.connect(deployerWallet).deploy() + await halo2VerifyingKey.deployed() + + console.log("Halo2VerifyingKey deployed at", halo2VerifyingKey.address) + + const Halo2Verifier = await ethers.getContractFactory('contracts/Halo2Verifier.sol:Halo2Verifier') const halo2Verifier = await Halo2Verifier.connect(deployerWallet).deploy() await halo2Verifier.deployed() @@ -35,13 +43,14 @@ async function main() { console.log("PseudoRand deployed at", pseudoRand.address) + halo2VerifyingKeyAddress = halo2VerifyingKey.address halo2VerifierAddress = halo2Verifier.address globalPublicParamsAddress = globalPublicParams.address pseudoRandAddress = pseudoRand.address } const Zkdvrf = await ethers.getContractFactory('zkdvrf') - const zkdvrf = await Zkdvrf.connect(deployerWallet).deploy(halo2VerifierAddress, globalPublicParamsAddress, pseudoRandAddress, minDeposit) + const zkdvrf = await Zkdvrf.connect(deployerWallet).deploy(halo2VerifierAddress, halo2VerifyingKeyAddress, globalPublicParamsAddress, pseudoRandAddress, minDeposit) await zkdvrf.deployed() console.log("Zkdvrf deployed at", zkdvrf.address) diff --git a/test/zkdvrf.spec.ts b/test/zkdvrf.spec.ts index 70768ea..bcf53d7 100644 --- a/test/zkdvrf.spec.ts +++ b/test/zkdvrf.spec.ts @@ -8,6 +8,7 @@ chai.use(solidity); let Zkdvrf: Contract let Halo2Verifier: Contract +let Halo2VerifyingKey: Contract let GlobalPublicParams: Contract let PseudoRand: Contract @@ -82,12 +83,13 @@ const local_provider = new providers.JsonRpcProvider(cfg['url']) describe('ZKDVRF on-chain tests', async () => { before(async () => { - Halo2Verifier = await(await ethers.getContractFactory('contracts/Halo2Verifier-3-5-18-g2.sol:Halo2Verifier')).deploy() + Halo2Verifier = await(await ethers.getContractFactory('contracts/Halo2Verifier.sol:Halo2Verifier')).deploy() + Halo2VerifyingKey = await(await ethers.getContractFactory('contracts/Halo2VerifyingKey-3-5-18-g2.sol:Halo2VerifyingKey')).deploy() GlobalPublicParams = await(await ethers.getContractFactory('GlobalPublicParams')).deploy() PseudoRand = await(await ethers.getContractFactory('PseudoRand')).deploy() Zkdvrf = await ( await ethers.getContractFactory('zkdvrf') - ).deploy(Halo2Verifier.address, GlobalPublicParams.address, PseudoRand.address, minDeposit) + ).deploy(Halo2Verifier.address, Halo2VerifyingKey.address, GlobalPublicParams.address, PseudoRand.address, minDeposit) account1 = (await ethers.getSigners())[0] account2 = (await ethers.getSigners())[1] From c5d22e8925b3ba2524188ec9a06703f5f441352b Mon Sep 17 00:00:00 2001 From: Jia Liu Date: Tue, 19 Mar 2024 15:05:47 +0000 Subject: [PATCH 2/3] customize (t,n) setup --- .env.example | 2 ++ contracts/zkdvrf.sol | 6 +++--- scripts/deploy.ts | 4 +++- test/zkdvrf.spec.ts | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.env.example b/.env.example index c591ea9..f98a412 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,7 @@ RPC_URL=http://localhost:7545 PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 +THRESHOLD=3 +NUMBER_OF_MEMBERS=5 DEPLOY_NO_HELPERS=false # optional HALO2V=0x26Aa5a7c4CA7D0F81943ea9CbDf97D80c560D6Fa # optional HALO2VK=0x63311f167b6B07fd0D3d83310c16512701B4Cb2d # optional diff --git a/contracts/zkdvrf.sol b/contracts/zkdvrf.sol index bc169e9..3633cec 100644 --- a/contracts/zkdvrf.sol +++ b/contracts/zkdvrf.sol @@ -63,10 +63,10 @@ contract zkdvrf is Ownable { mapping (uint256 => uint32) public roundSubmissionCount; mapping (uint256 => IPseudoRand.PseudoRandom) public roundToRandom; - constructor(address halo2VerifierAddress, address halo2VerifyingKeyAddress, address globalPublicParamsAddress, address pseudoRandAddress, uint256 minDeposit) Ownable(msg.sender) { + constructor(uint32 thresholdValue, uint32 numberValue, address halo2VerifierAddress, address halo2VerifyingKeyAddress, address globalPublicParamsAddress, address pseudoRandAddress, uint256 minDeposit) Ownable(msg.sender) { require (halo2VerifierAddress != address(0) && globalPublicParamsAddress != address(0) && pseudoRandAddress != address(0), "Cannot be zero addresses"); - memberCount = 5; - threshold = 3; + memberCount = numberValue; + threshold = thresholdValue; ppLength = 7 * memberCount + 14; halo2Verifier = halo2VerifierAddress; halo2VerifyingKey = halo2VerifyingKeyAddress; diff --git a/scripts/deploy.ts b/scripts/deploy.ts index 895b89c..19aa1f2 100644 --- a/scripts/deploy.ts +++ b/scripts/deploy.ts @@ -5,6 +5,8 @@ async function main() { const netprovider = new providers.JsonRpcProvider(process.env.RPC_URL) const accPrivateKey = process.env.PRIVATE_KEY ?? '' const deployerWallet = new Wallet(accPrivateKey, netprovider) + const threshold = process.env.THRESHOLD + const numberOfMembers = process.env.NUMBER_OF_MEMBERS const minDeposit = process.env.MIN_DEPOSIT ?? '0' const deployNoHelpers = process.env.DEPLOY_NO_HELPERS === 'true' @@ -50,7 +52,7 @@ async function main() { } const Zkdvrf = await ethers.getContractFactory('zkdvrf') - const zkdvrf = await Zkdvrf.connect(deployerWallet).deploy(halo2VerifierAddress, halo2VerifyingKeyAddress, globalPublicParamsAddress, pseudoRandAddress, minDeposit) + const zkdvrf = await Zkdvrf.connect(deployerWallet).deploy(threshold, numberOfMembers, halo2VerifierAddress, halo2VerifyingKeyAddress, globalPublicParamsAddress, pseudoRandAddress, minDeposit) await zkdvrf.deployed() console.log("Zkdvrf deployed at", zkdvrf.address) diff --git a/test/zkdvrf.spec.ts b/test/zkdvrf.spec.ts index bcf53d7..33380a5 100644 --- a/test/zkdvrf.spec.ts +++ b/test/zkdvrf.spec.ts @@ -89,7 +89,7 @@ describe('ZKDVRF on-chain tests', async () => { PseudoRand = await(await ethers.getContractFactory('PseudoRand')).deploy() Zkdvrf = await ( await ethers.getContractFactory('zkdvrf') - ).deploy(Halo2Verifier.address, Halo2VerifyingKey.address, GlobalPublicParams.address, PseudoRand.address, minDeposit) + ).deploy(3, 5, Halo2Verifier.address, Halo2VerifyingKey.address, GlobalPublicParams.address, PseudoRand.address, minDeposit) account1 = (await ethers.getSigners())[0] account2 = (await ethers.getSigners())[1] From 99ae3da5ef89d248676c0a463fda2b55aeb65c57 Mon Sep 17 00:00:00 2001 From: Jia Liu Date: Wed, 20 Mar 2024 10:24:33 +0000 Subject: [PATCH 3/3] customise contract name for (t,n,d) --- .env.example | 1 + scripts/deploy.ts | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index f98a412..4c869cb 100644 --- a/.env.example +++ b/.env.example @@ -2,6 +2,7 @@ RPC_URL=http://localhost:7545 PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 THRESHOLD=3 NUMBER_OF_MEMBERS=5 +DEGREE=18 DEPLOY_NO_HELPERS=false # optional HALO2V=0x26Aa5a7c4CA7D0F81943ea9CbDf97D80c560D6Fa # optional HALO2VK=0x63311f167b6B07fd0D3d83310c16512701B4Cb2d # optional diff --git a/scripts/deploy.ts b/scripts/deploy.ts index 19aa1f2..1730ff3 100644 --- a/scripts/deploy.ts +++ b/scripts/deploy.ts @@ -7,6 +7,7 @@ async function main() { const deployerWallet = new Wallet(accPrivateKey, netprovider) const threshold = process.env.THRESHOLD const numberOfMembers = process.env.NUMBER_OF_MEMBERS + const degree = process.env.DEGREE const minDeposit = process.env.MIN_DEPOSIT ?? '0' const deployNoHelpers = process.env.DEPLOY_NO_HELPERS === 'true' @@ -21,7 +22,7 @@ async function main() { globalPublicParamsAddress = process.env.GPP pseudoRandAddress = process.env.PSRAND } else { - const Halo2VerifyingKey = await ethers.getContractFactory('contracts/Halo2VerifyingKey-3-5-18-g2.sol:Halo2VerifyingKey') + const Halo2VerifyingKey = await ethers.getContractFactory(`contracts/Halo2VerifyingKey-${threshold}-${numberOfMembers}-${degree}-g2.sol:Halo2VerifyingKey`) const halo2VerifyingKey = await Halo2VerifyingKey.connect(deployerWallet).deploy() await halo2VerifyingKey.deployed()