@maci-protocol/circuits 0.0.0-ci.00107eb

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 (59) hide show
  1. package/CHANGELOG.md +673 -0
  2. package/LICENSE +21 -0
  3. package/README.md +20 -0
  4. package/build/ts/compile.d.ts +10 -0
  5. package/build/ts/compile.d.ts.map +1 -0
  6. package/build/ts/compile.js +123 -0
  7. package/build/ts/compile.js.map +1 -0
  8. package/build/ts/generateZkeys.d.ts +9 -0
  9. package/build/ts/generateZkeys.d.ts.map +1 -0
  10. package/build/ts/generateZkeys.js +67 -0
  11. package/build/ts/generateZkeys.js.map +1 -0
  12. package/build/ts/info.d.ts +2 -0
  13. package/build/ts/info.d.ts.map +1 -0
  14. package/build/ts/info.js +72 -0
  15. package/build/ts/info.js.map +1 -0
  16. package/build/ts/types.d.ts +104 -0
  17. package/build/ts/types.d.ts.map +1 -0
  18. package/build/ts/types.js +3 -0
  19. package/build/ts/types.js.map +1 -0
  20. package/build/tsconfig.build.tsbuildinfo +1 -0
  21. package/circom/circuits.json +74 -0
  22. package/circom/coordinator/full/MessageProcessor.circom +253 -0
  23. package/circom/coordinator/full/SingleMessageProcessor.circom +203 -0
  24. package/circom/coordinator/non-qv/MessageProcessor.circom +252 -0
  25. package/circom/coordinator/non-qv/SingleMessageProcessor.circom +199 -0
  26. package/circom/coordinator/non-qv/VoteTally.circom +161 -0
  27. package/circom/coordinator/qv/MessageProcessor.circom +250 -0
  28. package/circom/coordinator/qv/SingleMessageProcessor.circom +207 -0
  29. package/circom/coordinator/qv/VoteTally.circom +179 -0
  30. package/circom/coordinator/qv/VoteTallyWithIndividualCounts.circom +226 -0
  31. package/circom/utils/CalculateTotal.circom +24 -0
  32. package/circom/utils/EdDSAPoseidonVerifier.circom +91 -0
  33. package/circom/utils/MessageHasher.circom +57 -0
  34. package/circom/utils/MessageToCommand.circom +107 -0
  35. package/circom/utils/PoseidonHasher.circom +29 -0
  36. package/circom/utils/PrivateToPublicKey.circom +38 -0
  37. package/circom/utils/VerifySignature.circom +39 -0
  38. package/circom/utils/full/MessageValidator.circom +91 -0
  39. package/circom/utils/full/StateLeafAndBallotTransformer.circom +122 -0
  40. package/circom/utils/non-qv/MessageValidator.circom +91 -0
  41. package/circom/utils/non-qv/ResultCommitmentVerifier.circom +84 -0
  42. package/circom/utils/non-qv/StateLeafAndBallotTransformer.circom +105 -0
  43. package/circom/utils/qv/MessageValidator.circom +97 -0
  44. package/circom/utils/qv/ResultCommitmentVerifier.circom +107 -0
  45. package/circom/utils/qv/StateLeafAndBallotTransformer.circom +105 -0
  46. package/circom/utils/trees/BinaryMerkleRoot.circom +65 -0
  47. package/circom/utils/trees/CheckRoot.circom +49 -0
  48. package/circom/utils/trees/LeafExists.circom +27 -0
  49. package/circom/utils/trees/MerkleTreeInclusionProof.circom +50 -0
  50. package/circom/utils/trees/QuinaryCheckRoot.circom +54 -0
  51. package/circom/utils/trees/QuinaryGeneratePathIndices.circom +44 -0
  52. package/circom/utils/trees/QuinaryLeafExists.circom +30 -0
  53. package/circom/utils/trees/QuinarySelector.circom +42 -0
  54. package/circom/utils/trees/QuinaryTreeInclusionProof.circom +55 -0
  55. package/circom/utils/trees/Splicer.circom +76 -0
  56. package/circom/voter/PollJoined.circom +43 -0
  57. package/circom/voter/PollJoining.circom +54 -0
  58. package/circomkit.json +18 -0
  59. package/package.json +74 -0
@@ -0,0 +1,76 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // circomlib imports
4
+ include "./mux1.circom";
5
+ // zk-kit import
6
+ include "./safe-comparators.circom";
7
+ // local imports
8
+ include "./QuinarySelector.circom";
9
+
10
+ /**
11
+ * The output array contains the input items, with the leaf inserted at the
12
+ * specified index. For example, if input = [0, 20, 30, 40], index = 3, and
13
+ * leaf = 10, the output will be [0, 20, 30, 10, 40].
14
+ */
15
+ template Splicer(numItems) {
16
+ // The number of output items (because only one item is inserted).
17
+ var NUM_OUTPUT_ITEMS = numItems + 1;
18
+
19
+ // The input items to splice.
20
+ signal input in[numItems];
21
+ // The leaf to insert.
22
+ signal input leaf;
23
+ // The index at which to insert the leaf.
24
+ signal input index;
25
+ // The output array containing the spliced items.
26
+ signal output out[NUM_OUTPUT_ITEMS];
27
+
28
+ // There is a loop where the goal is to assign values to the output signal.
29
+ //
30
+ // | output[0] | output[1] | output[2] | ...
31
+ //
32
+ // We can either assign the leaf, or an item from the `items` signal, to the output, using Mux1().
33
+ // The Mux1's selector is 0 or 1 depending on whether the index is equal to the loop counter.
34
+ //
35
+ // i --> [IsEqual] <-- index
36
+ // |
37
+ // v
38
+ // leaf --> [Mux1] <-- <item from in>
39
+ // |
40
+ // v
41
+ // output[m]
42
+ //
43
+ // To obtain the value from <item from in>, we need to compute an item
44
+ // index (let it be `s`).
45
+ // 1. if index = 2 and i = 0, then s = 0
46
+ // 2. if index = 2 and i = 1, then s = 1
47
+ // 3. if index = 2 and i = 2, then s = 2
48
+ // 4. if index = 2 and i = 3, then s = 2
49
+ // 5. if index = 2 and i = 4, then s = 3
50
+ // We then wire `s`, as well as each item in `in` to a QuinarySelector.
51
+ // The output signal from the QuinarySelector is <item from in> and gets
52
+ // wired to Mux1 (as above).
53
+
54
+ var inputs[NUM_OUTPUT_ITEMS];
55
+
56
+ for (var i = 0; i < numItems; i++) {
57
+ inputs[i] = in[i];
58
+ }
59
+
60
+ inputs[NUM_OUTPUT_ITEMS - 1] = 0;
61
+
62
+ for (var i = 0; i < NUM_OUTPUT_ITEMS; i++) {
63
+ // Determines if current index is greater than the insertion index.
64
+ var computedIsIndexAfterInsertPoint = SafeGreaterThan(3)([i, index]);
65
+
66
+ // Calculates correct index for original items, adjusting for leaf insertion.
67
+ var computedAdjustedIndex = i - computedIsIndexAfterInsertPoint;
68
+
69
+ // Selects item from the original array or the leaf for insertion.
70
+ var computedQuinarySelected = QuinarySelector(NUM_OUTPUT_ITEMS)(inputs, computedAdjustedIndex);
71
+ var computedIsIndexEqual = IsEqual()([index, i]);
72
+ var mux = Mux1()([computedQuinarySelected, leaf], computedIsIndexEqual);
73
+
74
+ out[i] <== mux;
75
+ }
76
+ }
@@ -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
+ // Index
23
+ signal input index;
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
+ index,
39
+ pathElements
40
+ );
41
+
42
+ calculatedRoot === stateRoot;
43
+ }
@@ -0,0 +1,54 @@
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 Joining Circuit
9
+ // Allows a user to prove knowledge of a private key that is signed up to
10
+ // a MACI contract.
11
+ template PollJoining(stateTreeDepth) {
12
+ // Constants defining the tree structure
13
+ var STATE_TREE_ARITY = 2;
14
+
15
+ // User's private key
16
+ signal input privateKey;
17
+ // Poll's public key
18
+ signal input pollPublicKey[2];
19
+ // Siblings
20
+ signal input siblings[stateTreeDepth][STATE_TREE_ARITY - 1];
21
+ // Index
22
+ signal input index;
23
+ // User's hashed private key
24
+ signal input nullifier;
25
+ // MACI State tree root which proves the user is signed up
26
+ signal input stateRoot;
27
+ // The actual tree depth (might be <= stateTreeDepth) used in BinaryMerkleRoot
28
+ signal input actualStateTreeDepth;
29
+ // The poll id
30
+ signal input pollId;
31
+
32
+ // Compute the nullifier (hash of private key and poll id)
33
+ var computedNullifier = PoseidonHasher(2)([privateKey, pollId]);
34
+ nullifier === computedNullifier;
35
+
36
+ // User private to public key
37
+ var derivedPublicKey[2] = PrivateToPublicKey()(privateKey);
38
+ // Hash the public key
39
+ var publicKeyHash = PoseidonHasher(2)([derivedPublicKey[0], derivedPublicKey[1]]);
40
+
41
+ // Ensure the poll public key is the same as the maci one (public input)
42
+ derivedPublicKey[0] === pollPublicKey[0];
43
+ derivedPublicKey[1] === pollPublicKey[1];
44
+
45
+ // Inclusion proof
46
+ var calculatedRoot = BinaryMerkleRoot(stateTreeDepth)(
47
+ publicKeyHash,
48
+ actualStateTreeDepth,
49
+ index,
50
+ siblings
51
+ );
52
+
53
+ calculatedRoot === stateRoot;
54
+ }
package/circomkit.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "protocol": "groth16",
3
+ "prime": "bn128",
4
+ "version": "2.1.6",
5
+ "circuits": "./circom/circuits.json",
6
+ "dirPtau": "./ptau",
7
+ "dirCircuits": "./circom",
8
+ "dirInputs": "./inputs",
9
+ "dirBuild": "./build",
10
+ "optimization": 2,
11
+ "inspect": false,
12
+ "include": ["./node_modules/circomlib/circuits", "./node_modules/@zk-kit/circuits/circom"],
13
+ "groth16numContributions": 0,
14
+ "groth16askForEntropy": false,
15
+ "logLevel": "INFO",
16
+ "verbose": true,
17
+ "cWitness": true
18
+ }
package/package.json ADDED
@@ -0,0 +1,74 @@
1
+ {
2
+ "name": "@maci-protocol/circuits",
3
+ "version": "0.0.0-ci.00107eb",
4
+ "private": false,
5
+ "description": "zk-SNARK circuits for MACI",
6
+ "main": "build/ts/index.js",
7
+ "files": [
8
+ "build",
9
+ "circom",
10
+ "circomkit.json",
11
+ "LICENSE",
12
+ "README.md",
13
+ "CHANGELOG.md"
14
+ ],
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "scripts": {
19
+ "build-test-circuits-c": "ts-node ./ts/compile.ts --cWitness",
20
+ "build-test-circuits-wasm": "ts-node ./ts/compile.ts",
21
+ "generate-zkeys": "ts-node ./ts/generateZkeys.ts",
22
+ "info": "NODE_OPTIONS=--max-old-space-size=4096 ts-node ./ts/info.ts",
23
+ "watch": "tsc --watch",
24
+ "build": "tsc -p tsconfig.build.json",
25
+ "circom:build": "NODE_OPTIONS=--max-old-space-size=4096 circomkit compile",
26
+ "circom:setup": "NODE_OPTIONS=--max-old-space-size=4096 circomkit setup",
27
+ "types": "tsc -p tsconfig.json --noEmit",
28
+ "mocha-test": "NODE_OPTIONS=--max-old-space-size=4096 ts-mocha --exit -g '^(?!.*\\[fuzz\\]).*$'",
29
+ "test": "pnpm run mocha-test ts/__tests__/*.test.ts",
30
+ "test:fuzz": "NODE_OPTIONS=--max-old-space-size=4096 ts-mocha --exit -g '\\[fuzz\\]' ./ts/__tests__/*.test.ts",
31
+ "test:poseidonHasher": "pnpm run mocha-test ts/__tests__/PoseidonHasher.test.ts",
32
+ "test:messageHasher": "pnpm run mocha-test ts/__tests__/MessageHasher.test.ts",
33
+ "test:slAndBallotTransformer": "pnpm run mocha-test ts/__tests__/StateLeafAndBallotTransformer.test.ts",
34
+ "test:slAndBallotTransformerFull": "pnpm run mocha-test ts/__tests__/StateLeafAndBallotTransformerFull.test.ts",
35
+ "test:messageToCommand": "pnpm run mocha-test ts/__tests__/MessageToCommand.test.ts",
36
+ "test:messageValidator": "pnpm run mocha-test ts/__tests__/MessageValidator.test.ts",
37
+ "test:messageValidatorFull": "pnpm run mocha-test ts/__tests__/MessageValidatorFull.test.ts",
38
+ "test:verifySignature": "pnpm run mocha-test ts/__tests__/VerifySignature.test.ts",
39
+ "test:privateToPublicKey": "pnpm run mocha-test ts/__tests__/PrivateToPublicKey.test.ts",
40
+ "test:calculateTotal": "pnpm run mocha-test ts/__tests__/CalculateTotal.test.ts",
41
+ "test:messageProcessor": "pnpm run mocha-test ts/__tests__/MessageProcessor.test.ts",
42
+ "test:voteTally": "pnpm run mocha-test ts/__tests__/VoteTally.test.ts",
43
+ "test:ceremonyParams": "pnpm run mocha-test ts/__tests__/CeremonyParams.test.ts",
44
+ "test:incrementalQuinaryTree": "pnpm run mocha-test ts/__tests__/IncrementalQuinaryTree.test.ts",
45
+ "test:pollJoining": "pnpm run mocha-test ts/__tests__/PollJoining.test.ts",
46
+ "test:pollJoined": "pnpm run mocha-test ts/__tests__/PollJoined.test.ts"
47
+ },
48
+ "dependencies": {
49
+ "@maci-protocol/core": "0.0.0-ci.00107eb",
50
+ "@maci-protocol/crypto": "0.0.0-ci.00107eb",
51
+ "@maci-protocol/domainobjs": "0.0.0-ci.00107eb",
52
+ "@maci-protocol/sdk": "0.0.0-ci.00107eb",
53
+ "@zk-kit/circuits": "^0.4.0",
54
+ "circomkit": "^0.3.4",
55
+ "circomlib": "^2.0.5"
56
+ },
57
+ "devDependencies": {
58
+ "@types/chai": "^4.3.11",
59
+ "@types/chai-as-promised": "^8.0.2",
60
+ "@types/mocha": "^10.0.10",
61
+ "@types/node": "^24.2.0",
62
+ "@types/snarkjs": "^0.7.9",
63
+ "@zk-kit/baby-jubjub": "^1.0.3",
64
+ "chai": "^4.3.10",
65
+ "chai-as-promised": "^8.0.1",
66
+ "fast-check": "^4.2.0",
67
+ "glob": "^11.0.3",
68
+ "mocha": "^11.7.2",
69
+ "ts-mocha": "^11.1.0",
70
+ "ts-node": "^10.9.1",
71
+ "typescript": "^5.9.2"
72
+ },
73
+ "gitHead": "a35d35aea039b6cd1abbe8d842ac2aa754661135"
74
+ }