@waku/rln 0.1.6-16328a3.0 → 0.1.6-383e0b2.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.
- package/bundle/_virtual/index2.js +1 -1
- package/bundle/_virtual/utils.js +2 -2
- package/bundle/_virtual/utils2.js +2 -2
- package/bundle/index.js +1 -2
- package/bundle/node_modules/@ethersproject/keccak256/lib.esm/index.js +1 -1
- package/bundle/node_modules/{js-sha3 → @ethersproject/keccak256/node_modules/js-sha3}/src/sha3.js +2 -2
- package/bundle/node_modules/@noble/hashes/esm/_assert.js +32 -6
- package/bundle/node_modules/@noble/hashes/esm/_md.js +11 -22
- package/bundle/node_modules/@noble/hashes/esm/_u64.js +3 -4
- package/bundle/node_modules/@noble/hashes/esm/sha256.js +102 -5
- package/bundle/node_modules/@noble/hashes/esm/sha3.js +24 -30
- package/bundle/node_modules/@noble/hashes/esm/utils.js +18 -69
- package/bundle/node_modules/bn.js/lib/bn.js +0 -1
- package/bundle/node_modules/ethereum-cryptography/esm/sha256.js +1 -1
- package/bundle/packages/rln/dist/contract/constants.js +0 -1
- package/bundle/packages/rln/dist/contract/rln_base_contract.js +71 -127
- package/bundle/packages/rln/dist/credentials_manager.js +3 -3
- package/bundle/packages/rln/dist/keystore/cipher.js +3 -3
- package/bundle/packages/rln/dist/keystore/keystore.js +1 -1
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/checksum.js +3 -3
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/cipher.js +4 -4
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/class.js +7 -7
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/functional.js +7 -7
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/index.js +6 -6
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/kdf.js +5 -5
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/password.js +1 -1
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/schema-validation-generated.js +1 -1
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/schema-validation.js +2 -2
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/types.js +1 -1
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/aes.js +3 -3
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/pbkdf2.js +7 -7
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/random.js +3 -3
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/scrypt.js +3 -3
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/sha256.js +3 -3
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/utils.js +7 -7
- package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/_assert.js +1 -1
- package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/_sha2.js +3 -3
- package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/_u64.js +1 -1
- package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/cryptoBrowser.js +1 -1
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/_assert.js +43 -0
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/_sha2.js +116 -0
- package/bundle/{node_modules → packages/rln/node_modules}/@noble/hashes/esm/hmac.js +10 -19
- package/bundle/{node_modules/@noble/hashes/esm/sha2.js → packages/rln/node_modules/@noble/hashes/esm/sha256.js} +50 -36
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/utils.js +43 -0
- package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/hmac.js +3 -3
- package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/pbkdf2.js +4 -4
- package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/scrypt.js +5 -5
- package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/sha256.js +3 -3
- package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/sha512.js +4 -4
- package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/utils.js +2 -2
- package/dist/.tsbuildinfo +1 -1
- package/dist/contract/index.d.ts +0 -1
- package/dist/contract/index.js +0 -1
- package/dist/contract/index.js.map +1 -1
- package/dist/contract/rln_base_contract.d.ts +22 -32
- package/dist/contract/rln_base_contract.js +71 -127
- package/dist/contract/rln_base_contract.js.map +1 -1
- package/dist/contract/types.d.ts +0 -5
- package/dist/contract/types.js.map +1 -1
- package/dist/credentials_manager.js +1 -1
- package/dist/credentials_manager.js.map +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/dist/keystore/keystore.js.map +1 -1
- package/dist/keystore/types.d.ts +2 -2
- package/package.json +1 -1
- package/src/contract/index.ts +0 -1
- package/src/contract/rln_base_contract.ts +102 -166
- package/src/contract/types.ts +0 -5
- package/src/credentials_manager.ts +1 -1
- package/src/index.ts +1 -3
- package/src/keystore/keystore.ts +2 -4
- package/src/keystore/types.ts +2 -2
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/index.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/md5.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/nil.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/parse.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/regex.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/rng.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/sha1.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/stringify.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v1.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v3.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v35.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v4.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v5.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/validate.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/version.js +0 -0
@@ -1,5 +1,5 @@
|
|
1
1
|
import { getAugmentedNamespace } from './_commonjsHelpers.js';
|
2
|
-
import * as index from '../node_modules/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/index.js';
|
2
|
+
import * as index from '../packages/rln/node_modules/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/index.js';
|
3
3
|
|
4
4
|
var require$$0 = /*@__PURE__*/getAugmentedNamespace(index);
|
5
5
|
|
package/bundle/_virtual/utils.js
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
var utils = {
|
1
|
+
var utils = {};
|
2
2
|
|
3
|
-
export { utils as
|
3
|
+
export { utils as __exports };
|
@@ -1,3 +1,3 @@
|
|
1
|
-
var utils = {};
|
1
|
+
var utils = {exports: {}};
|
2
2
|
|
3
|
-
export { utils as
|
3
|
+
export { utils as __module };
|
package/bundle/index.js
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
export { RLNDecoder, RLNEncoder } from './packages/rln/dist/codec.js';
|
2
2
|
export { RLN_ABI } from './packages/rln/dist/contract/abi.js';
|
3
3
|
export { RLNContract } from './packages/rln/dist/contract/rln_contract.js';
|
4
|
-
export {
|
5
|
-
export { MembershipState } from './packages/rln/dist/contract/types.js';
|
4
|
+
export { LINEA_CONTRACT } from './packages/rln/dist/contract/constants.js';
|
6
5
|
export { RLNBaseContract } from './packages/rln/dist/contract/rln_base_contract.js';
|
7
6
|
export { createRLN } from './packages/rln/dist/create.js';
|
8
7
|
export { RLNCredentialsManager } from './packages/rln/dist/credentials_manager.js';
|
package/bundle/node_modules/{js-sha3 → @ethersproject/keccak256/node_modules/js-sha3}/src/sha3.js
RENAMED
@@ -1,5 +1,5 @@
|
|
1
|
-
import { getDefaultExportFromCjs, commonjsGlobal } from '
|
2
|
-
import { __module as sha3$1 } from '
|
1
|
+
import { getDefaultExportFromCjs, commonjsGlobal } from '../../../../../../_virtual/_commonjsHelpers.js';
|
2
|
+
import { __module as sha3$1 } from '../../../../../../_virtual/sha3.js';
|
3
3
|
|
4
4
|
/**
|
5
5
|
* [js-sha3]{@link https://github.com/emn178/js-sha3}
|
@@ -1,11 +1,37 @@
|
|
1
|
-
import { abytes as abytes$1 } from './utils.js';
|
2
|
-
|
3
1
|
/**
|
4
2
|
* Internal assertion helpers.
|
5
3
|
* @module
|
6
|
-
* @deprecated
|
7
4
|
*/
|
8
|
-
/**
|
9
|
-
|
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
|
+
}
|
10
36
|
|
11
|
-
export { abytes };
|
37
|
+
export { abytes, aexists, anumber, aoutput };
|
@@ -1,4 +1,5 @@
|
|
1
|
-
import {
|
1
|
+
import { aexists, aoutput } from './_assert.js';
|
2
|
+
import { Hash, createView, toBytes } from './utils.js';
|
2
3
|
|
3
4
|
/**
|
4
5
|
* Internal Merkle-Damgard hash utils.
|
@@ -32,22 +33,21 @@ function Maj(a, b, c) {
|
|
32
33
|
class HashMD extends Hash {
|
33
34
|
constructor(blockLen, outputLen, padOffset, isLE) {
|
34
35
|
super();
|
35
|
-
this.finished = false;
|
36
|
-
this.length = 0;
|
37
|
-
this.pos = 0;
|
38
|
-
this.destroyed = false;
|
39
36
|
this.blockLen = blockLen;
|
40
37
|
this.outputLen = outputLen;
|
41
38
|
this.padOffset = padOffset;
|
42
39
|
this.isLE = isLE;
|
40
|
+
this.finished = false;
|
41
|
+
this.length = 0;
|
42
|
+
this.pos = 0;
|
43
|
+
this.destroyed = false;
|
43
44
|
this.buffer = new Uint8Array(blockLen);
|
44
45
|
this.view = createView(this.buffer);
|
45
46
|
}
|
46
47
|
update(data) {
|
47
48
|
aexists(this);
|
48
|
-
data = toBytes(data);
|
49
|
-
abytes(data);
|
50
49
|
const { view, buffer, blockLen } = this;
|
50
|
+
data = toBytes(data);
|
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
|
-
|
84
|
+
this.buffer.subarray(pos).fill(0);
|
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,25 +119,14 @@ 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;
|
124
122
|
to.length = length;
|
125
123
|
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
|
-
}
|
133
130
|
}
|
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
|
-
]);
|
142
131
|
|
143
|
-
export { Chi, HashMD, Maj,
|
132
|
+
export { Chi, HashMD, Maj, setBigUint64 };
|
@@ -11,10 +11,9 @@ 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
|
-
|
15
|
-
let
|
16
|
-
let
|
17
|
-
for (let i = 0; i < len; i++) {
|
14
|
+
let Ah = new Uint32Array(lst.length);
|
15
|
+
let Al = new Uint32Array(lst.length);
|
16
|
+
for (let i = 0; i < lst.length; i++) {
|
18
17
|
const { h, l } = fromBig(lst[i], le);
|
19
18
|
[Ah[i], Al[i]] = [h, l];
|
20
19
|
}
|
@@ -1,4 +1,5 @@
|
|
1
|
-
import {
|
1
|
+
import { HashMD, Chi, Maj } from './_md.js';
|
2
|
+
import { wrapConstructor, rotr } from './utils.js';
|
2
3
|
|
3
4
|
/**
|
4
5
|
* SHA2-256 a.k.a. sha256. In JS, it is the fastest hash, even faster than Blake3.
|
@@ -8,9 +9,105 @@ import { sha256 as sha256$1 } from './sha2.js';
|
|
8
9
|
*
|
9
10
|
* Check out [FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
|
10
11
|
* @module
|
11
|
-
* @deprecated
|
12
12
|
*/
|
13
|
-
/**
|
14
|
-
|
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());
|
15
112
|
|
16
|
-
export { sha256 };
|
113
|
+
export { SHA256, sha256 };
|
@@ -1,5 +1,6 @@
|
|
1
|
+
import { anumber, aexists, abytes, aoutput } from './_assert.js';
|
1
2
|
import { rotlSH, rotlSL, rotlBH, rotlBL, split } from './_u64.js';
|
2
|
-
import {
|
3
|
+
import { wrapConstructor, Hash, u32, isLE, byteSwap32, toBytes } from './utils.js';
|
3
4
|
|
4
5
|
/**
|
5
6
|
* SHA3 (keccak) hash function, based on a new "Sponge function" design.
|
@@ -12,18 +13,16 @@ import { createHasher, Hash, anumber, u32, swap32IfBE, aexists, toBytes, abytes,
|
|
12
13
|
* Check out `sha3-addons` module for cSHAKE, k12, and others.
|
13
14
|
* @module
|
14
15
|
*/
|
15
|
-
// No __PURE__ annotations in sha3 header:
|
16
|
-
// EVERYTHING is in fact used on every export.
|
17
16
|
// Various per round constants calculations
|
18
|
-
const _0n = BigInt(0);
|
19
|
-
const _1n = BigInt(1);
|
20
|
-
const _2n = BigInt(2);
|
21
|
-
const _7n = BigInt(7);
|
22
|
-
const _256n = BigInt(256);
|
23
|
-
const _0x71n = BigInt(0x71);
|
24
17
|
const SHA3_PI = [];
|
25
18
|
const SHA3_ROTL = [];
|
26
19
|
const _SHA3_IOTA = [];
|
20
|
+
const _0n = /* @__PURE__ */ BigInt(0);
|
21
|
+
const _1n = /* @__PURE__ */ BigInt(1);
|
22
|
+
const _2n = /* @__PURE__ */ BigInt(2);
|
23
|
+
const _7n = /* @__PURE__ */ BigInt(7);
|
24
|
+
const _256n = /* @__PURE__ */ BigInt(256);
|
25
|
+
const _0x71n = /* @__PURE__ */ BigInt(0x71);
|
27
26
|
for (let round = 0, R = _1n, x = 1, y = 0; round < 24; round++) {
|
28
27
|
// Pi
|
29
28
|
[x, y] = [y, (2 * x + 3 * y) % 5];
|
@@ -39,9 +38,7 @@ for (let round = 0, R = _1n, x = 1, y = 0; round < 24; round++) {
|
|
39
38
|
}
|
40
39
|
_SHA3_IOTA.push(t);
|
41
40
|
}
|
42
|
-
const
|
43
|
-
const SHA3_IOTA_H = IOTAS[0];
|
44
|
-
const SHA3_IOTA_L = IOTAS[1];
|
41
|
+
const [SHA3_IOTA_H, SHA3_IOTA_L] = /* @__PURE__ */ split(_SHA3_IOTA, true);
|
45
42
|
// Left rotation (without 0, 32, 64)
|
46
43
|
const rotlH = (h, l, s) => (s > 32 ? rotlBH(h, l, s) : rotlSH(h, l, s));
|
47
44
|
const rotlL = (h, l, s) => (s > 32 ? rotlBL(h, l, s) : rotlSL(h, l, s));
|
@@ -89,47 +86,44 @@ function keccakP(s, rounds = 24) {
|
|
89
86
|
s[0] ^= SHA3_IOTA_H[round];
|
90
87
|
s[1] ^= SHA3_IOTA_L[round];
|
91
88
|
}
|
92
|
-
|
89
|
+
B.fill(0);
|
93
90
|
}
|
94
91
|
/** Keccak sponge function. */
|
95
92
|
class Keccak extends Hash {
|
96
93
|
// NOTE: we accept arguments in bytes instead of bits here.
|
97
94
|
constructor(blockLen, suffix, outputLen, enableXOF = false, rounds = 24) {
|
98
95
|
super();
|
99
|
-
this.pos = 0;
|
100
|
-
this.posOut = 0;
|
101
|
-
this.finished = false;
|
102
|
-
this.destroyed = false;
|
103
|
-
this.enableXOF = false;
|
104
96
|
this.blockLen = blockLen;
|
105
97
|
this.suffix = suffix;
|
106
98
|
this.outputLen = outputLen;
|
107
99
|
this.enableXOF = enableXOF;
|
108
100
|
this.rounds = rounds;
|
101
|
+
this.pos = 0;
|
102
|
+
this.posOut = 0;
|
103
|
+
this.finished = false;
|
104
|
+
this.destroyed = false;
|
109
105
|
// Can be passed from user as dkLen
|
110
106
|
anumber(outputLen);
|
111
107
|
// 1600 = 5x5 matrix of 64bit. 1600 bits === 200 bytes
|
112
108
|
// 0 < blockLen < 200
|
113
|
-
if (
|
114
|
-
throw new Error('only keccak-f1600 function
|
109
|
+
if (0 >= this.blockLen || this.blockLen >= 200)
|
110
|
+
throw new Error('Sha3 supports only keccak-f1600 function');
|
115
111
|
this.state = new Uint8Array(200);
|
116
112
|
this.state32 = u32(this.state);
|
117
113
|
}
|
118
|
-
clone() {
|
119
|
-
return this._cloneInto();
|
120
|
-
}
|
121
114
|
keccak() {
|
122
|
-
|
115
|
+
if (!isLE)
|
116
|
+
byteSwap32(this.state32);
|
123
117
|
keccakP(this.state32, this.rounds);
|
124
|
-
|
118
|
+
if (!isLE)
|
119
|
+
byteSwap32(this.state32);
|
125
120
|
this.posOut = 0;
|
126
121
|
this.pos = 0;
|
127
122
|
}
|
128
123
|
update(data) {
|
129
124
|
aexists(this);
|
130
|
-
data = toBytes(data);
|
131
|
-
abytes(data);
|
132
125
|
const { blockLen, state } = this;
|
126
|
+
data = toBytes(data);
|
133
127
|
const len = data.length;
|
134
128
|
for (let pos = 0; pos < len;) {
|
135
129
|
const take = Math.min(blockLen - this.pos, len - pos);
|
@@ -191,7 +185,7 @@ class Keccak extends Hash {
|
|
191
185
|
}
|
192
186
|
destroy() {
|
193
187
|
this.destroyed = true;
|
194
|
-
|
188
|
+
this.state.fill(0);
|
195
189
|
}
|
196
190
|
_cloneInto(to) {
|
197
191
|
const { blockLen, suffix, outputLen, rounds, enableXOF } = this;
|
@@ -209,8 +203,8 @@ class Keccak extends Hash {
|
|
209
203
|
return to;
|
210
204
|
}
|
211
205
|
}
|
212
|
-
const gen = (suffix, blockLen, outputLen) =>
|
206
|
+
const gen = (suffix, blockLen, outputLen) => wrapConstructor(() => new Keccak(blockLen, suffix, outputLen));
|
213
207
|
/** keccak-256 hash function. Different from SHA3-256. */
|
214
|
-
const keccak_256 = /* @__PURE__ */
|
208
|
+
const keccak_256 = /* @__PURE__ */ gen(0x01, 136, 256 / 8);
|
215
209
|
|
216
210
|
export { Keccak, keccakP, keccak_256 };
|
@@ -1,3 +1,5 @@
|
|
1
|
+
import { abytes } from './_assert.js';
|
2
|
+
|
1
3
|
/**
|
2
4
|
* Utilities for hex, bytes, CSPRNG.
|
3
5
|
* @module
|
@@ -9,55 +11,10 @@
|
|
9
11
|
// from `crypto` to `cryptoNode`, which imports native module.
|
10
12
|
// Makes the utils un-importable in browsers without a bundler.
|
11
13
|
// Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
|
12
|
-
/** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
|
13
|
-
function isBytes(a) {
|
14
|
-
return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
|
15
|
-
}
|
16
|
-
/** Asserts something is positive integer. */
|
17
|
-
function anumber(n) {
|
18
|
-
if (!Number.isSafeInteger(n) || n < 0)
|
19
|
-
throw new Error('positive integer expected, got ' + n);
|
20
|
-
}
|
21
|
-
/** Asserts something is Uint8Array. */
|
22
|
-
function abytes(b, ...lengths) {
|
23
|
-
if (!isBytes(b))
|
24
|
-
throw new Error('Uint8Array expected');
|
25
|
-
if (lengths.length > 0 && !lengths.includes(b.length))
|
26
|
-
throw new Error('Uint8Array expected of length ' + lengths + ', got length=' + b.length);
|
27
|
-
}
|
28
|
-
/** Asserts something is hash */
|
29
|
-
function ahash(h) {
|
30
|
-
if (typeof h !== 'function' || typeof h.create !== 'function')
|
31
|
-
throw new Error('Hash should be wrapped by utils.createHasher');
|
32
|
-
anumber(h.outputLen);
|
33
|
-
anumber(h.blockLen);
|
34
|
-
}
|
35
|
-
/** Asserts a hash instance has not been destroyed / finished */
|
36
|
-
function aexists(instance, checkFinished = true) {
|
37
|
-
if (instance.destroyed)
|
38
|
-
throw new Error('Hash instance has been destroyed');
|
39
|
-
if (checkFinished && instance.finished)
|
40
|
-
throw new Error('Hash#digest() has already been called');
|
41
|
-
}
|
42
|
-
/** Asserts output is properly-sized byte array */
|
43
|
-
function aoutput(out, instance) {
|
44
|
-
abytes(out);
|
45
|
-
const min = instance.outputLen;
|
46
|
-
if (out.length < min) {
|
47
|
-
throw new Error('digestInto() expects output buffer of length at least ' + min);
|
48
|
-
}
|
49
|
-
}
|
50
|
-
/** Cast u8 / u16 / u32 to u32. */
|
51
14
|
function u32(arr) {
|
52
15
|
return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
|
53
16
|
}
|
54
|
-
|
55
|
-
function clean(...arrays) {
|
56
|
-
for (let i = 0; i < arrays.length; i++) {
|
57
|
-
arrays[i].fill(0);
|
58
|
-
}
|
59
|
-
}
|
60
|
-
/** Create DataView of an array for easy byte-level manipulation. */
|
17
|
+
// Cast array to view
|
61
18
|
function createView(arr) {
|
62
19
|
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
63
20
|
}
|
@@ -67,7 +24,7 @@ function rotr(word, shift) {
|
|
67
24
|
}
|
68
25
|
/** Is current platform little-endian? Most are. Big-Endian platform: IBM */
|
69
26
|
const isLE = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)();
|
70
|
-
|
27
|
+
// The byte swap operation for uint32
|
71
28
|
function byteSwap(word) {
|
72
29
|
return (((word << 24) & 0xff000000) |
|
73
30
|
((word << 8) & 0xff0000) |
|
@@ -79,26 +36,15 @@ function byteSwap32(arr) {
|
|
79
36
|
for (let i = 0; i < arr.length; i++) {
|
80
37
|
arr[i] = byteSwap(arr[i]);
|
81
38
|
}
|
82
|
-
return arr;
|
83
39
|
}
|
84
|
-
const swap32IfBE = isLE
|
85
|
-
? (u) => u
|
86
|
-
: byteSwap32;
|
87
|
-
// Built-in hex conversion https://caniuse.com/mdn-javascript_builtins_uint8array_fromhex
|
88
|
-
const hasHexBuiltin = /* @__PURE__ */ (() =>
|
89
|
-
// @ts-ignore
|
90
|
-
typeof Uint8Array.from([]).toHex === 'function' && typeof Uint8Array.fromHex === 'function')();
|
91
40
|
// Array where index 0xf0 (240) is mapped to string 'f0'
|
92
41
|
const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'));
|
93
42
|
/**
|
94
|
-
* Convert byte array to hex string.
|
43
|
+
* Convert byte array to hex string.
|
95
44
|
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
|
96
45
|
*/
|
97
46
|
function bytesToHex(bytes) {
|
98
47
|
abytes(bytes);
|
99
|
-
// @ts-ignore
|
100
|
-
if (hasHexBuiltin)
|
101
|
-
return bytes.toHex();
|
102
48
|
// pre-caching improves the speed 6x
|
103
49
|
let hex = '';
|
104
50
|
for (let i = 0; i < bytes.length; i++) {
|
@@ -118,15 +64,12 @@ function asciiToBase16(ch) {
|
|
118
64
|
return;
|
119
65
|
}
|
120
66
|
/**
|
121
|
-
* Convert hex string to byte array.
|
67
|
+
* Convert hex string to byte array.
|
122
68
|
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
|
123
69
|
*/
|
124
70
|
function hexToBytes(hex) {
|
125
71
|
if (typeof hex !== 'string')
|
126
72
|
throw new Error('hex string expected, got ' + typeof hex);
|
127
|
-
// @ts-ignore
|
128
|
-
if (hasHexBuiltin)
|
129
|
-
return Uint8Array.fromHex(hex);
|
130
73
|
const hl = hex.length;
|
131
74
|
const al = hl / 2;
|
132
75
|
if (hl % 2)
|
@@ -144,12 +87,12 @@ function hexToBytes(hex) {
|
|
144
87
|
return array;
|
145
88
|
}
|
146
89
|
/**
|
147
|
-
*
|
148
|
-
* @example utf8ToBytes('abc') // Uint8Array
|
90
|
+
* Convert JS string to byte array.
|
91
|
+
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
|
149
92
|
*/
|
150
93
|
function utf8ToBytes(str) {
|
151
94
|
if (typeof str !== 'string')
|
152
|
-
throw new Error('string
|
95
|
+
throw new Error('utf8ToBytes expected string, got ' + typeof str);
|
153
96
|
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
|
154
97
|
}
|
155
98
|
/**
|
@@ -163,7 +106,9 @@ function toBytes(data) {
|
|
163
106
|
abytes(data);
|
164
107
|
return data;
|
165
108
|
}
|
166
|
-
/**
|
109
|
+
/**
|
110
|
+
* Copies several Uint8Arrays into one.
|
111
|
+
*/
|
167
112
|
function concatBytes(...arrays) {
|
168
113
|
let sum = 0;
|
169
114
|
for (let i = 0; i < arrays.length; i++) {
|
@@ -181,9 +126,13 @@ function concatBytes(...arrays) {
|
|
181
126
|
}
|
182
127
|
/** For runtime check if class implements interface */
|
183
128
|
class Hash {
|
129
|
+
// Safe version that clones internal state
|
130
|
+
clone() {
|
131
|
+
return this._cloneInto();
|
132
|
+
}
|
184
133
|
}
|
185
134
|
/** Wraps hash function, creating an interface on top of it */
|
186
|
-
function
|
135
|
+
function wrapConstructor(hashCons) {
|
187
136
|
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
188
137
|
const tmp = hashCons();
|
189
138
|
hashC.outputLen = tmp.outputLen;
|
@@ -192,4 +141,4 @@ function createHasher(hashCons) {
|
|
192
141
|
return hashC;
|
193
142
|
}
|
194
143
|
|
195
|
-
export { Hash,
|
144
|
+
export { Hash, byteSwap, byteSwap32, bytesToHex, concatBytes, createView, hexToBytes, isLE, rotr, toBytes, u32, utf8ToBytes, wrapConstructor };
|