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