starkshield 1.0.0

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 (156) hide show
  1. package/README.md +101 -0
  2. package/bin/circom +0 -0
  3. package/bin/cli.js +36 -0
  4. package/bin/index.js +13 -0
  5. package/circomlib/.vscode/launch.json +63 -0
  6. package/circomlib/README.md +18 -0
  7. package/circomlib/circuits/README.md +830 -0
  8. package/circomlib/circuits/aliascheck.circom +33 -0
  9. package/circomlib/circuits/babyjub.circom +107 -0
  10. package/circomlib/circuits/binsub.circom +74 -0
  11. package/circomlib/circuits/binsum.circom +101 -0
  12. package/circomlib/circuits/bitify.circom +106 -0
  13. package/circomlib/circuits/comparators.circom +141 -0
  14. package/circomlib/circuits/compconstant.circom +74 -0
  15. package/circomlib/circuits/eddsa.circom +139 -0
  16. package/circomlib/circuits/eddsamimc.circom +124 -0
  17. package/circomlib/circuits/eddsamimcsponge.circom +124 -0
  18. package/circomlib/circuits/eddsaposeidon.circom +123 -0
  19. package/circomlib/circuits/escalarmul.circom +166 -0
  20. package/circomlib/circuits/escalarmulany.circom +197 -0
  21. package/circomlib/circuits/escalarmulfix.circom +299 -0
  22. package/circomlib/circuits/escalarmulw4table.circom +52 -0
  23. package/circomlib/circuits/gates.circom +96 -0
  24. package/circomlib/circuits/mimc.circom +156 -0
  25. package/circomlib/circuits/mimcsponge.circom +293 -0
  26. package/circomlib/circuits/montgomery.circom +142 -0
  27. package/circomlib/circuits/multiplexer.circom +115 -0
  28. package/circomlib/circuits/mux1.circom +48 -0
  29. package/circomlib/circuits/mux2.circom +63 -0
  30. package/circomlib/circuits/mux3.circom +75 -0
  31. package/circomlib/circuits/mux4.circom +119 -0
  32. package/circomlib/circuits/pedersen.circom +257 -0
  33. package/circomlib/circuits/pedersen_old.circom +68 -0
  34. package/circomlib/circuits/pointbits.circom +164 -0
  35. package/circomlib/circuits/poseidon.circom +208 -0
  36. package/circomlib/circuits/poseidon_constants.circom +24959 -0
  37. package/circomlib/circuits/poseidon_constants_old.circom +252 -0
  38. package/circomlib/circuits/poseidon_old.circom +97 -0
  39. package/circomlib/circuits/sha256/ch.circom +47 -0
  40. package/circomlib/circuits/sha256/constants.circom +53 -0
  41. package/circomlib/circuits/sha256/main.circom +35 -0
  42. package/circomlib/circuits/sha256/maj.circom +45 -0
  43. package/circomlib/circuits/sha256/rotate.circom +28 -0
  44. package/circomlib/circuits/sha256/sha256.circom +81 -0
  45. package/circomlib/circuits/sha256/sha256_2.circom +91 -0
  46. package/circomlib/circuits/sha256/sha256compression.circom +166 -0
  47. package/circomlib/circuits/sha256/sha256compression_function.circom +112 -0
  48. package/circomlib/circuits/sha256/shift.circom +33 -0
  49. package/circomlib/circuits/sha256/sigma.circom +77 -0
  50. package/circomlib/circuits/sha256/sigmaplus.circom +50 -0
  51. package/circomlib/circuits/sha256/t1.circom +58 -0
  52. package/circomlib/circuits/sha256/t2.circom +51 -0
  53. package/circomlib/circuits/sha256/xor3.circom +45 -0
  54. package/circomlib/circuits/sign.circom +36 -0
  55. package/circomlib/circuits/smt/smthash_mimc.circom +58 -0
  56. package/circomlib/circuits/smt/smthash_poseidon.circom +57 -0
  57. package/circomlib/circuits/smt/smtlevins.circom +103 -0
  58. package/circomlib/circuits/smt/smtprocessor.circom +261 -0
  59. package/circomlib/circuits/smt/smtprocessorlevel.circom +95 -0
  60. package/circomlib/circuits/smt/smtprocessorsm.circom +165 -0
  61. package/circomlib/circuits/smt/smtverifier.circom +138 -0
  62. package/circomlib/circuits/smt/smtverifierlevel.circom +71 -0
  63. package/circomlib/circuits/smt/smtverifiersm.circom +106 -0
  64. package/circomlib/circuits/switcher.circom +42 -0
  65. package/circomlib/doc/root_transfer.monopic +0 -0
  66. package/circomlib/doc/smt_diagram_0.monopic +0 -0
  67. package/circomlib/doc/smt_diagram_1.monopic +0 -0
  68. package/circomlib/doc/smt_hash.monopic +0 -0
  69. package/circomlib/doc/smt_levins.monopic +0 -0
  70. package/circomlib/doc/smt_sm.monopic +0 -0
  71. package/circomlib/doc/smt_verifier_sm.monopic +0 -0
  72. package/circomlib/doc/voting.monopic +0 -0
  73. package/circomlib/doc/window.monopic +0 -0
  74. package/circomlib/doc/window_chain.monopic +0 -0
  75. package/circomlib/index.js +2 -0
  76. package/circomlib/package.json +33 -0
  77. package/circomlib/test/aliascheck.js +77 -0
  78. package/circomlib/test/babyjub.js +118 -0
  79. package/circomlib/test/binsub.js +52 -0
  80. package/circomlib/test/binsum.js +38 -0
  81. package/circomlib/test/circuits/aliascheck_test.circom +4 -0
  82. package/circomlib/test/circuits/babyadd_tester.circom +4 -0
  83. package/circomlib/test/circuits/babycheck_test.circom +4 -0
  84. package/circomlib/test/circuits/babypbk_test.circom +4 -0
  85. package/circomlib/test/circuits/binsub_test.circom +33 -0
  86. package/circomlib/test/circuits/constants_test.circom +20 -0
  87. package/circomlib/test/circuits/eddsa_test.circom +5 -0
  88. package/circomlib/test/circuits/eddsamimc_test.circom +5 -0
  89. package/circomlib/test/circuits/eddsaposeidon_test.circom +5 -0
  90. package/circomlib/test/circuits/edwards2montgomery.circom +5 -0
  91. package/circomlib/test/circuits/escalarmul_min_test.circom +27 -0
  92. package/circomlib/test/circuits/escalarmul_test.circom +33 -0
  93. package/circomlib/test/circuits/escalarmul_test_min.circom +28 -0
  94. package/circomlib/test/circuits/escalarmulany_test.circom +30 -0
  95. package/circomlib/test/circuits/escalarmulfix_test.circom +31 -0
  96. package/circomlib/test/circuits/escalarmulw4table.circom +20 -0
  97. package/circomlib/test/circuits/escalarmulw4table_test.circom +19 -0
  98. package/circomlib/test/circuits/escalarmulw4table_test3.circom +19 -0
  99. package/circomlib/test/circuits/greatereqthan.circom +5 -0
  100. package/circomlib/test/circuits/greaterthan.circom +5 -0
  101. package/circomlib/test/circuits/isequal.circom +5 -0
  102. package/circomlib/test/circuits/iszero.circom +5 -0
  103. package/circomlib/test/circuits/lesseqthan.circom +5 -0
  104. package/circomlib/test/circuits/lessthan.circom +5 -0
  105. package/circomlib/test/circuits/mimc_sponge_hash_test.circom +5 -0
  106. package/circomlib/test/circuits/mimc_sponge_test.circom +5 -0
  107. package/circomlib/test/circuits/mimc_test.circom +5 -0
  108. package/circomlib/test/circuits/montgomery2edwards.circom +5 -0
  109. package/circomlib/test/circuits/montgomeryadd.circom +5 -0
  110. package/circomlib/test/circuits/montgomerydouble.circom +5 -0
  111. package/circomlib/test/circuits/mux1_1.circom +33 -0
  112. package/circomlib/test/circuits/mux2_1.circom +37 -0
  113. package/circomlib/test/circuits/mux3_1.circom +41 -0
  114. package/circomlib/test/circuits/mux4_1.circom +56 -0
  115. package/circomlib/test/circuits/pedersen2_test.circom +34 -0
  116. package/circomlib/test/circuits/pedersen_test.circom +31 -0
  117. package/circomlib/test/circuits/pointbits_loopback.circom +25 -0
  118. package/circomlib/test/circuits/poseidon3_test.circom +5 -0
  119. package/circomlib/test/circuits/poseidon6_test.circom +5 -0
  120. package/circomlib/test/circuits/poseidonex_test.circom +5 -0
  121. package/circomlib/test/circuits/sha256_2_test.circom +17 -0
  122. package/circomlib/test/circuits/sha256_test448.circom +5 -0
  123. package/circomlib/test/circuits/sha256_test512.circom +5 -0
  124. package/circomlib/test/circuits/sign_test.circom +5 -0
  125. package/circomlib/test/circuits/smtprocessor10_test.circom +5 -0
  126. package/circomlib/test/circuits/smtverifier10_test.circom +5 -0
  127. package/circomlib/test/circuits/sum_test.circom +33 -0
  128. package/circomlib/test/comparators.js +187 -0
  129. package/circomlib/test/eddsa.js +75 -0
  130. package/circomlib/test/eddsamimc.js +102 -0
  131. package/circomlib/test/eddsaposeidon.js +103 -0
  132. package/circomlib/test/escalarmul.js +121 -0
  133. package/circomlib/test/escalarmulany.js +51 -0
  134. package/circomlib/test/escalarmulfix.js +95 -0
  135. package/circomlib/test/helpers/printsignal.js +22 -0
  136. package/circomlib/test/helpers/sha256.js +178 -0
  137. package/circomlib/test/mimccircuit.js +27 -0
  138. package/circomlib/test/mimcspongecircuit.js +47 -0
  139. package/circomlib/test/montgomery.js +101 -0
  140. package/circomlib/test/multiplexer.js +101 -0
  141. package/circomlib/test/pedersen.js +83 -0
  142. package/circomlib/test/pedersen2.js +56 -0
  143. package/circomlib/test/point2bits.js +30 -0
  144. package/circomlib/test/poseidoncircuit.js +80 -0
  145. package/circomlib/test/sha256.js +118 -0
  146. package/circomlib/test/sign.js +82 -0
  147. package/circomlib/test/smtprocessor.js +219 -0
  148. package/circomlib/test/smtverifier.js +141 -0
  149. package/lib/compile.js +82 -0
  150. package/lib/deploy.js +391 -0
  151. package/lib/test.js +47 -0
  152. package/lib/verify.js +153 -0
  153. package/package.json +43 -0
  154. package/pot12_0000.ptau +0 -0
  155. package/pot12_0001.ptau +0 -0
  156. package/ptau/pot12_final.ptau +0 -0
@@ -0,0 +1,71 @@
1
+ /*
2
+ Copyright 2018 0KIMS association.
3
+
4
+ This file is part of circom (Zero Knowledge Circuit Compiler).
5
+
6
+ circom is a free software: you can redistribute it and/or modify it
7
+ under the terms of the GNU General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ circom is distributed in the hope that it will be useful, but WITHOUT
12
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14
+ License for more details.
15
+
16
+ You should have received a copy of the GNU General Public License
17
+ along with circom. If not, see <https://www.gnu.org/licenses/>.
18
+ */
19
+
20
+ /******
21
+
22
+ SMTVerifierLevel
23
+
24
+ This circuit has 1 hash
25
+
26
+ Outputs according to the state.
27
+
28
+ State root
29
+ ===== =======
30
+ top H'(child, sibling)
31
+ i0 0
32
+ iold old1leaf
33
+ inew new1leaf
34
+ na 0
35
+
36
+ H' is the Hash function with the inputs shifted acordingly.
37
+
38
+ *****/
39
+ pragma circom 2.0.0;
40
+
41
+ template SMTVerifierLevel() {
42
+ signal input st_top;
43
+ signal input st_i0;
44
+ signal input st_iold;
45
+ signal input st_inew;
46
+ signal input st_na;
47
+
48
+ signal output root;
49
+ signal input sibling;
50
+ signal input old1leaf;
51
+ signal input new1leaf;
52
+ signal input lrbit;
53
+ signal input child;
54
+
55
+ signal aux[2];
56
+
57
+ component proofHash = SMTHash2();
58
+ component switcher = Switcher();
59
+
60
+ switcher.L <== child;
61
+ switcher.R <== sibling;
62
+
63
+ switcher.sel <== lrbit;
64
+ proofHash.L <== switcher.outL;
65
+ proofHash.R <== switcher.outR;
66
+
67
+ aux[0] <== proofHash.out * st_top;
68
+ aux[1] <== old1leaf*st_iold;
69
+
70
+ root <== aux[0] + aux[1] + new1leaf*st_inew;
71
+ }
@@ -0,0 +1,106 @@
1
+ /*
2
+ Copyright 2018 0KIMS association.
3
+
4
+ This file is part of circom (Zero Knowledge Circuit Compiler).
5
+
6
+ circom is a free software: you can redistribute it and/or modify it
7
+ under the terms of the GNU General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ circom is distributed in the hope that it will be useful, but WITHOUT
12
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14
+ License for more details.
15
+
16
+ You should have received a copy of the GNU General Public License
17
+ along with circom. If not, see <https://www.gnu.org/licenses/>.
18
+ */
19
+
20
+ /*
21
+ Each level in the SMTVerifier has a state.
22
+
23
+ This is the state machine.
24
+
25
+ The signals are
26
+
27
+ levIns: 1 if we are in the level where the insertion should happen
28
+ xor: 1 if the bitKey of the old and new keys are different in this level
29
+ is0: Input that indicates that the oldKey is 0
30
+ fnc: 0 -> VERIFY INCLUSION
31
+ 1 -> VERIFY NOT INCLUSION
32
+
33
+ err state is not a state itself. It's a lack of state.
34
+
35
+ The end of the last level will have to be `na`
36
+
37
+ levIns=0 any
38
+ ┌────┐ ┌────┐
39
+ │ │ │ │
40
+ │ ▼ levIns=1 ▼ │
41
+ │ ########### is0=1 ########### ########### │
42
+ │ # # fnc=1 # # any # # │
43
+ └──# top # ─────────────────────▶# i0 #───────────────▶# na #──┘
44
+ ## ## ──────────┐ ## ## ┌───────▶## ##
45
+ ########─────────────┐│ ######### │┌────────▶#########
46
+ ││ levIns=1 ││
47
+ ││ is0=0 ########### ││
48
+ ││ fnc=1 # # any│
49
+ │└──────────▶ # iold #────────┘│
50
+ │ ## ## │
51
+ │ ######### │
52
+ │ │
53
+ │ levIns=1 ########### │
54
+ │ fnc=0 # # any
55
+ └────────────▶# inew #─────────┘
56
+ ## ##
57
+ #########
58
+
59
+ */
60
+ pragma circom 2.0.0;
61
+
62
+
63
+ template SMTVerifierSM() {
64
+ signal input is0;
65
+ signal input levIns;
66
+ signal input fnc;
67
+
68
+ signal input prev_top;
69
+ signal input prev_i0;
70
+ signal input prev_iold;
71
+ signal input prev_inew;
72
+ signal input prev_na;
73
+
74
+ signal output st_top;
75
+ signal output st_i0;
76
+ signal output st_iold;
77
+ signal output st_inew;
78
+ signal output st_na;
79
+
80
+ signal prev_top_lev_ins;
81
+ signal prev_top_lev_ins_fnc;
82
+
83
+ prev_top_lev_ins <== prev_top * levIns;
84
+ prev_top_lev_ins_fnc <== prev_top_lev_ins*fnc; // prev_top * levIns * fnc
85
+
86
+ // st_top = prev_top * (1-levIns)
87
+ // = + prev_top
88
+ // - prev_top * levIns
89
+ st_top <== prev_top - prev_top_lev_ins;
90
+
91
+ // st_inew = prev_top * levIns * (1-fnc)
92
+ // = + prev_top * levIns
93
+ // - prev_top * levIns * fnc
94
+ st_inew <== prev_top_lev_ins - prev_top_lev_ins_fnc;
95
+
96
+ // st_iold = prev_top * levIns * (1-is0)*fnc
97
+ // = + prev_top * levIns * fnc
98
+ // - prev_top * levIns * fnc * is0
99
+ st_iold <== prev_top_lev_ins_fnc * (1 - is0);
100
+
101
+ // st_i0 = prev_top * levIns * is0
102
+ // = + prev_top * levIns * is0
103
+ st_i0 <== prev_top_lev_ins * is0;
104
+
105
+ st_na <== prev_na + prev_inew + prev_iold + prev_i0;
106
+ }
@@ -0,0 +1,42 @@
1
+ /*
2
+ Copyright 2018 0KIMS association.
3
+
4
+ This file is part of circom (Zero Knowledge Circuit Compiler).
5
+
6
+ circom is a free software: you can redistribute it and/or modify it
7
+ under the terms of the GNU General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ circom is distributed in the hope that it will be useful, but WITHOUT
12
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14
+ License for more details.
15
+
16
+ You should have received a copy of the GNU General Public License
17
+ along with circom. If not, see <https://www.gnu.org/licenses/>.
18
+ */
19
+
20
+ /*
21
+ Assume sel is binary.
22
+
23
+ If sel == 0 then outL = L and outR=R
24
+ If sel == 1 then outL = R and outR=L
25
+
26
+ */
27
+
28
+ pragma circom 2.0.0;
29
+
30
+ template Switcher() {
31
+ signal input sel;
32
+ signal input L;
33
+ signal input R;
34
+ signal output outL;
35
+ signal output outR;
36
+
37
+ signal aux;
38
+
39
+ aux <== (R-L)*sel; // We create aux in order to have only one multiplication
40
+ outL <== aux + L;
41
+ outR <== -aux + R;
42
+ }
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,2 @@
1
+
2
+
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "circomlib",
3
+ "version": "2.0.5",
4
+ "description": "Basic circuits library for Circom",
5
+ "main": "index.js",
6
+ "directories": {
7
+ "test": "test"
8
+ },
9
+ "scripts": {
10
+ "test": "mocha --max-old-space-size=4000"
11
+ },
12
+ "keywords": [
13
+ "pedersen",
14
+ "hash",
15
+ "ethereum",
16
+ "circuit",
17
+ "circom",
18
+ "zksnark"
19
+ ],
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/iden3/circomlib.git"
23
+ },
24
+ "author": "0Kims",
25
+ "license": "GPL-3.0",
26
+ "devDependencies": {
27
+ "blake-hash": "^2.0.0",
28
+ "chai": "^4.3.4",
29
+ "circom_tester": "0.0.13",
30
+ "circomlibjs": "^0.1.4",
31
+ "mocha": "^9.1.3"
32
+ }
33
+ }
@@ -0,0 +1,77 @@
1
+ const chai = require("chai");
2
+ const path = require("path");
3
+
4
+ const assert = chai.assert;
5
+
6
+ const Scalar = require("ffjavascript").Scalar;
7
+ const F1Field = require("ffjavascript").F1Field;
8
+ const utils = require("ffjavascript").utils;
9
+ const q = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
10
+ const F = new F1Field(q);
11
+
12
+ const wasm_tester = require("circom_tester").wasm;
13
+
14
+ function print(circuit, w, s) {
15
+ console.log(s + ": " + w[circuit.getSignalIdx(s)]);
16
+ }
17
+
18
+ function getBits(v, n) {
19
+ const res = [];
20
+ for (let i=0; i<n; i++) {
21
+ if (Scalar.isOdd(Scalar.shr(v,i))) {
22
+ res.push(F.one);
23
+ } else {
24
+ res.push(F.zero);
25
+ }
26
+ }
27
+ return res;
28
+ }
29
+
30
+
31
+ describe("Aliascheck test", function () {
32
+ this.timeout(100000);
33
+
34
+ let cir;
35
+ before( async() => {
36
+
37
+ cir = await wasm_tester(path.join(__dirname, "circuits", "aliascheck_test.circom"));
38
+ });
39
+
40
+ it("Satisfy the aliastest 0", async () => {
41
+ const inp = getBits(0, 254);
42
+ await cir.calculateWitness({in: inp}, true);
43
+ });
44
+
45
+ it("Satisfy the aliastest 3", async () => {
46
+ const inp = getBits(3, 254);
47
+ await cir.calculateWitness({in: inp}, true);
48
+ });
49
+
50
+ it("Satisfy the aliastest q-1", async () => {
51
+ const inp = getBits(F.e(-1), 254);
52
+ // console.log(JSON.stringify(utils.stringifyBigInts(inp)));
53
+ await cir.calculateWitness({in: inp}, true);
54
+ });
55
+
56
+ it("Should not satisfy an input of q", async () => {
57
+ const inp = getBits(q, 254);
58
+ try {
59
+ await cir.calculateWitness({in: inp}, true);
60
+ assert(false);
61
+ } catch(err) {
62
+ assert(err.message.includes("Assert Failed"));
63
+ }
64
+ });
65
+
66
+ it("Should not satisfy all ones", async () => {
67
+
68
+ const inp = getBits(Scalar.sub(Scalar.shl(1, 254) , 1) , 254);
69
+ try {
70
+ await cir.calculateWitness({in: inp}, true);
71
+ assert(false);
72
+ } catch(err) {
73
+ assert(err.message.includes("Assert Failed"));
74
+ }
75
+ });
76
+
77
+ });
@@ -0,0 +1,118 @@
1
+ const chai = require("chai");
2
+ const path = require("path");
3
+
4
+ const createBlakeHash = require("blake-hash");
5
+ const buildEddsa = require("circomlibjs").buildEddsa;
6
+
7
+ const assert = chai.assert;
8
+
9
+ const wasm_tester = require("circom_tester").wasm;
10
+ const utils = require("ffjavascript").utils;
11
+ const Scalar = require("ffjavascript").Scalar;
12
+
13
+ describe("Baby Jub test", function () {
14
+ let eddsa;
15
+ let F;
16
+ let circuitAdd;
17
+ let circuitTest;
18
+ let circuitPbk;
19
+
20
+ this.timeout(100000);
21
+
22
+ before( async() => {
23
+
24
+ eddsa = await buildEddsa();
25
+ F = eddsa.F;
26
+
27
+ circuitAdd = await wasm_tester(path.join(__dirname, "circuits", "babyadd_tester.circom"));
28
+
29
+ circuitTest = await wasm_tester(path.join(__dirname, "circuits", "babycheck_test.circom"));
30
+
31
+ circuitPbk = await wasm_tester(path.join(__dirname, "circuits", "babypbk_test.circom"));
32
+ });
33
+
34
+ it("Should add point (0,1) and (0,1)", async () => {
35
+
36
+ const input={
37
+ x1: 0,
38
+ y1: 1,
39
+ x2: 0,
40
+ y2: 1
41
+ };
42
+
43
+ const w = await circuitAdd.calculateWitness(input, true);
44
+
45
+ await circuitAdd.assertOut(w, {xout: 0, yout: 1});
46
+ });
47
+
48
+ it("Should add 2 same numbers", async () => {
49
+
50
+ const input={
51
+ x1: 17777552123799933955779906779655732241715742912184938656739573121738514868268n,
52
+ y1: 2626589144620713026669568689430873010625803728049924121243784502389097019475n,
53
+ x2: 17777552123799933955779906779655732241715742912184938656739573121738514868268n,
54
+ y2: 2626589144620713026669568689430873010625803728049924121243784502389097019475n
55
+ };
56
+
57
+ const w = await circuitAdd.calculateWitness(input, true);
58
+
59
+ await circuitAdd.assertOut(w, {
60
+ xout: 6890855772600357754907169075114257697580319025794532037257385534741338397365n,
61
+ yout: 4338620300185947561074059802482547481416142213883829469920100239455078257889n
62
+ });
63
+
64
+ });
65
+
66
+ it("Should add 2 different numbers", async () => {
67
+
68
+ const input={
69
+ x1: 17777552123799933955779906779655732241715742912184938656739573121738514868268n,
70
+ y1: 2626589144620713026669568689430873010625803728049924121243784502389097019475n,
71
+ x2: 16540640123574156134436876038791482806971768689494387082833631921987005038935n,
72
+ y2: 20819045374670962167435360035096875258406992893633759881276124905556507972311n
73
+ };
74
+
75
+ const w = await circuitAdd.calculateWitness(input, true);
76
+
77
+ await circuitAdd.assertOut(w, {
78
+ xout: 7916061937171219682591368294088513039687205273691143098332585753343424131937n,
79
+ yout: 14035240266687799601661095864649209771790948434046947201833777492504781204499n
80
+ });
81
+
82
+ });
83
+
84
+ it("Should check (0,1) is a valid point", async() => {
85
+ const w = await circuitTest.calculateWitness({x: 0, y:1}, true);
86
+
87
+ await circuitTest.checkConstraints(w);
88
+ });
89
+
90
+ it("Should check (1,0) is an invalid point", async() => {
91
+ try {
92
+ await circuitTest.calculateWitness({x: 1, y: 0}, true);
93
+ assert(false, "Should be a valid point");
94
+ } catch(err) {
95
+ assert(err.message.includes("Assert Failed"));
96
+ }
97
+ });
98
+
99
+ it("Should extract the public key from the private one", async () => {
100
+
101
+ const rawpvk = Buffer.from("0001020304050607080900010203040506070809000102030405060708090021", "hex");
102
+ const pvk = eddsa.pruneBuffer(createBlakeHash("blake512").update(rawpvk).digest().slice(0,32));
103
+ const S = Scalar.shr(utils.leBuff2int(pvk), 3);
104
+
105
+ const A = eddsa.prv2pub(rawpvk);
106
+
107
+ const input = {
108
+ in : S
109
+ };
110
+
111
+ const w = await circuitPbk.calculateWitness(input, true);
112
+
113
+ await circuitPbk.assertOut(w, {Ax : F.toObject(A[0]), Ay: F.toObject(A[1])});
114
+
115
+ await circuitPbk.checkConstraints(w);
116
+ });
117
+
118
+ });
@@ -0,0 +1,52 @@
1
+ const path = require("path");
2
+
3
+ const Scalar = require("ffjavascript").Scalar;
4
+ const wasm_tester = require("circom_tester").wasm;
5
+
6
+ function print(circuit, w, s) {
7
+ console.log(s + ": " + w[circuit.getSignalIdx(s)]);
8
+ }
9
+
10
+ async function checkSub(_a,_b, circuit) {
11
+ let a=Scalar.e(_a);
12
+ let b=Scalar.e(_b);
13
+ if (Scalar.lt(a, 0)) a = Scalar.add(a, Scalar.shl(1, 16));
14
+ if (Scalar.lt(b, 0)) b = Scalar.add(b, Scalar.shl(1, 16));
15
+ const w = await circuit.calculateWitness({a: a, b: b}, true);
16
+
17
+ let res = Scalar.sub(a, b);
18
+ if (Scalar.lt(res, 0)) res = Scalar.add(res, Scalar.shl(1, 16));
19
+
20
+ await circuit.assertOut(w, {out: res});
21
+ }
22
+
23
+ describe("BinSub test", function () {
24
+
25
+ this.timeout(100000);
26
+
27
+ let circuit;
28
+ before( async() => {
29
+ circuit = await wasm_tester(path.join(__dirname, "circuits", "binsub_test.circom"));
30
+ });
31
+
32
+ it("Should check variuos ege cases", async () => {
33
+ await checkSub(0,0, circuit);
34
+ await checkSub(1,0, circuit);
35
+ await checkSub(-1,0, circuit);
36
+ await checkSub(2,1, circuit);
37
+ await checkSub(2,2, circuit);
38
+ await checkSub(2,3, circuit);
39
+ await checkSub(2,-1, circuit);
40
+ await checkSub(2,-2, circuit);
41
+ await checkSub(2,-3, circuit);
42
+ await checkSub(-2,-3, circuit);
43
+ await checkSub(-2,-2, circuit);
44
+ await checkSub(-2,-1, circuit);
45
+ await checkSub(-2,0, circuit);
46
+ await checkSub(-2,1, circuit);
47
+ await checkSub(-2,2, circuit);
48
+ await checkSub(-2,3, circuit);
49
+ });
50
+
51
+
52
+ });
@@ -0,0 +1,38 @@
1
+ const chai = require("chai");
2
+ const path = require("path");
3
+
4
+ const wasm_tester = require("circom_tester").wasm;
5
+
6
+ const F1Field = require("ffjavascript").F1Field;
7
+ const Scalar = require("ffjavascript").Scalar;
8
+ exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
9
+ const Fr = new F1Field(exports.p);
10
+
11
+ const assert = chai.assert;
12
+
13
+ describe("Binary sum test", function () {
14
+ this.timeout(100000000);
15
+
16
+ it("Should create a constant circuit", async () => {
17
+ const circuit = await wasm_tester(path.join(__dirname, "circuits", "constants_test.circom"));
18
+ await circuit.loadConstraints();
19
+ assert.equal(circuit.nVars, 2);
20
+ assert.equal(circuit.constraints.length, 1);
21
+
22
+ const witness = await circuit.calculateWitness({ "in": Fr.toString(Fr.e("0xd807aa98"))}, true);
23
+
24
+ assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
25
+ assert(Fr.eq(Fr.e(witness[1]),Fr.e("0xd807aa98")));
26
+ });
27
+ it("Should create a sum circuit", async () => {
28
+ const circuit = await wasm_tester(path.join(__dirname, "circuits", "sum_test.circom"));
29
+ await circuit.loadConstraints();
30
+
31
+ assert.equal(circuit.constraints.length, 97); // 32 (in1) + 32(in2) + 32(out) + 1 (carry)
32
+
33
+ const witness = await circuit.calculateWitness({ "a": "111", "b": "222" }, true);
34
+
35
+ assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
36
+ assert(Fr.eq(Fr.e(witness[1]),Fr.e("333")));
37
+ });
38
+ });
@@ -0,0 +1,4 @@
1
+ pragma circom 2.0.0;
2
+ include "../../circuits/aliascheck.circom";
3
+
4
+ component main = AliasCheck();
@@ -0,0 +1,4 @@
1
+ pragma circom 2.0.0;
2
+ include "../../circuits/babyjub.circom";
3
+
4
+ component main = BabyAdd();
@@ -0,0 +1,4 @@
1
+ pragma circom 2.0.0;
2
+ include "../../circuits/babyjub.circom";
3
+
4
+ component main = BabyCheck();
@@ -0,0 +1,4 @@
1
+ pragma circom 2.0.0;
2
+ include "../../circuits/babyjub.circom";
3
+
4
+ component main = BabyPbk();
@@ -0,0 +1,33 @@
1
+ pragma circom 2.0.0;
2
+
3
+ include "../../circuits/bitify.circom";
4
+ include "../../circuits/binsub.circom";
5
+
6
+ template A() {
7
+ signal input a; //private
8
+ signal input b;
9
+ signal output out;
10
+
11
+ var i;
12
+
13
+ component n2ba = Num2Bits(16);
14
+ component n2bb = Num2Bits(16);
15
+ component sub = BinSub(16);
16
+ component b2n = Bits2Num(16);
17
+
18
+ n2ba.in <== a;
19
+ n2bb.in <== b;
20
+
21
+ for (i=0; i<16; i++) {
22
+ sub.in[0][i] <== n2ba.out[i];
23
+ sub.in[1][i] <== n2bb.out[i];
24
+ }
25
+
26
+ for (i=0; i<16; i++) {
27
+ b2n.in[i] <== sub.out[i];
28
+ }
29
+
30
+ out <== b2n.out;
31
+ }
32
+
33
+ component main = A();
@@ -0,0 +1,20 @@
1
+ pragma circom 2.0.0;
2
+
3
+ include "../../circuits/sha256/constants.circom";
4
+
5
+ template A() {
6
+ signal input in;
7
+ component h0;
8
+ h0 = K(8);
9
+
10
+ var lc = 0;
11
+ var e = 1;
12
+ for (var i=0; i<32; i++) {
13
+ lc = lc + e*h0.out[i];
14
+ e *= 2;
15
+ }
16
+
17
+ lc === in;
18
+ }
19
+
20
+ component main {public [in]} = A();
@@ -0,0 +1,5 @@
1
+ pragma circom 2.0.0;
2
+
3
+ include "../../circuits/eddsa.circom";
4
+
5
+ component main = EdDSAVerifier(80);
@@ -0,0 +1,5 @@
1
+ pragma circom 2.0.0;
2
+
3
+ include "../../circuits/eddsamimc.circom";
4
+
5
+ component main = EdDSAMiMCVerifier();
@@ -0,0 +1,5 @@
1
+ pragma circom 2.0.0;
2
+
3
+ include "../../circuits/eddsaposeidon.circom";
4
+
5
+ component main = EdDSAPoseidonVerifier();
@@ -0,0 +1,5 @@
1
+ pragma circom 2.0.0;
2
+
3
+ include "../../circuits/montgomery.circom";
4
+
5
+ component main = Edwards2Montgomery();