Skip to content

Commit

Permalink
tests(key-change): Start adding tests to assess the current key-chang…
Browse files Browse the repository at this point in the history
…e mechanism
  • Loading branch information
ctrlc03 committed Dec 8, 2023
1 parent 2ce6f5c commit dea1c25
Show file tree
Hide file tree
Showing 3 changed files with 438 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"postbuild": "cp package.json ./build",
"test": "ts-mocha --exit tests/*.test.ts",
"test:e2e": "ts-mocha --exit tests/e2e.test.ts",
"test:e2e-subsidy": "ts-mocha --exit tests/e2e.subsidy.test.ts"
"test:e2e-subsidy": "ts-mocha --exit tests/e2e.subsidy.test.ts",
"test:keyChange": "ts-mocha --exit tests/keyChange.test.ts"
},
"dependencies": {
"@nomicfoundation/hardhat-toolbox": "^4.0.0",
Expand Down
322 changes: 322 additions & 0 deletions cli/tests/keyChange.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,322 @@
import { isArm } from "maci-circuits";
import {
deploy,
deployPoll,
deployVkRegistryContract,
genProofs,
mergeMessages,
mergeSignups,
proveOnChain,
publish,
setVerifyingKeys,
signup,
timeTravel,
verify,
} from "../ts/commands";
import {
INT_STATE_TREE_DEPTH,
MSG_BATCH_DEPTH,
MSG_TREE_DEPTH,
STATE_TREE_DEPTH,
VOTE_OPTION_TREE_DEPTH,
coordinatorPrivKey,
coordinatorPubKey,
processMessageTestZkeyPath,
tallyVotesTestZkeyPath,
testProcessMessagesWasmPath,
testProcessMessagesWitnessPath,
testProofsDirPath,
testRapidsnarkPath,
testTallyFilePath,
testTallyVotesWasmPath,
testTallyVotesWitnessPath,
} from "./constants";
import { Keypair } from "maci-domainobjs";
import { cleanVanilla } from "./utils";
import { readFileSync } from "fs";
import { expect } from "chai";

describe("keyChange tests", function () {
const useWasm = isArm();
this.timeout(900000);

// before all tests we deploy the vk registry contract and set the verifying keys
before(async () => {
// we deploy the vk registry contract
await deployVkRegistryContract(true);
// we set the verifying keys
await setVerifyingKeys(
STATE_TREE_DEPTH,
INT_STATE_TREE_DEPTH,
MSG_TREE_DEPTH,
VOTE_OPTION_TREE_DEPTH,
MSG_BATCH_DEPTH,
processMessageTestZkeyPath,
tallyVotesTestZkeyPath,
);
});

describe("keyChange and new vote (new vote has same nonce)", () => {
after(async () => {
cleanVanilla();
});
const keypair1 = new Keypair();
const keypair2 = new Keypair();
const initialNonce = 1;
const initialVoteOption = 0;
const initialVoteAmount = 9;
const pollId = 0;
let stateIndex = 0;
const expectedTally = initialVoteAmount - 1;
const expectedPerVOteOptionTally = (initialVoteAmount - 1) ** 2;

before(async () => {
// deploy the smart contracts
await deploy(STATE_TREE_DEPTH);
// deploy a poll contract
await deployPoll(
90,
25,
25,
INT_STATE_TREE_DEPTH,
MSG_BATCH_DEPTH,
MSG_TREE_DEPTH,
VOTE_OPTION_TREE_DEPTH,
coordinatorPubKey,
);
stateIndex = parseInt(await signup(keypair1.pubKey.serialize()));
await publish(
keypair1.pubKey.serialize(),
stateIndex,
initialVoteOption,
initialNonce,
pollId,
initialVoteAmount,
undefined,
undefined,
keypair1.privKey.serialize(),
);
});
it("should publish a message to change the user maci key and cast a new vote", async () => {
await publish(
keypair2.pubKey.serialize(),
stateIndex,
initialVoteOption,
initialNonce,
pollId,
initialVoteAmount - 1,
undefined,
undefined,
keypair1.privKey.serialize(),
);
});
it("should generate zk-SNARK proofs and verify them", async () => {
await timeTravel(90, true);
await mergeMessages(0, undefined, undefined, true);
await mergeSignups(0, undefined, undefined, true);
await genProofs(
testProofsDirPath,
testTallyFilePath,
tallyVotesTestZkeyPath,
processMessageTestZkeyPath,
0,
undefined,
undefined,
testRapidsnarkPath,
testProcessMessagesWitnessPath,
testTallyVotesWitnessPath,
undefined,
coordinatorPrivKey,
undefined,
undefined,
testProcessMessagesWasmPath,
testTallyVotesWasmPath,
undefined,
useWasm,
);
await proveOnChain("0", testProofsDirPath);
await verify("0", testTallyFilePath);
});
it("should confirm the tally is correct", () => {
const tallyData = JSON.parse(readFileSync(testTallyFilePath).toString());
expect(tallyData.results.tally[0]).to.equal(expectedTally.toString());
expect(tallyData.perVOSpentVoiceCredits.tally[0]).to.equal(expectedPerVOteOptionTally.toString());
});
});

describe("keyChange and new vote (new vote has greater nonce and different vote option)", () => {
after(async () => {
cleanVanilla();
});
const keypair1 = new Keypair();
const keypair2 = new Keypair();
const initialNonce = 1;
const initialVoteOption = 0;
const initialVoteAmount = 9;
const pollId = 0;
let stateIndex = 0;
const expectedTally = initialVoteAmount;
const expectedPerVOteOptionTally = initialVoteAmount ** 2;

before(async () => {
// deploy the smart contracts
await deploy(STATE_TREE_DEPTH);
// deploy a poll contract
await deployPoll(
90,
25,
25,
INT_STATE_TREE_DEPTH,
MSG_BATCH_DEPTH,
MSG_TREE_DEPTH,
VOTE_OPTION_TREE_DEPTH,
coordinatorPubKey,
);
stateIndex = parseInt(await signup(keypair1.pubKey.serialize()));
await publish(
keypair1.pubKey.serialize(),
stateIndex,
initialVoteOption,
initialNonce,
pollId,
initialVoteAmount,
undefined,
undefined,
keypair1.privKey.serialize(),
);
});
it("should publish a message to change the user maci key and cast a new vote", async () => {
await publish(
keypair2.pubKey.serialize(),
stateIndex,
initialVoteOption + 1,
initialNonce + 1,
pollId,
initialVoteAmount - 1,
undefined,
undefined,
keypair1.privKey.serialize(),
);
});
it("should generate zk-SNARK proofs and verify them", async () => {
await timeTravel(90, true);
await mergeMessages(0, undefined, undefined, true);
await mergeSignups(0, undefined, undefined, true);
await genProofs(
testProofsDirPath,
testTallyFilePath,
tallyVotesTestZkeyPath,
processMessageTestZkeyPath,
0,
undefined,
undefined,
testRapidsnarkPath,
testProcessMessagesWitnessPath,
testTallyVotesWitnessPath,
undefined,
coordinatorPrivKey,
undefined,
undefined,
testProcessMessagesWasmPath,
testTallyVotesWasmPath,
undefined,
useWasm,
);
await proveOnChain("0", testProofsDirPath);
await verify("0", testTallyFilePath);
});
it("should confirm the tally is correct", () => {
const tallyData = JSON.parse(readFileSync(testTallyFilePath).toString());
expect(tallyData.results.tally[0]).to.equal(expectedTally.toString());
expect(tallyData.perVOSpentVoiceCredits.tally[0]).to.equal(expectedPerVOteOptionTally.toString());
});
});

describe("keyChange and new vote (new vote has same nonce and different vote option)", () => {
after(async () => {
cleanVanilla();
});
const keypair1 = new Keypair();
const keypair2 = new Keypair();
const initialNonce = 1;
const initialVoteOption = 0;
const initialVoteAmount = 9;
const pollId = 0;
let stateIndex = 0;
const expectedTally = initialVoteAmount - 3;
const expectedPerVOteOptionTally = (initialVoteAmount - 3) ** 2;

before(async () => {
// deploy the smart contracts
await deploy(STATE_TREE_DEPTH);
// deploy a poll contract
await deployPoll(
90,
25,
25,
INT_STATE_TREE_DEPTH,
MSG_BATCH_DEPTH,
MSG_TREE_DEPTH,
VOTE_OPTION_TREE_DEPTH,
coordinatorPubKey,
);
stateIndex = parseInt(await signup(keypair1.pubKey.serialize()));
await publish(
keypair1.pubKey.serialize(),
stateIndex,
initialVoteOption,
initialNonce,
pollId,
initialVoteAmount,
undefined,
undefined,
keypair1.privKey.serialize(),
);
});
it("should publish a message to change the user maci key, and a new vote", async () => {
await publish(
keypair2.pubKey.serialize(),
stateIndex,
initialVoteOption + 2,
initialNonce,
pollId,
initialVoteAmount - 3,
undefined,
undefined,
keypair1.privKey.serialize(),
);
});
it("should generate zk-SNARK proofs and verify them", async () => {
await timeTravel(90, true);
await mergeMessages(0, undefined, undefined, true);
await mergeSignups(0, undefined, undefined, true);
await genProofs(
testProofsDirPath,
testTallyFilePath,
tallyVotesTestZkeyPath,
processMessageTestZkeyPath,
0,
undefined,
undefined,
testRapidsnarkPath,
testProcessMessagesWitnessPath,
testTallyVotesWitnessPath,
undefined,
coordinatorPrivKey,
undefined,
undefined,
testProcessMessagesWasmPath,
testTallyVotesWasmPath,
undefined,
useWasm,
);
await proveOnChain("0", testProofsDirPath);
await verify("0", testTallyFilePath);
});
it("should confirm the tally is correct", () => {
const tallyData = JSON.parse(readFileSync(testTallyFilePath).toString());
expect(tallyData.results.tally[2]).to.equal(expectedTally.toString());
expect(tallyData.perVOSpentVoiceCredits.tally[2]).to.equal(expectedPerVOteOptionTally.toString());
});
});
});
Loading

0 comments on commit dea1c25

Please sign in to comment.