@amritk/nish-x86_64-linux 0.44.0 → 0.46.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/INSTALL.md +3 -1
- package/bin/nish +0 -0
- package/docs/AI.md +18 -16
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +8 -0
- package/runtime/core.mjs +16 -0
- package/runtime/nish.d.ts +88 -5
- package/runtime/nish.h +10 -0
- package/runtime/nish.mjs +5 -0
- package/runtime/runtime-host.c +15 -0
- package/runtime/runtime-net.c +223 -4
- package/runtime/runtime-os.c +15 -1
- package/runtime/runtime-uring.c +291 -31
- package/runtime/sqlite/nish-sqlite.c +301 -128
- package/std/README.md +15 -15
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +84 -61
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +3 -4
- package/std/crypto/sha1.ts +107 -65
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1315 -222
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -602
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +660 -278
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +647 -192
- package/std/net/http3.ts +667 -189
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1165 -443
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1459 -559
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +50 -18
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +225 -124
- package/std/net/quic-listener.ts +197 -89
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +568 -257
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +651 -232
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +370 -186
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +171 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +205 -124
- package/std/threads.ts +22 -2
|
@@ -143,7 +143,11 @@ class BcryptBatchKey {
|
|
|
143
143
|
readonly key: Secret<u8[]>
|
|
144
144
|
|
|
145
145
|
constructor(length: i32) {
|
|
146
|
-
|
|
146
|
+
const key: u8[] = []
|
|
147
|
+
for (let i: i32 = 0; i < length; i++) {
|
|
148
|
+
key.push(toU8(0))
|
|
149
|
+
}
|
|
150
|
+
this.key = secret(key)
|
|
147
151
|
}
|
|
148
152
|
|
|
149
153
|
/** @internal Called by `BcryptBatcher.run`; the class is not exported. */
|
|
@@ -192,11 +196,11 @@ class BcryptBatchShare {
|
|
|
192
196
|
*/
|
|
193
197
|
hashes: string[]
|
|
194
198
|
/**
|
|
195
|
-
* 16 bytes
|
|
199
|
+
* Each request's salt: 16 bytes for a hash, none for a verify.
|
|
196
200
|
*
|
|
197
201
|
* @internal Read by this module's task functions; the class is not exported.
|
|
198
202
|
*/
|
|
199
|
-
salts: u8[]
|
|
203
|
+
salts: u8[][]
|
|
200
204
|
/** @internal Read by this module's task functions; the class is not exported. */
|
|
201
205
|
cost: i32
|
|
202
206
|
/** @internal Read by this module's task functions; the class is not exported. */
|
|
@@ -212,9 +216,12 @@ class BcryptBatchShare {
|
|
|
212
216
|
}
|
|
213
217
|
}
|
|
214
218
|
|
|
215
|
-
/** The holder at `list[i]` of `share`, or
|
|
216
|
-
const bcryptBatchLane = (share: BcryptBatchShare, list: i32[], i: i32): BcryptBatchKey => {
|
|
217
|
-
|
|
219
|
+
/** The holder at `list[i]` of `share`, or null for a lane past the first `n` of `list`, which reads nothing. */
|
|
220
|
+
const bcryptBatchLane = (share: BcryptBatchShare, list: i32[], n: i32, i: i32): BcryptBatchKey | null => {
|
|
221
|
+
if (i < 0 || i >= n || i >= toI32(list.length)) {
|
|
222
|
+
return null
|
|
223
|
+
}
|
|
224
|
+
const at: i32 = list[i]
|
|
218
225
|
return share.passwords[at]
|
|
219
226
|
}
|
|
220
227
|
|
|
@@ -229,7 +236,8 @@ const bcryptBatchVerifyAlone = (held: BcryptBatchKey, hash: string): string => {
|
|
|
229
236
|
|
|
230
237
|
/**
|
|
231
238
|
* One `bcryptVerifyN` over the first `n` of `list`: a holder's field is
|
|
232
|
-
* passed only as a call's argument, so each lane count is its own call.
|
|
239
|
+
* passed only as a call's argument, so each lane count is its own call. A
|
|
240
|
+
* lane past `n` is null and reads nothing; an empty batch is an error.
|
|
233
241
|
*/
|
|
234
242
|
const bcryptBatchVerifyLanes = (
|
|
235
243
|
share: BcryptBatchShare,
|
|
@@ -237,25 +245,28 @@ const bcryptBatchVerifyLanes = (
|
|
|
237
245
|
n: i32,
|
|
238
246
|
hashes: string[]
|
|
239
247
|
): Result<boolean[], string> => {
|
|
240
|
-
const a: BcryptBatchKey = bcryptBatchLane(share, list, 0)
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
const
|
|
245
|
-
const
|
|
246
|
-
|
|
248
|
+
const a: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 0)
|
|
249
|
+
if (a === null) {
|
|
250
|
+
return Err("bcrypt: a batch has no lanes")
|
|
251
|
+
}
|
|
252
|
+
const b: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 1)
|
|
253
|
+
const c: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 2)
|
|
254
|
+
const d: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 3)
|
|
255
|
+
const e: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 4)
|
|
256
|
+
const f: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 5)
|
|
257
|
+
if (b === null) {
|
|
247
258
|
return bcryptVerifyN(a.key, null, null, null, null, null, hashes)
|
|
248
259
|
}
|
|
249
|
-
if (
|
|
260
|
+
if (c === null) {
|
|
250
261
|
return bcryptVerifyN(a.key, b.key, null, null, null, null, hashes)
|
|
251
262
|
}
|
|
252
|
-
if (
|
|
263
|
+
if (d === null) {
|
|
253
264
|
return bcryptVerifyN(a.key, b.key, c.key, null, null, null, hashes)
|
|
254
265
|
}
|
|
255
|
-
if (
|
|
266
|
+
if (e === null) {
|
|
256
267
|
return bcryptVerifyN(a.key, b.key, c.key, d.key, null, null, hashes)
|
|
257
268
|
}
|
|
258
|
-
if (
|
|
269
|
+
if (f === null) {
|
|
259
270
|
return bcryptVerifyN(a.key, b.key, c.key, d.key, e.key, null, hashes)
|
|
260
271
|
}
|
|
261
272
|
return bcryptVerifyN(a.key, b.key, c.key, d.key, e.key, f.key, hashes)
|
|
@@ -268,25 +279,28 @@ const bcryptBatchHashLanes = (
|
|
|
268
279
|
n: i32,
|
|
269
280
|
salts: u8[]
|
|
270
281
|
): Result<string[], string> => {
|
|
271
|
-
const a: BcryptBatchKey = bcryptBatchLane(share, list, 0)
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
const
|
|
276
|
-
const
|
|
277
|
-
|
|
282
|
+
const a: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 0)
|
|
283
|
+
if (a === null) {
|
|
284
|
+
return Err("bcrypt: a batch has no lanes")
|
|
285
|
+
}
|
|
286
|
+
const b: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 1)
|
|
287
|
+
const c: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 2)
|
|
288
|
+
const d: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 3)
|
|
289
|
+
const e: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 4)
|
|
290
|
+
const f: BcryptBatchKey | null = bcryptBatchLane(share, list, n, 5)
|
|
291
|
+
if (b === null) {
|
|
278
292
|
return bcryptHashN(a.key, null, null, null, null, null, salts, share.cost)
|
|
279
293
|
}
|
|
280
|
-
if (
|
|
294
|
+
if (c === null) {
|
|
281
295
|
return bcryptHashN(a.key, b.key, null, null, null, null, salts, share.cost)
|
|
282
296
|
}
|
|
283
|
-
if (
|
|
297
|
+
if (d === null) {
|
|
284
298
|
return bcryptHashN(a.key, b.key, c.key, null, null, null, salts, share.cost)
|
|
285
299
|
}
|
|
286
|
-
if (
|
|
300
|
+
if (e === null) {
|
|
287
301
|
return bcryptHashN(a.key, b.key, c.key, d.key, null, null, salts, share.cost)
|
|
288
302
|
}
|
|
289
|
-
if (
|
|
303
|
+
if (f === null) {
|
|
290
304
|
return bcryptHashN(a.key, b.key, c.key, d.key, e.key, null, salts, share.cost)
|
|
291
305
|
}
|
|
292
306
|
return bcryptHashN(a.key, b.key, c.key, d.key, e.key, f.key, salts, share.cost)
|
|
@@ -301,10 +315,12 @@ const bcryptBatchVerifyRun = (
|
|
|
301
315
|
out: string[]
|
|
302
316
|
): void => {
|
|
303
317
|
const lanes: i32[] = []
|
|
304
|
-
|
|
318
|
+
let i: i32 = from
|
|
319
|
+
for (let k: i32 = 0; k < n && i < toI32(list.length); k++) {
|
|
305
320
|
if (i >= 0) {
|
|
306
321
|
lanes.push(list[i])
|
|
307
322
|
}
|
|
323
|
+
i += 1
|
|
308
324
|
}
|
|
309
325
|
if (toI32(lanes.length) === 0) {
|
|
310
326
|
return
|
|
@@ -337,24 +353,31 @@ const bcryptBatchVerifyRun = (
|
|
|
337
353
|
|
|
338
354
|
/** The hashes of `share` at `list[from..from + n)` as one `bcryptHashN`, each lane's answer into `out` at its index. */
|
|
339
355
|
const bcryptBatchHashRun = (share: BcryptBatchShare, list: i32[], from: i32, n: i32, out: string[]): void => {
|
|
356
|
+
// Each lane's salt goes into `salts` at 16 times its lane; a request
|
|
357
|
+
// without a 16-byte salt is answered with bcrypt's error and joins no lane.
|
|
340
358
|
const lanes: i32[] = []
|
|
341
|
-
|
|
359
|
+
const salts: u8[] = new Array<u8>(96)
|
|
360
|
+
let q: i32 = 0
|
|
361
|
+
let i: i32 = from
|
|
362
|
+
for (let k: i32 = 0; k < n && i < toI32(list.length); k++) {
|
|
342
363
|
if (i >= 0) {
|
|
343
|
-
|
|
364
|
+
const at: i32 = list[i]
|
|
365
|
+
const salt: u8[] = at >= 0 && at < toI32(share.salts.length) ? share.salts[at] : []
|
|
366
|
+
if (toI32(salt.length) === 16 && q >= 0 && q <= toI32(salts.length) - 16) {
|
|
367
|
+
for (let j: i32 = 0; j < 16 && q < toI32(salts.length); j++) {
|
|
368
|
+
salts[q] = salt[j]
|
|
369
|
+
q += 1
|
|
370
|
+
}
|
|
371
|
+
lanes.push(at)
|
|
372
|
+
} else if (at >= 0 && at < toI32(out.length)) {
|
|
373
|
+
out[at] = "!bcrypt: the salt is not 16 bytes"
|
|
374
|
+
}
|
|
344
375
|
}
|
|
376
|
+
i += 1
|
|
345
377
|
}
|
|
346
378
|
if (toI32(lanes.length) === 0) {
|
|
347
379
|
return
|
|
348
380
|
}
|
|
349
|
-
const salts: u8[] = new Array<u8>(96)
|
|
350
|
-
for (let s: i32 = 0; s < toI32(lanes.length) && s < 6; s++) {
|
|
351
|
-
const at: i32 = lanes[s]
|
|
352
|
-
for (let j: i32 = 0; j < 16 && 16 * at + j < toI32(share.salts.length); j++) {
|
|
353
|
-
if (at >= 0) {
|
|
354
|
-
salts[16 * s + j] = share.salts[16 * at + j]
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
381
|
const made: Result<string[], string> = bcryptBatchHashLanes(share, lanes, toI32(lanes.length), salts)
|
|
359
382
|
for (let s: i32 = 0; s < toI32(lanes.length); s++) {
|
|
360
383
|
const at: i32 = lanes[s]
|
|
@@ -394,14 +417,22 @@ const bcryptBatchShareRun = (share: BcryptBatchShare): string => {
|
|
|
394
417
|
for (let i: i32 = 0; i < toI32(share.kinds.length); i++) {
|
|
395
418
|
out.push("")
|
|
396
419
|
}
|
|
397
|
-
|
|
420
|
+
// `bcryptBatcher` refuses a width outside 1 to 6, so this keeps it as given.
|
|
421
|
+
const least: i32 = share.width < 1 ? 1 : share.width
|
|
422
|
+
const width: i32 = least > 6 ? 6 : least
|
|
398
423
|
const verifies: i32[] = bcryptBatchOfKind(share, BCRYPT_BATCH_VERIFY)
|
|
399
|
-
for (let i: i32 = 0; i < toI32(verifies.length); i += width) {
|
|
400
|
-
bcryptBatchVerifyRun(share, verifies, i, width, out)
|
|
401
|
-
}
|
|
402
424
|
const hashes: i32[] = bcryptBatchOfKind(share, BCRYPT_BATCH_HASH)
|
|
403
|
-
|
|
404
|
-
|
|
425
|
+
if (width > 0) {
|
|
426
|
+
for (let i: i32 = 0; i < toI32(verifies.length); i++) {
|
|
427
|
+
if (i % width === 0) {
|
|
428
|
+
bcryptBatchVerifyRun(share, verifies, i, width, out)
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
for (let i: i32 = 0; i < toI32(hashes.length); i++) {
|
|
432
|
+
if (i % width === 0) {
|
|
433
|
+
bcryptBatchHashRun(share, hashes, i, width, out)
|
|
434
|
+
}
|
|
435
|
+
}
|
|
405
436
|
}
|
|
406
437
|
return out.join("\n")
|
|
407
438
|
}
|
|
@@ -429,11 +460,11 @@ const bcryptBatchLines = (joined: string): string[] => {
|
|
|
429
460
|
let from: i32 = 0
|
|
430
461
|
while (from >= 0 && from <= toI32(joined.length)) {
|
|
431
462
|
const at: i32 = toI32(joined.indexOf("\n", from))
|
|
432
|
-
if (at < from || at
|
|
433
|
-
lines.push(joined.
|
|
463
|
+
if (at < 0 || at < from || at >= toI32(joined.length)) {
|
|
464
|
+
lines.push(joined.substring(from))
|
|
434
465
|
return lines
|
|
435
466
|
}
|
|
436
|
-
lines.push(joined.
|
|
467
|
+
lines.push(joined.substring(from, at))
|
|
437
468
|
from = at + 1
|
|
438
469
|
}
|
|
439
470
|
return lines
|
|
@@ -457,11 +488,11 @@ const bcryptBatchAnswerOf = (ticket: i32, kind: i32, said: string): BcryptAnswer
|
|
|
457
488
|
*/
|
|
458
489
|
export class BcryptBatcher {
|
|
459
490
|
private holdNanos: i64
|
|
460
|
-
/** The queue, from `head`: each request's copy of its password, kind, stored hash, salt (16 bytes
|
|
491
|
+
/** The queue, from `head`: each request's copy of its password, kind, stored hash, salt (16 bytes for a hash, none for a verify), ticket and arrival. */
|
|
461
492
|
private passwords: BcryptBatchKey[]
|
|
462
493
|
private kinds: i32[]
|
|
463
494
|
private hashes: string[]
|
|
464
|
-
private salts: u8[]
|
|
495
|
+
private salts: u8[][]
|
|
465
496
|
private tickets: i32[]
|
|
466
497
|
private arrived: i64[]
|
|
467
498
|
/** Answers made without a batch (a salt of the wrong length), handed out by the next `run`. */
|
|
@@ -470,7 +501,10 @@ export class BcryptBatcher {
|
|
|
470
501
|
private cores: i32
|
|
471
502
|
private cost: i32
|
|
472
503
|
private head: i32
|
|
473
|
-
/**
|
|
504
|
+
/**
|
|
505
|
+
* The next ticket; after 2^31 - 1 it wraps to 0. Two requests share a
|
|
506
|
+
* ticket only if one is still queued after 2^31 others are made.
|
|
507
|
+
*/
|
|
474
508
|
private next: i32
|
|
475
509
|
|
|
476
510
|
/**
|
|
@@ -515,12 +549,15 @@ export class BcryptBatcher {
|
|
|
515
549
|
* @param hash The stored hash; its cost sets the work, so it must be one you
|
|
516
550
|
* stored.
|
|
517
551
|
* @param now The time of the call, from `monotonicNanos()`.
|
|
518
|
-
* @returns The ticket its `BcryptAnswer` will carry
|
|
552
|
+
* @returns The ticket its `BcryptAnswer` will carry: tickets count up from
|
|
553
|
+
* 0 and wrap to 0 after 2^31 - 1, so a later request can carry a smaller
|
|
554
|
+
* one.
|
|
519
555
|
*/
|
|
520
556
|
verify(password: Secret<u8[]>, hash: string, now: i64): i32 {
|
|
521
557
|
const ticket: i32 = this.next
|
|
522
|
-
this.next =
|
|
523
|
-
|
|
558
|
+
this.next = ticket < 0x7fffffff ? ticket + 1 : 0
|
|
559
|
+
const noSalt: u8[] = []
|
|
560
|
+
this.enqueue(password, BCRYPT_BATCH_VERIFY, hash, noSalt, ticket, now)
|
|
524
561
|
return ticket
|
|
525
562
|
}
|
|
526
563
|
|
|
@@ -535,11 +572,13 @@ export class BcryptBatcher {
|
|
|
535
572
|
* is bad, and the caller still owns and wipes it.
|
|
536
573
|
* @param salt 16 fresh random bytes; read, not kept.
|
|
537
574
|
* @param now The time of the call, from `monotonicNanos()`.
|
|
538
|
-
* @returns The ticket its `BcryptAnswer` will carry
|
|
575
|
+
* @returns The ticket its `BcryptAnswer` will carry: tickets count up from
|
|
576
|
+
* 0 and wrap to 0 after 2^31 - 1, so a later request can carry a smaller
|
|
577
|
+
* one.
|
|
539
578
|
*/
|
|
540
579
|
hash(password: Secret<u8[]>, salt: u8[], now: i64): i32 {
|
|
541
580
|
const ticket: i32 = this.next
|
|
542
|
-
this.next =
|
|
581
|
+
this.next = ticket < 0x7fffffff ? ticket + 1 : 0
|
|
543
582
|
if (toI32(salt.length) !== 16) {
|
|
544
583
|
this.early.push(new BcryptAnswer(ticket, false, "", "bcrypt: the salt is not 16 bytes"))
|
|
545
584
|
return ticket
|
|
@@ -553,9 +592,11 @@ export class BcryptBatcher {
|
|
|
553
592
|
this.passwords.push(bcryptBatchTake(password))
|
|
554
593
|
this.kinds.push(kind)
|
|
555
594
|
this.hashes.push(hash)
|
|
595
|
+
const own: u8[] = []
|
|
556
596
|
for (let j: i32 = 0; j < 16 && j < toI32(salt.length); j++) {
|
|
557
|
-
|
|
597
|
+
own.push(salt[j])
|
|
558
598
|
}
|
|
599
|
+
this.salts.push(own)
|
|
559
600
|
this.tickets.push(ticket)
|
|
560
601
|
this.arrived.push(now)
|
|
561
602
|
}
|
|
@@ -573,14 +614,18 @@ export class BcryptBatcher {
|
|
|
573
614
|
if (toI32(this.early.length) > 0) {
|
|
574
615
|
return 0
|
|
575
616
|
}
|
|
576
|
-
const
|
|
617
|
+
const head: i32 = this.head
|
|
618
|
+
if (head < 0) {
|
|
619
|
+
return 0
|
|
620
|
+
}
|
|
621
|
+
const queued: i32 = toI32(this.kinds.length) - head
|
|
577
622
|
if (queued <= 0) {
|
|
578
623
|
return -1
|
|
579
624
|
}
|
|
580
|
-
if (queued >= this.width ||
|
|
625
|
+
if (queued >= this.width || head >= toI32(this.arrived.length)) {
|
|
581
626
|
return 0
|
|
582
627
|
}
|
|
583
|
-
const waited: i64 = now - this.arrived[
|
|
628
|
+
const waited: i64 = now - this.arrived[head]
|
|
584
629
|
return waited >= this.holdNanos ? 0 : this.holdNanos - waited
|
|
585
630
|
}
|
|
586
631
|
|
|
@@ -606,30 +651,45 @@ export class BcryptBatcher {
|
|
|
606
651
|
if (this.waitNanos(now) !== 0) {
|
|
607
652
|
return answers
|
|
608
653
|
}
|
|
609
|
-
const
|
|
654
|
+
const head: i32 = this.head
|
|
655
|
+
if (head < 0) {
|
|
656
|
+
return answers
|
|
657
|
+
}
|
|
658
|
+
const queued: i32 = toI32(this.kinds.length) - head
|
|
610
659
|
if (queued <= 0) {
|
|
611
660
|
return answers
|
|
612
661
|
}
|
|
613
|
-
const most:
|
|
614
|
-
const taken: i32 = queued < most ? queued : most
|
|
662
|
+
const most: i64 = toI64(this.width) * toI64(this.cores)
|
|
663
|
+
const taken: i32 = toI64(queued) < most ? queued : toI32(most)
|
|
615
664
|
const order: i32[] = this.orderOf(taken)
|
|
616
665
|
const tasks: i32 = taken < this.cores ? taken : this.cores
|
|
666
|
+
const parts: i64 = toI64(tasks)
|
|
667
|
+
if (parts <= 0) {
|
|
668
|
+
return answers
|
|
669
|
+
}
|
|
617
670
|
const shares: BcryptBatchShare[] = []
|
|
618
671
|
// Share k takes order[taken·k/tasks, taken·(k+1)/tasks): contiguous, so one cost's verifies stay together.
|
|
619
672
|
const owner: i32[] = []
|
|
620
673
|
for (let k: i32 = 0; k < tasks; k++) {
|
|
621
674
|
const share: BcryptBatchShare = new BcryptBatchShare(this.cost, this.width)
|
|
622
|
-
const lo: i32 = (taken * k) /
|
|
623
|
-
const hi: i32 = (taken * (k + 1)) /
|
|
675
|
+
const lo: i32 = toI32((toI64(taken) * toI64(k)) / parts)
|
|
676
|
+
const hi: i32 = toI32((toI64(taken) * (toI64(k) + 1)) / parts)
|
|
624
677
|
for (let i: i32 = lo; i >= 0 && i < hi && i < toI32(order.length); i++) {
|
|
625
678
|
const at: i32 = order[i]
|
|
626
|
-
|
|
679
|
+
const queue: i32[] = this.kinds
|
|
680
|
+
const saltQueue: u8[][] = this.salts
|
|
681
|
+
if (
|
|
682
|
+
at >= 0 &&
|
|
683
|
+
at < toI32(this.passwords.length) &&
|
|
684
|
+
at < toI32(queue.length) &&
|
|
685
|
+
at < toI32(saltQueue.length)
|
|
686
|
+
) {
|
|
687
|
+
const kind: i32 = queue[at]
|
|
688
|
+
const salt: u8[] = saltQueue[at]
|
|
627
689
|
share.passwords.push(this.passwords[at])
|
|
628
|
-
share.kinds.push(
|
|
690
|
+
share.kinds.push(kind)
|
|
629
691
|
share.hashes.push(at < toI32(this.hashes.length) ? this.hashes[at] : "")
|
|
630
|
-
|
|
631
|
-
share.salts.push(16 * at + j < toI32(this.salts.length) ? this.salts[16 * at + j] : toU8(0))
|
|
632
|
-
}
|
|
692
|
+
share.salts.push(salt)
|
|
633
693
|
owner.push(at)
|
|
634
694
|
}
|
|
635
695
|
}
|
|
@@ -641,7 +701,7 @@ export class BcryptBatcher {
|
|
|
641
701
|
}
|
|
642
702
|
{
|
|
643
703
|
using s = scope()
|
|
644
|
-
for (let k: i32 = 0; k < toI32(shares.length); k++) {
|
|
704
|
+
for (let k: i32 = 0; k < toI32(shares.length) && k < toI32(said.length); k++) {
|
|
645
705
|
s.spawn(bcryptBatchShareRun, shares[k], said, k)
|
|
646
706
|
}
|
|
647
707
|
}
|
|
@@ -674,10 +734,14 @@ export class BcryptBatcher {
|
|
|
674
734
|
private orderOf(taken: i32): i32[] {
|
|
675
735
|
const keyed: i32[] = []
|
|
676
736
|
const keys: i32[] = []
|
|
677
|
-
|
|
737
|
+
let n: i32 = 0
|
|
738
|
+
const queue: i32[] = this.kinds
|
|
739
|
+
for (let i: i32 = this.head; n < taken && i >= 0 && i < toI32(queue.length); i++) {
|
|
740
|
+
n += 1
|
|
741
|
+
const kind: i32 = queue[i]
|
|
678
742
|
keyed.push(i)
|
|
679
743
|
let key: i32 = 1000
|
|
680
|
-
if (
|
|
744
|
+
if (kind !== BCRYPT_BATCH_HASH) {
|
|
681
745
|
key = i < toI32(this.hashes.length) ? bcryptBatchCostOf(this.hashes[i]) : -1
|
|
682
746
|
}
|
|
683
747
|
keys.push(key)
|
|
@@ -708,30 +772,40 @@ export class BcryptBatcher {
|
|
|
708
772
|
|
|
709
773
|
/** Drops the requests before `head` once they are most of the queue, so a long-lived batcher's arrays stay small. */
|
|
710
774
|
private compact(): void {
|
|
711
|
-
if (this.head < 64 || this.head * 2 <
|
|
775
|
+
if (this.head < 64 || toI64(this.head) * 2 < toI64(this.kinds.length)) {
|
|
712
776
|
return
|
|
713
777
|
}
|
|
714
778
|
const passwords: BcryptBatchKey[] = []
|
|
715
779
|
const kinds: i32[] = []
|
|
716
780
|
const hashes: string[] = []
|
|
717
|
-
const salts: u8[] = []
|
|
781
|
+
const salts: u8[][] = []
|
|
718
782
|
const tickets: i32[] = []
|
|
719
783
|
const arrived: i64[] = []
|
|
720
|
-
|
|
784
|
+
const oldPasswords: BcryptBatchKey[] = this.passwords
|
|
785
|
+
const oldKinds: i32[] = this.kinds
|
|
786
|
+
const oldHashes: string[] = this.hashes
|
|
787
|
+
const oldSalts: u8[][] = this.salts
|
|
788
|
+
const oldTickets: i32[] = this.tickets
|
|
789
|
+
const oldArrived: i64[] = this.arrived
|
|
790
|
+
for (let i: i32 = this.head; i >= 0 && i < toI32(oldKinds.length); i++) {
|
|
721
791
|
if (
|
|
722
|
-
i < toI32(
|
|
723
|
-
i < toI32(
|
|
724
|
-
i < toI32(
|
|
725
|
-
i < toI32(
|
|
792
|
+
i < toI32(oldPasswords.length) &&
|
|
793
|
+
i < toI32(oldHashes.length) &&
|
|
794
|
+
i < toI32(oldSalts.length) &&
|
|
795
|
+
i < toI32(oldTickets.length) &&
|
|
796
|
+
i < toI32(oldArrived.length)
|
|
726
797
|
) {
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
798
|
+
const kind: i32 = oldKinds[i]
|
|
799
|
+
const hash: string = oldHashes[i]
|
|
800
|
+
const salt: u8[] = oldSalts[i]
|
|
801
|
+
const ticket: i32 = oldTickets[i]
|
|
802
|
+
const time: i64 = oldArrived[i]
|
|
803
|
+
passwords.push(oldPasswords[i])
|
|
804
|
+
kinds.push(kind)
|
|
805
|
+
hashes.push(hash)
|
|
806
|
+
salts.push(salt)
|
|
807
|
+
tickets.push(ticket)
|
|
808
|
+
arrived.push(time)
|
|
735
809
|
}
|
|
736
810
|
}
|
|
737
811
|
this.passwords = passwords
|