Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(contracts): add struct for add tally results args #1868

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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,62 +388,45 @@

/**
* @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
);

unchecked {
i++;
}
}

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

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

totalSpent = _totalSpent;
totalSpent = args.totalSpent;
}

Check warning

Code scanning / Slither

Unused return Medium


/**
* @dev Add and verify tally votes and calculate sum of tally squares for alpha calculation.
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
Loading