@waku/rln 0.1.6-b133417.0 → 0.1.6-b4748fd.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 (46) hide show
  1. package/bundle/packages/rln/dist/contract/rln_base_contract.js +32 -8
  2. package/bundle/packages/rln/dist/contract/rln_contract.js +2 -2
  3. package/bundle/packages/rln/dist/credentials_manager.js +11 -17
  4. package/bundle/packages/rln/dist/identity.js +0 -5
  5. package/bundle/packages/rln/dist/keystore/keystore.js +11 -18
  6. package/bundle/packages/rln/dist/proof.js +2 -2
  7. package/bundle/packages/rln/dist/utils/bytes.js +61 -108
  8. package/bundle/packages/rln/dist/utils/hash.js +3 -3
  9. package/bundle/packages/rln/dist/zerokit.js +17 -17
  10. package/dist/.tsbuildinfo +1 -1
  11. package/dist/contract/rln_base_contract.d.ts +6 -0
  12. package/dist/contract/rln_base_contract.js +32 -8
  13. package/dist/contract/rln_base_contract.js.map +1 -1
  14. package/dist/contract/rln_contract.js +2 -2
  15. package/dist/contract/rln_contract.js.map +1 -1
  16. package/dist/credentials_manager.d.ts +0 -4
  17. package/dist/credentials_manager.js +11 -18
  18. package/dist/credentials_manager.js.map +1 -1
  19. package/dist/identity.d.ts +0 -1
  20. package/dist/identity.js +0 -4
  21. package/dist/identity.js.map +1 -1
  22. package/dist/keystore/keystore.js +11 -18
  23. package/dist/keystore/keystore.js.map +1 -1
  24. package/dist/proof.js +2 -2
  25. package/dist/proof.js.map +1 -1
  26. package/dist/utils/bytes.d.ts +16 -42
  27. package/dist/utils/bytes.js +60 -107
  28. package/dist/utils/bytes.js.map +1 -1
  29. package/dist/utils/hash.js +5 -5
  30. package/dist/utils/hash.js.map +1 -1
  31. package/dist/utils/index.d.ts +1 -1
  32. package/dist/utils/index.js +1 -1
  33. package/dist/utils/index.js.map +1 -1
  34. package/dist/zerokit.js +17 -17
  35. package/dist/zerokit.js.map +1 -1
  36. package/package.json +1 -1
  37. package/src/contract/rln_base_contract.ts +55 -17
  38. package/src/contract/rln_contract.ts +2 -5
  39. package/src/credentials_manager.ts +15 -31
  40. package/src/identity.ts +1 -7
  41. package/src/keystore/keystore.ts +11 -25
  42. package/src/proof.ts +2 -2
  43. package/src/utils/bytes.ts +67 -117
  44. package/src/utils/hash.ts +5 -15
  45. package/src/utils/index.ts +6 -1
  46. package/src/zerokit.ts +22 -30
@@ -14,8 +14,6 @@ import {
14
14
  import _ from "lodash";
15
15
  import { v4 as uuidV4 } from "uuid";
16
16
 
17
- import { BytesUtils } from "../utils/bytes.js";
18
-
19
17
  import { decryptEipKeystore, keccak256Checksum } from "./cipher.js";
20
18
  import { isCredentialValid, isKeystoreValid } from "./schema_validator.js";
21
19
  import type {
@@ -250,7 +248,6 @@ export class Keystore {
250
248
  const str = bytesToUtf8(bytes);
251
249
  const obj = JSON.parse(str);
252
250
 
253
- // Little Endian
254
251
  const idCommitmentLE = Keystore.fromArraylikeToBytes(
255
252
  _.get(obj, "identityCredential.idCommitment", [])
256
253
  );
@@ -264,21 +261,12 @@ export class Keystore {
264
261
  _.get(obj, "identityCredential.idSecretHash", [])
265
262
  );
266
263
 
267
- // Big Endian
268
- const idCommitmentBE = BytesUtils.switchEndianness(idCommitmentLE);
269
- const idTrapdoorBE = BytesUtils.switchEndianness(idTrapdoorLE);
270
- const idNullifierBE = BytesUtils.switchEndianness(idNullifierLE);
271
- const idSecretHashBE = BytesUtils.switchEndianness(idSecretHashLE);
272
- const idCommitmentBigInt =
273
- BytesUtils.buildBigIntFromUint8ArrayBE(idCommitmentBE);
274
-
275
264
  return {
276
265
  identity: {
277
- IDCommitment: idCommitmentBE,
278
- IDTrapdoor: idTrapdoorBE,
279
- IDNullifier: idNullifierBE,
280
- IDSecretHash: idSecretHashBE,
281
- IDCommitmentBigInt: idCommitmentBigInt
266
+ IDCommitment: idCommitmentLE,
267
+ IDTrapdoor: idTrapdoorLE,
268
+ IDNullifier: idNullifierLE,
269
+ IDSecretHash: idSecretHashLE
282
270
  },
283
271
  membership: {
284
272
  treeIndex: _.get(obj, "treeIndex"),
@@ -329,24 +317,22 @@ export class Keystore {
329
317
 
330
318
  // follows nwaku implementation
331
319
  // https://github.com/waku-org/nwaku/blob/f05528d4be3d3c876a8b07f9bb7dfaae8aa8ec6e/waku/waku_keystore/protocol_types.nim#L98
332
- // IdentityCredential is stored in Big Endian format => switch to Little Endian
333
320
  private static fromIdentityToBytes(options: KeystoreEntity): Uint8Array {
321
+ function toLittleEndian(bytes: Uint8Array): Uint8Array {
322
+ return new Uint8Array(bytes).reverse();
323
+ }
334
324
  return utf8ToBytes(
335
325
  JSON.stringify({
336
326
  treeIndex: options.membership.treeIndex,
337
327
  identityCredential: {
338
328
  idCommitment: Array.from(
339
- BytesUtils.switchEndianness(options.identity.IDCommitment)
340
- ),
341
- idNullifier: Array.from(
342
- BytesUtils.switchEndianness(options.identity.IDNullifier)
329
+ toLittleEndian(options.identity.IDCommitment)
343
330
  ),
331
+ idNullifier: Array.from(toLittleEndian(options.identity.IDNullifier)),
344
332
  idSecretHash: Array.from(
345
- BytesUtils.switchEndianness(options.identity.IDSecretHash)
333
+ toLittleEndian(options.identity.IDSecretHash)
346
334
  ),
347
- idTrapdoor: Array.from(
348
- BytesUtils.switchEndianness(options.identity.IDTrapdoor)
349
- )
335
+ idTrapdoor: Array.from(toLittleEndian(options.identity.IDTrapdoor))
350
336
  },
351
337
  membershipContract: {
352
338
  chainId: options.membership.chainId,
package/src/proof.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { IRateLimitProof } from "@waku/interfaces";
2
2
 
3
- import { BytesUtils, poseidonHash } from "./utils/index.js";
3
+ import { concatenate, poseidonHash } from "./utils/index.js";
4
4
 
5
5
  const proofOffset = 128;
6
6
  const rootOffset = proofOffset + 32;
@@ -57,7 +57,7 @@ export class Proof implements IRateLimitProof {
57
57
  }
58
58
 
59
59
  export function proofToBytes(p: IRateLimitProof): Uint8Array {
60
- return BytesUtils.concatenate(
60
+ return concatenate(
61
61
  p.proof,
62
62
  p.merkleRoot,
63
63
  p.epoch,
@@ -1,130 +1,80 @@
1
- export class BytesUtils {
2
- /**
3
- * Switches endianness of a byte array
4
- */
5
- public static switchEndianness(bytes: Uint8Array): Uint8Array {
6
- return new Uint8Array(bytes.reverse());
1
+ /**
2
+ * Concatenate Uint8Arrays
3
+ * @param input
4
+ * @returns concatenation of all Uint8Array received as input
5
+ */
6
+ export function concatenate(...input: Uint8Array[]): Uint8Array {
7
+ let totalLength = 0;
8
+ for (const arr of input) {
9
+ totalLength += arr.length;
7
10
  }
8
-
9
- /**
10
- * Builds a BigInt from a big-endian Uint8Array
11
- * @param bytes The big-endian bytes to convert
12
- * @returns The resulting BigInt in big-endian format
13
- */
14
- public static buildBigIntFromUint8ArrayBE(bytes: Uint8Array): bigint {
15
- let result = 0n;
16
- for (let i = 0; i < bytes.length; i++) {
17
- result = (result << 8n) + BigInt(bytes[i]);
18
- }
19
- return result;
11
+ const result = new Uint8Array(totalLength);
12
+ let offset = 0;
13
+ for (const arr of input) {
14
+ result.set(arr, offset);
15
+ offset += arr.length;
20
16
  }
17
+ return result;
18
+ }
21
19
 
22
- /**
23
- * Switches endianness of a bigint value
24
- * @param value The bigint value to switch endianness for
25
- * @returns The bigint value with reversed endianness
26
- */
27
- public static switchEndiannessBigInt(value: bigint): bigint {
28
- // Convert bigint to byte array
29
- const bytes = [];
30
- let tempValue = value;
31
- while (tempValue > 0n) {
32
- bytes.push(Number(tempValue & 0xffn));
33
- tempValue >>= 8n;
34
- }
20
+ export function switchEndianness(bytes: Uint8Array): Uint8Array {
21
+ return new Uint8Array(bytes.reverse());
22
+ }
35
23
 
36
- // Reverse bytes and convert back to bigint
37
- return bytes
38
- .reverse()
39
- .reduce((acc, byte) => (acc << 8n) + BigInt(byte), 0n);
40
- }
24
+ export function buildBigIntFromUint8ArrayBE(bytes: Uint8Array): bigint {
25
+ // Interpret bytes as big-endian
26
+ return bytes.reduce((acc, byte) => (acc << 8n) + BigInt(byte), 0n);
27
+ }
41
28
 
42
- /**
43
- * Converts a big-endian bigint to a 32-byte big-endian Uint8Array
44
- * @param value The big-endian bigint to convert
45
- * @returns A 32-byte big-endian Uint8Array
46
- */
47
- public static bigIntToUint8Array32BE(value: bigint): Uint8Array {
48
- const bytes = new Uint8Array(32);
49
- for (let i = 31; i >= 0; i--) {
50
- bytes[i] = Number(value & 0xffn);
51
- value >>= 8n;
52
- }
53
- return bytes;
29
+ export function writeUIntLE(
30
+ buf: Uint8Array,
31
+ value: number,
32
+ offset: number,
33
+ byteLength: number,
34
+ noAssert?: boolean
35
+ ): Uint8Array {
36
+ value = +value;
37
+ offset = offset >>> 0;
38
+ byteLength = byteLength >>> 0;
39
+ if (!noAssert) {
40
+ const maxBytes = Math.pow(2, 8 * byteLength) - 1;
41
+ checkInt(buf, value, offset, byteLength, maxBytes, 0);
54
42
  }
55
43
 
56
- /**
57
- * Writes an unsigned integer to a buffer in little-endian format
58
- */
59
- public static writeUIntLE(
60
- buf: Uint8Array,
61
- value: number,
62
- offset: number,
63
- byteLength: number,
64
- noAssert?: boolean
65
- ): Uint8Array {
66
- value = +value;
67
- offset = offset >>> 0;
68
- byteLength = byteLength >>> 0;
69
- if (!noAssert) {
70
- const maxBytes = Math.pow(2, 8 * byteLength) - 1;
71
- BytesUtils.checkInt(buf, value, offset, byteLength, maxBytes, 0);
72
- }
73
-
74
- let mul = 1;
75
- let i = 0;
76
- buf[offset] = value & 0xff;
77
- while (++i < byteLength && (mul *= 0x100)) {
78
- buf[offset + i] = (value / mul) & 0xff;
79
- }
80
-
81
- return buf;
44
+ let mul = 1;
45
+ let i = 0;
46
+ buf[offset] = value & 0xff;
47
+ while (++i < byteLength && (mul *= 0x100)) {
48
+ buf[offset + i] = (value / mul) & 0xff;
82
49
  }
83
50
 
84
- /**
85
- * Fills with zeros to set length
86
- * @param array little endian Uint8Array
87
- * @param length amount to pad
88
- * @returns little endian Uint8Array padded with zeros to set length
89
- */
90
- public static zeroPadLE(array: Uint8Array, length: number): Uint8Array {
91
- const result = new Uint8Array(length);
92
- for (let i = 0; i < length; i++) {
93
- result[i] = array[i] || 0;
94
- }
95
- return result;
96
- }
51
+ return buf;
52
+ }
97
53
 
98
- // Adapted from https://github.com/feross/buffer
99
- public static checkInt(
100
- buf: Uint8Array,
101
- value: number,
102
- offset: number,
103
- ext: number,
104
- max: number,
105
- min: number
106
- ): void {
107
- if (value > max || value < min)
108
- throw new RangeError('"value" argument is out of bounds');
109
- if (offset + ext > buf.length) throw new RangeError("Index out of range");
54
+ /**
55
+ * Fills with zeros to set length
56
+ * @param array little endian Uint8Array
57
+ * @param length amount to pad
58
+ * @returns little endian Uint8Array padded with zeros to set length
59
+ */
60
+ export function zeroPadLE(array: Uint8Array, length: number): Uint8Array {
61
+ const result = new Uint8Array(length);
62
+ for (let i = 0; i < length; i++) {
63
+ result[i] = array[i] || 0;
110
64
  }
65
+ return result;
66
+ }
111
67
 
112
- /**
113
- * Concatenate Uint8Arrays
114
- * @param input
115
- * @returns concatenation of all Uint8Array received as input
116
- */
117
- public static concatenate(...input: Uint8Array[]): Uint8Array {
118
- let totalLength = 0;
119
- for (const arr of input) {
120
- totalLength += arr.length;
121
- }
122
- const result = new Uint8Array(totalLength);
123
- let offset = 0;
124
- for (const arr of input) {
125
- result.set(arr, offset);
126
- offset += arr.length;
127
- }
128
- return result;
129
- }
68
+ // Adapted from https://github.com/feross/buffer
69
+ function checkInt(
70
+ buf: Uint8Array,
71
+ value: number,
72
+ offset: number,
73
+ ext: number,
74
+ max: number,
75
+ min: number
76
+ ): void {
77
+ if (value > max || value < min)
78
+ throw new RangeError('"value" argument is out of bounds');
79
+ if (offset + ext > buf.length) throw new RangeError("Index out of range");
130
80
  }
package/src/utils/hash.ts CHANGED
@@ -1,25 +1,15 @@
1
1
  import * as zerokitRLN from "@waku/zerokit-rln-wasm";
2
2
 
3
- import { BytesUtils } from "./bytes.js";
3
+ import { concatenate, writeUIntLE } from "./bytes.js";
4
4
 
5
5
  export function poseidonHash(...input: Array<Uint8Array>): Uint8Array {
6
- const inputLen = BytesUtils.writeUIntLE(
7
- new Uint8Array(8),
8
- input.length,
9
- 0,
10
- 8
11
- );
12
- const lenPrefixedData = BytesUtils.concatenate(inputLen, ...input);
6
+ const inputLen = writeUIntLE(new Uint8Array(8), input.length, 0, 8);
7
+ const lenPrefixedData = concatenate(inputLen, ...input);
13
8
  return zerokitRLN.poseidonHash(lenPrefixedData);
14
9
  }
15
10
 
16
11
  export function sha256(input: Uint8Array): Uint8Array {
17
- const inputLen = BytesUtils.writeUIntLE(
18
- new Uint8Array(8),
19
- input.length,
20
- 0,
21
- 8
22
- );
23
- const lenPrefixedData = BytesUtils.concatenate(inputLen, input);
12
+ const inputLen = writeUIntLE(new Uint8Array(8), input.length, 0, 8);
13
+ const lenPrefixedData = concatenate(inputLen, input);
24
14
  return zerokitRLN.hash(lenPrefixedData);
25
15
  }
@@ -1,4 +1,9 @@
1
1
  export { extractMetaMaskSigner } from "./metamask.js";
2
- export { BytesUtils } from "./bytes.js";
2
+ export {
3
+ concatenate,
4
+ writeUIntLE,
5
+ switchEndianness,
6
+ zeroPadLE
7
+ } from "./bytes.js";
3
8
  export { sha256, poseidonHash } from "./hash.js";
4
9
  export { dateToEpoch, epochIntToBytes, epochBytesToInt } from "./epoch.js";
package/src/zerokit.ts CHANGED
@@ -5,7 +5,12 @@ import { DEFAULT_RATE_LIMIT, RATE_LIMIT_PARAMS } from "./contract/constants.js";
5
5
  import { IdentityCredential } from "./identity.js";
6
6
  import { Proof, proofToBytes } from "./proof.js";
7
7
  import { WitnessCalculator } from "./resources/witness_calculator";
8
- import { BytesUtils, dateToEpoch, epochIntToBytes } from "./utils/index.js";
8
+ import {
9
+ concatenate,
10
+ dateToEpoch,
11
+ epochIntToBytes,
12
+ writeUIntLE
13
+ } from "./utils/index.js";
9
14
 
10
15
  export class Zerokit {
11
16
  public constructor(
@@ -52,16 +57,13 @@ export class Zerokit {
52
57
  ): void {
53
58
  // serializes a seq of IDCommitments to a byte seq
54
59
  // the order of serialization is |id_commitment_len<8>|id_commitment<var>|
55
- const idCommitmentLen = BytesUtils.writeUIntLE(
60
+ const idCommitmentLen = writeUIntLE(
56
61
  new Uint8Array(8),
57
62
  idCommitments.length,
58
63
  0,
59
64
  8
60
65
  );
61
- const idCommitmentBytes = BytesUtils.concatenate(
62
- idCommitmentLen,
63
- ...idCommitments
64
- );
66
+ const idCommitmentBytes = concatenate(idCommitmentLen, ...idCommitments);
65
67
  zerokitRLN.setLeavesFrom(this.zkRLN, index, idCommitmentBytes);
66
68
  }
67
69
 
@@ -81,19 +83,9 @@ export class Zerokit {
81
83
  rateLimit?: number
82
84
  ): Uint8Array {
83
85
  // calculate message length
84
- const msgLen = BytesUtils.writeUIntLE(
85
- new Uint8Array(8),
86
- uint8Msg.length,
87
- 0,
88
- 8
89
- );
90
- const memIndexBytes = BytesUtils.writeUIntLE(
91
- new Uint8Array(8),
92
- memIndex,
93
- 0,
94
- 8
95
- );
96
- const rateLimitBytes = BytesUtils.writeUIntLE(
86
+ const msgLen = writeUIntLE(new Uint8Array(8), uint8Msg.length, 0, 8);
87
+ const memIndexBytes = writeUIntLE(new Uint8Array(8), memIndex, 0, 8);
88
+ const rateLimitBytes = writeUIntLE(
97
89
  new Uint8Array(8),
98
90
  rateLimit ?? this.rateLimit,
99
91
  0,
@@ -101,7 +93,7 @@ export class Zerokit {
101
93
  );
102
94
 
103
95
  // [ id_key<32> | id_index<8> | epoch<32> | signal_len<8> | signal<var> | rate_limit<8> ]
104
- return BytesUtils.concatenate(
96
+ return concatenate(
105
97
  idKey,
106
98
  memIndexBytes,
107
99
  epoch,
@@ -177,8 +169,8 @@ export class Zerokit {
177
169
  }
178
170
 
179
171
  // calculate message length
180
- const msgLen = BytesUtils.writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
181
- const rateLimitBytes = BytesUtils.writeUIntLE(
172
+ const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
173
+ const rateLimitBytes = writeUIntLE(
182
174
  new Uint8Array(8),
183
175
  rateLimit ?? this.rateLimit,
184
176
  0,
@@ -187,7 +179,7 @@ export class Zerokit {
187
179
 
188
180
  return zerokitRLN.verifyRLNProof(
189
181
  this.zkRLN,
190
- BytesUtils.concatenate(pBytes, msgLen, msg, rateLimitBytes)
182
+ concatenate(pBytes, msgLen, msg, rateLimitBytes)
191
183
  );
192
184
  }
193
185
 
@@ -204,19 +196,19 @@ export class Zerokit {
204
196
  pBytes = proofToBytes(proof);
205
197
  }
206
198
  // calculate message length
207
- const msgLen = BytesUtils.writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
208
- const rateLimitBytes = BytesUtils.writeUIntLE(
199
+ const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
200
+ const rateLimitBytes = writeUIntLE(
209
201
  new Uint8Array(8),
210
202
  rateLimit ?? this.rateLimit,
211
203
  0,
212
204
  8
213
205
  );
214
206
 
215
- const rootsBytes = BytesUtils.concatenate(...roots);
207
+ const rootsBytes = concatenate(...roots);
216
208
 
217
209
  return zerokitRLN.verifyWithRoots(
218
210
  this.zkRLN,
219
- BytesUtils.concatenate(pBytes, msgLen, msg, rateLimitBytes),
211
+ concatenate(pBytes, msgLen, msg, rateLimitBytes),
220
212
  rootsBytes
221
213
  );
222
214
  }
@@ -234,8 +226,8 @@ export class Zerokit {
234
226
  }
235
227
 
236
228
  // calculate message length
237
- const msgLen = BytesUtils.writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
238
- const rateLimitBytes = BytesUtils.writeUIntLE(
229
+ const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
230
+ const rateLimitBytes = writeUIntLE(
239
231
  new Uint8Array(8),
240
232
  rateLimit ?? this.rateLimit,
241
233
  0,
@@ -244,7 +236,7 @@ export class Zerokit {
244
236
 
245
237
  return zerokitRLN.verifyWithRoots(
246
238
  this.zkRLN,
247
- BytesUtils.concatenate(pBytes, msgLen, msg, rateLimitBytes),
239
+ concatenate(pBytes, msgLen, msg, rateLimitBytes),
248
240
  new Uint8Array()
249
241
  );
250
242
  }