diff --git a/core/ts/Poll.ts b/core/ts/Poll.ts index bf18cac730..12e1a6677e 100644 --- a/core/ts/Poll.ts +++ b/core/ts/Poll.ts @@ -109,7 +109,7 @@ export class Poll implements IPoll { numBatchesTallied = 0; - totalSpentVoiceCredits = BigInt(0); + totalSpentVoiceCredits = 0n; // For coefficient and subsidy calculation subsidy: bigint[] = []; // size: M, M is number of vote options @@ -163,9 +163,9 @@ export class Poll implements IPoll { hash5, ); - this.tallyResult = new Array(this.maxValues.maxVoteOptions).fill(BigInt(0)) as bigint[]; - this.perVOSpentVoiceCredits = new Array(this.maxValues.maxVoteOptions).fill(BigInt(0)) as bigint[]; - this.subsidy = new Array(this.maxValues.maxVoteOptions).fill(BigInt(0)) as bigint[]; + this.tallyResult = new Array(this.maxValues.maxVoteOptions).fill(0n) as bigint[]; + this.perVOSpentVoiceCredits = new Array(this.maxValues.maxVoteOptions).fill(0n) as bigint[]; + this.subsidy = new Array(this.maxValues.maxVoteOptions).fill(0n) as bigint[]; // we put a blank state leaf to prevent a DoS attack this.emptyBallot = Ballot.genBlankBallot(this.maxValues.maxVoteOptions, treeDepths.voteOptionTreeDepth); @@ -214,7 +214,7 @@ export class Poll implements IPoll { // If the state tree index in the command is invalid, do nothing if ( stateLeafIndex >= BigInt(this.ballots.length) || - stateLeafIndex < BigInt(1) || + stateLeafIndex < 1n || stateLeafIndex >= BigInt(this.stateTree?.nextIndex || -1) ) { throw new ProcessMessageError(ProcessMessageErrors.InvalidStateLeafIndex); @@ -232,12 +232,12 @@ export class Poll implements IPoll { } // If the nonce is invalid, do nothing - if (command.nonce !== BigInt(`${ballot.nonce}`) + BigInt(1)) { + if (command.nonce !== BigInt(`${ballot.nonce}`) + 1n) { throw new ProcessMessageError(ProcessMessageErrors.InvalidNonce); } // If the vote option index is invalid, do nothing - if (command.voteOptionIndex < BigInt(0) || command.voteOptionIndex >= BigInt(this.maxValues.maxVoteOptions)) { + if (command.voteOptionIndex < 0n || command.voteOptionIndex >= BigInt(this.maxValues.maxVoteOptions)) { throw new ProcessMessageError(ProcessMessageErrors.InvalidVoteOptionIndex); } @@ -257,7 +257,7 @@ export class Poll implements IPoll { BigInt(command.newVoteWeight) * BigInt(command.newVoteWeight); // If the remaining voice credits is insufficient, do nothing - if (voiceCreditsLeft < BigInt(0)) { + if (voiceCreditsLeft < 0n) { throw new ProcessMessageError(ProcessMessageErrors.InsufficientVoiceCredits); } @@ -270,7 +270,7 @@ export class Poll implements IPoll { // Deep-copy the ballot and update its attributes const newBallot = ballot.copy(); // increase the nonce - newBallot.nonce = BigInt(`${newBallot.nonce}`) + BigInt(1); + newBallot.nonce = BigInt(`${newBallot.nonce}`) + 1n; // we change the vote for this exact vote option newBallot.votes[voteOptionIndex] = command.newVoteWeight; @@ -283,7 +283,7 @@ export class Poll implements IPoll { const originalBallotPathElements = this.ballotTree?.genMerklePath(Number(stateLeafIndex)).pathElements; // create a new quinary tree where we insert the votes of the origin (up until this message is processed) ballot - const vt = new IncrementalQuinTree(this.treeDepths.voteOptionTreeDepth, BigInt(0), STATE_TREE_ARITY, hash5); + const vt = new IncrementalQuinTree(this.treeDepths.voteOptionTreeDepth, 0n, STATE_TREE_ARITY, hash5); for (let i = 0; i < this.ballots[0].votes.length; i += 1) { vt.insert(ballot.votes[i]); } @@ -317,7 +317,7 @@ export class Poll implements IPoll { * @param message - The message to top up the voice credit balance */ topupMessage = (message: Message): void => { - assert(message.msgType === BigInt(2), "A Topup message must have msgType 2"); + assert(message.msgType === 2n, "A Topup message must have msgType 2"); message.data.forEach((d) => { assert(d < SNARK_FIELD_SIZE, "The message data is not in the correct range"); @@ -347,7 +347,7 @@ export class Poll implements IPoll { * @param encPubKey - The public key used to encrypt the message */ publishMessage = (message: Message, encPubKey: PubKey): void => { - assert(message.msgType === BigInt(1), "A vote or key change message must have msgType 1"); + assert(message.msgType === 1n, "A vote or key change message must have msgType 1"); assert( encPubKey.rawPubKey[0] < SNARK_FIELD_SIZE && encPubKey.rawPubKey[1] < SNARK_FIELD_SIZE, "The public key is not in the correct range", @@ -375,7 +375,7 @@ export class Poll implements IPoll { } catch (e) { // if there is an error we store an empty command const keyPair = new Keypair(); - const command = new PCommand(BigInt(0), keyPair.pubKey, BigInt(0), BigInt(0), BigInt(0), BigInt(0), BigInt(0)); + const command = new PCommand(0n, keyPair.pubKey, 0n, 0n, 0n, 0n, 0n); this.commands.push(command as ICommand); } }; @@ -449,7 +449,7 @@ export class Poll implements IPoll { } } - this.sbSalts[this.currentMessageBatchIndex] = BigInt(0); + this.sbSalts[this.currentMessageBatchIndex] = 0n; } // The starting index must be valid @@ -495,7 +495,7 @@ export class Poll implements IPoll { // based on the message type we have to process it differently switch (message.msgType) { - case BigInt(1): + case 1n: try { // check if the command is valid const r = this.processMessage(message, encPubKey); @@ -542,12 +542,12 @@ export class Poll implements IPoll { } } break; - case BigInt(2): + case 2n: try { // -------------------------------------- // generate topup circuit inputs - const stateIndex = Number(message.data[0] >= BigInt(this.ballots.length) ? BigInt(0) : message.data[0]); - const amount = message.data[0] >= BigInt(this.ballots.length) ? BigInt(0) : message.data[1]; + const stateIndex = Number(message.data[0] >= BigInt(this.ballots.length) ? 0n : message.data[0]); + const amount = message.data[0] >= BigInt(this.ballots.length) ? 0n : message.data[1]; currentStateLeaves.unshift(this.stateLeaves[stateIndex].copy()); currentStateLeavesPathElements.unshift(this.stateTree!.genMerklePath(stateIndex).pathElements); @@ -597,7 +597,7 @@ export class Poll implements IPoll { currentVoteWeights.unshift(this.ballots[0].votes[0]); // create a new quinary tree and add an empty vote - const vt = new IncrementalQuinTree(this.treeDepths.voteOptionTreeDepth, BigInt(0), STATE_TREE_ARITY, hash5); + const vt = new IncrementalQuinTree(this.treeDepths.voteOptionTreeDepth, 0n, STATE_TREE_ARITY, hash5); vt.insert(this.ballots[0].votes[0]); // get the path elements for this empty vote weight leaf @@ -728,9 +728,9 @@ export class Poll implements IPoll { /* eslint-disable no-bitwise */ const packedVals = BigInt(this.maxValues.maxVoteOptions) + - (BigInt(this.maciStateRef.numSignUps) << BigInt(50)) + - (BigInt(index) << BigInt(100)) + - (BigInt(batchEndIndex) << BigInt(150)); + (BigInt(this.maciStateRef.numSignUps) << 50n) + + (BigInt(index) << 100n) + + (BigInt(batchEndIndex) << 150n); /* eslint-enable no-bitwise */ return stringifyBigInts({ @@ -800,8 +800,8 @@ export class Poll implements IPoll { const sbCommitment = hash3([stateRoot, ballotRoot, sbSalt]); const currentSubsidy = this.subsidy.map((x) => BigInt(x.toString())); - let currentSubsidyCommitment = BigInt(0); - let currentSubsidySalt = BigInt(0); + let currentSubsidyCommitment = 0n; + let currentSubsidySalt = 0n; let saltIndex = this.previousSubsidyIndexToString(); if (this.rbi !== 0 || this.cbi !== 0) { @@ -900,7 +900,7 @@ export class Poll implements IPoll { * @returns Returns the calculated coefficient. */ private coefficientCalculation = (rowBallot: Ballot, colBallot: Ballot): bigint => { - let sum = BigInt(0); + let sum = 0n; for (let p = 0; p < this.maxValues.maxVoteOptions; p += 1) { sum += BigInt(rowBallot.votes[p].valueOf()) * colBallot.votes[p]; } @@ -965,10 +965,10 @@ export class Poll implements IPoll { const currentResultsRootSalt = batchStartIndex === 0 ? 0n : this.resultRootSalts[batchStartIndex - batchSize]; const currentPerVOSpentVoiceCreditsRootSalt = - batchStartIndex === 0 ? BigInt(0) : this.preVOSpentVoiceCreditsRootSalts[batchStartIndex - batchSize]; + batchStartIndex === 0 ? 0n : this.preVOSpentVoiceCreditsRootSalts[batchStartIndex - batchSize]; const currentSpentVoiceCreditSubtotalSalt = - batchStartIndex === 0 ? BigInt(0) : this.spentVoiceCreditSubtotalSalts[batchStartIndex - batchSize]; + batchStartIndex === 0 ? 0n : this.spentVoiceCreditSubtotalSalts[batchStartIndex - batchSize]; // generate a commitment to the current results const currentResultsCommitment = genTreeCommitment( @@ -998,7 +998,7 @@ export class Poll implements IPoll { // ]) const currentTallyCommitment = batchStartIndex === 0 - ? BigInt(0) + ? 0n : hash3([ currentResultsCommitment, currentSpentVoiceCreditsCommitment, @@ -1128,7 +1128,7 @@ export class Poll implements IPoll { * @returns Returns the hash of the total spent voice credits and a salt, computed as Poseidon([totalCredits, _salt]). */ private genSpentVoiceCreditSubtotalCommitment = (salt: bigint, numBallotsToCount: number): bigint => { - let subtotal = BigInt(0); + let subtotal = 0n; for (let i = 0; i < numBallotsToCount; i += 1) { if (this.ballots.length <= i) { break; diff --git a/core/ts/__tests__/MaciState.test.ts b/core/ts/__tests__/MaciState.test.ts index 709e9f1fb6..c5f3090b5c 100644 --- a/core/ts/__tests__/MaciState.test.ts +++ b/core/ts/__tests__/MaciState.test.ts @@ -7,7 +7,14 @@ import { MaciState } from "../MaciState"; import { STATE_TREE_DEPTH } from "../utils/constants"; import { IJsonMaciState } from "../utils/types"; -import { coordinatorKeypair, duration, maxValues, messageBatchSize, treeDepths, voiceCreditBalance } from "./constants"; +import { + coordinatorKeypair, + duration, + maxValues, + messageBatchSize, + treeDepths, + voiceCreditBalance, +} from "./utils/constants"; describe("MaciState", function test() { this.timeout(100000); @@ -34,15 +41,7 @@ describe("MaciState", function test() { messageBatchSize, coordinatorKeypair, ); - const command = new PCommand( - BigInt(0), - userKeypair.pubKey, - BigInt(0), - BigInt(0), - BigInt(0), - BigInt(pollId), - BigInt(0), - ); + const command = new PCommand(0n, userKeypair.pubKey, 0n, 0n, 0n, BigInt(pollId), 0n); const encKeypair = new Keypair(); const signature = command.sign(encKeypair.privKey); @@ -67,7 +66,7 @@ describe("MaciState", function test() { // modify user.voiceCreditBalance const m4 = m1.copy(); - m4.stateLeaves[0].voiceCreditBalance = BigInt(m4.stateLeaves[0].voiceCreditBalance) + BigInt(1); + m4.stateLeaves[0].voiceCreditBalance = BigInt(m4.stateLeaves[0].voiceCreditBalance) + 1n; expect(m1.equals(m4)).not.to.eq(true); // modify poll.coordinatorKeypair @@ -117,7 +116,7 @@ describe("MaciState", function test() { // modify poll.messages const m20 = m1.copy(); - m20.polls[pollId].messages[0].data[0] = BigInt(m20.polls[pollId].messages[0].data[0]) + BigInt(1); + m20.polls[pollId].messages[0].data[0] = BigInt(m20.polls[pollId].messages[0].data[0]) + 1n; expect(m1.equals(m20)).not.to.eq(true); // modify poll.encPubKeys diff --git a/core/ts/__tests__/Poll.test.ts b/core/ts/__tests__/Poll.test.ts index 1cec8680e2..32b37c80e7 100644 --- a/core/ts/__tests__/Poll.test.ts +++ b/core/ts/__tests__/Poll.test.ts @@ -5,7 +5,14 @@ import { MaciState } from "../MaciState"; import { Poll } from "../Poll"; import { STATE_TREE_DEPTH } from "../utils/constants"; -import { coordinatorKeypair, duration, maxValues, messageBatchSize, treeDepths, voiceCreditBalance } from "./constants"; +import { + coordinatorKeypair, + duration, + maxValues, + messageBatchSize, + treeDepths, + voiceCreditBalance, +} from "./utils/constants"; describe("Poll", function test() { this.timeout(90000); @@ -38,9 +45,9 @@ describe("Poll", function test() { // invalid state index as it is one more than the number of state leaves BigInt(user1StateIndex + 1), user1Keypair.pubKey, - BigInt(0), - BigInt(1), - BigInt(0), + 0n, + 1n, + 0n, BigInt(pollId), ); @@ -57,14 +64,7 @@ describe("Poll", function test() { }); it("should throw if a message has an invalid nonce", () => { - const command = new PCommand( - BigInt(user1StateIndex), - user1Keypair.pubKey, - BigInt(0), - BigInt(0), - BigInt(0), - BigInt(pollId), - ); + const command = new PCommand(BigInt(user1StateIndex), user1Keypair.pubKey, 0n, 0n, 0n, BigInt(pollId)); const signature = command.sign(user1Keypair.privKey); @@ -80,16 +80,9 @@ describe("Poll", function test() { }); it("should throw if a message has an invalid signature", () => { - const command = new PCommand( - BigInt(user1StateIndex), - user1Keypair.pubKey, - BigInt(0), - BigInt(0), - BigInt(0), - BigInt(pollId), - ); + const command = new PCommand(BigInt(user1StateIndex), user1Keypair.pubKey, 0n, 0n, 0n, BigInt(pollId)); - const signature = command.sign(new PrivKey(BigInt(0))); + const signature = command.sign(new PrivKey(0n)); const ecdhKeypair = new Keypair(); const sharedKey = Keypair.genEcdhSharedKey(ecdhKeypair.privKey, coordinatorKeypair.pubKey); @@ -105,10 +98,10 @@ describe("Poll", function test() { const command = new PCommand( BigInt(user1StateIndex), user1Keypair.pubKey, - BigInt(0), + 0n, // voice credits spent would be this value ** this value BigInt(Math.sqrt(Number.parseInt(voiceCreditBalance.toString(), 10)) + 1), - BigInt(1), + 1n, BigInt(pollId), ); @@ -131,8 +124,8 @@ describe("Poll", function test() { user1Keypair.pubKey, BigInt(maxValues.maxVoteOptions), // voice credits spent would be this value ** this value - BigInt(1), - BigInt(1), + 1n, + 1n, BigInt(pollId), ); @@ -150,14 +143,7 @@ describe("Poll", function test() { }); it("should throw if a message has an invalid vote option index (< 0)", () => { - const command = new PCommand( - BigInt(user1StateIndex), - user1Keypair.pubKey, - BigInt(-1), - BigInt(1), - BigInt(1), - BigInt(pollId), - ); + const command = new PCommand(BigInt(user1StateIndex), user1Keypair.pubKey, -1n, 1n, 1n, BigInt(pollId)); const signature = command.sign(user1Keypair.privKey); @@ -173,14 +159,7 @@ describe("Poll", function test() { }); it("should throw when passed a message that cannot be decrypted (wrong encPubKey)", () => { - const command = new PCommand( - BigInt(user1StateIndex), - user1Keypair.pubKey, - BigInt(0), - BigInt(1), - BigInt(1), - BigInt(pollId), - ); + const command = new PCommand(BigInt(user1StateIndex), user1Keypair.pubKey, 0n, 1n, 1n, BigInt(pollId)); const signature = command.sign(user1Keypair.privKey); @@ -196,14 +175,7 @@ describe("Poll", function test() { }); it("should throw when passed a corrupted message", () => { - const command = new PCommand( - BigInt(user1StateIndex), - user1Keypair.pubKey, - BigInt(0), - BigInt(1), - BigInt(1), - BigInt(pollId), - ); + const command = new PCommand(BigInt(user1StateIndex), user1Keypair.pubKey, 0n, 1n, 1n, BigInt(pollId)); const signature = command.sign(user1Keypair.privKey); @@ -213,7 +185,7 @@ describe("Poll", function test() { const message = command.encrypt(signature, sharedKey); poll.publishMessage(message, ecdhKeypair.pubKey); - message.data[0] = BigInt(0); + message.data[0] = 0n; expect(() => { poll.processMessage(message, user1Keypair.pubKey); @@ -242,14 +214,7 @@ describe("Poll", function test() { ); it("should throw if this is the first batch and currentMessageBatchIndex is defined", () => { - const command = new PCommand( - BigInt(user1StateIndex), - user1Keypair.pubKey, - BigInt(0), - BigInt(1), - BigInt(0), - BigInt(pollId), - ); + const command = new PCommand(BigInt(user1StateIndex), user1Keypair.pubKey, 0n, 1n, 0n, BigInt(pollId)); const signature = command.sign(user1Keypair.privKey); @@ -273,9 +238,9 @@ describe("Poll", function test() { // we only signed up one user so the state index is invalid BigInt(user1StateIndex + 1), user1Keypair.pubKey, - BigInt(0), - BigInt(1), - BigInt(0), + 0n, + 1n, + 0n, BigInt(pollId), ); @@ -340,9 +305,9 @@ describe("Poll", function test() { // we only signed up one user so the state index is invalid BigInt(user1StateIndex + 1), user1Keypair.pubKey, - BigInt(0), - BigInt(1), - BigInt(0), + 0n, + 1n, + 0n, BigInt(pollId), ); @@ -361,14 +326,7 @@ describe("Poll", function test() { }); it("should return the correct state leaves and ballots", () => { - const command = new PCommand( - BigInt(user1StateIndex + 1), - user1Keypair.pubKey, - BigInt(0), - BigInt(1), - BigInt(0), - BigInt(pollId), - ); + const command = new PCommand(BigInt(user1StateIndex + 1), user1Keypair.pubKey, 0n, 1n, 0n, BigInt(pollId)); const signature = command.sign(user1Keypair.privKey); @@ -388,14 +346,7 @@ describe("Poll", function test() { }); it("should have processed all messages", () => { - const command = new PCommand( - BigInt(user1StateIndex + 1), - user1Keypair.pubKey, - BigInt(0), - BigInt(1), - BigInt(0), - BigInt(pollId), - ); + const command = new PCommand(BigInt(user1StateIndex + 1), user1Keypair.pubKey, 0n, 1n, 0n, BigInt(pollId)); const signature = command.sign(user1Keypair.privKey); diff --git a/core/ts/__tests__/e2e.test.ts b/core/ts/__tests__/e2e.test.ts index 02b897d476..398a1afd38 100644 --- a/core/ts/__tests__/e2e.test.ts +++ b/core/ts/__tests__/e2e.test.ts @@ -2,16 +2,22 @@ import { expect } from "chai"; import { hash5, NOTHING_UP_MY_SLEEVE, IncrementalQuinTree, AccQueue } from "maci-crypto"; import { PCommand, Keypair, StateLeaf, blankStateLeafHash } from "maci-domainobjs"; -import { coordinatorKeypair, duration, maxValues, messageBatchSize, treeDepths, voiceCreditBalance } from "./constants"; -import { TestHarness, calculateTotal } from "./utils"; - -import { Poll } from "../Poll"; import { MaciState } from "../MaciState"; - +import { Poll } from "../Poll"; import { STATE_TREE_DEPTH, STATE_TREE_ARITY, STATE_TREE_SUBDEPTH } from "../utils/constants"; import { packProcessMessageSmallVals, unpackProcessMessageSmallVals } from "../utils/utils"; -describe("MaciState e2e", function test() { +import { + coordinatorKeypair, + duration, + maxValues, + messageBatchSize, + treeDepths, + voiceCreditBalance, +} from "./utils/constants"; +import { TestHarness, calculateTotal } from "./utils/utils"; + +describe("MaciState/Poll e2e", function test() { this.timeout(300000); describe("key changes", () => { @@ -22,12 +28,12 @@ describe("MaciState e2e", function test() { let pollId: number; let user1StateIndex: number; let user2StateIndex: number; - const user1VoteOptionIndex = BigInt(0); - const user2VoteOptionIndex = BigInt(1); - const user1VoteWeight = BigInt(9); - const user2VoteWeight = BigInt(3); - const user1NewVoteWeight = BigInt(5); - const user2NewVoteWeight = BigInt(7); + const user1VoteOptionIndex = 0n; + const user2VoteOptionIndex = 1n; + const user1VoteWeight = 9n; + const user2VoteWeight = 3n; + const user1NewVoteWeight = 5n; + const user2NewVoteWeight = 7n; describe("only user 1 changes key", () => { const maciState: MaciState = new MaciState(STATE_TREE_DEPTH); @@ -61,7 +67,7 @@ describe("MaciState e2e", function test() { user1Keypair.pubKey, user1VoteOptionIndex, user1VoteWeight, - BigInt(1), + 1n, BigInt(pollId), ); @@ -78,7 +84,7 @@ describe("MaciState e2e", function test() { user2Keypair.pubKey, user2VoteOptionIndex, user2VoteWeight, - BigInt(1), + 1n, BigInt(pollId), ); @@ -98,7 +104,7 @@ describe("MaciState e2e", function test() { user1SecondKeypair.pubKey, user1VoteOptionIndex, user1NewVoteWeight, - BigInt(1), + 1n, BigInt(pollId), ); @@ -160,7 +166,7 @@ describe("MaciState e2e", function test() { user1Keypair.pubKey, user1VoteOptionIndex, user1VoteWeight, - BigInt(1), + 1n, BigInt(pollId), ); @@ -177,7 +183,7 @@ describe("MaciState e2e", function test() { user2Keypair.pubKey, user2VoteOptionIndex, user2VoteWeight, - BigInt(1), + 1n, BigInt(pollId), ); @@ -197,7 +203,7 @@ describe("MaciState e2e", function test() { user1SecondKeypair.pubKey, user1VoteOptionIndex, user1NewVoteWeight, - BigInt(1), + 1n, BigInt(pollId), ); @@ -217,7 +223,7 @@ describe("MaciState e2e", function test() { user2SecondKeypair.pubKey, user2VoteOptionIndex, user2NewVoteWeight, - BigInt(1), + 1n, BigInt(pollId), ); @@ -275,7 +281,7 @@ describe("MaciState e2e", function test() { user1Keypair.pubKey, user1VoteOptionIndex, user1VoteWeight, - BigInt(1), + 1n, BigInt(pollId), ); @@ -292,11 +298,11 @@ describe("MaciState e2e", function test() { const poll = maciState.polls[pollId]; for (let i = 0; i < messageBatchSize - 1; i += 1) { const command = new PCommand( - BigInt(1), + 1n, user1Keypair.pubKey, user1VoteOptionIndex, user1VoteWeight, - BigInt(2), + 2n, BigInt(pollId), ); @@ -317,7 +323,7 @@ describe("MaciState e2e", function test() { user1SecondKeypair.pubKey, user1VoteOptionIndex, user1NewVoteWeight, - BigInt(1), + 1n, BigInt(pollId), ); @@ -350,8 +356,8 @@ describe("MaciState e2e", function test() { let pollId: number; let poll: Poll; let msgTree: IncrementalQuinTree; - const voteWeight = BigInt(9); - const voteOptionIndex = BigInt(0); + const voteWeight = 9n; + const voteOptionIndex = 0n; let stateIndex: number; const userKeypair = new Keypair(); @@ -394,7 +400,7 @@ describe("MaciState e2e", function test() { userKeypair.pubKey, voteOptionIndex, voteWeight, - BigInt(1), + 1n, BigInt(pollId), ); @@ -421,8 +427,8 @@ describe("MaciState e2e", function test() { }); it("packProcessMessageSmallVals and unpackProcessMessageSmallVals", () => { - const maxVoteOptions = BigInt(1); - const numUsers = BigInt(2); + const maxVoteOptions = 1n; + const numUsers = 2n; const batchStartIndex = 5; const batchEndIndex = 10; const packedVals = packProcessMessageSmallVals(maxVoteOptions, numUsers, batchStartIndex, batchEndIndex); @@ -462,7 +468,7 @@ describe("MaciState e2e", function test() { let maciState: MaciState; let pollId: number; let poll: Poll; - const voteWeight = BigInt(9); + const voteWeight = 9n; const users: Keypair[] = []; @@ -496,7 +502,7 @@ describe("MaciState e2e", function test() { userKeypair.pubKey, BigInt(i), // vote option index voteWeight, - BigInt(1), + 1n, BigInt(pollId), ); @@ -517,8 +523,8 @@ describe("MaciState e2e", function test() { BigInt(i + 1), userKeypair.pubKey, BigInt(i), // vote option index - voiceCreditBalance * BigInt(2), // invalid vote weight - BigInt(1), + voiceCreditBalance * 2n, // invalid vote weight + 1n, BigInt(pollId), ); @@ -600,16 +606,16 @@ describe("MaciState e2e", function test() { let testHarness: TestHarness; let poll: Poll; - beforeEach(async () => { + beforeEach(() => { testHarness = new TestHarness(); poll = testHarness.poll; }); - it("processMessages() should process a valid message", async () => { - const voteOptionIndex = BigInt(0); - const voteWeight = BigInt(9); - const nonce = BigInt(1); + it("should process a valid message", () => { + const voteOptionIndex = 0n; + const voteWeight = 9n; + const nonce = 1n; const users = testHarness.createUsers(1); testHarness.vote(users[0], testHarness.getStateIndex(users[0]), voteOptionIndex, voteWeight, nonce); @@ -620,14 +626,14 @@ describe("MaciState e2e", function test() { expect(messageLengthResult).to.eq(expectedNumVotes); const tallyResult = poll.tallyResult[0]; - const expectedTallyResult = BigInt(9); + const expectedTallyResult = 9n; expect(tallyResult).to.eq(expectedTallyResult); }); - it("processMessages() should not process messages twice", async () => { - const voteOptionIndex = BigInt(0); - const voteWeight = BigInt(9); - const nonce = BigInt(1); + it("should not process messages twice", () => { + const voteOptionIndex = 0n; + const voteWeight = 9n; + const nonce = 1n; const users = testHarness.createUsers(1); testHarness.vote(users[0], testHarness.getStateIndex(users[0]), voteOptionIndex, voteWeight, nonce); @@ -644,24 +650,24 @@ describe("MaciState e2e", function test() { expect(messageLengthResult).to.eq(expectedNumVotes); const tallyResult = poll.tallyResult[0]; - const expectedTallyResult = BigInt(9); + const expectedTallyResult = 9n; expect(tallyResult).to.eq(expectedTallyResult); }); - it("processMessages() should not process a message with an incorrect nonce", async () => { - const voteOptionIndex = BigInt(0); - const voteWeight = BigInt(9); + it("should not process a message with an incorrect nonce", () => { + const voteOptionIndex = 0n; + const voteWeight = 9n; const users = testHarness.createUsers(5); // generate a bunch of invalid votes with nonces that are not 1 let nonce: bigint; - for (let i = 0; i < users.length; i++) { + users.forEach((user) => { do { nonce = BigInt(Math.floor(Math.random() * 100) - 50); - } while (nonce === BigInt(1)); + } while (nonce === 1n); - testHarness.vote(users[i], testHarness.getStateIndex(users[i]), voteOptionIndex, voteWeight, nonce); - } + testHarness.vote(user, testHarness.getStateIndex(user), voteOptionIndex, voteWeight, nonce); + }); testHarness.finalizePoll(); @@ -670,7 +676,7 @@ describe("MaciState e2e", function test() { expect(messageLengthResult).to.eq(expectedNumVotes); const tallyResult = poll.tallyResult[0]; - const expectedTallyResult = BigInt(0); + const expectedTallyResult = 0n; expect(tallyResult).to.eq(expectedTallyResult); }); @@ -678,21 +684,21 @@ describe("MaciState e2e", function test() { // the square of the vote weight. Since the maximum voice credit is 100 here, // the vote weight can only be a value between 1 and 10 // (as these are the square roots of numbers up to 100). - it("processMessages() should not process a message with an incorrect vote weight", async () => { - const voteOptionIndex = BigInt(0); - const nonce = BigInt(1); + it("should not process a message with an incorrect vote weight", () => { + const voteOptionIndex = 0n; + const nonce = 1n; const users = testHarness.createUsers(5); // generate a bunch of invalid votes with vote weights that are not between 1 and 10 let voteWeight: bigint; - for (let i = 0; i < users.length; i++) { + users.forEach((user) => { do { voteWeight = BigInt(Math.floor(Math.random() * 100) - 50); - } while (BigInt(1) <= voteWeight && voteWeight <= BigInt(10)); + } while (voteWeight >= 1n && voteWeight <= 10n); - testHarness.vote(users[i], testHarness.getStateIndex(users[i]), voteOptionIndex, voteWeight, nonce); - } + testHarness.vote(user, testHarness.getStateIndex(user), voteOptionIndex, voteWeight, nonce); + }); testHarness.finalizePoll(); @@ -701,22 +707,22 @@ describe("MaciState e2e", function test() { expect(messageLengthResult).to.eq(expectedNumVotes); const tallyResult = poll.tallyResult[0]; - const expectedTallyResult = BigInt(0); + const expectedTallyResult = 0n; expect(tallyResult).to.eq(expectedTallyResult); }); - it("processMessages() should not process a message with an incorrect state tree index", async () => { - const voteOptionIndex = BigInt(0); - const nonce = BigInt(1); - const voteWeight = BigInt(9); + it("should not process a message with an incorrect state tree index", () => { + const voteOptionIndex = 0n; + const nonce = 1n; + const voteWeight = 9n; const numVotes = 5; const users = testHarness.createUsers(5); - for (let i = 0; i < users.length; i++) { + users.forEach((user) => { // generate a bunch of invalid votes with incorrect state tree index - testHarness.vote(users[i], testHarness.getStateIndex(users[i]) + 1, voteOptionIndex, voteWeight, nonce); - } + testHarness.vote(user, testHarness.getStateIndex(user) + 1, voteOptionIndex, voteWeight, nonce); + }); testHarness.finalizePoll(); @@ -725,14 +731,14 @@ describe("MaciState e2e", function test() { expect(messageLengthResult).to.eq(expectedNumVotes); const tallyResult = poll.tallyResult[0]; - const expectedTallyResult = BigInt(0); + const expectedTallyResult = 0n; expect(tallyResult).to.eq(expectedTallyResult); }); - it("processMessages() should not process a message with an incorrect signature", async () => { - const voteOptionIndex = BigInt(0); - const voteWeight = BigInt(9); - const nonce = BigInt(1); + it("should not process a message with an incorrect signature", () => { + const voteOptionIndex = 0n; + const voteWeight = 9n; + const nonce = 1n; const users = testHarness.createUsers(2); @@ -768,14 +774,14 @@ describe("MaciState e2e", function test() { expect(messageLengthResult).to.eq(expectedNumVotes); const tallyResult = poll.tallyResult[0]; - const expectedTallyResult = BigInt(0); + const expectedTallyResult = 0n; expect(tallyResult).to.eq(expectedTallyResult); }); - it("processMessages() should not process a message with an invalid coordinator key", async () => { - const voteOptionIndex = BigInt(0); - const voteWeight = BigInt(9); - const nonce = BigInt(1); + it("should not process a message with an invalid coordinator key", () => { + const voteOptionIndex = 0n; + const voteWeight = 9n; + const nonce = 1n; const users = testHarness.createUsers(1); @@ -797,7 +803,7 @@ describe("MaciState e2e", function test() { expect(messageLengthResult).to.eq(expectedNumVotes); const tallyResult = poll.tallyResult[0]; - const expectedTallyResult = BigInt(0); + const expectedTallyResult = 0n; expect(tallyResult).to.eq(expectedTallyResult); }); }); diff --git a/core/ts/__tests__/constants.ts b/core/ts/__tests__/utils/constants.ts similarity index 88% rename from core/ts/__tests__/constants.ts rename to core/ts/__tests__/utils/constants.ts index ce60e9d392..1e6c2215a2 100644 --- a/core/ts/__tests__/constants.ts +++ b/core/ts/__tests__/utils/constants.ts @@ -1,6 +1,6 @@ import { Keypair } from "maci-domainobjs"; -export const voiceCreditBalance = BigInt(100); +export const voiceCreditBalance = 100n; export const duration = 30; export const messageBatchSize = 25; export const coordinatorKeypair = new Keypair(); diff --git a/core/ts/__tests__/utils.ts b/core/ts/__tests__/utils/utils.ts similarity index 93% rename from core/ts/__tests__/utils.ts rename to core/ts/__tests__/utils/utils.ts index 75033dd23c..9911ccdc13 100644 --- a/core/ts/__tests__/utils.ts +++ b/core/ts/__tests__/utils/utils.ts @@ -1,29 +1,33 @@ import { Signature } from "maci-crypto"; import { PCommand, Message, Keypair, PubKey } from "maci-domainobjs"; -import { duration, maxValues, messageBatchSize, treeDepths, voiceCreditBalance } from "./constants"; - -import { Poll } from "../Poll"; -import { MaciState } from "../MaciState"; +import { MaciState } from "../../MaciState"; +import { Poll } from "../../Poll"; +import { STATE_TREE_DEPTH } from "../../utils/constants"; -import { STATE_TREE_DEPTH } from "../utils/constants" +import { duration, maxValues, messageBatchSize, treeDepths, voiceCreditBalance } from "./constants"; /** * Calculates the total of a tally result * @param tallyResult - the tally result * @returns the total of the tally result */ -export const calculateTotal = (tallyResult: bigint[]): bigint => tallyResult.reduce((acc, v) => acc + v, BigInt(0)); +export const calculateTotal = (tallyResult: bigint[]): bigint => tallyResult.reduce((acc, v) => acc + v, 0n); /** * A test harness for the MACI contract. */ export class TestHarness { maciState = new MaciState(STATE_TREE_DEPTH); + coordinatorKeypair = new Keypair(); + poll: Poll; + pollId: number; + users: Keypair[] = []; + stateIndices = new Map(); /** @@ -46,7 +50,7 @@ export class TestHarness { * @returns The keypairs of the newly created users. */ createUsers = (numUsers: number): Keypair[] => { - for (let i = 0; i < numUsers; i++) { + for (let i = 0; i < numUsers; i += 1) { const user = new Keypair(); this.users.push(user); const stateIndex = this.signup(user); @@ -145,7 +149,5 @@ export class TestHarness { * @param user - The keypair of the user. * @returns The state index of the user. */ - getStateIndex = (user: Keypair): number => { - return this.stateIndices.get(user)!; - }; -} \ No newline at end of file + getStateIndex = (user: Keypair): number => this.stateIndices.get(user) || -1; +} diff --git a/core/ts/utils/utils.ts b/core/ts/utils/utils.ts index cc6653eaaa..38374ef262 100644 --- a/core/ts/utils/utils.ts +++ b/core/ts/utils/utils.ts @@ -17,9 +17,9 @@ export const genProcessVkSig = ( voteOptionTreeDepth: number, batchSize: number, ): bigint => - (BigInt(batchSize) << BigInt(192)) + - (BigInt(stateTreeDepth) << BigInt(128)) + - (BigInt(messageTreeDepth) << BigInt(64)) + + (BigInt(batchSize) << 192n) + + (BigInt(stateTreeDepth) << 128n) + + (BigInt(messageTreeDepth) << 64n) + BigInt(voteOptionTreeDepth); /** @@ -71,9 +71,9 @@ export const packProcessMessageSmallVals = ( const packedVals = // Note: the << operator has lower precedence than + BigInt(`${maxVoteOptions}`) + - (BigInt(`${numUsers}`) << BigInt(50)) + - (BigInt(batchStartIndex) << BigInt(100)) + - (BigInt(batchEndIndex) << BigInt(150)); + (BigInt(`${numUsers}`) << 50n) + + (BigInt(batchStartIndex) << 100n) + + (BigInt(batchEndIndex) << 150n); return packedVals; }; @@ -118,7 +118,7 @@ export const unpackProcessMessageSmallVals = ( */ export const packTallyVotesSmallVals = (batchStartIndex: number, batchSize: number, numSignUps: number): bigint => { // Note: the << operator has lower precedence than + - const packedVals = BigInt(batchStartIndex) / BigInt(batchSize) + (BigInt(numSignUps) << BigInt(50)); + const packedVals = BigInt(batchStartIndex) / BigInt(batchSize) + (BigInt(numSignUps) << 50n); return packedVals; }; @@ -149,7 +149,7 @@ export const unpackTallyVotesSmallVals = (packedVals: bigint): { numSignUps: big */ export const packSubsidySmallVals = (row: number, col: number, numSignUps: number): bigint => { // Note: the << operator has lower precedence than + - const packedVals = (BigInt(numSignUps) << BigInt(100)) + (BigInt(row) << BigInt(50)) + BigInt(col); + const packedVals = (BigInt(numSignUps) << 100n) + (BigInt(row) << 50n) + BigInt(col); return packedVals; };