Skip to main content
Version: v1.2

Tally

The Tally contract is used during votes tallying and by users to verify the tally results.

TREE_ARITY

uint256 TREE_ARITY

tallyCommitment

uint256 tallyCommitment

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:

hash3( hashLeftRight(merkle root of current results, salt0) hashLeftRight(number of spent voice credits, salt1), hashLeftRight(merkle root of the no. of spent voice credits per vote option, salt2) )

Where each salt is unique and the merkle roots are of arrays of leaves TREE_ARITY ** voteOptionTreeDepth long.

tallyBatchNum

uint256 tallyBatchNum

sbCommitment

uint256 sbCommitment

verifier

contract IVerifier verifier

vkRegistry

contract IVkRegistry vkRegistry

poll

contract IPoll poll

messageProcessor

contract IMessageProcessor messageProcessor

ProcessingNotComplete

error ProcessingNotComplete()

custom errors

InvalidTallyVotesProof

error InvalidTallyVotesProof()

AllBallotsTallied

error AllBallotsTallied()

NumSignUpsTooLarge

error NumSignUpsTooLarge()

BatchStartIndexTooLarge

error BatchStartIndexTooLarge()

TallyBatchSizeTooLarge

error TallyBatchSizeTooLarge()

constructor

constructor(address _verifier, address _vkRegistry, address _poll, address _mp) public payable

Create a new Tally contract

Parameters

NameTypeDescription
_verifieraddressThe Verifier contract
_vkRegistryaddressThe VkRegistry contract
_polladdressThe Poll contract
_mpaddressThe MessageProcessor contract

genTallyVotesPackedVals

function genTallyVotesPackedVals(uint256 _numSignUps, uint256 _batchStartIndex, uint256 _tallyBatchSize) public pure returns (uint256 result)

Pack the batch start index and number of signups into a 100-bit value.

Parameters

NameTypeDescription
_numSignUpsuint256
_batchStartIndexuint256
_tallyBatchSizeuint256

Return Values

NameTypeDescription
resultuint256an uint256 representing the 3 inputs packed together

isTallied

function isTallied() public view returns (bool tallied)

Check if all ballots are tallied

Return Values

NameTypeDescription
talliedboolwhether all ballots are tallied

genTallyVotesPublicInputHash

function genTallyVotesPublicInputHash(uint256 _numSignUps, uint256 _batchStartIndex, uint256 _tallyBatchSize, uint256 _newTallyCommitment) public view returns (uint256 inputHash)

generate hash of public inputs for tally circuit

Parameters

NameTypeDescription
_numSignUpsuint256
_batchStartIndexuint256
_tallyBatchSizeuint256
_newTallyCommitmentuint256

Return Values

NameTypeDescription
inputHashuint256hash of public inputs

updateSbCommitment

function updateSbCommitment() public

Update the state and ballot root commitment

tallyVotes

function tallyVotes(uint256 _newTallyCommitment, uint256[8] _proof) public

Verify the result of a tally batch

Parameters

NameTypeDescription
_newTallyCommitmentuint256the new tally commitment to be verified
_proofuint256[8]the proof generated after tallying this batch

verifyTallyProof

function verifyTallyProof(uint256[8] _proof, uint256 _numSignUps, uint256 _batchStartIndex, uint256 _tallyBatchSize, uint256 _newTallyCommitment) public view returns (bool isValid)

Verify the tally proof using the verifying key

Parameters

NameTypeDescription
_proofuint256[8]the proof generated after processing all messages
_numSignUpsuint256number of signups for a given poll
_batchStartIndexuint256the number of batches multiplied by the size of the batch
_tallyBatchSizeuint256batch size for the tally
_newTallyCommitmentuint256the tally commitment to be verified at a given batch index

Return Values

NameTypeDescription
isValidboolwhether the proof is valid

computeMerkleRootFromPath

function computeMerkleRootFromPath(uint8 _depth, uint256 _index, uint256 _leaf, uint256[][] _pathElements) internal pure returns (uint256 current)

Compute the merkle root from the path elements and a leaf

Parameters

NameTypeDescription
_depthuint8the depth of the merkle tree
_indexuint256the index of the leaf
_leafuint256the leaf
_pathElementsuint256[][]the path elements to reconstruct the merkle root

Return Values

NameTypeDescription
currentuint256The merkle root

verifySpentVoiceCredits

function verifySpentVoiceCredits(uint256 _totalSpent, uint256 _totalSpentSalt, uint256 _resultCommitment, uint256 _perVOSpentVoiceCreditsHash) public view returns (bool isValid)

Verify the number of spent voice credits from the tally.json

Parameters

NameTypeDescription
_totalSpentuint256spent field retrieved in the totalSpentVoiceCredits object
_totalSpentSaltuint256the corresponding salt in the totalSpentVoiceCredit object
_resultCommitmentuint256hashLeftRight(merkle root of the results.tally, results.salt) in tally.json file
_perVOSpentVoiceCreditsHashuint256hashLeftRight(merkle root of the no spent voice credits per vote option, salt)

Return Values

NameTypeDescription
isValidboolWhether the provided values are valid

verifyPerVOSpentVoiceCredits

function verifyPerVOSpentVoiceCredits(uint256 _voteOptionIndex, uint256 _spent, uint256[][] _spentProof, uint256 _spentSalt, uint8 _voteOptionTreeDepth, uint256 _spentVoiceCreditsHash, uint256 _resultCommitment) public view returns (bool isValid)

Return Values

NameTypeDescription
isValidboolWhether the provided proof is valid

verifyTallyResult

function verifyTallyResult(uint256 _voteOptionIndex, uint256 _tallyResult, uint256[][] _tallyResultProof, uint256 _tallyResultSalt, uint8 _voteOptionTreeDepth, uint256 _spentVoiceCreditsHash, uint256 _perVOSpentVoiceCreditsHash) public view returns (bool isValid)

Verify the result generated from the tally.json

Parameters

NameTypeDescription
_voteOptionIndexuint256the index of the vote option to verify the correctness of the tally
_tallyResultuint256Flattened array of the tally
_tallyResultProofuint256[][]Corresponding proof of the tally result
_tallyResultSaltuint256the respective salt in the results object in the tally.json
_voteOptionTreeDepthuint8depth of the vote option tree
_spentVoiceCreditsHashuint256hashLeftRight(number of spent voice credits, spent salt)
_perVOSpentVoiceCreditsHashuint256hashLeftRight(merkle root of the no spent voice credits per vote option, perVOSpentVoiceCredits salt)

Return Values

NameTypeDescription
isValidboolWhether the provided proof is valid