@maci-protocol/circuits 0.0.0-ci.3dedbc5 → 0.0.0-ci.3ff6873

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 -7
  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 +21 -19
  6. package/circom/coordinator/non-qv/tallyVotes.circom +6 -3
  7. package/circom/coordinator/qv/processMessages.circom +21 -19
  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 +11 -10
  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 -93
@@ -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
  /**
@@ -32,14 +37,13 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
32
37
  var STATE_TREE_ARITY = 2;
33
38
  var MSG_LENGTH = 10;
34
39
  var PACKED_CMD_LENGTH = 4;
35
- var STATE_LEAF_LENGTH = 4;
40
+ var STATE_LEAF_LENGTH = 3;
36
41
  var BALLOT_LENGTH = 2;
37
42
  var BALLOT_NONCE_IDX = 0;
38
43
  var BALLOT_VO_ROOT_IDX = 1;
39
44
  var STATE_LEAF_PUB_X_IDX = 0;
40
45
  var STATE_LEAF_PUB_Y_IDX = 1;
41
46
  var STATE_LEAF_VOICE_CREDIT_BALANCE_IDX = 2;
42
- var STATE_LEAF_TIMESTAMP_IDX = 3;
43
47
  var msgTreeZeroValue = 8370432830353022751713833565135785980866757267633941821328460903436894336785;
44
48
  // Number of options for this poll.
45
49
  var maxVoteOptions = VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth;
@@ -51,11 +55,11 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
51
55
  // Value of chainHash at end of batch
52
56
  signal input outputBatchHash;
53
57
  // The messages.
54
- signal input msgs[batchSize][MSG_LENGTH];
58
+ signal input messages[batchSize][MSG_LENGTH];
55
59
  // The coordinator's private key.
56
- signal input coordPrivKey;
60
+ signal input coordinatorPrivateKey;
57
61
  // The ECDH public key per message.
58
- signal input encPubKeys[batchSize][2];
62
+ signal input encryptionPublicKeys[batchSize][2];
59
63
  // The current state root (before the processing).
60
64
  signal input currentStateRoot;
61
65
  // The actual tree depth (might be <= stateTreeDepth).
@@ -136,7 +140,7 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
136
140
  chainHash[0] = inputBatchHash;
137
141
  for (var i = 0; i < batchSize; i++) {
138
142
  // calculate message hash
139
- computedMessageHashers[i] = MessageHasher()(msgs[i], encPubKeys[i]);
143
+ computedMessageHashers[i] = MessageHasher()(messages[i], encryptionPublicKeys[i]);
140
144
  // check if message is valid or not (if index of message is less than index of last valid message in batch)
141
145
  var batchStartIndexValid = SafeLessThan(32)([index + i, batchEndIndex]);
142
146
  // calculate chain hash if message is valid
@@ -161,7 +165,7 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
161
165
  // Ensure that the coordinator's public key from the contract is correct
162
166
  // based on the given private key - that is, the prover knows the
163
167
  // coordinator's private key.
164
- var derivedPubKey[2] = PrivToPubKey()(coordPrivKey);
168
+ var derivedPubKey[2] = PrivateToPublicKey()(coordinatorPrivateKey);
165
169
  var derivedPubKeyHash = PoseidonHasher(2)(derivedPubKey);
166
170
  derivedPubKeyHash === coordinatorPublicKeyHash;
167
171
 
@@ -192,7 +196,7 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
192
196
  computedCommandsSigR8[i],
193
197
  computedCommandsSigS[i],
194
198
  computedCommandsPackedCommandOut[i]
195
- ) = MessageToCommand()(msgs[i], coordPrivKey, encPubKeys[i]);
199
+ ) = MessageToCommand()(messages[i], coordinatorPrivateKey, encryptionPublicKeys[i]);
196
200
  }
197
201
 
198
202
  // Process messages in reverse order.
@@ -266,7 +270,7 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
266
270
  */
267
271
  template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
268
272
  // Constants defining the structure and size of state and ballots.
269
- var STATE_LEAF_LENGTH = 4;
273
+ var STATE_LEAF_LENGTH = 3;
270
274
  var BALLOT_LENGTH = 2;
271
275
  var MSG_LENGTH = 10;
272
276
  var PACKED_CMD_LENGTH = 4;
@@ -282,8 +286,6 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
282
286
  var STATE_LEAF_PUB_Y_IDX = 1;
283
287
  // Voice Credit balance.
284
288
  var STATE_LEAF_VOICE_CREDIT_BALANCE_IDX = 2;
285
- // Timestamp.
286
- var STATE_LEAF_TIMESTAMP_IDX = 3;
287
289
  var N_BITS = 252;
288
290
 
289
291
  // Number of users that have completed the sign up.
@@ -357,7 +359,7 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
357
359
  var computedStateLeafPathIndices[stateTreeDepth] = MerkleGeneratePathIndices(stateTreeDepth)(stateIndexMux);
358
360
 
359
361
  // 3. Verify that the original state leaf exists in the given state root.
360
- var stateLeafHash = PoseidonHasher(4)(stateLeaf);
362
+ var stateLeafHash = PoseidonHasher(3)(stateLeaf);
361
363
  var stateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
362
364
  stateLeafHash,
363
365
  actualStateTreeDepth,
@@ -419,11 +421,10 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
419
421
  newBallotVoRoot <== newBallotVoRootMux;
420
422
 
421
423
  // 6. Generate a new state root.
422
- var computedNewStateLeafhash = PoseidonHasher(4)([
424
+ var computedNewStateLeafhash = PoseidonHasher(3)([
423
425
  computedNewSlPubKey[STATE_LEAF_PUB_X_IDX],
424
426
  computedNewSlPubKey[STATE_LEAF_PUB_Y_IDX],
425
- voiceCreditBalanceMux,
426
- stateLeaf[STATE_LEAF_TIMESTAMP_IDX]
427
+ voiceCreditBalanceMux
427
428
  ]);
428
429
 
429
430
  var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
@@ -445,3 +446,4 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
445
446
 
446
447
  newBallotRoot <== computedNewBallotQip;
447
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.
@@ -32,14 +37,13 @@ template ProcessMessages(
32
37
  var STATE_TREE_ARITY = 2;
33
38
  var MSG_LENGTH = 10;
34
39
  var PACKED_CMD_LENGTH = 4;
35
- var STATE_LEAF_LENGTH = 4;
40
+ var STATE_LEAF_LENGTH = 3;
36
41
  var BALLOT_LENGTH = 2;
37
42
  var BALLOT_NONCE_IDX = 0;
38
43
  var BALLOT_VO_ROOT_IDX = 1;
39
44
  var STATE_LEAF_PUB_X_IDX = 0;
40
45
  var STATE_LEAF_PUB_Y_IDX = 1;
41
46
  var STATE_LEAF_VOICE_CREDIT_BALANCE_IDX = 2;
42
- var STATE_LEAF_TIMESTAMP_IDX = 3;
43
47
  var msgTreeZeroValue = 8370432830353022751713833565135785980866757267633941821328460903436894336785;
44
48
  // Number of options for this poll.
45
49
  var maxVoteOptions = VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth;
@@ -51,11 +55,11 @@ template ProcessMessages(
51
55
  // Value of chainHash at end of batch
52
56
  signal input outputBatchHash;
53
57
  // The messages.
54
- signal input msgs[batchSize][MSG_LENGTH];
58
+ signal input messages[batchSize][MSG_LENGTH];
55
59
  // The coordinator's private key.
56
- signal input coordPrivKey;
60
+ signal input coordinatorPrivateKey;
57
61
  // The ECDH public key per message.
58
- signal input encPubKeys[batchSize][2];
62
+ signal input encryptionPublicKeys[batchSize][2];
59
63
  // The current state root (before the processing).
60
64
  signal input currentStateRoot;
61
65
  // The actual tree depth (might be <= stateTreeDepth).
@@ -131,7 +135,7 @@ template ProcessMessages(
131
135
 
132
136
  for (var i = 0; i < batchSize; i++) {
133
137
  // calculate message hash
134
- computedMessageHashers[i] = MessageHasher()(msgs[i], encPubKeys[i]);
138
+ computedMessageHashers[i] = MessageHasher()(messages[i], encryptionPublicKeys[i]);
135
139
  // check if message is valid or not (if index of message is less than index of last valid message in batch)
136
140
  var batchStartIndexValid = SafeLessThan(32)([index + i, batchEndIndex]);
137
141
  // calculate chain hash if message is valid
@@ -155,7 +159,7 @@ template ProcessMessages(
155
159
  // Ensure that the coordinator's public key from the contract is correct
156
160
  // based on the given private key - that is, the prover knows the
157
161
  // coordinator's private key.
158
- var derivedPubKey[2] = PrivToPubKey()(coordPrivKey);
162
+ var derivedPubKey[2] = PrivateToPublicKey()(coordinatorPrivateKey);
159
163
  var derivedPubKeyHash = PoseidonHasher(2)(derivedPubKey);
160
164
  derivedPubKeyHash === coordinatorPublicKeyHash;
161
165
 
@@ -186,7 +190,7 @@ template ProcessMessages(
186
190
  computedCommandsSigR8[i],
187
191
  computedCommandsSigS[i],
188
192
  computedCommandsPackedCommandOut[i]
189
- ) = MessageToCommand()(msgs[i], coordPrivKey, encPubKeys[i]);
193
+ ) = MessageToCommand()(messages[i], coordinatorPrivateKey, encryptionPublicKeys[i]);
190
194
  }
191
195
 
192
196
  // Process messages in reverse order.
@@ -260,7 +264,7 @@ template ProcessMessages(
260
264
  */
261
265
  template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
262
266
  // Constants defining the structure and size of state and ballots.
263
- var STATE_LEAF_LENGTH = 4;
267
+ var STATE_LEAF_LENGTH = 3;
264
268
  var BALLOT_LENGTH = 2;
265
269
  var MSG_LENGTH = 10;
266
270
  var PACKED_CMD_LENGTH = 4;
@@ -276,8 +280,6 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
276
280
  var STATE_LEAF_PUB_Y_IDX = 1;
277
281
  // Voice Credit balance.
278
282
  var STATE_LEAF_VOICE_CREDIT_BALANCE_IDX = 2;
279
- // Timestamp.
280
- var STATE_LEAF_TIMESTAMP_IDX = 3;
281
283
  var N_BITS = 252;
282
284
 
283
285
  // Number of users that have completed the sign up.
@@ -356,7 +358,7 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
356
358
  var computedStateLeafPathIndices[stateTreeDepth] = MerkleGeneratePathIndices(stateTreeDepth)(stateIndexMux);
357
359
 
358
360
  // 3. Verify that the original state leaf exists in the given state root.
359
- var stateLeafHash = PoseidonHasher(4)(stateLeaf);
361
+ var stateLeafHash = PoseidonHasher(3)(stateLeaf);
360
362
  var stateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
361
363
  stateLeafHash,
362
364
  actualStateTreeDepth,
@@ -421,11 +423,10 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
421
423
  newBallotVoRoot <== newBallotVoRootMux;
422
424
 
423
425
  // 6. Generate a new state root.
424
- var computedNewStateLeafhash = PoseidonHasher(4)([
426
+ var computedNewStateLeafhash = PoseidonHasher(3)([
425
427
  computedNewSlPubKey[STATE_LEAF_PUB_X_IDX],
426
428
  computedNewSlPubKey[STATE_LEAF_PUB_Y_IDX],
427
- voiceCreditBalanceMux,
428
- stateLeaf[STATE_LEAF_TIMESTAMP_IDX]
429
+ voiceCreditBalanceMux
429
430
  ]);
430
431
 
431
432
  var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
@@ -447,3 +448,4 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
447
448
 
448
449
  newBallotRoot <== computedNewBallotQip;
449
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
+ }