@amritk/nish-aarch64-linux 0.44.0 → 0.45.1

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 (111) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +18 -16
  3. package/package.json +1 -1
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +8 -0
  7. package/runtime/core.mjs +16 -0
  8. package/runtime/nish.d.ts +47 -5
  9. package/runtime/nish.mjs +4 -0
  10. package/std/README.md +3 -3
  11. package/std/array.ts +10 -2
  12. package/std/bytes.ts +239 -26
  13. package/std/cli-args.ts +1072 -606
  14. package/std/cli-flags.ts +180 -98
  15. package/std/cli.ts +21 -13
  16. package/std/collections.ts +245 -127
  17. package/std/compress/checksum.ts +32 -37
  18. package/std/compress/deflate.ts +1102 -542
  19. package/std/compress/gzip.ts +290 -145
  20. package/std/compress/zlib.ts +57 -21
  21. package/std/crypto/aes.ts +153 -75
  22. package/std/crypto/base64url.ts +84 -61
  23. package/std/crypto/bcrypt-batch.ts +164 -90
  24. package/std/crypto/bcrypt.ts +185 -107
  25. package/std/crypto/chacha20poly1305.ts +84 -68
  26. package/std/crypto/ct.ts +9 -7
  27. package/std/crypto/ed25519.ts +64 -27
  28. package/std/crypto/hkdf.ts +105 -63
  29. package/std/crypto/hmac.ts +95 -89
  30. package/std/crypto/jwt.ts +248 -126
  31. package/std/crypto/p256.ts +758 -444
  32. package/std/crypto/p384.ts +20 -13
  33. package/std/crypto/pki-roots.ts +95 -38
  34. package/std/crypto/pki.ts +164 -60
  35. package/std/crypto/rsa.ts +266 -223
  36. package/std/crypto/scram.ts +3 -4
  37. package/std/crypto/sha1.ts +107 -65
  38. package/std/crypto/sha256.ts +502 -422
  39. package/std/crypto/sha512.ts +204 -135
  40. package/std/crypto/x509-random.ts +9 -3
  41. package/std/crypto/x509.ts +331 -171
  42. package/std/json-equal.ts +35 -19
  43. package/std/json-format.ts +215 -108
  44. package/std/json-number.ts +126 -72
  45. package/std/json-pointer.ts +42 -13
  46. package/std/json-tokenizer.ts +498 -307
  47. package/std/json-typed.ts +115 -36
  48. package/std/json-value.ts +341 -152
  49. package/std/json.ts +300 -158
  50. package/std/net/buffer-pool.ts +17 -7
  51. package/std/net/dns-config.ts +427 -236
  52. package/std/net/dns-message.ts +114 -47
  53. package/std/net/dns-resolver.ts +736 -237
  54. package/std/net/dns.ts +205 -52
  55. package/std/net/fetch.ts +320 -184
  56. package/std/net/grpc.ts +948 -0
  57. package/std/net/handler.ts +1275 -217
  58. package/std/net/hpack.ts +395 -180
  59. package/std/net/http-compress.ts +502 -65
  60. package/std/net/http-fields.ts +85 -49
  61. package/std/net/http-server.ts +1764 -602
  62. package/std/net/http1-client-codec.ts +372 -161
  63. package/std/net/http1-codec.ts +665 -331
  64. package/std/net/http1-server.ts +660 -278
  65. package/std/net/http2-cleartext.ts +165 -95
  66. package/std/net/http2-frame.ts +232 -89
  67. package/std/net/http2-tls.ts +42 -12
  68. package/std/net/http2.ts +327 -101
  69. package/std/net/http3-frame.ts +68 -33
  70. package/std/net/http3-server.ts +647 -192
  71. package/std/net/http3.ts +667 -189
  72. package/std/net/oidc.ts +63 -24
  73. package/std/net/outbound.ts +207 -142
  74. package/std/net/postgres-async.ts +1165 -443
  75. package/std/net/postgres-carrier.ts +575 -372
  76. package/std/net/postgres.ts +1459 -559
  77. package/std/net/prefork.ts +254 -98
  78. package/std/net/protobuf.ts +1017 -0
  79. package/std/net/qpack.ts +143 -92
  80. package/std/net/quic-conn-ack.ts +50 -18
  81. package/std/net/quic-conn-cid.ts +66 -26
  82. package/std/net/quic-conn-params.ts +13 -7
  83. package/std/net/quic-datagram.ts +90 -27
  84. package/std/net/quic-frame.ts +225 -124
  85. package/std/net/quic-listener.ts +197 -89
  86. package/std/net/quic-packet.ts +289 -163
  87. package/std/net/quic-recovery.ts +279 -128
  88. package/std/net/quic-stream.ts +466 -167
  89. package/std/net/quic.ts +568 -257
  90. package/std/net/router.ts +450 -99
  91. package/std/net/serve.ts +651 -232
  92. package/std/net/static.ts +1458 -0
  93. package/std/net/tls/client.ts +140 -45
  94. package/std/net/tls/codec.ts +510 -260
  95. package/std/net/tls/record-client.ts +268 -76
  96. package/std/net/tls/record-server.ts +236 -79
  97. package/std/net/tls/record.ts +173 -47
  98. package/std/net/tls/schedule.ts +58 -39
  99. package/std/net/tls/ticket.ts +295 -103
  100. package/std/net/tls/verify.ts +1 -1
  101. package/std/net/tls-tcp.ts +218 -75
  102. package/std/net/tls.ts +260 -152
  103. package/std/net/upstream.ts +283 -86
  104. package/std/net/url.ts +277 -102
  105. package/std/net/websocket.ts +370 -186
  106. package/std/net/webtransport.ts +755 -436
  107. package/std/regex.ts +1659 -829
  108. package/std/sync/ring.ts +320 -156
  109. package/std/sync/shm.ts +53 -6
  110. package/std/text.ts +205 -124
  111. package/std/threads.ts +22 -2
@@ -260,27 +260,15 @@ const sha384Initial = (): u64[] => [
260
260
  w64(0x47b5481d, 0xbefa4fa4),
261
261
  ]
262
262
 
263
- /** The big-endian 64-bit word at `buf[off]`. */
264
- const loadWord = (buf: u8[], off: i32): u64 => {
265
- let word: u64 = 0
266
- for (let i: i32 = 0; i < 8; i++) {
267
- word = (word << 8) | toU64(buf[off + i])
268
- }
269
- return word
270
- }
271
-
272
- /** Writes `word` big-endian into `buf[off]` .. `buf[off + 7]`. */
273
- const storeWord = (buf: u8[], off: i32, word: u64): void => {
274
- for (let i: i32 = 0; i < 8; i++) {
275
- buf[off + i] = toU8(word >> toU64((7 - i) * 8))
276
- }
277
- }
278
-
279
263
  /**
280
264
  * The state both hashes share: the hash value, the partial block, and the
281
265
  * message length in bytes. §5.1.2 appends the length in bits as 128 bits; a
282
266
  * byte count in a `u64` shifted left by three fills those 128 bits for any
283
267
  * message shorter than 2^64 bytes, which is every message there can be.
268
+ *
269
+ * No index in it is checked at run time: each read and write sits under a
270
+ * loop or block condition that proves it in range, and every caller's
271
+ * arguments make that condition hold.
284
272
  */
285
273
  class Sha512Engine {
286
274
  state: u64[]
@@ -304,72 +292,86 @@ class Sha512Engine {
304
292
  private compress(src: u8[], off: i32): void {
305
293
  const w = this.schedule
306
294
  const h = this.state
307
- const wLen: i32 = toI32(w.length)
308
- for (let t: i32 = 0; t < 16 && t < wLen; t++) {
309
- w[t] = loadWord(src, off + 8 * t)
310
- }
311
- for (let t: i32 = 16; t < wLen; t++) {
312
- // Named offsets rather than `w[t - 2]`, so the bounds prover sees each index.
313
- const t2: i32 = t - 2
314
- const t7: i32 = t - 7
315
- const t15: i32 = t - 15
316
- const t16: i32 = t - 16
317
- w[t] = smallSigma1(w[t2]) + w[t7] + smallSigma0(w[t15]) + w[t16]
318
- }
319
- let a = h[0]
320
- let b = h[1]
321
- let c = h[2]
322
- let d = h[3]
323
- let e = h[4]
324
- let f = h[5]
325
- let g = h[6]
326
- let hh = h[7]
327
- // §6.4.2 step 4, eight rounds per pass. Rather than shift eight words down
328
- // each round, the rounds rename them: T1 accumulates in the word that
329
- // becomes the new `a`, and the word that becomes the new `e` is `d + T1`,
330
- // so after eight rounds every word is back in its own name. Each index is
331
- // `u` minus a constant, which the bounds prover can see; `u + 1` it cannot.
332
- for (let u: i32 = 7; u < ROUNDS && u < wLen; u += 8) {
333
- const r0: i32 = u - 7
334
- const r1: i32 = u - 6
335
- const r2: i32 = u - 5
336
- const r3: i32 = u - 4
337
- const r4: i32 = u - 3
338
- const r5: i32 = u - 2
339
- const r6: i32 = u - 1
340
- hh = hh + bigSigma1(e) + ((e & f) ^ (~e & g)) + roundConstant(r0) + w[r0]
341
- d = d + hh
342
- hh = hh + bigSigma0(a) + ((a & b) ^ (a & c) ^ (b & c))
343
- g = g + bigSigma1(d) + ((d & e) ^ (~d & f)) + roundConstant(r1) + w[r1]
344
- c = c + g
345
- g = g + bigSigma0(hh) + ((hh & a) ^ (hh & b) ^ (a & b))
346
- f = f + bigSigma1(c) + ((c & d) ^ (~c & e)) + roundConstant(r2) + w[r2]
347
- b = b + f
348
- f = f + bigSigma0(g) + ((g & hh) ^ (g & a) ^ (hh & a))
349
- e = e + bigSigma1(b) + ((b & c) ^ (~b & d)) + roundConstant(r3) + w[r3]
350
- a = a + e
351
- e = e + bigSigma0(f) + ((f & g) ^ (f & hh) ^ (g & hh))
352
- d = d + bigSigma1(a) + ((a & b) ^ (~a & c)) + roundConstant(r4) + w[r4]
353
- hh = hh + d
354
- d = d + bigSigma0(e) + ((e & f) ^ (e & g) ^ (f & g))
355
- c = c + bigSigma1(hh) + ((hh & a) ^ (~hh & b)) + roundConstant(r5) + w[r5]
356
- g = g + c
357
- c = c + bigSigma0(d) + ((d & e) ^ (d & f) ^ (e & f))
358
- b = b + bigSigma1(g) + ((g & hh) ^ (~g & a)) + roundConstant(r6) + w[r6]
359
- f = f + b
360
- b = b + bigSigma0(c) + ((c & d) ^ (c & e) ^ (d & e))
361
- a = a + bigSigma1(f) + ((f & g) ^ (~f & hh)) + roundConstant(u) + w[u]
362
- e = e + a
363
- a = a + bigSigma0(b) + ((b & c) ^ (b & d) ^ (c & d))
295
+ const n: i32 = toI32(src.length)
296
+ if (toI32(h.length) >= 8 && toI32(w.length) >= 80) {
297
+ // Step 1: the sixteen big-endian words of the block (§3.1), then the
298
+ // rest of the schedule. `p < n - 7` proves each `src[p + k]`.
299
+ let p: i32 = off
300
+ for (let t: i32 = 0; t < 16 && p >= 0 && p < n - 7; t++) {
301
+ w[t] =
302
+ (toU64(src[p]) << 56) |
303
+ (toU64(src[p + 1]) << 48) |
304
+ (toU64(src[p + 2]) << 40) |
305
+ (toU64(src[p + 3]) << 32) |
306
+ (toU64(src[p + 4]) << 24) |
307
+ (toU64(src[p + 5]) << 16) |
308
+ (toU64(src[p + 6]) << 8) |
309
+ toU64(src[p + 7])
310
+ p += 8
311
+ }
312
+ for (let t: i32 = 16; t < 80; t++) {
313
+ // Named offsets rather than `w[t - 2]`, so the bounds prover sees each index.
314
+ const t2: i32 = t - 2
315
+ const t7: i32 = t - 7
316
+ const t15: i32 = t - 15
317
+ const t16: i32 = t - 16
318
+ w[t] = smallSigma1(w[t2]) + w[t7] + smallSigma0(w[t15]) + w[t16]
319
+ }
320
+ let a = h[0]
321
+ let b = h[1]
322
+ let c = h[2]
323
+ let d = h[3]
324
+ let e = h[4]
325
+ let f = h[5]
326
+ let g = h[6]
327
+ let hh = h[7]
328
+ // §6.4.2 step 4, eight rounds per pass. Rather than shift eight words down
329
+ // each round, the rounds rename them: T1 accumulates in the word that
330
+ // becomes the new `a`, and the word that becomes the new `e` is `d + T1`,
331
+ // so after eight rounds every word is back in its own name. Each index is
332
+ // `u` minus a constant, which the bounds prover can see; `u + 1` it cannot.
333
+ for (let u: i32 = 7; u < 80; u += 8) {
334
+ const r0: i32 = u - 7
335
+ const r1: i32 = u - 6
336
+ const r2: i32 = u - 5
337
+ const r3: i32 = u - 4
338
+ const r4: i32 = u - 3
339
+ const r5: i32 = u - 2
340
+ const r6: i32 = u - 1
341
+ hh = hh + bigSigma1(e) + ((e & f) ^ (~e & g)) + roundConstant(r0) + w[r0]
342
+ d = d + hh
343
+ hh = hh + bigSigma0(a) + ((a & b) ^ (a & c) ^ (b & c))
344
+ g = g + bigSigma1(d) + ((d & e) ^ (~d & f)) + roundConstant(r1) + w[r1]
345
+ c = c + g
346
+ g = g + bigSigma0(hh) + ((hh & a) ^ (hh & b) ^ (a & b))
347
+ f = f + bigSigma1(c) + ((c & d) ^ (~c & e)) + roundConstant(r2) + w[r2]
348
+ b = b + f
349
+ f = f + bigSigma0(g) + ((g & hh) ^ (g & a) ^ (hh & a))
350
+ e = e + bigSigma1(b) + ((b & c) ^ (~b & d)) + roundConstant(r3) + w[r3]
351
+ a = a + e
352
+ e = e + bigSigma0(f) + ((f & g) ^ (f & hh) ^ (g & hh))
353
+ d = d + bigSigma1(a) + ((a & b) ^ (~a & c)) + roundConstant(r4) + w[r4]
354
+ hh = hh + d
355
+ d = d + bigSigma0(e) + ((e & f) ^ (e & g) ^ (f & g))
356
+ c = c + bigSigma1(hh) + ((hh & a) ^ (~hh & b)) + roundConstant(r5) + w[r5]
357
+ g = g + c
358
+ c = c + bigSigma0(d) + ((d & e) ^ (d & f) ^ (e & f))
359
+ b = b + bigSigma1(g) + ((g & hh) ^ (~g & a)) + roundConstant(r6) + w[r6]
360
+ f = f + b
361
+ b = b + bigSigma0(c) + ((c & d) ^ (c & e) ^ (d & e))
362
+ a = a + bigSigma1(f) + ((f & g) ^ (~f & hh)) + roundConstant(u) + w[u]
363
+ e = e + a
364
+ a = a + bigSigma0(b) + ((b & c) ^ (b & d) ^ (c & d))
365
+ }
366
+ h[0] = h[0] + a
367
+ h[1] = h[1] + b
368
+ h[2] = h[2] + c
369
+ h[3] = h[3] + d
370
+ h[4] = h[4] + e
371
+ h[5] = h[5] + f
372
+ h[6] = h[6] + g
373
+ h[7] = h[7] + hh
364
374
  }
365
- h[0] = h[0] + a
366
- h[1] = h[1] + b
367
- h[2] = h[2] + c
368
- h[3] = h[3] + d
369
- h[4] = h[4] + e
370
- h[5] = h[5] + f
371
- h[6] = h[6] + g
372
- h[7] = h[7] + hh
373
375
  }
374
376
 
375
377
  /** @internal Called by `Sha512` and `Sha384`, which hold the engine. */
@@ -377,36 +379,81 @@ class Sha512Engine {
377
379
  if (this.finished) {
378
380
  panic("sha512: update after digest")
379
381
  }
380
- if (off < 0 || len < 0 || off > toI32(data.length) - len) {
382
+ const size: i32 = toI32(data.length)
383
+ if (off < 0 || len < 0 || off > size - len) {
381
384
  panic("sha512: the window is outside the buffer")
382
385
  }
383
386
  this.count = this.count + toU64(len)
384
- let at = off
385
- let left = len
386
- if (this.filled > 0) {
387
- const room: i32 = SHA512_BLOCK - this.filled
388
- const take: i32 = left < room ? left : room
389
- for (let i: i32 = 0; i < take; i++) {
390
- this.block[this.filled + i] = data[at + i]
387
+ const block: u8[] = this.block
388
+ const room: i32 = toI32(block.length)
389
+ let at: i32 = off
390
+ let left: i32 = len
391
+ let fill: i32 = this.filled
392
+ // Top up a partly filled block first, so that the loop below only ever
393
+ // starts at a block boundary. Each loop's condition beyond its count is
394
+ // what proves its indices in range; the window check above makes it hold.
395
+ if (fill > 0) {
396
+ while (left > 0 && fill >= 0 && fill < SHA512_BLOCK && fill < room && at >= 0 && at < size) {
397
+ block[fill] = data[at]
398
+ fill += 1
399
+ at += 1
400
+ left -= 1
391
401
  }
392
- this.filled = this.filled + take
393
- at = at + take
394
- left = left - take
395
- if (this.filled < SHA512_BLOCK) {
402
+ if (fill < SHA512_BLOCK) {
403
+ this.filled = fill
396
404
  return
397
405
  }
398
- this.compress(this.block, OFFSET_ZERO)
399
- this.filled = 0
406
+ this.compress(block, OFFSET_ZERO)
400
407
  }
401
- while (left >= SHA512_BLOCK) {
408
+ while (left >= 128 && at >= 0 && at <= size - 128) {
402
409
  this.compress(data, at)
403
- at = at + SHA512_BLOCK
404
- left = left - SHA512_BLOCK
410
+ at += 128
411
+ left -= 128
412
+ }
413
+ fill = 0
414
+ while (fill < left && fill < room && at >= 0 && at < size) {
415
+ block[fill] = data[at]
416
+ fill += 1
417
+ at += 1
418
+ }
419
+ this.filled = fill
420
+ }
421
+
422
+ /**
423
+ * Absorbs all of `data`. An array of up to 2^31 - 1 bytes goes through
424
+ * `absorb`. A longer one (possible under `--number-mode f64`, where `toI32`
425
+ * of its length would saturate and leave a prefix) is walked with a `number`
426
+ * offset instead, a block at a time through `block`, so it is absorbed
427
+ * whole. After `finish` it panics, as `absorb` does.
428
+ *
429
+ * @internal Called by `sha512`, `sha384` and `nish/crypto/hmac`.
430
+ */
431
+ absorbAll(data: u8[]): void {
432
+ const n: number = data.length
433
+ if (this.finished || n <= 2147483647) {
434
+ // A finished engine panics in `absorb` before it reads the window, so
435
+ // the saturated length of a longer array is never used.
436
+ this.absorb(data, OFFSET_ZERO, toI32(n))
437
+ return
405
438
  }
406
- for (let i: i32 = 0; i < left; i++) {
407
- this.block[i] = data[at + i]
439
+ this.count += toU64(toI64(n))
440
+ const block: u8[] = this.block
441
+ const room: i32 = toI32(block.length)
442
+ let fill: i32 = this.filled
443
+ let off: number = 0
444
+ while (true) {
445
+ while (fill >= 0 && fill < SHA512_BLOCK && fill < room && off >= 0 && off < n) {
446
+ block[fill] = data[off]
447
+ fill += 1
448
+ off += 1
449
+ }
450
+ if (fill < SHA512_BLOCK) {
451
+ this.filled = fill
452
+ return
453
+ }
454
+ this.compress(block, OFFSET_ZERO)
455
+ fill = 0
408
456
  }
409
- this.filled = left
410
457
  }
411
458
 
412
459
  /**
@@ -416,48 +463,73 @@ class Sha512Engine {
416
463
  * @internal Called by `Sha512` and `Sha384`, which hold the engine.
417
464
  */
418
465
  copyInto(twin: Sha512Engine): void {
419
- for (let i: i32 = 0; i < 8; i++) {
420
- twin.state[i] = this.state[i]
466
+ const from: u64[] = this.state
467
+ const to: u64[] = twin.state
468
+ for (let i: i32 = 0; i < 8 && i < toI32(from.length) && i < toI32(to.length); i++) {
469
+ to[i] = from[i]
421
470
  }
422
- for (let i: i32 = 0; i < this.filled; i++) {
423
- twin.block[i] = this.block[i]
471
+ const fill: i32 = this.filled
472
+ const src: u8[] = this.block
473
+ const dst: u8[] = twin.block
474
+ for (let i: i32 = 0; i < fill && i < toI32(src.length) && i < toI32(dst.length); i++) {
475
+ dst[i] = src[i]
424
476
  }
425
- twin.filled = this.filled
477
+ twin.filled = fill
426
478
  twin.count = this.count
427
479
  twin.finished = this.finished
428
480
  }
429
481
 
430
482
  /**
431
- * Pads (§5.1.2), compresses what is left, and answers the first `size` bytes of H(N) (§6.4.2, §6.5).
483
+ * Pads (§5.1.2), compresses what is left, and writes the first
484
+ * `out.length / 8` words of H(N) (§6.4.2, §6.5), at most eight, into `out`.
432
485
  *
433
486
  * @internal Called by `Sha512` and `Sha384`, which hold the engine.
434
487
  */
435
- finish(size: i32): u8[] {
488
+ finish(out: u8[]): void {
436
489
  if (this.finished) {
437
490
  panic("sha512: digest taken twice")
438
491
  }
439
492
  this.finished = true
440
493
  const block = this.block
441
- const blockLen: i32 = toI32(block.length)
442
- block[this.filled] = 0x80
443
- for (let i: i32 = this.filled + 1; i >= 0 && i < blockLen; i++) {
444
- block[i] = 0
494
+ const room: i32 = toI32(block.length)
495
+ const fill: i32 = this.filled
496
+ // The 0x80 that ends the message, then zeros to the end of the block.
497
+ for (let i: i32 = fill; i >= 0 && i < room; i++) {
498
+ block[i] = i === fill ? 0x80 : 0
445
499
  }
446
- if (this.filled >= LENGTH_AT) {
500
+ if (fill >= LENGTH_AT) {
447
501
  this.compress(block, OFFSET_ZERO)
448
- for (let i: i32 = 0; i < LENGTH_AT && i < blockLen; i++) {
502
+ for (let i: i32 = 0; i < LENGTH_AT && i < room; i++) {
449
503
  block[i] = 0
450
504
  }
451
505
  }
452
- storeWord(block, LENGTH_AT, this.count >> 61)
453
- storeWord(block, LENGTH_AT + 8, this.count << 3)
506
+ // The length in bits, big-endian in the last 16 bytes: the top 64 bits are
507
+ // `count >> 61`, the bottom 64 are `count << 3`.
508
+ const high: u64 = this.count >> 61
509
+ const low: u64 = this.count << 3
510
+ let shift: u64 = 0
511
+ for (let i: i32 = 127; i >= 120 && i < room; i--) {
512
+ block[i] = toU8(low >> shift)
513
+ block[i - 8] = toU8(high >> shift)
514
+ shift += 8
515
+ }
454
516
  this.compress(block, OFFSET_ZERO)
455
- // Both sizes are whole words, so the digest is the first `size / 8` of them.
456
- const out = new Array<u8>(size)
457
- for (let i: i32 = 0; i < size / 8 && i < 8; i++) {
458
- storeWord(out, 8 * i, this.state[i])
517
+ // Both sizes are whole words, so the digest is the first `out.length / 8`.
518
+ const state: u64[] = this.state
519
+ const size: i32 = toI32(out.length)
520
+ let p: i32 = 0
521
+ for (let i: i32 = 0; i < 8 && i < toI32(state.length) && p < size - 7; i++) {
522
+ const word: u64 = state[i]
523
+ out[p] = toU8(word >> 56)
524
+ out[p + 1] = toU8(word >> 48)
525
+ out[p + 2] = toU8(word >> 40)
526
+ out[p + 3] = toU8(word >> 32)
527
+ out[p + 4] = toU8(word >> 24)
528
+ out[p + 5] = toU8(word >> 16)
529
+ out[p + 6] = toU8(word >> 8)
530
+ out[p + 7] = toU8(word)
531
+ p += 8
459
532
  }
460
- return out
461
533
  }
462
534
  }
463
535
 
@@ -514,7 +586,9 @@ export class Sha512 {
514
586
  * @returns A fresh 64-byte digest, which the caller owns.
515
587
  */
516
588
  digest(): u8[] {
517
- return this.engine.finish(SHA512_SIZE)
589
+ const out: u8[] = new Array<u8>(SHA512_SIZE)
590
+ this.engine.finish(out)
591
+ return out
518
592
  }
519
593
  }
520
594
 
@@ -570,40 +644,35 @@ export class Sha384 {
570
644
  * @returns A fresh 48-byte digest, which the caller owns.
571
645
  */
572
646
  digest(): u8[] {
573
- return this.engine.finish(SHA384_SIZE)
647
+ const out: u8[] = new Array<u8>(SHA384_SIZE)
648
+ this.engine.finish(out)
649
+ return out
574
650
  }
575
651
  }
576
652
 
577
653
  /**
578
654
  * The SHA-512 digest of all of `data`, as a fresh 64-byte array. An array
579
- * longer than 2^31 - 1 bytes panics, for the reason `sha256` gives: a longer
580
- * length does not survive `toI32` under `--number-mode f64`, and the digest
581
- * would be of a prefix.
655
+ * longer than 2^31 - 1 bytes (possible under `--number-mode f64`) is hashed
656
+ * whole, never cut to a prefix.
582
657
  *
583
658
  * @param data The whole message; it is read, not changed.
584
659
  * @returns A fresh 64-byte digest.
585
660
  */
586
661
  export const sha512 = (data: u8[]): u8[] => {
587
- if (data.length > 2147483647) {
588
- panic("sha512: a message longer than 2^31 - 1 bytes")
589
- }
590
662
  const hasher = new Sha512()
591
- hasher.update(data, OFFSET_ZERO, toI32(data.length))
663
+ hasher.engine.absorbAll(data)
592
664
  return hasher.digest()
593
665
  }
594
666
 
595
667
  /**
596
668
  * The SHA-384 digest of all of `data`, as a fresh 48-byte array. An array
597
- * longer than 2^31 - 1 bytes panics, as in `sha512`.
669
+ * longer than 2^31 - 1 bytes is hashed whole, as in `sha512`.
598
670
  *
599
671
  * @param data The whole message; it is read, not changed.
600
672
  * @returns A fresh 48-byte digest.
601
673
  */
602
674
  export const sha384 = (data: u8[]): u8[] => {
603
- if (data.length > 2147483647) {
604
- panic("sha384: a message longer than 2^31 - 1 bytes")
605
- }
606
675
  const hasher = new Sha384()
607
- hasher.update(data, OFFSET_ZERO, toI32(data.length))
676
+ hasher.engine.absorbAll(data)
608
677
  return hasher.digest()
609
678
  }
@@ -54,9 +54,15 @@ export const x509DrawP256Key = (): Secret<u8[]> => {
54
54
  * @returns The 20 serial octets, big-endian, in a fresh array.
55
55
  */
56
56
  export const x509DrawSerial = (): u8[] => {
57
- const serial: u8[] = new Array<u8>(X509_MAX_SERIAL)
58
- crypto.getRandomValues(serial)
59
- serial[0] = (serial[0] & toU8(0x3f)) | toU8(0x40)
57
+ const drawn: u8[] = new Array<u8>(X509_MAX_SERIAL)
58
+ crypto.getRandomValues(drawn)
59
+ // Copied rather than indexed: a call forgets the length the bounds proof
60
+ // knew, and a for-of reads each drawn octet without an index to prove, so
61
+ // every octet of the serial is one the CSPRNG drew.
62
+ const serial: u8[] = []
63
+ for (const b of drawn) {
64
+ serial.push(serial.length === 0 ? (b & toU8(0x3f)) | toU8(0x40) : b)
65
+ }
60
66
  return serial
61
67
  }
62
68