@maci-protocol/circuits 0.0.0-ci.f9da2fc → 0.0.0-ci.fb960ed

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 (32) hide show
  1. package/build/ts/types.d.ts +6 -6
  2. package/build/ts/types.d.ts.map +1 -1
  3. package/build/tsconfig.build.tsbuildinfo +1 -1
  4. package/circom/circuits.json +3 -3
  5. package/circom/coordinator/non-qv/processMessages.circom +16 -10
  6. package/circom/coordinator/non-qv/tallyVotes.circom +6 -3
  7. package/circom/coordinator/qv/processMessages.circom +16 -10
  8. package/circom/coordinator/qv/tallyVotes.circom +5 -3
  9. package/circom/utils/{calculateTotal.circom → CalculateTotal.circom} +2 -0
  10. package/circom/utils/{verifySignature.circom → EdDSAPoseidonVerifier.circom} +40 -66
  11. package/circom/utils/MessageHasher.circom +57 -0
  12. package/circom/utils/MessageToCommand.circom +107 -0
  13. package/circom/utils/PoseidonHasher.circom +29 -0
  14. package/circom/utils/{privToPubKey.circom → PrivateToPublicKey.circom} +11 -9
  15. package/circom/utils/VerifySignature.circom +39 -0
  16. package/circom/utils/non-qv/messageValidator.circom +3 -3
  17. package/circom/utils/non-qv/stateLeafAndBallotTransformer.circom +2 -2
  18. package/circom/utils/qv/messageValidator.circom +3 -3
  19. package/circom/utils/qv/stateLeafAndBallotTransformer.circom +2 -2
  20. package/circom/utils/trees/BinaryMerkleRoot.circom +62 -0
  21. package/circom/utils/trees/CheckRoot.circom +49 -0
  22. package/circom/utils/trees/LeafExists.circom +27 -0
  23. package/circom/utils/trees/MerkleGeneratePathIndices.circom +44 -0
  24. package/circom/utils/trees/MerkleTreeInclusionProof.circom +50 -0
  25. package/circom/utils/trees/incrementalQuinaryTree.circom +2 -2
  26. package/circom/voter/PollJoined.circom +43 -0
  27. package/circom/voter/PollJoining.circom +54 -0
  28. package/package.json +10 -9
  29. package/circom/utils/hashers.circom +0 -78
  30. package/circom/utils/messageToCommand.circom +0 -78
  31. package/circom/utils/trees/incrementalMerkleTree.circom +0 -198
  32. package/circom/voter/poll.circom +0 -91
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "PollJoining_10_test": {
3
- "file": "./voter/poll",
3
+ "file": "./voter/PollJoining",
4
4
  "template": "PollJoining",
5
5
  "params": [10],
6
- "pubs": ["nullifier", "stateRoot", "pollPubKey", "pollId"]
6
+ "pubs": ["nullifier", "stateRoot", "pollPublicKey", "pollId"]
7
7
  },
8
8
  "PollJoined_10_test": {
9
- "file": "./voter/poll",
9
+ "file": "./voter/PollJoined",
10
10
  "template": "PollJoined",
11
11
  "params": [10],
12
12
  "pubs": ["stateRoot"]
@@ -5,11 +5,16 @@ include "./mux1.circom";
5
5
  // zk-kit imports
6
6
  include "./safe-comparators.circom";
7
7
  // local imports
8
- include "../../utils/hashers.circom";
9
- include "../../utils/messageToCommand.circom";
10
- include "../../utils/privToPubKey.circom";
8
+ include "../../utils/PoseidonHasher.circom";
9
+ include "../../utils/MessageHasher.circom";
10
+ include "../../utils/MessageToCommand.circom";
11
+ include "../../utils/PrivateToPublicKey.circom";
11
12
  include "../../utils/non-qv/stateLeafAndBallotTransformer.circom";
12
- include "../../utils/trees/incrementalMerkleTree.circom";
13
+ include "../../utils/trees/MerkleTreeInclusionProof.circom";
14
+ include "../../utils/trees/LeafExists.circom";
15
+ include "../../utils/trees/CheckRoot.circom";
16
+ include "../../utils/trees/MerkleGeneratePathIndices.circom";
17
+ include "../../utils/trees/BinaryMerkleRoot.circom";
13
18
  include "../../utils/trees/incrementalQuinaryTree.circom";
14
19
 
15
20
  /**
@@ -50,11 +55,11 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
50
55
  // Value of chainHash at end of batch
51
56
  signal input outputBatchHash;
52
57
  // The messages.
53
- signal input msgs[batchSize][MSG_LENGTH];
58
+ signal input messages[batchSize][MSG_LENGTH];
54
59
  // The coordinator's private key.
55
- signal input coordPrivKey;
60
+ signal input coordinatorPrivateKey;
56
61
  // The ECDH public key per message.
57
- signal input encPubKeys[batchSize][2];
62
+ signal input encryptionPublicKeys[batchSize][2];
58
63
  // The current state root (before the processing).
59
64
  signal input currentStateRoot;
60
65
  // The actual tree depth (might be <= stateTreeDepth).
@@ -135,7 +140,7 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
135
140
  chainHash[0] = inputBatchHash;
136
141
  for (var i = 0; i < batchSize; i++) {
137
142
  // calculate message hash
138
- computedMessageHashers[i] = MessageHasher()(msgs[i], encPubKeys[i]);
143
+ computedMessageHashers[i] = MessageHasher()(messages[i], encryptionPublicKeys[i]);
139
144
  // check if message is valid or not (if index of message is less than index of last valid message in batch)
140
145
  var batchStartIndexValid = SafeLessThan(32)([index + i, batchEndIndex]);
141
146
  // calculate chain hash if message is valid
@@ -160,7 +165,7 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
160
165
  // Ensure that the coordinator's public key from the contract is correct
161
166
  // based on the given private key - that is, the prover knows the
162
167
  // coordinator's private key.
163
- var derivedPubKey[2] = PrivToPubKey()(coordPrivKey);
168
+ var derivedPubKey[2] = PrivateToPublicKey()(coordinatorPrivateKey);
164
169
  var derivedPubKeyHash = PoseidonHasher(2)(derivedPubKey);
165
170
  derivedPubKeyHash === coordinatorPublicKeyHash;
166
171
 
@@ -191,7 +196,7 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
191
196
  computedCommandsSigR8[i],
192
197
  computedCommandsSigS[i],
193
198
  computedCommandsPackedCommandOut[i]
194
- ) = MessageToCommand()(msgs[i], coordPrivKey, encPubKeys[i]);
199
+ ) = MessageToCommand()(messages[i], coordinatorPrivateKey, encryptionPublicKeys[i]);
195
200
  }
196
201
 
197
202
  // Process messages in reverse order.
@@ -441,3 +446,4 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
441
446
 
442
447
  newBallotRoot <== computedNewBallotQip;
443
448
  }
449
+
@@ -5,10 +5,12 @@ include "./comparators.circom";
5
5
  // zk-kit import
6
6
  include "./unpack-element.circom";
7
7
  // local imports
8
- include "../../utils/trees/incrementalMerkleTree.circom";
8
+ include "../../utils/trees/CheckRoot.circom";
9
+ include "../../utils/trees/MerkleGeneratePathIndices.circom";
10
+ include "../../utils/trees/LeafExists.circom";
9
11
  include "../../utils/trees/incrementalQuinaryTree.circom";
10
- include "../../utils/calculateTotal.circom";
11
- include "../../utils/hashers.circom";
12
+ include "../../utils/CalculateTotal.circom";
13
+ include "../../utils/PoseidonHasher.circom";
12
14
 
13
15
  /**
14
16
  * Processes batches of votes and verifies their validity in a Merkle tree structure.
@@ -230,3 +232,4 @@ template TallyVotesNonQv(
230
232
 
231
233
  computedNewTallyCommitment === newTallyCommitment;
232
234
  }
235
+
@@ -5,12 +5,17 @@ include "./mux1.circom";
5
5
  // zk-kit imports
6
6
  include "./safe-comparators.circom";
7
7
  // local imports
8
- include "../../utils/hashers.circom";
9
- include "../../utils/messageToCommand.circom";
10
- include "../../utils/privToPubKey.circom";
8
+ include "../../utils/PoseidonHasher.circom";
9
+ include "../../utils/MessageHasher.circom";
10
+ include "../../utils/MessageToCommand.circom";
11
+ include "../../utils/PrivateToPublicKey.circom";
11
12
  include "../../utils/qv/stateLeafAndBallotTransformer.circom";
12
13
  include "../../utils/trees/incrementalQuinaryTree.circom";
13
- include "../../utils/trees/incrementalMerkleTree.circom";
14
+ include "../../utils/trees/MerkleTreeInclusionProof.circom";
15
+ include "../../utils/trees/LeafExists.circom";
16
+ include "../../utils/trees/CheckRoot.circom";
17
+ include "../../utils/trees/MerkleGeneratePathIndices.circom";
18
+ include "../../utils/trees/BinaryMerkleRoot.circom";
14
19
 
15
20
  /**
16
21
  * Proves the correctness of processing a batch of MACI messages.
@@ -50,11 +55,11 @@ template ProcessMessages(
50
55
  // Value of chainHash at end of batch
51
56
  signal input outputBatchHash;
52
57
  // The messages.
53
- signal input msgs[batchSize][MSG_LENGTH];
58
+ signal input messages[batchSize][MSG_LENGTH];
54
59
  // The coordinator's private key.
55
- signal input coordPrivKey;
60
+ signal input coordinatorPrivateKey;
56
61
  // The ECDH public key per message.
57
- signal input encPubKeys[batchSize][2];
62
+ signal input encryptionPublicKeys[batchSize][2];
58
63
  // The current state root (before the processing).
59
64
  signal input currentStateRoot;
60
65
  // The actual tree depth (might be <= stateTreeDepth).
@@ -130,7 +135,7 @@ template ProcessMessages(
130
135
 
131
136
  for (var i = 0; i < batchSize; i++) {
132
137
  // calculate message hash
133
- computedMessageHashers[i] = MessageHasher()(msgs[i], encPubKeys[i]);
138
+ computedMessageHashers[i] = MessageHasher()(messages[i], encryptionPublicKeys[i]);
134
139
  // check if message is valid or not (if index of message is less than index of last valid message in batch)
135
140
  var batchStartIndexValid = SafeLessThan(32)([index + i, batchEndIndex]);
136
141
  // calculate chain hash if message is valid
@@ -154,7 +159,7 @@ template ProcessMessages(
154
159
  // Ensure that the coordinator's public key from the contract is correct
155
160
  // based on the given private key - that is, the prover knows the
156
161
  // coordinator's private key.
157
- var derivedPubKey[2] = PrivToPubKey()(coordPrivKey);
162
+ var derivedPubKey[2] = PrivateToPublicKey()(coordinatorPrivateKey);
158
163
  var derivedPubKeyHash = PoseidonHasher(2)(derivedPubKey);
159
164
  derivedPubKeyHash === coordinatorPublicKeyHash;
160
165
 
@@ -185,7 +190,7 @@ template ProcessMessages(
185
190
  computedCommandsSigR8[i],
186
191
  computedCommandsSigS[i],
187
192
  computedCommandsPackedCommandOut[i]
188
- ) = MessageToCommand()(msgs[i], coordPrivKey, encPubKeys[i]);
193
+ ) = MessageToCommand()(messages[i], coordinatorPrivateKey, encryptionPublicKeys[i]);
189
194
  }
190
195
 
191
196
  // Process messages in reverse order.
@@ -443,3 +448,4 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
443
448
 
444
449
  newBallotRoot <== computedNewBallotQip;
445
450
  }
451
+
@@ -5,10 +5,12 @@ include "./comparators.circom";
5
5
  // zk-kit import
6
6
  include "./unpack-element.circom";
7
7
  // local imports
8
- include "../../utils/trees/incrementalMerkleTree.circom";
8
+ include "../../utils/trees/CheckRoot.circom";
9
+ include "../../utils/trees/MerkleGeneratePathIndices.circom";
10
+ include "../../utils/trees/LeafExists.circom";
9
11
  include "../../utils/trees/incrementalQuinaryTree.circom";
10
- include "../../utils/calculateTotal.circom";
11
- include "../../utils/hashers.circom";
12
+ include "../../utils/CalculateTotal.circom";
13
+ include "../../utils/PoseidonHasher.circom";
12
14
 
13
15
  /**
14
16
  * Processes batches of votes and verifies their validity in a Merkle tree structure.
@@ -8,7 +8,9 @@ pragma circom 2.0.0;
8
8
  * final output reflects the cumulative total of the inputs provided.
9
9
  */
10
10
  template CalculateTotal(n) {
11
+ // Array of values.
11
12
  signal input nums[n];
13
+ // Total sum.
12
14
  signal output sum;
13
15
 
14
16
  signal sums[n];
@@ -8,60 +8,69 @@ include "./bitify.circom";
8
8
  include "./escalarmulany.circom";
9
9
  include "./escalarmulfix.circom";
10
10
  // local imports
11
- include "./hashers.circom";
11
+ include "./PoseidonHasher.circom";
12
12
 
13
13
  /**
14
14
  * Variant of the EdDSAPoseidonVerifier template from circomlib
15
15
  * https://github.com/iden3/circomlib/blob/master/circuits/eddsa.circom
16
16
  */
17
- template EdDSAPoseidonVerifier_patched() {
17
+ template EdDSAPoseidonVerifier() {
18
+ // The prime subgroup order.
19
+ var SUBGROUP_ORDER = 2736030358979909402780800718157159386076813972158567259200215660948447373041;
20
+
21
+ // The base point of the BabyJubJub curve.
22
+ var BASE8[2] = [
23
+ 5299619240641551281634865583518297030282874472190772894086521144482721001553,
24
+ 16950150798460657717958625567821834550301663161624707787222815936182638968203
25
+ ];
26
+
18
27
  // The x and y coordinates of the public key.
19
- signal input Ax;
20
- signal input Ay;
28
+ signal input publicKeyX;
29
+ signal input publicKeyY;
21
30
  // Signature scalar.
22
- signal input S;
31
+ signal input signatureScalar;
23
32
  // The x and y coordinates of the signature point.
24
- signal input R8x;
25
- signal input R8y;
33
+ signal input signaturePointX;
34
+ signal input signaturePointY;
26
35
  // Message hash.
27
- signal input M;
36
+ signal input messageHash;
37
+ // Output signal for the validity of the signature.
38
+ signal output isValid;
28
39
 
29
- signal output valid;
30
-
31
- // Ensure S<Subgroup Order.
32
- // convert the signature scalar S into its binary representation.
33
- var computedNum2Bits[254] = Num2Bits(254)(S);
40
+ // Ensure signatureScalar<Subgroup Order.
41
+ // convert the signature scalar signatureScalar into its binary representation.
42
+ var computedNum2Bits[254] = Num2Bits(254)(signatureScalar);
34
43
 
35
44
  var computedCompConstantIn[254] = computedNum2Bits;
36
45
  computedCompConstantIn[253] = 0;
37
46
 
38
- // A component that ensures S is within a valid range,
47
+ // A component that ensures signatureScalar is within a valid range,
39
48
  // comparing it against a constant representing the subgroup order.
40
- var computedCompConstant = CompConstant(2736030358979909402780800718157159386076813972158567259200215660948447373040)(computedCompConstantIn);
49
+ var computedCompConstant = CompConstant(SUBGROUP_ORDER - 1)(computedCompConstantIn);
41
50
 
42
- // Calculate the h = H(R,A, msg).
43
- var computedH2Bits[254] = Num2Bits_strict()(PoseidonHasher(5)([R8x, R8y, Ax, Ay, M]));
51
+ // Calculate the h = H(R, A, msg).
52
+ var computedH2Bits[254] = Num2Bits_strict()(PoseidonHasher(5)([
53
+ signaturePointX,
54
+ signaturePointY,
55
+ publicKeyX,
56
+ publicKeyY,
57
+ messageHash
58
+ ]));
44
59
 
45
60
  // These components perform point doubling operations on the public key
46
61
  // to align it within the correct subgroup as part of the verification process.
47
- var (computedDbl1XOut, computedDbl1YOut) = BabyDbl()(Ax, Ay);
48
- var (computedDbl2XOut, computedDbl2YOut) = BabyDbl()(computedDbl1XOut, computedDbl1YOut);
49
- var (computedDbl3XOut, computedDbl3YOut) = BabyDbl()(computedDbl2XOut, computedDbl2YOut);
62
+ var (computedDouble1XOut, computedDouble1YOut) = BabyDbl()(publicKeyX, publicKeyY);
63
+ var (computedDouble2XOut, computedDouble2YOut) = BabyDbl()(computedDouble1XOut, computedDouble1YOut);
64
+ var (computedDouble3XOut, computedDouble3YOut) = BabyDbl()(computedDouble2XOut, computedDouble2YOut);
50
65
 
51
66
  // A component that performs scalar multiplication of the
52
67
  // adjusted public key by the hash output, essential for the verification calculation.
53
- var computedEscalarMulAny[2] = EscalarMulAny(254)(computedH2Bits, [computedDbl3XOut, computedDbl3YOut]);
68
+ var computedEscalarMulAny[2] = EscalarMulAny(254)(computedH2Bits, [computedDouble3XOut, computedDouble3YOut]);
54
69
 
55
70
  // Compute the right side: right = R8 + right2.
56
- var (computedAddRightXOut, computedAddRightYOut) = BabyAdd()(R8x, R8y, computedEscalarMulAny[0], computedEscalarMulAny[1]);
57
-
58
- // Calculate the left side: left = S * B8.
59
- var BASE8[2] = [
60
- 5299619240641551281634865583518297030282874472190772894086521144482721001553,
61
- 16950150798460657717958625567821834550301663161624707787222815936182638968203
62
- ];
71
+ var (computedAddRightXOut, computedAddRightYOut) = BabyAdd()(signaturePointX, signaturePointY, computedEscalarMulAny[0], computedEscalarMulAny[1]);
63
72
 
64
- // Fixed-base scalar multiplication of a base point by S.
73
+ // Fixed-base scalar multiplication of a base point by signatureScalar.
65
74
  var computedEscalarMulFix[2] = EscalarMulFix(254, BASE8)(computedNum2Bits);
66
75
 
67
76
  // Components to check the equality of x and y coordinates
@@ -73,45 +82,10 @@ template EdDSAPoseidonVerifier_patched() {
73
82
  // Components to handle edge cases and ensure that all conditions
74
83
  // for a valid signature are met, including the
75
84
  // public key not being zero and other integrity checks.
76
- var computedIsAxZero = IsZero()(Ax);
85
+ var computedIsAxZero = IsZero()(publicKeyX);
77
86
  var computedIsAxEqual = IsEqual()([computedIsAxZero, 0]);
78
87
  var computedIsCcZero = IsZero()(computedCompConstant);
79
88
  var computedIsValid = IsEqual()([computedIsLeftRightValid + computedIsAxEqual + computedIsCcZero, 3]);
80
89
 
81
- valid <== computedIsValid;
82
- }
83
-
84
- /**
85
- * Verifies the EdDSA signature for a given command, which has exactly four elements in the hash preimage.
86
- */
87
- template VerifySignature() {
88
- // Public key of the signer, consisting of two coordinates [x, y].
89
- signal input pubKey[2];
90
- // R8 point from the signature, consisting of two coordinates [x, y].
91
- signal input R8[2];
92
- // Scalar component of the signature.
93
- signal input S;
94
-
95
- // Number of elements in the hash preimage.
96
- var k = 4;
97
-
98
- // The preimage data that was hashed, an array of four elements.
99
- signal input preimage[k];
100
-
101
- signal output valid;
102
-
103
- // Hash the preimage using the Poseidon hashing function configured for four inputs.
104
- var computedM = PoseidonHasher(4)(preimage);
105
-
106
- // Instantiate the patched EdDSA Poseidon verifier with the necessary inputs.
107
- var computedVerifier = EdDSAPoseidonVerifier_patched()(
108
- pubKey[0],
109
- pubKey[1],
110
- S,
111
- R8[0],
112
- R8[1],
113
- computedM
114
- );
115
-
116
- valid <== computedVerifier;
90
+ isValid <== computedIsValid;
117
91
  }
@@ -0,0 +1,57 @@
1
+ pragma circom 2.0.0;
2
+
3
+ include "./PoseidonHasher.circom";
4
+
5
+ /**
6
+ * Hashes a MACI message and the public key used for message encryption.
7
+ * This template processes 10 message inputs and a 2-element public key
8
+ * combining them using the Poseidon hash function. The hashing process involves two stages:
9
+ * 1. hashing message parts data groups of five and,
10
+ * 2. hashing the grouped results alongside the first message input and
11
+ * the encryption public key to produce a final hash output.
12
+ */
13
+ template MessageHasher() {
14
+ // Message parts
15
+ var MESSAGE_PARTS = 10;
16
+ var STATE_INDEX = 0;
17
+ var VOTE_OPTION_INDEX = 1;
18
+ var NEW_VOTE_WEIGHT = 2;
19
+ var NONCE = 3;
20
+ var POLL_ID = 4;
21
+ var SIGNATURE_POINT_X = 5;
22
+ var SIGNATURE_POINT_Y = 6;
23
+ var SIGNATURE_SCALAR = 7;
24
+ var ENCRYPTED_PUBLIC_KEY_X = 8;
25
+ var ENCRYPTED_PUBLIC_KEY_Y = 9;
26
+
27
+ // The MACI message is composed of 10 parts.
28
+ signal input data[MESSAGE_PARTS];
29
+ // the public key used to encrypt the message.
30
+ signal input encryptionPublicKey[2];
31
+
32
+ // we output an hash.
33
+ signal output hash;
34
+
35
+ var computedHasherPart1 = PoseidonHasher(5)([
36
+ data[STATE_INDEX],
37
+ data[VOTE_OPTION_INDEX],
38
+ data[NEW_VOTE_WEIGHT],
39
+ data[NONCE],
40
+ data[POLL_ID]
41
+ ]);
42
+
43
+ var computedHasherPart2 = PoseidonHasher(5)([
44
+ data[SIGNATURE_POINT_X],
45
+ data[SIGNATURE_POINT_Y],
46
+ data[SIGNATURE_SCALAR],
47
+ data[ENCRYPTED_PUBLIC_KEY_X],
48
+ data[ENCRYPTED_PUBLIC_KEY_Y]
49
+ ]);
50
+
51
+ hash <== PoseidonHasher(4)([
52
+ computedHasherPart1,
53
+ computedHasherPart2,
54
+ encryptionPublicKey[0],
55
+ encryptionPublicKey[1]
56
+ ]);
57
+ }
@@ -0,0 +1,107 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // circomlib import
4
+ include "./bitify.circom";
5
+ // zk-kit imports
6
+ include "./ecdh.circom";
7
+ include "./unpack-element.circom";
8
+ include "./poseidon-cipher.circom";
9
+
10
+ /**
11
+ * Converts a MACI message to a command by decrypting it.
12
+ * Processes encrypted MACI messages into structured MACI commands
13
+ * by decrypting using a shared key derived from ECDH. After decryption,
14
+ * unpacks and assigns decrypted values to specific command components.
15
+ */
16
+ template MessageToCommand() {
17
+ var MESSAGE_LENGTH = 7;
18
+ var PACKED_COMMAND_LENGTH = 4;
19
+ var UNPACK_ELEMENT_LENGTH = 5;
20
+ var DECRYPTED_LENGTH = 9;
21
+ var MESSAGE_PARTS = 10;
22
+
23
+ // Element indices.
24
+ var ELEMENT_POLL_ID = 0;
25
+ var ELEMENT_NONCE = 1;
26
+ var ELEMENT_NEW_VOTE_WEIGHT = 2;
27
+ var ELEMENT_VOTE_OPTION_INDEX = 3;
28
+ var ELEMENT_STATE_INDEX = 4;
29
+
30
+ // Command indices.
31
+ var COMMAND_STATE_INDEX = 0;
32
+ var COMMAND_PUBLIC_KEY_X = 1;
33
+ var COMMAND_PUBLIC_KEY_Y = 2;
34
+ var COMMAND_SALT = 3;
35
+
36
+ // Decryptor indices.
37
+ var SIGNATURE_POINT_X = 4;
38
+ var SIGNATURE_POINT_Y = 5;
39
+ var SIGNATURE_SCALAR = 6;
40
+
41
+ // The message is an array of 10 parts.
42
+ signal input message[MESSAGE_PARTS];
43
+ // The encryption private key
44
+ signal input encryptionPrivateKey;
45
+ // The encryption public key
46
+ signal input encryptionPublicKey[2];
47
+
48
+ // Command parts.
49
+ signal output stateIndex;
50
+ // The new public key.
51
+ signal output newPublicKey[2];
52
+ // The vote option index.
53
+ signal output voteOptionIndex;
54
+ // The new vote weight.
55
+ signal output newVoteWeight;
56
+ // The nonce.
57
+ signal output nonce;
58
+ // The poll id.
59
+ signal output pollId;
60
+ // The salt.
61
+ signal output salt;
62
+ // The signature point.
63
+ signal output signaturePoint[2];
64
+ // The signature scalar.
65
+ signal output signatureScalar;
66
+
67
+ // Packed command.
68
+ signal output packedCommandOut[PACKED_COMMAND_LENGTH];
69
+
70
+ // Generate the shared key for decrypting the message.
71
+ var computedEcdh[2] = Ecdh()(encryptionPrivateKey, encryptionPublicKey);
72
+
73
+ // Decrypt the message using Poseidon decryption.
74
+ var computedDecryptor[DECRYPTED_LENGTH] = PoseidonDecryptWithoutCheck(MESSAGE_LENGTH)(
75
+ message,
76
+ 0,
77
+ computedEcdh
78
+ );
79
+
80
+ // Save the decrypted message into a packed command signal.
81
+ signal packedCommand[PACKED_COMMAND_LENGTH];
82
+
83
+ for (var i = 0; i < PACKED_COMMAND_LENGTH; i++) {
84
+ packedCommand[i] <== computedDecryptor[i];
85
+ }
86
+
87
+ var computedUnpackElement[UNPACK_ELEMENT_LENGTH] = UnpackElement(UNPACK_ELEMENT_LENGTH)(
88
+ packedCommand[COMMAND_STATE_INDEX]
89
+ );
90
+
91
+ // Everything below were packed into the first element.
92
+ pollId <== computedUnpackElement[ELEMENT_POLL_ID];
93
+ nonce <== computedUnpackElement[ELEMENT_NONCE];
94
+ newVoteWeight <== computedUnpackElement[ELEMENT_NEW_VOTE_WEIGHT];
95
+ voteOptionIndex <== computedUnpackElement[ELEMENT_VOTE_OPTION_INDEX];
96
+ stateIndex <== computedUnpackElement[ELEMENT_STATE_INDEX];
97
+
98
+ newPublicKey[0] <== packedCommand[COMMAND_PUBLIC_KEY_X];
99
+ newPublicKey[1] <== packedCommand[COMMAND_PUBLIC_KEY_Y];
100
+ salt <== packedCommand[COMMAND_SALT];
101
+
102
+ signaturePoint[0] <== computedDecryptor[SIGNATURE_POINT_X];
103
+ signaturePoint[1] <== computedDecryptor[SIGNATURE_POINT_Y];
104
+ signatureScalar <== computedDecryptor[SIGNATURE_SCALAR];
105
+
106
+ packedCommandOut <== packedCommand;
107
+ }
@@ -0,0 +1,29 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // zk-kit imports
4
+ include "./poseidon-cipher.circom";
5
+
6
+ /**
7
+ * Computes the Poseidon hash for an array of n inputs, including a default initial state
8
+ * of zero not counted in n. First, extends the inputs by prepending a zero, creating an array [0, inputs].
9
+ * Then, the Poseidon hash of the extended inputs is calculated, with the first element of the
10
+ * result assigned as the output.
11
+ */
12
+ template PoseidonHasher(n) {
13
+ signal input inputs[n];
14
+ signal output out;
15
+
16
+ // [0, inputs].
17
+ var computedExtendedInputs[n + 1];
18
+ computedExtendedInputs[0] = 0;
19
+
20
+ for (var i = 0; i < n; i++) {
21
+ computedExtendedInputs[i + 1] = inputs[i];
22
+ }
23
+
24
+ // Compute the Poseidon hash of the extended inputs.
25
+ var computedPoseidonPerm[n + 1];
26
+ computedPoseidonPerm = PoseidonPerm(n + 1)(computedExtendedInputs);
27
+
28
+ out <== computedPoseidonPerm[0];
29
+ }
@@ -9,28 +9,30 @@ include "./escalarmulfix.circom";
9
9
  * Converts a private key to a public key on the BabyJubJub curve.
10
10
  * The input private key needs to be hashed and then pruned before.
11
11
  */
12
- template PrivToPubKey() {
12
+ template PrivateToPublicKey() {
13
13
  // The base point of the BabyJubJub curve.
14
14
  var BASE8[2] = [
15
15
  5299619240641551281634865583518297030282874472190772894086521144482721001553,
16
16
  16950150798460657717958625567821834550301663161624707787222815936182638968203
17
17
  ];
18
18
 
19
- // Prime subgroup order 'l'.
20
- var l = 2736030358979909402780800718157159386076813972158567259200215660948447373041;
19
+ // The prime subgroup order.
20
+ var SUBGROUP_ORDER = 2736030358979909402780800718157159386076813972158567259200215660948447373041;
21
21
 
22
- signal input privKey;
23
- signal output pubKey[2];
22
+ // The private key
23
+ signal input privateKey;
24
+ // The public key
25
+ signal output publicKey[2];
24
26
 
25
- // Check if private key is in the prime subgroup order 'l'
26
- var isLessThan = LessThan(251)([privKey, l]);
27
+ // Check if private key is in the prime subgroup order
28
+ var isLessThan = LessThan(251)([privateKey, SUBGROUP_ORDER]);
27
29
  isLessThan === 1;
28
30
 
29
31
  // Convert the private key to bits.
30
- var computedPrivBits[253] = Num2Bits(253)(privKey);
32
+ var computedPrivBits[253] = Num2Bits(253)(privateKey);
31
33
 
32
34
  // Perform scalar multiplication with the basepoint.
33
35
  var computedEscalarMulFix[2] = EscalarMulFix(253, BASE8)(computedPrivBits);
34
36
 
35
- pubKey <== computedEscalarMulFix;
37
+ publicKey <== computedEscalarMulFix;
36
38
  }
@@ -0,0 +1,39 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // local imports
4
+ include "./EdDSAPoseidonVerifier.circom";
5
+ include "./PoseidonHasher.circom";
6
+
7
+ /**
8
+ * Verifies the EdDSA signature for a given command, which has exactly four elements in the hash preimage.
9
+ */
10
+ template VerifySignature() {
11
+ // Number of elements in the hash preimage.
12
+ var PRE_IMAGE_LENGTH = 4;
13
+
14
+ // Public key of the signer, consisting of two coordinates [x, y].
15
+ signal input publicKey[2];
16
+ // signaturePoint point from the signature, consisting of two coordinates [x, y].
17
+ signal input signaturePoint[2];
18
+ // Scalar component of the signature.
19
+ signal input signatureScalar;
20
+ // The preimage data that was hashed, an array of four elements.
21
+ signal input preimage[PRE_IMAGE_LENGTH];
22
+ // The validity of the signature.
23
+ signal output isValid;
24
+
25
+ // Hash the preimage using the Poseidon hashing function configured for four inputs.
26
+ var computedPreimage = PoseidonHasher(4)(preimage);
27
+
28
+ // Instantiate the patched EdDSA Poseidon verifier with the necessary inputs.
29
+ var computedIsValid = EdDSAPoseidonVerifier()(
30
+ publicKey[0],
31
+ publicKey[1],
32
+ signatureScalar,
33
+ signaturePoint[0],
34
+ signaturePoint[1],
35
+ computedPreimage
36
+ );
37
+
38
+ isValid <== computedIsValid;
39
+ }
@@ -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.
@@ -28,7 +28,7 @@ template MessageValidatorNonQv() {
28
28
  // Packed command.
29
29
  signal input cmd[PACKED_CMD_LENGTH];
30
30
  // Public key of the state leaf (user).
31
- signal input pubKey[2];
31
+ signal input publicKey[2];
32
32
  // ECDSA signature of the command (R part).
33
33
  signal input sigR8[2];
34
34
  // ECDSA signature of the command (S part).
@@ -60,7 +60,7 @@ template MessageValidatorNonQv() {
60
60
  var computedIsNonceValid = IsEqual()([originalNonce + 1, nonce]);
61
61
 
62
62
  // Check (4) - The signature must be correct.
63
- var computedIsSignatureValid = VerifySignature()(pubKey, sigR8, sigS, cmd);
63
+ var computedIsSignatureValid = VerifySignature()(publicKey, sigR8, sigS, cmd);
64
64
 
65
65
  // Check (5) - There must be sufficient voice credits.
66
66
  // The check ensure that currentVoiceCreditBalance + (currentVotesForOption) >= (voteWeight).
@@ -85,8 +85,8 @@ template StateLeafAndBallotTransformerNonQv() {
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].
88
+ // This means using a Mux1() for publicKey[0] and another one
89
+ // for publicKey[1].
90
90
  var computedNewSlPubKey0Mux = Mux1()([slPubKey[0], cmdNewPubKey[0]], computedMessageValidator);
91
91
  var computedNewSlPubKey1Mux = Mux1()([slPubKey[1], cmdNewPubKey[1]], computedMessageValidator);
92
92