-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #835 from clrfund/fix/verify-tally-on-chain
Add verifyTallyResult, verifyPerVOSpentVoiceCredits, verifySpentVoiceCredits
- Loading branch information
Showing
5 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { Contract } from "ethers"; | ||
import { genTreeProof } from "maci-crypto"; | ||
import { TallyData } from "./interfaces"; | ||
|
||
/** | ||
* Loop through each per vote option spent voice credits and verify it on-chain | ||
* | ||
* @param tallyContract The tally contract | ||
* @param tallyData The tally.json file data | ||
* @param voteOptionTreeDepth The vote option tree depth | ||
* @param newSpentVoiceCreditsCommitment The total spent voice credits commitment | ||
* @param newResultsCommitment The tally result commitment | ||
* | ||
* @returns list of the indexes of the tally result that failed on-chain verification | ||
*/ | ||
export const verifyPerVOSpentVoiceCredits = async ( | ||
tallyContract: Contract, | ||
tallyData: TallyData, | ||
voteOptionTreeDepth: number, | ||
newSpentVoiceCreditsCommitment: bigint, | ||
newResultsCommitment: bigint, | ||
): Promise<number[]> => { | ||
const failedIndices: number[] = []; | ||
|
||
for (let i = 0; i < tallyData.perVOSpentVoiceCredits.tally.length; i += 1) { | ||
const proof = genTreeProof( | ||
i, | ||
tallyData.perVOSpentVoiceCredits.tally.map((x) => BigInt(x)), | ||
voteOptionTreeDepth, | ||
); | ||
|
||
const isValid = await tallyContract.verifyPerVOSpentVoiceCredits( | ||
i, | ||
tallyData.perVOSpentVoiceCredits.tally[i], | ||
proof, | ||
tallyData.perVOSpentVoiceCredits.salt, | ||
voteOptionTreeDepth, | ||
newSpentVoiceCreditsCommitment, | ||
newResultsCommitment, | ||
); | ||
if (!isValid) { | ||
failedIndices.push(i); | ||
} | ||
} | ||
|
||
return failedIndices; | ||
}; | ||
|
||
/** | ||
* Loop through each tally result and verify it on-chain | ||
* @param tallyContract The tally contract | ||
* @param tallyData The tally.json file data | ||
* @param voteOptionTreeDepth The vote option tree depth | ||
* @param newSpentVoiceCreditsCommitment The total spent voice credits commitment | ||
* @param newPerVOSpentVoiceCreditsCommitment The per vote option voice credits commitment | ||
* @returns list of the indexes of the tally result that failed on-chain verification | ||
*/ | ||
export const verifyTallyResults = async ( | ||
tallyContract: Contract, | ||
tallyData: TallyData, | ||
voteOptionTreeDepth: number, | ||
newSpentVoiceCreditsCommitment: bigint, | ||
newPerVOSpentVoiceCreditsCommitment: bigint, | ||
): Promise<number[]> => { | ||
const failedIndices: number[] = []; | ||
|
||
for (let i = 0; i < tallyData.results.tally.length; i += 1) { | ||
const proof = genTreeProof( | ||
i, | ||
tallyData.results.tally.map((x) => BigInt(x)), | ||
voteOptionTreeDepth, | ||
); | ||
|
||
const isValid = await tallyContract.verifyTallyResult( | ||
i, | ||
tallyData.results.tally[i], | ||
proof, | ||
tallyData.results.salt, | ||
voteOptionTreeDepth, | ||
newSpentVoiceCreditsCommitment, | ||
newPerVOSpentVoiceCreditsCommitment, | ||
); | ||
|
||
if (!isValid) { | ||
failedIndices.push(i); | ||
} | ||
} | ||
|
||
return failedIndices; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters