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,95 @@
1
+ const chai = require("chai");
2
+ const path = require("path");
3
+ const wasm_tester = require("circom_tester").wasm;
4
+ const buildBabyjub = require("circomlibjs").buildBabyjub;
5
+ const Scalar = require("ffjavascript").Scalar;
6
+
7
+ const assert = chai.assert;
8
+
9
+ function print(circuit, w, s) {
10
+ console.log(s + ": " + w[circuit.getSignalIdx(s)]);
11
+ }
12
+
13
+ describe("Escalarmul test", function () {
14
+ let babyJub;
15
+ let Fr;
16
+ let circuit;
17
+
18
+ this.timeout(100000);
19
+
20
+
21
+ before( async() => {
22
+ babyJub = await buildBabyjub();
23
+ Fr = babyJub.F;
24
+ circuit = await wasm_tester(path.join(__dirname, "circuits", "escalarmulfix_test.circom"));
25
+ });
26
+
27
+ it("Should generate Same escalar mul", async () => {
28
+
29
+ const w = await circuit.calculateWitness({"e": 0});
30
+
31
+ await circuit.checkConstraints(w);
32
+
33
+ await circuit.assertOut(w, {out: [0,1]}, true);
34
+
35
+ });
36
+
37
+ it("Should generate Same escalar mul", async () => {
38
+
39
+ const w = await circuit.calculateWitness({"e": 1}, true);
40
+
41
+ await circuit.checkConstraints(w);
42
+
43
+ await circuit.assertOut(w, {out: [Fr.toObject(babyJub.Base8[0]), Fr.toObject(babyJub.Base8[1])]});
44
+
45
+ });
46
+
47
+ it("Should generate scalar mul of a specific constant", async () => {
48
+
49
+ const s = Scalar.e("2351960337287830298912035165133676222414898052661454064215017316447594616519");
50
+ const base8 = [
51
+ Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
52
+ Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
53
+ ];
54
+
55
+ const w = await circuit.calculateWitness({"e": s}, true);
56
+
57
+ await circuit.checkConstraints(w);
58
+
59
+ const expectedRes = babyJub.mulPointEscalar(base8, s);
60
+
61
+ await circuit.assertOut(w, {out: [Fr.toObject(expectedRes[0]), Fr.toObject(expectedRes[1])]});
62
+
63
+ });
64
+
65
+ it("Should generate scalar mul of the firsts 50 elements", async () => {
66
+
67
+ const base8 = [
68
+ Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
69
+ Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
70
+ ];
71
+
72
+ for (let i=0; i<50; i++) {
73
+ const s = Scalar.e(i);
74
+
75
+ const w = await circuit.calculateWitness({"e": s}, true);
76
+
77
+ await circuit.checkConstraints(w);
78
+
79
+ const expectedRes = babyJub.mulPointEscalar(base8, s);
80
+
81
+ await circuit.assertOut(w, {out: [Fr.toObject(expectedRes[0]), Fr.toObject(expectedRes[1])]});
82
+ }
83
+ });
84
+
85
+ it("If multiply by order should return 0", async () => {
86
+
87
+ const w = await circuit.calculateWitness({"e": babyJub.subOrder }, true);
88
+
89
+ await circuit.checkConstraints(w);
90
+
91
+ await circuit.assertOut(w, {out: [0,1]});
92
+ });
93
+
94
+ });
95
+
@@ -0,0 +1,22 @@
1
+
2
+ const snarkjs = require("snarkjs");
3
+
4
+ const bigInt = snarkjs.bigInt;
5
+
6
+ module.exports = function hexBits(cir, witness, sig, nBits) {
7
+ let v = bigInt(0);
8
+ for (let i=nBits-1; i>=0; i--) {
9
+ v = v.shiftLeft(1);
10
+ const name = sig+"["+i+"]";
11
+ const idx = cir.getSignalIdx(name);
12
+ const vbit = bigInt(witness[idx].toString());
13
+ if (vbit.equals(bigInt(1))) {
14
+ v = v.add(bigInt(1));
15
+ } else if (vbit.equals(bigInt(0))) {
16
+ v;
17
+ } else {
18
+ console.log("Not Binary: "+name);
19
+ }
20
+ }
21
+ return v.toString(16);
22
+ };
@@ -0,0 +1,178 @@
1
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2
+ /* SHA-256 (FIPS 180-4) implementation in JavaScript (c) Chris Veness 2002-2017 */
3
+ /* MIT Licence */
4
+ /* www.movable-type.co.uk/scripts/sha256.html */
5
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
6
+
7
+ 'use strict';
8
+
9
+
10
+ /**
11
+ * SHA-256 hash function reference implementation.
12
+ *
13
+ * This is an annotated direct implementation of FIPS 180-4, without any optimisations. It is
14
+ * intended to aid understanding of the algorithm rather than for production use.
15
+ *
16
+ * While it could be used where performance is not critical, I would recommend using the ‘Web
17
+ * Cryptography API’ (developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest) for the browser,
18
+ * or the ‘crypto’ library (nodejs.org/api/crypto.html#crypto_class_hash) in Node.js.
19
+ *
20
+ * See csrc.nist.gov/groups/ST/toolkit/secure_hashing.html
21
+ * csrc.nist.gov/groups/ST/toolkit/examples.html
22
+ */
23
+ class Sha256 {
24
+
25
+ /**
26
+ * Generates SHA-256 hash of string.
27
+ *
28
+ * @param {string} msg - (Unicode) string to be hashed.
29
+ * @param {Object} [options]
30
+ * @param {string} [options.msgFormat=string] - Message format: 'string' for JavaScript string
31
+ * (gets converted to UTF-8 for hashing); 'hex-bytes' for string of hex bytes ('616263' ≡ 'abc') .
32
+ * @param {string} [options.outFormat=hex] - Output format: 'hex' for string of contiguous
33
+ * hex bytes; 'hex-w' for grouping hex bytes into groups of (4 byte / 8 character) words.
34
+ * @returns {string} Hash of msg as hex character string.
35
+ */
36
+ static hash(msg, options) {
37
+ const defaults = { msgFormat: 'string', outFormat: 'hex' };
38
+ const opt = Object.assign(defaults, options);
39
+
40
+ // note use throughout this routine of 'n >>> 0' to coerce Number 'n' to unsigned 32-bit integer
41
+
42
+ switch (opt.msgFormat) {
43
+ default: // default is to convert string to UTF-8, as SHA only deals with byte-streams
44
+ case 'string': msg = utf8Encode(msg); break;
45
+ case 'hex-bytes':msg = hexBytesToString(msg); break; // mostly for running tests
46
+ }
47
+
48
+ // constants [§4.2.2]
49
+ const K = [
50
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
51
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
52
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
53
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
54
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
55
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
56
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
57
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 ];
58
+
59
+ // initial hash value [§5.3.3]
60
+ const H = [
61
+ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 ];
62
+
63
+ // PREPROCESSING [§6.2.1]
64
+
65
+ msg += String.fromCharCode(0x80); // add trailing '1' bit (+ 0's padding) to string [§5.1.1]
66
+
67
+ // convert string msg into 512-bit blocks (array of 16 32-bit integers) [§5.2.1]
68
+ const l = msg.length/4 + 2; // length (in 32-bit integers) of msg + ‘1’ + appended length
69
+ const N = Math.ceil(l/16); // number of 16-integer (512-bit) blocks required to hold 'l' ints
70
+ const M = new Array(N); // message M is N×16 array of 32-bit integers
71
+
72
+ for (let i=0; i<N; i++) {
73
+ M[i] = new Array(16);
74
+ for (let j=0; j<16; j++) { // encode 4 chars per integer (64 per block), big-endian encoding
75
+ M[i][j] = (msg.charCodeAt(i*64+j*4+0)<<24) | (msg.charCodeAt(i*64+j*4+1)<<16)
76
+ | (msg.charCodeAt(i*64+j*4+2)<< 8) | (msg.charCodeAt(i*64+j*4+3)<< 0);
77
+ } // note running off the end of msg is ok 'cos bitwise ops on NaN return 0
78
+ }
79
+ // add length (in bits) into final pair of 32-bit integers (big-endian) [§5.1.1]
80
+ // note: most significant word would be (len-1)*8 >>> 32, but since JS converts
81
+ // bitwise-op args to 32 bits, we need to simulate this by arithmetic operators
82
+ const lenHi = ((msg.length-1)*8) / Math.pow(2, 32);
83
+ const lenLo = ((msg.length-1)*8) >>> 0;
84
+ M[N-1][14] = Math.floor(lenHi);
85
+ M[N-1][15] = lenLo;
86
+
87
+ // HASH COMPUTATION [§6.2.2]
88
+
89
+ for (let i=0; i<N; i++) {
90
+ const W = new Array(64);
91
+
92
+ // 1 - prepare message schedule 'W'
93
+ for (let t=0; t<16; t++) W[t] = M[i][t];
94
+ for (let t=16; t<64; t++) {
95
+ W[t] = (Sha256.σ1(W[t-2]) + W[t-7] + Sha256.σ0(W[t-15]) + W[t-16]) >>> 0;
96
+ }
97
+
98
+ // 2 - initialise working variables a, b, c, d, e, f, g, h with previous hash value
99
+ let a = H[0], b = H[1], c = H[2], d = H[3], e = H[4], f = H[5], g = H[6], h = H[7];
100
+
101
+ // 3 - main loop (note '>>> 0' for 'addition modulo 2^32')
102
+ for (let t=0; t<64; t++) {
103
+ const T1 = h + Sha256.Σ1(e) + Sha256.Ch(e, f, g) + K[t] + W[t];
104
+ const T2 = Sha256.Σ0(a) + Sha256.Maj(a, b, c);
105
+ h = g;
106
+ g = f;
107
+ f = e;
108
+ e = (d + T1) >>> 0;
109
+ d = c;
110
+ c = b;
111
+ b = a;
112
+ a = (T1 + T2) >>> 0;
113
+ }
114
+
115
+ // 4 - compute the new intermediate hash value (note '>>> 0' for 'addition modulo 2^32')
116
+ H[0] = (H[0]+a) >>> 0;
117
+ H[1] = (H[1]+b) >>> 0;
118
+ H[2] = (H[2]+c) >>> 0;
119
+ H[3] = (H[3]+d) >>> 0;
120
+ H[4] = (H[4]+e) >>> 0;
121
+ H[5] = (H[5]+f) >>> 0;
122
+ H[6] = (H[6]+g) >>> 0;
123
+ H[7] = (H[7]+h) >>> 0;
124
+ }
125
+
126
+ // convert H0..H7 to hex strings (with leading zeros)
127
+ for (let h=0; h<H.length; h++) H[h] = ('00000000'+H[h].toString(16)).slice(-8);
128
+
129
+ // concatenate H0..H7, with separator if required
130
+ const separator = opt.outFormat=='hex-w' ? ' ' : '';
131
+
132
+ return H.join(separator);
133
+
134
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
135
+
136
+ function utf8Encode(str) {
137
+ try {
138
+ return new TextEncoder().encode(str, 'utf-8').reduce((prev, curr) => prev + String.fromCharCode(curr), '');
139
+ } catch (e) { // no TextEncoder available?
140
+ return unescape(encodeURIComponent(str)); // monsur.hossa.in/2012/07/20/utf-8-in-javascript.html
141
+ }
142
+ }
143
+
144
+ function hexBytesToString(hexStr) { // convert string of hex numbers to a string of chars (eg '616263' -> 'abc').
145
+ const str = hexStr.replace(' ', ''); // allow space-separated groups
146
+ return str=='' ? '' : str.match(/.{2}/g).map(byte => String.fromCharCode(parseInt(byte, 16))).join('');
147
+ }
148
+ }
149
+
150
+
151
+
152
+ /**
153
+ * Rotates right (circular right shift) value x by n positions [§3.2.4].
154
+ * @private
155
+ */
156
+ static ROTR(n, x) {
157
+ return (x >>> n) | (x << (32-n));
158
+ }
159
+
160
+
161
+ /**
162
+ * Logical functions [§4.1.2].
163
+ * @private
164
+ */
165
+ static Σ0(x) { return Sha256.ROTR(2, x) ^ Sha256.ROTR(13, x) ^ Sha256.ROTR(22, x); }
166
+ static Σ1(x) { return Sha256.ROTR(6, x) ^ Sha256.ROTR(11, x) ^ Sha256.ROTR(25, x); }
167
+ static σ0(x) { return Sha256.ROTR(7, x) ^ Sha256.ROTR(18, x) ^ (x>>>3); }
168
+ static σ1(x) { return Sha256.ROTR(17, x) ^ Sha256.ROTR(19, x) ^ (x>>>10); }
169
+ static Ch(x, y, z) { return (x & y) ^ (~x & z); } // 'choice'
170
+ static Maj(x, y, z) { return (x & y) ^ (x & z) ^ (y & z); } // 'majority'
171
+
172
+ }
173
+
174
+
175
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
176
+
177
+ if (typeof module != 'undefined' && module.exports) module.exports = Sha256; // ≡ export default Sha256
178
+
@@ -0,0 +1,27 @@
1
+ const chai = require("chai");
2
+ const path = require("path");
3
+ const wasm_tester = require("circom_tester").wasm;
4
+
5
+ const buildMimc7 = require("circomlibjs").buildMimc7;
6
+
7
+ describe("MiMC Circuit test", function () {
8
+ let circuit;
9
+ let mimc7;
10
+
11
+ this.timeout(100000);
12
+
13
+ before( async () => {
14
+ mimc7 = await buildMimc7();
15
+ circuit = await wasm_tester(path.join(__dirname, "circuits", "mimc_test.circom"));
16
+ });
17
+
18
+ it("Should check constrain", async () => {
19
+ const w = await circuit.calculateWitness({x_in: 1, k: 2}, true);
20
+
21
+ const res2 = mimc7.hash(1,2,91);
22
+
23
+ await circuit.assertOut(w, {out: mimc7.F.toObject(res2)});
24
+
25
+ await circuit.checkConstraints(w);
26
+ });
27
+ });
@@ -0,0 +1,47 @@
1
+ const path = require("path");
2
+ const wasm_tester = require("circom_tester").wasm;
3
+
4
+ const buildMimcSponge = require("circomlibjs").buildMimcSponge;
5
+
6
+
7
+ describe("MiMC Sponge Circuit test", function () {
8
+ let circuit;
9
+ let mimcSponge;
10
+ let F;
11
+
12
+ this.timeout(100000);
13
+
14
+ before( async () => {
15
+ mimcSponge = await buildMimcSponge();
16
+ F = mimcSponge.F;
17
+ });
18
+
19
+
20
+ it("Should check permutation", async () => {
21
+
22
+ circuit = await wasm_tester(path.join(__dirname, "circuits", "mimc_sponge_test.circom"));
23
+
24
+ const w = await circuit.calculateWitness({xL_in: 1, xR_in: 2, k: 3});
25
+
26
+ const out2 = mimcSponge.hash(1,2,3);
27
+
28
+ await circuit.assertOut(w, {xL_out: F.toObject(out2.xL), xR_out: F.toObject(out2.xR)});
29
+
30
+ await circuit.checkConstraints(w);
31
+
32
+ });
33
+
34
+ it("Should check hash", async () => {
35
+ circuit = await wasm_tester(path.join(__dirname, "circuits", "mimc_sponge_hash_test.circom"));
36
+
37
+ const w = await circuit.calculateWitness({ins: [1, 2], k: 0});
38
+
39
+ const out2 = mimcSponge.multiHash([1,2], 0, 3);
40
+
41
+ for (let i=0; i<out2.length; i++) out2[i] = F.toObject(out2[i]);
42
+
43
+ await circuit.assertOut(w, {outs: out2});
44
+
45
+ await circuit.checkConstraints(w);
46
+ });
47
+ });
@@ -0,0 +1,101 @@
1
+ const chai = require("chai");
2
+ const path = require("path");
3
+ const wasm_tester = require("circom_tester").wasm;
4
+ const Scalar = require("ffjavascript").Scalar;
5
+ const buildBabyjub = require("circomlibjs").buildBabyjub;
6
+
7
+ const assert = chai.assert;
8
+
9
+ describe("Montgomery test", function () {
10
+ let babyJub;
11
+ let Fr;
12
+ let circuitE2M;
13
+ let circuitM2E;
14
+ let circuitMAdd;
15
+ let circuitMDouble;
16
+
17
+ let g;
18
+
19
+ let mg, mg2, g2, g3, mg3;
20
+
21
+ this.timeout(100000);
22
+
23
+
24
+ before( async() => {
25
+ babyJub = await buildBabyjub();
26
+ Fr = babyJub.F;
27
+ g = [
28
+ Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
29
+ Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
30
+ ];
31
+
32
+ circuitE2M = await wasm_tester(path.join(__dirname, "circuits", "edwards2montgomery.circom"));
33
+ await circuitE2M.loadSymbols();
34
+ circuitM2E = await wasm_tester(path.join(__dirname, "circuits", "montgomery2edwards.circom"));
35
+ await circuitM2E.loadSymbols();
36
+ circuitMAdd = await wasm_tester(path.join(__dirname, "circuits", "montgomeryadd.circom"));
37
+ await circuitMAdd.loadSymbols();
38
+ circuitMDouble = await wasm_tester(path.join(__dirname, "circuits", "montgomerydouble.circom"));
39
+ await circuitMDouble.loadSymbols();
40
+ });
41
+
42
+ it("Convert Edwards to Montgomery and back again", async () => {
43
+ let w, xout, yout;
44
+
45
+ w = await circuitE2M.calculateWitness({ in: [Fr.toObject(g[0]), Fr.toObject(g[1])]}, true);
46
+
47
+ xout = w[circuitE2M.symbols["main.out[0]"].varIdx];
48
+ yout = w[circuitE2M.symbols["main.out[1]"].varIdx];
49
+
50
+ mg = [xout, yout];
51
+
52
+ w = await circuitM2E.calculateWitness({ in: [xout, yout]}, true);
53
+
54
+ xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
55
+ yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
56
+
57
+ assert(Fr.eq(Fr.e(xout), g[0]));
58
+ assert(Fr.eq(Fr.e(yout), g[1]));
59
+ });
60
+ it("Should double a point", async () => {
61
+ let w, xout, yout;
62
+
63
+ g2 = babyJub.addPoint(g,g);
64
+
65
+ w = await circuitMDouble.calculateWitness({ in: mg}, true);
66
+
67
+ xout = w[circuitE2M.symbols["main.out[0]"].varIdx];
68
+ yout = w[circuitE2M.symbols["main.out[1]"].varIdx];
69
+
70
+ mg2 = [xout, yout];
71
+
72
+ w = await circuitM2E.calculateWitness({ in: mg2}, true);
73
+
74
+ xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
75
+ yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
76
+
77
+
78
+ assert(Fr.eq(Fr.e(xout), g2[0]));
79
+ assert(Fr.eq(Fr.e(yout), g2[1]));
80
+ });
81
+ it("Should add a point", async () => {
82
+ let w, xout, yout;
83
+
84
+ g3 = babyJub.addPoint(g,g2);
85
+
86
+ w = await circuitMAdd.calculateWitness({ in1: mg, in2: mg2}, true);
87
+
88
+ xout = w[circuitMAdd.symbols["main.out[0]"].varIdx];
89
+ yout = w[circuitMAdd.symbols["main.out[1]"].varIdx];
90
+
91
+ mg3 = [xout, yout];
92
+
93
+ w = await circuitM2E.calculateWitness({ in: mg3}, true);
94
+
95
+ xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
96
+ yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
97
+
98
+ assert(Fr.eq(Fr.e(xout), g3[0]));
99
+ assert(Fr.eq(Fr.e(yout), g3[1]));
100
+ });
101
+ });
@@ -0,0 +1,101 @@
1
+ const path = require("path");
2
+ const wasm_tester = require("circom_tester").wasm;
3
+ const F1Field = require("ffjavascript").F1Field;
4
+ const Scalar = require("ffjavascript").Scalar;
5
+ exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
6
+ const Fr = new F1Field(exports.p);
7
+
8
+ describe("Mux4 test", function() {
9
+ this.timeout(100000);
10
+ it("Should create a constant multiplexer 4", async () => {
11
+
12
+ const circuit = await wasm_tester(path.join(__dirname, "circuits", "mux4_1.circom"));
13
+
14
+ const ct16 = [
15
+ Fr.e("123"),
16
+ Fr.e("456"),
17
+ Fr.e("789"),
18
+ Fr.e("012"),
19
+ Fr.e("111"),
20
+ Fr.e("222"),
21
+ Fr.e("333"),
22
+ Fr.e("4546"),
23
+ Fr.e("134523"),
24
+ Fr.e("44356"),
25
+ Fr.e("15623"),
26
+ Fr.e("4566"),
27
+ Fr.e("1223"),
28
+ Fr.e("4546"),
29
+ Fr.e("4256"),
30
+ Fr.e("4456")
31
+ ];
32
+
33
+ for (let i=0; i<16; i++) {
34
+ const w = await circuit.calculateWitness({ "selector": i }, true);
35
+
36
+ await circuit.checkConstraints(w);
37
+
38
+ await circuit.assertOut(w, {out: ct16[i]});
39
+ }
40
+ });
41
+
42
+ it("Should create a constant multiplexer 3", async () => {
43
+
44
+ const circuit = await wasm_tester(path.join(__dirname, "circuits", "mux3_1.circom"));
45
+
46
+ const ct8 = [
47
+ Fr.e("37"),
48
+ Fr.e("47"),
49
+ Fr.e("53"),
50
+ Fr.e("71"),
51
+ Fr.e("89"),
52
+ Fr.e("107"),
53
+ Fr.e("163"),
54
+ Fr.e("191")
55
+ ];
56
+
57
+ for (let i=0; i<8; i++) {
58
+ const w = await circuit.calculateWitness({ "selector": i }, true);
59
+
60
+ await circuit.checkConstraints(w);
61
+
62
+ await circuit.assertOut(w, {out: ct8[i]});
63
+ }
64
+ });
65
+ it("Should create a constant multiplexer 2", async () => {
66
+
67
+ const circuit = await wasm_tester(path.join(__dirname, "circuits", "mux2_1.circom"));
68
+
69
+ const ct4 = [
70
+ Fr.e("37"),
71
+ Fr.e("47"),
72
+ Fr.e("53"),
73
+ Fr.e("71"),
74
+ ];
75
+
76
+ for (let i=0; i<4; i++) {
77
+ const w = await circuit.calculateWitness({ "selector": i }, true);
78
+
79
+ await circuit.checkConstraints(w);
80
+
81
+ await circuit.assertOut(w, {out: ct4[i]});
82
+ }
83
+ });
84
+ it("Should create a constant multiplexer 1", async () => {
85
+
86
+ const circuit = await wasm_tester(path.join(__dirname, "circuits", "mux1_1.circom"));
87
+
88
+ const ct2 = [
89
+ Fr.e("37"),
90
+ Fr.e("47"),
91
+ ];
92
+
93
+ for (let i=0; i<2; i++) {
94
+ const w = await circuit.calculateWitness({ "selector": i }, true);
95
+
96
+ await circuit.checkConstraints(w);
97
+
98
+ await circuit.assertOut(w, {out: ct2[i]});
99
+ }
100
+ });
101
+ });
@@ -0,0 +1,83 @@
1
+ const chai = require("chai");
2
+ const path = require("path");
3
+
4
+ const Scalar = require("ffjavascript").Scalar;
5
+
6
+ const wasm_tester = require("circom_tester").wasm;
7
+
8
+ const buildBabyjub = require("circomlibjs").buildBabyjub;
9
+
10
+
11
+ describe("Double Pedersen test", function() {
12
+ let babyJub;
13
+ let Fr;
14
+ let PBASE;
15
+ let circuit;
16
+ this.timeout(100000);
17
+ before( async() => {
18
+ babyJub = await buildBabyjub();
19
+ Fr = babyJub.F;
20
+ PBASE =
21
+ [
22
+ [Fr.e("10457101036533406547632367118273992217979173478358440826365724437999023779287"),Fr.e("19824078218392094440610104313265183977899662750282163392862422243483260492317")],
23
+ [Fr.e("2671756056509184035029146175565761955751135805354291559563293617232983272177"),Fr.e("2663205510731142763556352975002641716101654201788071096152948830924149045094")],
24
+ [Fr.e("5802099305472655231388284418920769829666717045250560929368476121199858275951"),Fr.e("5980429700218124965372158798884772646841287887664001482443826541541529227896")],
25
+ [Fr.e("7107336197374528537877327281242680114152313102022415488494307685842428166594"),Fr.e("2857869773864086953506483169737724679646433914307247183624878062391496185654")],
26
+ [Fr.e("20265828622013100949498132415626198973119240347465898028410217039057588424236"),Fr.e("1160461593266035632937973507065134938065359936056410650153315956301179689506")]
27
+ ];
28
+ circuit = await wasm_tester(path.join(__dirname, "circuits", "pedersen_test.circom"));
29
+
30
+ });
31
+
32
+ it("Should pedersen at zero", async () => {
33
+
34
+ let w;
35
+
36
+ w = await circuit.calculateWitness({ in: ["0", "0"]}, true);
37
+
38
+ await circuit.assertOut(w, {out: [0,1]});
39
+
40
+ });
41
+ it("Should pedersen at one first generator", async () => {
42
+ let w;
43
+
44
+ w = await circuit.calculateWitness({ in: ["1", "0"]}, true);
45
+
46
+ await circuit.assertOut(w, {out: [Fr.toObject(PBASE[0][0]), Fr.toObject(PBASE[0][1])]});
47
+
48
+ });
49
+ it("Should pedersen at one second generator", async () => {
50
+ let w;
51
+
52
+ w = await circuit.calculateWitness({ in: ["0", "1"]}, true);
53
+
54
+ await circuit.assertOut(w, {out: [Fr.toObject(PBASE[1][0]), Fr.toObject(PBASE[1][1])]});
55
+
56
+ });
57
+ it("Should pedersen at mixed generators", async () => {
58
+ let w;
59
+ w = await circuit.calculateWitness({ in: ["3", "7"]}, true);
60
+
61
+ const r = babyJub.addPoint(
62
+ babyJub.mulPointEscalar(PBASE[0], 3),
63
+ babyJub.mulPointEscalar(PBASE[1], 7)
64
+ );
65
+
66
+ await circuit.assertOut(w, {out: [Fr.toObject(r[0]), Fr.toObject(r[1])]});
67
+
68
+ });
69
+ it("Should pedersen all ones", async () => {
70
+ let w;
71
+
72
+ const allOnes = Scalar.sub(Scalar.shl(Scalar.e(1), 250), Scalar.e(1));
73
+ w = await circuit.calculateWitness({ in: [allOnes, allOnes]}, true);
74
+
75
+
76
+ const r2 = babyJub.addPoint(
77
+ babyJub.mulPointEscalar(PBASE[0], allOnes),
78
+ babyJub.mulPointEscalar(PBASE[1], allOnes)
79
+ );
80
+
81
+ await circuit.assertOut(w, {out: [Fr.toObject(r2[0]), Fr.toObject(r2[1])]});
82
+ });
83
+ });