@leofcoin/chain 1.10.12 → 1.10.14
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/exports/beacon-envelope.js +2 -2
- package/exports/beacon-epoch.js +1 -1
- package/exports/beacon-lifecycle.js +2 -2
- package/exports/beacon-round.js +1 -1
- package/exports/beacon.js +1 -1
- package/exports/browser/beacon-envelope.js +79 -3
- package/exports/browser/beacon-epoch.js +2 -1
- package/exports/browser/beacon-lifecycle.js +3 -2
- package/exports/browser/beacon-round.js +2 -1
- package/exports/browser/beacon-wire.js +3 -3
- package/exports/browser/beacon.js +3 -2
- package/exports/browser/{browser-DvU1xNFS-sdlKNCJc.js → browser-DvU1xNFS-4-PGqr4c.js} +4 -4
- package/exports/browser/browser-DvU1xNFS-BJCQsckZ.js +151 -0
- package/exports/browser/browser-DvU1xNFS-D9eSzJGJ.js +151 -0
- package/exports/browser/browser-crypto-BHeOz9rj.js +8893 -0
- package/exports/browser/browser-crypto-CTP3Yk4M.js +8893 -0
- package/exports/browser/{proposal.proto-BcUgd885.js → browser-crypto-Yk_e6OHw.js} +35 -6
- package/exports/browser/chain.js +63 -321
- package/exports/browser/{client-B-jyclOB-CsNYfj4Z.js → client-DDC8C510-CUi3_3uT.js} +36 -12
- package/exports/browser/client-DDC8C510-DbIdQjhy.js +2225 -0
- package/exports/browser/client-DDC8C510-ZN_MLo1Y.js +2225 -0
- package/exports/browser/connection-monitor-BjXI0nBP.js +453 -0
- package/exports/browser/connection-monitor-Cf0vNDCA.js +453 -0
- package/exports/browser/connection-monitor-pdopeM1I.js +453 -0
- package/exports/browser/hkdf-B9AXp_7K.js +222 -0
- package/exports/browser/hkdf-CnZvwpZ9.js +222 -0
- package/exports/browser/{index-Bxr5Iztg-C6xBk0rK.js → index-Bxr5Iztg-AmkW0EHS.js} +4 -4
- package/exports/browser/index-Bxr5Iztg-Bdpw_E83.js +40 -0
- package/exports/browser/index-Bxr5Iztg-CxXVYm3t.js +40 -0
- package/exports/browser/{index-D7FUx7Dd.js → index-CPQ5yvZ2.js} +5 -6
- package/exports/browser/index-C_RakUAS.js +4995 -0
- package/exports/browser/index-uFaocBqT.js +4995 -0
- package/exports/browser/{messages-FMRAS8QX-CvlZBzy5.js → messages-DH0tgNM9-BRp0gcJh.js} +4 -4
- package/exports/browser/messages-DH0tgNM9-CZwrcL37.js +208 -0
- package/exports/browser/messages-DH0tgNM9-DkNnpePH.js +208 -0
- package/exports/browser/{node-browser-xlqSBOaN.js → node-browser-1VSuCeew.js} +6 -6
- package/exports/browser/node-browser-CQugE1WZ.js +17880 -0
- package/exports/browser/node-browser-wDmRaOEo.js +17880 -0
- package/exports/browser/node-browser.js +4 -4
- package/exports/browser/sha2-Dkoic3vb.js +602 -0
- package/exports/browser/sha2-piimb6xL.js +614 -0
- package/exports/browser/weierstrass-HGdA_prK.js +2156 -0
- package/exports/browser/{weierstrass-C-jX_jly.js → weierstrass-Q0QND1ZT.js} +1 -1
- package/exports/browser/workers/machine-worker.js +419 -147
- package/exports/chain.js +56 -313
- package/exports/connection-monitor.js +324 -0
- package/exports/node.js +2 -0
- package/exports/workers/machine-worker.js +419 -147
- package/package.json +4 -4
- package/exports/browser/browser-CWeoyGUw-BabtHowB.js +0 -57
- package/exports/browser/constants-Cv0p224A.js +0 -130
- package/exports/browser/hkdf-DhdhLAAv.js +0 -147
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { N as default } from './node-browser-
|
|
2
|
-
import 'crypto';
|
|
3
|
-
import './
|
|
1
|
+
export { N as default } from './node-browser-CQugE1WZ.js';
|
|
2
|
+
import './browser-crypto-CTP3Yk4M.js';
|
|
3
|
+
import './connection-monitor-pdopeM1I.js';
|
|
4
4
|
import './index-CvKt4UDE.js';
|
|
5
|
-
import './
|
|
5
|
+
import './sha2-piimb6xL.js';
|
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
// Split a JS number into u32 halves without a BigInt allocation. Exact only for integers
|
|
2
|
+
// `0 <= n < 2**53`; callers use it on byte / bit counters, which JS length math caps far below
|
|
3
|
+
// that (an ArrayBuffer cannot exceed 2**53 - 1 bytes).
|
|
4
|
+
const fromNumH = (n) => (n / 2 ** 32) | 0;
|
|
5
|
+
const fromNumL = (n) => n >>> 0;
|
|
6
|
+
// Drop-in replacement for `view.setBigUint64(byteOffset, BigInt(n), isLE)` without the per-call
|
|
7
|
+
// BigInt allocation. Same `n < 2**53` precondition as `fromNumH`/`fromNumL`.
|
|
8
|
+
function setU64FromNum(view, byteOffset, n, isLE) {
|
|
9
|
+
const h = fromNumH(n);
|
|
10
|
+
const l = fromNumL(n);
|
|
11
|
+
view.setUint32(byteOffset, isLE ? l : h, isLE);
|
|
12
|
+
view.setUint32(byteOffset + 4, isLE ? h : l, isLE);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Checks if something is Uint8Array. Be careful: nodejs Buffer will return true.
|
|
17
|
+
* @param a - value to test
|
|
18
|
+
* @returns `true` when the value is a Uint8Array-compatible view.
|
|
19
|
+
* @example
|
|
20
|
+
* Check whether a value is a Uint8Array-compatible view.
|
|
21
|
+
* ```ts
|
|
22
|
+
* isBytes(new Uint8Array([1, 2, 3]));
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
function isBytes(a) {
|
|
26
|
+
// Plain `instanceof Uint8Array` is too strict for some Buffer / proxy / cross-realm cases.
|
|
27
|
+
// The fallback still requires a real ArrayBuffer view, so plain
|
|
28
|
+
// JSON-deserialized `{ constructor: ... }` spoofing is rejected, and
|
|
29
|
+
// `BYTES_PER_ELEMENT === 1` keeps the fallback on byte-oriented views.
|
|
30
|
+
return (a instanceof Uint8Array ||
|
|
31
|
+
(ArrayBuffer.isView(a) &&
|
|
32
|
+
a.constructor.name === 'Uint8Array' &&
|
|
33
|
+
'BYTES_PER_ELEMENT' in a &&
|
|
34
|
+
a.BYTES_PER_ELEMENT === 1));
|
|
35
|
+
}
|
|
36
|
+
// Shared error-message prefix builder. Only called on throw paths, so assert
|
|
37
|
+
// success paths never pay for the string concatenation.
|
|
38
|
+
const atitle = (title) => (title ? `"${title}" ` : '');
|
|
39
|
+
/**
|
|
40
|
+
* Asserts something is a non-negative integer.
|
|
41
|
+
* @param n - number to validate
|
|
42
|
+
* @param title - label included in thrown errors
|
|
43
|
+
* @returns The validated number.
|
|
44
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
45
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
46
|
+
* @example
|
|
47
|
+
* Validate a non-negative integer option.
|
|
48
|
+
* ```ts
|
|
49
|
+
* anumber(32, 'length');
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
function anumber(n, title = '') {
|
|
53
|
+
if (typeof n !== 'number')
|
|
54
|
+
throw new TypeError(atitle(title) + 'expected number, got ' + typeof n);
|
|
55
|
+
if (!Number.isSafeInteger(n) || n < 0)
|
|
56
|
+
throw new RangeError(atitle(title) + 'expected integer >= 0, got ' + n);
|
|
57
|
+
return n;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Asserts something is Uint8Array.
|
|
61
|
+
* @param value - value to validate
|
|
62
|
+
* @param length - optional exact length constraint
|
|
63
|
+
* @param title - label included in thrown errors
|
|
64
|
+
* @returns The validated byte array.
|
|
65
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
66
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
67
|
+
* @example
|
|
68
|
+
* Validate that a value is a byte array.
|
|
69
|
+
* ```ts
|
|
70
|
+
* abytes(new Uint8Array([1, 2, 3]));
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
function abytes(value, length, title = '') {
|
|
74
|
+
// Success path first: this runs at the start of every update() / digestInto(), and the
|
|
75
|
+
// common `abytes(data)` form must not pay for length handling it does not use.
|
|
76
|
+
if (isBytes(value) && (length === undefined))
|
|
77
|
+
return value;
|
|
78
|
+
const bytes = isBytes(value);
|
|
79
|
+
const ofLen = '';
|
|
80
|
+
const got = bytes ? `length=${value.length}` : `type=${typeof value}`;
|
|
81
|
+
const message = atitle(title) + 'expected Uint8Array' + ofLen + ', got ' + got;
|
|
82
|
+
if (!bytes)
|
|
83
|
+
throw new TypeError(message);
|
|
84
|
+
throw new RangeError(message);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Asserts something is a wrapped hash constructor.
|
|
88
|
+
* @param h - hash constructor to validate
|
|
89
|
+
* @throws On wrong argument types or invalid hash wrapper shape. {@link TypeError}
|
|
90
|
+
* @throws On invalid hash metadata ranges or values. {@link RangeError}
|
|
91
|
+
* @throws If the hash metadata allows empty outputs or block sizes. {@link Error}
|
|
92
|
+
* @example
|
|
93
|
+
* Validate a callable hash wrapper.
|
|
94
|
+
* ```ts
|
|
95
|
+
* import { ahash } from '@noble/hashes/utils.js';
|
|
96
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
97
|
+
* ahash(sha256);
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
function ahash(h) {
|
|
101
|
+
if (typeof h !== 'function' || typeof h.create !== 'function')
|
|
102
|
+
throw new TypeError('expected hash wrapped by utils.createHasher');
|
|
103
|
+
anumber(h.outputLen);
|
|
104
|
+
anumber(h.blockLen);
|
|
105
|
+
// HMAC and KDF callers treat these as real byte lengths; allowing zero lets fake wrappers pass
|
|
106
|
+
// validation and can produce empty outputs instead of failing fast.
|
|
107
|
+
if (h.outputLen < 1 || h.blockLen < 1)
|
|
108
|
+
throw new Error('hash blockLen / outputLen must be >= 1');
|
|
109
|
+
}
|
|
110
|
+
const aobject = (value, label) => {
|
|
111
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value))
|
|
112
|
+
throw new TypeError((label === 'object' ? '' : `"${label}" `) + 'expected object, got type=' + typeof value);
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* Asserts a hash instance has not been destroyed or finished.
|
|
116
|
+
* @param instance - hash instance to validate
|
|
117
|
+
* @param checkFinished - whether to reject finalized instances
|
|
118
|
+
* @throws If the hash instance has already been destroyed or finalized. {@link Error}
|
|
119
|
+
* @example
|
|
120
|
+
* Validate that a hash instance is still usable.
|
|
121
|
+
* ```ts
|
|
122
|
+
* import { aexists } from '@noble/hashes/utils.js';
|
|
123
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
124
|
+
* const hash = sha256.create();
|
|
125
|
+
* aexists(hash);
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
function aexists(instance, checkFinished = true) {
|
|
129
|
+
// Runs on every update()/digestInto(); the flags are library-owned booleans, so only their
|
|
130
|
+
// truthiness is checked - re-validating their type per call was pure hot-path overhead.
|
|
131
|
+
if (instance.destroyed)
|
|
132
|
+
throw new Error('hash was destroyed');
|
|
133
|
+
if (checkFinished && instance.finished)
|
|
134
|
+
throw new Error('digest() was already called');
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Asserts output is a sufficiently-sized byte array.
|
|
138
|
+
* @param out - destination buffer
|
|
139
|
+
* @param instance - hash instance providing output length
|
|
140
|
+
* Oversized buffers are allowed; downstream code only promises to fill the first `outputLen` bytes.
|
|
141
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
142
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
143
|
+
* @example
|
|
144
|
+
* Validate a caller-provided digest buffer.
|
|
145
|
+
* ```ts
|
|
146
|
+
* import { aoutput } from '@noble/hashes/utils.js';
|
|
147
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
148
|
+
* const hash = sha256.create();
|
|
149
|
+
* aoutput(new Uint8Array(hash.outputLen), hash);
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
function aoutput(out, instance) {
|
|
153
|
+
abytes(out, undefined, 'output');
|
|
154
|
+
// `outputLen` is a library-owned readonly number; the negated comparison keeps failing fast
|
|
155
|
+
// when it is missing/NaN (comparisons with undefined/NaN are false) without an anumber() call.
|
|
156
|
+
const min = instance.outputLen;
|
|
157
|
+
if (!(out.length >= min)) {
|
|
158
|
+
throw new RangeError('"output" expected length >= ' + min);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Zeroizes typed arrays in place. Warning: JS provides no guarantees.
|
|
163
|
+
* @param arrays - arrays to overwrite with zeros
|
|
164
|
+
* @example
|
|
165
|
+
* Zeroize sensitive buffers in place.
|
|
166
|
+
* ```ts
|
|
167
|
+
* clean(new Uint8Array([1, 2, 3]));
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
function clean(...arrays) {
|
|
171
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
172
|
+
arrays[i].fill(0);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Creates a DataView for byte-level manipulation.
|
|
177
|
+
* @param arr - source typed array
|
|
178
|
+
* @returns DataView over the same buffer region.
|
|
179
|
+
* @example
|
|
180
|
+
* Create a DataView over an existing buffer.
|
|
181
|
+
* ```ts
|
|
182
|
+
* createView(new Uint8Array(4));
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
function createView(arr) {
|
|
186
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Rotate-right operation for uint32 values.
|
|
190
|
+
* @param word - source word
|
|
191
|
+
* @param shift - shift amount in bits
|
|
192
|
+
* @returns Rotated word.
|
|
193
|
+
* @example
|
|
194
|
+
* Rotate a 32-bit word to the right.
|
|
195
|
+
* ```ts
|
|
196
|
+
* rotr(0x12345678, 8);
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
function rotr(word, shift) {
|
|
200
|
+
return (word << (32 - shift)) | (word >>> shift);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Merges default options and passed options.
|
|
204
|
+
* @param defaults - base option object
|
|
205
|
+
* @param opts - user overrides
|
|
206
|
+
* @param title - label included in thrown override errors
|
|
207
|
+
* @returns Merged option object. The merge mutates `defaults` in place.
|
|
208
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
209
|
+
* @example
|
|
210
|
+
* Merge user overrides onto default options.
|
|
211
|
+
* ```ts
|
|
212
|
+
* checkOpts({ dkLen: 32 }, { asyncTick: 10 });
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
function checkOpts(defaults, opts, title = 'opts') {
|
|
216
|
+
aobject(defaults, 'defaults');
|
|
217
|
+
if (opts !== undefined)
|
|
218
|
+
aobject(opts, title);
|
|
219
|
+
const merged = Object.assign(defaults, opts);
|
|
220
|
+
return merged;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Creates a callable hash function from a stateful class constructor.
|
|
224
|
+
* @param hashCons - hash constructor or factory
|
|
225
|
+
* @param info - optional metadata such as DER OID
|
|
226
|
+
* @returns Frozen callable hash wrapper with `.create()`.
|
|
227
|
+
* Wrapper construction eagerly calls `hashCons(undefined)` once to read
|
|
228
|
+
* `outputLen` / `blockLen`, so constructor side effects happen at module
|
|
229
|
+
* init time.
|
|
230
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
231
|
+
* @example
|
|
232
|
+
* Wrap a stateful hash constructor into a callable helper.
|
|
233
|
+
* ```ts
|
|
234
|
+
* import { createHasher } from '@noble/hashes/utils.js';
|
|
235
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
236
|
+
* const wrapped = createHasher(sha256.create, { oid: sha256.oid });
|
|
237
|
+
* wrapped(new Uint8Array([1]));
|
|
238
|
+
* ```
|
|
239
|
+
*/
|
|
240
|
+
function createHasher(hashCons, info = {}) {
|
|
241
|
+
if (typeof hashCons !== 'function')
|
|
242
|
+
throw new TypeError('"hashCons" expected function, got type=' + typeof hashCons);
|
|
243
|
+
info = checkOpts({}, info, 'info');
|
|
244
|
+
const hashC = (msg, opts) => hashCons(opts)
|
|
245
|
+
.update(msg)
|
|
246
|
+
.digest();
|
|
247
|
+
const tmp = hashCons(undefined);
|
|
248
|
+
hashC.outputLen = tmp.outputLen;
|
|
249
|
+
hashC.blockLen = tmp.blockLen;
|
|
250
|
+
hashC.canXOF = tmp.canXOF;
|
|
251
|
+
hashC.create = (opts) => hashCons(opts);
|
|
252
|
+
Object.assign(hashC, info);
|
|
253
|
+
return Object.freeze(hashC);
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Creates OID metadata for NIST hashes with prefix `06 09 60 86 48 01 65 03 04 02`.
|
|
257
|
+
* @param suffix - final OID byte for the selected hash.
|
|
258
|
+
* The helper accepts any byte even though only the documented NIST hash
|
|
259
|
+
* suffixes are meaningful downstream.
|
|
260
|
+
* @returns Object containing the DER-encoded OID.
|
|
261
|
+
* @example
|
|
262
|
+
* Build OID metadata for a NIST hash.
|
|
263
|
+
* ```ts
|
|
264
|
+
* oidNist(0x01);
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
const oidNist = (suffix) => ({
|
|
268
|
+
// Current NIST hashAlgs suffixes used here fit in one DER subidentifier octet.
|
|
269
|
+
// Larger suffix values would need base-128 OID encoding and a different length byte.
|
|
270
|
+
oid: Uint8Array.from([0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, suffix]),
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Internal Merkle-Damgard hash utils.
|
|
275
|
+
* @module
|
|
276
|
+
*/
|
|
277
|
+
/**
|
|
278
|
+
* Shared 32-bit conditional boolean primitive reused by SHA-256, SHA-1, and MD5 `F`.
|
|
279
|
+
* Returns bits from `b` when `a` is set, otherwise from `c`.
|
|
280
|
+
* The XOR form is equivalent to MD5's `F(X,Y,Z) = XY v not(X)Z` because the masked terms never
|
|
281
|
+
* set the same bit.
|
|
282
|
+
* @param a - selector word
|
|
283
|
+
* @param b - word chosen when selector bit is set
|
|
284
|
+
* @param c - word chosen when selector bit is clear
|
|
285
|
+
* @returns Mixed 32-bit word.
|
|
286
|
+
* @example
|
|
287
|
+
* Combine three words with the shared 32-bit choice primitive.
|
|
288
|
+
* ```ts
|
|
289
|
+
* Chi(0xffffffff, 0x12345678, 0x87654321);
|
|
290
|
+
* ```
|
|
291
|
+
*/
|
|
292
|
+
function Chi(a, b, c) {
|
|
293
|
+
return (a & b) ^ (~a & c);
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Shared 32-bit majority primitive reused by SHA-256 and SHA-1.
|
|
297
|
+
* Returns bits shared by at least two inputs.
|
|
298
|
+
* @param a - first input word
|
|
299
|
+
* @param b - second input word
|
|
300
|
+
* @param c - third input word
|
|
301
|
+
* @returns Mixed 32-bit word.
|
|
302
|
+
* @example
|
|
303
|
+
* Combine three words with the shared 32-bit majority primitive.
|
|
304
|
+
* ```ts
|
|
305
|
+
* Maj(0xffffffff, 0x12345678, 0x87654321);
|
|
306
|
+
* ```
|
|
307
|
+
*/
|
|
308
|
+
function Maj(a, b, c) {
|
|
309
|
+
return (a & b) ^ (a & c) ^ (b & c);
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Merkle-Damgard hash construction base class.
|
|
313
|
+
* Could be used to create MD5, RIPEMD, SHA1, SHA2.
|
|
314
|
+
* Accepts only byte-aligned `Uint8Array` input, even when the underlying spec describes bit
|
|
315
|
+
* strings with partial-byte tails.
|
|
316
|
+
* @param blockLen - internal block size in bytes
|
|
317
|
+
* @param outputLen - digest size in bytes
|
|
318
|
+
* @param padOffset - trailing length field size in bytes
|
|
319
|
+
* @param isLE - whether length and state words are encoded in little-endian
|
|
320
|
+
* @example
|
|
321
|
+
* Use a concrete subclass to get the shared Merkle-Damgard update/digest flow.
|
|
322
|
+
* ```ts
|
|
323
|
+
* import { _SHA1 } from '@noble/hashes/legacy.js';
|
|
324
|
+
* const hash = new _SHA1();
|
|
325
|
+
* hash.update(new Uint8Array([97, 98, 99]));
|
|
326
|
+
* hash.digest();
|
|
327
|
+
* ```
|
|
328
|
+
*/
|
|
329
|
+
class HashMD {
|
|
330
|
+
blockLen;
|
|
331
|
+
outputLen;
|
|
332
|
+
canXOF = false;
|
|
333
|
+
padOffset;
|
|
334
|
+
isLE;
|
|
335
|
+
// For partial updates less than block size
|
|
336
|
+
buffer;
|
|
337
|
+
view;
|
|
338
|
+
finished = false;
|
|
339
|
+
length = 0;
|
|
340
|
+
pos = 0;
|
|
341
|
+
destroyed = false;
|
|
342
|
+
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
343
|
+
this.blockLen = blockLen;
|
|
344
|
+
this.outputLen = outputLen;
|
|
345
|
+
this.padOffset = padOffset;
|
|
346
|
+
this.isLE = isLE;
|
|
347
|
+
this.buffer = new Uint8Array(blockLen);
|
|
348
|
+
this.view = createView(this.buffer);
|
|
349
|
+
}
|
|
350
|
+
update(data) {
|
|
351
|
+
aexists(this);
|
|
352
|
+
abytes(data);
|
|
353
|
+
const { view, buffer, blockLen } = this;
|
|
354
|
+
const len = data.length;
|
|
355
|
+
let processed = false;
|
|
356
|
+
for (let pos = 0; pos < len;) {
|
|
357
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
358
|
+
// Fast path only when there is no buffered partial block: `take === blockLen` implies
|
|
359
|
+
// `this.pos === 0`, so we can process full blocks directly from the input view.
|
|
360
|
+
if (take === blockLen) {
|
|
361
|
+
const dataView = createView(data);
|
|
362
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
|
363
|
+
this.process(dataView, pos);
|
|
364
|
+
processed = true;
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
// When the whole input is buffered in one go (common for short messages), passing `data`
|
|
368
|
+
// directly avoids allocating a subarray view.
|
|
369
|
+
buffer.set(pos === 0 && take === len ? data : data.subarray(pos, pos + take), this.pos);
|
|
370
|
+
this.pos += take;
|
|
371
|
+
pos += take;
|
|
372
|
+
if (this.pos === blockLen) {
|
|
373
|
+
this.process(view, 0);
|
|
374
|
+
this.pos = 0;
|
|
375
|
+
processed = true;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
this.length += data.length;
|
|
379
|
+
// Shared schedule buffers only pick up input-derived words inside process(); if everything
|
|
380
|
+
// was buffered without processing, there is nothing to zero.
|
|
381
|
+
if (processed)
|
|
382
|
+
this.roundClean();
|
|
383
|
+
return this;
|
|
384
|
+
}
|
|
385
|
+
digestInto(out) {
|
|
386
|
+
aexists(this);
|
|
387
|
+
aoutput(out, this);
|
|
388
|
+
this.finished = true;
|
|
389
|
+
// Padding
|
|
390
|
+
// We can avoid allocation of buffer for padding completely if it
|
|
391
|
+
// was previously not allocated here. But it won't change performance.
|
|
392
|
+
const { buffer, view, blockLen, isLE } = this;
|
|
393
|
+
let { pos } = this;
|
|
394
|
+
// append the bit '1' to the message, then zero-pad the rest of the block
|
|
395
|
+
buffer[pos++] = 0b10000000;
|
|
396
|
+
buffer.fill(0, pos);
|
|
397
|
+
// we have less than padOffset left in buffer, so we cannot put length in
|
|
398
|
+
// current block, need process it and pad again
|
|
399
|
+
if (this.padOffset > blockLen - pos) {
|
|
400
|
+
this.process(view, 0);
|
|
401
|
+
buffer.fill(0);
|
|
402
|
+
}
|
|
403
|
+
// `padOffset` reserves the whole length field. For SHA-384/512 the high 64 bits stay zero from
|
|
404
|
+
// the padding fill above, and JS will overflow before user input can make that half non-zero.
|
|
405
|
+
// So we only need to write the low 64 bits here (`length * 8` only scales the exponent of an
|
|
406
|
+
// integer below 2**53, so the split inside the helper stays exact).
|
|
407
|
+
setU64FromNum(view, blockLen - 8, this.length * 8, isLE);
|
|
408
|
+
this.process(view, 0);
|
|
409
|
+
// The final block above is processed outside update(), so the shared message-schedule
|
|
410
|
+
// buffers (e.g. SHA256_W) would otherwise retain input-derived words after digest().
|
|
411
|
+
this.roundClean();
|
|
412
|
+
// digest() passes our own `buffer` as `out`; reuse its cached view instead of allocating one.
|
|
413
|
+
const oview = out === buffer ? view : createView(out);
|
|
414
|
+
const len = this.outputLen;
|
|
415
|
+
// NOTE: we do division by 4 later, which must be fused in single op with modulo by JIT
|
|
416
|
+
const outLen = len / 4;
|
|
417
|
+
const state = this.get();
|
|
418
|
+
// Subclass-misconfiguration invariant: outputLen must be 32-bit aligned and fit the state.
|
|
419
|
+
if (len % 4 || outLen > state.length)
|
|
420
|
+
throw new Error('invalid outputLen');
|
|
421
|
+
for (let i = 0; i < outLen; i++)
|
|
422
|
+
oview.setUint32(4 * i, state[i], isLE);
|
|
423
|
+
}
|
|
424
|
+
digest() {
|
|
425
|
+
const { buffer, outputLen } = this;
|
|
426
|
+
this.digestInto(buffer);
|
|
427
|
+
// Copy before destroy(): subclasses wipe `buffer` during cleanup, but `digest()` must return
|
|
428
|
+
// fresh bytes to the caller.
|
|
429
|
+
const res = buffer.slice(0, outputLen);
|
|
430
|
+
this.destroy();
|
|
431
|
+
return res;
|
|
432
|
+
}
|
|
433
|
+
_cloneIntoMeta(to) {
|
|
434
|
+
const { buffer, length, finished, destroyed, pos } = this;
|
|
435
|
+
to.destroyed = destroyed;
|
|
436
|
+
to.finished = finished;
|
|
437
|
+
to.length = length;
|
|
438
|
+
to.pos = pos;
|
|
439
|
+
// Only partial-block bytes need copying: when `length % blockLen === 0`, `pos === 0` and
|
|
440
|
+
// later `update()` / `digestInto()` overwrite `to.buffer` from the start before reading it.
|
|
441
|
+
if (pos)
|
|
442
|
+
to.buffer.set(buffer); // Avoid a hot modulo guard.
|
|
443
|
+
return to;
|
|
444
|
+
}
|
|
445
|
+
clone() {
|
|
446
|
+
return this._cloneInto();
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
|
|
451
|
+
* Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
|
|
452
|
+
*/
|
|
453
|
+
/** Initial SHA256 state from RFC 6234 §6.1: the first 32 bits of the fractional parts of the
|
|
454
|
+
* square roots of the first eight prime numbers. Exported as a shared table; callers must treat
|
|
455
|
+
* it as read-only because constructors copy words from it by index. */
|
|
456
|
+
const SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
457
|
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
|
|
458
|
+
]);
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
|
|
462
|
+
* SHA256 is the fastest hash implementable in JS, even faster than Blake3.
|
|
463
|
+
* Check out {@link https://www.rfc-editor.org/rfc/rfc4634 | RFC 4634} and
|
|
464
|
+
* {@link https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf | FIPS 180-4}.
|
|
465
|
+
* @module
|
|
466
|
+
*/
|
|
467
|
+
/**
|
|
468
|
+
* SHA-224 / SHA-256 round constants from RFC 6234 §5.1: the first 32 bits
|
|
469
|
+
* of the cube roots of the first 64 primes (2..311).
|
|
470
|
+
*/
|
|
471
|
+
// prettier-ignore
|
|
472
|
+
const SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
473
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
474
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
475
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
476
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
477
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
478
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
479
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
480
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
481
|
+
]);
|
|
482
|
+
/** Reusable SHA-224 / SHA-256 message schedule buffer `W_t` from RFC 6234 §6.2 step 1. */
|
|
483
|
+
const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
484
|
+
/** Internal SHA-224 / SHA-256 compression engine from RFC 6234 §6.2. */
|
|
485
|
+
class SHA2_32B extends HashMD {
|
|
486
|
+
// We cannot use array here since array allows indexing by variable
|
|
487
|
+
// which means optimizer/compiler cannot use registers.
|
|
488
|
+
// Numeric initializers matter: starting the fields as `undefined` changes
|
|
489
|
+
// V8's field representation and makes sha256 3x slower (measured).
|
|
490
|
+
A = 0;
|
|
491
|
+
B = 0;
|
|
492
|
+
C = 0;
|
|
493
|
+
D = 0;
|
|
494
|
+
E = 0;
|
|
495
|
+
F = 0;
|
|
496
|
+
G = 0;
|
|
497
|
+
H = 0;
|
|
498
|
+
constructor(outputLen, IV) {
|
|
499
|
+
super(64, outputLen, 8, false);
|
|
500
|
+
this.A = IV[0] | 0;
|
|
501
|
+
this.B = IV[1] | 0;
|
|
502
|
+
this.C = IV[2] | 0;
|
|
503
|
+
this.D = IV[3] | 0;
|
|
504
|
+
this.E = IV[4] | 0;
|
|
505
|
+
this.F = IV[5] | 0;
|
|
506
|
+
this.G = IV[6] | 0;
|
|
507
|
+
this.H = IV[7] | 0;
|
|
508
|
+
}
|
|
509
|
+
get() {
|
|
510
|
+
const { A, B, C, D, E, F, G, H } = this;
|
|
511
|
+
return [A, B, C, D, E, F, G, H];
|
|
512
|
+
}
|
|
513
|
+
// prettier-ignore
|
|
514
|
+
set(A, B, C, D, E, F, G, H) {
|
|
515
|
+
this.A = A | 0;
|
|
516
|
+
this.B = B | 0;
|
|
517
|
+
this.C = C | 0;
|
|
518
|
+
this.D = D | 0;
|
|
519
|
+
this.E = E | 0;
|
|
520
|
+
this.F = F | 0;
|
|
521
|
+
this.G = G | 0;
|
|
522
|
+
this.H = H | 0;
|
|
523
|
+
}
|
|
524
|
+
_cloneInto(to) {
|
|
525
|
+
(to ||= new this.constructor()).set(...this.get());
|
|
526
|
+
return this._cloneIntoMeta(to);
|
|
527
|
+
}
|
|
528
|
+
process(view, offset) {
|
|
529
|
+
// Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
|
|
530
|
+
for (let i = 0; i < 16; i++, offset += 4)
|
|
531
|
+
SHA256_W[i] = view.getUint32(offset, false);
|
|
532
|
+
for (let i = 16; i < 64; i++) {
|
|
533
|
+
const W15 = SHA256_W[i - 15];
|
|
534
|
+
const W2 = SHA256_W[i - 2];
|
|
535
|
+
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
|
|
536
|
+
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
|
|
537
|
+
SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
|
|
538
|
+
}
|
|
539
|
+
// Compression function main loop, 64 rounds
|
|
540
|
+
let { A, B, C, D, E, F, G, H } = this;
|
|
541
|
+
for (let i = 0; i < 64; i++) {
|
|
542
|
+
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
543
|
+
const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
|
|
544
|
+
const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
|
|
545
|
+
const T2 = (sigma0 + Maj(A, B, C)) | 0;
|
|
546
|
+
H = G;
|
|
547
|
+
G = F;
|
|
548
|
+
F = E;
|
|
549
|
+
E = (D + T1) | 0;
|
|
550
|
+
D = C;
|
|
551
|
+
C = B;
|
|
552
|
+
B = A;
|
|
553
|
+
A = (T1 + T2) | 0;
|
|
554
|
+
}
|
|
555
|
+
// Add the compressed chunk to the current hash value
|
|
556
|
+
A = (A + this.A) | 0;
|
|
557
|
+
B = (B + this.B) | 0;
|
|
558
|
+
C = (C + this.C) | 0;
|
|
559
|
+
D = (D + this.D) | 0;
|
|
560
|
+
E = (E + this.E) | 0;
|
|
561
|
+
F = (F + this.F) | 0;
|
|
562
|
+
G = (G + this.G) | 0;
|
|
563
|
+
H = (H + this.H) | 0;
|
|
564
|
+
this.set(A, B, C, D, E, F, G, H);
|
|
565
|
+
}
|
|
566
|
+
roundClean() {
|
|
567
|
+
clean(SHA256_W);
|
|
568
|
+
}
|
|
569
|
+
destroy() {
|
|
570
|
+
// HashMD callers route post-destroy usability through `destroyed`; zeroizing alone still leaves
|
|
571
|
+
// update()/digest() callable on reused instances.
|
|
572
|
+
this.destroyed = true;
|
|
573
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
574
|
+
clean(this.buffer);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
/** Internal SHA-256 hash class grounded in RFC 6234 §6.2. */
|
|
578
|
+
class _SHA256 extends SHA2_32B {
|
|
579
|
+
constructor() {
|
|
580
|
+
super(32, SHA256_IV);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
/**
|
|
584
|
+
* SHA2-256 hash function from RFC 4634. In JS it's the fastest: even faster than Blake3. Some info:
|
|
585
|
+
*
|
|
586
|
+
* - Trying 2^128 hashes would get 50% chance of collision, using birthday attack.
|
|
587
|
+
* - BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
|
|
588
|
+
* - Each sha256 hash is executing 2^18 bit operations.
|
|
589
|
+
* - Good 2024 ASICs can do 200Th/sec with 3500 watts of power, corresponding to 2^36 hashes/joule.
|
|
590
|
+
* @param msg - message bytes to hash
|
|
591
|
+
* @param opts - Reserved hash options.
|
|
592
|
+
* @returns Digest bytes.
|
|
593
|
+
* @example
|
|
594
|
+
* Hash a message with SHA2-256.
|
|
595
|
+
* ```ts
|
|
596
|
+
* sha256(new Uint8Array([97, 98, 99]));
|
|
597
|
+
* ```
|
|
598
|
+
*/
|
|
599
|
+
const sha256 = /* @__PURE__ */ createHasher(() => new _SHA256(),
|
|
600
|
+
/* @__PURE__ */ oidNist(0x01));
|
|
601
|
+
|
|
602
|
+
export { ahash as a, abytes as b, clean as c, aexists as d, aoutput as e, anumber as f, sha256 as s };
|