@waku/rln 0.0.2 → 0.0.4

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.
@@ -0,0 +1,335 @@
1
+ // File generated with https://github.com/iden3/circom
2
+ // following the instructions from:
3
+ // https://github.com/vacp2p/zerokit/tree/master/rln#compiling-circuits
4
+
5
+ export async function builder(code, options) {
6
+
7
+ options = options || {};
8
+
9
+ let wasmModule;
10
+ try {
11
+ wasmModule = await WebAssembly.compile(code);
12
+ } catch (err) {
13
+ console.log(err);
14
+ console.log("\nTry to run circom --c in order to generate c++ code instead\n");
15
+ throw new Error(err);
16
+ }
17
+
18
+ let wc;
19
+
20
+ let errStr = "";
21
+ let msgStr = "";
22
+
23
+ const instance = await WebAssembly.instantiate(wasmModule, {
24
+ runtime: {
25
+ exceptionHandler : function(code) {
26
+ let err;
27
+ if (code == 1) {
28
+ err = "Signal not found.\n";
29
+ } else if (code == 2) {
30
+ err = "Too many signals set.\n";
31
+ } else if (code == 3) {
32
+ err = "Signal already set.\n";
33
+ } else if (code == 4) {
34
+ err = "Assert Failed.\n";
35
+ } else if (code == 5) {
36
+ err = "Not enough memory.\n";
37
+ } else if (code == 6) {
38
+ err = "Input signal array access exceeds the size.\n";
39
+ } else {
40
+ err = "Unknown error.\n";
41
+ }
42
+ throw new Error(err + errStr);
43
+ },
44
+ printErrorMessage : function() {
45
+ errStr += getMessage() + "\n";
46
+ // console.error(getMessage());
47
+ },
48
+ writeBufferMessage : function() {
49
+ const msg = getMessage();
50
+ // Any calls to `log()` will always end with a `\n`, so that's when we print and reset
51
+ if (msg === "\n") {
52
+ console.log(msgStr);
53
+ msgStr = "";
54
+ } else {
55
+ // If we've buffered other content, put a space in between the items
56
+ if (msgStr !== "") {
57
+ msgStr += " "
58
+ }
59
+ // Then append the message to the message we are creating
60
+ msgStr += msg;
61
+ }
62
+ },
63
+ showSharedRWMemory : function() {
64
+ printSharedRWMemory ();
65
+ }
66
+
67
+ }
68
+ });
69
+
70
+ const sanityCheck =
71
+ options
72
+ // options &&
73
+ // (
74
+ // options.sanityCheck ||
75
+ // options.logGetSignal ||
76
+ // options.logSetSignal ||
77
+ // options.logStartComponent ||
78
+ // options.logFinishComponent
79
+ // );
80
+
81
+
82
+ wc = new WitnessCalculator(instance, sanityCheck);
83
+ return wc;
84
+
85
+ function getMessage() {
86
+ var message = "";
87
+ var c = instance.exports.getMessageChar();
88
+ while ( c != 0 ) {
89
+ message += String.fromCharCode(c);
90
+ c = instance.exports.getMessageChar();
91
+ }
92
+ return message;
93
+ }
94
+
95
+ function printSharedRWMemory () {
96
+ const shared_rw_memory_size = instance.exports.getFieldNumLen32();
97
+ const arr = new Uint32Array(shared_rw_memory_size);
98
+ for (let j=0; j<shared_rw_memory_size; j++) {
99
+ arr[shared_rw_memory_size-1-j] = instance.exports.readSharedRWMemory(j);
100
+ }
101
+
102
+ // If we've buffered other content, put a space in between the items
103
+ if (msgStr !== "") {
104
+ msgStr += " "
105
+ }
106
+ // Then append the value to the message we are creating
107
+ msgStr += (fromArray32(arr).toString());
108
+ }
109
+
110
+ };
111
+
112
+ class WitnessCalculator {
113
+ constructor(instance, sanityCheck) {
114
+ this.instance = instance;
115
+
116
+ this.version = this.instance.exports.getVersion();
117
+ this.n32 = this.instance.exports.getFieldNumLen32();
118
+
119
+ this.instance.exports.getRawPrime();
120
+ const arr = new Uint32Array(this.n32);
121
+ for (let i=0; i<this.n32; i++) {
122
+ arr[this.n32-1-i] = this.instance.exports.readSharedRWMemory(i);
123
+ }
124
+ this.prime = fromArray32(arr);
125
+
126
+ this.witnessSize = this.instance.exports.getWitnessSize();
127
+
128
+ this.sanityCheck = sanityCheck;
129
+ }
130
+
131
+ circom_version() {
132
+ return this.instance.exports.getVersion();
133
+ }
134
+
135
+ async _doCalculateWitness(input, sanityCheck) {
136
+ //input is assumed to be a map from signals to arrays of bigints
137
+ this.instance.exports.init((this.sanityCheck || sanityCheck) ? 1 : 0);
138
+ const keys = Object.keys(input);
139
+ var input_counter = 0;
140
+ keys.forEach( (k) => {
141
+ const h = fnvHash(k);
142
+ const hMSB = parseInt(h.slice(0,8), 16);
143
+ const hLSB = parseInt(h.slice(8,16), 16);
144
+ const fArr = flatArray(input[k]);
145
+ let signalSize = this.instance.exports.getInputSignalSize(hMSB, hLSB);
146
+ if (signalSize < 0){
147
+ throw new Error(`Signal ${k} not found\n`);
148
+ }
149
+ if (fArr.length < signalSize) {
150
+ throw new Error(`Not enough values for input signal ${k}\n`);
151
+ }
152
+ if (fArr.length > signalSize) {
153
+ throw new Error(`Too many values for input signal ${k}\n`);
154
+ }
155
+ for (let i=0; i<fArr.length; i++) {
156
+ const arrFr = toArray32(BigInt(fArr[i])%this.prime,this.n32)
157
+ for (let j=0; j<this.n32; j++) {
158
+ this.instance.exports.writeSharedRWMemory(j,arrFr[this.n32-1-j]);
159
+ }
160
+ try {
161
+ this.instance.exports.setInputSignal(hMSB, hLSB,i);
162
+ input_counter++;
163
+ } catch (err) {
164
+ // console.log(`After adding signal ${i} of ${k}`)
165
+ throw new Error(err);
166
+ }
167
+ }
168
+
169
+ });
170
+ if (input_counter < this.instance.exports.getInputSize()) {
171
+ throw new Error(`Not all inputs have been set. Only ${input_counter} out of ${this.instance.exports.getInputSize()}`);
172
+ }
173
+ }
174
+
175
+ async calculateWitness(input, sanityCheck) {
176
+
177
+ const w = [];
178
+
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 toArray32(rem,size) {
282
+ const res = []; //new Uint32Array(size); //has no unshift
283
+ const radix = BigInt(0x100000000);
284
+ while (rem) {
285
+ res.unshift( Number(rem % radix));
286
+ rem = rem / radix;
287
+ }
288
+ if (size) {
289
+ var i = size - res.length;
290
+ while (i>0) {
291
+ res.unshift(0);
292
+ i--;
293
+ }
294
+ }
295
+ return res;
296
+ }
297
+
298
+ function fromArray32(arr) { //returns a BigInt
299
+ var res = BigInt(0);
300
+ const radix = BigInt(0x100000000);
301
+ for (let i = 0; i<arr.length; i++) {
302
+ res = res*radix + BigInt(arr[i]);
303
+ }
304
+ return res;
305
+ }
306
+
307
+ function flatArray(a) {
308
+ var res = [];
309
+ fillArray(res, a);
310
+ return res;
311
+
312
+ function fillArray(res, a) {
313
+ if (Array.isArray(a)) {
314
+ for (let i=0; i<a.length; i++) {
315
+ fillArray(res, a[i]);
316
+ }
317
+ } else {
318
+ res.push(a);
319
+ }
320
+ }
321
+ }
322
+
323
+ function fnvHash(str) {
324
+ const uint64_max = BigInt(2) ** BigInt(64);
325
+ let hash = BigInt("0xCBF29CE484222325");
326
+ for (var i = 0; i < str.length; i++) {
327
+ hash ^= BigInt(str[i].charCodeAt());
328
+ hash *= BigInt(0x100000001B3);
329
+ hash %= uint64_max;
330
+ }
331
+ let shash = hash.toString(16);
332
+ let n = 16 - shash.length;
333
+ shash = '0'.repeat(n).concat(shash);
334
+ return shash;
335
+ }