Skip to content

Commit

Permalink
test(ceremony): update ceremony tests to work with v2 params and keys (
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlc03 committed Sep 4, 2024
1 parent 3428af2 commit 6bf1ffb
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nightly-ceremony.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
steps:
- uses: actions/checkout@v4
with:
ref: v1.2.2
ref: dev

- uses: pnpm/action-setup@v4
with:
Expand Down
214 changes: 107 additions & 107 deletions packages/circuits/ts/__tests__/CeremonyParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import { generateRandomIndex, circomkitInstance } from "./utils/utils";
describe("Ceremony param tests", () => {
const params = {
// processMessages and Tally
stateTreeDepth: 6,
stateTreeDepth: 14,
// processMessages
messageTreeDepth: 9,
// processMessages
messageBatchTreeDepth: 2,
// processMessages and Tally
voteOptionTreeDepth: 3,
// Tally
stateLeafBatchDepth: 2,
stateLeafBatchDepth: 5,
};

const treeDepths = {
intStateTreeDepth: params.messageBatchTreeDepth,
intStateTreeDepth: params.stateLeafBatchDepth,
messageTreeDepth: params.messageTreeDepth,
messageTreeSubDepth: params.messageBatchTreeDepth,
voteOptionTreeDepth: params.voteOptionTreeDepth,
Expand Down Expand Up @@ -71,7 +71,7 @@ describe("Ceremony param tests", () => {
circuit = await circomkitInstance.WitnessTester("processMessages", {
file: "./core/qv/processMessages",
template: "ProcessMessages",
params: [6, 9, 2, 3],
params: [14, 9, 2, 3],
});
});

Expand Down Expand Up @@ -175,117 +175,117 @@ describe("Ceremony param tests", () => {
expect(newBallotRoot?.toString()).not.to.be.eq(currentBallotRoot.toString());
});
});
});

describe("TallyVotes circuit", function test() {
this.timeout(900000);

let testCircuit: WitnessTester<
[
"stateRoot",
"ballotRoot",
"sbSalt",
"sbCommitment",
"index",
"numSignUps",
"currentTallyCommitment",
"newTallyCommitment",
"ballots",
"ballotPathElements",
"votes",
"currentResults",
"currentResultsRootSalt",
"currentSpentVoiceCreditSubtotal",
"currentSpentVoiceCreditSubtotalSalt",
"currentPerVOSpentVoiceCredits",
"currentPerVOSpentVoiceCreditsRootSalt",
"newResultsRootSalt",
"newPerVOSpentVoiceCreditsRootSalt",
"newSpentVoiceCreditSubtotalSalt",
]
>;

before(async () => {
testCircuit = await circomkitInstance.WitnessTester("tallyVotes", {
file: "./core/qv/tallyVotes",
template: "TallyVotes",
params: [6, 2, 3],
});
describe("TallyVotes circuit", function test() {
this.timeout(900000);

let testCircuit: WitnessTester<
[
"stateRoot",
"ballotRoot",
"sbSalt",
"sbCommitment",
"index",
"numSignUps",
"currentTallyCommitment",
"newTallyCommitment",
"ballots",
"ballotPathElements",
"votes",
"currentResults",
"currentResultsRootSalt",
"currentSpentVoiceCreditSubtotal",
"currentSpentVoiceCreditSubtotalSalt",
"currentPerVOSpentVoiceCredits",
"currentPerVOSpentVoiceCreditsRootSalt",
"newResultsRootSalt",
"newPerVOSpentVoiceCreditsRootSalt",
"newSpentVoiceCreditSubtotalSalt",
]
>;

before(async () => {
testCircuit = await circomkitInstance.WitnessTester("tallyVotes", {
file: "./core/qv/tallyVotes",
template: "TallyVotes",
params: [14, 5, 3],
});
});

describe("1 user, 2 messages", () => {
let stateIndex: bigint;
let pollId: bigint;
let poll: Poll;
let maciState: MaciState;
const voteWeight = BigInt(9);
const voteOptionIndex = BigInt(0);

beforeEach(() => {
maciState = new MaciState(params.stateTreeDepth);
const messages: Message[] = [];
const commands: PCommand[] = [];
// Sign up and publish
const userKeypair = new Keypair();
stateIndex = BigInt(
maciState.signUp(userKeypair.pubKey, voiceCreditBalance, BigInt(Math.floor(Date.now() / 1000))),
);

pollId = maciState.deployPoll(
BigInt(Math.floor(Date.now() / 1000) + duration),
treeDepths,
messageBatchSize,
coordinatorKeypair,
);

poll = maciState.polls.get(pollId)!;

// update the state
poll.updatePoll(BigInt(maciState.stateLeaves.length));

// First command (valid)
const command = new PCommand(
stateIndex,
userKeypair.pubKey,
voteOptionIndex, // voteOptionIndex,
voteWeight, // vote weight
BigInt(1), // nonce
BigInt(pollId),
);

const signature = command.sign(userKeypair.privKey);

const ecdhKeypair = new Keypair();
const sharedKey = Keypair.genEcdhSharedKey(ecdhKeypair.privKey, coordinatorKeypair.pubKey);
const message = command.encrypt(signature, sharedKey);
messages.push(message);
commands.push(command);

poll.publishMessage(message, ecdhKeypair.pubKey);

// Process messages
poll.processMessages(pollId);
});
describe("1 user, 2 messages", () => {
let stateIndex: bigint;
let pollId: bigint;
let poll: Poll;
let maciState: MaciState;
const voteWeight = BigInt(9);
const voteOptionIndex = BigInt(0);

it("should produce the correct result commitments", async () => {
const generatedInputs = poll.tallyVotes() as unknown as ITallyVotesInputs;
const witness = await testCircuit.calculateWitness(generatedInputs);
await testCircuit.expectConstraintPass(witness);
});
beforeEach(() => {
maciState = new MaciState(params.stateTreeDepth);
const messages: Message[] = [];
const commands: PCommand[] = [];
// Sign up and publish
const userKeypair = new Keypair();
stateIndex = BigInt(
maciState.signUp(userKeypair.pubKey, voiceCreditBalance, BigInt(Math.floor(Date.now() / 1000))),
);

it("should produce the correct result if the initial tally is not zero", async () => {
const generatedInputs = poll.tallyVotes() as unknown as ITallyVotesInputs;
pollId = maciState.deployPoll(
BigInt(Math.floor(Date.now() / 1000) + duration),
treeDepths,
messageBatchSize,
coordinatorKeypair,
);

// Start the tally from non-zero value
let randIdx = generateRandomIndex(Object.keys(generatedInputs).length);
while (randIdx === 0) {
randIdx = generateRandomIndex(Object.keys(generatedInputs).length);
}
poll = maciState.polls.get(pollId)!;

generatedInputs.currentResults[randIdx] = 1n;
// update the state
poll.updatePoll(BigInt(maciState.stateLeaves.length));

const witness = await testCircuit.calculateWitness(generatedInputs);
await testCircuit.expectConstraintPass(witness);
});
// First command (valid)
const command = new PCommand(
stateIndex,
userKeypair.pubKey,
voteOptionIndex, // voteOptionIndex,
voteWeight, // vote weight
BigInt(1), // nonce
BigInt(pollId),
);

const signature = command.sign(userKeypair.privKey);

const ecdhKeypair = new Keypair();
const sharedKey = Keypair.genEcdhSharedKey(ecdhKeypair.privKey, coordinatorKeypair.pubKey);
const message = command.encrypt(signature, sharedKey);
messages.push(message);
commands.push(command);

poll.publishMessage(message, ecdhKeypair.pubKey);

// Process messages
poll.processMessages(pollId);
});

it("should produce the correct result commitments", async () => {
const generatedInputs = poll.tallyVotes() as unknown as ITallyVotesInputs;
const witness = await testCircuit.calculateWitness(generatedInputs);
await testCircuit.expectConstraintPass(witness);
});

it("should produce the correct result if the initial tally is not zero", async () => {
const generatedInputs = poll.tallyVotes() as unknown as ITallyVotesInputs;

// Start the tally from non-zero value
let randIdx = generateRandomIndex(Object.keys(generatedInputs).length);
while (randIdx === 0) {
randIdx = generateRandomIndex(Object.keys(generatedInputs).length);
}

generatedInputs.currentResults[randIdx] = 1n;

const witness = await testCircuit.calculateWitness(generatedInputs);
await testCircuit.expectConstraintPass(witness);
});
});
});
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/tests/ceremony-params/ceremonyParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ import {
} from "../constants";
import { clean, isArm } from "../utils";

describe("Stress tests with ceremony params (6,9,2,3)", function test() {
describe("Stress tests with ceremony params (14,9,2,3)", function test() {
const messageTreeDepth = 9;
const stateTreeDepth = 6;
const stateTreeDepth = 14;
const voteOptionTreeDepth = 3;
const messageBatchDepth = 2;
const intStateTreeDepth = 2;
const intStateTreeDepth = 5;

const pollDuration = 60000;

Expand Down
34 changes: 16 additions & 18 deletions packages/cli/tests/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,32 @@ export const testProcessMessagesWasmPath =
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 ceremonyProcessMessagesZkeyPath = "./zkeys/ProcessMessages_6-9-2-3/processMessages_6-9-2-3.zkey";
export const ceremonyProcessMessagesZkeyPath = "./zkeys/ProcessMessages_14-9-2-3/processMessages_14-9-2-3.zkey";
export const ceremonyProcessMessagesNonQvZkeyPath =
"./zkeys/ProcessMessagesNonQv_6-9-2-3/processMessagesNonQv_6-9-2-3.zkey";
export const ceremonyTallyVotesZkeyPath = "./zkeys/TallyVotes_6-2-3/tallyVotes_6-2-3.zkey";
export const ceremonyTallyVotesNonQvZkeyPath = "./zkeys/TallyVotesNonQv_6-2-3/tallyVotesNonQv_6-2-3.zkey";
"./zkeys/ProcessMessagesNonQv_14-9-2-3/processMessagesNonQv_14-9-2-3.zkey";
export const ceremonyTallyVotesZkeyPath = "./zkeys/TallyVotes_14-5-3/tallyVotes_14-5-3.zkey";
export const ceremonyTallyVotesNonQvZkeyPath = "./zkeys/TallyVotesNonQv_14-5-3/tallyVotesNonQv_14-5-3.zkey";
export const ceremonyProcessMessagesWitnessPath =
"./zkeys/ProcessMessages_6-9-2-3/ProcessMessages_6-9-2-3_cpp/ProcessMessages_6-9-2-3";
"./zkeys/ProcessMessages_14-9-2-3/ProcessMessages_14-9-2-3_cpp/ProcessMessages_14-9-2-3";
export const ceremonyProcessMessagesNonQvWitnessPath =
"./zkeys/ProcessMessagesNonQv_6-9-2-3/ProcessMessagesNonQv_6-9-2-3_cpp/ProcessMessagesNonQv_6-9-2-3";
"./zkeys/ProcessMessagesNonQv_14-9-2-3/ProcessMessagesNonQv_14-9-2-3_cpp/ProcessMessagesNonQv_14-9-2-3";
export const ceremonyProcessMessagesDatPath =
"./zkeys/ProcessMessages_6-9-2-3/ProcessMessages_6-9-2-3_cpp/ProcessMessages_6-9-2-3.dat";
"./zkeys/ProcessMessages_14-9-2-3/ProcessMessages_14-9-2-3_cpp/ProcessMessages_14-9-2-3.dat";
export const ceremonyProcessMessagesNonQvDatPath =
"./zkeys/ProcessMessagesNonQv_6-9-2-3/ProcessMessagesNonQv_6-9-2-3_cpp/ProcessMessagesNonQv_6-9-2-3.dat";
export const ceremonyTallyVotesWitnessPath = "./zkeys/TallyVotes_6-2-3/TallyVotes_6-2-3_cpp/TallyVotes_6-2-3";
"./zkeys/ProcessMessagesNonQv_14-9-2-3/ProcessMessagesNonQv_14-9-2-3_cpp/ProcessMessagesNonQv_14-9-2-3.dat";
export const ceremonyTallyVotesWitnessPath = "./zkeys/TallyVotes_14-5-3/TallyVotes_14-5-3_cpp/TallyVotes_14-5-3";
export const ceremonyTallyVotesNonQvWitnessPath =
"./zkeys/TallyVotesNonQv_6-2-3/TallyVotesNonQv_6-2-3_cpp/TallyVotesNonQv_6-2-3";
export const ceremonyTallyVotesDatPath = "./zkeys/TallyVotes_6-2-3/TallyVotes_6-2-3_cpp/TallyVotes_6-2-3.dat";
"./zkeys/TallyVotesNonQv_14-5-3/TallyVotesNonQv_14-5-3_cpp/TallyVotesNonQv_14-5-3";
export const ceremonyTallyVotesDatPath = "./zkeys/TallyVotes_14-5-3/TallyVotes_14-5-3_cpp/TallyVotes_14-5-3.dat";
export const ceremonyTallyVotesNonQvDatPath =
"./zkeys/TallyVotesNonQv_6-2-3/TallyVotesNonQv_6-2-3_cpp/TallyVotesNonQv_6-2-3.dat";
"./zkeys/TallyVotesNonQv_14-5-3/TallyVotesNonQv_14-5-3_cpp/TallyVotesNonQv_14-5-3.dat";
export const ceremonyProcessMessagesWasmPath =
"./zkeys/ProcessMessages_6-9-2-3/ProcessMessages_6-9-2-3_js/ProcessMessages_6-9-2-3.wasm";
"./zkeys/ProcessMessages_14-9-2-3/ProcessMessages_14-9-2-3_js/ProcessMessages_14-9-2-3.wasm";
export const ceremonyProcessMessagesNonQvWasmPath =
"./zkeys/ProcessMessagesNonQv_6-9-2-3/ProcessMessagesNonQv_6-9-2-3_js/ProcessMessagesNonQv_6-9-2-3.wasm";
export const ceremonyTallyVotesWasmPath = "./zkeys/TallyVotes_6-2-3/TallyVotes_6-2-3_js/TallyVotes_6-2-3.wasm";
"./zkeys/ProcessMessagesNonQv_14-9-2-3/ProcessMessagesNonQv_14-9-2-3_js/ProcessMessagesNonQv_14-9-2-3.wasm";
export const ceremonyTallyVotesWasmPath = "./zkeys/TallyVotes_14-5-3/TallyVotes_14-5-3_js/TallyVotes_14-5-3.wasm";
export const ceremonyTallyVotesNonQvWasmPath =
"./zkeys/TallyVotesNonQv_6-2-3/TallyVotesNonQv_6-2-3_js/TallyVotesNonQv_6-2-3.wasm";
"./zkeys/TallyVotesNonQv_14-5-3/TallyVotesNonQv_14-5-3_js/TallyVotesNonQv_14-5-3.wasm";
export const testProcessMessagesNonQvWitnessPath =
"./zkeys/ProcessMessagesNonQv_10-2-1-2_test/ProcessMessagesNonQv_10-2-1-2_test_cpp/ProcessMessagesNonQv_10-2-1-2_test";
export const testProcessMessagesNonQvWitnessDatPath =
Expand All @@ -84,8 +84,6 @@ export const testTallyVotesNonQvWasmPath =
"./zkeys/TallyVotesNonQv_10-1-2_test/TallyVotesNonQv_10-1-2_test_js/TallyVotesNonQv_10-1-2_test.wasm";

export const pollDuration = 90;
export const maxMessages = 25;
export const maxVoteOptions = 25;

export const setVerifyingKeysArgs: Omit<SetVerifyingKeysArgs, "signer"> = {
quiet: true,
Expand Down

0 comments on commit 6bf1ffb

Please sign in to comment.