@auditable/privacy-pool-zk-sdk 0.0.2-rc.6

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.
Binary file
Binary file
@@ -0,0 +1,381 @@
1
+ module.exports = async function builder(code, options) {
2
+
3
+ options = options || {};
4
+
5
+ let wasmModule;
6
+ try {
7
+ wasmModule = await WebAssembly.compile(code);
8
+ } catch (err) {
9
+ console.log(err);
10
+ console.log("\nTry to run circom --c in order to generate c++ code instead\n");
11
+ throw new Error(err);
12
+ }
13
+
14
+ let wc;
15
+
16
+ let errStr = "";
17
+ let msgStr = "";
18
+
19
+ const instance = await WebAssembly.instantiate(wasmModule, {
20
+ runtime: {
21
+ exceptionHandler : function(code) {
22
+ let err;
23
+ if (code == 1) {
24
+ err = "Signal not found.\n";
25
+ } else if (code == 2) {
26
+ err = "Too many signals set.\n";
27
+ } else if (code == 3) {
28
+ err = "Signal already set.\n";
29
+ } else if (code == 4) {
30
+ err = "Assert Failed.\n";
31
+ } else if (code == 5) {
32
+ err = "Not enough memory.\n";
33
+ } else if (code == 6) {
34
+ err = "Input signal array access exceeds the size.\n";
35
+ } else {
36
+ err = "Unknown error.\n";
37
+ }
38
+ throw new Error(err + errStr);
39
+ },
40
+ printErrorMessage : function() {
41
+ errStr += getMessage() + "\n";
42
+ // console.error(getMessage());
43
+ },
44
+ writeBufferMessage : function() {
45
+ const msg = getMessage();
46
+ // Any calls to `log()` will always end with a `\n`, so that's when we print and reset
47
+ if (msg === "\n") {
48
+ console.log(msgStr);
49
+ msgStr = "";
50
+ } else {
51
+ // If we've buffered other content, put a space in between the items
52
+ if (msgStr !== "") {
53
+ msgStr += " "
54
+ }
55
+ // Then append the message to the message we are creating
56
+ msgStr += msg;
57
+ }
58
+ },
59
+ showSharedRWMemory : function() {
60
+ printSharedRWMemory ();
61
+ }
62
+
63
+ }
64
+ });
65
+
66
+ const sanityCheck =
67
+ options
68
+ // options &&
69
+ // (
70
+ // options.sanityCheck ||
71
+ // options.logGetSignal ||
72
+ // options.logSetSignal ||
73
+ // options.logStartComponent ||
74
+ // options.logFinishComponent
75
+ // );
76
+
77
+
78
+ wc = new WitnessCalculator(instance, sanityCheck);
79
+ return wc;
80
+
81
+ function getMessage() {
82
+ var message = "";
83
+ var c = instance.exports.getMessageChar();
84
+ while ( c != 0 ) {
85
+ message += String.fromCharCode(c);
86
+ c = instance.exports.getMessageChar();
87
+ }
88
+ return message;
89
+ }
90
+
91
+ function printSharedRWMemory () {
92
+ const shared_rw_memory_size = instance.exports.getFieldNumLen32();
93
+ const arr = new Uint32Array(shared_rw_memory_size);
94
+ for (let j=0; j<shared_rw_memory_size; j++) {
95
+ arr[shared_rw_memory_size-1-j] = instance.exports.readSharedRWMemory(j);
96
+ }
97
+
98
+ // If we've buffered other content, put a space in between the items
99
+ if (msgStr !== "") {
100
+ msgStr += " "
101
+ }
102
+ // Then append the value to the message we are creating
103
+ msgStr += (fromArray32(arr).toString());
104
+ }
105
+
106
+ };
107
+
108
+ class WitnessCalculator {
109
+ constructor(instance, sanityCheck) {
110
+ this.instance = instance;
111
+
112
+ this.version = this.instance.exports.getVersion();
113
+ this.n32 = this.instance.exports.getFieldNumLen32();
114
+
115
+ this.instance.exports.getRawPrime();
116
+ const arr = new Uint32Array(this.n32);
117
+ for (let i=0; i<this.n32; i++) {
118
+ arr[this.n32-1-i] = this.instance.exports.readSharedRWMemory(i);
119
+ }
120
+ this.prime = fromArray32(arr);
121
+
122
+ this.witnessSize = this.instance.exports.getWitnessSize();
123
+
124
+ this.sanityCheck = sanityCheck;
125
+ }
126
+
127
+ circom_version() {
128
+ return this.instance.exports.getVersion();
129
+ }
130
+
131
+ async _doCalculateWitness(input_orig, sanityCheck) {
132
+ //input is assumed to be a map from signals to arrays of bigints
133
+ this.instance.exports.init((this.sanityCheck || sanityCheck) ? 1 : 0);
134
+ let prefix = "";
135
+ var input = new Object();
136
+ //console.log("Input: ", input_orig);
137
+ qualify_input(prefix,input_orig,input);
138
+ //console.log("Input after: ",input);
139
+ const keys = Object.keys(input);
140
+ var input_counter = 0;
141
+ keys.forEach( (k) => {
142
+ const h = fnvHash(k);
143
+ const hMSB = parseInt(h.slice(0,8), 16);
144
+ const hLSB = parseInt(h.slice(8,16), 16);
145
+ const fArr = flatArray(input[k]);
146
+ let signalSize = this.instance.exports.getInputSignalSize(hMSB, hLSB);
147
+ if (signalSize < 0){
148
+ throw new Error(`Signal ${k} not found\n`);
149
+ }
150
+ if (fArr.length < signalSize) {
151
+ throw new Error(`Not enough values for input signal ${k}\n`);
152
+ }
153
+ if (fArr.length > signalSize) {
154
+ throw new Error(`Too many values for input signal ${k}\n`);
155
+ }
156
+ for (let i=0; i<fArr.length; i++) {
157
+ const arrFr = toArray32(normalize(fArr[i],this.prime),this.n32)
158
+ for (let j=0; j<this.n32; j++) {
159
+ this.instance.exports.writeSharedRWMemory(j,arrFr[this.n32-1-j]);
160
+ }
161
+ try {
162
+ this.instance.exports.setInputSignal(hMSB, hLSB,i);
163
+ input_counter++;
164
+ } catch (err) {
165
+ // console.log(`After adding signal ${i} of ${k}`)
166
+ throw new Error(err);
167
+ }
168
+ }
169
+
170
+ });
171
+ if (input_counter < this.instance.exports.getInputSize()) {
172
+ throw new Error(`Not all inputs have been set. Only ${input_counter} out of ${this.instance.exports.getInputSize()}`);
173
+ }
174
+ }
175
+
176
+ async calculateWitness(input, sanityCheck) {
177
+
178
+ const w = [];
179
+ await this._doCalculateWitness(input, sanityCheck);
180
+
181
+ for (let i=0; i<this.witnessSize; i++) {
182
+ this.instance.exports.getWitness(i);
183
+ const arr = new Uint32Array(this.n32);
184
+ for (let j=0; j<this.n32; j++) {
185
+ arr[this.n32-1-j] = this.instance.exports.readSharedRWMemory(j);
186
+ }
187
+ w.push(fromArray32(arr));
188
+ }
189
+
190
+ return w;
191
+ }
192
+
193
+
194
+ async calculateBinWitness(input, sanityCheck) {
195
+
196
+ const buff32 = new Uint32Array(this.witnessSize*this.n32);
197
+ const buff = new Uint8Array( buff32.buffer);
198
+ await this._doCalculateWitness(input, sanityCheck);
199
+
200
+ for (let i=0; i<this.witnessSize; i++) {
201
+ this.instance.exports.getWitness(i);
202
+ const pos = i*this.n32;
203
+ for (let j=0; j<this.n32; j++) {
204
+ buff32[pos+j] = this.instance.exports.readSharedRWMemory(j);
205
+ }
206
+ }
207
+
208
+ return buff;
209
+ }
210
+
211
+
212
+ async calculateWTNSBin(input, sanityCheck) {
213
+
214
+ const buff32 = new Uint32Array(this.witnessSize*this.n32+this.n32+11);
215
+ const buff = new Uint8Array( buff32.buffer);
216
+ await this._doCalculateWitness(input, sanityCheck);
217
+
218
+ //"wtns"
219
+ buff[0] = "w".charCodeAt(0)
220
+ buff[1] = "t".charCodeAt(0)
221
+ buff[2] = "n".charCodeAt(0)
222
+ buff[3] = "s".charCodeAt(0)
223
+
224
+ //version 2
225
+ buff32[1] = 2;
226
+
227
+ //number of sections: 2
228
+ buff32[2] = 2;
229
+
230
+ //id section 1
231
+ buff32[3] = 1;
232
+
233
+ const n8 = this.n32*4;
234
+ //id section 1 length in 64bytes
235
+ const idSection1length = 8 + n8;
236
+ const idSection1lengthHex = idSection1length.toString(16);
237
+ buff32[4] = parseInt(idSection1lengthHex.slice(0,8), 16);
238
+ buff32[5] = parseInt(idSection1lengthHex.slice(8,16), 16);
239
+
240
+ //this.n32
241
+ buff32[6] = n8;
242
+
243
+ //prime number
244
+ this.instance.exports.getRawPrime();
245
+
246
+ var pos = 7;
247
+ for (let j=0; j<this.n32; j++) {
248
+ buff32[pos+j] = this.instance.exports.readSharedRWMemory(j);
249
+ }
250
+ pos += this.n32;
251
+
252
+ // witness size
253
+ buff32[pos] = this.witnessSize;
254
+ pos++;
255
+
256
+ //id section 2
257
+ buff32[pos] = 2;
258
+ pos++;
259
+
260
+ // section 2 length
261
+ const idSection2length = n8*this.witnessSize;
262
+ const idSection2lengthHex = idSection2length.toString(16);
263
+ buff32[pos] = parseInt(idSection2lengthHex.slice(0,8), 16);
264
+ buff32[pos+1] = parseInt(idSection2lengthHex.slice(8,16), 16);
265
+
266
+ pos += 2;
267
+ for (let i=0; i<this.witnessSize; i++) {
268
+ this.instance.exports.getWitness(i);
269
+ for (let j=0; j<this.n32; j++) {
270
+ buff32[pos+j] = this.instance.exports.readSharedRWMemory(j);
271
+ }
272
+ pos += this.n32;
273
+ }
274
+
275
+ return buff;
276
+ }
277
+
278
+ }
279
+
280
+
281
+ function qualify_input_list(prefix,input,input1){
282
+ if (Array.isArray(input)) {
283
+ for (let i = 0; i<input.length; i++) {
284
+ let new_prefix = prefix + "[" + i + "]";
285
+ qualify_input_list(new_prefix,input[i],input1);
286
+ }
287
+ } else {
288
+ qualify_input(prefix,input,input1);
289
+ }
290
+ }
291
+
292
+ function qualify_input(prefix,input,input1) {
293
+ if (Array.isArray(input)) {
294
+ a = flatArray(input);
295
+ if (a.length > 0) {
296
+ let t = typeof a[0];
297
+ for (let i = 1; i<a.length; i++) {
298
+ if (typeof a[i] != t){
299
+ throw new Error(`Types are not the same in the key ${prefix}`);
300
+ }
301
+ }
302
+ if (t == "object") {
303
+ qualify_input_list(prefix,input,input1);
304
+ } else {
305
+ input1[prefix] = input;
306
+ }
307
+ } else {
308
+ input1[prefix] = input;
309
+ }
310
+ } else if (typeof input == "object") {
311
+ const keys = Object.keys(input);
312
+ keys.forEach( (k) => {
313
+ let new_prefix = prefix == ""? k : prefix + "." + k;
314
+ qualify_input(new_prefix,input[k],input1);
315
+ });
316
+ } else {
317
+ input1[prefix] = input;
318
+ }
319
+ }
320
+
321
+ function toArray32(rem,size) {
322
+ const res = []; //new Uint32Array(size); //has no unshift
323
+ const radix = BigInt(0x100000000);
324
+ while (rem) {
325
+ res.unshift( Number(rem % radix));
326
+ rem = rem / radix;
327
+ }
328
+ if (size) {
329
+ var i = size - res.length;
330
+ while (i>0) {
331
+ res.unshift(0);
332
+ i--;
333
+ }
334
+ }
335
+ return res;
336
+ }
337
+
338
+ function fromArray32(arr) { //returns a BigInt
339
+ var res = BigInt(0);
340
+ const radix = BigInt(0x100000000);
341
+ for (let i = 0; i<arr.length; i++) {
342
+ res = res*radix + BigInt(arr[i]);
343
+ }
344
+ return res;
345
+ }
346
+
347
+ function flatArray(a) {
348
+ var res = [];
349
+ fillArray(res, a);
350
+ return res;
351
+
352
+ function fillArray(res, a) {
353
+ if (Array.isArray(a)) {
354
+ for (let i=0; i<a.length; i++) {
355
+ fillArray(res, a[i]);
356
+ }
357
+ } else {
358
+ res.push(a);
359
+ }
360
+ }
361
+ }
362
+
363
+ function normalize(n, prime) {
364
+ let res = BigInt(n) % prime
365
+ if (res < 0) res += prime
366
+ return res
367
+ }
368
+
369
+ function fnvHash(str) {
370
+ const uint64_max = BigInt(2) ** BigInt(64);
371
+ let hash = BigInt("0xCBF29CE484222325");
372
+ for (var i = 0; i < str.length; i++) {
373
+ hash ^= BigInt(str[i].charCodeAt());
374
+ hash *= BigInt(0x100000001B3);
375
+ hash %= uint64_max;
376
+ }
377
+ let shash = hash.toString(16);
378
+ let n = 16 - shash.length;
379
+ shash = '0'.repeat(n).concat(shash);
380
+ return shash;
381
+ }
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};