@maci-protocol/circuits 0.0.0-ci.2df0337 → 0.0.0-ci.2ff3d96
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -2
- package/README.md +2 -2
- package/build/ts/{genZkeys.d.ts → generateZkeys.d.ts} +1 -1
- package/build/ts/generateZkeys.d.ts.map +1 -0
- package/build/ts/{genZkeys.js → generateZkeys.js} +1 -1
- package/build/ts/generateZkeys.js.map +1 -0
- package/build/ts/types.d.ts +9 -9
- package/build/ts/types.d.ts.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/circom/circuits.json +32 -16
- package/circom/coordinator/full/MessageProcessor.circom +245 -0
- package/circom/coordinator/full/SingleMessageProcessor.circom +203 -0
- package/circom/coordinator/non-qv/MessageProcessor.circom +244 -0
- package/circom/coordinator/non-qv/SingleMessageProcessor.circom +197 -0
- package/circom/coordinator/non-qv/VoteTally.circom +161 -0
- package/circom/coordinator/qv/MessageProcessor.circom +242 -0
- package/circom/coordinator/qv/SingleMessageProcessor.circom +204 -0
- package/circom/coordinator/qv/VoteTally.circom +179 -0
- package/circom/coordinator/qv/VoteTallyWithIndividualCounts.circom +226 -0
- package/circom/utils/CalculateTotal.circom +6 -6
- package/circom/utils/EdDSAPoseidonVerifier.circom +8 -3
- package/circom/utils/IsOnCurve.circom +40 -0
- package/circom/utils/PrivateToPublicKey.circom +3 -3
- package/circom/utils/full/MessageValidator.circom +91 -0
- package/circom/utils/full/StateLeafAndBallotTransformer.circom +127 -0
- package/circom/utils/non-qv/{messageValidator.circom → MessageValidator.circom} +15 -13
- package/circom/utils/non-qv/ResultCommitmentVerifier.circom +84 -0
- package/circom/utils/non-qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +39 -34
- package/circom/utils/qv/{messageValidator.circom → MessageValidator.circom} +15 -13
- package/circom/utils/qv/ResultCommitmentVerifier.circom +107 -0
- package/circom/utils/qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +39 -34
- package/circom/utils/trees/BinaryMerkleRoot.circom +6 -3
- package/circom/utils/trees/LeafExists.circom +2 -2
- package/circom/utils/trees/MerkleTreeInclusionProof.circom +4 -4
- package/circom/utils/trees/QuinaryCheckRoot.circom +54 -0
- package/circom/utils/trees/{MerkleGeneratePathIndices.circom → QuinaryGeneratePathIndices.circom} +18 -18
- package/circom/utils/trees/QuinaryLeafExists.circom +30 -0
- package/circom/utils/trees/QuinarySelector.circom +42 -0
- package/circom/utils/trees/QuinaryTreeInclusionProof.circom +55 -0
- package/circom/utils/trees/Splicer.circom +76 -0
- package/circom/voter/PollJoined.circom +5 -5
- package/circom/voter/PollJoining.circom +3 -3
- package/package.json +20 -17
- package/build/ts/genZkeys.d.ts.map +0 -1
- package/build/ts/genZkeys.js.map +0 -1
- package/circom/coordinator/non-qv/processMessages.circom +0 -449
- package/circom/coordinator/non-qv/tallyVotes.circom +0 -235
- package/circom/coordinator/qv/processMessages.circom +0 -451
- package/circom/coordinator/qv/tallyVotes.circom +0 -279
- package/circom/utils/trees/incrementalQuinaryTree.circom +0 -287
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./comparators.circom";
|
|
5
|
+
// local imports
|
|
6
|
+
include "../trees/QuinaryCheckRoot.circom";
|
|
7
|
+
include "../PoseidonHasher.circom";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Performs verifications and computations related to current voting results.
|
|
11
|
+
* Also, computes and outputs a commitment to the new results.
|
|
12
|
+
* This template does not support Quadratic Voting (QV).
|
|
13
|
+
*/
|
|
14
|
+
template ResultCommitmentVerifierNonQv(voteOptionTreeDepth) {
|
|
15
|
+
// Number of children per node in the tree, defining the tree's branching factor.
|
|
16
|
+
var TREE_ARITY = 5;
|
|
17
|
+
// Number of voting options available, determined by the depth of the vote option tree.
|
|
18
|
+
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
19
|
+
|
|
20
|
+
// Equal to 1 if this is the first batch, otherwise 0.
|
|
21
|
+
signal input isFirstBatch;
|
|
22
|
+
// Commitment to the current tally before this batch.
|
|
23
|
+
signal input currentTallyCommitment;
|
|
24
|
+
// Commitment to the new tally after processing this batch.
|
|
25
|
+
signal input newTallyCommitment;
|
|
26
|
+
|
|
27
|
+
// Current results for each vote option.
|
|
28
|
+
signal input currentResults[totalVoteOptions];
|
|
29
|
+
// Salt for the root of the current results.
|
|
30
|
+
signal input currentResultsRootSalt;
|
|
31
|
+
|
|
32
|
+
// New results for each vote option.
|
|
33
|
+
signal input newResults[totalVoteOptions];
|
|
34
|
+
// Salt for the root of the new results.
|
|
35
|
+
signal input newResultsRootSalt;
|
|
36
|
+
|
|
37
|
+
// Total voice credits spent so far.
|
|
38
|
+
signal input currentSpentVoiceCreditSubtotal;
|
|
39
|
+
// Salt for the total spent voice credits.
|
|
40
|
+
signal input currentSpentVoiceCreditSubtotalSalt;
|
|
41
|
+
|
|
42
|
+
// Total new voice credits spent.
|
|
43
|
+
signal input newSpentVoiceCreditSubtotal;
|
|
44
|
+
// Salt for the new total spent voice credits.
|
|
45
|
+
signal input newSpentVoiceCreditSubtotalSalt;
|
|
46
|
+
|
|
47
|
+
// Compute the commitment to the current results.
|
|
48
|
+
var computedCurrentResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentResults);
|
|
49
|
+
|
|
50
|
+
// Verify currentResultsCommitmentHash.
|
|
51
|
+
var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
|
|
52
|
+
|
|
53
|
+
// Compute the commitment to the current spent voice credits.
|
|
54
|
+
var computedCurrentSpentVoiceCreditsCommitment = PoseidonHasher(2)([currentSpentVoiceCreditSubtotal, currentSpentVoiceCreditSubtotalSalt]);
|
|
55
|
+
|
|
56
|
+
// Commit to the current tally
|
|
57
|
+
var computedCurrentTallyCommitment = PoseidonHasher(2)([computedCurrentResultsCommitment, computedCurrentSpentVoiceCreditsCommitment]);
|
|
58
|
+
|
|
59
|
+
// Check if the current tally commitment is correct only if this is not the first batch.
|
|
60
|
+
// computedIsZero.out is 1 if this is not the first batch.
|
|
61
|
+
// computedIsZero.out is 0 if this is the first batch.
|
|
62
|
+
var computedIsZero = IsZero()(isFirstBatch);
|
|
63
|
+
|
|
64
|
+
// isFirstCommitment is 0 if this is the first batch, currentTallyCommitment should be 0 if this is the first batch.
|
|
65
|
+
// isFirstCommitment is 1 if this is not the first batch, currentTallyCommitment should not be 0 if this is the first batch.
|
|
66
|
+
signal isFirstCommitment;
|
|
67
|
+
isFirstCommitment <== computedIsZero * computedCurrentTallyCommitment;
|
|
68
|
+
isFirstCommitment === currentTallyCommitment;
|
|
69
|
+
|
|
70
|
+
// Compute the root of the new results.
|
|
71
|
+
var computedNewResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newResults);
|
|
72
|
+
var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
|
|
73
|
+
|
|
74
|
+
// Compute the commitment to the new spent voice credits value.
|
|
75
|
+
var computedNewSpentVoiceCreditsCommitment = PoseidonHasher(2)([newSpentVoiceCreditSubtotal, newSpentVoiceCreditSubtotalSalt]);
|
|
76
|
+
|
|
77
|
+
// Commit to the new tally.
|
|
78
|
+
var computedNewTallyCommitment = PoseidonHasher(2)([
|
|
79
|
+
computedNewResultsCommitment,
|
|
80
|
+
computedNewSpentVoiceCreditsCommitment
|
|
81
|
+
]);
|
|
82
|
+
|
|
83
|
+
computedNewTallyCommitment === newTallyCommitment;
|
|
84
|
+
}
|
|
@@ -3,7 +3,7 @@ pragma circom 2.0.0;
|
|
|
3
3
|
// circomlib import
|
|
4
4
|
include "./mux1.circom";
|
|
5
5
|
// local import
|
|
6
|
-
include "./
|
|
6
|
+
include "./MessageValidator.circom";
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Processes a command by verifying its validity and updates the state leaf and ballot accordingly.
|
|
@@ -13,18 +13,18 @@ include "./messageValidator.circom";
|
|
|
13
13
|
*/
|
|
14
14
|
template StateLeafAndBallotTransformerNonQv() {
|
|
15
15
|
// Length of the packed command.
|
|
16
|
-
var
|
|
16
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
17
17
|
|
|
18
18
|
// Number of user sign-ups in the state tree.
|
|
19
|
-
signal input
|
|
19
|
+
signal input totalSignups;
|
|
20
20
|
// Number of valid vote options for the poll.
|
|
21
21
|
signal input voteOptions;
|
|
22
22
|
|
|
23
23
|
// The following signals represents a state leaf (signed up user).
|
|
24
24
|
// Public key.
|
|
25
|
-
signal input
|
|
25
|
+
signal input stateLeafPublicKey[2];
|
|
26
26
|
// Current voice credit balance.
|
|
27
|
-
signal input
|
|
27
|
+
signal input stateLeafVoiceCreditBalance;
|
|
28
28
|
|
|
29
29
|
// The following signals represents a ballot.
|
|
30
30
|
// Nonce.
|
|
@@ -34,29 +34,29 @@ template StateLeafAndBallotTransformerNonQv() {
|
|
|
34
34
|
|
|
35
35
|
// The following signals represents a command.
|
|
36
36
|
// State index of the user.
|
|
37
|
-
signal input
|
|
37
|
+
signal input commandStateIndex;
|
|
38
38
|
// Public key of the user.
|
|
39
|
-
signal input
|
|
39
|
+
signal input commandPublicKey[2];
|
|
40
40
|
// Vote option index.
|
|
41
|
-
signal input
|
|
41
|
+
signal input commandVoteOptionIndex;
|
|
42
42
|
// Vote weight.
|
|
43
|
-
signal input
|
|
43
|
+
signal input commandNewVoteWeight;
|
|
44
44
|
// Nonce.
|
|
45
|
-
signal input
|
|
45
|
+
signal input commandNonce;
|
|
46
46
|
// Poll identifier.
|
|
47
|
-
signal input
|
|
47
|
+
signal input commandPollId;
|
|
48
48
|
// Salt.
|
|
49
|
-
signal input
|
|
50
|
-
//
|
|
51
|
-
signal input
|
|
52
|
-
//
|
|
53
|
-
signal input
|
|
49
|
+
signal input commandSalt;
|
|
50
|
+
// EdDSA signature of the command (R part).
|
|
51
|
+
signal input commandSignaturePoint[2];
|
|
52
|
+
// EdDSA signature of the command (S part).
|
|
53
|
+
signal input commandSignatureScalar;
|
|
54
54
|
// Packed command.
|
|
55
55
|
// nb. we are assuming that the packedCommand is always valid.
|
|
56
|
-
signal input packedCommand[
|
|
56
|
+
signal input packedCommand[PACKED_COMMAND_LENGTH];
|
|
57
57
|
|
|
58
58
|
// New state leaf (if the command is valid).
|
|
59
|
-
signal output
|
|
59
|
+
signal output newStateLeafPublicKey[2];
|
|
60
60
|
// New ballot (if the command is valid).
|
|
61
61
|
signal output newBallotNonce;
|
|
62
62
|
|
|
@@ -68,38 +68,43 @@ template StateLeafAndBallotTransformerNonQv() {
|
|
|
68
68
|
signal output isVoteOptionIndexValid;
|
|
69
69
|
|
|
70
70
|
// Check if the command / message is valid.
|
|
71
|
-
var (
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
var (computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = MessageValidatorNonQv()(
|
|
72
|
+
commandStateIndex,
|
|
73
|
+
totalSignups,
|
|
74
|
+
commandVoteOptionIndex,
|
|
75
75
|
voteOptions,
|
|
76
76
|
ballotNonce,
|
|
77
|
-
|
|
77
|
+
commandNonce,
|
|
78
78
|
packedCommand,
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
79
|
+
stateLeafPublicKey,
|
|
80
|
+
commandSignaturePoint,
|
|
81
|
+
commandSignatureScalar,
|
|
82
|
+
stateLeafVoiceCreditBalance,
|
|
83
83
|
ballotCurrentVotesForOption,
|
|
84
|
-
|
|
84
|
+
commandNewVoteWeight
|
|
85
85
|
);
|
|
86
86
|
|
|
87
87
|
// If the message is valid then we swap out the public key.
|
|
88
88
|
// This means using a Mux1() for publicKey[0] and another one
|
|
89
89
|
// for publicKey[1].
|
|
90
|
-
var
|
|
91
|
-
var
|
|
90
|
+
var computedNewstateLeafPublicKey0Mux = Mux1()([stateLeafPublicKey[0], commandPublicKey[0]], computedIsValid);
|
|
91
|
+
var computedNewstateLeafPublicKey1Mux = Mux1()([stateLeafPublicKey[1], commandPublicKey[1]], computedIsValid);
|
|
92
92
|
|
|
93
|
-
|
|
94
|
-
|
|
93
|
+
newStateLeafPublicKey[0] <== computedNewstateLeafPublicKey0Mux;
|
|
94
|
+
newStateLeafPublicKey[1] <== computedNewstateLeafPublicKey1Mux;
|
|
95
95
|
|
|
96
96
|
// If the message is valid, then we swap out the ballot nonce
|
|
97
97
|
// using a Mux1().
|
|
98
|
-
var computedNewBallotNonceMux = Mux1()([ballotNonce,
|
|
98
|
+
var computedNewBallotNonceMux = Mux1()([ballotNonce, commandNonce], computedIsValid);
|
|
99
99
|
|
|
100
100
|
newBallotNonce <== computedNewBallotNonceMux;
|
|
101
101
|
|
|
102
|
-
isValid <==
|
|
102
|
+
isValid <== computedIsValid;
|
|
103
103
|
isStateLeafIndexValid <== computedIsStateLeafIndexValid;
|
|
104
104
|
isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
|
|
105
|
+
|
|
106
|
+
// Constrain commandPollId and commandSalt using dummy squares.
|
|
107
|
+
// This binds the proof to a specific poll and salt.
|
|
108
|
+
signal commandPollIdSquare <== commandPollId * commandPollId;
|
|
109
|
+
signal commandSaltSquared <== commandSalt * commandSalt;
|
|
105
110
|
}
|
|
@@ -11,12 +11,14 @@ include "../VerifySignature.circom";
|
|
|
11
11
|
*/
|
|
12
12
|
template MessageValidator() {
|
|
13
13
|
// Length of the packed command.
|
|
14
|
-
var
|
|
14
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
15
|
+
// Number of checks to be performed.
|
|
16
|
+
var TOTAL_CHECKS = 6;
|
|
15
17
|
|
|
16
18
|
// State index of the user.
|
|
17
19
|
signal input stateTreeIndex;
|
|
18
20
|
// Number of user sign-ups in the state tree.
|
|
19
|
-
signal input
|
|
21
|
+
signal input totalSignups;
|
|
20
22
|
// Vote option index.
|
|
21
23
|
signal input voteOptionIndex;
|
|
22
24
|
// Number of valid vote options for the poll.
|
|
@@ -24,15 +26,15 @@ template MessageValidator() {
|
|
|
24
26
|
// Ballot nonce.
|
|
25
27
|
signal input originalNonce;
|
|
26
28
|
// Command nonce.
|
|
27
|
-
signal input
|
|
29
|
+
signal input commandNonce;
|
|
28
30
|
// Packed command.
|
|
29
|
-
signal input
|
|
31
|
+
signal input command[PACKED_COMMAND_LENGTH];
|
|
30
32
|
// Public key of the state leaf (user).
|
|
31
33
|
signal input publicKey[2];
|
|
32
|
-
//
|
|
33
|
-
signal input
|
|
34
|
-
//
|
|
35
|
-
signal input
|
|
34
|
+
// EdDSA signature of the command (R part).
|
|
35
|
+
signal input signaturePoint[2];
|
|
36
|
+
// EdDSA signature of the command (S part).
|
|
37
|
+
signal input signatureScalar;
|
|
36
38
|
// State leaf current voice credit balance.
|
|
37
39
|
signal input currentVoiceCreditBalance;
|
|
38
40
|
// Current number of votes for specific option.
|
|
@@ -48,19 +50,19 @@ template MessageValidator() {
|
|
|
48
50
|
signal output isVoteOptionIndexValid;
|
|
49
51
|
|
|
50
52
|
// Check (1) - The state leaf index must be valid.
|
|
51
|
-
// The check ensure that the stateTreeIndex <
|
|
53
|
+
// The check ensure that the stateTreeIndex < totalSignups as first validation.
|
|
52
54
|
// Must be < because the stateTreeIndex is 0-based. Zero is for blank state leaf
|
|
53
55
|
// while 1 is for the first actual user.
|
|
54
|
-
var computedIsStateLeafIndexValid = SafeLessThan(252)([stateTreeIndex,
|
|
56
|
+
var computedIsStateLeafIndexValid = SafeLessThan(252)([stateTreeIndex, totalSignups]);
|
|
55
57
|
|
|
56
58
|
// Check (2) - The vote option index must be less than the number of valid vote options (0 indexed).
|
|
57
59
|
var computedIsVoteOptionIndexValid = SafeLessThan(252)([voteOptionIndex, voteOptions]);
|
|
58
60
|
|
|
59
61
|
// Check (3) - The nonce must be correct.
|
|
60
|
-
var computedIsNonceValid = IsEqual()([originalNonce + 1,
|
|
62
|
+
var computedIsNonceValid = IsEqual()([originalNonce + 1, commandNonce]);
|
|
61
63
|
|
|
62
64
|
// Check (4) - The signature must be correct.
|
|
63
|
-
var computedIsSignatureValid = VerifySignature()(publicKey,
|
|
65
|
+
var computedIsSignatureValid = VerifySignature()(publicKey, signaturePoint, signatureScalar, command);
|
|
64
66
|
|
|
65
67
|
// Check (5) - There must be sufficient voice credits.
|
|
66
68
|
// The check ensure that the voteWeight is < sqrt(field size)
|
|
@@ -79,7 +81,7 @@ template MessageValidator() {
|
|
|
79
81
|
// When all six checks are correct, then isValid = 1.
|
|
80
82
|
var computedIsUpdateValid = IsEqual()(
|
|
81
83
|
[
|
|
82
|
-
|
|
84
|
+
TOTAL_CHECKS,
|
|
83
85
|
computedIsSignatureValid +
|
|
84
86
|
computedAreVoiceCreditsSufficient +
|
|
85
87
|
computedIsVoteWeightValid +
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./comparators.circom";
|
|
5
|
+
// local imports
|
|
6
|
+
include "../trees/QuinaryCheckRoot.circom";
|
|
7
|
+
include "../PoseidonHasher.circom";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Performs verifications and computations related to current voting results.
|
|
11
|
+
* Also, computes and outputs a commitment to the new results.
|
|
12
|
+
* This template supports the Quadratic Voting (QV).
|
|
13
|
+
*/
|
|
14
|
+
template ResultCommitmentVerifierQv(voteOptionTreeDepth) {
|
|
15
|
+
// Number of children per node in the tree, defining the tree's branching factor.
|
|
16
|
+
var TREE_ARITY = 5;
|
|
17
|
+
// Number of voting options available, determined by the depth of the vote option tree.
|
|
18
|
+
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
19
|
+
|
|
20
|
+
// Equal to 1 if this is the first batch, otherwise 0.
|
|
21
|
+
signal input isFirstBatch;
|
|
22
|
+
// Commitment to the current tally before this batch.
|
|
23
|
+
signal input currentTallyCommitment;
|
|
24
|
+
// Commitment to the new tally after processing this batch.
|
|
25
|
+
signal input newTallyCommitment;
|
|
26
|
+
|
|
27
|
+
// Current results for each vote option.
|
|
28
|
+
signal input currentResults[totalVoteOptions];
|
|
29
|
+
// Salt for the root of the current results.
|
|
30
|
+
signal input currentResultsRootSalt;
|
|
31
|
+
|
|
32
|
+
// New results for each vote option.
|
|
33
|
+
signal input newResults[totalVoteOptions];
|
|
34
|
+
// Salt for the root of the new results.
|
|
35
|
+
signal input newResultsRootSalt;
|
|
36
|
+
|
|
37
|
+
// Total voice credits spent so far.
|
|
38
|
+
signal input currentSpentVoiceCreditSubtotal;
|
|
39
|
+
// Salt for the total spent voice credits.
|
|
40
|
+
signal input currentSpentVoiceCreditSubtotalSalt;
|
|
41
|
+
|
|
42
|
+
// Total new voice credits spent.
|
|
43
|
+
signal input newSpentVoiceCreditSubtotal;
|
|
44
|
+
// Salt for the new total spent voice credits.
|
|
45
|
+
signal input newSpentVoiceCreditSubtotalSalt;
|
|
46
|
+
|
|
47
|
+
// Spent voice credits per vote option.
|
|
48
|
+
signal input currentPerVoteOptionSpentVoiceCredits[totalVoteOptions];
|
|
49
|
+
// Salt for the root of spent credits per option.
|
|
50
|
+
signal input currentPerVoteOptionSpentVoiceCreditsRootSalt;
|
|
51
|
+
|
|
52
|
+
// New spent voice credits per vote option.
|
|
53
|
+
signal input newPerVoteOptionSpentVoiceCredits[totalVoteOptions];
|
|
54
|
+
// Salt for the root of new spent credits per option.
|
|
55
|
+
signal input newPerVoteOptionSpentVoiceCreditsRootSalt;
|
|
56
|
+
|
|
57
|
+
// Compute the commitment to the current results.
|
|
58
|
+
var computedCurrentResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentResults);
|
|
59
|
+
|
|
60
|
+
// Verify currentResultsCommitmentHash.
|
|
61
|
+
var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
|
|
62
|
+
|
|
63
|
+
// Compute the commitment to the current spent voice credits.
|
|
64
|
+
var computedCurrentSpentVoiceCreditsCommitment = PoseidonHasher(2)([currentSpentVoiceCreditSubtotal, currentSpentVoiceCreditSubtotalSalt]);
|
|
65
|
+
|
|
66
|
+
// Compute the root of the spent voice credits per vote option.
|
|
67
|
+
var computedCurrentPerVoteOptionSpentVoiceCreditsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentPerVoteOptionSpentVoiceCredits);
|
|
68
|
+
var computedCurrentPerVoteOptionSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedCurrentPerVoteOptionSpentVoiceCreditsRoot, currentPerVoteOptionSpentVoiceCreditsRootSalt]);
|
|
69
|
+
|
|
70
|
+
// Commit to the current tally.
|
|
71
|
+
var computedCurrentTallyCommitment = PoseidonHasher(3)([
|
|
72
|
+
computedCurrentResultsCommitment,
|
|
73
|
+
computedCurrentSpentVoiceCreditsCommitment,
|
|
74
|
+
computedCurrentPerVoteOptionSpentVoiceCreditsCommitment
|
|
75
|
+
]);
|
|
76
|
+
|
|
77
|
+
// Check if the current tally commitment is correct only if this is not the first batch.
|
|
78
|
+
// computedIsZero.out is 1 if this is not the first batch.
|
|
79
|
+
// computedIsZero.out is 0 if this is the first batch.
|
|
80
|
+
var computedIsZero = IsZero()(isFirstBatch);
|
|
81
|
+
|
|
82
|
+
// isFirstCommitment is 0 if this is the first batch, currentTallyCommitment should be 0 if this is the first batch.
|
|
83
|
+
// isFirstCommitment is 1 if this is not the first batch, currentTallyCommitment should not be 0 if this is the first batch.
|
|
84
|
+
signal isFirstCommitment;
|
|
85
|
+
isFirstCommitment <== computedIsZero * computedCurrentTallyCommitment;
|
|
86
|
+
isFirstCommitment === currentTallyCommitment;
|
|
87
|
+
|
|
88
|
+
// Compute the root of the new results.
|
|
89
|
+
var computedNewResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newResults);
|
|
90
|
+
var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
|
|
91
|
+
|
|
92
|
+
// Compute the commitment to the new spent voice credits value.
|
|
93
|
+
var computedNewSpentVoiceCreditsCommitment = PoseidonHasher(2)([newSpentVoiceCreditSubtotal, newSpentVoiceCreditSubtotalSalt]);
|
|
94
|
+
|
|
95
|
+
// Compute the root of the spent voice credits per vote option.
|
|
96
|
+
var computedNewPerVoteOptionSpentVoiceCreditsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newPerVoteOptionSpentVoiceCredits);
|
|
97
|
+
var computedNewPerVoteOptionSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedNewPerVoteOptionSpentVoiceCreditsRoot, newPerVoteOptionSpentVoiceCreditsRootSalt]);
|
|
98
|
+
|
|
99
|
+
// Commit to the new tally.
|
|
100
|
+
var computedNewTallyCommitment = PoseidonHasher(3)([
|
|
101
|
+
computedNewResultsCommitment,
|
|
102
|
+
computedNewSpentVoiceCreditsCommitment,
|
|
103
|
+
computedNewPerVoteOptionSpentVoiceCreditsCommitment
|
|
104
|
+
]);
|
|
105
|
+
|
|
106
|
+
computedNewTallyCommitment === newTallyCommitment;
|
|
107
|
+
}
|
|
@@ -3,7 +3,7 @@ pragma circom 2.0.0;
|
|
|
3
3
|
// circomlib import
|
|
4
4
|
include "./mux1.circom";
|
|
5
5
|
// local import
|
|
6
|
-
include "./
|
|
6
|
+
include "./MessageValidator.circom";
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Processes a command by verifying its validity and updates the state leaf and ballot accordingly.
|
|
@@ -13,18 +13,18 @@ include "./messageValidator.circom";
|
|
|
13
13
|
*/
|
|
14
14
|
template StateLeafAndBallotTransformer() {
|
|
15
15
|
// Length of the packed command.
|
|
16
|
-
var
|
|
16
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
17
17
|
|
|
18
18
|
// Number of user sign-ups in the state tree.
|
|
19
|
-
signal input
|
|
19
|
+
signal input totalSignups;
|
|
20
20
|
// Number of valid vote options for the poll.
|
|
21
21
|
signal input voteOptions;
|
|
22
22
|
|
|
23
23
|
// The following signals represents a state leaf (signed up user).
|
|
24
24
|
// Public key.
|
|
25
|
-
signal input
|
|
25
|
+
signal input stateLeafPublicKey[2];
|
|
26
26
|
// Current voice credit balance.
|
|
27
|
-
signal input
|
|
27
|
+
signal input stateLeafVoiceCreditBalance;
|
|
28
28
|
|
|
29
29
|
// The following signals represents a ballot.
|
|
30
30
|
// Nonce.
|
|
@@ -34,29 +34,29 @@ template StateLeafAndBallotTransformer() {
|
|
|
34
34
|
|
|
35
35
|
// The following signals represents a command.
|
|
36
36
|
// State index of the user.
|
|
37
|
-
signal input
|
|
37
|
+
signal input commandStateIndex;
|
|
38
38
|
// Public key of the user.
|
|
39
|
-
signal input
|
|
39
|
+
signal input commandPublicKey[2];
|
|
40
40
|
// Vote option index.
|
|
41
|
-
signal input
|
|
41
|
+
signal input commandVoteOptionIndex;
|
|
42
42
|
// Vote weight.
|
|
43
|
-
signal input
|
|
43
|
+
signal input commandNewVoteWeight;
|
|
44
44
|
// Nonce.
|
|
45
|
-
signal input
|
|
45
|
+
signal input commandNonce;
|
|
46
46
|
// Poll identifier.
|
|
47
|
-
signal input
|
|
47
|
+
signal input commandPollId;
|
|
48
48
|
// Salt.
|
|
49
|
-
signal input
|
|
50
|
-
//
|
|
51
|
-
signal input
|
|
52
|
-
//
|
|
53
|
-
signal input
|
|
49
|
+
signal input commandSalt;
|
|
50
|
+
// EdDSA signature of the command (R part).
|
|
51
|
+
signal input commandSignaturePoint[2];
|
|
52
|
+
// EdDSA signature of the command (S part).
|
|
53
|
+
signal input commandSignatureScalar;
|
|
54
54
|
// Packed command.
|
|
55
55
|
// nb. we are assuming that the packedCommand is always valid.
|
|
56
|
-
signal input packedCommand[
|
|
56
|
+
signal input packedCommand[PACKED_COMMAND_LENGTH];
|
|
57
57
|
|
|
58
58
|
// New state leaf (if the command is valid).
|
|
59
|
-
signal output
|
|
59
|
+
signal output newStateLeafPublicKey[2];
|
|
60
60
|
// New ballot (if the command is valid).
|
|
61
61
|
signal output newBallotNonce;
|
|
62
62
|
|
|
@@ -68,38 +68,43 @@ template StateLeafAndBallotTransformer() {
|
|
|
68
68
|
signal output isVoteOptionIndexValid;
|
|
69
69
|
|
|
70
70
|
// Check if the command / message is valid.
|
|
71
|
-
var (
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
var (computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = MessageValidator()(
|
|
72
|
+
commandStateIndex,
|
|
73
|
+
totalSignups,
|
|
74
|
+
commandVoteOptionIndex,
|
|
75
75
|
voteOptions,
|
|
76
76
|
ballotNonce,
|
|
77
|
-
|
|
77
|
+
commandNonce,
|
|
78
78
|
packedCommand,
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
79
|
+
stateLeafPublicKey,
|
|
80
|
+
commandSignaturePoint,
|
|
81
|
+
commandSignatureScalar,
|
|
82
|
+
stateLeafVoiceCreditBalance,
|
|
83
83
|
ballotCurrentVotesForOption,
|
|
84
|
-
|
|
84
|
+
commandNewVoteWeight
|
|
85
85
|
);
|
|
86
86
|
|
|
87
87
|
// If the message is valid then we swap out the public key.
|
|
88
88
|
// This means using a Mux1() for publicKey[0] and another one
|
|
89
89
|
// for publicKey[1].
|
|
90
|
-
var
|
|
91
|
-
var
|
|
90
|
+
var computedNewstateLeafPublicKey0Mux = Mux1()([stateLeafPublicKey[0], commandPublicKey[0]], computedIsValid);
|
|
91
|
+
var computedNewstateLeafPublicKey1Mux = Mux1()([stateLeafPublicKey[1], commandPublicKey[1]], computedIsValid);
|
|
92
92
|
|
|
93
|
-
|
|
94
|
-
|
|
93
|
+
newStateLeafPublicKey[0] <== computedNewstateLeafPublicKey0Mux;
|
|
94
|
+
newStateLeafPublicKey[1] <== computedNewstateLeafPublicKey1Mux;
|
|
95
95
|
|
|
96
96
|
// If the message is valid, then we swap out the ballot nonce
|
|
97
97
|
// using a Mux1().
|
|
98
|
-
var computedNewBallotNonceMux = Mux1()([ballotNonce,
|
|
98
|
+
var computedNewBallotNonceMux = Mux1()([ballotNonce, commandNonce], computedIsValid);
|
|
99
99
|
|
|
100
100
|
newBallotNonce <== computedNewBallotNonceMux;
|
|
101
101
|
|
|
102
|
-
isValid <==
|
|
102
|
+
isValid <== computedIsValid;
|
|
103
103
|
isStateLeafIndexValid <== computedIsStateLeafIndexValid;
|
|
104
104
|
isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
|
|
105
|
+
|
|
106
|
+
// Constrain commandPollId and commandSalt using dummy squares.
|
|
107
|
+
// This binds the proof to a specific poll and salt.
|
|
108
|
+
signal commandPollIdSquare <== commandPollId * commandPollId;
|
|
109
|
+
signal commandSaltSquared <== commandSalt * commandSalt;
|
|
105
110
|
}
|
|
@@ -7,7 +7,7 @@ include "./comparators.circom";
|
|
|
7
7
|
include "../PoseidonHasher.circom";
|
|
8
8
|
|
|
9
9
|
// @note taken from @zk-kit/circuits
|
|
10
|
-
// if used directly in
|
|
10
|
+
// if used directly in MessageProcessor circom complains about duplicated
|
|
11
11
|
// templates (Ark, Poseidon, etc.)
|
|
12
12
|
// This circuit is designed to calculate the root of a binary Merkle
|
|
13
13
|
// tree given a leaf, its depth, and the necessary sibling
|
|
@@ -25,14 +25,17 @@ template BinaryMerkleRoot(MAX_DEPTH) {
|
|
|
25
25
|
signal input leaf;
|
|
26
26
|
// The depth of the Merkle tree.
|
|
27
27
|
signal input depth;
|
|
28
|
-
// The
|
|
29
|
-
signal input
|
|
28
|
+
// The index of the leaf node in the Merkle tree.
|
|
29
|
+
signal input index;
|
|
30
30
|
// The sibling nodes of the leaf node in the Merkle tree.
|
|
31
31
|
signal input siblings[MAX_DEPTH][1];
|
|
32
32
|
|
|
33
33
|
// The output of the Merkle tree root.
|
|
34
34
|
signal output out;
|
|
35
35
|
|
|
36
|
+
// The indices of the leaf node in the Merkle tree.
|
|
37
|
+
signal indices[MAX_DEPTH] <== Num2Bits(MAX_DEPTH)(index);
|
|
38
|
+
|
|
36
39
|
signal nodes[MAX_DEPTH + 1];
|
|
37
40
|
nodes[0] <== leaf;
|
|
38
41
|
|
|
@@ -13,13 +13,13 @@ template LeafExists(levels) {
|
|
|
13
13
|
// The elements along the path needed for the inclusion proof.
|
|
14
14
|
signal input path_elements[levels][1];
|
|
15
15
|
// The indices indicating the path taken through the tree for the leaf.
|
|
16
|
-
signal input
|
|
16
|
+
signal input path_indices[levels];
|
|
17
17
|
// The root of the Merkle tree, against which the inclusion is verified.
|
|
18
18
|
signal input root;
|
|
19
19
|
|
|
20
20
|
var computedMerkleRoot = MerkleTreeInclusionProof(levels)(
|
|
21
21
|
leaf,
|
|
22
|
-
|
|
22
|
+
path_indices,
|
|
23
23
|
path_elements
|
|
24
24
|
);
|
|
25
25
|
|
|
@@ -12,7 +12,7 @@ template MerkleTreeInclusionProof(n_levels) {
|
|
|
12
12
|
// The leaf node from which the Merkle root is calculated.
|
|
13
13
|
signal input leaf;
|
|
14
14
|
// Indices indicating left or right child for each level of the tree.
|
|
15
|
-
signal input
|
|
15
|
+
signal input path_indices[n_levels];
|
|
16
16
|
// Sibling node values required to compute the hash at each level.
|
|
17
17
|
signal input path_elements[n_levels][1];
|
|
18
18
|
|
|
@@ -25,8 +25,8 @@ template MerkleTreeInclusionProof(n_levels) {
|
|
|
25
25
|
levelHashes[0] <== leaf;
|
|
26
26
|
|
|
27
27
|
for (var i = 0; i < n_levels; i++) {
|
|
28
|
-
// Validate
|
|
29
|
-
|
|
28
|
+
// Validate path_indices to be either 0 or 1, ensuring no other values.
|
|
29
|
+
path_indices[i] * (1 - path_indices[i]) === 0;
|
|
30
30
|
|
|
31
31
|
// Configure the multiplexer based on the path index for the current level.
|
|
32
32
|
var multiplexer[2][2] = [
|
|
@@ -36,7 +36,7 @@ template MerkleTreeInclusionProof(n_levels) {
|
|
|
36
36
|
|
|
37
37
|
var multiplexerResult[2] = MultiMux1(2)(
|
|
38
38
|
multiplexer,
|
|
39
|
-
|
|
39
|
+
path_indices[i]
|
|
40
40
|
);
|
|
41
41
|
|
|
42
42
|
var computedLevelHash = PoseidonHasher(2)([multiplexerResult[0], multiplexerResult[1]]);
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// local imports
|
|
4
|
+
include "../PoseidonHasher.circom";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Computes the root of a quintary Merkle tree given a list of leaves.
|
|
8
|
+
* This template constructs a Merkle tree with each node having 5 children (quintary)
|
|
9
|
+
* and computes the root by hashing with Poseidon the leaves and intermediate nodes in the given order.
|
|
10
|
+
* The computation is performed by first hashing groups of 5 leaves to form the bottom layer of nodes,
|
|
11
|
+
* then recursively hashing groups of these nodes to form the next layer, and so on, until the root is computed.
|
|
12
|
+
*/
|
|
13
|
+
template QuinaryCheckRoot(levels) {
|
|
14
|
+
var LEAVES_PER_NODE = 5;
|
|
15
|
+
var totalLeaves = LEAVES_PER_NODE ** levels;
|
|
16
|
+
var numLeafHashers = LEAVES_PER_NODE ** (levels - 1);
|
|
17
|
+
|
|
18
|
+
signal input leaves[totalLeaves];
|
|
19
|
+
signal output root;
|
|
20
|
+
|
|
21
|
+
// Determine the total number of hashers.
|
|
22
|
+
var numHashers = 0;
|
|
23
|
+
for (var i = 0; i < levels; i++) {
|
|
24
|
+
numHashers += LEAVES_PER_NODE ** i;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var computedHashers[numHashers];
|
|
28
|
+
|
|
29
|
+
// Initialize hashers for the leaves.
|
|
30
|
+
for (var i = 0; i < numLeafHashers; i++) {
|
|
31
|
+
computedHashers[i] = PoseidonHasher(5)([
|
|
32
|
+
leaves[i * LEAVES_PER_NODE + 0],
|
|
33
|
+
leaves[i * LEAVES_PER_NODE + 1],
|
|
34
|
+
leaves[i * LEAVES_PER_NODE + 2],
|
|
35
|
+
leaves[i * LEAVES_PER_NODE + 3],
|
|
36
|
+
leaves[i * LEAVES_PER_NODE + 4]
|
|
37
|
+
]);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Initialize hashers for intermediate nodes and compute the root.
|
|
41
|
+
var k = 0;
|
|
42
|
+
for (var i = numLeafHashers; i < numHashers; i++) {
|
|
43
|
+
computedHashers[i] = PoseidonHasher(5)([
|
|
44
|
+
computedHashers[k * LEAVES_PER_NODE + 0],
|
|
45
|
+
computedHashers[k * LEAVES_PER_NODE + 1],
|
|
46
|
+
computedHashers[k * LEAVES_PER_NODE + 2],
|
|
47
|
+
computedHashers[k * LEAVES_PER_NODE + 3],
|
|
48
|
+
computedHashers[k * LEAVES_PER_NODE + 4]
|
|
49
|
+
]);
|
|
50
|
+
k++;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
root <== computedHashers[numHashers - 1];
|
|
54
|
+
}
|