@maci-protocol/circuits 0.0.0-ci.01622be
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/CHANGELOG.md +673 -0
- package/LICENSE +21 -0
- package/README.md +20 -0
- package/build/ts/compile.d.ts +10 -0
- package/build/ts/compile.d.ts.map +1 -0
- package/build/ts/compile.js +123 -0
- package/build/ts/compile.js.map +1 -0
- package/build/ts/generateZkeys.d.ts +9 -0
- package/build/ts/generateZkeys.d.ts.map +1 -0
- package/build/ts/generateZkeys.js +67 -0
- package/build/ts/generateZkeys.js.map +1 -0
- package/build/ts/info.d.ts +2 -0
- package/build/ts/info.d.ts.map +1 -0
- package/build/ts/info.js +72 -0
- package/build/ts/info.js.map +1 -0
- package/build/ts/types.d.ts +104 -0
- package/build/ts/types.d.ts.map +1 -0
- package/build/ts/types.js +3 -0
- package/build/ts/types.js.map +1 -0
- package/build/tsconfig.build.tsbuildinfo +1 -0
- package/circom/circuits.json +74 -0
- package/circom/coordinator/full/MessageProcessor.circom +253 -0
- package/circom/coordinator/full/SingleMessageProcessor.circom +204 -0
- package/circom/coordinator/non-qv/processMessages.circom +447 -0
- package/circom/coordinator/non-qv/tallyVotes.circom +238 -0
- package/circom/coordinator/qv/processMessages.circom +452 -0
- package/circom/coordinator/qv/tallyVotes.circom +279 -0
- package/circom/utils/CalculateTotal.circom +24 -0
- package/circom/utils/EdDSAPoseidonVerifier.circom +91 -0
- package/circom/utils/MessageHasher.circom +57 -0
- package/circom/utils/MessageToCommand.circom +107 -0
- package/circom/utils/PoseidonHasher.circom +29 -0
- package/circom/utils/PrivateToPublicKey.circom +38 -0
- package/circom/utils/VerifySignature.circom +39 -0
- package/circom/utils/full/MessageValidator.circom +91 -0
- package/circom/utils/full/StateLeafAndBallotTransformer.circom +122 -0
- package/circom/utils/non-qv/MessageValidator.circom +91 -0
- package/circom/utils/non-qv/StateLeafAndBallotTransformer.circom +105 -0
- package/circom/utils/qv/MessageValidator.circom +97 -0
- package/circom/utils/qv/StateLeafAndBallotTransformer.circom +105 -0
- package/circom/utils/trees/BinaryMerkleRoot.circom +62 -0
- package/circom/utils/trees/CheckRoot.circom +49 -0
- package/circom/utils/trees/LeafExists.circom +27 -0
- package/circom/utils/trees/MerklePathIndicesGenerator.circom +44 -0
- package/circom/utils/trees/MerkleTreeInclusionProof.circom +50 -0
- package/circom/utils/trees/QuinaryCheckRoot.circom +54 -0
- package/circom/utils/trees/QuinaryGeneratePathIndices.circom +44 -0
- 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 +43 -0
- package/circom/voter/PollJoining.circom +54 -0
- package/circomkit.json +18 -0
- package/package.json +74 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// zk-kit imports
|
|
4
|
+
include "./safe-comparators.circom";
|
|
5
|
+
// local imports
|
|
6
|
+
include "../VerifySignature.circom";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Checks if a MACI message is valid or not.
|
|
10
|
+
* This template supports the full mode (all credits are spent on one option)
|
|
11
|
+
*/
|
|
12
|
+
template MessageValidatorFull() {
|
|
13
|
+
// Length of the packed command.
|
|
14
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
15
|
+
// Number of checks to be performed.
|
|
16
|
+
var TOTAL_CHECKS = 5;
|
|
17
|
+
|
|
18
|
+
// State index of the user.
|
|
19
|
+
signal input stateTreeIndex;
|
|
20
|
+
// Number of user sign-ups in the state tree.
|
|
21
|
+
signal input totalSignups;
|
|
22
|
+
// Vote option index.
|
|
23
|
+
signal input voteOptionIndex;
|
|
24
|
+
// Number of valid vote options for the poll.
|
|
25
|
+
signal input voteOptions;
|
|
26
|
+
// Ballot nonce.
|
|
27
|
+
signal input originalNonce;
|
|
28
|
+
// Command nonce.
|
|
29
|
+
signal input commandNonce;
|
|
30
|
+
// Packed command.
|
|
31
|
+
signal input command[PACKED_COMMAND_LENGTH];
|
|
32
|
+
// Public key of the state leaf (user).
|
|
33
|
+
signal input publicKey[2];
|
|
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;
|
|
38
|
+
// State leaf current voice credit balance.
|
|
39
|
+
signal input currentVoiceCreditBalance;
|
|
40
|
+
// Current number of votes for specific option.
|
|
41
|
+
signal input currentVotesForOption;
|
|
42
|
+
// Vote weight.
|
|
43
|
+
signal input voteWeight;
|
|
44
|
+
|
|
45
|
+
// True when the command is valid; otherwise false.
|
|
46
|
+
signal output isValid;
|
|
47
|
+
// True if the state leaf index is valid
|
|
48
|
+
signal output isStateLeafIndexValid;
|
|
49
|
+
// True if the vote option index is valid
|
|
50
|
+
signal output isVoteOptionIndexValid;
|
|
51
|
+
|
|
52
|
+
// Check (1) - The state leaf index must be valid.
|
|
53
|
+
// The check ensure that the stateTreeIndex < totalSignups as first validation.
|
|
54
|
+
// Must be < because the stateTreeIndex is 0-based. Zero is for blank state leaf
|
|
55
|
+
// while 1 is for the first actual user.
|
|
56
|
+
var computedIsStateLeafIndexValid = SafeLessThan(252)([stateTreeIndex, totalSignups]);
|
|
57
|
+
|
|
58
|
+
// Check (2) - The vote option index must be less than the number of valid vote options (0 indexed).
|
|
59
|
+
var computedIsVoteOptionIndexValid = SafeLessThan(252)([voteOptionIndex, voteOptions]);
|
|
60
|
+
|
|
61
|
+
// Check (3) - The nonce must be correct.
|
|
62
|
+
var computedIsNonceValid = IsEqual()([originalNonce + 1, commandNonce]);
|
|
63
|
+
|
|
64
|
+
// Check (4) - The signature must be correct.
|
|
65
|
+
var computedIsSignatureValid = VerifySignature()(publicKey, signaturePoint, signatureScalar, command);
|
|
66
|
+
|
|
67
|
+
// Check (5) - There must be sufficient voice credits.
|
|
68
|
+
// The check ensure that currentVotesForOption + currentVoiceCreditBalance is equal to voteWeight.
|
|
69
|
+
var computedAreVoiceCreditsSpent = IsEqual()(
|
|
70
|
+
[
|
|
71
|
+
currentVotesForOption + currentVoiceCreditBalance,
|
|
72
|
+
voteWeight
|
|
73
|
+
]
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
// When all five checks are correct, then isValid = 1.
|
|
77
|
+
var computedIsUpdateValid = IsEqual()(
|
|
78
|
+
[
|
|
79
|
+
TOTAL_CHECKS,
|
|
80
|
+
computedIsSignatureValid +
|
|
81
|
+
computedAreVoiceCreditsSpent +
|
|
82
|
+
computedIsNonceValid +
|
|
83
|
+
computedIsStateLeafIndexValid +
|
|
84
|
+
computedIsVoteOptionIndexValid
|
|
85
|
+
]
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
isValid <== computedIsUpdateValid;
|
|
89
|
+
isStateLeafIndexValid <== computedIsStateLeafIndexValid;
|
|
90
|
+
isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
|
|
91
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./mux1.circom";
|
|
5
|
+
// local import
|
|
6
|
+
include "./MessageValidator.circom";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Processes a command by verifying its validity and updates the state leaf and ballot accordingly.
|
|
10
|
+
* If the message is correct, updates the public key in the state leaf and the nonce
|
|
11
|
+
* in the ballot using multiplexer components.
|
|
12
|
+
* This template supports the full mode (all credits are spent on one option)
|
|
13
|
+
*/
|
|
14
|
+
template StateLeafAndBallotTransformerFull() {
|
|
15
|
+
// Length of the packed command.
|
|
16
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
17
|
+
|
|
18
|
+
// Number of user sign-ups in the state tree.
|
|
19
|
+
signal input totalSignups;
|
|
20
|
+
// Number of valid vote options for the poll.
|
|
21
|
+
signal input voteOptions;
|
|
22
|
+
|
|
23
|
+
// The following signals represents a state leaf (signed up user).
|
|
24
|
+
// Public key.
|
|
25
|
+
signal input stateLeafPublicKey[2];
|
|
26
|
+
// Current voice credit balance.
|
|
27
|
+
signal input stateLeafVoiceCreditBalance;
|
|
28
|
+
|
|
29
|
+
// The following signals represents a ballot.
|
|
30
|
+
// Nonce.
|
|
31
|
+
signal input ballotNonce;
|
|
32
|
+
// Current number of votes for specific option.
|
|
33
|
+
signal input ballotCurrentVotesForOption;
|
|
34
|
+
|
|
35
|
+
// The following signals represents a command.
|
|
36
|
+
// State index of the user.
|
|
37
|
+
signal input commandStateIndex;
|
|
38
|
+
// Public key of the user.
|
|
39
|
+
signal input commandPublicKey[2];
|
|
40
|
+
// Vote option index.
|
|
41
|
+
signal input commandVoteOptionIndex;
|
|
42
|
+
// Vote weight.
|
|
43
|
+
signal input commandNewVoteWeight;
|
|
44
|
+
// Nonce.
|
|
45
|
+
signal input commandNonce;
|
|
46
|
+
// Poll identifier.
|
|
47
|
+
signal input commandPollId;
|
|
48
|
+
// Salt.
|
|
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
|
+
// Packed command.
|
|
55
|
+
// nb. we are assuming that the packedCommand is always valid.
|
|
56
|
+
signal input packedCommand[PACKED_COMMAND_LENGTH];
|
|
57
|
+
|
|
58
|
+
// New state leaf (if the command is valid).
|
|
59
|
+
signal output newStateLeafPublicKey[2];
|
|
60
|
+
// New ballot (if the command is valid).
|
|
61
|
+
signal output newBallotNonce;
|
|
62
|
+
|
|
63
|
+
// True when the command is valid; otherwise false.
|
|
64
|
+
signal output isValid;
|
|
65
|
+
// True if the state leaf index is valid
|
|
66
|
+
signal output isStateLeafIndexValid;
|
|
67
|
+
// True if the vote option index is valid
|
|
68
|
+
signal output isVoteOptionIndexValid;
|
|
69
|
+
|
|
70
|
+
// Check if the command / message is valid.
|
|
71
|
+
var (
|
|
72
|
+
computedIsValid,
|
|
73
|
+
computedIsStateLeafIndexValid,
|
|
74
|
+
computedIsVoteOptionIndexValid
|
|
75
|
+
) = MessageValidatorFull()(
|
|
76
|
+
commandStateIndex,
|
|
77
|
+
totalSignups,
|
|
78
|
+
commandVoteOptionIndex,
|
|
79
|
+
voteOptions,
|
|
80
|
+
ballotNonce,
|
|
81
|
+
commandNonce,
|
|
82
|
+
packedCommand,
|
|
83
|
+
stateLeafPublicKey,
|
|
84
|
+
commandSignaturePoint,
|
|
85
|
+
commandSignatureScalar,
|
|
86
|
+
stateLeafVoiceCreditBalance,
|
|
87
|
+
ballotCurrentVotesForOption,
|
|
88
|
+
commandNewVoteWeight
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
// If the message is valid then we swap out the public key.
|
|
92
|
+
// This means using a Mux1() for publicKey[0] and another one
|
|
93
|
+
// for publicKey[1].
|
|
94
|
+
var computedNewstateLeafPublicKey0Mux = Mux1()(
|
|
95
|
+
[
|
|
96
|
+
stateLeafPublicKey[0],
|
|
97
|
+
commandPublicKey[0]
|
|
98
|
+
],
|
|
99
|
+
computedIsValid
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
var computedNewstateLeafPublicKey1Mux = Mux1()(
|
|
103
|
+
[
|
|
104
|
+
stateLeafPublicKey[1],
|
|
105
|
+
commandPublicKey[1]
|
|
106
|
+
],
|
|
107
|
+
computedIsValid
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
newStateLeafPublicKey[0] <== computedNewstateLeafPublicKey0Mux;
|
|
111
|
+
newStateLeafPublicKey[1] <== computedNewstateLeafPublicKey1Mux;
|
|
112
|
+
|
|
113
|
+
// If the message is valid, then we swap out the ballot nonce
|
|
114
|
+
// using a Mux1().
|
|
115
|
+
var computedNewBallotNonceMux = Mux1()([ballotNonce, commandNonce], computedIsValid);
|
|
116
|
+
|
|
117
|
+
newBallotNonce <== computedNewBallotNonceMux;
|
|
118
|
+
|
|
119
|
+
isValid <== computedIsValid;
|
|
120
|
+
isStateLeafIndexValid <== computedIsStateLeafIndexValid;
|
|
121
|
+
isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
|
|
122
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// zk-kit imports
|
|
4
|
+
include "./safe-comparators.circom";
|
|
5
|
+
// local imports
|
|
6
|
+
include "../VerifySignature.circom";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Checks if a MACI message is valid or not.
|
|
10
|
+
* This template does not support Quadratic Voting (QV).
|
|
11
|
+
*/
|
|
12
|
+
template MessageValidatorNonQv() {
|
|
13
|
+
// Length of the packed command.
|
|
14
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
15
|
+
// Number of checks to be performed.
|
|
16
|
+
var TOTAL_CHECKS = 5;
|
|
17
|
+
|
|
18
|
+
// State index of the user.
|
|
19
|
+
signal input stateTreeIndex;
|
|
20
|
+
// Number of user sign-ups in the state tree.
|
|
21
|
+
signal input totalSignups;
|
|
22
|
+
// Vote option index.
|
|
23
|
+
signal input voteOptionIndex;
|
|
24
|
+
// Number of valid vote options for the poll.
|
|
25
|
+
signal input voteOptions;
|
|
26
|
+
// Ballot nonce.
|
|
27
|
+
signal input originalNonce;
|
|
28
|
+
// Command nonce.
|
|
29
|
+
signal input commandNonce;
|
|
30
|
+
// Packed command.
|
|
31
|
+
signal input command[PACKED_COMMAND_LENGTH];
|
|
32
|
+
// Public key of the state leaf (user).
|
|
33
|
+
signal input publicKey[2];
|
|
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;
|
|
38
|
+
// State leaf current voice credit balance.
|
|
39
|
+
signal input currentVoiceCreditBalance;
|
|
40
|
+
// Current number of votes for specific option.
|
|
41
|
+
signal input currentVotesForOption;
|
|
42
|
+
// Vote weight.
|
|
43
|
+
signal input voteWeight;
|
|
44
|
+
|
|
45
|
+
// True when the command is valid; otherwise false.
|
|
46
|
+
signal output isValid;
|
|
47
|
+
// True if the state leaf index is valid
|
|
48
|
+
signal output isStateLeafIndexValid;
|
|
49
|
+
// True if the vote option index is valid
|
|
50
|
+
signal output isVoteOptionIndexValid;
|
|
51
|
+
|
|
52
|
+
// Check (1) - The state leaf index must be valid.
|
|
53
|
+
// The check ensure that the stateTreeIndex < totalSignups as first validation.
|
|
54
|
+
// Must be < because the stateTreeIndex is 0-based. Zero is for blank state leaf
|
|
55
|
+
// while 1 is for the first actual user.
|
|
56
|
+
var computedIsStateLeafIndexValid = SafeLessThan(252)([stateTreeIndex, totalSignups]);
|
|
57
|
+
|
|
58
|
+
// Check (2) - The vote option index must be less than the number of valid vote options (0 indexed).
|
|
59
|
+
var computedIsVoteOptionIndexValid = SafeLessThan(252)([voteOptionIndex, voteOptions]);
|
|
60
|
+
|
|
61
|
+
// Check (3) - The nonce must be correct.
|
|
62
|
+
var computedIsNonceValid = IsEqual()([originalNonce + 1, commandNonce]);
|
|
63
|
+
|
|
64
|
+
// Check (4) - The signature must be correct.
|
|
65
|
+
var computedIsSignatureValid = VerifySignature()(publicKey, signaturePoint, signatureScalar, command);
|
|
66
|
+
|
|
67
|
+
// Check (5) - There must be sufficient voice credits.
|
|
68
|
+
// The check ensure that currentVoiceCreditBalance + (currentVotesForOption) >= (voteWeight).
|
|
69
|
+
var computedAreVoiceCreditsSufficient = SafeGreaterEqThan(252)(
|
|
70
|
+
[
|
|
71
|
+
currentVotesForOption + currentVoiceCreditBalance,
|
|
72
|
+
voteWeight
|
|
73
|
+
]
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
// When all five checks are correct, then isValid = 1.
|
|
77
|
+
var computedIsUpdateValid = IsEqual()(
|
|
78
|
+
[
|
|
79
|
+
TOTAL_CHECKS,
|
|
80
|
+
computedIsSignatureValid +
|
|
81
|
+
computedAreVoiceCreditsSufficient +
|
|
82
|
+
computedIsNonceValid +
|
|
83
|
+
computedIsStateLeafIndexValid +
|
|
84
|
+
computedIsVoteOptionIndexValid
|
|
85
|
+
]
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
isValid <== computedIsUpdateValid;
|
|
89
|
+
isStateLeafIndexValid <== computedIsStateLeafIndexValid;
|
|
90
|
+
isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
|
|
91
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./mux1.circom";
|
|
5
|
+
// local import
|
|
6
|
+
include "./MessageValidator.circom";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Processes a command by verifying its validity and updates the state leaf and ballot accordingly.
|
|
10
|
+
* If the message is correct, updates the public key in the state leaf and the nonce
|
|
11
|
+
* in the ballot using multiplexer components.
|
|
12
|
+
* This template does not support Quadratic Voting (QV).
|
|
13
|
+
*/
|
|
14
|
+
template StateLeafAndBallotTransformerNonQv() {
|
|
15
|
+
// Length of the packed command.
|
|
16
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
17
|
+
|
|
18
|
+
// Number of user sign-ups in the state tree.
|
|
19
|
+
signal input totalSignups;
|
|
20
|
+
// Number of valid vote options for the poll.
|
|
21
|
+
signal input voteOptions;
|
|
22
|
+
|
|
23
|
+
// The following signals represents a state leaf (signed up user).
|
|
24
|
+
// Public key.
|
|
25
|
+
signal input stateLeafPublicKey[2];
|
|
26
|
+
// Current voice credit balance.
|
|
27
|
+
signal input stateLeafVoiceCreditBalance;
|
|
28
|
+
|
|
29
|
+
// The following signals represents a ballot.
|
|
30
|
+
// Nonce.
|
|
31
|
+
signal input ballotNonce;
|
|
32
|
+
// Current number of votes for specific option.
|
|
33
|
+
signal input ballotCurrentVotesForOption;
|
|
34
|
+
|
|
35
|
+
// The following signals represents a command.
|
|
36
|
+
// State index of the user.
|
|
37
|
+
signal input commandStateIndex;
|
|
38
|
+
// Public key of the user.
|
|
39
|
+
signal input commandPublicKey[2];
|
|
40
|
+
// Vote option index.
|
|
41
|
+
signal input commandVoteOptionIndex;
|
|
42
|
+
// Vote weight.
|
|
43
|
+
signal input commandNewVoteWeight;
|
|
44
|
+
// Nonce.
|
|
45
|
+
signal input commandNonce;
|
|
46
|
+
// Poll identifier.
|
|
47
|
+
signal input commandPollId;
|
|
48
|
+
// Salt.
|
|
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
|
+
// Packed command.
|
|
55
|
+
// nb. we are assuming that the packedCommand is always valid.
|
|
56
|
+
signal input packedCommand[PACKED_COMMAND_LENGTH];
|
|
57
|
+
|
|
58
|
+
// New state leaf (if the command is valid).
|
|
59
|
+
signal output newStateLeafPublicKey[2];
|
|
60
|
+
// New ballot (if the command is valid).
|
|
61
|
+
signal output newBallotNonce;
|
|
62
|
+
|
|
63
|
+
// True when the command is valid; otherwise false.
|
|
64
|
+
signal output isValid;
|
|
65
|
+
// True if the state leaf index is valid
|
|
66
|
+
signal output isStateLeafIndexValid;
|
|
67
|
+
// True if the vote option index is valid
|
|
68
|
+
signal output isVoteOptionIndexValid;
|
|
69
|
+
|
|
70
|
+
// Check if the command / message is valid.
|
|
71
|
+
var (computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = MessageValidatorNonQv()(
|
|
72
|
+
commandStateIndex,
|
|
73
|
+
totalSignups,
|
|
74
|
+
commandVoteOptionIndex,
|
|
75
|
+
voteOptions,
|
|
76
|
+
ballotNonce,
|
|
77
|
+
commandNonce,
|
|
78
|
+
packedCommand,
|
|
79
|
+
stateLeafPublicKey,
|
|
80
|
+
commandSignaturePoint,
|
|
81
|
+
commandSignatureScalar,
|
|
82
|
+
stateLeafVoiceCreditBalance,
|
|
83
|
+
ballotCurrentVotesForOption,
|
|
84
|
+
commandNewVoteWeight
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
// If the message is valid then we swap out the public key.
|
|
88
|
+
// This means using a Mux1() for publicKey[0] and another one
|
|
89
|
+
// for publicKey[1].
|
|
90
|
+
var computedNewstateLeafPublicKey0Mux = Mux1()([stateLeafPublicKey[0], commandPublicKey[0]], computedIsValid);
|
|
91
|
+
var computedNewstateLeafPublicKey1Mux = Mux1()([stateLeafPublicKey[1], commandPublicKey[1]], computedIsValid);
|
|
92
|
+
|
|
93
|
+
newStateLeafPublicKey[0] <== computedNewstateLeafPublicKey0Mux;
|
|
94
|
+
newStateLeafPublicKey[1] <== computedNewstateLeafPublicKey1Mux;
|
|
95
|
+
|
|
96
|
+
// If the message is valid, then we swap out the ballot nonce
|
|
97
|
+
// using a Mux1().
|
|
98
|
+
var computedNewBallotNonceMux = Mux1()([ballotNonce, commandNonce], computedIsValid);
|
|
99
|
+
|
|
100
|
+
newBallotNonce <== computedNewBallotNonceMux;
|
|
101
|
+
|
|
102
|
+
isValid <== computedIsValid;
|
|
103
|
+
isStateLeafIndexValid <== computedIsStateLeafIndexValid;
|
|
104
|
+
isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
|
|
105
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// zk-kit imports
|
|
4
|
+
include "./safe-comparators.circom";
|
|
5
|
+
// local imports
|
|
6
|
+
include "../VerifySignature.circom";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Checks if a MACI message is valid or not.
|
|
10
|
+
* This template supports the Quadratic Voting (QV).
|
|
11
|
+
*/
|
|
12
|
+
template MessageValidator() {
|
|
13
|
+
// Length of the packed command.
|
|
14
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
15
|
+
// Number of checks to be performed.
|
|
16
|
+
var TOTAL_CHECKS = 6;
|
|
17
|
+
|
|
18
|
+
// State index of the user.
|
|
19
|
+
signal input stateTreeIndex;
|
|
20
|
+
// Number of user sign-ups in the state tree.
|
|
21
|
+
signal input totalSignups;
|
|
22
|
+
// Vote option index.
|
|
23
|
+
signal input voteOptionIndex;
|
|
24
|
+
// Number of valid vote options for the poll.
|
|
25
|
+
signal input voteOptions;
|
|
26
|
+
// Ballot nonce.
|
|
27
|
+
signal input originalNonce;
|
|
28
|
+
// Command nonce.
|
|
29
|
+
signal input commandNonce;
|
|
30
|
+
// Packed command.
|
|
31
|
+
signal input command[PACKED_COMMAND_LENGTH];
|
|
32
|
+
// Public key of the state leaf (user).
|
|
33
|
+
signal input publicKey[2];
|
|
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;
|
|
38
|
+
// State leaf current voice credit balance.
|
|
39
|
+
signal input currentVoiceCreditBalance;
|
|
40
|
+
// Current number of votes for specific option.
|
|
41
|
+
signal input currentVotesForOption;
|
|
42
|
+
// Vote weight.
|
|
43
|
+
signal input voteWeight;
|
|
44
|
+
|
|
45
|
+
// True when the command is valid; otherwise false.
|
|
46
|
+
signal output isValid;
|
|
47
|
+
// True if the state leaf index is valid
|
|
48
|
+
signal output isStateLeafIndexValid;
|
|
49
|
+
// True if the vote option index is valid
|
|
50
|
+
signal output isVoteOptionIndexValid;
|
|
51
|
+
|
|
52
|
+
// Check (1) - The state leaf index must be valid.
|
|
53
|
+
// The check ensure that the stateTreeIndex < totalSignups as first validation.
|
|
54
|
+
// Must be < because the stateTreeIndex is 0-based. Zero is for blank state leaf
|
|
55
|
+
// while 1 is for the first actual user.
|
|
56
|
+
var computedIsStateLeafIndexValid = SafeLessThan(252)([stateTreeIndex, totalSignups]);
|
|
57
|
+
|
|
58
|
+
// Check (2) - The vote option index must be less than the number of valid vote options (0 indexed).
|
|
59
|
+
var computedIsVoteOptionIndexValid = SafeLessThan(252)([voteOptionIndex, voteOptions]);
|
|
60
|
+
|
|
61
|
+
// Check (3) - The nonce must be correct.
|
|
62
|
+
var computedIsNonceValid = IsEqual()([originalNonce + 1, commandNonce]);
|
|
63
|
+
|
|
64
|
+
// Check (4) - The signature must be correct.
|
|
65
|
+
var computedIsSignatureValid = VerifySignature()(publicKey, signaturePoint, signatureScalar, command);
|
|
66
|
+
|
|
67
|
+
// Check (5) - There must be sufficient voice credits.
|
|
68
|
+
// The check ensure that the voteWeight is < sqrt(field size)
|
|
69
|
+
// so that voteWeight ^ 2 will not overflow.
|
|
70
|
+
var computedIsVoteWeightValid = SafeLessEqThan(252)([voteWeight, 147946756881789319005730692170996259609]);
|
|
71
|
+
|
|
72
|
+
// Check (6) - Check the current voice credit balance.
|
|
73
|
+
// The check ensure that currentVoiceCreditBalance + (currentVotesForOption ** 2) >= (voteWeight ** 2)
|
|
74
|
+
var computedAreVoiceCreditsSufficient = SafeGreaterEqThan(252)(
|
|
75
|
+
[
|
|
76
|
+
(currentVotesForOption * currentVotesForOption) + currentVoiceCreditBalance,
|
|
77
|
+
voteWeight * voteWeight
|
|
78
|
+
]
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
// When all six checks are correct, then isValid = 1.
|
|
82
|
+
var computedIsUpdateValid = IsEqual()(
|
|
83
|
+
[
|
|
84
|
+
TOTAL_CHECKS,
|
|
85
|
+
computedIsSignatureValid +
|
|
86
|
+
computedAreVoiceCreditsSufficient +
|
|
87
|
+
computedIsVoteWeightValid +
|
|
88
|
+
computedIsNonceValid +
|
|
89
|
+
computedIsStateLeafIndexValid +
|
|
90
|
+
computedIsVoteOptionIndexValid
|
|
91
|
+
]
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
isValid <== computedIsUpdateValid;
|
|
95
|
+
isStateLeafIndexValid <== computedIsStateLeafIndexValid;
|
|
96
|
+
isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
|
|
97
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./mux1.circom";
|
|
5
|
+
// local import
|
|
6
|
+
include "./MessageValidator.circom";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Processes a command by verifying its validity and updates the state leaf and ballot accordingly.
|
|
10
|
+
* If the message is correct, updates the public key in the state leaf and the nonce
|
|
11
|
+
* in the ballot using multiplexer components.
|
|
12
|
+
* This template supports the Quadratic Voting (QV).
|
|
13
|
+
*/
|
|
14
|
+
template StateLeafAndBallotTransformer() {
|
|
15
|
+
// Length of the packed command.
|
|
16
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
17
|
+
|
|
18
|
+
// Number of user sign-ups in the state tree.
|
|
19
|
+
signal input totalSignups;
|
|
20
|
+
// Number of valid vote options for the poll.
|
|
21
|
+
signal input voteOptions;
|
|
22
|
+
|
|
23
|
+
// The following signals represents a state leaf (signed up user).
|
|
24
|
+
// Public key.
|
|
25
|
+
signal input stateLeafPublicKey[2];
|
|
26
|
+
// Current voice credit balance.
|
|
27
|
+
signal input stateLeafVoiceCreditBalance;
|
|
28
|
+
|
|
29
|
+
// The following signals represents a ballot.
|
|
30
|
+
// Nonce.
|
|
31
|
+
signal input ballotNonce;
|
|
32
|
+
// Current number of votes for specific option.
|
|
33
|
+
signal input ballotCurrentVotesForOption;
|
|
34
|
+
|
|
35
|
+
// The following signals represents a command.
|
|
36
|
+
// State index of the user.
|
|
37
|
+
signal input commandStateIndex;
|
|
38
|
+
// Public key of the user.
|
|
39
|
+
signal input commandPublicKey[2];
|
|
40
|
+
// Vote option index.
|
|
41
|
+
signal input commandVoteOptionIndex;
|
|
42
|
+
// Vote weight.
|
|
43
|
+
signal input commandNewVoteWeight;
|
|
44
|
+
// Nonce.
|
|
45
|
+
signal input commandNonce;
|
|
46
|
+
// Poll identifier.
|
|
47
|
+
signal input commandPollId;
|
|
48
|
+
// Salt.
|
|
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
|
+
// Packed command.
|
|
55
|
+
// nb. we are assuming that the packedCommand is always valid.
|
|
56
|
+
signal input packedCommand[PACKED_COMMAND_LENGTH];
|
|
57
|
+
|
|
58
|
+
// New state leaf (if the command is valid).
|
|
59
|
+
signal output newStateLeafPublicKey[2];
|
|
60
|
+
// New ballot (if the command is valid).
|
|
61
|
+
signal output newBallotNonce;
|
|
62
|
+
|
|
63
|
+
// True when the command is valid; otherwise false.
|
|
64
|
+
signal output isValid;
|
|
65
|
+
// True if the state leaf index is valid
|
|
66
|
+
signal output isStateLeafIndexValid;
|
|
67
|
+
// True if the vote option index is valid
|
|
68
|
+
signal output isVoteOptionIndexValid;
|
|
69
|
+
|
|
70
|
+
// Check if the command / message is valid.
|
|
71
|
+
var (computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = MessageValidator()(
|
|
72
|
+
commandStateIndex,
|
|
73
|
+
totalSignups,
|
|
74
|
+
commandVoteOptionIndex,
|
|
75
|
+
voteOptions,
|
|
76
|
+
ballotNonce,
|
|
77
|
+
commandNonce,
|
|
78
|
+
packedCommand,
|
|
79
|
+
stateLeafPublicKey,
|
|
80
|
+
commandSignaturePoint,
|
|
81
|
+
commandSignatureScalar,
|
|
82
|
+
stateLeafVoiceCreditBalance,
|
|
83
|
+
ballotCurrentVotesForOption,
|
|
84
|
+
commandNewVoteWeight
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
// If the message is valid then we swap out the public key.
|
|
88
|
+
// This means using a Mux1() for publicKey[0] and another one
|
|
89
|
+
// for publicKey[1].
|
|
90
|
+
var computedNewstateLeafPublicKey0Mux = Mux1()([stateLeafPublicKey[0], commandPublicKey[0]], computedIsValid);
|
|
91
|
+
var computedNewstateLeafPublicKey1Mux = Mux1()([stateLeafPublicKey[1], commandPublicKey[1]], computedIsValid);
|
|
92
|
+
|
|
93
|
+
newStateLeafPublicKey[0] <== computedNewstateLeafPublicKey0Mux;
|
|
94
|
+
newStateLeafPublicKey[1] <== computedNewstateLeafPublicKey1Mux;
|
|
95
|
+
|
|
96
|
+
// If the message is valid, then we swap out the ballot nonce
|
|
97
|
+
// using a Mux1().
|
|
98
|
+
var computedNewBallotNonceMux = Mux1()([ballotNonce, commandNonce], computedIsValid);
|
|
99
|
+
|
|
100
|
+
newBallotNonce <== computedNewBallotNonceMux;
|
|
101
|
+
|
|
102
|
+
isValid <== computedIsValid;
|
|
103
|
+
isStateLeafIndexValid <== computedIsStateLeafIndexValid;
|
|
104
|
+
isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
|
|
105
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./mux1.circom";
|
|
5
|
+
include "./comparators.circom";
|
|
6
|
+
// local import
|
|
7
|
+
include "../PoseidonHasher.circom";
|
|
8
|
+
|
|
9
|
+
// @note taken from @zk-kit/circuits
|
|
10
|
+
// if used directly in processMessages circom complains about duplicated
|
|
11
|
+
// templates (Ark, Poseidon, etc.)
|
|
12
|
+
// This circuit is designed to calculate the root of a binary Merkle
|
|
13
|
+
// tree given a leaf, its depth, and the necessary sibling
|
|
14
|
+
// information (aka proof of membership).
|
|
15
|
+
// A circuit is designed without the capability to iterate through
|
|
16
|
+
// a dynamic array. To address this, a parameter with the static maximum
|
|
17
|
+
// tree depth is defined (i.e. 'MAX_DEPTH'). And additionally, the circuit
|
|
18
|
+
// receives a dynamic depth as an input, which is utilized in calculating the
|
|
19
|
+
// true root of the Merkle tree. The actual depth of the Merkle tree
|
|
20
|
+
// may be equal to or less than the static maximum depth.
|
|
21
|
+
// NOTE: This circuit will successfully verify `out = 0` for `depth > MAX_DEPTH`.
|
|
22
|
+
// Make sure to enforce `depth <= MAX_DEPTH` outside the circuit.
|
|
23
|
+
template BinaryMerkleRoot(MAX_DEPTH) {
|
|
24
|
+
// The leaf node of the Merkle tree.
|
|
25
|
+
signal input leaf;
|
|
26
|
+
// The depth of the Merkle tree.
|
|
27
|
+
signal input depth;
|
|
28
|
+
// The indices of the leaf node in the Merkle tree.
|
|
29
|
+
signal input indices[MAX_DEPTH];
|
|
30
|
+
// The sibling nodes of the leaf node in the Merkle tree.
|
|
31
|
+
signal input siblings[MAX_DEPTH][1];
|
|
32
|
+
|
|
33
|
+
// The output of the Merkle tree root.
|
|
34
|
+
signal output out;
|
|
35
|
+
|
|
36
|
+
signal nodes[MAX_DEPTH + 1];
|
|
37
|
+
nodes[0] <== leaf;
|
|
38
|
+
|
|
39
|
+
signal roots[MAX_DEPTH];
|
|
40
|
+
var root = 0;
|
|
41
|
+
|
|
42
|
+
for (var i = 0; i < MAX_DEPTH; i++) {
|
|
43
|
+
var isDepth = IsEqual()([depth, i]);
|
|
44
|
+
|
|
45
|
+
roots[i] <== isDepth * nodes[i];
|
|
46
|
+
|
|
47
|
+
root += roots[i];
|
|
48
|
+
|
|
49
|
+
var c[2][2] = [
|
|
50
|
+
[nodes[i], siblings[i][0]],
|
|
51
|
+
[siblings[i][0], nodes[i]]
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
var childNodes[2] = MultiMux1(2)(c, indices[i]);
|
|
55
|
+
|
|
56
|
+
nodes[i + 1] <== PoseidonHasher(2)(childNodes);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
var isDepth = IsEqual()([depth, MAX_DEPTH]);
|
|
60
|
+
|
|
61
|
+
out <== root + isDepth * nodes[MAX_DEPTH];
|
|
62
|
+
}
|