Skip to content

Commit

Permalink
fix: add coordinator public key hash public input
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmad committed Aug 5, 2024
1 parent 624b8de commit 898bc4e
Show file tree
Hide file tree
Showing 53 changed files with 214 additions and 535 deletions.
2 changes: 1 addition & 1 deletion .github/scripts/downloadZkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import path from "path";

const ZKEY_PATH = path.resolve(process.argv.slice(3)[0]);
const ZKEYS_URLS = {
test: "https://maci-develop-fra.s3.eu-central-1.amazonaws.com/v1.3.0/maci_artifacts_10-2-1-2_test.tar.gz",
test: "https://maci-develop-fra.s3.eu-central-1.amazonaws.com/v2.0.0/maci_artifacts_10-2-1-2_test.tar.gz",
prod: "https://maci-develop-fra.s3.eu-central-1.amazonaws.com/v1.2.0/maci_artifacts_6-9-2-3_prod.tar.gz",
};
const ARCHIVE_NAME = path.resolve(ZKEY_PATH, "maci_keys.tar.gz");
Expand Down
6 changes: 4 additions & 2 deletions circuits/circom/circuits.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"currentSbCommitment",
"newSbCommitment",
"pollEndTimestamp",
"actualStateTreeDepth"
"actualStateTreeDepth",
"coordinatorPublicKeyHash"
]
},
"ProcessMessagesNonQv_10-2-1-2_test": {
Expand All @@ -26,7 +27,8 @@
"currentSbCommitment",
"newSbCommitment",
"pollEndTimestamp",
"actualStateTreeDepth"
"actualStateTreeDepth",
"coordinatorPublicKeyHash"
]
},
"TallyVotes_10-1-2_test": {
Expand Down
7 changes: 4 additions & 3 deletions circuits/circom/core/non-qv/processMessages.circom
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ include "../../trees/incrementalQuinaryTree.circom";
signal input msgSubrootPathElements[msgTreeDepth - msgBatchDepth][MESSAGE_TREE_ARITY - 1];
// The coordinator's private key.
signal input coordPrivKey;
// The cooordinator's public key (derived from the contract).
signal input coordPubKey[2];
// The ECDH public key per message.
signal input encPubKeys[batchSize][2];
// The current state root (before the processing).
Expand All @@ -74,6 +72,8 @@ include "../../trees/incrementalQuinaryTree.circom";
signal input batchEndIndex;
// The batch index of current message batch
signal input index;
// The coordinator public key hash
signal input coordinatorPublicKeyHash;

// The state leaves upon which messages are applied.
// transform(currentStateLeaf[4], message5) => newStateLeaf4
Expand Down Expand Up @@ -185,7 +185,8 @@ include "../../trees/incrementalQuinaryTree.circom";
// based on the given private key - that is, the prover knows the
// coordinator's private key.
var derivedPubKey[2] = PrivToPubKey()(coordPrivKey);
derivedPubKey === coordPubKey;
var derivedPubKeyHash = PoseidonHasher(2)(derivedPubKey);
derivedPubKeyHash === coordinatorPublicKeyHash;

// Decrypt each Message into a Command.
// The command i-th is composed by the following fields.
Expand Down
7 changes: 4 additions & 3 deletions circuits/circom/core/qv/processMessages.circom
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ template ProcessMessages(
signal input msgSubrootPathElements[msgTreeDepth - msgBatchDepth][MESSAGE_TREE_ARITY - 1];
// The coordinator's private key.
signal input coordPrivKey;
// The cooordinator's public key (derived from the contract).
signal input coordPubKey[2];
// The ECDH public key per message.
signal input encPubKeys[batchSize][2];
// The current state root (before the processing).
Expand All @@ -74,6 +72,8 @@ template ProcessMessages(
signal input batchEndIndex;
// The batch index of current message batch
signal input index;
// The coordinator public key hash
signal input coordinatorPublicKeyHash;

// The state leaves upon which messages are applied.
// transform(currentStateLeaf[4], message5) => newStateLeaf4
Expand Down Expand Up @@ -180,7 +180,8 @@ template ProcessMessages(
// based on the given private key - that is, the prover knows the
// coordinator's private key.
var derivedPubKey[2] = PrivToPubKey()(coordPrivKey);
derivedPubKey === coordPubKey;
var derivedPubKeyHash = PoseidonHasher(2)(derivedPubKey);
derivedPubKeyHash === coordinatorPublicKeyHash;

// Decrypt each Message into a Command.
// The command i-th is composed by the following fields.
Expand Down
2 changes: 1 addition & 1 deletion circuits/circom/test/ProcessMessages_10-2-1-2_test.circom
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ pragma circom 2.0.0;

include ".././core/qv/processMessages.circom";

component main {public[numSignUps, index, batchEndIndex, msgRoot, currentSbCommitment, newSbCommitment, pollEndTimestamp, actualStateTreeDepth]} = ProcessMessages(10, 2, 1, 2);
component main {public[numSignUps, index, batchEndIndex, msgRoot, currentSbCommitment, newSbCommitment, pollEndTimestamp, actualStateTreeDepth, coordinatorPublicKeyHash]} = ProcessMessages(10, 2, 1, 2);
15 changes: 5 additions & 10 deletions circuits/ts/__tests__/CeremonyParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ describe("Ceremony param tests", () => {
stateLeafBatchDepth: 2,
};

const maxValues = {
maxUsers: STATE_TREE_ARITY ** params.stateTreeDepth,
maxMessages: MESSAGE_TREE_ARITY ** params.messageTreeDepth,
maxVoteOptions: MESSAGE_TREE_ARITY ** params.voteOptionTreeDepth,
};

const treeDepths = {
intStateTreeDepth: params.messageBatchTreeDepth,
messageTreeDepth: params.messageTreeDepth,
Expand Down Expand Up @@ -56,7 +50,7 @@ describe("Ceremony param tests", () => {
"msgs",
"msgSubrootPathElements",
"coordPrivKey",
"coordPubKey",
"coordinatorPublicKeyHash",
"encPubKeys",
"currentStateRoot",
"currentStateLeaves",
Expand Down Expand Up @@ -100,7 +94,6 @@ describe("Ceremony param tests", () => {

pollId = maciState.deployPoll(
BigInt(Math.floor(Date.now() / 1000) + duration),
maxValues,
treeDepths,
messageBatchSize,
coordinatorKeypair,
Expand Down Expand Up @@ -152,7 +145,10 @@ describe("Ceremony param tests", () => {

it("should produce the correct state root and ballot root", async () => {
// The current roots
const emptyBallot = new Ballot(poll.maxValues.maxVoteOptions, poll.treeDepths.voteOptionTreeDepth);
const emptyBallot = new Ballot(
MESSAGE_TREE_ARITY ** poll.treeDepths.voteOptionTreeDepth,
poll.treeDepths.voteOptionTreeDepth,
);
const emptyBallotHash = emptyBallot.hash();
const ballotTree = new IncrementalQuinTree(params.stateTreeDepth, emptyBallot.hash(), STATE_TREE_ARITY, hash5);
ballotTree.insert(emptyBallot.hash());
Expand Down Expand Up @@ -236,7 +232,6 @@ describe("Ceremony param tests", () => {

pollId = maciState.deployPoll(
BigInt(Math.floor(Date.now() / 1000) + duration),
maxValues,
treeDepths,
messageBatchSize,
coordinatorKeypair,
Expand Down
46 changes: 23 additions & 23 deletions circuits/ts/__tests__/ProcessMessages.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { expect } from "chai";
import { type WitnessTester } from "circomkit";
import { MaciState, Poll, STATE_TREE_ARITY } from "maci-core";
import { MaciState, MESSAGE_TREE_ARITY, Poll, STATE_TREE_ARITY } from "maci-core";
import { IncrementalQuinTree, hash2 } from "maci-crypto";
import { PrivKey, Keypair, PCommand, Message, Ballot, PubKey } from "maci-domainobjs";

import { IProcessMessagesInputs } from "../types";

import {
STATE_TREE_DEPTH,
duration,
maxValues,
messageBatchSize,
treeDepths,
voiceCreditBalance,
} from "./utils/constants";
import { STATE_TREE_DEPTH, duration, messageBatchSize, treeDepths, voiceCreditBalance } from "./utils/constants";
import { circomkitInstance } from "./utils/utils";

describe("ProcessMessage circuit", function test() {
Expand All @@ -31,7 +24,7 @@ describe("ProcessMessage circuit", function test() {
"msgs",
"msgSubrootPathElements",
"coordPrivKey",
"coordPubKey",
"coordinatorPublicKeyHash",
"encPubKeys",
"currentStateRoot",
"currentStateLeaves",
Expand Down Expand Up @@ -84,7 +77,6 @@ describe("ProcessMessage circuit", function test() {

pollId = maciState.deployPoll(
BigInt(Math.floor(Date.now() / 1000) + duration),
maxValues,
treeDepths,
messageBatchSize,
coordinatorKeypair,
Expand Down Expand Up @@ -162,7 +154,6 @@ describe("ProcessMessage circuit", function test() {

pollId = maciState.deployPoll(
BigInt(Math.floor(Date.now() / 1000) + duration),
maxValues,
treeDepths,
messageBatchSize,
coordinatorKeypair,
Expand Down Expand Up @@ -212,7 +203,10 @@ describe("ProcessMessage circuit", function test() {

it("should produce the correct state root and ballot root", async () => {
// The current roots
const emptyBallot = new Ballot(poll.maxValues.maxVoteOptions, poll.treeDepths.voteOptionTreeDepth);
const emptyBallot = new Ballot(
MESSAGE_TREE_ARITY ** poll.treeDepths.voteOptionTreeDepth,
poll.treeDepths.voteOptionTreeDepth,
);
const emptyBallotHash = emptyBallot.hash();
const ballotTree = new IncrementalQuinTree(STATE_TREE_DEPTH, emptyBallot.hash(), STATE_TREE_ARITY, hash2);

Expand Down Expand Up @@ -266,7 +260,6 @@ describe("ProcessMessage circuit", function test() {

pollId = maciState.deployPoll(
BigInt(2 + duration), // BigInt(Math.floor(Date.now() / 1000) + duration),
maxValues,
treeDepths,
messageBatchSize,
coordinatorKeypair,
Expand Down Expand Up @@ -298,7 +291,10 @@ describe("ProcessMessage circuit", function test() {

it("should produce the correct state root and ballot root", async () => {
// The current roots
const emptyBallot = new Ballot(poll.maxValues.maxVoteOptions, poll.treeDepths.voteOptionTreeDepth);
const emptyBallot = new Ballot(
MESSAGE_TREE_ARITY ** poll.treeDepths.voteOptionTreeDepth,
poll.treeDepths.voteOptionTreeDepth,
);
const emptyBallotHash = emptyBallot.hash();
const ballotTree = new IncrementalQuinTree(STATE_TREE_DEPTH, emptyBallot.hash(), STATE_TREE_ARITY, hash2);

Expand Down Expand Up @@ -350,7 +346,6 @@ describe("ProcessMessage circuit", function test() {

pollId = maciState.deployPoll(
BigInt(2 + duration), // BigInt(Math.floor(Date.now() / 1000) + duration),
maxValues,
treeDepths,
messageBatchSize,
coordinatorKeypair,
Expand Down Expand Up @@ -427,7 +422,6 @@ describe("ProcessMessage circuit", function test() {
// Sign up and publish
const id = state.deployPoll(
BigInt(Math.floor(Date.now() / 1000) + duration),
maxValues,
treeDepths,
messageBatchSize,
coordinatorKeypair,
Expand Down Expand Up @@ -486,7 +480,6 @@ describe("ProcessMessage circuit", function test() {

pollId = maciState.deployPoll(
BigInt(Math.floor(Date.now() / 1000) + duration),
maxValues,
treeDepths,
messageBatchSize,
coordinatorKeypair,
Expand Down Expand Up @@ -556,7 +549,10 @@ describe("ProcessMessage circuit", function test() {

it("should produce the correct state root and ballot root", async () => {
// The current roots
const emptyBallot = new Ballot(poll.maxValues.maxVoteOptions, poll.treeDepths.voteOptionTreeDepth);
const emptyBallot = new Ballot(
MESSAGE_TREE_ARITY ** poll.treeDepths.voteOptionTreeDepth,
poll.treeDepths.voteOptionTreeDepth,
);
const emptyBallotHash = emptyBallot.hash();
const ballotTree = new IncrementalQuinTree(STATE_TREE_DEPTH, emptyBallot.hash(), STATE_TREE_ARITY, hash2);

Expand Down Expand Up @@ -603,7 +599,6 @@ describe("ProcessMessage circuit", function test() {

pollId = maciState.deployPoll(
BigInt(Math.floor(Date.now() / 1000) + duration),
maxValues,
treeDepths,
messageBatchSize,
coordinatorKeypair,
Expand Down Expand Up @@ -678,7 +673,10 @@ describe("ProcessMessage circuit", function test() {

it("should produce the correct state root and ballot root", async () => {
// The current roots
const emptyBallot = new Ballot(poll.maxValues.maxVoteOptions, poll.treeDepths.voteOptionTreeDepth);
const emptyBallot = new Ballot(
MESSAGE_TREE_ARITY ** poll.treeDepths.voteOptionTreeDepth,
poll.treeDepths.voteOptionTreeDepth,
);
const emptyBallotHash = emptyBallot.hash();
const ballotTree = new IncrementalQuinTree(STATE_TREE_DEPTH, emptyBallot.hash(), STATE_TREE_ARITY, hash2);

Expand Down Expand Up @@ -728,7 +726,6 @@ describe("ProcessMessage circuit", function test() {

pollId = maciState.deployPoll(
BigInt(Math.floor(Date.now() / 1000) + duration),
maxValues,
treeDepths,
messageBatchSize,
coordinatorKeypair,
Expand Down Expand Up @@ -822,7 +819,10 @@ describe("ProcessMessage circuit", function test() {

it("should produce the correct state root and ballot root", async () => {
// The current roots
const emptyBallot = new Ballot(poll.maxValues.maxVoteOptions, poll.treeDepths.voteOptionTreeDepth);
const emptyBallot = new Ballot(
MESSAGE_TREE_ARITY ** poll.treeDepths.voteOptionTreeDepth,
poll.treeDepths.voteOptionTreeDepth,
);
const emptyBallotHash = emptyBallot.hash();
const ballotTree = new IncrementalQuinTree(STATE_TREE_DEPTH, emptyBallot.hash(), STATE_TREE_ARITY, hash2);

Expand Down
5 changes: 1 addition & 4 deletions circuits/ts/__tests__/TallyVotes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Keypair, PCommand, Message } from "maci-domainobjs";

import { ITallyVotesInputs } from "../types";

import { STATE_TREE_DEPTH, duration, maxValues, messageBatchSize, voiceCreditBalance } from "./utils/constants";
import { STATE_TREE_DEPTH, duration, messageBatchSize, voiceCreditBalance } from "./utils/constants";
import { generateRandomIndex, circomkitInstance } from "./utils/utils";

describe("TallyVotes circuit", function test() {
Expand Down Expand Up @@ -80,7 +80,6 @@ describe("TallyVotes circuit", function test() {

pollId = maciState.deployPoll(
BigInt(Math.floor(Date.now() / 1000) + duration),
maxValues,
treeDepths,
messageBatchSize,
coordinatorKeypair,
Expand Down Expand Up @@ -154,7 +153,6 @@ describe("TallyVotes circuit", function test() {

pollId = maciState.deployPoll(
BigInt(Math.floor(Date.now() / 1000) + duration),
maxValues,
treeDepths,
messageBatchSize,
coordinatorKeypair,
Expand Down Expand Up @@ -224,7 +222,6 @@ describe("TallyVotes circuit", function test() {

const pollId = maciState.deployPoll(
BigInt(Math.floor(Date.now() / 1000) + duration),
maxValues,
treeDepths,
messageBatchSize,
coordinatorKeypair,
Expand Down
5 changes: 0 additions & 5 deletions circuits/ts/__tests__/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
export const STATE_TREE_DEPTH = 10;
export const voiceCreditBalance = BigInt(100);
export const duration = 30;
export const maxValues = {
maxUsers: 25,
maxMessages: 25,
maxVoteOptions: 25,
};
export const treeDepths = {
intStateTreeDepth: 5,
messageTreeDepth: 2,
Expand Down
2 changes: 1 addition & 1 deletion circuits/ts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export interface IProcessMessagesInputs {
msgs: bigint[];
msgSubrootPathElements: bigint[][];
coordPrivKey: bigint;
coordPubKey: [bigint, bigint];
coordinatorPublicKeyHash: bigint;
encPubKeys: bigint[];
currentStateRoot: bigint;
currentStateLeaves: bigint[];
Expand Down
12 changes: 4 additions & 8 deletions cli/ts/commands/proveOnChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Verifier__factory as VerifierFactory,
} from "maci-contracts/typechain-types";
import { MESSAGE_TREE_ARITY, STATE_TREE_ARITY } from "maci-core";
import { G1Point, G2Point, hashLeftRight } from "maci-crypto";
import { G1Point, G2Point } from "maci-crypto";
import { VerifyingKey } from "maci-domainobjs";

import fs from "fs";
Expand Down Expand Up @@ -214,13 +214,9 @@ export const proveOnChain = async ({
logError("currentSbCommitment mismatch.");
}

const coordPubKeyHashOnChain = BigInt(await pollContract.coordinatorPubKeyHash());
if (
hashLeftRight(
BigInt((circuitInputs.coordPubKey as BigNumberish[])[0]),
BigInt((circuitInputs.coordPubKey as BigNumberish[])[1]),
).toString() !== coordPubKeyHashOnChain.toString()
) {
const coordPubKeyHashOnChain = await pollContract.coordinatorPubKeyHash();

if (circuitInputs.coordinatorPublicKeyHash.toString() !== coordPubKeyHashOnChain.toString()) {
logError("coordPubKey mismatch.");
}

Expand Down
Loading

0 comments on commit 898bc4e

Please sign in to comment.