@maci-protocol/circuits 0.0.0-ci.a577366 → 0.0.0-ci.a584b1a

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.
Files changed (40) hide show
  1. package/LICENSE +1 -2
  2. package/build/ts/{genZkeys.d.ts → generateZkeys.d.ts} +1 -1
  3. package/build/ts/generateZkeys.d.ts.map +1 -0
  4. package/build/ts/{genZkeys.js → generateZkeys.js} +1 -1
  5. package/build/ts/generateZkeys.js.map +1 -0
  6. package/build/ts/types.d.ts +11 -11
  7. package/build/ts/types.d.ts.map +1 -1
  8. package/build/tsconfig.build.tsbuildinfo +1 -1
  9. package/circom/circuits.json +7 -7
  10. package/circom/coordinator/non-qv/processMessages.circom +97 -100
  11. package/circom/coordinator/non-qv/tallyVotes.circom +35 -32
  12. package/circom/coordinator/qv/processMessages.circom +98 -98
  13. package/circom/coordinator/qv/tallyVotes.circom +54 -54
  14. package/circom/utils/{calculateTotal.circom → CalculateTotal.circom} +2 -0
  15. package/circom/utils/{verifySignature.circom → EdDSAPoseidonVerifier.circom} +40 -66
  16. package/circom/utils/MessageHasher.circom +57 -0
  17. package/circom/utils/MessageToCommand.circom +107 -0
  18. package/circom/utils/PoseidonHasher.circom +29 -0
  19. package/circom/utils/{privToPubKey.circom → PrivateToPublicKey.circom} +12 -10
  20. package/circom/utils/VerifySignature.circom +39 -0
  21. package/circom/utils/full/MessageValidator.circom +93 -0
  22. package/circom/utils/full/StateLeafAndBallotTransformer.circom +122 -0
  23. package/circom/utils/non-qv/{messageValidator.circom → MessageValidator.circom} +15 -13
  24. package/circom/utils/non-qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +36 -36
  25. package/circom/utils/qv/{messageValidator.circom → MessageValidator.circom} +15 -13
  26. package/circom/utils/qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +36 -36
  27. package/circom/utils/trees/BinaryMerkleRoot.circom +11 -3
  28. package/circom/utils/trees/CheckRoot.circom +18 -14
  29. package/circom/utils/trees/LeafExists.circom +1 -1
  30. package/circom/utils/trees/{MerkleGeneratePathIndices.circom → MerklePathIndicesGenerator.circom} +11 -7
  31. package/circom/utils/trees/MerkleTreeInclusionProof.circom +6 -5
  32. package/circom/utils/trees/incrementalQuinaryTree.circom +2 -2
  33. package/circom/voter/PollJoined.circom +43 -0
  34. package/circom/voter/PollJoining.circom +54 -0
  35. package/package.json +13 -10
  36. package/build/ts/genZkeys.d.ts.map +0 -1
  37. package/build/ts/genZkeys.js.map +0 -1
  38. package/circom/utils/hashers.circom +0 -78
  39. package/circom/utils/messageToCommand.circom +0 -78
  40. package/circom/voter/poll.circom +0 -92
@@ -3,7 +3,7 @@ pragma circom 2.0.0;
3
3
  // zk-kit imports
4
4
  include "./safe-comparators.circom";
5
5
  // local imports
6
- include "../verifySignature.circom";
6
+ include "../VerifySignature.circom";
7
7
 
8
8
  /**
9
9
  * Checks if a MACI message is valid or not.
@@ -11,12 +11,14 @@ include "../verifySignature.circom";
11
11
  */
12
12
  template MessageValidatorNonQv() {
13
13
  // Length of the packed command.
14
- var PACKED_CMD_LENGTH = 4;
14
+ var PACKED_COMMAND_LENGTH = 4;
15
+ // Number of checks to be performed.
16
+ var TOTAL_CHECKS = 5;
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 numSignUps;
21
+ signal input totalSignups;
20
22
  // Vote option index.
21
23
  signal input voteOptionIndex;
22
24
  // Number of valid vote options for the poll.
@@ -26,13 +28,13 @@ template MessageValidatorNonQv() {
26
28
  // Command nonce.
27
29
  signal input nonce;
28
30
  // Packed command.
29
- signal input cmd[PACKED_CMD_LENGTH];
31
+ signal input command[PACKED_COMMAND_LENGTH];
30
32
  // Public key of the state leaf (user).
31
- signal input pubKey[2];
32
- // ECDSA signature of the command (R part).
33
- signal input sigR8[2];
34
- // ECDSA signature of the command (S part).
35
- signal input sigS;
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;
36
38
  // State leaf current voice credit balance.
37
39
  signal input currentVoiceCreditBalance;
38
40
  // Current number of votes for specific option.
@@ -48,10 +50,10 @@ template MessageValidatorNonQv() {
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 < numSignUps as first validation.
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, numSignUps]);
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]);
@@ -60,7 +62,7 @@ template MessageValidatorNonQv() {
60
62
  var computedIsNonceValid = IsEqual()([originalNonce + 1, nonce]);
61
63
 
62
64
  // Check (4) - The signature must be correct.
63
- var computedIsSignatureValid = VerifySignature()(pubKey, sigR8, sigS, cmd);
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 currentVoiceCreditBalance + (currentVotesForOption) >= (voteWeight).
@@ -74,7 +76,7 @@ template MessageValidatorNonQv() {
74
76
  // When all five checks are correct, then isValid = 1.
75
77
  var computedIsUpdateValid = IsEqual()(
76
78
  [
77
- 5,
79
+ TOTAL_CHECKS,
78
80
  computedIsSignatureValid +
79
81
  computedAreVoiceCreditsSufficient +
80
82
  computedIsNonceValid +
@@ -3,7 +3,7 @@ pragma circom 2.0.0;
3
3
  // circomlib import
4
4
  include "./mux1.circom";
5
5
  // local import
6
- include "./messageValidator.circom";
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 PACKED_CMD_LENGTH = 4;
16
+ var PACKED_COMMAND_LENGTH = 4;
17
17
 
18
18
  // Number of user sign-ups in the state tree.
19
- signal input numSignUps;
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 slPubKey[2];
25
+ signal input stateLeafPublicKey[2];
26
26
  // Current voice credit balance.
27
- signal input slVoiceCreditBalance;
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 cmdStateIndex;
37
+ signal input commandStateIndex;
38
38
  // Public key of the user.
39
- signal input cmdNewPubKey[2];
39
+ signal input commandPublicKey[2];
40
40
  // Vote option index.
41
- signal input cmdVoteOptionIndex;
41
+ signal input commandVoteOptionIndex;
42
42
  // Vote weight.
43
- signal input cmdNewVoteWeight;
43
+ signal input commandNewVoteWeight;
44
44
  // Nonce.
45
- signal input cmdNonce;
45
+ signal input commandNonce;
46
46
  // Poll identifier.
47
- signal input cmdPollId;
47
+ signal input commandPollId;
48
48
  // Salt.
49
- signal input cmdSalt;
50
- // ECDSA signature of the command (R part).
51
- signal input cmdSigR8[2];
52
- // ECDSA signature of the command (S part).
53
- signal input cmdSigS;
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[PACKED_CMD_LENGTH];
56
+ signal input packedCommand[PACKED_COMMAND_LENGTH];
57
57
 
58
58
  // New state leaf (if the command is valid).
59
- signal output newSlPubKey[2];
59
+ signal output newStateLeafPublicKey[2];
60
60
  // New ballot (if the command is valid).
61
61
  signal output newBallotNonce;
62
62
 
@@ -68,38 +68,38 @@ template StateLeafAndBallotTransformerNonQv() {
68
68
  signal output isVoteOptionIndexValid;
69
69
 
70
70
  // Check if the command / message is valid.
71
- var (computedMessageValidator, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = MessageValidatorNonQv()(
72
- cmdStateIndex,
73
- numSignUps,
74
- cmdVoteOptionIndex,
71
+ var (computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = MessageValidatorNonQv()(
72
+ commandStateIndex,
73
+ totalSignups,
74
+ commandVoteOptionIndex,
75
75
  voteOptions,
76
76
  ballotNonce,
77
- cmdNonce,
77
+ commandNonce,
78
78
  packedCommand,
79
- slPubKey,
80
- cmdSigR8,
81
- cmdSigS,
82
- slVoiceCreditBalance,
79
+ stateLeafPublicKey,
80
+ commandSignaturePoint,
81
+ commandSignatureScalar,
82
+ stateLeafVoiceCreditBalance,
83
83
  ballotCurrentVotesForOption,
84
- cmdNewVoteWeight
84
+ commandNewVoteWeight
85
85
  );
86
86
 
87
87
  // If the message is valid then we swap out the public key.
88
- // This means using a Mux1() for pubKey[0] and another one
89
- // for pubKey[1].
90
- var computedNewSlPubKey0Mux = Mux1()([slPubKey[0], cmdNewPubKey[0]], computedMessageValidator);
91
- var computedNewSlPubKey1Mux = Mux1()([slPubKey[1], cmdNewPubKey[1]], computedMessageValidator);
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
92
 
93
- newSlPubKey[0] <== computedNewSlPubKey0Mux;
94
- newSlPubKey[1] <== computedNewSlPubKey1Mux;
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, cmdNonce], computedMessageValidator);
98
+ var computedNewBallotNonceMux = Mux1()([ballotNonce, commandNonce], computedIsValid);
99
99
 
100
100
  newBallotNonce <== computedNewBallotNonceMux;
101
101
 
102
- isValid <== computedMessageValidator;
102
+ isValid <== computedIsValid;
103
103
  isStateLeafIndexValid <== computedIsStateLeafIndexValid;
104
104
  isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
105
105
  }
@@ -3,7 +3,7 @@ pragma circom 2.0.0;
3
3
  // zk-kit imports
4
4
  include "./safe-comparators.circom";
5
5
  // local imports
6
- include "../verifySignature.circom";
6
+ include "../VerifySignature.circom";
7
7
 
8
8
  /**
9
9
  * Checks if a MACI message is valid or not.
@@ -11,12 +11,14 @@ include "../verifySignature.circom";
11
11
  */
12
12
  template MessageValidator() {
13
13
  // Length of the packed command.
14
- var PACKED_CMD_LENGTH = 4;
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 numSignUps;
21
+ signal input totalSignups;
20
22
  // Vote option index.
21
23
  signal input voteOptionIndex;
22
24
  // Number of valid vote options for the poll.
@@ -26,13 +28,13 @@ template MessageValidator() {
26
28
  // Command nonce.
27
29
  signal input nonce;
28
30
  // Packed command.
29
- signal input cmd[PACKED_CMD_LENGTH];
31
+ signal input command[PACKED_COMMAND_LENGTH];
30
32
  // Public key of the state leaf (user).
31
- signal input pubKey[2];
32
- // ECDSA signature of the command (R part).
33
- signal input sigR8[2];
34
- // ECDSA signature of the command (S part).
35
- signal input sigS;
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;
36
38
  // State leaf current voice credit balance.
37
39
  signal input currentVoiceCreditBalance;
38
40
  // Current number of votes for specific option.
@@ -48,10 +50,10 @@ 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 < numSignUps as first validation.
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, numSignUps]);
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]);
@@ -60,7 +62,7 @@ template MessageValidator() {
60
62
  var computedIsNonceValid = IsEqual()([originalNonce + 1, nonce]);
61
63
 
62
64
  // Check (4) - The signature must be correct.
63
- var computedIsSignatureValid = VerifySignature()(pubKey, sigR8, sigS, cmd);
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
- 6,
84
+ TOTAL_CHECKS,
83
85
  computedIsSignatureValid +
84
86
  computedAreVoiceCreditsSufficient +
85
87
  computedIsVoteWeightValid +
@@ -3,7 +3,7 @@ pragma circom 2.0.0;
3
3
  // circomlib import
4
4
  include "./mux1.circom";
5
5
  // local import
6
- include "./messageValidator.circom";
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 PACKED_CMD_LENGTH = 4;
16
+ var PACKED_COMMAND_LENGTH = 4;
17
17
 
18
18
  // Number of user sign-ups in the state tree.
19
- signal input numSignUps;
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 slPubKey[2];
25
+ signal input stateLeafPublicKey[2];
26
26
  // Current voice credit balance.
27
- signal input slVoiceCreditBalance;
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 cmdStateIndex;
37
+ signal input commandStateIndex;
38
38
  // Public key of the user.
39
- signal input cmdNewPubKey[2];
39
+ signal input commandPublicKey[2];
40
40
  // Vote option index.
41
- signal input cmdVoteOptionIndex;
41
+ signal input commandVoteOptionIndex;
42
42
  // Vote weight.
43
- signal input cmdNewVoteWeight;
43
+ signal input commandNewVoteWeight;
44
44
  // Nonce.
45
- signal input cmdNonce;
45
+ signal input commandNonce;
46
46
  // Poll identifier.
47
- signal input cmdPollId;
47
+ signal input commandPollId;
48
48
  // Salt.
49
- signal input cmdSalt;
50
- // ECDSA signature of the command (R part).
51
- signal input cmdSigR8[2];
52
- // ECDSA signature of the command (S part).
53
- signal input cmdSigS;
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[PACKED_CMD_LENGTH];
56
+ signal input packedCommand[PACKED_COMMAND_LENGTH];
57
57
 
58
58
  // New state leaf (if the command is valid).
59
- signal output newSlPubKey[2];
59
+ signal output newStateLeafPublicKey[2];
60
60
  // New ballot (if the command is valid).
61
61
  signal output newBallotNonce;
62
62
 
@@ -68,38 +68,38 @@ template StateLeafAndBallotTransformer() {
68
68
  signal output isVoteOptionIndexValid;
69
69
 
70
70
  // Check if the command / message is valid.
71
- var (computedMessageValidator, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = MessageValidator()(
72
- cmdStateIndex,
73
- numSignUps,
74
- cmdVoteOptionIndex,
71
+ var (computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = MessageValidator()(
72
+ commandStateIndex,
73
+ totalSignups,
74
+ commandVoteOptionIndex,
75
75
  voteOptions,
76
76
  ballotNonce,
77
- cmdNonce,
77
+ commandNonce,
78
78
  packedCommand,
79
- slPubKey,
80
- cmdSigR8,
81
- cmdSigS,
82
- slVoiceCreditBalance,
79
+ stateLeafPublicKey,
80
+ commandSignaturePoint,
81
+ commandSignatureScalar,
82
+ stateLeafVoiceCreditBalance,
83
83
  ballotCurrentVotesForOption,
84
- cmdNewVoteWeight
84
+ commandNewVoteWeight
85
85
  );
86
86
 
87
87
  // If the message is valid then we swap out the public key.
88
- // This means using a Mux1() for pubKey[0] and another one
89
- // for pubKey[1].
90
- var computedNewSlPubKey0Mux = Mux1()([slPubKey[0], cmdNewPubKey[0]], computedMessageValidator);
91
- var computedNewSlPubKey1Mux = Mux1()([slPubKey[1], cmdNewPubKey[1]], computedMessageValidator);
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
92
 
93
- newSlPubKey[0] <== computedNewSlPubKey0Mux;
94
- newSlPubKey[1] <== computedNewSlPubKey1Mux;
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, cmdNonce], computedMessageValidator);
98
+ var computedNewBallotNonceMux = Mux1()([ballotNonce, commandNonce], computedIsValid);
99
99
 
100
100
  newBallotNonce <== computedNewBallotNonceMux;
101
101
 
102
- isValid <== computedMessageValidator;
102
+ isValid <== computedIsValid;
103
103
  isStateLeafIndexValid <== computedIsStateLeafIndexValid;
104
104
  isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
105
105
  }
@@ -4,7 +4,7 @@ pragma circom 2.0.0;
4
4
  include "./mux1.circom";
5
5
  include "./comparators.circom";
6
6
  // local import
7
- include "../hashers.circom";
7
+ include "../PoseidonHasher.circom";
8
8
 
9
9
  // @note taken from @zk-kit/circuits
10
10
  // if used directly in processMessages circom complains about duplicated
@@ -21,8 +21,16 @@ include "../hashers.circom";
21
21
  // NOTE: This circuit will successfully verify `out = 0` for `depth > MAX_DEPTH`.
22
22
  // Make sure to enforce `depth <= MAX_DEPTH` outside the circuit.
23
23
  template BinaryMerkleRoot(MAX_DEPTH) {
24
- signal input leaf, depth, indices[MAX_DEPTH], siblings[MAX_DEPTH][1];
25
-
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.
26
34
  signal output out;
27
35
 
28
36
  signal nodes[MAX_DEPTH + 1];
@@ -1,7 +1,7 @@
1
1
  pragma circom 2.0.0;
2
2
 
3
3
  // local import
4
- include "../hashers.circom";
4
+ include "../PoseidonHasher.circom";
5
5
 
6
6
  /**
7
7
  * Verifies the correct construction of a Merkle tree from a set of leaves.
@@ -10,36 +10,40 @@ include "../hashers.circom";
10
10
  */
11
11
  template CheckRoot(levels) {
12
12
  // The total number of leaves in the Merkle tree, calculated as 2 to the power of `levels`.
13
- var totalLeaves = 2 ** levels;
13
+ var TOTAL_LEVELS = 2 ** levels;
14
14
  // The number of first-level hashers needed, equal to half the total leaves, as each hasher combines two leaves.
15
- var numLeafHashers = totalLeaves / 2;
15
+ var LEAF_HASHERS = TOTAL_LEVELS / 2;
16
16
  // The number of intermediate hashers, one less than the number of leaf hashers,
17
17
  // as each level of hashing reduces the number of hash elements by about half.
18
- var numIntermediateHashers = numLeafHashers - 1;
18
+ var INTERMEDIATE_HASHERS = LEAF_HASHERS - 1;
19
19
 
20
20
  // Array of leaf values input to the circuit.
21
- signal input leaves[totalLeaves];
21
+ signal input leaves[TOTAL_LEVELS];
22
22
 
23
23
  // Output signal for the Merkle root that results from hashing all the input leaves.
24
24
  signal output root;
25
25
 
26
26
  // Total number of hashers used in constructing the tree, one less than the total number of leaves,
27
27
  // since each level of the tree combines two elements into one.
28
- var numHashers = totalLeaves - 1;
29
- var computedLevelHashers[numHashers];
28
+ var hashersLength = TOTAL_LEVELS - 1;
29
+ var computedLevelHashers[hashersLength];
30
30
 
31
31
  // Initialize hashers for the leaves, each taking two adjacent leaves as inputs.
32
- for (var i = 0; i < numLeafHashers; i++){
33
- computedLevelHashers[i] = PoseidonHasher(2)([leaves[i*2], leaves[i*2+1]]);
32
+ for (var i = 0; i < LEAF_HASHERS; i++){
33
+ computedLevelHashers[i] = PoseidonHasher(2)([leaves[i * 2], leaves[i * 2 + 1]]);
34
34
  }
35
35
 
36
36
  // Initialize hashers for intermediate levels, each taking the outputs of two hashers from the previous level.
37
- var k = 0;
38
- for (var i = numLeafHashers; i < numLeafHashers + numIntermediateHashers; i++) {
39
- computedLevelHashers[i] = PoseidonHasher(2)([computedLevelHashers[k*2], computedLevelHashers[k*2+1]]);
40
- k++;
37
+ var index = 0;
38
+
39
+ for (var i = LEAF_HASHERS; i < LEAF_HASHERS + INTERMEDIATE_HASHERS; i++) {
40
+ computedLevelHashers[i] = PoseidonHasher(2)([
41
+ computedLevelHashers[index * 2],
42
+ computedLevelHashers[index * 2 + 1]
43
+ ]);
44
+ index++;
41
45
  }
42
46
 
43
47
  // Connect the output of the final hasher in the array to the root output signal.
44
- root <== computedLevelHashers[numHashers-1];
48
+ root <== computedLevelHashers[hashersLength - 1];
45
49
  }
@@ -6,7 +6,7 @@ include "./MerkleTreeInclusionProof.circom";
6
6
  /**
7
7
  * Ensures that a leaf exists within a Merkle tree with a given root.
8
8
  */
9
- template LeafExists(levels){
9
+ template LeafExists(levels) {
10
10
  // The leaf whose existence within the tree is being verified.
11
11
  signal input leaf;
12
12
 
@@ -3,27 +3,31 @@ pragma circom 2.0.0;
3
3
  // zk-kit imports
4
4
  include "./safe-comparators.circom";
5
5
  // local import
6
- include "../calculateTotal.circom";
6
+ include "../CalculateTotal.circom";
7
7
 
8
8
  /**
9
9
  * Calculates the path indices required for Merkle proof verifications.
10
10
  * Given a node index within an IMT and the total tree levels, it outputs the path indices leading to that node.
11
11
  * The template handles the modulo and division operations to break down the tree index into its constituent path indices.
12
12
  */
13
- template MerkleGeneratePathIndices(levels) {
13
+ template MerklePathIndicesGenerator(levels) {
14
+ // The base used for the modulo and division operations, set to 2 for binary trees.
14
15
  var BASE = 2;
15
16
 
16
- signal input in;
17
+ // The total sum of the path indices.
18
+ signal input indices;
19
+
20
+ // The generated path indices.
17
21
  signal output out[levels];
18
22
 
19
- var m = in;
23
+ var computedIndices = indices;
20
24
  var computedResults[levels];
21
25
 
22
26
  for (var i = 0; i < levels; i++) {
23
27
  // circom's best practices suggests to avoid using <-- unless you
24
28
  // are aware of what's going on. This is the only way to do modulo operation.
25
- out[i] <-- m % BASE;
26
- m = m \ BASE;
29
+ out[i] <-- computedIndices % BASE;
30
+ computedIndices = computedIndices \ BASE;
27
31
 
28
32
  // Check that each output element is less than the base.
29
33
  var computedIsOutputElementLessThanBase = SafeLessThan(3)([out[i], BASE]);
@@ -36,5 +40,5 @@ template MerkleGeneratePathIndices(levels) {
36
40
  // Check that the total sum matches the index.
37
41
  var computedCalculateTotal = CalculateTotal(levels)(computedResults);
38
42
 
39
- computedCalculateTotal === in;
43
+ computedCalculateTotal === indices;
40
44
  }
@@ -3,7 +3,7 @@ pragma circom 2.0.0;
3
3
  // circomlib import
4
4
  include "./mux1.circom";
5
5
  // local import
6
- include "../hashers.circom";
6
+ include "../PoseidonHasher.circom";
7
7
 
8
8
  /**
9
9
  * Recomputes a Merkle root from a given leaf and its path in a Merkle tree.
@@ -16,6 +16,7 @@ template MerkleTreeInclusionProof(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
 
19
+ // The merkle root.
19
20
  signal output root;
20
21
 
21
22
  // Stores the hash at each level starting from the leaf to the root.
@@ -28,17 +29,17 @@ template MerkleTreeInclusionProof(n_levels) {
28
29
  path_index[i] * (1 - path_index[i]) === 0;
29
30
 
30
31
  // Configure the multiplexer based on the path index for the current level.
31
- var c[2][2] = [
32
+ var multiplexer[2][2] = [
32
33
  [levelHashes[i], path_elements[i][0]],
33
34
  [path_elements[i][0], levelHashes[i]]
34
35
  ];
35
36
 
36
- var mux[2] = MultiMux1(2)(
37
- c,
37
+ var multiplexerResult[2] = MultiMux1(2)(
38
+ multiplexer,
38
39
  path_index[i]
39
40
  );
40
41
 
41
- var computedLevelHash = PoseidonHasher(2)([mux[0], mux[1]]);
42
+ var computedLevelHash = PoseidonHasher(2)([multiplexerResult[0], multiplexerResult[1]]);
42
43
 
43
44
  // Store the resulting hash as the next level's hash.
44
45
  levelHashes[i + 1] <== computedLevelHash;
@@ -6,8 +6,8 @@ include "./mux1.circom";
6
6
  // zk-kit import
7
7
  include "./safe-comparators.circom";
8
8
  // local imports
9
- include "../calculateTotal.circom";
10
- include "../hashers.circom";
9
+ include "../CalculateTotal.circom";
10
+ include "../PoseidonHasher.circom";
11
11
 
12
12
  // Incremental Quintary Merkle Tree (IQT) verification circuits.
13
13
  // Since each node contains 5 leaves, we are using PoseidonT6 for hashing them.
@@ -0,0 +1,43 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // local imports
4
+ include "../utils/PoseidonHasher.circom";
5
+ include "../utils/PrivateToPublicKey.circom";
6
+ include "../utils/trees/BinaryMerkleRoot.circom";
7
+
8
+ // Poll Joined Circuit
9
+ // Allows a user to prove that they have joined a MACI poll.
10
+ // This is to be used with the MACI offchain implementation to allow
11
+ // users to authenticate to the relayer service (to reduce spamming).
12
+ template PollJoined(stateTreeDepth) {
13
+ // Constants defining the tree structure
14
+ var STATE_TREE_ARITY = 2;
15
+
16
+ // User's private key
17
+ signal input privateKey;
18
+ // User's voice credits balance
19
+ signal input voiceCreditsBalance;
20
+ // Path elements
21
+ signal input pathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
22
+ // Path indices
23
+ signal input pathIndices[stateTreeDepth];
24
+ // Poll State tree root which proves the user is joined
25
+ signal input stateRoot;
26
+ // The actual tree depth (might be <= stateTreeDepth) Used in BinaryMerkleRoot
27
+ signal input actualStateTreeDepth;
28
+
29
+ // User private to public key
30
+ var derivedPublicKey[2] = PrivateToPublicKey()(privateKey);
31
+
32
+ var stateLeaf = PoseidonHasher(3)([derivedPublicKey[0], derivedPublicKey[1], voiceCreditsBalance]);
33
+
34
+ // Inclusion proof
35
+ var calculatedRoot = BinaryMerkleRoot(stateTreeDepth)(
36
+ stateLeaf,
37
+ actualStateTreeDepth,
38
+ pathIndices,
39
+ pathElements
40
+ );
41
+
42
+ calculatedRoot === stateRoot;
43
+ }