Skip to content

Commit

Permalink
fix(cli): add a check that the subsidy verifying key was set correctl…
Browse files Browse the repository at this point in the history
…y on chain

fix #446
  • Loading branch information
ctrlc03 committed Dec 10, 2023
1 parent 88ceb65 commit a2ea186
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
20 changes: 20 additions & 0 deletions cli/tests/e2e.subsidy.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Keypair } from "maci-domainobjs";
import {
checkVerifyingKeys,
deploy,
deployPoll,
deployVkRegistryContract,
Expand Down Expand Up @@ -1240,4 +1241,23 @@ describe("e2e with Subsidy tests", function () {
);
});
});

describe("checkKeys", () => {
before(async () => {
// deploy maci as we need the address
await deploy(STATE_TREE_DEPTH);
});
it("should check if the verifying keys have been set correctly", async () => {
await checkVerifyingKeys(
STATE_TREE_DEPTH,
INT_STATE_TREE_DEPTH,
MSG_TREE_DEPTH,
VOTE_OPTION_TREE_DEPTH,
MSG_BATCH_DEPTH,
processMessageTestZkeyPath,
tallyVotesTestZkeyPath,
subsidyTestZkeyPath,
);
});
});
});
23 changes: 18 additions & 5 deletions cli/ts/commands/checkVerifyingKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const checkVerifyingKeys = async (
messageBatchDepth: number,
processMessagesZkeyPath: string,
tallyVotesZkeyPath: string,
subsidyZkeyPath?: string,
maciContract?: string,
quiet = true,
): Promise<boolean> => {
Expand All @@ -52,18 +53,25 @@ export const checkVerifyingKeys = async (
const maciContractInstance = new Contract(maciAddress, await parseArtifact("MACI")[0], signer);

// we need to ensure that the zkey files exist
if (!existsSync(processMessagesZkeyPath)) logError("Process messages zkey does not exist");
if (!existsSync(tallyVotesZkeyPath)) logError("Tally votes zkey does not exist");
if (!existsSync(processMessagesZkeyPath)) logError("The provided Process messages zkey does not exist");
if (!existsSync(tallyVotesZkeyPath)) logError("The provided Tally votes zkey does not exist");

// extract the verification keys from the zkey files
const processVk: VerifyingKey = VerifyingKey.fromObj(await extractVk(processMessagesZkeyPath));
const tallyVk: VerifyingKey = VerifyingKey.fromObj(await extractVk(tallyVotesZkeyPath));
const processVk = VerifyingKey.fromObj(await extractVk(processMessagesZkeyPath));
const tallyVk = VerifyingKey.fromObj(await extractVk(tallyVotesZkeyPath));

// check the subsidy key
let subsidyVk: VerifyingKey;
if (subsidyZkeyPath) {
if (!existsSync(subsidyZkeyPath)) logError("The provided Subsidy zkey does not exist");
subsidyVk = VerifyingKey.fromObj(await extractVk(subsidyZkeyPath));
}

try {
logYellow(quiet, info("Retrieving verifying keys from the contract..."));
// retrieve the verifying keys from the contract
const vkRegistryAddress = await maciContractInstance.vkRegistry();
const vkRegistryContract = new Contract(vkRegistryAddress, await parseArtifact("VkRegistry")[0], signer);
const vkRegistryContract = new Contract(vkRegistryAddress, parseArtifact("VkRegistry")[0], signer);

const messageBatchSize = 5 ** messageBatchDepth;

Expand All @@ -76,9 +84,14 @@ export const checkVerifyingKeys = async (

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

let subsidyVkOnChain: VerifyingKey;
if (subsidyVk)
subsidyVkOnChain = await vkRegistryContract.getSubsidyVk(stateTreeDepth, intStateTreeDepth, voteOptionTreeDepth);

// do the actual validation
if (!compareVks(processVk, processVkOnChain)) logError("Process verifying keys do not match");
if (!compareVks(tallyVk, tallyVkOnChain)) logError("Tally verifying keys do not match");
if (subsidyVk && !compareVks(subsidyVk, subsidyVkOnChain)) logError("Subsidy verifying keys do not match");
} catch (error: any) {
logError(error.message);
}
Expand Down
9 changes: 5 additions & 4 deletions cli/ts/commands/setVerifyingKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,17 @@ export const setVerifyingKeys = async (
const ssStateTreeDepth = Number(ssMatch[1]);
const ssIntStateTreeDepth = Number(ssMatch[2]);
const ssVoteOptionTreeDepth = Number(ssMatch[3]);

if (
stateTreeDepth !== ssStateTreeDepth ||
intStateTreeDepth !== ssIntStateTreeDepth ||
voteOptionTreeDepth !== ssVoteOptionTreeDepth
Number(stateTreeDepth) !== ssStateTreeDepth ||
Number(intStateTreeDepth) !== ssIntStateTreeDepth ||
Number(voteOptionTreeDepth) !== ssVoteOptionTreeDepth
)
logError("Incorrect .zkey file; please check the circuit params");

const subsidyVkSig = genSubsidyVkSig(stateTreeDepth, intStateTreeDepth, voteOptionTreeDepth);
if (await vkRegistryContract.isSubsidyVkSet(subsidyVkSig))
logError("This subsidy verifying key is already set in the contract");
info("This subsidy verifying key is already set in the contract");
}

// actually set those values
Expand Down
2 changes: 2 additions & 0 deletions cli/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ program
.requiredOption("-b, --msg-batch-depth <messageBatchDepth>", "the message batch depth")
.requiredOption("-p, --process-messages-zkey <processMessagesZkeyPath>", "the process messages zkey path")
.requiredOption("-t, --tally-votes-zkey <tallyVotesZkeyPath>", "the tally votes zkey path")
.option("-ss, --subsidy-zkey <subsidyZkeyPath>", "the subsidy zkey path")
.action(async (cmdOptions) => {
try {
await checkVerifyingKeys(
Expand All @@ -82,6 +83,7 @@ program
cmdOptions.msgBatchDepth,
cmdOptions.processMessagesZkey,
cmdOptions.tallyVotesZkey,
cmdOptions.subsidyZkey,
cmdOptions.maciContract,
cmdOptions.quiet,
);
Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-v1.x/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ npm run hardhat
| Command | Description | Options |
| -------------------- | ------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `create` | Deploy the contracts | `-v, --vkRegistryAddress <vkRegistryAddress>`: The vk registry contract address <br/> `-i, --initialVoiceCredits <initialVoiceCredits>`: The initial voice credits <br/> `-p, --initialVoiceCreditsProxyAddress <initialVoiceCreditsProxyAddress>`: The initial voice credits proxy contract address <br/> `-g, --signupGatekeeperAddress <signupGatekeeperAddress>`: The signup gatekeeper contract address <br/> `-q, --quiet`: Whether to print values to the console <br/> `-s, --stateTreeDepth <stateTreeDepth>`: The state tree depth |
| `checkVerifyingKeys` | Check that the verifying keys in the contract match the local ones | `-q, --quiet`: Whether to print values to the console <br/> `-x, --maci-contract <maciContract>`: The MACI contract address <br/> `-s, --state-tree-depth <stateTreeDepth>`: The state tree depth <br/> `-i, --int-state-tree-depth <intStateTreeDepth>`: The intermediate state tree depth <br/> `-m, --msg-tree-depth <messageTreeDepth>`: The message tree depth <br/> `-v, --vote-option-tree-depth <voteOptionTreeDepth>`: The vote option tree depth <br/> `-b, --msg-batch-depth <messageBatchDepth>`: The message batch depth <br/> `-p, --process-messages-zkey <processMessagesZkeyPath>`: The process messages zkey path <br/> `-t, --tally-votes-zkey <tallyVotesZkeyPath>`: The tally votes zkey path |
| `checkVerifyingKeys` | Check that the verifying keys in the contract match the local ones | `-q, --quiet`: Whether to print values to the console <br/> `-x, --maci-contract <maciContract>`: The MACI contract address <br/> `-s, --state-tree-depth <stateTreeDepth>`: The state tree depth <br/> `-i, --int-state-tree-depth <intStateTreeDepth>`: The intermediate state tree depth <br/> `-m, --msg-tree-depth <messageTreeDepth>`: The message tree depth <br/> `-v, --vote-option-tree-depth <voteOptionTreeDepth>`: The vote option tree depth <br/> `-b, --msg-batch-depth <messageBatchDepth>`: The message batch depth <br/> `-p, --process-messages-zkey <processMessagesZkeyPath>`: The process messages zkey path <br/> `-t, --tally-votes-zkey <tallyVotesZkeyPath>`: The tally votes zkey path <br /> `-ss, --subsidy-zkey <subsidyZkeyPath>`: The subsidy zkey path |
| `genMaciPubKey` | Generate a new MACI public key | `-sk, --privkey <privkey>`: The private key |
| `genMaciKeyPair` | Generate a new MACI key pair | No options |
| `airdrop` | Airdrop topup credits to the coordinator | `-a, --amount <amount>`: The amount of topup <br/> `-x, --contract <contract>`: The MACI contract address <br/> `-o, --poll-id <pollId>`: Poll id <br/> `-t, --token-address <tokenAddress>`: The token address <br/> `-q, --quiet`: Whether to print values to the console |
Expand Down

0 comments on commit a2ea186

Please sign in to comment.