@amritk/nish-x86_64-linux 0.50.0 → 0.51.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +4 -1
  3. package/package.json +1 -1
  4. package/runtime/nish.d.ts +82 -2
  5. package/runtime/nish.h +5 -0
  6. package/runtime/runtime-host.c +21 -5
  7. package/std/README.md +7 -7
  8. package/std/base64.ts +61 -95
  9. package/std/bytes.ts +6 -4
  10. package/std/cli-args.ts +150 -107
  11. package/std/compress/checksum.ts +68 -10
  12. package/std/compress/deflate.ts +193 -100
  13. package/std/compress/gzip.ts +124 -49
  14. package/std/compress/zlib.ts +57 -15
  15. package/std/crypto/aes.ts +51 -9
  16. package/std/crypto/ed25519.ts +12 -10
  17. package/std/crypto/hkdf.ts +405 -283
  18. package/std/crypto/hmac.ts +483 -148
  19. package/std/crypto/jwt.ts +19 -21
  20. package/std/crypto/p256.ts +5 -7
  21. package/std/crypto/pki-roots.ts +28 -8
  22. package/std/crypto/rsa-sign.ts +5 -2
  23. package/std/crypto/sha256.ts +81 -26
  24. package/std/crypto/sha512.ts +135 -49
  25. package/std/json-value.ts +14 -15
  26. package/std/net/buffer-pool.ts +25 -14
  27. package/std/net/fetch.ts +108 -27
  28. package/std/net/handler.ts +11 -22
  29. package/std/net/hpack.ts +38 -19
  30. package/std/net/http-compress.ts +21 -12
  31. package/std/net/http-server.ts +9 -4
  32. package/std/net/http1-client-codec.ts +22 -15
  33. package/std/net/http1-codec.ts +37 -36
  34. package/std/net/http3-server.ts +127 -45
  35. package/std/net/http3.ts +99 -50
  36. package/std/net/postgres-async.ts +5 -10
  37. package/std/net/qpack.ts +83 -17
  38. package/std/net/quic-listener.ts +42 -27
  39. package/std/net/quic-packet.ts +91 -50
  40. package/std/net/quic-stream.ts +29 -22
  41. package/std/net/quic.ts +48 -23
  42. package/std/net/static.ts +5 -3
  43. package/std/net/tls/client.ts +81 -34
  44. package/std/net/tls/codec.ts +3 -4
  45. package/std/net/tls/record.ts +54 -22
  46. package/std/net/tls/schedule.ts +339 -149
  47. package/std/net/tls.ts +42 -12
  48. package/std/net/upstream.ts +18 -19
  49. package/std/net/url.ts +78 -21
  50. package/std/net/websocket.ts +3 -11
  51. package/std/net/webtransport.ts +95 -21
  52. package/std/raster.ts +12 -2
  53. package/std/term-pixels.ts +18 -2
  54. package/std/text.ts +5 -5
package/std/crypto/jwt.ts CHANGED
@@ -86,14 +86,7 @@
86
86
  * functions share the importing program's flat symbol namespace
87
87
  * (`docs/wp26-stdlib.md` §3e).
88
88
  */
89
- import {
90
- HMAC_SHA256,
91
- HMAC_SHA384,
92
- HMAC_SHA512,
93
- HmacKey,
94
- hmacKeyedInto,
95
- hmacKeyedVerify,
96
- } from "nish/crypto/hmac"
89
+ import { HmacHash, HmacKey, hmacKeyedInto, hmacKeyedVerify } from "nish/crypto/hmac"
97
90
  import {
98
91
  RSA_HASH_SHA256,
99
92
  RSA_HASH_SHA384,
@@ -239,9 +232,14 @@ const jwtSignatureSize = (key: JwtKey): i32 => {
239
232
  }
240
233
 
241
234
  const jwtCopy = (a: u8[]): u8[] => {
242
- const out: u8[] = []
243
- for (let k: i32 = 0; k < toI32(a.length); k += 1) {
244
- out.push(a[k])
235
+ const n: i32 = toI32(a.length)
236
+ if (n <= 0) {
237
+ return []
238
+ }
239
+ const out: u8[] = new Array<u8>(n)
240
+ // Always true; the prover does not carry `out.length === n` from `new Array`.
241
+ if (toI32(a.length) <= toI32(out.length)) {
242
+ out.set(a, 0)
245
243
  }
246
244
  return out
247
245
  }
@@ -342,18 +340,15 @@ const jwtPublicKey = (
342
340
  * @returns A new key that verifies and signs, or `null`.
343
341
  */
344
342
  export const jwtHmacKey = (alg: i32, key: Secret<u8[]>): JwtKey | null => {
345
- let hash: i32 = 0
346
- let size: i32 = 0
347
- if (alg === JWT_ALG_HS256) {
348
- hash = HMAC_SHA256
349
- size = 32
350
- } else if (alg === JWT_ALG_HS384) {
351
- hash = HMAC_SHA384
343
+ let hash: HmacHash = HmacHash.Sha256
344
+ let size: i32 = 32
345
+ if (alg === JWT_ALG_HS384) {
346
+ hash = HmacHash.Sha384
352
347
  size = 48
353
348
  } else if (alg === JWT_ALG_HS512) {
354
- hash = HMAC_SHA512
349
+ hash = HmacHash.Sha512
355
350
  size = 64
356
- } else {
351
+ } else if (alg !== JWT_ALG_HS256) {
357
352
  return null
358
353
  }
359
354
  if (expose(key, jwtLength) < size) {
@@ -1893,7 +1888,10 @@ export const jwtSignKid = (key: JwtKey, kid: string | null, claims: u8[]): strin
1893
1888
  for (let i: i32 = 0; i < size; i += 1) {
1894
1889
  tag.push(toU8(0))
1895
1890
  }
1896
- hmacKeyedInto(hmac, msg, 0, toI32(msg.length), tag, 0)
1891
+ // `false` once the `HmacKey` itself is wiped.
1892
+ if (!hmacKeyedInto(hmac, msg, 0, toI32(msg.length), tag, 0)) {
1893
+ return null
1894
+ }
1897
1895
  sig = tag
1898
1896
  } else if (key.alg === JWT_ALG_ES256) {
1899
1897
  sig = p256SignSha256(key.priv, msg)
@@ -5104,8 +5104,9 @@ const p256WipeSignScratch = (sc: P256SignScratch): void => {
5104
5104
  * `p256SignSha256` of `msg[off .. off + len)` in `scratch`, written as `r || s`
5105
5105
  * at `out[at .. at + 64)`: the same 64 bytes `p256SignSha256` answers for the
5106
5106
  * same key and message. Answers `false`, writing nothing, for a private key
5107
- * `p256PublicKey` would refuse. A message or signature window outside its
5108
- * array panics before anything is read.
5107
+ * `p256PublicKey` would refuse, for a signature window outside `out` before
5108
+ * anything is read, and for a message window outside `msg` before any nonce
5109
+ * is derived (`p256ShaInto`'s test).
5109
5110
  *
5110
5111
  * It runs inside an arena block of its own and stores nothing, so a signature
5111
5112
  * leaves the arena where it was; every secret the scratch held — RFC 6979's
@@ -5119,7 +5120,7 @@ const p256WipeSignScratch = (sc: P256SignScratch): void => {
5119
5120
  * @param len The message's length in bytes.
5120
5121
  * @param out The array the signature is written into.
5121
5122
  * @param at Where the 64 bytes start in `out`.
5122
- * @returns `true` once the signature is written, `false` for a refused key,
5123
+ * @returns `true` once the signature is written, `false` for a refused key or window,
5123
5124
  * for a scratch whose arrays a hashing step refuses (which
5124
5125
  * `new P256SignScratch()` never builds), or when RFC 6979 finds no usable
5125
5126
  * nonce within 64 rounds.
@@ -5133,11 +5134,8 @@ export const p256SignSha256Into = (
5133
5134
  out: u8[],
5134
5135
  at: i32
5135
5136
  ): boolean => {
5136
- if (!p256WindowFits(toI32(msg.length), off, len)) {
5137
- panic("p256SignSha256Into: the message window is outside its array")
5138
- }
5139
5137
  if (!p256WindowFits(toI32(out.length), at, P256_SIGNATURE_SIZE)) {
5140
- panic("p256SignSha256Into: the signature window is outside its array")
5138
+ return false
5141
5139
  }
5142
5140
  // Whether the table is built yet is public: it is the first signature or not. It
5143
5141
  // is built before the arena opens, so it outlives this call.
@@ -58,7 +58,11 @@ export class PkiRoots {
58
58
  roots: X509Certificate[]
59
59
  /** Every file that was read, in order, whether or not it held a root. */
60
60
  sources: string[]
61
- /** The PEM blocks that were not a root, and the files too large or past the cap to read. */
61
+ /**
62
+ * The PEM blocks that were not a root, and the files too large or past the
63
+ * cap to read. The count stops at `PKI_ROOTS_MAX_SKIPPED`: a value there
64
+ * means at least that many were skipped.
65
+ */
62
66
  skipped: i32 = 0
63
67
 
64
68
  /** An empty load: no roots, no sources, nothing skipped. */
@@ -87,11 +91,18 @@ export const pkiSystemBundles = (): string[] => [
87
91
  "/etc/ssl/cert.pem",
88
92
  ]
89
93
 
94
+ /** Where `PkiRoots.skipped` stops counting, 2^31 − 1: a value there means at least that many. */
95
+ export const PKI_ROOTS_MAX_SKIPPED: i32 = 2147483647
96
+
90
97
  /** The directory read beside the system bundle. */
91
98
  export const PKI_SYSTEM_DIR: string = "/etc/ssl/certs"
92
99
 
93
- /** What one load has read so far, and the DER already kept. */
94
- class PkiRootsLoad {
100
+ /**
101
+ * What one load has read so far, and the DER already kept.
102
+ *
103
+ * @internal
104
+ */
105
+ export class PkiRootsLoad {
95
106
  /** @internal */
96
107
  out: PkiRoots
97
108
  /** @internal */
@@ -99,6 +110,7 @@ class PkiRootsLoad {
99
110
  /** @internal */
100
111
  files: i32 = 0
101
112
 
113
+ /** A load that has read nothing: no roots, no sources, nothing skipped. */
102
114
  constructor() {
103
115
  this.out = new PkiRoots()
104
116
  this.seen = new Set<string>()
@@ -106,12 +118,20 @@ class PkiRootsLoad {
106
118
  }
107
119
 
108
120
  /**
109
- * Counts one PEM block or file skipped. The count is not capped: past
110
- * `PKI_ROOTS_MAX_FILES` a trust directory's entries are each counted without
111
- * a read, so no bound below 2^31 holds for it (PKI-R8).
121
+ * Counts one PEM block or file skipped, up to `PKI_ROOTS_MAX_SKIPPED`. Past
122
+ * `PKI_ROOTS_MAX_FILES` a trust directory's entries are each counted without a
123
+ * read, so no smaller bound holds; the count stops there rather than wrapping
124
+ * or panicking, and the field's doc says what a value at the cap means
125
+ * (PKI-R8).
126
+ *
127
+ * @internal
128
+ * @param load the load whose `out.skipped` counts one more skip
112
129
  */
113
- const pkiRootsSkip = (load: PkiRootsLoad): void => {
114
- load.out.skipped = load.out.skipped + 1
130
+ export const pkiRootsSkip = (load: PkiRootsLoad): void => {
131
+ const skipped: i32 = load.out.skipped
132
+ if (skipped < PKI_ROOTS_MAX_SKIPPED) {
133
+ load.out.skipped = skipped + 1
134
+ }
115
135
  }
116
136
 
117
137
  /**
@@ -471,11 +471,14 @@ export class RsaSignScratch {
471
471
  * @param counter MGF1's block counter, or -1 for none.
472
472
  * @param out The array the digest is written into.
473
473
  * @param at The index of the digest's first byte.
474
- * @returns Whether `out` had room for the digest.
474
+ * @returns Whether the input window was inside `data` and `out` had room
475
+ * for the digest.
475
476
  */
476
477
  hashInto(data: u8[], off: i32, len: i32, counter: i32, out: u8[], at: i32): boolean {
477
478
  hmacCopySha256(this.fresh, this.work)
478
- this.work.update(data, off, len)
479
+ if (!this.work.update(data, off, len)) {
480
+ return false
481
+ }
479
482
  if (counter >= 0) {
480
483
  const c: u8[] = [toU8(counter >> 24), toU8(counter >> 16), toU8(counter >> 8), toU8(counter)]
481
484
  this.work.update(c, toI32(0), toI32(4))
@@ -460,10 +460,10 @@ const sha256Compress = (h: u32[], w: u32[], src: u8[], at: i32): void => {
460
460
  * the digest does not depend on where the message was split. `digest` pads the
461
461
  * message (§5.1.1), runs the last block or two, and ends the computation: the
462
462
  * padding is written into this hasher's own state, so a later `update` would
463
- * carry the padding as message, and it panics instead of answering a wrong
464
- * value. A second `digest` answers the same digest again, and a `copy` of a
465
- * finished hasher is finished too. `copy` first when the computation has to go
466
- * on.
463
+ * carry the padding as message, so it answers `false` and absorbs nothing
464
+ * instead of hashing a wrong value. A second `digest` answers
465
+ * the same digest again, and a `copy` of a finished hasher is finished too.
466
+ * `copy` first when the computation has to go on.
467
467
  */
468
468
  export class Sha256 {
469
469
  /**
@@ -515,21 +515,34 @@ export class Sha256 {
515
515
  }
516
516
 
517
517
  /**
518
- * Absorbs `data[off .. off + len)`. A window outside `data` panics rather
519
- * than hashing a short read, since a digest over the wrong bytes looks
520
- * exactly like a digest over the right ones.
518
+ * Absorbs `data[off .. off + len)`. A window outside `data`, or a call
519
+ * after `digest`, answers `false` and absorbs nothing rather than hashing a
520
+ * short read. Check it, since a digest over the wrong bytes looks exactly
521
+ * like a digest over the right ones.
521
522
  *
522
523
  * @param data The buffer holding the window; it is read, not kept.
523
524
  * @param off Where the window starts in `data`.
524
525
  * @param len How many bytes to absorb; 0 absorbs nothing.
526
+ * @returns `true` once the window is absorbed; `false`, absorbing nothing,
527
+ * for a window outside `data` or a hasher already digested.
525
528
  */
526
- update(data: u8[], off: i32, len: i32): void {
527
- if (this.finished) {
528
- panic("Sha256: update after digest")
529
- }
530
- if (!sha256Absorb(this, data, off, len)) {
531
- panic("Sha256: the window is outside the buffer")
532
- }
529
+ update(data: u8[], off: i32, len: i32): boolean {
530
+ return !this.finished && sha256Absorb(this, data, off, len)
531
+ }
532
+
533
+ /**
534
+ * The same answer as `update`, kept for callers written against it: a
535
+ * window outside `data`, or an update after `digest`, answers `false` and
536
+ * absorbs nothing.
537
+ *
538
+ * @param data The buffer holding the window; it is read, not kept.
539
+ * @param off Where the window starts in `data`.
540
+ * @param len How many bytes to absorb; 0 absorbs nothing.
541
+ * @returns `true` once the window is absorbed; `false`, absorbing nothing,
542
+ * for a window outside `data` or a hasher already digested.
543
+ */
544
+ tryUpdate(data: u8[], off: i32, len: i32): boolean {
545
+ return !this.finished && sha256Absorb(this, data, off, len)
533
546
  }
534
547
 
535
548
  /**
@@ -537,20 +550,61 @@ export class Sha256 {
537
550
  * `update`. A longer one (possible under `--number-mode f64`, where `toI32`
538
551
  * of its length would saturate and leave a prefix) is walked with a `number`
539
552
  * offset instead, a block at a time through `block`, so it is absorbed
540
- * whole. After `digest` it panics, as `update` does.
553
+ * whole. After `digest` it answers `false` and absorbs nothing, as
554
+ * `update` does.
541
555
  *
542
556
  * @param data The message, or the rest of it; it is read, not kept.
557
+ * @returns `true` once `data` is absorbed; `false`, absorbing nothing, for a
558
+ * hasher already digested.
543
559
  */
544
- updateAll(data: u8[]): void {
545
- const n: number = data.length
546
- if (this.finished || n <= 2147483647) {
547
- // A finished hasher panics in `update` before it reads the window, so
548
- // the saturated length of a longer array is never used.
549
- const from: i32 = 0
550
- this.update(data, from, toI32(n))
551
- return
560
+ updateAll(data: u8[]): boolean {
561
+ if (this.finished) {
562
+ return false
552
563
  }
553
- sha256AbsorbLong(this, data)
564
+ sha256AbsorbWhole(this, data)
565
+ return true
566
+ }
567
+
568
+ /**
569
+ * The same answer as `updateAll`, kept for callers written against it:
570
+ * after `digest` it answers `false` and absorbs nothing.
571
+ *
572
+ * @param data The message, or the rest of it; it is read, not kept.
573
+ * @returns `true` once `data` is absorbed; `false`, absorbing nothing, for a
574
+ * hasher already digested.
575
+ */
576
+ tryUpdateAll(data: u8[]): boolean {
577
+ if (this.finished) {
578
+ return false
579
+ }
580
+ sha256AbsorbWhole(this, data)
581
+ return true
582
+ }
583
+
584
+ /**
585
+ * `updateAll` past its finished test, for a caller holding a hasher that
586
+ * `digest` cannot have ended: one it made itself and has not digested (the
587
+ * HMAC key and pad blocks, a tag's inner digest). The whole array is the
588
+ * window, so there is no window to get wrong.
589
+ *
590
+ * @internal Called by `nish/crypto/hmac`.
591
+ */
592
+ absorbWhole(data: u8[]): void {
593
+ sha256AbsorbWhole(this, data)
594
+ }
595
+
596
+ /**
597
+ * `update` past its finished test, for the same callers as `absorbWhole`
598
+ * with a window (an HMAC tag's inner digest). A window outside `data`
599
+ * absorbs nothing, as `tryUpdate`'s does.
600
+ *
601
+ * @internal Called by `nish/crypto/hmac`.
602
+ * @param data The bytes to read from; they are read, not kept.
603
+ * @param off Where the window starts in `data`.
604
+ * @param len How many bytes the window holds.
605
+ */
606
+ absorbWindow(data: u8[], off: i32, len: i32): void {
607
+ sha256Absorb(this, data, off, len)
554
608
  }
555
609
 
556
610
  /**
@@ -604,7 +658,7 @@ export class Sha256 {
604
658
  * hasher as it was and answers `false`, rather than writing the part of the
605
659
  * digest that fits. Once finished, the hash value is the digest, so a second
606
660
  * `digest` or `digestInto` writes the same 32 bytes again; an `update` after
607
- * it panics.
661
+ * it answers `false` and absorbs nothing.
608
662
  *
609
663
  * @param out The caller's array the digest is written into.
610
664
  * @param at Where the 32 digest bytes start in `out`.
@@ -748,7 +802,8 @@ const sha256AbsorbWhole = (h: Sha256, data: u8[]): void => {
748
802
  const n: number = data.length
749
803
  if (n <= 2147483647) {
750
804
  // The window is the whole array, so it always lies inside it.
751
- sha256Absorb(h, data, 0, toI32(n))
805
+ const from: i32 = 0
806
+ sha256Absorb(h, data, from, toI32(n))
752
807
  } else {
753
808
  sha256AbsorbLong(h, data)
754
809
  }
@@ -374,15 +374,31 @@ class Sha512Engine {
374
374
  }
375
375
  }
376
376
 
377
- /** @internal Called by `Sha512` and `Sha384`, which hold the engine. */
378
- absorb(data: u8[], off: i32, len: i32): void {
379
- if (this.finished) {
380
- panic("sha512: update after digest")
381
- }
377
+ /**
378
+ * Absorbs `data[off .. off + len)`: `false`, absorbing nothing, for a
379
+ * window outside `data` or an engine `finish` has ended.
380
+ *
381
+ * @internal Called by `Sha512` and `Sha384`, which hold the engine.
382
+ */
383
+ absorb(data: u8[], off: i32, len: i32): boolean {
382
384
  const size: i32 = toI32(data.length)
383
- if (off < 0 || len < 0 || off > size - len) {
384
- panic("sha512: the window is outside the buffer")
385
+ if (this.finished || off < 0 || len < 0 || off > size - len) {
386
+ return false
385
387
  }
388
+ this.absorbWindow(data, off, len)
389
+ return true
390
+ }
391
+
392
+ /**
393
+ * The body of `absorb` past its tests: absorbs `data[off .. off + len)`,
394
+ * a window its caller has checked lies inside `data`. Each loop's condition
395
+ * beyond its count bounds its own indices, so a bad window reads nothing
396
+ * outside `data`; it would only miscount, which is why every caller checks.
397
+ *
398
+ * @internal Called by `absorb` and `absorbWhole`.
399
+ */
400
+ absorbWindow(data: u8[], off: i32, len: i32): void {
401
+ const size: i32 = toI32(data.length)
386
402
  this.count = this.count + toU64(len)
387
403
  const block: u8[] = this.block
388
404
  const room: i32 = toI32(block.length)
@@ -421,19 +437,44 @@ class Sha512Engine {
421
437
 
422
438
  /**
423
439
  * 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.
440
+ * `absorbWindow`. A longer one (possible under `--number-mode f64`, where
441
+ * `toI32` of its length would saturate and leave a prefix) is walked with a
442
+ * `number` offset instead, a block at a time through `block`, so it is
443
+ * absorbed whole. After `finish` it answers `false` and absorbs nothing,
444
+ * as `absorb` does; otherwise `true`.
445
+ *
446
+ * @internal Called by `nish/crypto/hmac` and `nish/crypto/hkdf`.
447
+ */
448
+ absorbAll(data: u8[]): boolean {
449
+ if (this.finished) {
450
+ return false
451
+ }
452
+ this.absorbWhole(data)
453
+ return true
454
+ }
455
+
456
+ /**
457
+ * The same answer as `absorbAll`, kept for callers written against it.
428
458
  *
429
- * @internal Called by `sha512`, `sha384` and `nish/crypto/hmac`.
459
+ * @internal Called by `nish/crypto/hkdf`.
430
460
  */
431
- absorbAll(data: u8[]): void {
461
+ tryAbsorbAll(data: u8[]): boolean {
462
+ return this.absorbAll(data)
463
+ }
464
+
465
+ /**
466
+ * `absorbAll` past its finished test, for a caller holding an engine that
467
+ * `finish` cannot have ended: one it made itself and has not digested
468
+ * (`sha512`, `sha384`, the HMAC key and pad blocks, a tag's inner digest).
469
+ * The whole array is the window, so there is no window to get wrong.
470
+ *
471
+ * @internal Called by `sha512`, `sha384`, `nish/crypto/hmac`,
472
+ * `nish/crypto/hkdf` and `nish/crypto/ed25519`.
473
+ */
474
+ absorbWhole(data: u8[]): void {
432
475
  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))
476
+ if (n <= 2147483647) {
477
+ this.absorbWindow(data, OFFSET_ZERO, toI32(n))
437
478
  return
438
479
  }
439
480
  this.count += toU64(toI64(n))
@@ -482,13 +523,34 @@ class Sha512Engine {
482
523
  /**
483
524
  * Pads (§5.1.2), compresses what is left, and writes the first
484
525
  * `out.length / 8` words of H(N) (§6.4.2, §6.5), at most eight, into `out`.
526
+ * A second call writes the same words: the padding is absorbed once.
485
527
  *
486
528
  * @internal Called by `Sha512` and `Sha384`, which hold the engine.
487
529
  */
488
530
  finish(out: u8[]): void {
489
- if (this.finished) {
490
- panic("sha512: digest taken twice")
531
+ if (!this.finished) {
532
+ this.pad()
491
533
  }
534
+ // Both sizes are whole words, so the digest is the first `out.length / 8`.
535
+ const state: u64[] = this.state
536
+ const size: i32 = toI32(out.length)
537
+ let p: i32 = 0
538
+ for (let i: i32 = 0; i < 8 && i < toI32(state.length) && p < size - 7; i++) {
539
+ const word: u64 = state[i]
540
+ out[p] = toU8(word >> 56)
541
+ out[p + 1] = toU8(word >> 48)
542
+ out[p + 2] = toU8(word >> 40)
543
+ out[p + 3] = toU8(word >> 32)
544
+ out[p + 4] = toU8(word >> 24)
545
+ out[p + 5] = toU8(word >> 16)
546
+ out[p + 6] = toU8(word >> 8)
547
+ out[p + 7] = toU8(word)
548
+ p += 8
549
+ }
550
+ }
551
+
552
+ /** Appends the padding and the length (§5.1.2) and compresses them: `finish`'s first call. */
553
+ private pad(): void {
492
554
  this.finished = true
493
555
  const block = this.block
494
556
  const room: i32 = toI32(block.length)
@@ -514,29 +576,14 @@ class Sha512Engine {
514
576
  shift += 8
515
577
  }
516
578
  this.compress(block, OFFSET_ZERO)
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
532
- }
533
579
  }
534
580
  }
535
581
 
536
582
  /**
537
583
  * A streaming SHA-512 hasher. `update` takes a window `(data, off, len)`;
538
- * `digest` answers a fresh 64-byte array, after which the hasher is spent and
539
- * another `update` or `digest` panics.
584
+ * `digest` answers a fresh 64-byte array and ends the message: a later
585
+ * `update` answers `false` and absorbs nothing, and a later `digest` answers
586
+ * the same digest.
540
587
  */
541
588
  export class Sha512 {
542
589
  /**
@@ -555,15 +602,34 @@ export class Sha512 {
555
602
  }
556
603
 
557
604
  /**
558
- * Absorbs `data[off .. off + len)`. A window outside `data`, or an update
559
- * after `digest`, panics.
605
+ * Absorbs `data[off .. off + len)`. A window outside `data`, or a call
606
+ * after `digest`, answers `false` and absorbs nothing rather than hashing a
607
+ * short read. Check it, since a digest over the wrong bytes looks exactly
608
+ * like a digest over the right ones.
609
+ *
610
+ * @param data The buffer holding the window; it is read, not kept.
611
+ * @param off Where the window starts in `data`.
612
+ * @param len How many bytes to absorb; 0 absorbs nothing.
613
+ * @returns `true` once the window is absorbed; `false`, absorbing nothing,
614
+ * for a window outside `data` or a hasher already digested.
615
+ */
616
+ update(data: u8[], off: i32, len: i32): boolean {
617
+ return this.engine.absorb(data, off, len)
618
+ }
619
+
620
+ /**
621
+ * The same answer as `update`, kept for callers written against it: a
622
+ * window outside `data`, or an update after `digest`, answers `false` and
623
+ * absorbs nothing.
560
624
  *
561
625
  * @param data The buffer holding the window; it is read, not kept.
562
626
  * @param off Where the window starts in `data`.
563
627
  * @param len How many bytes to absorb; 0 absorbs nothing.
628
+ * @returns `true` once the window is absorbed; `false`, absorbing nothing,
629
+ * for a window outside `data` or a hasher already digested.
564
630
  */
565
- update(data: u8[], off: i32, len: i32): void {
566
- this.engine.absorb(data, off, len)
631
+ tryUpdate(data: u8[], off: i32, len: i32): boolean {
632
+ return this.engine.absorb(data, off, len)
567
633
  }
568
634
 
569
635
  /**
@@ -581,7 +647,7 @@ export class Sha512 {
581
647
 
582
648
  /**
583
649
  * Pads the message (§5.1.2) and answers its digest. Ends the computation: a
584
- * second `digest` panics.
650
+ * second `digest` answers the same digest.
585
651
  *
586
652
  * @returns A fresh 64-byte digest, which the caller owns.
587
653
  */
@@ -613,15 +679,34 @@ export class Sha384 {
613
679
  }
614
680
 
615
681
  /**
616
- * Absorbs `data[off .. off + len)`. A window outside `data`, or an update
617
- * after `digest`, panics.
682
+ * Absorbs `data[off .. off + len)`. A window outside `data`, or a call
683
+ * after `digest`, answers `false` and absorbs nothing rather than hashing a
684
+ * short read. Check it, since a digest over the wrong bytes looks exactly
685
+ * like a digest over the right ones.
686
+ *
687
+ * @param data The buffer holding the window; it is read, not kept.
688
+ * @param off Where the window starts in `data`.
689
+ * @param len How many bytes to absorb; 0 absorbs nothing.
690
+ * @returns `true` once the window is absorbed; `false`, absorbing nothing,
691
+ * for a window outside `data` or a hasher already digested.
692
+ */
693
+ update(data: u8[], off: i32, len: i32): boolean {
694
+ return this.engine.absorb(data, off, len)
695
+ }
696
+
697
+ /**
698
+ * The same answer as `update`, kept for callers written against it: a
699
+ * window outside `data`, or an update after `digest`, answers `false` and
700
+ * absorbs nothing.
618
701
  *
619
702
  * @param data The buffer holding the window; it is read, not kept.
620
703
  * @param off Where the window starts in `data`.
621
704
  * @param len How many bytes to absorb; 0 absorbs nothing.
705
+ * @returns `true` once the window is absorbed; `false`, absorbing nothing,
706
+ * for a window outside `data` or a hasher already digested.
622
707
  */
623
- update(data: u8[], off: i32, len: i32): void {
624
- this.engine.absorb(data, off, len)
708
+ tryUpdate(data: u8[], off: i32, len: i32): boolean {
709
+ return this.engine.absorb(data, off, len)
625
710
  }
626
711
 
627
712
  /**
@@ -639,7 +724,8 @@ export class Sha384 {
639
724
 
640
725
  /**
641
726
  * Pads the message (§5.1.2) and answers its digest, the first 48 bytes of the
642
- * final hash value (§6.5). Ends the computation: a second `digest` panics.
727
+ * final hash value (§6.5). Ends the computation: a second `digest` answers
728
+ * the same digest.
643
729
  *
644
730
  * @returns A fresh 48-byte digest, which the caller owns.
645
731
  */
@@ -660,7 +746,7 @@ export class Sha384 {
660
746
  */
661
747
  export const sha512 = (data: u8[]): u8[] => {
662
748
  const hasher = new Sha512()
663
- hasher.engine.absorbAll(data)
749
+ hasher.engine.absorbWhole(data)
664
750
  return hasher.digest()
665
751
  }
666
752
 
@@ -673,6 +759,6 @@ export const sha512 = (data: u8[]): u8[] => {
673
759
  */
674
760
  export const sha384 = (data: u8[]): u8[] => {
675
761
  const hasher = new Sha384()
676
- hasher.engine.absorbAll(data)
762
+ hasher.engine.absorbWhole(data)
677
763
  return hasher.digest()
678
764
  }
package/std/json-value.ts CHANGED
@@ -2333,20 +2333,21 @@ const jsonValueSpan = (from: i32, to: i32): i32 => (from < 0 || to < from ? -1 :
2333
2333
  * `a` when it holds at least `n` elements, and otherwise a copy of it at
2334
2334
  * least twice as long, for the caller to keep in `a`'s place. Doubling is
2335
2335
  * what keeps a run of appends linear, and the old array is left in the arena.
2336
- * The copy is built by `push`, so no length is computed for `new Array`.
2336
+ * The copy is one zeroed allocation and one bulk `set`, not a `push` per
2337
+ * element. `jsonValueGrownLength` answers at most `JSON_VALUE_I32_MAX`, so
2338
+ * clearing its top bit (`<< 1 >>> 1`), which is what proves the
2339
+ * allocation's length non-negative, changes nothing.
2337
2340
  */
2338
2341
  const jsonValueRoom = (a: u8[], n: i32): u8[] => {
2339
2342
  const have: i32 = toI32(a.length)
2340
2343
  if (n <= have) {
2341
2344
  return a
2342
2345
  }
2343
- const target: u32 = jsonValueGrownLength(have, n)
2344
- const grown: u8[] = []
2345
- for (let i: i32 = 0; i < toI32(a.length); i += 1) {
2346
- grown.push(a[i])
2347
- }
2348
- while (toU32(grown.length) < target) {
2349
- grown.push(0)
2346
+ const target: i32 = toI32((jsonValueGrownLength(have, n) << toU32(1)) >>> toU32(1))
2347
+ const grown: u8[] = new Array<u8>(target)
2348
+ // Always true (`target` > `have`); the prover does not carry `grown.length === target` from `new Array`.
2349
+ if (toI32(a.length) <= toI32(grown.length)) {
2350
+ grown.set(a, 0)
2350
2351
  }
2351
2352
  return grown
2352
2353
  }
@@ -2357,13 +2358,11 @@ const jsonValueRoomI32 = (a: i32[], n: i32): i32[] => {
2357
2358
  if (n <= have) {
2358
2359
  return a
2359
2360
  }
2360
- const target: u32 = jsonValueGrownLength(have, n)
2361
- const grown: i32[] = []
2362
- for (let i: i32 = 0; i < toI32(a.length); i += 1) {
2363
- grown.push(a[i])
2364
- }
2365
- while (toU32(grown.length) < target) {
2366
- grown.push(0)
2361
+ const target: i32 = toI32((jsonValueGrownLength(have, n) << toU32(1)) >>> toU32(1))
2362
+ const grown: i32[] = new Array<i32>(target)
2363
+ // Always true (`target` > `have`); the prover does not carry `grown.length === target` from `new Array`.
2364
+ if (toI32(a.length) <= toI32(grown.length)) {
2365
+ grown.set(a, 0)
2367
2366
  }
2368
2367
  return grown
2369
2368
  }