@waku/rln 0.1.5-ff0222a.0 → 0.1.6-b7e9b08.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 (158) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/bundle/_virtual/index2.js +1 -1
  3. package/bundle/index.js +2 -2
  4. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/checksum.js +3 -3
  5. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/cipher.js +4 -4
  6. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/class.js +7 -7
  7. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/functional.js +7 -7
  8. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/index.js +6 -6
  9. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/kdf.js +5 -5
  10. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/password.js +1 -1
  11. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/schema-validation-generated.js +1 -1
  12. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/schema-validation.js +2 -2
  13. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/types.js +1 -1
  14. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/_assert.js +1 -1
  15. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/_sha2.js +3 -3
  16. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/_u64.js +1 -1
  17. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/cryptoBrowser.js +1 -1
  18. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/hmac.js +3 -3
  19. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/pbkdf2.js +4 -4
  20. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/scrypt.js +5 -5
  21. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/sha256.js +3 -3
  22. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/sha512.js +4 -4
  23. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/utils.js +2 -2
  24. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/aes.js +3 -3
  25. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/pbkdf2.js +7 -7
  26. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/random.js +3 -3
  27. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/scrypt.js +3 -3
  28. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/sha256.js +3 -3
  29. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/utils.js +7 -7
  30. package/bundle/node_modules/@ethersproject/abi/lib.esm/_version.js +1 -1
  31. package/bundle/node_modules/@ethersproject/abstract-provider/lib.esm/_version.js +1 -1
  32. package/bundle/node_modules/@ethersproject/abstract-signer/lib.esm/_version.js +1 -1
  33. package/bundle/node_modules/@ethersproject/address/lib.esm/_version.js +1 -1
  34. package/bundle/node_modules/@ethersproject/bignumber/lib.esm/_version.js +1 -1
  35. package/bundle/node_modules/@ethersproject/contracts/lib.esm/_version.js +1 -1
  36. package/bundle/node_modules/@ethersproject/contracts/lib.esm/index.js +1 -1
  37. package/bundle/node_modules/@ethersproject/hash/lib.esm/_version.js +1 -1
  38. package/bundle/node_modules/@ethersproject/networks/lib.esm/_version.js +1 -1
  39. package/bundle/node_modules/@ethersproject/networks/lib.esm/index.js +19 -1
  40. package/bundle/node_modules/@ethersproject/properties/lib.esm/_version.js +1 -1
  41. package/bundle/node_modules/@ethersproject/providers/lib.esm/_version.js +1 -1
  42. package/bundle/node_modules/@ethersproject/signing-key/lib.esm/_version.js +1 -1
  43. package/bundle/node_modules/@ethersproject/signing-key/lib.esm/elliptic.js +58 -10
  44. package/bundle/node_modules/@ethersproject/strings/lib.esm/_version.js +1 -1
  45. package/bundle/node_modules/@ethersproject/transactions/lib.esm/_version.js +1 -1
  46. package/bundle/node_modules/@ethersproject/web/lib.esm/_version.js +1 -1
  47. package/bundle/node_modules/@noble/hashes/esm/_assert.js +6 -32
  48. package/bundle/node_modules/@noble/hashes/esm/_md.js +22 -11
  49. package/bundle/node_modules/@noble/hashes/esm/_u64.js +4 -3
  50. package/bundle/{packages/rln/node_modules → node_modules}/@noble/hashes/esm/hmac.js +19 -10
  51. package/bundle/{packages/rln/node_modules/@noble/hashes/esm/sha256.js → node_modules/@noble/hashes/esm/sha2.js} +36 -50
  52. package/bundle/node_modules/@noble/hashes/esm/sha256.js +5 -102
  53. package/bundle/node_modules/@noble/hashes/esm/sha3.js +30 -24
  54. package/bundle/node_modules/@noble/hashes/esm/utils.js +69 -18
  55. package/bundle/node_modules/bn.js/lib/bn.js +1 -0
  56. package/bundle/node_modules/ethereum-cryptography/esm/sha256.js +1 -1
  57. package/bundle/packages/core/dist/lib/connection_manager/connection_manager.js +3 -0
  58. package/bundle/packages/core/dist/lib/connection_manager/keep_alive_manager.js +3 -3
  59. package/bundle/packages/core/dist/lib/filter/filter.js +3 -0
  60. package/bundle/packages/core/dist/lib/light_push/light_push.js +3 -0
  61. package/bundle/packages/core/dist/lib/metadata/metadata.js +3 -0
  62. package/bundle/packages/core/dist/lib/store/store.js +3 -3
  63. package/bundle/packages/proto/dist/generated/sds_message.js +59 -3
  64. package/bundle/packages/rln/dist/codec.js +3 -3
  65. package/bundle/packages/rln/dist/contract/constants.js +1 -1
  66. package/bundle/packages/rln/dist/contract/{rln_light_contract.js → rln_base_contract.js} +180 -180
  67. package/bundle/packages/rln/dist/contract/rln_contract.js +12 -422
  68. package/bundle/packages/rln/dist/contract/types.js +9 -0
  69. package/bundle/packages/rln/dist/create.js +1 -1
  70. package/bundle/packages/rln/dist/credentials_manager.js +215 -0
  71. package/bundle/packages/rln/dist/identity.js +0 -9
  72. package/bundle/packages/rln/dist/keystore/cipher.js +3 -3
  73. package/bundle/packages/rln/dist/keystore/keystore.js +32 -18
  74. package/bundle/packages/rln/dist/rln.js +59 -169
  75. package/bundle/packages/rln/dist/utils/epoch.js +3 -3
  76. package/bundle/packages/rln/dist/zerokit.js +5 -5
  77. package/bundle/packages/utils/dist/common/sharding/index.js +4 -4
  78. package/bundle/packages/utils/node_modules/@waku/interfaces/dist/connection_manager.js +19 -0
  79. package/bundle/packages/utils/node_modules/@waku/interfaces/dist/health_indicator.js +12 -0
  80. package/bundle/packages/utils/node_modules/@waku/interfaces/dist/protocols.js +92 -0
  81. package/dist/.tsbuildinfo +1 -1
  82. package/dist/contract/constants.d.ts +1 -1
  83. package/dist/contract/constants.js +1 -1
  84. package/dist/contract/constants.js.map +1 -1
  85. package/dist/contract/{rln_light_contract.d.ts → rln_base_contract.d.ts} +25 -53
  86. package/dist/contract/{rln_light_contract.js → rln_base_contract.js} +177 -177
  87. package/dist/contract/rln_base_contract.js.map +1 -0
  88. package/dist/contract/rln_contract.d.ts +5 -122
  89. package/dist/contract/rln_contract.js +8 -417
  90. package/dist/contract/rln_contract.js.map +1 -1
  91. package/dist/contract/types.d.ts +40 -0
  92. package/dist/contract/types.js +8 -0
  93. package/dist/contract/types.js.map +1 -0
  94. package/dist/create.js +1 -1
  95. package/dist/create.js.map +1 -1
  96. package/dist/credentials_manager.d.ts +44 -0
  97. package/dist/credentials_manager.js +197 -0
  98. package/dist/credentials_manager.js.map +1 -0
  99. package/dist/identity.d.ts +0 -1
  100. package/dist/identity.js +0 -9
  101. package/dist/identity.js.map +1 -1
  102. package/dist/index.d.ts +3 -3
  103. package/dist/index.js +3 -3
  104. package/dist/index.js.map +1 -1
  105. package/dist/keystore/keystore.d.ts +1 -0
  106. package/dist/keystore/keystore.js +27 -13
  107. package/dist/keystore/keystore.js.map +1 -1
  108. package/dist/rln.d.ts +9 -52
  109. package/dist/rln.js +54 -163
  110. package/dist/rln.js.map +1 -1
  111. package/dist/types.d.ts +27 -0
  112. package/dist/types.js +2 -0
  113. package/dist/types.js.map +1 -0
  114. package/dist/zerokit.d.ts +3 -3
  115. package/dist/zerokit.js +5 -5
  116. package/dist/zerokit.js.map +1 -1
  117. package/package.json +1 -1
  118. package/src/contract/constants.ts +1 -1
  119. package/src/contract/{rln_light_contract.ts → rln_base_contract.ts} +294 -312
  120. package/src/contract/rln_contract.ts +9 -663
  121. package/src/contract/types.ts +48 -0
  122. package/src/create.ts +1 -1
  123. package/src/credentials_manager.ts +282 -0
  124. package/src/identity.ts +0 -10
  125. package/src/index.ts +4 -4
  126. package/src/keystore/keystore.ts +49 -25
  127. package/src/rln.ts +67 -258
  128. package/src/types.ts +31 -0
  129. package/src/zerokit.ts +3 -3
  130. package/bundle/packages/rln/dist/rln_light.js +0 -149
  131. package/bundle/packages/rln/node_modules/@noble/hashes/esm/_assert.js +0 -43
  132. package/bundle/packages/rln/node_modules/@noble/hashes/esm/_sha2.js +0 -116
  133. package/bundle/packages/rln/node_modules/@noble/hashes/esm/utils.js +0 -43
  134. package/dist/contract/rln_light_contract.js.map +0 -1
  135. package/dist/rln_light.d.ts +0 -64
  136. package/dist/rln_light.js +0 -144
  137. package/dist/rln_light.js.map +0 -1
  138. package/src/rln_light.ts +0 -235
  139. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/index.js +0 -0
  140. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/md5.js +0 -0
  141. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/nil.js +0 -0
  142. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/parse.js +0 -0
  143. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/regex.js +0 -0
  144. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/rng.js +0 -0
  145. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/sha1.js +0 -0
  146. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/stringify.js +0 -0
  147. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v1.js +0 -0
  148. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v3.js +0 -0
  149. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v35.js +0 -0
  150. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v4.js +0 -0
  151. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v5.js +0 -0
  152. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/validate.js +0 -0
  153. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/version.js +0 -0
  154. /package/bundle/{packages/rln/node_modules → node_modules}/uuid/dist/esm-browser/native.js +0 -0
  155. /package/bundle/{packages/rln/node_modules → node_modules}/uuid/dist/esm-browser/rng.js +0 -0
  156. /package/bundle/{packages/rln/node_modules → node_modules}/uuid/dist/esm-browser/stringify.js +0 -0
  157. /package/bundle/{packages/rln/node_modules → node_modules}/uuid/dist/esm-browser/v4.js +0 -0
  158. /package/bundle/packages/{interfaces → utils/node_modules/@waku/interfaces}/dist/constants.js +0 -0
@@ -103,12 +103,15 @@ utils.encode = utils_1.encode;
103
103
  // Represent num in a w-NAF form
104
104
  function getNAF(num, w, bits) {
105
105
  var naf = new Array(Math.max(num.bitLength(), bits) + 1);
106
- naf.fill(0);
106
+ var i;
107
+ for (i = 0; i < naf.length; i += 1) {
108
+ naf[i] = 0;
109
+ }
107
110
 
108
111
  var ws = 1 << (w + 1);
109
112
  var k = num.clone();
110
113
 
111
- for (var i = 0; i < naf.length; i++) {
114
+ for (i = 0; i < naf.length; i++) {
112
115
  var z;
113
116
  var mod = k.andln(ws - 1);
114
117
  if (k.isOdd()) {
@@ -1994,8 +1997,8 @@ KeyPair.prototype.sign = function sign(msg, enc, options) {
1994
1997
  return this.ec.sign(msg, this, enc, options);
1995
1998
  };
1996
1999
 
1997
- KeyPair.prototype.verify = function verify(msg, signature) {
1998
- return this.ec.verify(msg, signature, this);
2000
+ KeyPair.prototype.verify = function verify(msg, signature, options) {
2001
+ return this.ec.verify(msg, signature, this, undefined, options);
1999
2002
  };
2000
2003
 
2001
2004
  KeyPair.prototype.inspect = function inspect() {
@@ -2041,6 +2044,10 @@ function getLength(buf, p) {
2041
2044
  return false;
2042
2045
  }
2043
2046
 
2047
+ if(buf[p.place] === 0x00) {
2048
+ return false;
2049
+ }
2050
+
2044
2051
  var val = 0;
2045
2052
  for (var i = 0, off = p.place; i < octetLen; i++, off++) {
2046
2053
  val <<= 8;
@@ -2089,6 +2096,9 @@ Signature.prototype._importDER = function _importDER(data, enc) {
2089
2096
  if (rlen === false) {
2090
2097
  return false;
2091
2098
  }
2099
+ if ((data[p.place] & 128) !== 0) {
2100
+ return false;
2101
+ }
2092
2102
  var r = data.slice(p.place, rlen + p.place);
2093
2103
  p.place += rlen;
2094
2104
  if (data[p.place++] !== 0x02) {
@@ -2101,6 +2111,9 @@ Signature.prototype._importDER = function _importDER(data, enc) {
2101
2111
  if (data.length !== slen + p.place) {
2102
2112
  return false;
2103
2113
  }
2114
+ if ((data[p.place] & 128) !== 0) {
2115
+ return false;
2116
+ }
2104
2117
  var s = data.slice(p.place, slen + p.place);
2105
2118
  if (r[0] === 0) {
2106
2119
  if (r[1] & 0x80) {
@@ -2246,8 +2259,27 @@ EC.prototype.genKeyPair = function genKeyPair(options) {
2246
2259
  }
2247
2260
  };
2248
2261
 
2249
- EC.prototype._truncateToN = function _truncateToN(msg, truncOnly) {
2250
- var delta = msg.byteLength() * 8 - this.n.bitLength();
2262
+ EC.prototype._truncateToN = function _truncateToN(msg, truncOnly, bitLength) {
2263
+ var byteLength;
2264
+ if (BN.isBN(msg) || typeof msg === 'number') {
2265
+ msg = new BN(msg, 16);
2266
+ byteLength = msg.byteLength();
2267
+ } else if (typeof msg === 'object') {
2268
+ // BN assumes an array-like input and asserts length
2269
+ byteLength = msg.length;
2270
+ msg = new BN(msg, 16);
2271
+ } else {
2272
+ // BN converts the value to string
2273
+ var str = msg.toString();
2274
+ // HEX encoding
2275
+ byteLength = (str.length + 1) >>> 1;
2276
+ msg = new BN(str, 16);
2277
+ }
2278
+ // Allow overriding
2279
+ if (typeof bitLength !== 'number') {
2280
+ bitLength = byteLength * 8;
2281
+ }
2282
+ var delta = bitLength - this.n.bitLength();
2251
2283
  if (delta > 0)
2252
2284
  msg = msg.ushrn(delta);
2253
2285
  if (!truncOnly && msg.cmp(this.n) >= 0)
@@ -2264,8 +2296,18 @@ EC.prototype.sign = function sign(msg, key, enc, options) {
2264
2296
  if (!options)
2265
2297
  options = {};
2266
2298
 
2299
+ if (typeof msg !== 'string' && typeof msg !== 'number' && !BN.isBN(msg)) {
2300
+ assert$5(typeof msg === 'object' && msg && typeof msg.length === 'number',
2301
+ 'Expected message to be an array-like, a hex string, or a BN instance');
2302
+ assert$5((msg.length >>> 0) === msg.length); // non-negative 32-bit integer
2303
+ for (var i = 0; i < msg.length; i++) assert$5((msg[i] & 255) === msg[i]);
2304
+ }
2305
+
2267
2306
  key = this.keyFromPrivate(key, enc);
2268
- msg = this._truncateToN(new BN(msg, 16));
2307
+ msg = this._truncateToN(msg, false, options.msgBitLength);
2308
+
2309
+ // Would fail further checks, but let's make the error message clear
2310
+ assert$5(!msg.isNeg(), 'Can not sign a negative message');
2269
2311
 
2270
2312
  // Zero-extend key to provide enough entropy
2271
2313
  var bytes = this.n.byteLength();
@@ -2274,6 +2316,9 @@ EC.prototype.sign = function sign(msg, key, enc, options) {
2274
2316
  // Zero-extend nonce to have the same byte size as N
2275
2317
  var nonce = msg.toArray('be', bytes);
2276
2318
 
2319
+ // Recheck nonce to be bijective to msg
2320
+ assert$5((new BN(nonce)).eq(msg), 'Can not sign message');
2321
+
2277
2322
  // Instantiate Hmac_DRBG
2278
2323
  var drbg = new hmacDrbg({
2279
2324
  hash: this.hash,
@@ -2321,8 +2366,11 @@ EC.prototype.sign = function sign(msg, key, enc, options) {
2321
2366
  }
2322
2367
  };
2323
2368
 
2324
- EC.prototype.verify = function verify(msg, signature$1, key, enc) {
2325
- msg = this._truncateToN(new BN(msg, 16));
2369
+ EC.prototype.verify = function verify(msg, signature$1, key, enc, options) {
2370
+ if (!options)
2371
+ options = {};
2372
+
2373
+ msg = this._truncateToN(msg, false, options.msgBitLength);
2326
2374
  key = this.keyFromPublic(key, enc);
2327
2375
  signature$1 = new signature(signature$1, 'hex');
2328
2376
 
@@ -2414,7 +2462,7 @@ var elliptic_1 = createCommonjsModule(function (module, exports) {
2414
2462
 
2415
2463
  var elliptic = exports;
2416
2464
 
2417
- elliptic.version = /*RicMoo:ethers*/{ version: "6.5.4" }.version;
2465
+ elliptic.version = /*RicMoo:ethers*/{ version: "6.6.1" }.version;
2418
2466
  elliptic.utils = utils_1$1;
2419
2467
  elliptic.rand = /*RicMoo:ethers:require(brorand)*/(function() { throw new Error('unsupported'); });
2420
2468
  elliptic.curve = curve_1;
@@ -1,3 +1,3 @@
1
- const version = "strings/5.7.0";
1
+ const version = "strings/5.8.0";
2
2
 
3
3
  export { version };
@@ -1,3 +1,3 @@
1
- const version = "transactions/5.7.0";
1
+ const version = "transactions/5.8.0";
2
2
 
3
3
  export { version };
@@ -1,3 +1,3 @@
1
- const version = "web/5.7.1";
1
+ const version = "web/5.8.0";
2
2
 
3
3
  export { version };
@@ -1,37 +1,11 @@
1
+ import { abytes as abytes$1 } from './utils.js';
2
+
1
3
  /**
2
4
  * Internal assertion helpers.
3
5
  * @module
6
+ * @deprecated
4
7
  */
5
- /** Asserts something is positive integer. */
6
- function anumber(n) {
7
- if (!Number.isSafeInteger(n) || n < 0)
8
- throw new Error('positive integer expected, got ' + n);
9
- }
10
- /** Is number an Uint8Array? Copied from utils for perf. */
11
- function isBytes(a) {
12
- return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
13
- }
14
- /** Asserts something is Uint8Array. */
15
- function abytes(b, ...lengths) {
16
- if (!isBytes(b))
17
- throw new Error('Uint8Array expected');
18
- if (lengths.length > 0 && !lengths.includes(b.length))
19
- throw new Error('Uint8Array expected of length ' + lengths + ', got length=' + b.length);
20
- }
21
- /** Asserts a hash instance has not been destroyed / finished */
22
- function aexists(instance, checkFinished = true) {
23
- if (instance.destroyed)
24
- throw new Error('Hash instance has been destroyed');
25
- if (checkFinished && instance.finished)
26
- throw new Error('Hash#digest() has already been called');
27
- }
28
- /** Asserts output is properly-sized byte array */
29
- function aoutput(out, instance) {
30
- abytes(out);
31
- const min = instance.outputLen;
32
- if (out.length < min) {
33
- throw new Error('digestInto() expects output buffer of length at least ' + min);
34
- }
35
- }
8
+ /** @deprecated Use import from `noble/hashes/utils` module */
9
+ const abytes = abytes$1;
36
10
 
37
- export { abytes, aexists, anumber, aoutput };
11
+ export { abytes };
@@ -1,5 +1,4 @@
1
- import { aexists, aoutput } from './_assert.js';
2
- import { Hash, createView, toBytes } from './utils.js';
1
+ import { Hash, createView, aexists, toBytes, abytes, aoutput, clean } from './utils.js';
3
2
 
4
3
  /**
5
4
  * Internal Merkle-Damgard hash utils.
@@ -33,21 +32,22 @@ function Maj(a, b, c) {
33
32
  class HashMD extends Hash {
34
33
  constructor(blockLen, outputLen, padOffset, isLE) {
35
34
  super();
36
- this.blockLen = blockLen;
37
- this.outputLen = outputLen;
38
- this.padOffset = padOffset;
39
- this.isLE = isLE;
40
35
  this.finished = false;
41
36
  this.length = 0;
42
37
  this.pos = 0;
43
38
  this.destroyed = false;
39
+ this.blockLen = blockLen;
40
+ this.outputLen = outputLen;
41
+ this.padOffset = padOffset;
42
+ this.isLE = isLE;
44
43
  this.buffer = new Uint8Array(blockLen);
45
44
  this.view = createView(this.buffer);
46
45
  }
47
46
  update(data) {
48
47
  aexists(this);
49
- const { view, buffer, blockLen } = this;
50
48
  data = toBytes(data);
49
+ abytes(data);
50
+ const { view, buffer, blockLen } = this;
51
51
  const len = data.length;
52
52
  for (let pos = 0; pos < len;) {
53
53
  const take = Math.min(blockLen - this.pos, len - pos);
@@ -81,7 +81,7 @@ class HashMD extends Hash {
81
81
  let { pos } = this;
82
82
  // append the bit '1' to the message
83
83
  buffer[pos++] = 0b10000000;
84
- this.buffer.subarray(pos).fill(0);
84
+ clean(this.buffer.subarray(pos));
85
85
  // we have less than padOffset left in buffer, so we cannot put length in
86
86
  // current block, need process it and pad again
87
87
  if (this.padOffset > blockLen - pos) {
@@ -119,14 +119,25 @@ class HashMD extends Hash {
119
119
  to || (to = new this.constructor());
120
120
  to.set(...this.get());
121
121
  const { blockLen, buffer, length, finished, destroyed, pos } = this;
122
+ to.destroyed = destroyed;
123
+ to.finished = finished;
122
124
  to.length = length;
123
125
  to.pos = pos;
124
- to.finished = finished;
125
- to.destroyed = destroyed;
126
126
  if (length % blockLen)
127
127
  to.buffer.set(buffer);
128
128
  return to;
129
129
  }
130
+ clone() {
131
+ return this._cloneInto();
132
+ }
130
133
  }
134
+ /**
135
+ * Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
136
+ * Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
137
+ */
138
+ /** Initial SHA256 state. Bits 0..32 of frac part of sqrt of primes 2..19 */
139
+ const SHA256_IV = /* @__PURE__ */ Uint32Array.from([
140
+ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
141
+ ]);
131
142
 
132
- export { Chi, HashMD, Maj, setBigUint64 };
143
+ export { Chi, HashMD, Maj, SHA256_IV, setBigUint64 };
@@ -11,9 +11,10 @@ function fromBig(n, le = false) {
11
11
  return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
12
12
  }
13
13
  function split(lst, le = false) {
14
- let Ah = new Uint32Array(lst.length);
15
- let Al = new Uint32Array(lst.length);
16
- for (let i = 0; i < lst.length; i++) {
14
+ const len = lst.length;
15
+ let Ah = new Uint32Array(len);
16
+ let Al = new Uint32Array(len);
17
+ for (let i = 0; i < len; i++) {
17
18
  const { h, l } = fromBig(lst[i], le);
18
19
  [Ah[i], Al[i]] = [h, l];
19
20
  }
@@ -1,17 +1,19 @@
1
- import assert from './_assert.js';
2
- import { Hash, toBytes } from './utils.js';
1
+ import { Hash, ahash, toBytes, clean, aexists, abytes } from './utils.js';
3
2
 
4
- // HMAC (RFC 2104)
3
+ /**
4
+ * HMAC: RFC2104 message authentication code.
5
+ * @module
6
+ */
5
7
  class HMAC extends Hash {
6
8
  constructor(hash, _key) {
7
9
  super();
8
10
  this.finished = false;
9
11
  this.destroyed = false;
10
- assert.hash(hash);
12
+ ahash(hash);
11
13
  const key = toBytes(_key);
12
14
  this.iHash = hash.create();
13
15
  if (typeof this.iHash.update !== 'function')
14
- throw new TypeError('Expected instance of class which extends utils.Hash');
16
+ throw new Error('Expected instance of class which extends utils.Hash');
15
17
  this.blockLen = this.iHash.blockLen;
16
18
  this.outputLen = this.iHash.outputLen;
17
19
  const blockLen = this.blockLen;
@@ -27,16 +29,16 @@ class HMAC extends Hash {
27
29
  for (let i = 0; i < pad.length; i++)
28
30
  pad[i] ^= 0x36 ^ 0x5c;
29
31
  this.oHash.update(pad);
30
- pad.fill(0);
32
+ clean(pad);
31
33
  }
32
34
  update(buf) {
33
- assert.exists(this);
35
+ aexists(this);
34
36
  this.iHash.update(buf);
35
37
  return this;
36
38
  }
37
39
  digestInto(out) {
38
- assert.exists(this);
39
- assert.bytes(out, this.outputLen);
40
+ aexists(this);
41
+ abytes(out, this.outputLen);
40
42
  this.finished = true;
41
43
  this.iHash.digestInto(out);
42
44
  this.oHash.update(out);
@@ -61,6 +63,9 @@ class HMAC extends Hash {
61
63
  to.iHash = iHash._cloneInto(to.iHash);
62
64
  return to;
63
65
  }
66
+ clone() {
67
+ return this._cloneInto();
68
+ }
64
69
  destroy() {
65
70
  this.destroyed = true;
66
71
  this.oHash.destroy();
@@ -72,8 +77,12 @@ class HMAC extends Hash {
72
77
  * @param hash - function that would be used e.g. sha256
73
78
  * @param key - message key
74
79
  * @param message - message data
80
+ * @example
81
+ * import { hmac } from '@noble/hashes/hmac';
82
+ * import { sha256 } from '@noble/hashes/sha2';
83
+ * const mac1 = hmac(sha256, 'key', 'message');
75
84
  */
76
85
  const hmac = (hash, key, message) => new HMAC(hash, key).update(message).digest();
77
86
  hmac.create = (hash, key) => new HMAC(hash, key);
78
87
 
79
- export { hmac };
88
+ export { HMAC, hmac };
@@ -1,14 +1,19 @@
1
- import { SHA2 } from './_sha2.js';
2
- import { wrapConstructor, rotr } from './utils.js';
1
+ import { HashMD, SHA256_IV, Chi, Maj } from './_md.js';
2
+ import { createHasher, clean, rotr } from './utils.js';
3
3
 
4
- // Choice: a ? b : c
5
- const Chi = (a, b, c) => (a & b) ^ (~a & c);
6
- // Majority function, true if any two inpust is true
7
- const Maj = (a, b, c) => (a & b) ^ (a & c) ^ (b & c);
8
- // Round constants:
9
- // first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311)
4
+ /**
5
+ * SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
6
+ * SHA256 is the fastest hash implementable in JS, even faster than Blake3.
7
+ * Check out [RFC 4634](https://datatracker.ietf.org/doc/html/rfc4634) and
8
+ * [FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
9
+ * @module
10
+ */
11
+ /**
12
+ * Round constants:
13
+ * First 32 bits of fractional parts of the cube roots of the first 64 primes 2..311)
14
+ */
10
15
  // prettier-ignore
11
- const SHA256_K = new Uint32Array([
16
+ const SHA256_K = /* @__PURE__ */ Uint32Array.from([
12
17
  0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
13
18
  0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
14
19
  0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
@@ -18,27 +23,21 @@ const SHA256_K = new Uint32Array([
18
23
  0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
19
24
  0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
20
25
  ]);
21
- // Initial state (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
22
- // prettier-ignore
23
- const IV = new Uint32Array([
24
- 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
25
- ]);
26
- // Temporary buffer, not used to store anything between runs
27
- // Named this way because it matches specification.
28
- const SHA256_W = new Uint32Array(64);
29
- class SHA256 extends SHA2 {
30
- constructor() {
31
- super(64, 32, 8, false);
26
+ /** Reusable temporary buffer. "W" comes straight from spec. */
27
+ const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
28
+ class SHA256 extends HashMD {
29
+ constructor(outputLen = 32) {
30
+ super(64, outputLen, 8, false);
32
31
  // We cannot use array here since array allows indexing by variable
33
32
  // which means optimizer/compiler cannot use registers.
34
- this.A = IV[0] | 0;
35
- this.B = IV[1] | 0;
36
- this.C = IV[2] | 0;
37
- this.D = IV[3] | 0;
38
- this.E = IV[4] | 0;
39
- this.F = IV[5] | 0;
40
- this.G = IV[6] | 0;
41
- this.H = IV[7] | 0;
33
+ this.A = SHA256_IV[0] | 0;
34
+ this.B = SHA256_IV[1] | 0;
35
+ this.C = SHA256_IV[2] | 0;
36
+ this.D = SHA256_IV[3] | 0;
37
+ this.E = SHA256_IV[4] | 0;
38
+ this.F = SHA256_IV[5] | 0;
39
+ this.G = SHA256_IV[6] | 0;
40
+ this.H = SHA256_IV[7] | 0;
42
41
  }
43
42
  get() {
44
43
  const { A, B, C, D, E, F, G, H } = this;
@@ -94,33 +93,20 @@ class SHA256 extends SHA2 {
94
93
  this.set(A, B, C, D, E, F, G, H);
95
94
  }
96
95
  roundClean() {
97
- SHA256_W.fill(0);
96
+ clean(SHA256_W);
98
97
  }
99
98
  destroy() {
100
99
  this.set(0, 0, 0, 0, 0, 0, 0, 0);
101
- this.buffer.fill(0);
102
- }
103
- }
104
- // Constants from https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
105
- class SHA224 extends SHA256 {
106
- constructor() {
107
- super();
108
- this.A = 0xc1059ed8 | 0;
109
- this.B = 0x367cd507 | 0;
110
- this.C = 0x3070dd17 | 0;
111
- this.D = 0xf70e5939 | 0;
112
- this.E = 0xffc00b31 | 0;
113
- this.F = 0x68581511 | 0;
114
- this.G = 0x64f98fa7 | 0;
115
- this.H = 0xbefa4fa4 | 0;
116
- this.outputLen = 28;
100
+ clean(this.buffer);
117
101
  }
118
102
  }
119
103
  /**
120
- * SHA2-256 hash function
121
- * @param message - data that would be hashed
104
+ * SHA2-256 hash function from RFC 4634.
105
+ *
106
+ * It is the fastest JS hash, even faster than Blake3.
107
+ * To break sha256 using birthday attack, attackers need to try 2^128 hashes.
108
+ * BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
122
109
  */
123
- const sha256 = wrapConstructor(() => new SHA256());
124
- wrapConstructor(() => new SHA224());
110
+ const sha256 = /* @__PURE__ */ createHasher(() => new SHA256());
125
111
 
126
- export { sha256 };
112
+ export { SHA256, sha256 };
@@ -1,5 +1,4 @@
1
- import { HashMD, Chi, Maj } from './_md.js';
2
- import { wrapConstructor, rotr } from './utils.js';
1
+ import { sha256 as sha256$1 } from './sha2.js';
3
2
 
4
3
  /**
5
4
  * SHA2-256 a.k.a. sha256. In JS, it is the fastest hash, even faster than Blake3.
@@ -9,105 +8,9 @@ import { wrapConstructor, rotr } from './utils.js';
9
8
  *
10
9
  * Check out [FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
11
10
  * @module
11
+ * @deprecated
12
12
  */
13
- /** Round constants: first 32 bits of fractional parts of the cube roots of the first 64 primes 2..311). */
14
- // prettier-ignore
15
- const SHA256_K = /* @__PURE__ */ new Uint32Array([
16
- 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
17
- 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
18
- 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
19
- 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
20
- 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
21
- 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
22
- 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
23
- 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
24
- ]);
25
- /** Initial state: first 32 bits of fractional parts of the square roots of the first 8 primes 2..19. */
26
- // prettier-ignore
27
- const SHA256_IV = /* @__PURE__ */ new Uint32Array([
28
- 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
29
- ]);
30
- /**
31
- * Temporary buffer, not used to store anything between runs.
32
- * Named this way because it matches specification.
33
- */
34
- const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
35
- class SHA256 extends HashMD {
36
- constructor() {
37
- super(64, 32, 8, false);
38
- // We cannot use array here since array allows indexing by variable
39
- // which means optimizer/compiler cannot use registers.
40
- this.A = SHA256_IV[0] | 0;
41
- this.B = SHA256_IV[1] | 0;
42
- this.C = SHA256_IV[2] | 0;
43
- this.D = SHA256_IV[3] | 0;
44
- this.E = SHA256_IV[4] | 0;
45
- this.F = SHA256_IV[5] | 0;
46
- this.G = SHA256_IV[6] | 0;
47
- this.H = SHA256_IV[7] | 0;
48
- }
49
- get() {
50
- const { A, B, C, D, E, F, G, H } = this;
51
- return [A, B, C, D, E, F, G, H];
52
- }
53
- // prettier-ignore
54
- set(A, B, C, D, E, F, G, H) {
55
- this.A = A | 0;
56
- this.B = B | 0;
57
- this.C = C | 0;
58
- this.D = D | 0;
59
- this.E = E | 0;
60
- this.F = F | 0;
61
- this.G = G | 0;
62
- this.H = H | 0;
63
- }
64
- process(view, offset) {
65
- // Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
66
- for (let i = 0; i < 16; i++, offset += 4)
67
- SHA256_W[i] = view.getUint32(offset, false);
68
- for (let i = 16; i < 64; i++) {
69
- const W15 = SHA256_W[i - 15];
70
- const W2 = SHA256_W[i - 2];
71
- const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
72
- const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
73
- SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
74
- }
75
- // Compression function main loop, 64 rounds
76
- let { A, B, C, D, E, F, G, H } = this;
77
- for (let i = 0; i < 64; i++) {
78
- const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
79
- const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
80
- const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
81
- const T2 = (sigma0 + Maj(A, B, C)) | 0;
82
- H = G;
83
- G = F;
84
- F = E;
85
- E = (D + T1) | 0;
86
- D = C;
87
- C = B;
88
- B = A;
89
- A = (T1 + T2) | 0;
90
- }
91
- // Add the compressed chunk to the current hash value
92
- A = (A + this.A) | 0;
93
- B = (B + this.B) | 0;
94
- C = (C + this.C) | 0;
95
- D = (D + this.D) | 0;
96
- E = (E + this.E) | 0;
97
- F = (F + this.F) | 0;
98
- G = (G + this.G) | 0;
99
- H = (H + this.H) | 0;
100
- this.set(A, B, C, D, E, F, G, H);
101
- }
102
- roundClean() {
103
- SHA256_W.fill(0);
104
- }
105
- destroy() {
106
- this.set(0, 0, 0, 0, 0, 0, 0, 0);
107
- this.buffer.fill(0);
108
- }
109
- }
110
- /** SHA2-256 hash function */
111
- const sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
13
+ /** @deprecated Use import from `noble/hashes/sha2` module */
14
+ const sha256 = sha256$1;
112
15
 
113
- export { SHA256, sha256 };
16
+ export { sha256 };