@waku/rln 0.1.8-e224c05.0 → 0.1.8-e800af3.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 (187) hide show
  1. package/bundle/_virtual/utils.js +2 -2
  2. package/bundle/_virtual/utils2.js +2 -2
  3. package/bundle/index.js +6 -1
  4. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/_sha2.js +1 -1
  5. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/hmac.js +1 -1
  6. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/pbkdf2.js +1 -1
  7. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/scrypt.js +1 -1
  8. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/sha256.js +1 -1
  9. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/sha512.js +1 -1
  10. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/utils.js +1 -1
  11. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/random.js +1 -1
  12. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/utils.js +2 -2
  13. package/bundle/node_modules/@chainsafe/is-ip/lib/is-ip.js +12 -0
  14. package/bundle/node_modules/@chainsafe/is-ip/lib/parse.js +26 -0
  15. package/bundle/node_modules/@chainsafe/is-ip/lib/parser.js +202 -0
  16. package/bundle/node_modules/@multiformats/multiaddr/dist/src/constants.js +43 -0
  17. package/bundle/node_modules/@multiformats/multiaddr/dist/src/errors.js +17 -0
  18. package/bundle/node_modules/@multiformats/multiaddr/dist/src/registry.js +245 -0
  19. package/bundle/node_modules/@multiformats/multiaddr/dist/src/utils.js +191 -0
  20. package/bundle/node_modules/@multiformats/multiaddr/dist/src/validation.js +30 -0
  21. package/bundle/node_modules/@noble/hashes/esm/hmac.js +88 -0
  22. package/bundle/node_modules/@noble/hashes/esm/sha3.js +1 -1
  23. package/bundle/node_modules/@noble/hashes/esm/utils.js +8 -1
  24. package/bundle/node_modules/@waku/zerokit-rln-wasm/rln_wasm.js +517 -255
  25. package/bundle/node_modules/it-length-prefixed/dist/src/decode.js +6 -0
  26. package/bundle/node_modules/multiformats/dist/src/bases/base10.js +3 -1
  27. package/bundle/node_modules/multiformats/dist/src/bases/base16.js +4 -2
  28. package/bundle/node_modules/multiformats/dist/src/bases/base2.js +3 -1
  29. package/bundle/node_modules/multiformats/dist/src/bases/base256emoji.js +3 -1
  30. package/bundle/node_modules/multiformats/dist/src/bases/base32.js +11 -9
  31. package/bundle/node_modules/multiformats/dist/src/bases/base36.js +4 -2
  32. package/bundle/node_modules/multiformats/dist/src/bases/base58.js +4 -2
  33. package/bundle/node_modules/multiformats/dist/src/bases/base64.js +6 -4
  34. package/bundle/node_modules/multiformats/dist/src/bases/base8.js +3 -1
  35. package/bundle/node_modules/multiformats/dist/src/bases/identity.js +3 -1
  36. package/bundle/node_modules/multiformats/dist/src/basics.js +15 -0
  37. package/bundle/node_modules/multiformats/dist/src/bytes.js +15 -1
  38. package/bundle/node_modules/multiformats/dist/src/cid.js +371 -0
  39. package/bundle/node_modules/multiformats/dist/src/hashes/digest.js +62 -0
  40. package/bundle/node_modules/multiformats/dist/src/varint.js +15 -0
  41. package/bundle/node_modules/multiformats/dist/src/vendor/varint.js +78 -0
  42. package/bundle/node_modules/protons-runtime/dist/src/codec.js +20 -0
  43. package/bundle/node_modules/protons-runtime/dist/src/codecs/enum.js +24 -0
  44. package/bundle/node_modules/protons-runtime/dist/src/codecs/message.js +7 -0
  45. package/bundle/node_modules/protons-runtime/dist/src/decode.js +8 -0
  46. package/bundle/node_modules/protons-runtime/dist/src/encode.js +11 -0
  47. package/bundle/node_modules/protons-runtime/dist/src/index.js +30 -0
  48. package/bundle/node_modules/protons-runtime/dist/src/utils/float.js +54 -0
  49. package/bundle/node_modules/protons-runtime/dist/src/utils/longbits.js +175 -0
  50. package/bundle/node_modules/protons-runtime/dist/src/utils/pool.js +28 -0
  51. package/bundle/node_modules/protons-runtime/dist/src/utils/reader.js +367 -0
  52. package/bundle/node_modules/protons-runtime/dist/src/utils/utf8.js +99 -0
  53. package/bundle/node_modules/protons-runtime/dist/src/utils/writer.js +438 -0
  54. package/bundle/node_modules/uint8-varint/dist/src/index.js +124 -0
  55. package/bundle/node_modules/uint8arrays/dist/src/alloc.js +17 -0
  56. package/bundle/node_modules/uint8arrays/dist/src/concat.js +20 -0
  57. package/bundle/node_modules/uint8arrays/dist/src/from-string.js +19 -0
  58. package/bundle/node_modules/uint8arrays/dist/src/to-string.js +19 -0
  59. package/bundle/node_modules/uint8arrays/dist/src/util/as-uint8array.js +9 -0
  60. package/bundle/node_modules/uint8arrays/dist/src/util/bases.js +49 -0
  61. package/bundle/packages/core/dist/lib/connection_manager/connection_limiter.js +18 -0
  62. package/bundle/packages/core/dist/lib/connection_manager/connection_manager.js +24 -0
  63. package/bundle/packages/core/dist/lib/connection_manager/dialer.js +14 -0
  64. package/bundle/packages/core/dist/lib/connection_manager/discovery_dialer.js +14 -0
  65. package/bundle/packages/core/dist/lib/connection_manager/keep_alive_manager.js +15 -0
  66. package/bundle/packages/core/dist/lib/connection_manager/shard_reader.js +14 -0
  67. package/bundle/packages/core/dist/lib/filter/filter.js +28 -0
  68. package/bundle/packages/core/dist/lib/light_push/light_push.js +28 -0
  69. package/bundle/packages/core/dist/lib/message/version_0.js +172 -0
  70. package/bundle/packages/core/dist/lib/metadata/metadata.js +28 -0
  71. package/bundle/packages/core/dist/lib/store/store.js +24 -0
  72. package/bundle/packages/interfaces/dist/connection_manager.js +9 -0
  73. package/bundle/packages/interfaces/dist/health_status.js +17 -0
  74. package/bundle/packages/interfaces/dist/protocols.js +92 -0
  75. package/bundle/packages/interfaces/dist/waku.js +7 -0
  76. package/bundle/packages/proto/dist/generated/filter.js +447 -0
  77. package/bundle/packages/proto/dist/generated/filter_v2.js +426 -0
  78. package/bundle/packages/proto/dist/generated/light_push.js +550 -0
  79. package/bundle/packages/proto/dist/generated/message.js +215 -0
  80. package/bundle/packages/proto/dist/generated/metadata.js +132 -0
  81. package/bundle/packages/proto/dist/generated/peer_exchange.js +211 -0
  82. package/bundle/packages/proto/dist/generated/sds_message.js +172 -0
  83. package/bundle/packages/proto/dist/generated/store_v3.js +492 -0
  84. package/bundle/packages/proto/dist/generated/topic_only_message.js +63 -0
  85. package/bundle/packages/rln/dist/codec.js +92 -0
  86. package/bundle/packages/rln/dist/contract/constants.js +14 -7
  87. package/bundle/packages/rln/dist/contract/rln_base_contract.js +2 -1
  88. package/bundle/packages/rln/dist/contract/rln_contract.js +109 -0
  89. package/bundle/packages/rln/dist/credentials_manager.js +45 -4
  90. package/bundle/packages/rln/dist/identity.js +2 -1
  91. package/bundle/packages/rln/dist/keystore/keystore.js +30 -9
  92. package/bundle/packages/rln/dist/message.js +59 -0
  93. package/bundle/packages/rln/dist/proof.js +54 -0
  94. package/bundle/packages/rln/dist/resources/verification_key.js +112 -0
  95. package/bundle/packages/rln/dist/resources/witness_calculator.js +1 -1
  96. package/bundle/packages/rln/dist/rln.js +36 -4
  97. package/bundle/packages/rln/dist/root_tracker.js +76 -0
  98. package/bundle/packages/rln/dist/utils/bytes.js +70 -31
  99. package/bundle/packages/rln/dist/utils/epoch.js +23 -1
  100. package/bundle/packages/rln/dist/utils/hash.js +10 -0
  101. package/bundle/packages/rln/dist/zerokit.js +99 -2
  102. package/bundle/packages/utils/dist/bytes/index.js +31 -0
  103. package/bundle/resources/rln.wasm +0 -0
  104. package/bundle/resources/rln_final.zkey +0 -0
  105. package/bundle/resources/verification_key.d.ts +13 -0
  106. package/bundle/resources/verification_key.js +112 -0
  107. package/bundle/resources/witness_calculator.d.ts +7 -21
  108. package/bundle/resources/witness_calculator.js +1 -1
  109. package/dist/.tsbuildinfo +1 -1
  110. package/dist/codec.d.ts +40 -0
  111. package/dist/codec.js +79 -0
  112. package/dist/codec.js.map +1 -0
  113. package/dist/codec.test-utils.d.ts +37 -0
  114. package/dist/codec.test-utils.js +61 -0
  115. package/dist/codec.test-utils.js.map +1 -0
  116. package/dist/contract/constants.d.ts +10 -3
  117. package/dist/contract/constants.js +13 -6
  118. package/dist/contract/constants.js.map +1 -1
  119. package/dist/contract/index.d.ts +1 -0
  120. package/dist/contract/index.js +1 -0
  121. package/dist/contract/index.js.map +1 -1
  122. package/dist/contract/rln_base_contract.js +2 -1
  123. package/dist/contract/rln_base_contract.js.map +1 -1
  124. package/dist/contract/rln_contract.d.ts +17 -0
  125. package/dist/contract/rln_contract.js +107 -0
  126. package/dist/contract/rln_contract.js.map +1 -0
  127. package/dist/contract/test_setup.d.ts +26 -0
  128. package/dist/contract/test_setup.js +56 -0
  129. package/dist/contract/test_setup.js.map +1 -0
  130. package/dist/contract/test_utils.d.ts +39 -0
  131. package/dist/contract/test_utils.js +118 -0
  132. package/dist/contract/test_utils.js.map +1 -0
  133. package/dist/credentials_manager.d.ts +14 -2
  134. package/dist/credentials_manager.js +45 -4
  135. package/dist/credentials_manager.js.map +1 -1
  136. package/dist/identity.js +2 -1
  137. package/dist/identity.js.map +1 -1
  138. package/dist/index.d.ts +6 -2
  139. package/dist/index.js +6 -2
  140. package/dist/index.js.map +1 -1
  141. package/dist/keystore/keystore.js +30 -9
  142. package/dist/keystore/keystore.js.map +1 -1
  143. package/dist/message.d.ts +19 -0
  144. package/dist/message.js +51 -0
  145. package/dist/message.js.map +1 -0
  146. package/dist/proof.d.ts +21 -0
  147. package/dist/proof.js +50 -0
  148. package/dist/proof.js.map +1 -0
  149. package/dist/resources/rln.wasm +0 -0
  150. package/dist/resources/rln_final.zkey +0 -0
  151. package/dist/resources/verification_key.d.ts +13 -0
  152. package/dist/resources/verification_key.js +112 -0
  153. package/dist/resources/witness_calculator.d.ts +7 -21
  154. package/dist/resources/witness_calculator.js +1 -1
  155. package/dist/rln.d.ts +9 -0
  156. package/dist/rln.js +32 -4
  157. package/dist/rln.js.map +1 -1
  158. package/dist/root_tracker.d.ts +10 -0
  159. package/dist/root_tracker.js +75 -0
  160. package/dist/root_tracker.js.map +1 -0
  161. package/dist/utils/bytes.d.ts +31 -9
  162. package/dist/utils/bytes.js +70 -31
  163. package/dist/utils/bytes.js.map +1 -1
  164. package/dist/zerokit.d.ts +11 -0
  165. package/dist/zerokit.js +97 -1
  166. package/dist/zerokit.js.map +1 -1
  167. package/package.json +1 -1
  168. package/src/codec.test-utils.ts +88 -0
  169. package/src/codec.ts +138 -0
  170. package/src/contract/constants.ts +16 -6
  171. package/src/contract/index.ts +1 -0
  172. package/src/contract/rln_base_contract.ts +2 -1
  173. package/src/contract/rln_contract.ts +147 -0
  174. package/src/contract/test_setup.ts +86 -0
  175. package/src/contract/test_utils.ts +179 -0
  176. package/src/credentials_manager.ts +72 -8
  177. package/src/identity.ts +2 -1
  178. package/src/index.ts +11 -1
  179. package/src/keystore/keystore.ts +32 -9
  180. package/src/message.ts +73 -0
  181. package/src/proof.ts +69 -0
  182. package/src/resources/verification_key.d.ts +13 -0
  183. package/src/resources/witness_calculator.d.ts +7 -21
  184. package/src/rln.ts +65 -5
  185. package/src/root_tracker.ts +92 -0
  186. package/src/utils/bytes.ts +73 -36
  187. package/src/zerokit.ts +217 -1
@@ -0,0 +1,112 @@
1
+ const verificationKey = {
2
+ protocol: "groth16",
3
+ curve: "bn128",
4
+ nPublic: 6,
5
+ vk_alpha_1: [
6
+ "20124996762962216725442980738609010303800849578410091356605067053491763969391",
7
+ "9118593021526896828671519912099489027245924097793322973632351264852174143923",
8
+ "1"
9
+ ],
10
+ vk_beta_2: [
11
+ [
12
+ "4693952934005375501364248788849686435240706020501681709396105298107971354382",
13
+ "14346958885444710485362620645446987998958218205939139994511461437152241966681"
14
+ ],
15
+ [
16
+ "16851772916911573982706166384196538392731905827088356034885868448550849804972",
17
+ "823612331030938060799959717749043047845343400798220427319188951998582076532"
18
+ ],
19
+ ["1", "0"]
20
+ ],
21
+ vk_gamma_2: [
22
+ [
23
+ "10857046999023057135944570762232829481370756359578518086990519993285655852781",
24
+ "11559732032986387107991004021392285783925812861821192530917403151452391805634"
25
+ ],
26
+ [
27
+ "8495653923123431417604973247489272438418190587263600148770280649306958101930",
28
+ "4082367875863433681332203403145435568316851327593401208105741076214120093531"
29
+ ],
30
+ ["1", "0"]
31
+ ],
32
+ vk_delta_2: [
33
+ [
34
+ "8353516066399360694538747105302262515182301251524941126222712285088022964076",
35
+ "9329524012539638256356482961742014315122377605267454801030953882967973561832"
36
+ ],
37
+ [
38
+ "16805391589556134376869247619848130874761233086443465978238468412168162326401",
39
+ "10111259694977636294287802909665108497237922060047080343914303287629927847739"
40
+ ],
41
+ ["1", "0"]
42
+ ],
43
+ vk_alphabeta_12: [
44
+ [
45
+ [
46
+ "12608968655665301215455851857466367636344427685631271961542642719683786103711",
47
+ "9849575605876329747382930567422916152871921500826003490242628251047652318086"
48
+ ],
49
+ [
50
+ "6322029441245076030714726551623552073612922718416871603535535085523083939021",
51
+ "8700115492541474338049149013125102281865518624059015445617546140629435818912"
52
+ ],
53
+ [
54
+ "10674973475340072635573101639867487770811074181475255667220644196793546640210",
55
+ "2926286967251299230490668407790788696102889214647256022788211245826267484824"
56
+ ]
57
+ ],
58
+ [
59
+ [
60
+ "9660441540778523475944706619139394922744328902833875392144658911530830074820",
61
+ "19548113127774514328631808547691096362144426239827206966690021428110281506546"
62
+ ],
63
+ [
64
+ "1870837942477655969123169532603615788122896469891695773961478956740992497097",
65
+ "12536105729661705698805725105036536744930776470051238187456307227425796690780"
66
+ ],
67
+ [
68
+ "21811903352654147452884857281720047789720483752548991551595462057142824037334",
69
+ "19021616763967199151052893283384285352200445499680068407023236283004353578353"
70
+ ]
71
+ ]
72
+ ],
73
+ IC: [
74
+ [
75
+ "11992897507809711711025355300535923222599547639134311050809253678876341466909",
76
+ "17181525095924075896332561978747020491074338784673526378866503154966799128110",
77
+ "1"
78
+ ],
79
+ [
80
+ "17018665030246167677911144513385572506766200776123272044534328594850561667818",
81
+ "18601114175490465275436712413925513066546725461375425769709566180981674884464",
82
+ "1"
83
+ ],
84
+ [
85
+ "18799470100699658367834559797874857804183288553462108031963980039244731716542",
86
+ "13064227487174191981628537974951887429496059857753101852163607049188825592007",
87
+ "1"
88
+ ],
89
+ [
90
+ "17432501889058124609368103715904104425610382063762621017593209214189134571156",
91
+ "13406815149699834788256141097399354592751313348962590382887503595131085938635",
92
+ "1"
93
+ ],
94
+ [
95
+ "10320964835612716439094703312987075811498239445882526576970512041988148264481",
96
+ "9024164961646353611176283204118089412001502110138072989569118393359029324867",
97
+ "1"
98
+ ],
99
+ [
100
+ "718355081067365548229685160476620267257521491773976402837645005858953849298",
101
+ "14635482993933988261008156660773180150752190597753512086153001683711587601974",
102
+ "1"
103
+ ],
104
+ [
105
+ "11777720285956632126519898515392071627539405001940313098390150593689568177535",
106
+ "8483603647274280691250972408211651407952870456587066148445913156086740744515",
107
+ "1"
108
+ ]
109
+ ]
110
+ };
111
+
112
+ export { verificationKey as default };
@@ -1,6 +1,6 @@
1
1
  // File generated with https://github.com/iden3/circom
2
2
  // following the instructions from:
3
- // https://github.com/vacp2p/zerokit/tree/master/rln#advanced-custom-circuit-compilation
3
+ // https://github.com/vacp2p/zerokit/tree/master/rln#compiling-circuits
4
4
 
5
5
  async function builder(code, options) {
6
6
  options = options || {};
@@ -1,3 +1,8 @@
1
+ import { createEncoder, createDecoder } from '../../core/dist/lib/message/version_0.js';
2
+ import '../../core/dist/lib/filter/filter.js';
3
+ import '../../core/dist/lib/light_push/light_push.js';
4
+ import '../../core/dist/lib/store/store.js';
5
+ import '../../core/dist/lib/connection_manager/connection_manager.js';
1
6
  import '../../../node_modules/multiformats/dist/src/bases/base10.js';
2
7
  import '../../../node_modules/multiformats/dist/src/bases/base16.js';
3
8
  import '../../../node_modules/multiformats/dist/src/bases/base2.js';
@@ -10,9 +15,12 @@ import '../../../node_modules/multiformats/dist/src/bases/base8.js';
10
15
  import '../../../node_modules/multiformats/dist/src/bases/identity.js';
11
16
  import '../../../node_modules/multiformats/dist/src/codecs/json.js';
12
17
  import { Logger } from '../../utils/dist/logger.js';
13
- import __wbg_init, { initPanicHook, newRLN } from '../../../node_modules/@waku/zerokit-rln-wasm/rln_wasm.js';
18
+ import '../../core/dist/lib/metadata/metadata.js';
19
+ import __wbg_init, { init_panic_hook, newRLN } from '../../../node_modules/@waku/zerokit-rln-wasm/rln_wasm.js';
20
+ import { createRLNEncoder, createRLNDecoder } from './codec.js';
14
21
  import { DEFAULT_RATE_LIMIT } from './contract/constants.js';
15
22
  import { RLNCredentialsManager } from './credentials_manager.js';
23
+ import verificationKey from './resources/verification_key.js';
16
24
  import { builder } from './resources/witness_calculator.js';
17
25
  import { Zerokit } from './zerokit.js';
18
26
 
@@ -25,11 +33,15 @@ class RLNInstance extends RLNCredentialsManager {
25
33
  */
26
34
  static async create() {
27
35
  try {
28
- await __wbg_init();
29
- initPanicHook();
36
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
+ await __wbg_init?.();
38
+ init_panic_hook();
30
39
  const witnessCalculator = await RLNInstance.loadWitnessCalculator();
31
40
  const zkey = await RLNInstance.loadZkey();
32
- const zkRLN = newRLN(zkey);
41
+ const stringEncoder = new TextEncoder();
42
+ const vkey = stringEncoder.encode(JSON.stringify(verificationKey));
43
+ const DEPTH = 20;
44
+ const zkRLN = newRLN(DEPTH, zkey, vkey);
33
45
  const zerokit = new Zerokit(zkRLN, witnessCalculator, DEFAULT_RATE_LIMIT);
34
46
  return new RLNInstance(zerokit);
35
47
  }
@@ -42,6 +54,26 @@ class RLNInstance extends RLNCredentialsManager {
42
54
  super(zerokit);
43
55
  this.zerokit = zerokit;
44
56
  }
57
+ async createEncoder(options) {
58
+ const { credentials: decryptedCredentials } = await RLNInstance.decryptCredentialsIfNeeded(options.credentials);
59
+ const credentials = decryptedCredentials || this.credentials;
60
+ if (!credentials) {
61
+ throw Error("Failed to create Encoder: missing RLN credentials. Use createRLNEncoder directly.");
62
+ }
63
+ await this.verifyCredentialsAgainstContract(credentials);
64
+ return createRLNEncoder({
65
+ encoder: createEncoder(options),
66
+ rlnInstance: this,
67
+ index: credentials.membership.treeIndex,
68
+ credential: credentials.identity
69
+ });
70
+ }
71
+ createDecoder(contentTopic, routingInfo) {
72
+ return createRLNDecoder({
73
+ rlnInstance: this,
74
+ decoder: createDecoder(contentTopic, routingInfo)
75
+ });
76
+ }
45
77
  static async loadWitnessCalculator() {
46
78
  try {
47
79
  const url = new URL("./resources/rln.wasm", import.meta.url);
@@ -0,0 +1,76 @@
1
+ class RootPerBlock {
2
+ root;
3
+ blockNumber;
4
+ constructor(root, blockNumber) {
5
+ this.root = root;
6
+ this.blockNumber = blockNumber;
7
+ }
8
+ }
9
+ const maxBufferSize = 20;
10
+ class MerkleRootTracker {
11
+ acceptableRootWindowSize;
12
+ validMerkleRoots = new Array();
13
+ merkleRootBuffer = new Array();
14
+ constructor(acceptableRootWindowSize, initialRoot) {
15
+ this.acceptableRootWindowSize = acceptableRootWindowSize;
16
+ this.pushRoot(0, initialRoot);
17
+ }
18
+ backFill(fromBlockNumber) {
19
+ if (this.validMerkleRoots.length == 0)
20
+ return;
21
+ let numBlocks = 0;
22
+ for (let i = this.validMerkleRoots.length - 1; i >= 0; i--) {
23
+ if (this.validMerkleRoots[i].blockNumber >= fromBlockNumber) {
24
+ numBlocks++;
25
+ }
26
+ }
27
+ if (numBlocks == 0)
28
+ return;
29
+ const olderBlock = fromBlockNumber < this.validMerkleRoots[0].blockNumber;
30
+ // Remove last roots
31
+ let rootsToPop = numBlocks;
32
+ if (this.validMerkleRoots.length < rootsToPop) {
33
+ rootsToPop = this.validMerkleRoots.length;
34
+ }
35
+ this.validMerkleRoots = this.validMerkleRoots.slice(0, this.validMerkleRoots.length - rootsToPop);
36
+ if (this.merkleRootBuffer.length == 0)
37
+ return;
38
+ if (olderBlock) {
39
+ const idx = this.merkleRootBuffer.findIndex((x) => x.blockNumber == fromBlockNumber);
40
+ if (idx > -1) {
41
+ this.merkleRootBuffer = this.merkleRootBuffer.slice(0, idx);
42
+ }
43
+ }
44
+ // Backfill the tree's acceptable roots
45
+ let rootsToRestore = this.acceptableRootWindowSize - this.validMerkleRoots.length;
46
+ if (this.merkleRootBuffer.length < rootsToRestore) {
47
+ rootsToRestore = this.merkleRootBuffer.length;
48
+ }
49
+ for (let i = 0; i < rootsToRestore; i++) {
50
+ const x = this.merkleRootBuffer.pop();
51
+ if (x)
52
+ this.validMerkleRoots.unshift(x);
53
+ }
54
+ }
55
+ pushRoot(blockNumber, root) {
56
+ this.validMerkleRoots.push(new RootPerBlock(root, blockNumber));
57
+ // Maintain valid merkle root window
58
+ if (this.validMerkleRoots.length > this.acceptableRootWindowSize) {
59
+ const x = this.validMerkleRoots.shift();
60
+ if (x)
61
+ this.merkleRootBuffer.push(x);
62
+ }
63
+ // Maintain merkle root buffer
64
+ if (this.merkleRootBuffer.length > maxBufferSize) {
65
+ this.merkleRootBuffer.shift();
66
+ }
67
+ }
68
+ roots() {
69
+ return this.validMerkleRoots.map((x) => x.root);
70
+ }
71
+ buffer() {
72
+ return this.merkleRootBuffer.map((x) => x.root);
73
+ }
74
+ }
75
+
76
+ export { MerkleRootTracker };
@@ -1,44 +1,52 @@
1
1
  class BytesUtils {
2
2
  /**
3
- * Concatenate Uint8Arrays
4
- * @param input
5
- * @returns concatenation of all Uint8Array received as input
3
+ * Switches endianness of a byte array
6
4
  */
7
- static concatenate(...input) {
8
- let totalLength = 0;
9
- for (const arr of input) {
10
- totalLength += arr.length;
11
- }
12
- const result = new Uint8Array(totalLength);
13
- let offset = 0;
14
- for (const arr of input) {
15
- result.set(arr, offset);
16
- offset += arr.length;
5
+ static switchEndianness(bytes) {
6
+ return new Uint8Array([...bytes].reverse());
7
+ }
8
+ /**
9
+ * Builds a BigInt from a big-endian Uint8Array
10
+ * @param bytes The big-endian bytes to convert
11
+ * @returns The resulting BigInt in big-endian format
12
+ */
13
+ static buildBigIntFromUint8ArrayBE(bytes) {
14
+ let result = 0n;
15
+ for (let i = 0; i < bytes.length; i++) {
16
+ result = (result << 8n) + BigInt(bytes[i]);
17
17
  }
18
18
  return result;
19
19
  }
20
20
  /**
21
- * Convert a Uint8Array to a BigInt with configurable input endianness
22
- * @param bytes - The byte array to convert
23
- * @param inputEndianness - Endianness of the input bytes ('big' or 'little')
24
- * @returns BigInt representation of the bytes
21
+ * Switches endianness of a bigint value
22
+ * @param value The bigint value to switch endianness for
23
+ * @returns The bigint value with reversed endianness
25
24
  */
26
- static toBigInt(bytes, inputEndianness = "little") {
27
- if (bytes.length === 0) {
28
- return 0n;
29
- }
30
- // Create a copy to avoid modifying the original array
31
- const workingBytes = new Uint8Array(bytes);
32
- // Reverse bytes if input is little-endian to work with big-endian internally
33
- if (inputEndianness === "little") {
34
- workingBytes.reverse();
25
+ static switchEndiannessBigInt(value) {
26
+ // Convert bigint to byte array
27
+ const bytes = [];
28
+ let tempValue = value;
29
+ while (tempValue > 0n) {
30
+ bytes.push(Number(tempValue & 0xffn));
31
+ tempValue >>= 8n;
35
32
  }
36
- // Convert to BigInt
37
- let result = 0n;
38
- for (let i = 0; i < workingBytes.length; i++) {
39
- result = (result << 8n) | BigInt(workingBytes[i]);
33
+ // Reverse bytes and convert back to bigint
34
+ return bytes
35
+ .reverse()
36
+ .reduce((acc, byte) => (acc << 8n) + BigInt(byte), 0n);
37
+ }
38
+ /**
39
+ * Converts a big-endian bigint to a 32-byte big-endian Uint8Array
40
+ * @param value The big-endian bigint to convert
41
+ * @returns A 32-byte big-endian Uint8Array
42
+ */
43
+ static bigIntToUint8Array32BE(value) {
44
+ const bytes = new Uint8Array(32);
45
+ for (let i = 31; i >= 0; i--) {
46
+ bytes[i] = Number(value & 0xffn);
47
+ value >>= 8n;
40
48
  }
41
- return result;
49
+ return bytes;
42
50
  }
43
51
  /**
44
52
  * Writes an unsigned integer to a buffer in little-endian format
@@ -59,6 +67,19 @@ class BytesUtils {
59
67
  }
60
68
  return buf;
61
69
  }
70
+ /**
71
+ * Fills with zeros to set length
72
+ * @param array little endian Uint8Array
73
+ * @param length amount to pad
74
+ * @returns little endian Uint8Array padded with zeros to set length
75
+ */
76
+ static zeroPadLE(array, length) {
77
+ const result = new Uint8Array(length);
78
+ for (let i = 0; i < length; i++) {
79
+ result[i] = array[i] || 0;
80
+ }
81
+ return result;
82
+ }
62
83
  // Adapted from https://github.com/feross/buffer
63
84
  static checkInt(buf, value, offset, ext, max, min) {
64
85
  if (value > max || value < min)
@@ -66,6 +87,24 @@ class BytesUtils {
66
87
  if (offset + ext > buf.length)
67
88
  throw new RangeError("Index out of range");
68
89
  }
90
+ /**
91
+ * Concatenate Uint8Arrays
92
+ * @param input
93
+ * @returns concatenation of all Uint8Array received as input
94
+ */
95
+ static concatenate(...input) {
96
+ let totalLength = 0;
97
+ for (const arr of input) {
98
+ totalLength += arr.length;
99
+ }
100
+ const result = new Uint8Array(totalLength);
101
+ let offset = 0;
102
+ for (const arr of input) {
103
+ result.set(arr, offset);
104
+ offset += arr.length;
105
+ }
106
+ return result;
107
+ }
69
108
  }
70
109
 
71
110
  export { BytesUtils };
@@ -11,4 +11,26 @@ import '../../../../node_modules/multiformats/dist/src/bases/identity.js';
11
11
  import '../../../../node_modules/multiformats/dist/src/codecs/json.js';
12
12
  import { Logger } from '../../../utils/dist/logger.js';
13
13
 
14
- new Logger("waku:rln:epoch");
14
+ const DefaultEpochUnitSeconds = 10; // the rln-relay epoch length in seconds
15
+ const log = new Logger("waku:rln:epoch");
16
+ function dateToEpoch(timestamp, epochUnitSeconds = DefaultEpochUnitSeconds) {
17
+ const time = timestamp.getTime();
18
+ const epoch = Math.floor(time / 1000 / epochUnitSeconds);
19
+ log.info("generated epoch", epoch);
20
+ return epoch;
21
+ }
22
+ function epochIntToBytes(epoch) {
23
+ const bytes = new Uint8Array(32);
24
+ const db = new DataView(bytes.buffer);
25
+ db.setUint32(0, epoch, true);
26
+ log.info("encoded epoch", epoch, bytes);
27
+ return bytes;
28
+ }
29
+ function epochBytesToInt(bytes) {
30
+ const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
31
+ const epoch = dv.getUint32(0, true);
32
+ log.info("decoded epoch", epoch, bytes);
33
+ return epoch;
34
+ }
35
+
36
+ export { dateToEpoch, epochBytesToInt, epochIntToBytes };
@@ -0,0 +1,10 @@
1
+ import { poseidonHash as poseidonHash$1 } from '../../../../node_modules/@waku/zerokit-rln-wasm/rln_wasm.js';
2
+ import { BytesUtils } from './bytes.js';
3
+
4
+ function poseidonHash(...input) {
5
+ const inputLen = BytesUtils.writeUIntLE(new Uint8Array(8), input.length, 0, 8);
6
+ const lenPrefixedData = BytesUtils.concatenate(inputLen, ...input);
7
+ return poseidonHash$1(lenPrefixedData);
8
+ }
9
+
10
+ export { poseidonHash };
@@ -1,6 +1,9 @@
1
- import { generateSeededExtendedMembershipKey } from '../../../node_modules/@waku/zerokit-rln-wasm/rln_wasm.js';
2
- import { DEFAULT_RATE_LIMIT } from './contract/constants.js';
1
+ import { generateExtendedMembershipKey, generateSeededExtendedMembershipKey, insertMember, setLeavesFrom, deleteLeaf, getRoot, getSerializedRLNWitness, RLNWitnessToJson, generate_rln_proof_with_witness, verifyRLNProof, verifyWithRoots } from '../../../node_modules/@waku/zerokit-rln-wasm/rln_wasm.js';
2
+ import { DEFAULT_RATE_LIMIT, RATE_LIMIT_PARAMS } from './contract/constants.js';
3
3
  import { IdentityCredential } from './identity.js';
4
+ import { Proof, proofToBytes } from './proof.js';
5
+ import { BytesUtils } from './utils/bytes.js';
6
+ import { epochIntToBytes, dateToEpoch } from './utils/epoch.js';
4
7
 
5
8
  class Zerokit {
6
9
  zkRLN;
@@ -20,12 +23,106 @@ class Zerokit {
20
23
  get rateLimit() {
21
24
  return this._rateLimit;
22
25
  }
26
+ generateIdentityCredentials() {
27
+ const memKeys = generateExtendedMembershipKey(this.zkRLN); // TODO: rename this function in zerokit rln-wasm
28
+ return IdentityCredential.fromBytes(memKeys);
29
+ }
23
30
  generateSeededIdentityCredential(seed) {
24
31
  const stringEncoder = new TextEncoder();
25
32
  const seedBytes = stringEncoder.encode(seed);
33
+ // TODO: rename this function in zerokit rln-wasm
26
34
  const memKeys = generateSeededExtendedMembershipKey(this.zkRLN, seedBytes);
27
35
  return IdentityCredential.fromBytes(memKeys);
28
36
  }
37
+ insertMember(idCommitment) {
38
+ insertMember(this.zkRLN, idCommitment);
39
+ }
40
+ insertMembers(index, ...idCommitments) {
41
+ // serializes a seq of IDCommitments to a byte seq
42
+ // the order of serialization is |id_commitment_len<8>|id_commitment<var>|
43
+ const idCommitmentLen = BytesUtils.writeUIntLE(new Uint8Array(8), idCommitments.length, 0, 8);
44
+ const idCommitmentBytes = BytesUtils.concatenate(idCommitmentLen, ...idCommitments);
45
+ setLeavesFrom(this.zkRLN, index, idCommitmentBytes);
46
+ }
47
+ deleteMember(index) {
48
+ deleteLeaf(this.zkRLN, index);
49
+ }
50
+ getMerkleRoot() {
51
+ return getRoot(this.zkRLN);
52
+ }
53
+ serializeMessage(uint8Msg, memIndex, epoch, idKey, rateLimit) {
54
+ // calculate message length
55
+ const msgLen = BytesUtils.writeUIntLE(new Uint8Array(8), uint8Msg.length, 0, 8);
56
+ const memIndexBytes = BytesUtils.writeUIntLE(new Uint8Array(8), memIndex, 0, 8);
57
+ const rateLimitBytes = BytesUtils.writeUIntLE(new Uint8Array(8), rateLimit ?? this.rateLimit, 0, 8);
58
+ // [ id_key<32> | id_index<8> | epoch<32> | signal_len<8> | signal<var> | rate_limit<8> ]
59
+ return BytesUtils.concatenate(idKey, memIndexBytes, epoch, msgLen, uint8Msg, rateLimitBytes);
60
+ }
61
+ async generateRLNProof(msg, index, epoch, idSecretHash, rateLimit) {
62
+ if (epoch === undefined) {
63
+ epoch = epochIntToBytes(dateToEpoch(new Date()));
64
+ }
65
+ else if (epoch instanceof Date) {
66
+ epoch = epochIntToBytes(dateToEpoch(epoch));
67
+ }
68
+ const effectiveRateLimit = rateLimit ?? this.rateLimit;
69
+ if (epoch.length !== 32)
70
+ throw new Error("invalid epoch");
71
+ if (idSecretHash.length !== 32)
72
+ throw new Error("invalid id secret hash");
73
+ if (index < 0)
74
+ throw new Error("index must be >= 0");
75
+ if (effectiveRateLimit < RATE_LIMIT_PARAMS.MIN_RATE ||
76
+ effectiveRateLimit > RATE_LIMIT_PARAMS.MAX_RATE) {
77
+ throw new Error(`Rate limit must be between ${RATE_LIMIT_PARAMS.MIN_RATE} and ${RATE_LIMIT_PARAMS.MAX_RATE}`);
78
+ }
79
+ const serialized_msg = this.serializeMessage(msg, index, epoch, idSecretHash, effectiveRateLimit);
80
+ const rlnWitness = getSerializedRLNWitness(this.zkRLN, serialized_msg);
81
+ const inputs = RLNWitnessToJson(this.zkRLN, rlnWitness);
82
+ const calculatedWitness = await this.witnessCalculator.calculateWitness(inputs, false);
83
+ const proofBytes = generate_rln_proof_with_witness(this.zkRLN, calculatedWitness, rlnWitness);
84
+ return new Proof(proofBytes);
85
+ }
86
+ verifyRLNProof(proof, msg, rateLimit) {
87
+ let pBytes;
88
+ if (proof instanceof Uint8Array) {
89
+ pBytes = proof;
90
+ }
91
+ else {
92
+ pBytes = proofToBytes(proof);
93
+ }
94
+ // calculate message length
95
+ const msgLen = BytesUtils.writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
96
+ const rateLimitBytes = BytesUtils.writeUIntLE(new Uint8Array(8), rateLimit ?? this.rateLimit, 0, 8);
97
+ return verifyRLNProof(this.zkRLN, BytesUtils.concatenate(pBytes, msgLen, msg, rateLimitBytes));
98
+ }
99
+ verifyWithRoots(proof, msg, roots, rateLimit) {
100
+ let pBytes;
101
+ if (proof instanceof Uint8Array) {
102
+ pBytes = proof;
103
+ }
104
+ else {
105
+ pBytes = proofToBytes(proof);
106
+ }
107
+ // calculate message length
108
+ const msgLen = BytesUtils.writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
109
+ const rateLimitBytes = BytesUtils.writeUIntLE(new Uint8Array(8), rateLimit ?? this.rateLimit, 0, 8);
110
+ const rootsBytes = BytesUtils.concatenate(...roots);
111
+ return verifyWithRoots(this.zkRLN, BytesUtils.concatenate(pBytes, msgLen, msg, rateLimitBytes), rootsBytes);
112
+ }
113
+ verifyWithNoRoot(proof, msg, rateLimit) {
114
+ let pBytes;
115
+ if (proof instanceof Uint8Array) {
116
+ pBytes = proof;
117
+ }
118
+ else {
119
+ pBytes = proofToBytes(proof);
120
+ }
121
+ // calculate message length
122
+ const msgLen = BytesUtils.writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
123
+ const rateLimitBytes = BytesUtils.writeUIntLE(new Uint8Array(8), rateLimit ?? this.rateLimit, 0, 8);
124
+ return verifyWithRoots(this.zkRLN, BytesUtils.concatenate(pBytes, msgLen, msg, rateLimitBytes), new Uint8Array());
125
+ }
29
126
  }
30
127
 
31
128
  export { Zerokit };
@@ -0,0 +1,31 @@
1
+ import { fromString } from '../../../../node_modules/uint8arrays/dist/src/from-string.js';
2
+ import '../../../../node_modules/multiformats/dist/src/bases/base10.js';
3
+ import '../../../../node_modules/multiformats/dist/src/bases/base16.js';
4
+ import '../../../../node_modules/multiformats/dist/src/bases/base2.js';
5
+ import '../../../../node_modules/multiformats/dist/src/bases/base256emoji.js';
6
+ import '../../../../node_modules/multiformats/dist/src/bases/base32.js';
7
+ import '../../../../node_modules/multiformats/dist/src/bases/base36.js';
8
+ import '../../../../node_modules/multiformats/dist/src/bases/base58.js';
9
+ import '../../../../node_modules/multiformats/dist/src/bases/base64.js';
10
+ import '../../../../node_modules/multiformats/dist/src/bases/base8.js';
11
+ import '../../../../node_modules/multiformats/dist/src/bases/identity.js';
12
+ import '../../../../node_modules/multiformats/dist/src/codecs/json.js';
13
+
14
+ /**
15
+ * Convert input to a byte array.
16
+ *
17
+ * Handles both `0x` prefixed and non-prefixed strings.
18
+ */
19
+ function hexToBytes(hex) {
20
+ if (typeof hex === "string") {
21
+ const _hex = hex.replace(/^0x/i, "");
22
+ return fromString(_hex.toLowerCase(), "base16");
23
+ }
24
+ return hex;
25
+ }
26
+ /**
27
+ * Encode utf-8 string to byte array.
28
+ */
29
+ const utf8ToBytes = (s) => fromString(s, "utf8");
30
+
31
+ export { hexToBytes, utf8ToBytes };
Binary file
Binary file
@@ -0,0 +1,13 @@
1
+ declare const verificationKey: {
2
+ protocol: string;
3
+ curve: string;
4
+ nPublic: number;
5
+ vk_alpha_1: string[];
6
+ vk_beta_2: string[][];
7
+ vk_gamma_2: string[][];
8
+ vk_delta_2: string[][];
9
+ vk_alphabeta_12: string[][][];
10
+ IC: string[][];
11
+ };
12
+
13
+ export default verificationKey;