Skip to content

Commit

Permalink
refactor(core): remove unnecessary messageAq from Poll
Browse files Browse the repository at this point in the history
  • Loading branch information
baumstern committed Dec 7, 2023
1 parent 22a4f28 commit d3930f7
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 162 deletions.
71 changes: 48 additions & 23 deletions circuits/ts/__tests__/ProcessMessages.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import fs from "fs";
import path from "path";

import { MaciState, packProcessMessageSmallVals } from "maci-core";
import { expect } from "chai";

import { MaciState, packProcessMessageSmallVals, STATE_TREE_ARITY } from "maci-core";
import { PrivKey, Keypair, PCommand, Message, Ballot } from "maci-domainobjs";

import { hash5, IncrementalQuinTree, stringifyBigInts, NOTHING_UP_MY_SLEEVE } from "maci-crypto";
import { AccQueue, hash5, IncrementalQuinTree, stringifyBigInts, NOTHING_UP_MY_SLEEVE } from "maci-crypto";

import { STATE_TREE_DEPTH, getSignal } from "./utils";
import path from "path";
import { expect } from "chai";

const tester = require("circom_tester").wasm;

const voiceCreditBalance = BigInt(100);
Expand Down Expand Up @@ -114,17 +114,27 @@ describe("ProcessMessage circuit", function () {
commands.push(command2);
poll.publishMessage(message2, ecdhKeypair2.pubKey);

poll.messageAq.mergeSubRoots(0);
poll.messageAq.merge(treeDepths.messageTreeDepth);
// Use the accumulator queue to compare the root of the message tree
const accumulatorQueue: AccQueue = new AccQueue(
treeDepths.messageTreeSubDepth,
STATE_TREE_ARITY,
NOTHING_UP_MY_SLEEVE,
);
accumulatorQueue.enqueue(message.hash(ecdhKeypair.pubKey));
accumulatorQueue.enqueue(message2.hash(ecdhKeypair2.pubKey));
accumulatorQueue.mergeSubRoots(0);
accumulatorQueue.merge(treeDepths.messageTreeDepth);

expect(poll.messageTree.root.toString()).to.be.eq(poll.messageAq.getRoot(treeDepths.messageTreeDepth).toString());
expect(poll.messageTree.root.toString()).to.be.eq(
accumulatorQueue.mainRoots[treeDepths.messageTreeDepth].toString(),
);
});

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 emptyBallotHash = emptyBallot.hash();
const ballotTree = new IncrementalQuinTree(STATE_TREE_DEPTH, emptyBallot.hash(), poll.STATE_TREE_ARITY, hash5);
const ballotTree = new IncrementalQuinTree(STATE_TREE_DEPTH, emptyBallot.hash(), STATE_TREE_ARITY, hash5);

ballotTree.insert(emptyBallot.hash());

Expand Down Expand Up @@ -224,18 +234,26 @@ describe("ProcessMessage circuit", function () {

poll.publishMessage(message, ecdhKeypair.pubKey);

// Merge
poll.messageAq.mergeSubRoots(0);
poll.messageAq.merge(treeDepths.messageTreeDepth);
// Use the accumulator queue to compare the root of the message tree
const accumulatorQueue: AccQueue = new AccQueue(
treeDepths.messageTreeSubDepth,
STATE_TREE_ARITY,
NOTHING_UP_MY_SLEEVE,
);
accumulatorQueue.enqueue(message.hash(ecdhKeypair.pubKey));
accumulatorQueue.mergeSubRoots(0);
accumulatorQueue.merge(treeDepths.messageTreeDepth);

expect(poll.messageTree.root.toString()).to.be.eq(poll.messageAq.getRoot(treeDepths.messageTreeDepth).toString());
expect(poll.messageTree.root.toString()).to.be.eq(
accumulatorQueue.getRoot(treeDepths.messageTreeDepth).toString(),
);
});

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 emptyBallotHash = emptyBallot.hash();
const ballotTree = new IncrementalQuinTree(STATE_TREE_DEPTH, emptyBallot.hash(), poll.STATE_TREE_ARITY, hash5);
const ballotTree = new IncrementalQuinTree(STATE_TREE_DEPTH, emptyBallot.hash(), STATE_TREE_ARITY, hash5);

ballotTree.insert(emptyBallot.hash());

Expand Down Expand Up @@ -353,18 +371,28 @@ describe("ProcessMessage circuit", function () {
commands.push(command3);
poll.publishMessage(message3, ecdhKeypair3.pubKey);

// Merge
poll.messageAq.mergeSubRoots(0);
poll.messageAq.merge(treeDepths.messageTreeDepth);

expect(poll.messageTree.root.toString()).to.be.eq(poll.messageAq.getRoot(treeDepths.messageTreeDepth).toString());
// Use the accumulator queue to compare the root of the message tree
const accumulatorQueue: AccQueue = new AccQueue(
treeDepths.messageTreeSubDepth,
STATE_TREE_ARITY,
NOTHING_UP_MY_SLEEVE,
);
accumulatorQueue.enqueue(message.hash(ecdhKeypair.pubKey));
accumulatorQueue.enqueue(message2.hash(ecdhKeypair2.pubKey));
accumulatorQueue.enqueue(message3.hash(ecdhKeypair3.pubKey));
accumulatorQueue.mergeSubRoots(0);
accumulatorQueue.merge(treeDepths.messageTreeDepth);

expect(poll.messageTree.root.toString()).to.be.eq(
accumulatorQueue.getRoot(treeDepths.messageTreeDepth).toString(),
);
});

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 emptyBallotHash = emptyBallot.hash();
const ballotTree = new IncrementalQuinTree(STATE_TREE_DEPTH, emptyBallot.hash(), poll.STATE_TREE_ARITY, hash5);
const ballotTree = new IncrementalQuinTree(STATE_TREE_DEPTH, emptyBallot.hash(), STATE_TREE_ARITY, hash5);

ballotTree.insert(NOTHING_UP_MY_SLEEVE);

Expand Down Expand Up @@ -433,9 +461,6 @@ describe("ProcessMessage circuit", function () {
poll.publishMessage(message, ecdhKeypair.pubKey);
}

poll.messageAq.mergeSubRoots(0);
poll.messageAq.merge(treeDepths.messageTreeDepth);

for (let i = 0; i < NUM_BATCHES; i++) {
const generatedInputs = poll.processMessages(pollId);

Expand Down
31 changes: 19 additions & 12 deletions circuits/ts/__tests__/TallyVotes.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { STATE_TREE_DEPTH } from "./utils";

import { MaciState } from "maci-core";

import { Keypair, PCommand, Message } from "maci-domainobjs";

import path from "path";
import { expect } from "chai";
import { beforeEach } from "mocha";

import { Keypair, PCommand, Message } from "maci-domainobjs";
import { MaciState, STATE_TREE_ARITY } from "maci-core";
import { AccQueue, NOTHING_UP_MY_SLEEVE } from "maci-crypto";

import { STATE_TREE_DEPTH } from "./utils";

const tester = require("circom_tester").wasm;

const voiceCreditBalance = BigInt(100);
Expand Down Expand Up @@ -85,10 +86,19 @@ describe("TallyVotes circuit", function () {

poll.publishMessage(message, ecdhKeypair.pubKey);

poll.messageAq.mergeSubRoots(0);
poll.messageAq.merge(treeDepths.messageTreeDepth);
// Use the accumulator queue to compare the root of the message tree
const accumulatorQueue: AccQueue = new AccQueue(
treeDepths.messageTreeSubDepth,
STATE_TREE_ARITY,
NOTHING_UP_MY_SLEEVE,
);
accumulatorQueue.enqueue(message.hash(ecdhKeypair.pubKey));
accumulatorQueue.mergeSubRoots(0);
accumulatorQueue.merge(treeDepths.messageTreeDepth);

expect(poll.messageTree.root.toString()).to.be.eq(poll.messageAq.getRoot(treeDepths.messageTreeDepth).toString());
expect(poll.messageTree.root.toString()).to.be.eq(
accumulatorQueue.mainRoots[treeDepths.messageTreeDepth].toString(),
);
// Process messages
poll.processMessages();
});
Expand Down Expand Up @@ -200,9 +210,6 @@ describe("TallyVotes circuit", function () {
poll.publishMessage(message, ecdhKeypair.pubKey);
}

poll.messageAq.mergeSubRoots(0);
poll.messageAq.merge(treeDepths.messageTreeDepth);

for (let i = 0; i < NUM_BATCHES; i++) {
poll.processMessages(pollId);
}
Expand Down
4 changes: 1 addition & 3 deletions cli/ts/commands/genProofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { Keypair, PrivKey, VerifyingKey } from "maci-domainobjs";
import { extractVk, genProof, verifyProof } from "maci-circuits";
import { genMaciStateFromContract, getDefaultSigner, parseArtifact } from "maci-contracts";
import { Contract } from "ethers";
import { MaciState, Poll } from "maci-core";
import { MaciState } from "maci-core";
import { hash3, hashLeftRight, genTreeCommitment } from "maci-crypto";
import { join } from "path";

Expand Down Expand Up @@ -184,8 +184,6 @@ export const genProofs = async (
} catch (error: any) {
logError(error.message);
}
// ensure we merge all messages
maciState.polls.forEach((poll: Poll) => poll.mergeAllMessages());
} else {
maciState = await genMaciStateFromContract(
signer.provider,
Expand Down
22 changes: 9 additions & 13 deletions contracts/tests/MACI.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { utils, Contract } from "ethers";
import { timeTravel } from "./utils";
import { parseArtifact, getDefaultSigner } from "../ts/deploy";
import { deployTestContracts } from "../ts/utils";
import { PCommand, VerifyingKey, Keypair, PubKey, Message } from "maci-domainobjs";
import { expect } from "chai";

import { PCommand, VerifyingKey, Keypair, PubKey, Message } from "maci-domainobjs";
import {
MaciState,
genProcessVkSig,
Expand All @@ -12,10 +10,12 @@ import {
packProcessMessageSmallVals,
packTallyVotesSmallVals,
} from "maci-core";
import { expect } from "chai";

import { G1Point, G2Point, NOTHING_UP_MY_SLEEVE } from "maci-crypto";

import { timeTravel } from "./utils";
import { parseArtifact, getDefaultSigner } from "../ts/deploy";
import { deployTestContracts } from "../ts/utils";

const STATE_TREE_DEPTH = 10;
const STATE_TREE_ARITY = 5;
const MESSAGE_TREE_DEPTH = 4;
Expand Down Expand Up @@ -404,17 +404,13 @@ describe("MACI", () => {
tx = await pollContract.mergeMessageAq({ gasLimit: 4000000 });
receipt = await tx.wait();
expect(receipt.status).to.eq(1);

const poll = maciState.polls[pollId];
poll.messageAq.mergeSubRoots(0);
poll.messageAq.merge(MESSAGE_TREE_DEPTH);
});

it("the message root must be correct", async () => {
const onChainMessageRoot = await messageAqContract.getMainRoot(MESSAGE_TREE_DEPTH);
expect(onChainMessageRoot.toString()).to.eq(
maciState.polls[pollId].messageAq.mainRoots[MESSAGE_TREE_DEPTH].toString(),
);
const offChainMessageRoot = maciState.polls[pollId].messageTree.root;

expect(onChainMessageRoot.toString()).to.eq(offChainMessageRoot.toString());
});
});

Expand Down
6 changes: 1 addition & 5 deletions contracts/ts/genMaciState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,8 @@ const genMaciStateFromContract = async (
maciState.polls[pollId].publishMessage(action.data.message, action.data.encPubKey);
} else if (action["type"] === "TopupMessage") {
maciState.polls[pollId].topupMessage(action.data.message);
} else if (action["type"] === "MergeMessageAqSubRoots") {
maciState.polls[pollId].messageAq.mergeSubRoots(action.data.numSrQueueOps);
} else if (action["type"] === "MergeMessageAq") {
maciState.polls[pollId].messageAq.merge(treeDepths.messageTreeDepth);
const poll = maciState.polls[pollId];
assert(poll.messageAq.mainRoots[treeDepths.messageTreeDepth] === action.data.messageRoot);
assert(maciState.polls[pollId].messageTree.root === action.data.messageRoot);
}
}

Expand Down
12 changes: 4 additions & 8 deletions core/ts/MaciState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import { PubKey, Keypair, StateLeaf } from "maci-domainobjs";

import { Poll } from "./Poll";
import { TreeDepths, MaxValues } from "./utils/utils";
import { STATE_TREE_ARITY } from "./utils/constants";

// todo: organize this in domainobjs
const blankStateLeaf = StateLeaf.genBlankLeaf();
const blankStateLeafHash = blankStateLeaf.hash();
import { STATE_TREE_ARITY, BlankStateLeaf, BlankStateLeafHash } from "./utils/constants";

/*
* File Overview:
Expand Down Expand Up @@ -49,10 +45,10 @@ class MaciState implements IMaciState {

constructor(_stateTreeDepth: number) {
this.stateTreeDepth = _stateTreeDepth;
this.stateTree = new IncrementalQuinTree(this.stateTreeDepth, blankStateLeafHash, STATE_TREE_ARITY, hash5);
this.stateTree = new IncrementalQuinTree(this.stateTreeDepth, BlankStateLeafHash, STATE_TREE_ARITY, hash5);

this.stateLeaves.push(blankStateLeaf);
this.stateTree.insert(blankStateLeafHash);
this.stateLeaves.push(BlankStateLeaf);
this.stateTree.insert(BlankStateLeafHash);
}

public signUp(_pubKey: PubKey, _initialVoiceCreditBalance: bigint, _timestamp: bigint): number {
Expand Down
Loading

0 comments on commit d3930f7

Please sign in to comment.