Skip to content

Commit

Permalink
feat(contracts): add total spent to tally
Browse files Browse the repository at this point in the history
- [x] Update add tally results method signature
- [x] Compatibility fixes
- [x] Add total spent to tally contract
  • Loading branch information
0xmad authored and kittybest committed Oct 22, 2024
1 parent 971fe9c commit ac206f3
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
3 changes: 3 additions & 0 deletions packages/cli/ts/commands/proveOnChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,10 @@ export const proveOnChain = async ({
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,
)
Expand Down
34 changes: 28 additions & 6 deletions packages/contracts/contracts/Tally.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ contract Tally is Ownable, SnarkCommon, CommonUtilities, Hasher, DomainObjs, ITa
// The total tally results number
uint256 public totalTallyResults;

// spent field retrieved in the totalSpentVoiceCredits object
uint256 public totalSpent;

/// @notice custom errors
error ProcessingNotComplete();
error InvalidTallyVotesProof();
Expand All @@ -75,6 +78,7 @@ contract Tally is Ownable, SnarkCommon, CommonUtilities, Hasher, DomainObjs, ITa
error TallyBatchSizeTooLarge();
error NotSupported();
error VotesNotTallied();
error IncorrectSpentVoiceCredits();

/// @notice Create a new Tally contract
/// @param _verifier The Verifier contract
Expand Down Expand Up @@ -365,17 +369,22 @@ contract Tally is Ownable, SnarkCommon, CommonUtilities, Hasher, DomainObjs, ITa
* @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 _spentVoiceCreditsHashes hashLeftRight(number of spent voice credits, spent salt)
* @param _perVOSpentVoiceCreditsHashes hashLeftRight(merkle root of the no spent voice credits per vote option, perVOSpentVoiceCredits salt)
* @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)
*/
function addTallyResults(
uint256[] calldata _voteOptionIndices,
uint256[] calldata _tallyResults,
uint256[][][] calldata _tallyResultProofs,
uint256 _totalSpent,
uint256 _totalSpentSalt,
uint256 _tallyResultSalt,
uint256 _spentVoiceCreditsHashes,
uint256 _perVOSpentVoiceCreditsHashes
uint256 _newResultsCommitment,
uint256 _spentVoiceCreditsHash,
uint256 _perVOSpentVoiceCreditsHash
) public virtual onlyOwner {
if (!isTallied()) {
revert VotesNotTallied();
Expand All @@ -390,15 +399,28 @@ contract Tally is Ownable, SnarkCommon, CommonUtilities, Hasher, DomainObjs, ITa
_tallyResults[i],
_tallyResultProofs[i],
_tallyResultSalt,
_spentVoiceCreditsHashes,
_perVOSpentVoiceCreditsHashes,
_spentVoiceCreditsHash,
_perVOSpentVoiceCreditsHash,
voteOptionTreeDepth
);

unchecked {
i++;
}
}

bool verified = verifySpentVoiceCredits(
_totalSpent,
_totalSpentSalt,
_newResultsCommitment,
_perVOSpentVoiceCreditsHash
);

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

totalSpent = _totalSpent;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions packages/contracts/tasks/helpers/Prover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,10 @@ export class Prover {
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,
)
Expand Down
26 changes: 26 additions & 0 deletions packages/contracts/tests/Tally.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,10 @@ describe("TallyVotes", () => {
tallyData.results.tally.map((_, index) => index),
tallyData.results.tally,
tallyResultProofs,
tallyData.totalSpentVoiceCredits.spent,
tallyData.totalSpentVoiceCredits.salt,
tallyData.results.salt,
tallyData.results.commitment,
tallyData.totalSpentVoiceCredits.commitment,
0n,
),
Expand Down Expand Up @@ -372,12 +375,29 @@ describe("TallyVotes", () => {

const indices = tallyData.results.tally.map((_, index) => index);

await expect(
tallyContract.addTallyResults(
indices,
tallyData.results.tally,
tallyResultProofs,
0n,
0n,
tallyData.results.salt,
0n,
tallyData.totalSpentVoiceCredits.commitment,
newPerVOSpentVoiceCreditsCommitment,
),
).to.be.revertedWithCustomError(tallyContract, "IncorrectSpentVoiceCredits");

await tallyContract
.addTallyResults(
indices,
tallyData.results.tally,
tallyResultProofs,
tallyData.totalSpentVoiceCredits.spent,
tallyData.totalSpentVoiceCredits.salt,
tallyData.results.salt,
tallyData.results.commitment,
tallyData.totalSpentVoiceCredits.commitment,
newPerVOSpentVoiceCreditsCommitment,
)
Expand All @@ -394,7 +414,10 @@ describe("TallyVotes", () => {
indices,
tallyData.results.tally,
tallyResultProofs,
tallyData.totalSpentVoiceCredits.spent,
tallyData.totalSpentVoiceCredits.salt,
tallyData.results.salt,
tallyData.results.commitment,
tallyData.totalSpentVoiceCredits.commitment,
newPerVOSpentVoiceCreditsCommitment,
)
Expand Down Expand Up @@ -436,7 +459,10 @@ describe("TallyVotes", () => {
tallyData.results.tally.map((_, index) => index),
tallyData.results.tally,
tallyResultProofs,
tallyData.totalSpentVoiceCredits.spent,
tallyData.totalSpentVoiceCredits.salt,
tallyData.results.salt,
tallyData.results.commitment,
tallyData.totalSpentVoiceCredits.commitment,
0n,
),
Expand Down

0 comments on commit ac206f3

Please sign in to comment.