Skip to content

Commit

Permalink
feat: add poll joined vk
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmad committed Jan 23, 2025
1 parent d63a7f2 commit 812b39c
Show file tree
Hide file tree
Showing 19 changed files with 413 additions and 190 deletions.
2 changes: 2 additions & 0 deletions packages/cli/tests/ceremony-params/ceremonyParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
ceremonyProcessMessagesNonQvWasmPath,
ceremonyTallyVotesNonQvWasmPath,
ceremonyPollJoiningZkeyPath,
ceremonyPollJoinedZkeyPath,
} from "../constants";
import { clean, isArm } from "../utils";

Expand All @@ -68,6 +69,7 @@ describe("Stress tests with ceremony params (6,3,2,20)", function test() {
voteOptionTreeDepth,
messageBatchSize,
pollJoiningZkeyPath: ceremonyPollJoiningZkeyPath,
pollJoinedZkeyPath: ceremonyPollJoinedZkeyPath,
processMessagesZkeyPathQv: ceremonyProcessMessagesZkeyPath,
tallyVotesZkeyPathQv: ceremonyTallyVotesZkeyPath,
processMessagesZkeyPathNonQv: ceremonyProcessMessagesNonQvZkeyPath,
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/tests/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const coordinatorPubKey = coordinatorKeypair.pubKey.serialize();
export const coordinatorPrivKey = coordinatorKeypair.privKey.serialize();

export const pollJoiningTestZkeyPath = "./zkeys/PollJoining_10_test/PollJoining_10_test.0.zkey";
export const pollJoinedTestZkeyPath = "./zkeys/PollJoined_10_test/PollJoined_10_test.0.zkey";
export const processMessageTestZkeyPath = "./zkeys/ProcessMessages_10-20-2_test/ProcessMessages_10-20-2_test.0.zkey";
export const tallyVotesTestZkeyPath = "./zkeys/TallyVotes_10-1-2_test/TallyVotes_10-1-2_test.0.zkey";
export const processMessageTestNonQvZkeyPath =
Expand All @@ -47,6 +48,7 @@ export const testTallyVotesWasmPath =
"./zkeys/TallyVotes_10-1-2_test/TallyVotes_10-1-2_test_js/TallyVotes_10-1-2_test.wasm";
export const testRapidsnarkPath = `${homedir()}/rapidsnark/build/prover`;
export const ceremonyPollJoiningZkeyPath = "./zkeys/PollJoining_10_test/PollJoining_10_test.0.zkey";
export const ceremonyPollJoinedZkeyPath = "./zkeys/PollJoined_10_test/PollJoined_10_test.0.zkey";
export const ceremonyProcessMessagesZkeyPath = "./zkeys/ProcessMessages_6-9-2-3/processMessages_6-9-2-3.zkey";
export const ceremonyProcessMessagesNonQvZkeyPath =
"./zkeys/ProcessMessagesNonQv_6-9-2-3/processMessagesNonQv_6-9-2-3.zkey";
Expand Down Expand Up @@ -98,6 +100,7 @@ export const setVerifyingKeysArgs: Omit<SetVerifyingKeysArgs, "signer"> = {
voteOptionTreeDepth: VOTE_OPTION_TREE_DEPTH,
messageBatchSize: MESSAGE_BATCH_SIZE,
pollJoiningZkeyPath: pollJoiningTestZkeyPath,
pollJoinedZkeyPath: pollJoinedTestZkeyPath,
processMessagesZkeyPathQv: processMessageTestZkeyPath,
tallyVotesZkeyPathQv: tallyVotesTestZkeyPath,
};
Expand All @@ -109,6 +112,7 @@ export const setVerifyingKeysNonQvArgs: Omit<SetVerifyingKeysArgs, "signer"> = {
voteOptionTreeDepth: VOTE_OPTION_TREE_DEPTH,
messageBatchSize: MESSAGE_BATCH_SIZE,
pollJoiningZkeyPath: pollJoiningTestZkeyPath,
pollJoinedZkeyPath: pollJoinedTestZkeyPath,
processMessagesZkeyPathNonQv: processMessageTestNonQvZkeyPath,
tallyVotesZkeyPathNonQv: tallyVotesTestNonQvZkeyPath,
};
Expand All @@ -119,6 +123,7 @@ export const checkVerifyingKeysArgs: Omit<CheckVerifyingKeysArgs, "signer"> = {
voteOptionTreeDepth: VOTE_OPTION_TREE_DEPTH,
messageBatchSize: MESSAGE_BATCH_SIZE,
pollJoiningZkeyPath: pollJoiningTestZkeyPath,
pollJoinedZkeyPath: pollJoinedTestZkeyPath,
processMessagesZkeyPath: processMessageTestZkeyPath,
tallyVotesZkeyPath: tallyVotesTestZkeyPath,
};
Expand Down
31 changes: 15 additions & 16 deletions packages/cli/ts/commands/checkVerifyingKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const checkVerifyingKeys = async ({
processMessagesZkeyPath,
tallyVotesZkeyPath,
pollJoiningZkeyPath,
pollJoinedZkeyPath,
vkRegistry,
signer,
useQuadraticVoting = true,
Expand Down Expand Up @@ -69,30 +70,28 @@ export const checkVerifyingKeys = async ({
// extract the verification keys from the zkey files
const processVk = VerifyingKey.fromObj(await extractVk(processMessagesZkeyPath));
const tallyVk = VerifyingKey.fromObj(await extractVk(tallyVotesZkeyPath));
const pollVk = VerifyingKey.fromObj(await extractVk(pollJoiningZkeyPath));
const pollJoiningVk = VerifyingKey.fromObj(await extractVk(pollJoiningZkeyPath));
const pollJoinedVk = VerifyingKey.fromObj(await extractVk(pollJoinedZkeyPath));

try {
logYellow(quiet, info("Retrieving verifying keys from the contract..."));
// retrieve the verifying keys from the contract

const pollVkOnChain = await vkRegistryContractInstance.getPollVk(stateTreeDepth, voteOptionTreeDepth);
const mode = useQuadraticVoting ? EMode.QV : EMode.NON_QV;

const processVkOnChain = await vkRegistryContractInstance.getProcessVk(
stateTreeDepth,
voteOptionTreeDepth,
messageBatchSize,
useQuadraticVoting ? EMode.QV : EMode.NON_QV,
);

const tallyVkOnChain = await vkRegistryContractInstance.getTallyVk(
stateTreeDepth,
intStateTreeDepth,
voteOptionTreeDepth,
useQuadraticVoting ? EMode.QV : EMode.NON_QV,
);
const [pollJoiningVkOnChain, pollJoinedVkOnChain, processVkOnChain, tallyVkOnChain] = await Promise.all([
vkRegistryContractInstance.getPollJoiningVk(stateTreeDepth, voteOptionTreeDepth),
vkRegistryContractInstance.getPollJoinedVk(stateTreeDepth, voteOptionTreeDepth),
vkRegistryContractInstance.getProcessVk(stateTreeDepth, voteOptionTreeDepth, messageBatchSize, mode),
vkRegistryContractInstance.getTallyVk(stateTreeDepth, intStateTreeDepth, voteOptionTreeDepth, mode),
]);

// do the actual validation
if (!compareVks(pollVk, pollVkOnChain)) {
if (!compareVks(pollJoiningVk, pollJoiningVkOnChain)) {
logError("Poll verifying keys do not match");
}

if (!compareVks(pollJoinedVk, pollJoinedVkOnChain)) {
logError("Poll verifying keys do not match");
}

Expand Down
8 changes: 5 additions & 3 deletions packages/cli/ts/commands/extractVkToFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,22 @@ export const extractVkToFile = async ({
processMessagesZkeyPathQv,
tallyVotesZkeyPathQv,
processMessagesZkeyPathNonQv,
tallyVotesZkeyPathNonQv,
pollJoinedZkeyPath,
pollJoiningZkeyPath,
tallyVotesZkeyPathNonQv,
outputFilePath,
}: ExtractVkToFileArgs): Promise<void> => {
const [processVkQv, tallyVkQv, processVkNonQv, tallyVkNonQv, pollVk] = await Promise.all([
const [processVkQv, tallyVkQv, processVkNonQv, tallyVkNonQv, pollJoiningVk, pollJoinedVk] = await Promise.all([
extractVk(processMessagesZkeyPathQv),
extractVk(tallyVotesZkeyPathQv),
extractVk(processMessagesZkeyPathNonQv),
extractVk(tallyVotesZkeyPathNonQv),
extractVk(pollJoiningZkeyPath),
extractVk(pollJoinedZkeyPath),
]);

await fs.promises.writeFile(
outputFilePath,
JSON.stringify({ processVkQv, tallyVkQv, processVkNonQv, tallyVkNonQv, pollVk }),
JSON.stringify({ processVkQv, tallyVkQv, processVkNonQv, tallyVkNonQv, pollJoiningVk, pollJoinedVk }),
);
};
14 changes: 7 additions & 7 deletions packages/cli/ts/commands/joinPoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ const getStateIndex = (pubKeys: PubKey[], userMaciPubKey: PubKey, stateIndex?: b
* @param rapidsnarkExePath - the path to the rapidnsark binary
* @param witnessExePath - the path to the compiled witness binary
* @param wasmPath - the path to the wasm witness
* @param pollVk - Poll verifying key
* @param pollJoiningVk - Poll verifying key
* @returns proof - an array of strings
*/
const generateAndVerifyProof = async (
const generateAndVerifyJoiningProof = async (
inputs: CircuitInputs,
zkeyPath: string,
useWasm: boolean | undefined,
rapidsnarkExePath: string | undefined,
witnessExePath: string | undefined,
wasmPath: string | undefined,
pollVk: IVkObjectParams,
pollJoiningVk: IVkObjectParams,
): Promise<string[]> => {
const r = await genProof({
inputs,
Expand All @@ -74,7 +74,7 @@ const generateAndVerifyProof = async (
});

// verify it
const isValid = await verifyProof(r.publicSignals, r.proof, pollVk);
const isValid = await verifyProof(r.publicSignals, r.proof, pollJoiningVk);

if (!isValid) {
throw new Error("Generated an invalid proof");
Expand Down Expand Up @@ -298,7 +298,7 @@ export const joinPoll = async ({
) as unknown as CircuitInputs;
}

const pollVk = await extractVk(pollJoiningZkey);
const pollJoiningVk = await extractVk(pollJoiningZkey);

let pollStateIndex = "";
let receipt: ContractTransactionReceipt | null = null;
Expand All @@ -308,14 +308,14 @@ export const joinPoll = async ({

try {
// generate the proof for this batch
const proof = await generateAndVerifyProof(
const proof = await generateAndVerifyJoiningProof(
circuitInputs,
pollJoiningZkey,
useWasm,
rapidsnark,
pollWitgen,
pollWasm,
pollVk,
pollJoiningVk,
);

// submit the message onchain as well as the encryption public key
Expand Down
101 changes: 39 additions & 62 deletions packages/cli/ts/commands/setVerifyingKeys.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { extractVk } from "maci-circuits";
import { type IVerifyingKeyStruct, VkRegistry__factory as VkRegistryFactory, EMode } from "maci-contracts";
import { genPollVkSig, genProcessVkSig, genTallyVkSig } from "maci-core";
import { genPollJoinedVkSig, genPollJoiningVkSig, genProcessVkSig, genTallyVkSig } from "maci-core";
import { VerifyingKey } from "maci-domainobjs";

import fs from "fs";
Expand Down Expand Up @@ -29,6 +29,7 @@ export const setVerifyingKeys = async ({
voteOptionTreeDepth,
messageBatchSize,
pollJoiningZkeyPath,
pollJoinedZkeyPath,
processMessagesZkeyPathQv,
tallyVotesZkeyPathQv,
processMessagesZkeyPathNonQv,
Expand Down Expand Up @@ -83,7 +84,8 @@ export const setVerifyingKeys = async ({
}

// extract the vks
const pollVk = pollJoiningZkeyPath && VerifyingKey.fromObj(await extractVk(pollJoiningZkeyPath));
const pollJoiningVk = pollJoiningZkeyPath && VerifyingKey.fromObj(await extractVk(pollJoiningZkeyPath));
const pollJoinedVk = pollJoinedZkeyPath && VerifyingKey.fromObj(await extractVk(pollJoinedZkeyPath));
const processVkQv = processMessagesZkeyPathQv && VerifyingKey.fromObj(await extractVk(processMessagesZkeyPathQv));
const tallyVkQv = tallyVotesZkeyPathQv && VerifyingKey.fromObj(await extractVk(tallyVotesZkeyPathQv));
const processVkNonQv =
Expand Down Expand Up @@ -127,8 +129,16 @@ export const setVerifyingKeys = async ({
const vkRegistryContract = VkRegistryFactory.connect(vkRegistryAddress, signer);

// check if the poll vk was already set
const pollVkSig = genPollVkSig(stateTreeDepth, voteOptionTreeDepth);
if (await vkRegistryContract.isPollVkSet(pollVkSig)) {
const pollJoiningVkSig = genPollJoiningVkSig(stateTreeDepth, voteOptionTreeDepth);

if (await vkRegistryContract.isPollJoiningVkSet(pollJoiningVkSig)) {
logError("This poll verifying key is already set in the contract");
}

// check if the poll vk was already set
const pollJoinedVkSig = genPollJoinedVkSig(stateTreeDepth, voteOptionTreeDepth);

if (await vkRegistryContract.isPollJoinedVkSet(pollJoinedVkSig)) {
logError("This poll verifying key is already set in the contract");
}

Expand Down Expand Up @@ -157,7 +167,6 @@ export const setVerifyingKeys = async ({
// actually set those values
try {
logYellow(quiet, info("Setting verifying keys..."));
const pollZkeys = (pollVk as VerifyingKey).asContractParam() as IVerifyingKeyStruct;

const processZkeys = [processVkQv, processVkNonQv]
.filter(Boolean)
Expand All @@ -182,7 +191,8 @@ export const setVerifyingKeys = async ({
voteOptionTreeDepth,
messageBatchSize,
modes,
pollZkeys,
(pollJoiningVk as VerifyingKey).asContractParam() as IVerifyingKeyStruct,
(pollJoinedVk as VerifyingKey).asContractParam() as IVerifyingKeyStruct,
processZkeys,
tallyZkeys,
);
Expand All @@ -196,62 +206,29 @@ export const setVerifyingKeys = async ({
logYellow(quiet, info(`Transaction hash: ${receipt!.hash}`));

// confirm that they were actually set correctly
if (useQuadraticVoting) {
const pollVkOnChain = await vkRegistryContract.getPollVk(stateTreeDepth, voteOptionTreeDepth);

const processVkOnChain = await vkRegistryContract.getProcessVk(
stateTreeDepth,
voteOptionTreeDepth,
messageBatchSize,
EMode.QV,
);

const tallyVkOnChain = await vkRegistryContract.getTallyVk(
stateTreeDepth,
intStateTreeDepth,
voteOptionTreeDepth,
EMode.QV,
);

if (!compareVks(pollVk as VerifyingKey, pollVkOnChain)) {
logError("pollVk mismatch");
}

if (!compareVks(processVkQv as VerifyingKey, processVkOnChain)) {
logError("processVk mismatch");
}

if (!compareVks(tallyVkQv as VerifyingKey, tallyVkOnChain)) {
logError("tallyVk mismatch");
}
} else {
const pollVkOnChain = await vkRegistryContract.getPollVk(stateTreeDepth, voteOptionTreeDepth);

const processVkOnChain = await vkRegistryContract.getProcessVk(
stateTreeDepth,
voteOptionTreeDepth,
messageBatchSize,
EMode.NON_QV,
);

const tallyVkOnChain = await vkRegistryContract.getTallyVk(
stateTreeDepth,
intStateTreeDepth,
voteOptionTreeDepth,
EMode.NON_QV,
);

if (!compareVks(pollVk as VerifyingKey, pollVkOnChain)) {
logError("pollVk mismatch");
}

if (!compareVks(processVkNonQv as VerifyingKey, processVkOnChain)) {
logError("processVk mismatch");
}

if (!compareVks(tallyVkNonQv as VerifyingKey, tallyVkOnChain)) {
logError("tallyVk mismatch");
}
const mode = useQuadraticVoting ? EMode.QV : EMode.NON_QV;

const [pollJoiningVkOnChain, pollJoinedVkOnChain, processVkOnChain, tallyVkOnChain] = await Promise.all([
vkRegistryContract.getPollJoiningVk(stateTreeDepth, voteOptionTreeDepth),
vkRegistryContract.getPollJoinedVk(stateTreeDepth, voteOptionTreeDepth),
vkRegistryContract.getProcessVk(stateTreeDepth, voteOptionTreeDepth, messageBatchSize, mode),
vkRegistryContract.getTallyVk(stateTreeDepth, intStateTreeDepth, voteOptionTreeDepth, mode),
]);

if (!compareVks(pollJoiningVk as VerifyingKey, pollJoiningVkOnChain)) {
logError("pollJoiningVk mismatch");
}

if (!compareVks(pollJoinedVk as VerifyingKey, pollJoinedVkOnChain)) {
logError("pollJoinedVk mismatch");
}

if (!compareVks(processVkQv as VerifyingKey, processVkOnChain)) {
logError("processVk mismatch");
}

if (!compareVks(tallyVkQv as VerifyingKey, tallyVkOnChain)) {
logError("tallyVk mismatch");
}
} catch (error) {
logError((error as Error).message);
Expand Down
Loading

0 comments on commit 812b39c

Please sign in to comment.