From 346ebee25b0cc624bccfc0f06f9f5a6c05b0e00c Mon Sep 17 00:00:00 2001 From: yu-zhen Date: Wed, 15 May 2024 02:57:28 +0800 Subject: [PATCH] feat: add extractVkToFile function to cli command --- cli/ts/commands/extractVkToFile.ts | 29 ++++++++++++++++ cli/ts/commands/index.ts | 1 + cli/ts/index.ts | 34 +++++++++++++++++++ cli/ts/utils/index.ts | 1 + cli/ts/utils/interfaces.ts | 27 +++++++++++++++ .../versioned_docs/version-v1.3_alpha/cli.md | 1 + 6 files changed, 93 insertions(+) create mode 100644 cli/ts/commands/extractVkToFile.ts diff --git a/cli/ts/commands/extractVkToFile.ts b/cli/ts/commands/extractVkToFile.ts new file mode 100644 index 0000000000..3d96d77ba2 --- /dev/null +++ b/cli/ts/commands/extractVkToFile.ts @@ -0,0 +1,29 @@ +import { extractVk } from "maci-circuits"; + +import fs from "fs"; + +import { ExtractVkToFileArgs } from "../utils"; + +/** + * Command to confirm that the verifying keys in the contract match the + * local ones + * @note see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing + * @param CheckVerifyingKeysArgs - The arguments for the checkVerifyingKeys command + * @returns Whether the verifying keys match or not + */ +export const extractVkToFile = async ({ + processMessagesZkeyPathQv, + tallyVotesZkeyPathQv, + processMessagesZkeyPathNonQv, + tallyVotesZkeyPathNonQv, + outputFilePath, +}: ExtractVkToFileArgs): Promise => { + const [processVkQv, tallyVkQv, processVkNonQv, tallyVkNonQv] = await Promise.all([ + extractVk(processMessagesZkeyPathQv), + extractVk(tallyVotesZkeyPathQv), + extractVk(processMessagesZkeyPathNonQv), + extractVk(tallyVotesZkeyPathNonQv), + ]); + + await fs.promises.writeFile(outputFilePath, JSON.stringify({ processVkQv, tallyVkQv, processVkNonQv, tallyVkNonQv })); +}; diff --git a/cli/ts/commands/index.ts b/cli/ts/commands/index.ts index ba6beff754..b3f413b6ba 100644 --- a/cli/ts/commands/index.ts +++ b/cli/ts/commands/index.ts @@ -17,3 +17,4 @@ export { fundWallet } from "./fundWallet"; export { proveOnChain } from "./proveOnChain"; export { checkVerifyingKeys } from "./checkVerifyingKeys"; export { genLocalState } from "./genLocalState"; +export { extractVkToFile } from "./extractVkToFile"; diff --git a/cli/ts/index.ts b/cli/ts/index.ts index 6775877500..78bb1d7849 100644 --- a/cli/ts/index.ts +++ b/cli/ts/index.ts @@ -29,6 +29,7 @@ import { proveOnChain, checkVerifyingKeys, genLocalState, + extractVkToFile, } from "./commands"; import { TallyData, logError, promptSensitiveValue, readContractAddress } from "./utils"; @@ -357,6 +358,39 @@ program program.error((error as Error).message, { exitCode: 1 }); } }); +program + .command("extractVkToFile") + .description("extract vkey to json file") + .requiredOption( + "-pqv, --process-messages-zkey-qv ", + "the process messages qv zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)", + ) + .requiredOption( + "-tqv, --tally-votes-zkey-qv ", + "the tally votes qv zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)", + ) + .requiredOption( + "-pnqv, --process-messages-zkey-non-qv ", + "the process messages non-qv zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)", + ) + .requiredOption( + "-tnqv, --tally-votes-zkey-non-qv ", + "the tally votes non-qv zkey path (see different options for zkey files to use specific circuits https://maci.pse.dev/docs/trusted-setup, https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)", + ) + .requiredOption("-o, --output-file ", "the output file path of extracted vkeys") + .action(async (cmdObj) => { + try { + await extractVkToFile({ + processMessagesZkeyPathQv: cmdObj.processMessagesZkeyQv, + tallyVotesZkeyPathQv: cmdObj.tallyVotesZkeyQv, + processMessagesZkeyPathNonQv: cmdObj.processMessagesZkeyNonQv, + tallyVotesZkeyPathNonQv: cmdObj.tallyVotesZkeyNonQv, + outputFilePath: cmdObj.outputFile, + }); + } catch (error) { + program.error((error as Error).message, { exitCode: 1 }); + } + }); program .command("signup") .description("Sign up to a MACI contract") diff --git a/cli/ts/utils/index.ts b/cli/ts/utils/index.ts index 2d9f4a59f7..9bb3f1ebbd 100644 --- a/cli/ts/utils/index.ts +++ b/cli/ts/utils/index.ts @@ -36,6 +36,7 @@ export type { PollContracts, TallyData, VerifyArgs, + ExtractVkToFileArgs, IRegisteredUserArgs, IGenKeypairArgs, IGetPollArgs, diff --git a/cli/ts/utils/interfaces.ts b/cli/ts/utils/interfaces.ts index 24021d0caf..4ff59f5c89 100644 --- a/cli/ts/utils/interfaces.ts +++ b/cli/ts/utils/interfaces.ts @@ -1062,3 +1062,30 @@ export interface DeployVkRegistryArgs { */ quiet?: boolean; } + +export interface ExtractVkToFileArgs { + /** + * File path for processMessagesQv zkey + */ + processMessagesZkeyPathQv: string; + + /** + * File path for tallyVotesQv zkey + */ + tallyVotesZkeyPathQv: string; + + /** + * File path for processMessagesNonQv zkey + */ + processMessagesZkeyPathNonQv: string; + + /** + * File path for tallyVotes zkey + */ + tallyVotesZkeyPathNonQv: string; + + /** + * Output file path of extracted vkeys + */ + outputFilePath: string; +} diff --git a/website/versioned_docs/version-v1.3_alpha/cli.md b/website/versioned_docs/version-v1.3_alpha/cli.md index 020478dced..db95c1aa79 100644 --- a/website/versioned_docs/version-v1.3_alpha/cli.md +++ b/website/versioned_docs/version-v1.3_alpha/cli.md @@ -42,6 +42,7 @@ pnpm run hardhat | `mergeMessages` | Merge the message accumulator queue | `-q, --quiet`: Whether to print values to the console
`-r, --rpc-provider `: The rpc provider URL
`-x, --maci-address `: The MACI contract address
`-o, --poll-id `: The poll id
`-n, --num-queue-ops `: The number of queue operations | | `mergeSignups` | Merge the signups accumulator queue | `-q, --quiet`: Whether to print values to the console
`-r, --rpc-provider `: The rpc provider URL
`-x, --maci-address `: The MACI contract address
`-o, --poll-id `: The poll id
`-n, --num-queue-ops `: The number of queue operations | | `timeTravel` | Fast-forward the time (only works for local hardhat testing) | `-s, --seconds `: The number of seconds to fast-forward
`-q, --quiet`: Whether to print values to the console
`-r, --rpc-provider `: The rpc provider URL | +| `extractVkToFile` | Extract verification keys (vKey) from zKey files | `-pqv, --process-messages-zkey-qv `: The path of processMessagesQv zkey files
`-tqv, --tally-votes-zkey-qv `: The path of tallyVotesQv zkey files
`-pnqv, --process-messages-zkey-non-qv `: The path of processMessagesNonQv zkey files
`-tnqv, --tally-votes-zkey-non-qv `: The path of tallyVotesNonQv zkey files
| | `signup` | Sign up to a MACI contract | `-p, --pubkey `: The MACI public key
`-x, --maci-address `: The MACI contract address
`-s, --sg-data `: The signup gateway data
`-i, --ivcp-data `: The initial voice credit proxy data
`-q, --quiet`: Whether to print values to the console
`-r, --rpc-provider `: The rpc provider URL | | `isRegisteredUser` | Checks if user is registered with public key | `-p, --pubkey `: The MACI public key
`-x, --maci-address `: The MACI contract address
`-q, --quiet`: Whether to print values to the console | | `topup` | Top up an account with voice credits | `-a, --amount `: The amount of topup
`-x, --maci-address `: The MACI contract address
`-i, --state-index `: State leaf index
`-o, --poll-id `: Poll id
`-q, --quiet`: Whether to print values to the console
`-r, --rpc-provider `: The rpc provider URL |