@waku/rln 0.1.6-ace7ca2.0 → 0.1.6-b133417.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 +8 -32
  2. package/bundle/packages/rln/dist/contract/rln_contract.js +2 -2
  3. package/bundle/packages/rln/dist/credentials_manager.js +17 -11
  4. package/bundle/packages/rln/dist/identity.js +5 -0
  5. package/bundle/packages/rln/dist/keystore/keystore.js +18 -11
  6. package/bundle/packages/rln/dist/proof.js +2 -2
  7. package/bundle/packages/rln/dist/utils/bytes.js +108 -61
  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 +0 -6
  12. package/dist/contract/rln_base_contract.js +8 -32
  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 +4 -0
  17. package/dist/credentials_manager.js +18 -11
  18. package/dist/credentials_manager.js.map +1 -1
  19. package/dist/identity.d.ts +1 -0
  20. package/dist/identity.js +4 -0
  21. package/dist/identity.js.map +1 -1
  22. package/dist/keystore/keystore.js +18 -11
  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 +42 -16
  27. package/dist/utils/bytes.js +107 -60
  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 +17 -50
  38. package/src/contract/rln_contract.ts +5 -2
  39. package/src/credentials_manager.ts +31 -15
  40. package/src/identity.ts +7 -1
  41. package/src/keystore/keystore.ts +25 -11
  42. package/src/proof.ts +2 -2
  43. package/src/utils/bytes.ts +117 -67
  44. package/src/utils/hash.ts +15 -5
  45. package/src/utils/index.ts +1 -6
  46. package/src/zerokit.ts +30 -22
@@ -14,6 +14,8 @@ import {
14
14
  import _ from "lodash";
15
15
  import { v4 as uuidV4 } from "uuid";
16
16
 
17
+ import { BytesUtils } from "../utils/bytes.js";
18
+
17
19
  import { decryptEipKeystore, keccak256Checksum } from "./cipher.js";
18
20
  import { isCredentialValid, isKeystoreValid } from "./schema_validator.js";
19
21
  import type {
@@ -248,6 +250,7 @@ export class Keystore {
248
250
  const str = bytesToUtf8(bytes);
249
251
  const obj = JSON.parse(str);
250
252
 
253
+ // Little Endian
251
254
  const idCommitmentLE = Keystore.fromArraylikeToBytes(
252
255
  _.get(obj, "identityCredential.idCommitment", [])
253
256
  );
@@ -261,12 +264,21 @@ export class Keystore {
261
264
  _.get(obj, "identityCredential.idSecretHash", [])
262
265
  );
263
266
 
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
+
264
275
  return {
265
276
  identity: {
266
- IDCommitment: idCommitmentLE,
267
- IDTrapdoor: idTrapdoorLE,
268
- IDNullifier: idNullifierLE,
269
- IDSecretHash: idSecretHashLE
277
+ IDCommitment: idCommitmentBE,
278
+ IDTrapdoor: idTrapdoorBE,
279
+ IDNullifier: idNullifierBE,
280
+ IDSecretHash: idSecretHashBE,
281
+ IDCommitmentBigInt: idCommitmentBigInt
270
282
  },
271
283
  membership: {
272
284
  treeIndex: _.get(obj, "treeIndex"),
@@ -317,22 +329,24 @@ export class Keystore {
317
329
 
318
330
  // follows nwaku implementation
319
331
  // 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
320
333
  private static fromIdentityToBytes(options: KeystoreEntity): Uint8Array {
321
- function toLittleEndian(bytes: Uint8Array): Uint8Array {
322
- return new Uint8Array(bytes).reverse();
323
- }
324
334
  return utf8ToBytes(
325
335
  JSON.stringify({
326
336
  treeIndex: options.membership.treeIndex,
327
337
  identityCredential: {
328
338
  idCommitment: Array.from(
329
- toLittleEndian(options.identity.IDCommitment)
339
+ BytesUtils.switchEndianness(options.identity.IDCommitment)
340
+ ),
341
+ idNullifier: Array.from(
342
+ BytesUtils.switchEndianness(options.identity.IDNullifier)
330
343
  ),
331
- idNullifier: Array.from(toLittleEndian(options.identity.IDNullifier)),
332
344
  idSecretHash: Array.from(
333
- toLittleEndian(options.identity.IDSecretHash)
345
+ BytesUtils.switchEndianness(options.identity.IDSecretHash)
334
346
  ),
335
- idTrapdoor: Array.from(toLittleEndian(options.identity.IDTrapdoor))
347
+ idTrapdoor: Array.from(
348
+ BytesUtils.switchEndianness(options.identity.IDTrapdoor)
349
+ )
336
350
  },
337
351
  membershipContract: {
338
352
  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 { concatenate, poseidonHash } from "./utils/index.js";
3
+ import { BytesUtils, 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 concatenate(
60
+ return BytesUtils.concatenate(
61
61
  p.proof,
62
62
  p.merkleRoot,
63
63
  p.epoch,
@@ -1,80 +1,130 @@
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;
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());
10
7
  }
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;
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;
16
20
  }
17
- return result;
18
- }
19
21
 
20
- export function switchEndianness(bytes: Uint8Array): Uint8Array {
21
- return new Uint8Array(bytes.reverse());
22
- }
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
+ }
23
35
 
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
- }
36
+ // Reverse bytes and convert back to bigint
37
+ return bytes
38
+ .reverse()
39
+ .reduce((acc, byte) => (acc << 8n) + BigInt(byte), 0n);
40
+ }
28
41
 
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);
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;
42
54
  }
43
55
 
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;
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;
49
82
  }
50
83
 
51
- return buf;
52
- }
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
+ }
53
97
 
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;
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");
64
110
  }
65
- return result;
66
- }
67
111
 
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");
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
+ }
80
130
  }
package/src/utils/hash.ts CHANGED
@@ -1,15 +1,25 @@
1
1
  import * as zerokitRLN from "@waku/zerokit-rln-wasm";
2
2
 
3
- import { concatenate, writeUIntLE } from "./bytes.js";
3
+ import { BytesUtils } from "./bytes.js";
4
4
 
5
5
  export function poseidonHash(...input: Array<Uint8Array>): Uint8Array {
6
- const inputLen = writeUIntLE(new Uint8Array(8), input.length, 0, 8);
7
- const lenPrefixedData = concatenate(inputLen, ...input);
6
+ const inputLen = BytesUtils.writeUIntLE(
7
+ new Uint8Array(8),
8
+ input.length,
9
+ 0,
10
+ 8
11
+ );
12
+ const lenPrefixedData = BytesUtils.concatenate(inputLen, ...input);
8
13
  return zerokitRLN.poseidonHash(lenPrefixedData);
9
14
  }
10
15
 
11
16
  export function sha256(input: Uint8Array): Uint8Array {
12
- const inputLen = writeUIntLE(new Uint8Array(8), input.length, 0, 8);
13
- const lenPrefixedData = concatenate(inputLen, input);
17
+ const inputLen = BytesUtils.writeUIntLE(
18
+ new Uint8Array(8),
19
+ input.length,
20
+ 0,
21
+ 8
22
+ );
23
+ const lenPrefixedData = BytesUtils.concatenate(inputLen, input);
14
24
  return zerokitRLN.hash(lenPrefixedData);
15
25
  }
@@ -1,9 +1,4 @@
1
1
  export { extractMetaMaskSigner } from "./metamask.js";
2
- export {
3
- concatenate,
4
- writeUIntLE,
5
- switchEndianness,
6
- zeroPadLE
7
- } from "./bytes.js";
2
+ export { BytesUtils } from "./bytes.js";
8
3
  export { sha256, poseidonHash } from "./hash.js";
9
4
  export { dateToEpoch, epochIntToBytes, epochBytesToInt } from "./epoch.js";
package/src/zerokit.ts CHANGED
@@ -5,12 +5,7 @@ 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 {
9
- concatenate,
10
- dateToEpoch,
11
- epochIntToBytes,
12
- writeUIntLE
13
- } from "./utils/index.js";
8
+ import { BytesUtils, dateToEpoch, epochIntToBytes } from "./utils/index.js";
14
9
 
15
10
  export class Zerokit {
16
11
  public constructor(
@@ -57,13 +52,16 @@ export class Zerokit {
57
52
  ): void {
58
53
  // serializes a seq of IDCommitments to a byte seq
59
54
  // the order of serialization is |id_commitment_len<8>|id_commitment<var>|
60
- const idCommitmentLen = writeUIntLE(
55
+ const idCommitmentLen = BytesUtils.writeUIntLE(
61
56
  new Uint8Array(8),
62
57
  idCommitments.length,
63
58
  0,
64
59
  8
65
60
  );
66
- const idCommitmentBytes = concatenate(idCommitmentLen, ...idCommitments);
61
+ const idCommitmentBytes = BytesUtils.concatenate(
62
+ idCommitmentLen,
63
+ ...idCommitments
64
+ );
67
65
  zerokitRLN.setLeavesFrom(this.zkRLN, index, idCommitmentBytes);
68
66
  }
69
67
 
@@ -83,9 +81,19 @@ export class Zerokit {
83
81
  rateLimit?: number
84
82
  ): Uint8Array {
85
83
  // calculate message length
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(
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(
89
97
  new Uint8Array(8),
90
98
  rateLimit ?? this.rateLimit,
91
99
  0,
@@ -93,7 +101,7 @@ export class Zerokit {
93
101
  );
94
102
 
95
103
  // [ id_key<32> | id_index<8> | epoch<32> | signal_len<8> | signal<var> | rate_limit<8> ]
96
- return concatenate(
104
+ return BytesUtils.concatenate(
97
105
  idKey,
98
106
  memIndexBytes,
99
107
  epoch,
@@ -169,8 +177,8 @@ export class Zerokit {
169
177
  }
170
178
 
171
179
  // calculate message length
172
- const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
173
- const rateLimitBytes = writeUIntLE(
180
+ const msgLen = BytesUtils.writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
181
+ const rateLimitBytes = BytesUtils.writeUIntLE(
174
182
  new Uint8Array(8),
175
183
  rateLimit ?? this.rateLimit,
176
184
  0,
@@ -179,7 +187,7 @@ export class Zerokit {
179
187
 
180
188
  return zerokitRLN.verifyRLNProof(
181
189
  this.zkRLN,
182
- concatenate(pBytes, msgLen, msg, rateLimitBytes)
190
+ BytesUtils.concatenate(pBytes, msgLen, msg, rateLimitBytes)
183
191
  );
184
192
  }
185
193
 
@@ -196,19 +204,19 @@ export class Zerokit {
196
204
  pBytes = proofToBytes(proof);
197
205
  }
198
206
  // calculate message length
199
- const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
200
- const rateLimitBytes = writeUIntLE(
207
+ const msgLen = BytesUtils.writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
208
+ const rateLimitBytes = BytesUtils.writeUIntLE(
201
209
  new Uint8Array(8),
202
210
  rateLimit ?? this.rateLimit,
203
211
  0,
204
212
  8
205
213
  );
206
214
 
207
- const rootsBytes = concatenate(...roots);
215
+ const rootsBytes = BytesUtils.concatenate(...roots);
208
216
 
209
217
  return zerokitRLN.verifyWithRoots(
210
218
  this.zkRLN,
211
- concatenate(pBytes, msgLen, msg, rateLimitBytes),
219
+ BytesUtils.concatenate(pBytes, msgLen, msg, rateLimitBytes),
212
220
  rootsBytes
213
221
  );
214
222
  }
@@ -226,8 +234,8 @@ export class Zerokit {
226
234
  }
227
235
 
228
236
  // calculate message length
229
- const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
230
- const rateLimitBytes = writeUIntLE(
237
+ const msgLen = BytesUtils.writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
238
+ const rateLimitBytes = BytesUtils.writeUIntLE(
231
239
  new Uint8Array(8),
232
240
  rateLimit ?? this.rateLimit,
233
241
  0,
@@ -236,7 +244,7 @@ export class Zerokit {
236
244
 
237
245
  return zerokitRLN.verifyWithRoots(
238
246
  this.zkRLN,
239
- concatenate(pBytes, msgLen, msg, rateLimitBytes),
247
+ BytesUtils.concatenate(pBytes, msgLen, msg, rateLimitBytes),
240
248
  new Uint8Array()
241
249
  );
242
250
  }