Skip to content

Commit

Permalink
Merge branch 'dev' of programmerraja:programmerraja/maci into feat/ma…
Browse files Browse the repository at this point in the history
…ci-script
  • Loading branch information
programmerraja committed Oct 28, 2024
2 parents e5995a2 + ae7fdaa commit 84ad290
Show file tree
Hide file tree
Showing 15 changed files with 433 additions and 262 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/typos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Use typos with config file
uses: crate-ci/[email protected].0
uses: crate-ci/[email protected].8
with:
config: .github/workflows/config/typos.toml
4 changes: 2 additions & 2 deletions apps/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@docusaurus/core": "^3.5.1",
"@docusaurus/preset-classic": "^3.5.1",
"@docusaurus/theme-classic": "^3.5.1",
"@docusaurus/theme-common": "^3.5.1",
"@docusaurus/theme-common": "^3.5.2",
"@docusaurus/types": "^3.5.1",
"@easyops-cn/docusaurus-search-local": "^0.44.5",
"@mdx-js/react": "^3.0.1",
Expand All @@ -38,7 +38,7 @@
"devDependencies": {
"@docusaurus/module-type-aliases": "^3.5.2",
"@docusaurus/tsconfig": "^3.5.2",
"@types/node": "^22.4.1",
"@types/node": "^22.8.1",
"@types/react": "^18.3.11",
"ts-node": "^10.9.2",
"typescript": "^5.5.4"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"tar": "^7.4.3",
"ts-node": "^10.9.1",
"typedoc": "^0.26.7",
"typedoc-plugin-markdown": "^4.2.9",
"typedoc-plugin-markdown": "^4.2.6",
"typescript": "^5.5.4"
},
"config": {
Expand Down
2 changes: 1 addition & 1 deletion packages/circuits/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@types/chai": "^4.3.11",
"@types/chai-as-promised": "^7.1.8",
"@types/mocha": "^10.0.8",
"@types/node": "^22.4.1",
"@types/node": "^22.8.1",
"@types/snarkjs": "^0.7.8",
"@zk-kit/baby-jubjub": "^1.0.3",
"chai": "^4.3.10",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"@types/chai": "^4.3.9",
"@types/chai-as-promised": "^7.1.8",
"@types/mocha": "^10.0.8",
"@types/node": "^22.4.1",
"@types/node": "^22.8.1",
"@types/prompt": "^1.1.9",
"@types/snarkjs": "^0.7.8",
"chai": "^4.3.10",
Expand Down
18 changes: 9 additions & 9 deletions packages/cli/ts/commands/proveOnChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,17 +379,17 @@ export const proveOnChain = async ({
);

await tallyContract
.addTallyResults(
tallyData.results.tally.map((_, index) => index),
.addTallyResults({
voteOptionIndices: tallyData.results.tally.map((_, index) => index),
tallyResults,
tallyResultProofs,
tallyData.totalSpentVoiceCredits.spent,
tallyData.totalSpentVoiceCredits.salt,
tallyData.results.salt,
tallyData.results.commitment,
tallyData.totalSpentVoiceCredits.commitment,
tallyData.perVOSpentVoiceCredits?.commitment ?? 0n,
)
totalSpent: tallyData.totalSpentVoiceCredits.spent,
totalSpentSalt: tallyData.totalSpentVoiceCredits.salt,
tallyResultSalt: tallyData.results.salt,
newResultsCommitment: tallyData.results.commitment,
spentVoiceCreditsHash: tallyData.totalSpentVoiceCredits.commitment,
perVOSpentVoiceCreditsHash: tallyData.perVOSpentVoiceCredits?.commitment ?? 0n,
})
.then((tx) => tx.wait());
}
};
67 changes: 36 additions & 31 deletions packages/contracts/contracts/Tally.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ contract Tally is Ownable, SnarkCommon, CommonUtilities, Hasher, DomainObjs, ITa
bool flag;
}

/// @notice tally result args
struct AddTallyResultsArgs {
/// @param voteOptionIndices Vote option index.
uint256[] voteOptionIndices;
/// @param tallyResults The results of vote tally for the recipients.
uint256[] tallyResults;
/// @param tallyResultProofs Proofs of correctness of the vote tally results.
uint256[][][] tallyResultProofs;
/// @param totalSpent spent field retrieved in the totalSpentVoiceCredits object
uint256 totalSpent;
/// @param totalSpentSalt spent salt
uint256 totalSpentSalt;
/// @param tallyResultSalt the respective salt in the results object in the tally.json
uint256 tallyResultSalt;
/// @param newResultsCommitment The salted commitment of the vote tally for this batch of leaves plus the vote tally from currentResults
uint256 newResultsCommitment;
/// @param spentVoiceCreditsHash hashLeftRight(number of spent voice credits, spent salt)
uint256 spentVoiceCreditsHash;
/// @param perVOSpentVoiceCreditsHash hashLeftRight(merkle root of the no spent voice credits per vote option, perVOSpentVoiceCredits salt)
uint256 perVOSpentVoiceCreditsHash;
}

/// @notice The commitment to the tally results. Its initial value is 0, but after
/// the tally of each batch is proven on-chain via a zk-SNARK, it should be
/// updated to:
Expand Down Expand Up @@ -366,41 +388,24 @@ contract Tally is Ownable, SnarkCommon, CommonUtilities, Hasher, DomainObjs, ITa

/**
* @notice Add and verify tally results by batch.
* @param _voteOptionIndices Vote option index.
* @param _tallyResults The results of vote tally for the recipients.
* @param _tallyResultProofs Proofs of correctness of the vote tally results.
* @param _totalSpent spent field retrieved in the totalSpentVoiceCredits object
* @param _tallyResultSalt the respective salt in the results object in the tally.json
* @param _newResultsCommitment The salted commitment of the vote tally for this batch of leaves plus the vote tally from currentResults
* @param _spentVoiceCreditsHash hashLeftRight(number of spent voice credits, spent salt)
* @param _perVOSpentVoiceCreditsHash hashLeftRight(merkle root of the no spent voice credits per vote option, perVOSpentVoiceCredits salt)
* @param args add tally result args
*/
function addTallyResults(
uint256[] calldata _voteOptionIndices,
uint256[] calldata _tallyResults,
uint256[][][] calldata _tallyResultProofs,
uint256 _totalSpent,
uint256 _totalSpentSalt,
uint256 _tallyResultSalt,
uint256 _newResultsCommitment,
uint256 _spentVoiceCreditsHash,
uint256 _perVOSpentVoiceCreditsHash
) public virtual onlyOwner {
function addTallyResults(AddTallyResultsArgs calldata args) public virtual onlyOwner {
if (!isTallied()) {
revert VotesNotTallied();
}

(, , , uint8 voteOptionTreeDepth) = poll.treeDepths();
uint256 voteOptionsLength = _voteOptionIndices.length;
uint256 voteOptionsLength = args.voteOptionIndices.length;

for (uint256 i = 0; i < voteOptionsLength; ) {
addTallyResult(
_voteOptionIndices[i],
_tallyResults[i],
_tallyResultProofs[i],
_tallyResultSalt,
_spentVoiceCreditsHash,
_perVOSpentVoiceCreditsHash,
args.voteOptionIndices[i],
args.tallyResults[i],
args.tallyResultProofs[i],
args.tallyResultSalt,
args.spentVoiceCreditsHash,
args.perVOSpentVoiceCreditsHash,
voteOptionTreeDepth
);

Expand All @@ -410,17 +415,17 @@ contract Tally is Ownable, SnarkCommon, CommonUtilities, Hasher, DomainObjs, ITa
}

bool verified = verifySpentVoiceCredits(
_totalSpent,
_totalSpentSalt,
_newResultsCommitment,
_perVOSpentVoiceCreditsHash
args.totalSpent,
args.totalSpentSalt,
args.newResultsCommitment,
args.perVOSpentVoiceCreditsHash
);

if (!verified) {
revert IncorrectSpentVoiceCredits();
}

totalSpent = _totalSpent;
totalSpent = args.totalSpent;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
"@types/circomlibjs": "^0.1.6",
"@types/lowdb": "^1.0.15",
"@types/mocha": "^10.0.8",
"@types/node": "^22.4.1",
"@types/node": "^22.8.1",
"@types/snarkjs": "^0.7.8",
"@types/uuid": "^10.0.0",
"chai": "^4.3.10",
Expand Down
18 changes: 9 additions & 9 deletions packages/contracts/tasks/helpers/Prover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,17 +317,17 @@ export class Prover {
);

await this.tallyContract
.addTallyResults(
tallyData.results.tally.map((_, index) => index),
.addTallyResults({
voteOptionIndices: tallyData.results.tally.map((_, index) => index),
tallyResults,
tallyResultProofs,
tallyData.totalSpentVoiceCredits.spent,
tallyData.totalSpentVoiceCredits.salt,
tallyData.results.salt,
tallyData.results.commitment,
tallyData.totalSpentVoiceCredits.commitment,
tallyData.perVOSpentVoiceCredits?.commitment ?? 0n,
)
totalSpent: tallyData.totalSpentVoiceCredits.spent,
totalSpentSalt: tallyData.totalSpentVoiceCredits.salt,
tallyResultSalt: tallyData.results.salt,
newResultsCommitment: tallyData.results.commitment,
spentVoiceCreditsHash: tallyData.totalSpentVoiceCredits.commitment,
perVOSpentVoiceCreditsHash: tallyData.perVOSpentVoiceCredits?.commitment ?? 0n,
})
.then((tx) => tx.wait());
}

Expand Down
100 changes: 50 additions & 50 deletions packages/contracts/tests/Tally.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,17 @@ describe("TallyVotes", () => {
);

await expect(
tallyContract.addTallyResults(
tallyData.results.tally.map((_, index) => index),
tallyData.results.tally,
tallyContract.addTallyResults({
voteOptionIndices: tallyData.results.tally.map((_, index) => index),
tallyResults: tallyData.results.tally,
tallyResultProofs,
tallyData.totalSpentVoiceCredits.spent,
tallyData.totalSpentVoiceCredits.salt,
tallyData.results.salt,
tallyData.results.commitment,
tallyData.totalSpentVoiceCredits.commitment,
0n,
),
totalSpent: tallyData.totalSpentVoiceCredits.spent,
totalSpentSalt: tallyData.totalSpentVoiceCredits.salt,
tallyResultSalt: tallyData.results.salt,
newResultsCommitment: tallyData.results.commitment,
spentVoiceCreditsHash: tallyData.totalSpentVoiceCredits.commitment,
perVOSpentVoiceCreditsHash: 0n,
}),
).to.be.revertedWithCustomError(tallyContract, "VotesNotTallied");
});

Expand Down Expand Up @@ -376,31 +376,31 @@ describe("TallyVotes", () => {
const indices = tallyData.results.tally.map((_, index) => index);

await expect(
tallyContract.addTallyResults(
indices,
tallyData.results.tally,
tallyContract.addTallyResults({
voteOptionIndices: indices,
tallyResults: tallyData.results.tally,
tallyResultProofs,
0n,
0n,
tallyData.results.salt,
0n,
tallyData.totalSpentVoiceCredits.commitment,
newPerVOSpentVoiceCreditsCommitment,
),
totalSpent: 0n,
totalSpentSalt: 0n,
tallyResultSalt: tallyData.results.salt,
newResultsCommitment: 0n,
spentVoiceCreditsHash: tallyData.totalSpentVoiceCredits.commitment,
perVOSpentVoiceCreditsHash: newPerVOSpentVoiceCreditsCommitment,
}),
).to.be.revertedWithCustomError(tallyContract, "IncorrectSpentVoiceCredits");

await tallyContract
.addTallyResults(
indices,
tallyData.results.tally,
.addTallyResults({
voteOptionIndices: indices,
tallyResults: tallyData.results.tally,
tallyResultProofs,
tallyData.totalSpentVoiceCredits.spent,
tallyData.totalSpentVoiceCredits.salt,
tallyData.results.salt,
tallyData.results.commitment,
tallyData.totalSpentVoiceCredits.commitment,
newPerVOSpentVoiceCreditsCommitment,
)
totalSpent: tallyData.totalSpentVoiceCredits.spent,
totalSpentSalt: tallyData.totalSpentVoiceCredits.salt,
tallyResultSalt: tallyData.results.salt,
newResultsCommitment: tallyData.results.commitment,
spentVoiceCreditsHash: tallyData.totalSpentVoiceCredits.commitment,
perVOSpentVoiceCreditsHash: newPerVOSpentVoiceCreditsCommitment,
})
.then((tx) => tx.wait());

const initialResults = await Promise.all(indices.map((index) => tallyContract.tallyResults(index)));
Expand All @@ -410,17 +410,17 @@ describe("TallyVotes", () => {
expect(initialResults.map((result) => result.value)).to.deep.equal(tallyData.results.tally);

await tallyContract
.addTallyResults(
indices,
tallyData.results.tally,
.addTallyResults({
voteOptionIndices: indices,
tallyResults: tallyData.results.tally,
tallyResultProofs,
tallyData.totalSpentVoiceCredits.spent,
tallyData.totalSpentVoiceCredits.salt,
tallyData.results.salt,
tallyData.results.commitment,
tallyData.totalSpentVoiceCredits.commitment,
newPerVOSpentVoiceCreditsCommitment,
)
totalSpent: tallyData.totalSpentVoiceCredits.spent,
totalSpentSalt: tallyData.totalSpentVoiceCredits.salt,
tallyResultSalt: tallyData.results.salt,
newResultsCommitment: tallyData.results.commitment,
spentVoiceCreditsHash: tallyData.totalSpentVoiceCredits.commitment,
perVOSpentVoiceCreditsHash: newPerVOSpentVoiceCreditsCommitment,
})
.then((tx) => tx.wait());

const results = await Promise.all(indices.map((index) => tallyContract.tallyResults(index)));
Expand Down Expand Up @@ -455,17 +455,17 @@ describe("TallyVotes", () => {
);

await expect(
tallyContract.addTallyResults(
tallyData.results.tally.map((_, index) => index),
tallyData.results.tally,
tallyContract.addTallyResults({
voteOptionIndices: tallyData.results.tally.map((_, index) => index),
tallyResults: tallyData.results.tally,
tallyResultProofs,
tallyData.totalSpentVoiceCredits.spent,
tallyData.totalSpentVoiceCredits.salt,
tallyData.results.salt,
tallyData.results.commitment,
tallyData.totalSpentVoiceCredits.commitment,
0n,
),
totalSpent: tallyData.totalSpentVoiceCredits.spent,
totalSpentSalt: tallyData.totalSpentVoiceCredits.salt,
tallyResultSalt: tallyData.results.salt,
newResultsCommitment: tallyData.results.commitment,
spentVoiceCreditsHash: tallyData.totalSpentVoiceCredits.commitment,
perVOSpentVoiceCreditsHash: 0n,
}),
).to.be.revertedWithCustomError(tallyContract, "InvalidTallyVotesProof");
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"devDependencies": {
"@types/chai": "^4.3.11",
"@types/mocha": "^10.0.8",
"@types/node": "^22.4.1",
"@types/node": "^22.8.1",
"benny": "^3.7.1",
"chai": "^4.3.10",
"mocha": "^10.7.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"devDependencies": {
"@types/chai": "^4.3.11",
"@types/mocha": "^10.0.8",
"@types/node": "^22.4.1",
"@types/node": "^22.8.1",
"benny": "^3.7.1",
"chai": "^4.3.10",
"mocha": "^10.7.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/domainobjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"devDependencies": {
"@types/chai": "^4.3.11",
"@types/mocha": "^10.0.8",
"@types/node": "^22.4.1",
"@types/node": "^22.8.1",
"chai": "^4.3.10",
"mocha": "^10.7.3",
"nyc": "^17.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/integrationTests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@types/chai": "^4.3.11",
"@types/chai-as-promised": "^7.1.8",
"@types/mocha": "^10.0.8",
"@types/node": "^22.4.1",
"@types/node": "^22.8.1",
"chai": "^4.3.10",
"chai-as-promised": "^7.1.2",
"hardhat": "^2.22.13",
Expand Down
Loading

0 comments on commit 84ad290

Please sign in to comment.