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,56 @@
1
+ const path = require("path");
2
+
3
+ const Scalar = require("ffjavascript").Scalar;
4
+
5
+ const buildPedersenHash = require("circomlibjs").buildPedersenHash;
6
+ const buildBabyJub = require("circomlibjs").buildBabyjub;
7
+
8
+ const wasm_tester = require("circom_tester").wasm;
9
+
10
+
11
+ describe("Pedersen test", function() {
12
+ let babyJub
13
+ let pedersen;
14
+ let F;
15
+ let circuit;
16
+ this.timeout(100000);
17
+ before( async() => {
18
+
19
+ babyJub = await buildBabyJub();
20
+ F = babyJub.F;
21
+ pedersen = await buildPedersenHash();
22
+ circuit = await wasm_tester(path.join(__dirname, "circuits", "pedersen2_test.circom"));
23
+ });
24
+ it("Should pedersen at zero", async () => {
25
+
26
+ let w;
27
+
28
+ w = await circuit.calculateWitness({ in: 0}, true);
29
+
30
+ const b = Buffer.alloc(32);
31
+
32
+ const h = pedersen.hash(b);
33
+ const hP = babyJub.unpackPoint(h);
34
+
35
+ await circuit.assertOut(w, {out: [F.toObject(hP[0]), F.toObject(hP[1])] });
36
+
37
+ });
38
+ it("Should pedersen with 253 ones", async () => {
39
+
40
+ let w;
41
+
42
+ const n = F.e(Scalar.sub(Scalar.shl(Scalar.e(1), 253), Scalar.e(1)));
43
+
44
+ w = await circuit.calculateWitness({ in: F.toObject(n)}, true);
45
+
46
+ const b = Buffer.alloc(32);
47
+ for (let i=0; i<31; i++) b[i] = 0xFF;
48
+ b[31] = 0x1F;
49
+
50
+ const h = pedersen.hash(b);
51
+ const hP = babyJub.unpackPoint(h);
52
+
53
+ await circuit.assertOut(w, {out: [F.toObject(hP[0]), F.toObject(hP[1])] });
54
+
55
+ });
56
+ });
@@ -0,0 +1,30 @@
1
+ const path = require("path");
2
+ const wasm_tester = require("circom_tester").wasm;
3
+ const buildBabyJub = require("circomlibjs").buildBabyjub;
4
+
5
+ const babyJub = require("circomlibjs").babyjub;
6
+
7
+
8
+ describe("Point 2 bits test", function() {
9
+ let babyJub;
10
+ let F;
11
+ let circuit;
12
+ this.timeout(100000);
13
+ before( async() => {
14
+ babyJub = await buildBabyJub();
15
+ F = babyJub.F;
16
+
17
+ circuit = await wasm_tester(path.join(__dirname, "circuits", "pointbits_loopback.circom"));
18
+ });
19
+
20
+ it("Should do the both convertions for 8Base", async () => {
21
+ const w = await circuit.calculateWitness({ in: [F.toObject(babyJub.Base8[0]), F.toObject(babyJub.Base8[1])]}, true);
22
+
23
+ await circuit.checkConstraints(w);
24
+ });
25
+ it("Should do the both convertions for Zero point", async () => {
26
+ const w = await circuit.calculateWitness({ in: [0, 1]}, true);
27
+
28
+ await circuit.checkConstraints(w);
29
+ });
30
+ });
@@ -0,0 +1,80 @@
1
+ const chai = require("chai");
2
+ const path = require("path");
3
+ const wasm_tester = require("circom_tester").wasm;
4
+
5
+ const buildPoseidon = require("circomlibjs").buildPoseidon;
6
+
7
+ const assert = chai.assert;
8
+
9
+ describe("Poseidon Circuit test", function () {
10
+ let poseidon;
11
+ let F;
12
+ let circuit6;
13
+ let circuit3;
14
+ let circuitEx;
15
+
16
+ this.timeout(1000000);
17
+
18
+ before( async () => {
19
+ poseidon = await buildPoseidon();
20
+ F = poseidon.F;
21
+ circuit6 = await wasm_tester(path.join(__dirname, "circuits", "poseidon6_test.circom"));
22
+ circuit3 = await wasm_tester(path.join(__dirname, "circuits", "poseidon3_test.circom"));
23
+ circuitEx = await wasm_tester(path.join(__dirname, "circuits", "poseidonex_test.circom"));
24
+ });
25
+
26
+ it("Should check constrain of hash([1, 2]) t=6", async () => {
27
+ const w = await circuit6.calculateWitness({inputs: [1, 2, 0,0,0]}, true);
28
+
29
+ const res2 = poseidon([1,2,0,0,0]);
30
+
31
+ assert(F.eq(F.e("1018317224307729531995786483840663576608797660851238720571059489595066344487"), F.e(res2)));
32
+ await circuit6.assertOut(w, {out : F.toObject(res2)});
33
+ await circuit6.checkConstraints(w);
34
+ });
35
+
36
+ it("Should check constrain of hash([3, 4]) t=6", async () => {
37
+ const w = await circuit6.calculateWitness({inputs: [3, 4,5,10,23]});
38
+
39
+ const res2 = poseidon([3, 4,5,10,23]);
40
+
41
+ assert(F.eq(F.e("13034429309846638789535561449942021891039729847501137143363028890275222221409"), F.e(res2)));
42
+ await circuit6.assertOut(w, {out : F.toObject(res2)});
43
+ await circuit6.checkConstraints(w);
44
+ });
45
+
46
+
47
+ it("Should check constrain of hash([1, 2]) t=3", async () => {
48
+ const w = await circuit3.calculateWitness({inputs: [1, 2]});
49
+
50
+ const res2 = poseidon([1,2]);
51
+
52
+ assert(F.eq(F.e("7853200120776062878684798364095072458815029376092732009249414926327459813530"), F.e(res2)));
53
+ await circuit3.assertOut(w, {out : F.toObject(res2)});
54
+ await circuit3.checkConstraints(w);
55
+ });
56
+
57
+ it("Should check constrain of hash([3, 4]) t=3", async () => {
58
+ const w = await circuit3.calculateWitness({inputs: [3, 4]});
59
+
60
+ const res2 = poseidon([3, 4]);
61
+
62
+ assert(F.eq(F.e("14763215145315200506921711489642608356394854266165572616578112107564877678998"), F.e(res2)));
63
+ await circuit3.assertOut(w, {out : F.toObject(res2)});
64
+ await circuit3.checkConstraints(w);
65
+ });
66
+
67
+ it("Should check constrain of hash with state and 16 ins and outs", async () => {
68
+ const ins = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
69
+ const w = await circuitEx.calculateWitness({inputs: ins, initialState: 17});
70
+
71
+ const res2 = poseidon(ins, 17, 17);
72
+ const res2f = [];
73
+ for (let i=0; i<res2.length; i++) {
74
+ res2f[i] = F.toObject(res2[i]);
75
+ }
76
+ await circuitEx.assertOut(w, {out : res2f});
77
+ await circuitEx.checkConstraints(w);
78
+ });
79
+
80
+ });
@@ -0,0 +1,118 @@
1
+ const chai = require("chai");
2
+ const path = require("path");
3
+ const crypto = require("crypto");
4
+ const F1Field = require("ffjavascript").F1Field;
5
+ const Scalar = require("ffjavascript").Scalar;
6
+ exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
7
+ const Fr = new F1Field(exports.p);
8
+
9
+ const assert = chai.assert;
10
+
11
+ const sha256 = require("./helpers/sha256");
12
+
13
+ const wasm_tester = require("circom_tester").wasm;
14
+
15
+ // const printSignal = require("./helpers/printsignal");
16
+
17
+
18
+ function buffer2bitArray(b) {
19
+ const res = [];
20
+ for (let i=0; i<b.length; i++) {
21
+ for (let j=0; j<8; j++) {
22
+ res.push((b[i] >> (7-j) &1));
23
+ }
24
+ }
25
+ return res;
26
+ }
27
+
28
+ function bitArray2buffer(a) {
29
+ const len = Math.floor((a.length -1 )/8)+1;
30
+ const b = new Buffer.alloc(len);
31
+
32
+ for (let i=0; i<a.length; i++) {
33
+ const p = Math.floor(i/8);
34
+ b[p] = b[p] | (Number(a[i]) << ( 7 - (i%8) ));
35
+ }
36
+ return b;
37
+ }
38
+
39
+
40
+ describe("SHA256 test", function () {
41
+ this.timeout(100000);
42
+
43
+
44
+ it("Should work bits to array and array to bits", async () => {
45
+ const b = new Buffer.alloc(64);
46
+ for (let i=0; i<64; i++) {
47
+ b[i] = i+1;
48
+ }
49
+ const a = buffer2bitArray(b);
50
+ const b2 = bitArray2buffer(a);
51
+
52
+ assert.equal(b.toString("hex"), b2.toString("hex"), true);
53
+ });
54
+
55
+ it("Should calculate a hash of 1 compressor", async () => {
56
+ const cir = await wasm_tester(path.join(__dirname, "circuits", "sha256_2_test.circom"));
57
+
58
+ const witness = await cir.calculateWitness({ "a": "1", "b": "2" }, true);
59
+
60
+ const b = new Buffer.alloc(54);
61
+ b[26] = 1;
62
+ b[53] = 2;
63
+
64
+ const hash = crypto.createHash("sha256")
65
+ .update(b)
66
+ .digest("hex");
67
+ const r = "0x" + hash.slice(10);
68
+
69
+ const hash2 = sha256.hash(b.toString("hex"), {msgFormat: "hex-bytes"});
70
+
71
+ assert.equal(hash, hash2);
72
+
73
+ assert(Fr.eq(witness[1], Fr.e(r)));
74
+ }).timeout(1000000);
75
+
76
+ it("Should calculate a hash of 2 compressor", async () => {
77
+ const cir = await wasm_tester(path.join(__dirname, "circuits", "sha256_test512.circom"));
78
+
79
+ const b = new Buffer.alloc(64);
80
+ for (let i=0; i<64; i++) {
81
+ b[i] = i+1;
82
+ }
83
+
84
+ const hash = crypto.createHash("sha256")
85
+ .update(b)
86
+ .digest("hex");
87
+
88
+ const arrIn = buffer2bitArray(b);
89
+ const witness = await cir.calculateWitness({ "in": arrIn }, true);
90
+
91
+ const arrOut = witness.slice(1, 257);
92
+ const hash2 = bitArray2buffer(arrOut).toString("hex");
93
+
94
+ assert.equal(hash, hash2);
95
+
96
+ }).timeout(1000000);
97
+ it ("Should calculate a hash of 2 compressor", async () => {
98
+ const cir = await wasm_tester(path.join(__dirname, "circuits", "sha256_test448.circom"));
99
+
100
+ const testStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
101
+
102
+ const b = Buffer.from(testStr, "utf8");
103
+
104
+ const hash = crypto.createHash("sha256")
105
+ .update(b)
106
+ .digest("hex");
107
+
108
+ const arrIn = buffer2bitArray(b);
109
+
110
+ const witness = await cir.calculateWitness({ "in": arrIn }, true);
111
+
112
+ const arrOut = witness.slice(1, 257);
113
+ const hash2 = bitArray2buffer(arrOut).toString("hex");
114
+
115
+ assert.equal(hash, hash2);
116
+ });
117
+
118
+ });
@@ -0,0 +1,82 @@
1
+ const path = require("path");
2
+ const F1Field = require("ffjavascript").F1Field;
3
+ const Scalar = require("ffjavascript").Scalar;
4
+ exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
5
+ const Fr = new F1Field(exports.p);
6
+ const wasm_tester = require("circom_tester").wasm;
7
+
8
+ function print(circuit, w, s) {
9
+ console.log(s + ": " + w[circuit.getSignalIdx(s)]);
10
+ }
11
+
12
+ function getBits(v, n) {
13
+ const res = [];
14
+ for (let i=0; i<n; i++) {
15
+ if (Scalar.isOdd(Scalar.shr(v, i))) {
16
+ res.push(Fr.one);
17
+ } else {
18
+ res.push(Fr.zero);
19
+ }
20
+ }
21
+ return res;
22
+ }
23
+
24
+ const q = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
25
+
26
+ describe("Sign test", function() {
27
+ let circuit;
28
+ this.timeout(100000);
29
+
30
+ before( async() => {
31
+ circuit = await wasm_tester(path.join(__dirname, "circuits", "sign_test.circom"));
32
+ });
33
+
34
+ it("Sign of 0", async () => {
35
+ const inp = getBits(Scalar.e(0), 254);
36
+ const w = await circuit.calculateWitness({in: inp}, true);
37
+
38
+ await circuit.assertOut(w, {sign: 0});
39
+ });
40
+
41
+ it("Sign of 3", async () => {
42
+ const inp = getBits(Scalar.e(3), 254);
43
+ const w = await circuit.calculateWitness({in: inp}, true);
44
+
45
+ await circuit.assertOut(w, {sign: 0});
46
+ });
47
+
48
+ it("Sign of q/2", async () => {
49
+ const inp = getBits(Scalar.shr(q, 1), 254);
50
+ const w = await circuit.calculateWitness({in: inp}, true);
51
+
52
+ await circuit.assertOut(w, {sign: 0});
53
+ });
54
+
55
+ it("Sign of q/2+1", async () => {
56
+ const inp = getBits(Scalar.add(Scalar.shr(q, 1), 1) , 254);
57
+ const w = await circuit.calculateWitness({in: inp}, true);
58
+
59
+ await circuit.assertOut(w, {sign: 1});
60
+ });
61
+
62
+ it("Sign of q-1", async () => {
63
+ const inp = getBits(Scalar.sub(q, 1), 254);
64
+ const w = await circuit.calculateWitness({in: inp}, true);
65
+
66
+ await circuit.assertOut(w, {sign: 1});
67
+ });
68
+
69
+ it("Sign of q", async () => {
70
+ const inp = getBits(q, 254);
71
+ const w = await circuit.calculateWitness({in: inp}, true);
72
+
73
+ await circuit.assertOut(w, {sign: 1});
74
+ });
75
+
76
+ it("Sign of all ones", async () => {
77
+ const inp = getBits(Scalar.sub(Scalar.shl(1,254),1), 254);
78
+ const w = await circuit.calculateWitness({in: inp}, true);
79
+
80
+ await circuit.assertOut(w, {sign: 1});
81
+ });
82
+ });
@@ -0,0 +1,219 @@
1
+ const chai = require("chai");
2
+ const path = require("path");
3
+ const wasm_tester = require("circom_tester").wasm;
4
+ const F1Field = require("ffjavascript").F1Field;
5
+ const Scalar = require("ffjavascript").Scalar;
6
+
7
+ const newMemEmptyTrie = require("circomlibjs").newMemEmptyTrie;
8
+
9
+ const assert = chai.assert;
10
+
11
+ function print(circuit, w, s) {
12
+ console.log(s + ": " + w[circuit.getSignalIdx(s)]);
13
+ }
14
+
15
+ async function testInsert(tree, _key, _value, circuit ) {
16
+ const key = tree.F.e(_key);
17
+ const value = tree.F.e(_value)
18
+
19
+ const res = await tree.insert(key,value);
20
+ let siblings = res.siblings;
21
+ for (let i=0; i<siblings.length; i++) siblings[i] = tree.F.toObject(siblings[i]);
22
+ while (siblings.length<10) siblings.push(0);
23
+
24
+ const w = await circuit.calculateWitness({
25
+ fnc: [1,0],
26
+ oldRoot: tree.F.toObject(res.oldRoot),
27
+ siblings: siblings,
28
+ oldKey: res.isOld0 ? 0 : tree.F.toObject(res.oldKey),
29
+ oldValue: res.isOld0 ? 0 : tree.F.toObject(res.oldValue),
30
+ isOld0: res.isOld0 ? 1 : 0,
31
+ newKey: tree.F.toObject(key),
32
+ newValue: tree.F.toObject(value)
33
+ }, true);
34
+
35
+ await circuit.checkConstraints(w);
36
+
37
+ await circuit.assertOut(w, {newRoot: tree.F.toObject(res.newRoot)});
38
+
39
+ }
40
+
41
+ async function testDelete(tree, _key, circuit) {
42
+ const key = tree.F.e(_key);
43
+ const res = await tree.delete(key);
44
+ let siblings = res.siblings;
45
+ for (let i=0; i<siblings.length; i++) siblings[i] = tree.F.toObject(siblings[i]);
46
+ while (siblings.length<10) siblings.push(0);
47
+
48
+ const w = await circuit.calculateWitness({
49
+ fnc: [1,1],
50
+ oldRoot: tree.F.toObject(res.oldRoot),
51
+ siblings: siblings,
52
+ oldKey: res.isOld0 ? 0 : tree.F.toObject(res.oldKey),
53
+ oldValue: res.isOld0 ? 0 : tree.F.toObject(res.oldValue),
54
+ isOld0: res.isOld0 ? 1 : 0,
55
+ newKey: tree.F.toObject(res.delKey),
56
+ newValue: tree.F.toObject(res.delValue)
57
+ }, true);
58
+
59
+ await circuit.checkConstraints(w);
60
+
61
+ await circuit.assertOut(w, {newRoot: tree.F.toObject(res.newRoot)});
62
+ }
63
+
64
+ async function testUpdate(tree, _key, _newValue, circuit) {
65
+ const key = tree.F.e(_key);
66
+ const newValue = tree.F.e(_newValue);
67
+ const res = await tree.update(key, newValue);
68
+ let siblings = res.siblings;
69
+ for (let i=0; i<siblings.length; i++) siblings[i] = tree.F.toObject(siblings[i]);
70
+ while (siblings.length<10) siblings.push(0);
71
+
72
+ const w = await circuit.calculateWitness({
73
+ fnc: [0,1],
74
+ oldRoot: tree.F.toObject(res.oldRoot),
75
+ siblings: siblings,
76
+ oldKey: tree.F.toObject(res.oldKey),
77
+ oldValue: tree.F.toObject(res.oldValue),
78
+ isOld0: 0,
79
+ newKey: tree.F.toObject(res.newKey),
80
+ newValue: tree.F.toObject(res.newValue)
81
+ });
82
+
83
+ await circuit.checkConstraints(w);
84
+
85
+ await circuit.assertOut(w, {newRoot: tree.F.toObject(res.newRoot)});
86
+ }
87
+
88
+
89
+ describe("SMT Processor test", function () {
90
+ let circuit;
91
+ let tree;
92
+ let Fr;
93
+
94
+ this.timeout(1000000000);
95
+
96
+ before( async () => {
97
+ circuit = await wasm_tester(path.join(__dirname, "circuits", "smtprocessor10_test.circom"));
98
+ await circuit.loadSymbols();
99
+
100
+ tree = await newMemEmptyTrie();
101
+ Fr = tree.F;
102
+ });
103
+
104
+ it("Should verify an insert to an empty tree", async () => {
105
+ const key = Fr.e(111);
106
+ const value = Fr.e(222);
107
+
108
+ await testInsert(tree, key, value, circuit);
109
+ });
110
+
111
+ it("It should add another element", async () => {
112
+ const key = Fr.e(333);
113
+ const value = Fr.e(444);
114
+
115
+ await testInsert(tree, key, value, circuit);
116
+ });
117
+
118
+ it("Should remove an element", async () => {
119
+ await testDelete(tree, 111, circuit);
120
+ await testDelete(tree, 333, circuit);
121
+ });
122
+
123
+ it("Should test convination of adding and removing 3 elements", async () => {
124
+ const keys = [Fr.e(8), Fr.e(9), Fr.e(32)];
125
+ const values = [Fr.e(88), Fr.e(99), Fr.e(3232)];
126
+ const tree1 = await newMemEmptyTrie();
127
+ const tree2 = await newMemEmptyTrie();
128
+ const tree3 = await newMemEmptyTrie();
129
+ const tree4 = await newMemEmptyTrie();
130
+ const tree5 = await newMemEmptyTrie();
131
+ const tree6 = await newMemEmptyTrie();
132
+
133
+ await testInsert(tree1,keys[0],values[0], circuit);
134
+ await testInsert(tree1,keys[1],values[1], circuit);
135
+ await testInsert(tree1,keys[2],values[2], circuit);
136
+
137
+ await testInsert(tree2,keys[0],values[0], circuit);
138
+ await testInsert(tree2,keys[2],values[2], circuit);
139
+ await testInsert(tree2,keys[1],values[1], circuit);
140
+
141
+ await testInsert(tree3,keys[1],values[1], circuit);
142
+ await testInsert(tree3,keys[0],values[0], circuit);
143
+ await testInsert(tree3,keys[2],values[2], circuit);
144
+
145
+ await testInsert(tree4,keys[1],values[1], circuit);
146
+ await testInsert(tree4,keys[2],values[2], circuit);
147
+ await testInsert(tree4,keys[0],values[0], circuit);
148
+
149
+ await testInsert(tree5,keys[2],values[2], circuit);
150
+ await testInsert(tree5,keys[0],values[0], circuit);
151
+ await testInsert(tree5,keys[1],values[1], circuit);
152
+
153
+ await testInsert(tree6,keys[2],values[2], circuit);
154
+ await testInsert(tree6,keys[1],values[1], circuit);
155
+ await testInsert(tree6,keys[0],values[0], circuit);
156
+
157
+
158
+ await testDelete(tree1, keys[0], circuit);
159
+ await testDelete(tree1, keys[1], circuit);
160
+ await testDelete(tree2, keys[1], circuit);
161
+ await testDelete(tree2, keys[0], circuit);
162
+
163
+ await testDelete(tree3, keys[0], circuit);
164
+ await testDelete(tree3, keys[2], circuit);
165
+ await testDelete(tree4, keys[2], circuit);
166
+ await testDelete(tree4, keys[0], circuit);
167
+
168
+
169
+ await testDelete(tree5, keys[1], circuit);
170
+ await testDelete(tree5, keys[2], circuit);
171
+ await testDelete(tree6, keys[2], circuit);
172
+ await testDelete(tree6, keys[1], circuit);
173
+
174
+ await testDelete(tree1, keys[2], circuit);
175
+ await testDelete(tree2, keys[2], circuit);
176
+ await testDelete(tree3, keys[1], circuit);
177
+ await testDelete(tree4, keys[1], circuit);
178
+ await testDelete(tree5, keys[0], circuit);
179
+ await testDelete(tree6, keys[0], circuit);
180
+ });
181
+
182
+ it("Should match a NOp with random vals", async () => {
183
+ let siblings = [];
184
+ while (siblings.length<10) siblings.push(88);
185
+ const w = await circuit.calculateWitness({
186
+ fnc: [0,0],
187
+ oldRoot: 11,
188
+ siblings: siblings,
189
+ oldKey: 33,
190
+ oldValue: 44,
191
+ isOld0: 55,
192
+ newKey: 66,
193
+ newValue: 77
194
+ });
195
+
196
+ const root1 = Fr.e(w[circuit.symbols["main.oldRoot"].varIdx]);
197
+ const root2 = Fr.e(w[circuit.symbols["main.newRoot"].varIdx]);
198
+
199
+ await circuit.checkConstraints(w);
200
+
201
+ assert(Fr.eq(root1, root2));
202
+ });
203
+ it("Should update an element", async () => {
204
+ const tree1 = await newMemEmptyTrie();
205
+ const tree2 = await newMemEmptyTrie();
206
+
207
+ await testInsert(tree1,8,88, circuit);
208
+ await testInsert(tree1,9,99, circuit);
209
+ await testInsert(tree1,32,3232, circuit);
210
+
211
+ await testInsert(tree2,8,888, circuit);
212
+ await testInsert(tree2,9,999, circuit);
213
+ await testInsert(tree2,32,323232, circuit);
214
+
215
+ await testUpdate(tree1, 8, 888, circuit);
216
+ await testUpdate(tree1, 9, 999, circuit);
217
+ await testUpdate(tree1, 32, 323232, circuit);
218
+ });
219
+ });