nish 0.49.0 → 0.50.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 (92) hide show
  1. package/README.md +27 -21
  2. package/docs/AI.md +233 -159
  3. package/docs/INSTALL.md +1 -1
  4. package/package.json +12 -12
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +12 -9
  7. package/std/README.md +8 -5
  8. package/std/base64.ts +74 -1
  9. package/std/bytes.ts +201 -93
  10. package/std/compress/deflate.ts +20 -15
  11. package/std/crypto/base64url.ts +93 -66
  12. package/std/crypto/bcrypt.ts +25 -21
  13. package/std/crypto/hmac.ts +1 -1
  14. package/std/crypto/jwt.ts +32 -19
  15. package/std/crypto/p256.ts +9 -12
  16. package/std/crypto/pki-roots.ts +7 -4
  17. package/std/crypto/pki.ts +43 -55
  18. package/std/crypto/rsa-sign.ts +1632 -0
  19. package/std/crypto/rsa.ts +26 -4
  20. package/std/crypto/sha256.ts +135 -68
  21. package/std/crypto/x25519.ts +12 -11
  22. package/std/crypto/x509.ts +164 -40
  23. package/std/gpu/sys.ts +490 -0
  24. package/std/json-equal.ts +33 -18
  25. package/std/json-format.ts +39 -8
  26. package/std/json-number.ts +15 -3
  27. package/std/json-tokenizer.ts +4 -3
  28. package/std/json-typed.ts +29 -12
  29. package/std/json-value.ts +222 -104
  30. package/std/json.ts +81 -46
  31. package/std/net/dns-config.ts +18 -5
  32. package/std/net/dns-message.ts +60 -27
  33. package/std/net/dns-resolver.ts +35 -16
  34. package/std/net/dns.ts +3 -2
  35. package/std/net/fetch.ts +120 -38
  36. package/std/net/handler.ts +178 -66
  37. package/std/net/hpack.ts +192 -98
  38. package/std/net/http-compress.ts +83 -30
  39. package/std/net/http-fields.ts +327 -31
  40. package/std/net/http-server.ts +317 -20
  41. package/std/net/http1-client-codec.ts +137 -40
  42. package/std/net/http1-codec.ts +391 -202
  43. package/std/net/http1-server.ts +220 -38
  44. package/std/net/http2-cleartext.ts +10 -4
  45. package/std/net/http2-frame.ts +2 -2
  46. package/std/net/http2-tls.ts +3 -3
  47. package/std/net/http2.ts +69 -15
  48. package/std/net/http3-frame.ts +11 -14
  49. package/std/net/http3-server.ts +12 -15
  50. package/std/net/http3.ts +41 -29
  51. package/std/net/multipart.ts +592 -0
  52. package/std/net/outbound.ts +24 -8
  53. package/std/net/postgres-async.ts +92 -54
  54. package/std/net/postgres-carrier.ts +28 -4
  55. package/std/net/postgres.ts +166 -65
  56. package/std/net/prefork.ts +86 -5
  57. package/std/net/protobuf.ts +6 -2
  58. package/std/net/qpack.ts +66 -25
  59. package/std/net/quic-conn-ack.ts +27 -7
  60. package/std/net/quic-conn-cid.ts +13 -7
  61. package/std/net/quic-datagram.ts +6 -5
  62. package/std/net/quic-frame.ts +67 -23
  63. package/std/net/quic-listener.ts +19 -12
  64. package/std/net/quic-recovery.ts +11 -9
  65. package/std/net/quic-stream.ts +7 -5
  66. package/std/net/quic.ts +36 -31
  67. package/std/net/router.ts +136 -24
  68. package/std/net/serve.ts +59 -5
  69. package/std/net/static.ts +81 -33
  70. package/std/net/tls/record.ts +26 -13
  71. package/std/net/tls/ticket.ts +6 -5
  72. package/std/net/tls-tcp.ts +69 -30
  73. package/std/net/tls.ts +152 -38
  74. package/std/net/upstream.ts +65 -19
  75. package/std/net/url.ts +97 -78
  76. package/std/net/websocket.ts +33 -15
  77. package/std/net/webtransport.ts +28 -12
  78. package/std/regex.ts +600 -388
  79. package/std/sqlite.ts +37 -19
  80. package/std/sync/ring.ts +232 -115
  81. package/std/sync/shm.ts +11 -4
  82. package/std/temporal.ts +232 -133
  83. package/std/term-io.ts +12 -6
  84. package/std/term-pixels.ts +158 -22
  85. package/std/term-rate.ts +8 -3
  86. package/std/term.ts +4 -4
  87. package/std/text.ts +33 -11
  88. package/std/threads.ts +52 -8
  89. package/std/yaml-json.ts +238 -106
  90. package/std/yaml-lines.ts +47 -27
  91. package/std/yaml-write.ts +58 -24
  92. package/std/yaml.ts +808 -395
package/std/net/hpack.ts CHANGED
@@ -79,6 +79,8 @@
79
79
  * one against the appendix's table.
80
80
  */
81
81
 
82
+ import { wrappingAdd } from "nish:unsafe"
83
+
82
84
  /** A decode that read the whole block and answered every field. */
83
85
  export const HPACK_OK: i32 = 0
84
86
 
@@ -386,29 +388,58 @@ const hpackSameAsWindow = (bytes: u8[], store: u8[], at: i32, n: i32): boolean =
386
388
  if (toI32(bytes.length) !== n) {
387
389
  return false
388
390
  }
391
+ // `j` walks the window alongside `i`, and steps on only from inside `store`, so it never passes
392
+ // `store.length`; a window that leaves `store` reads as a difference.
393
+ let j: i32 = at
389
394
  for (let i: i32 = 0; i < n && i < toI32(bytes.length); i += 1) {
390
- // `at` and `i` are each below 2^31, so the unsigned sum cannot wrap; a sum past `store`, a window
391
- // the caller has checked, reads as a difference.
392
- const j: i32 = toI32(toU32(at) + toU32(i))
393
- if (at < 0 || j < 0 || j >= toI32(store.length) || bytes[i] !== store[j]) {
395
+ if (j < 0 || j >= toI32(store.length) || bytes[i] !== store[j]) {
394
396
  return false
395
397
  }
398
+ j += 1
396
399
  }
397
400
  return true
398
401
  }
399
402
 
400
403
  /**
401
- * What an entry of this name and value costs in the table (§4.1), in `i64`
402
- * because two lengths of up to 2^31 - 1 and the overhead do not fit an `i32`.
404
+ * The dynamic table index of the combined index `index`, which must be past the static table's 61 entries,
405
+ * or -1 for one that is not. The literal is `HPACK_STATIC_LENGTH`, spelled out because the bounds prover
406
+ * reads only a literal, and it is what proves the difference cannot overflow.
403
407
  */
404
- const hpackEntrySize = (name: u8[], value: u8[]): i64 =>
405
- toI64(name.length) + toI64(value.length) + toI64(HPACK_ENTRY_OVERHEAD)
408
+ const hpackDynamicIndex = (index: i32): i32 => {
409
+ let i: i32 = -1
410
+ if (index > 61) {
411
+ i = index - 61
412
+ }
413
+ return i
414
+ }
406
415
 
407
- /** Panics unless `[off, off + len)` lies inside `buf`; the window is the caller's. */
416
+ /**
417
+ * `off + len` when `buf[off, off + len)` lies inside `buf`, else -1. The sum is taken in `i64`, where two
418
+ * `i32`s cannot overflow; one no larger than `buf.length` fits an `i32` again.
419
+ */
420
+ const hpackWindowEnd = (buf: u8[], off: i32, len: i32): i32 => {
421
+ const end: i64 = toI64(off) + toI64(len)
422
+ if (off < 0 || len < 0 || end > toI64(buf.length)) {
423
+ return -1
424
+ }
425
+ return toI32(end)
426
+ }
427
+
428
+ /**
429
+ * The panic of a window outside `buf`, `hpackCheckedWindowEnd`'s, kept apart from its test so that the
430
+ * test stays small enough to inline; the window is the caller's.
431
+ */
408
432
  const hpackCheckWindow = (what: string, buf: u8[], off: i32, len: i32): void => {
409
- if (off < 0 || len < 0 || toI64(off) + toI64(len) > toI64(buf.length)) {
410
- panic(`${what}: the window [${off}, ${off} + ${len}) is outside a buffer of ${toI32(buf.length)} bytes`)
433
+ panic(`${what}: the window [${off}, ${off} + ${len}) is outside a buffer of ${toI32(buf.length)} bytes`)
434
+ }
435
+
436
+ /** Panics unless `[off, off + len)` lies inside `buf`, and answers the window's end, `off + len`. */
437
+ const hpackCheckedWindowEnd = (what: string, buf: u8[], off: i32, len: i32): i32 => {
438
+ const end: i32 = hpackWindowEnd(buf, off, len)
439
+ if (end < 0) {
440
+ hpackCheckWindow(what, buf, off, len)
411
441
  }
442
+ return end
412
443
  }
413
444
 
414
445
  /**
@@ -454,7 +485,7 @@ export class HpackHuffman {
454
485
  * Where each length's symbols start in `symbols`, by length.
455
486
  * @internal Read by `hpackHuffmanDecode`; not for callers.
456
487
  */
457
- baseRank: i32[]
488
+ baseRank: u32[]
458
489
 
459
490
  /** Rebuilds the code from Appendix B's 257 code lengths. Build one and reuse it: each build allocates. */
460
491
  constructor() {
@@ -465,23 +496,25 @@ export class HpackHuffman {
465
496
  this.symbols = new Array<i32>(257)
466
497
  this.firstCode = new Array<i32>(31)
467
498
  this.countAt = new Array<i32>(31)
468
- this.baseRank = new Array<i32>(31)
499
+ this.baseRank = new Array<u32>(31)
469
500
  // Every bound below is the array's own length, which the constants fix at 257 symbols and 31 code
470
501
  // lengths, so no test ever stops a loop early.
471
502
  const table: string = HPACK_HUFFMAN_LENGTHS
472
503
  for (let sym: i32 = 0; sym < toI32(this.lengths.length) && sym < toI32(table.length); sym += 1) {
473
504
  this.lengths[sym] = toI32(table.charCodeAt(sym)) - 60
474
505
  }
475
- // How many symbols each length has. Appendix B's lengths are 5 to 30, so every one lands in a row.
506
+ // How many symbols each length has, counted in the unsigned `baseRank` rows, which 257 symbols cannot
507
+ // wrap. Appendix B's lengths are 5 to 30, so every one lands in a row.
476
508
  for (let sym: i32 = 0; sym < toI32(this.lengths.length); sym += 1) {
477
509
  const len: i32 = this.lengths[sym]
478
- if (len >= 0 && len < toI32(this.countAt.length)) {
479
- this.countAt[len] = toI32(toU32(this.countAt[len]) + 1)
510
+ if (len >= 0 && len < toI32(this.baseRank.length)) {
511
+ this.baseRank[len] += 1
480
512
  }
481
513
  }
482
514
  // Canonical order is by length, then by symbol: each length's first code and rank follow from the
483
515
  // counts of the shorter ones. Unsigned: the code is below 2^30 and the rank at most 257, so neither
484
- // can wrap. Each count goes back to 0, to count again as the pass after hands the codes out.
516
+ // can wrap. Each count becomes its length's first rank, the next slot of `symbols` the pass after
517
+ // fills.
485
518
  let code: u32 = 0
486
519
  let rank: u32 = 0
487
520
  for (
@@ -492,34 +525,46 @@ export class HpackHuffman {
492
525
  len < toI32(this.countAt.length);
493
526
  len += 1
494
527
  ) {
528
+ const count: u32 = this.baseRank[len]
495
529
  this.firstCode[len] = toI32(code)
496
- this.baseRank[len] = toI32(rank)
497
- const count: u32 = toU32(this.countAt[len])
498
- this.countAt[len] = 0
530
+ this.baseRank[len] = rank
531
+ this.countAt[len] = toI32(rank)
499
532
  code = (code + count) << 1
500
533
  rank += count
501
534
  }
502
- // Walking the symbols in order hands each length its codes in symbol order, as the canonical code
503
- // does, and leaves every count as it was. Appendix B has 257 symbols, so the rank never reaches
504
- // past `symbols`.
535
+ // Walking the symbols in order hands each length its slots of `symbols` and its codes in symbol
536
+ // order, as the canonical code does, with the count holding the next slot and the first code the next
537
+ // code. Appendix B has 257 symbols and codes below 2^30, so the tests never refuse a symbol; they are
538
+ // what keep the slot in `symbols` and prove the increments cannot overflow.
505
539
  for (let sym: i32 = 0; sym < toI32(this.lengths.length) && sym < toI32(this.codes.length); sym += 1) {
506
540
  const len: i32 = this.lengths[sym]
507
- if (
508
- len >= 5 &&
509
- len <= HPACK_HUFFMAN_MAX_BITS &&
510
- len < toI32(this.firstCode.length) &&
511
- len < toI32(this.baseRank.length) &&
512
- len < toI32(this.countAt.length)
513
- ) {
514
- const done: u32 = toU32(this.countAt[len])
515
- const at: i32 = toI32(toU32(this.baseRank[len]) + done)
516
- if (at >= 0 && at < toI32(this.symbols.length)) {
517
- this.codes[sym] = toI32(toU32(this.firstCode[len]) + done)
541
+ if (len >= 5 && len < toI32(this.countAt.length) && len < toI32(this.firstCode.length)) {
542
+ const at: i32 = this.countAt[len]
543
+ const next: i32 = this.firstCode[len]
544
+ if (at >= 0 && at < toI32(this.symbols.length) && next < 1073741824) {
518
545
  this.symbols[at] = sym
519
- this.countAt[len] = toI32(done + 1)
546
+ this.codes[sym] = next
547
+ this.countAt[len] = at + 1
548
+ this.firstCode[len] = next + 1
520
549
  }
521
550
  }
522
551
  }
552
+ // The same walk over the lengths as above puts each first code back, and each count, from the slot
553
+ // past its run less the run's first rank. Unsigned, as above.
554
+ code = 0
555
+ rank = 0
556
+ for (
557
+ let len: i32 = 5;
558
+ len <= HPACK_HUFFMAN_MAX_BITS && len < toI32(this.firstCode.length) && len < toI32(this.countAt.length);
559
+ len += 1
560
+ ) {
561
+ const end: u32 = toU32(this.countAt[len])
562
+ const count: u32 = end - rank
563
+ this.firstCode[len] = toI32(code)
564
+ this.countAt[len] = toI32(count)
565
+ code = (code + count) << 1
566
+ rank = end
567
+ }
523
568
  }
524
569
  }
525
570
 
@@ -536,7 +581,7 @@ export class HpackHuffman {
536
581
  * internal fields).
537
582
  */
538
583
  export const hpackHuffmanLength = (h: HpackHuffman, src: u8[], off: i32, len: i32): i32 => {
539
- hpackCheckWindow("hpackHuffmanLength", src, off, len)
584
+ hpackCheckedWindowEnd("hpackHuffmanLength", src, off, len)
540
585
  // Unsigned: at most 30 bits for each of fewer than 2^31 octets, far below 2^64.
541
586
  let bits: u64 = 0
542
587
  // Widened, so `off + len` cannot wrap. The window check has already proved
@@ -573,7 +618,7 @@ export const hpackHuffmanLength = (h: HpackHuffman, src: u8[], off: i32, len: i3
573
618
  * @param out Where the coded bytes go: appended after what it holds, `hpackHuffmanLength` of them.
574
619
  */
575
620
  export const hpackHuffmanEncode = (h: HpackHuffman, src: u8[], off: i32, len: i32, out: u8[]): void => {
576
- hpackCheckWindow("hpackHuffmanEncode", src, off, len)
621
+ hpackCheckedWindowEnd("hpackHuffmanEncode", src, off, len)
577
622
  let acc: u64 = 0
578
623
  // Unsigned: fewer than eight waiting bits plus one code of at most 31, so it never wraps.
579
624
  let bits: u32 = 0
@@ -620,7 +665,7 @@ export const hpackHuffmanEncode = (h: HpackHuffman, src: u8[], off: i32, len: i3
620
665
  * which only a table cut short (by writing its internal fields) allows.
621
666
  */
622
667
  export const hpackHuffmanDecode = (h: HpackHuffman, src: u8[], off: i32, len: i32, out: u8[]): i32 => {
623
- hpackCheckWindow("hpackHuffmanDecode", src, off, len)
668
+ hpackCheckedWindowEnd("hpackHuffmanDecode", src, off, len)
624
669
  // Unsigned, and `bits` never passes 30: the code is complete, so thirty bits always end a symbol,
625
670
  // and the row test below gives up before a 31st.
626
671
  let code: u32 = 0
@@ -644,13 +689,13 @@ export const hpackHuffmanDecode = (h: HpackHuffman, src: u8[], off: i32, len: i3
644
689
  ) {
645
690
  return HPACK_ERR_HUFFMAN_PADDING
646
691
  }
647
- // Both sides fit an `i32`, so the `i64` difference cannot wrap.
692
+ // The difference is taken in `i64`, where it cannot overflow: the code is under 2^30 and every
693
+ // table entry an `i32`. Past the test `at` is in `[0, 2^31)`, which `toU32` keeps whole, and the
694
+ // slot is `u32` like the rank it starts from; a slot outside `symbols` matches no code.
648
695
  const at: i64 = toI64(code) - toI64(h.firstCode[row])
649
696
  if (at >= 0 && at < toI64(h.countAt[row])) {
650
- // `at` is below `h.countAt[row]`, so in `[0, 2^31)`; the unsigned sum of two such cannot wrap,
651
- // and one past `symbols` matches no code.
652
- const slot: i32 = toI32(toU32(h.baseRank[row]) + toU32(at))
653
- if (slot < 0 || slot >= toI32(h.symbols.length)) {
697
+ const slot: u32 = h.baseRank[row] + toU32(at)
698
+ if (slot >= toU32(h.symbols.length)) {
654
699
  return HPACK_ERR_HUFFMAN_PADDING
655
700
  }
656
701
  const sym: i32 = h.symbols[slot]
@@ -696,12 +741,9 @@ export class HpackCursor {
696
741
  * @param len How many bytes from `off` on the cursor may read.
697
742
  */
698
743
  constructor(buf: u8[], off: i32, len: i32) {
699
- hpackCheckWindow("HpackCursor", buf, off, len)
700
744
  this.buf = buf
701
745
  this.pos = off
702
- // The window check proved `off` and `len` non-negative with a sum at most `buf.length`, so the unsigned
703
- // sum does not wrap.
704
- this.end = toI32(toU32(off) + toU32(len))
746
+ this.end = hpackCheckedWindowEnd("HpackCursor", buf, off, len)
705
747
  }
706
748
 
707
749
  /**
@@ -709,12 +751,9 @@ export class HpackCursor {
709
751
  * @internal Called by `HpackDecoder.decode` for each block; not for callers.
710
752
  */
711
753
  reset(buf: u8[], off: i32, len: i32): void {
712
- hpackCheckWindow("HpackCursor", buf, off, len)
713
754
  this.buf = buf
714
755
  this.pos = off
715
- // The window check proved `off` and `len` non-negative with a sum at most `buf.length`, so the unsigned
716
- // sum does not wrap.
717
- this.end = toI32(toU32(off) + toU32(len))
756
+ this.end = hpackCheckedWindowEnd("HpackCursor", buf, off, len)
718
757
  }
719
758
  }
720
759
 
@@ -734,14 +773,19 @@ export const hpackEncodeInteger = (out: u8[], flags: i32, prefixBits: i32, value
734
773
  if (prefixBits < 1 || prefixBits > 8 || value < 0) {
735
774
  panic(`hpackEncodeInteger: a value of ${value} with a ${prefixBits}-bit prefix, outside 1 to 8`)
736
775
  }
737
- // Unsigned: the prefix is 1 to 8 bits, so `max` is 1 to 255, and `value` is at least `max` below.
738
- const max: i32 = toI32((toU32(1) << toU32(prefixBits)) - toU32(1))
776
+ // The prefix is 1 to 8 bits, so `max` is 1 to 255, and `value` is at least `max` below.
777
+ const max: i32 = toI32(255) >> (8 - prefixBits)
739
778
  if (value < max) {
740
779
  out.push(toU8(flags | value))
741
780
  return
742
781
  }
743
782
  out.push(toU8(flags | max))
744
- let rest: i32 = toI32(toU32(value) - toU32(max))
783
+ // `value` is at least `max` here and `max` is positive, so the difference is ordered; the test is what
784
+ // proves it.
785
+ let rest: i32 = 0
786
+ if (max >= 0 && value >= max) {
787
+ rest = value - max
788
+ }
745
789
  while (rest >= 128) {
746
790
  out.push(toU8((rest & 127) | 128))
747
791
  rest = rest >> 7
@@ -774,7 +818,7 @@ export const hpackDecodeInteger = (c: HpackCursor, prefixBits: i32): i32 => {
774
818
  if (at >= c.end || at < 0 || at >= toI32(buf.length)) {
775
819
  return HPACK_ERR_TRUNCATED
776
820
  }
777
- const max: i32 = toI32((toU32(1) << toU32(prefixBits)) - toU32(1))
821
+ const max: i32 = toI32(255) >> (8 - prefixBits)
778
822
  const first: i32 = toI32(buf[at]) & max
779
823
  c.pos = at + 1
780
824
  if (first < max) {
@@ -993,11 +1037,9 @@ export class HpackTable {
993
1037
  valueStart(i: i32): i32 {
994
1038
  const start: i32 = this.nameStart(i)
995
1039
  const length: i32 = this.nameLength(i)
996
- if (start < 0 || length < 0) {
997
- return -1
998
- }
999
- // Both are non-negative, and the name lies inside `bytes`, so the unsigned sum does not wrap.
1000
- return toI32(toU32(start) + toU32(length))
1040
+ // The name lies inside `bytes`, so its end is the window's end, which cannot overflow; a name
1041
+ // outside `bytes`, which only a caller writing the public fields makes, has no value.
1042
+ return hpackWindowEnd(this.bytes, start, length)
1001
1043
  }
1002
1044
 
1003
1045
  /**
@@ -1010,8 +1052,13 @@ export class HpackTable {
1010
1052
  if (k < 0 || start < 0) {
1011
1053
  return -1
1012
1054
  }
1013
- // The value ends at or after it starts, so the unsigned difference does not wrap.
1014
- return toI32(toU32(this.endOf(k)) - toU32(start))
1055
+ // The value ends at or after it starts, so the difference is ordered; the test is what proves it.
1056
+ const end: i32 = this.endOf(k)
1057
+ let length: i32 = 0
1058
+ if (end > start) {
1059
+ length = end - start
1060
+ }
1061
+ return length
1015
1062
  }
1016
1063
 
1017
1064
  /**
@@ -1050,11 +1097,23 @@ export class HpackTable {
1050
1097
  * @internal Called by `add`, `resize` and `HpackDecoder`; not for callers.
1051
1098
  */
1052
1099
  evictTo(limit: i32): void {
1053
- while (toI64(this.size) > toI64(limit) && this.count() > 0) {
1054
- // The size counts every live entry's cost, so the unsigned difference does not wrap; and `first` is
1055
- // below the slot count, under 2^30, while `count()` is positive.
1056
- this.size = toI32(toU32(this.size) - toU32(this.costOf(this.first)))
1057
- this.first = toI32(toU32(this.first) + 1)
1100
+ while (toI64(this.size) > toI64(limit)) {
1101
+ // `count()` is positive exactly when `first` is a slot below the slot count, which is what proves
1102
+ // the step on cannot overflow.
1103
+ const f: i32 = this.first
1104
+ if (f < 0 || f >= toI32(this.entries.length) >> 1) {
1105
+ break
1106
+ }
1107
+ // The size counts every live entry's cost, so the difference is ordered; the test is what proves
1108
+ // it, and a size short of the cost, which only a caller writing the public fields makes, empties.
1109
+ const size: i32 = this.size
1110
+ const cost: i32 = this.costOf(f)
1111
+ let left: i32 = 0
1112
+ if (cost >= 0 && size > cost) {
1113
+ left = size - cost
1114
+ }
1115
+ this.size = left
1116
+ this.first = f + 1
1058
1117
  }
1059
1118
  this.compact()
1060
1119
  }
@@ -1073,11 +1132,15 @@ export class HpackTable {
1073
1132
  if (f > 0 && f <= n && f >= n - f) {
1074
1133
  const live: i32 = toI32(toU32(n - f) << 1)
1075
1134
  const from: i32 = toI32(toU32(f) << 1)
1135
+ // `at` runs alongside `k` from `from`, and `from + live` is the slot count doubled, at most
1136
+ // `entries.length`, so the walk ends where the old index test did.
1137
+ let at: i32 = from
1076
1138
  for (let k: i32 = 0; k < live && k < toI32(entries.length); k += 1) {
1077
- const at: i32 = toI32(toU32(from) + toU32(k))
1078
- if (at >= 0 && at < toI32(entries.length)) {
1079
- entries[k] = entries[at]
1139
+ if (at < 0 || at >= toI32(entries.length)) {
1140
+ break
1080
1141
  }
1142
+ entries[k] = entries[at]
1143
+ at += 1
1081
1144
  }
1082
1145
  while (toI32(entries.length) > live && toI32(entries.length) > 0) {
1083
1146
  entries.pop()
@@ -1096,12 +1159,18 @@ export class HpackTable {
1096
1159
  while (toI32(bytes.length) > keep && toI32(bytes.length) > 0) {
1097
1160
  bytes.pop()
1098
1161
  }
1099
- // Every live entry starts at or after `start`, so the unsigned difference does not wrap.
1162
+ // Every live entry starts at or after `start`, so the difference is ordered; the test is what
1163
+ // proves it.
1100
1164
  const slots: i32 = toI32(entries.length) >> 1
1101
1165
  for (let k: i32 = this.first; k >= 0 && k < slots; k += 1) {
1102
1166
  const at: i32 = toI32(toU32(k) << 1)
1103
1167
  if (at >= 0 && at < toI32(entries.length)) {
1104
- entries[at] = toI32(toU32(entries[at]) - toU32(start))
1168
+ const was: i32 = entries[at]
1169
+ let moved: i32 = 0
1170
+ if (was > start) {
1171
+ moved = was - start
1172
+ }
1173
+ entries[at] = moved
1105
1174
  }
1106
1175
  }
1107
1176
  }
@@ -1120,15 +1189,29 @@ export class HpackTable {
1120
1189
  * and value are both empty.
1121
1190
  */
1122
1191
  add(name: u8[], value: u8[]): i32 {
1123
- const cost: i64 = hpackEntrySize(name, value)
1124
- if (cost > toI64(this.maxSize)) {
1192
+ // `limit` is `maxSize` less the cost, taken one ordered difference at a time; the entry does not fit
1193
+ // exactly when one of them would go below zero, which is the cost passing `maxSize`.
1194
+ const nameLength: i32 = toI32(name.length)
1195
+ const valueLength: i32 = toI32(value.length)
1196
+ const max: i32 = this.maxSize
1197
+ let limit: i32 = -1
1198
+ if (max >= 32) {
1199
+ const afterOverhead: i32 = max - 32
1200
+ if (nameLength >= 0 && nameLength <= afterOverhead) {
1201
+ const afterName: i32 = afterOverhead - nameLength
1202
+ if (valueLength >= 0 && valueLength <= afterName) {
1203
+ limit = afterName - valueLength
1204
+ }
1205
+ }
1206
+ }
1207
+ if (limit < 0) {
1125
1208
  const nothing: i32 = 0
1126
1209
  this.evictTo(nothing)
1127
1210
  return nothing
1128
1211
  }
1129
- // The cost is from 32 to `maxSize`, so neither unsigned sum nor difference below wraps: the size after
1130
- // eviction is at most `maxSize - cost`, and the octets pushed are the cost less 32.
1131
- this.evictTo(toI32(toU32(this.maxSize) - toU32(cost)))
1212
+ // The size after eviction is at most `limit`, so the entry's cost, `maxSize - limit`, takes it to at
1213
+ // most `maxSize`.
1214
+ this.evictTo(limit)
1132
1215
  this.entries.push(toI32(this.bytes.length))
1133
1216
  this.entries.push(toI32(name.length))
1134
1217
  let pushed: u32 = 0
@@ -1140,7 +1223,18 @@ export class HpackTable {
1140
1223
  this.bytes.push(b)
1141
1224
  pushed += 1
1142
1225
  }
1143
- this.size = toI32(toU32(this.size) + toU32(cost))
1226
+ // The new size is `maxSize` less the room eviction left, `limit - size`; both differences are ordered,
1227
+ // and a size past `limit`, which only a caller writing the public fields makes, counts no room.
1228
+ const size: i32 = this.size
1229
+ let room: i32 = 0
1230
+ if (size >= 0 && limit > size) {
1231
+ room = limit - size
1232
+ }
1233
+ let grown: i32 = 0
1234
+ if (room >= 0 && max > room) {
1235
+ grown = max - room
1236
+ }
1237
+ this.size = grown
1144
1238
  return toI32(pushed)
1145
1239
  }
1146
1240
 
@@ -1396,8 +1490,7 @@ export class HpackDecoder {
1396
1490
  hpackAppendText(out, hpackStaticName(index))
1397
1491
  return
1398
1492
  }
1399
- // `index` is past HPACK_STATIC_LENGTH here, so the unsigned difference does not wrap.
1400
- const i: i32 = toI32(toU32(index) - toU32(HPACK_STATIC_LENGTH))
1493
+ const i: i32 = hpackDynamicIndex(index)
1401
1494
  hpackAppend(out, this.table.bytes, this.table.nameStart(i), this.table.nameLength(i))
1402
1495
  this.countCopied(this.table.nameLength(i))
1403
1496
  }
@@ -1408,8 +1501,7 @@ export class HpackDecoder {
1408
1501
  hpackAppendText(out, hpackStaticValue(index))
1409
1502
  return
1410
1503
  }
1411
- // `index` is past HPACK_STATIC_LENGTH here, so the unsigned difference does not wrap.
1412
- const i: i32 = toI32(toU32(index) - toU32(HPACK_STATIC_LENGTH))
1504
+ const i: i32 = hpackDynamicIndex(index)
1413
1505
  hpackAppend(out, this.table.bytes, this.table.valueStart(i), this.table.valueLength(i))
1414
1506
  this.countCopied(this.table.valueLength(i))
1415
1507
  }
@@ -1419,8 +1511,7 @@ export class HpackDecoder {
1419
1511
  if (index <= HPACK_STATIC_LENGTH) {
1420
1512
  return toI32(hpackStaticName(index).length)
1421
1513
  }
1422
- // `index` is past HPACK_STATIC_LENGTH here, so the unsigned difference does not wrap.
1423
- return this.table.nameLength(toI32(toU32(index) - toU32(HPACK_STATIC_LENGTH)))
1514
+ return this.table.nameLength(hpackDynamicIndex(index))
1424
1515
  }
1425
1516
 
1426
1517
  /** What the entry at `index` counts towards a header list, without building it. */
@@ -1432,14 +1523,17 @@ export class HpackDecoder {
1432
1523
  toI64(HPACK_ENTRY_OVERHEAD)
1433
1524
  )
1434
1525
  }
1435
- // `index` is past HPACK_STATIC_LENGTH here, so the unsigned difference does not wrap.
1436
- return toI64(this.table.entrySize(toI32(toU32(index) - toU32(HPACK_STATIC_LENGTH))))
1526
+ return toI64(this.table.entrySize(hpackDynamicIndex(index)))
1437
1527
  }
1438
1528
 
1439
- /** Adds `n` octets, when there are any, to `tableCopied`; the count would need 2^63 octets to wrap. */
1529
+ /**
1530
+ * Adds `n` octets, when there are any, to `tableCopied`. The count is a statistic that tests read as a
1531
+ * difference between two reads, never a length or a decision, so it wraps mod 2^64 rather than panic:
1532
+ * the difference stays exact across a wrap, and reaching one takes 2^63 octets.
1533
+ */
1440
1534
  private countCopied(n: i32): void {
1441
1535
  if (n > 0) {
1442
- this.tableCopied = toI64(toU64(this.tableCopied) + toU64(n))
1536
+ this.tableCopied = wrappingAdd(this.tableCopied, toI64(n))
1443
1537
  }
1444
1538
  }
1445
1539
 
@@ -1741,16 +1835,17 @@ export class HpackEncoder {
1741
1835
  return i
1742
1836
  }
1743
1837
  }
1744
- // Unsigned counters: the dynamic table holds at most `maxSize / 32` entries, so neither they nor the index wrap.
1745
- const n: u32 = toU32(this.table.count())
1746
- for (let k: u32 = 1; k <= n; k += 1) {
1838
+ // The dynamic table holds at most `maxSize / 32` entries, under 2^26, so the literal cap never stops
1839
+ // the walk; it is what proves neither the counter nor the index can overflow. The 61 is
1840
+ // `HPACK_STATIC_LENGTH`, spelled out because the bounds prover reads only a literal.
1841
+ const n: i32 = this.table.count()
1842
+ for (let i: i32 = 1; i <= n && i < 1073741824; i += 1) {
1747
1843
  const t: HpackTable = this.table
1748
- const i: i32 = toI32(k)
1749
1844
  if (
1750
1845
  hpackSameAsWindow(name, t.bytes, t.nameStart(i), t.nameLength(i)) &&
1751
1846
  hpackSameAsWindow(value, t.bytes, t.valueStart(i), t.valueLength(i))
1752
1847
  ) {
1753
- return toI32(toU32(HPACK_STATIC_LENGTH) + k)
1848
+ return 61 + i
1754
1849
  }
1755
1850
  }
1756
1851
  return 0
@@ -1764,12 +1859,11 @@ export class HpackEncoder {
1764
1859
  return i
1765
1860
  }
1766
1861
  }
1767
- // Unsigned counters, as in `findField`.
1768
- const n: u32 = toU32(this.table.count())
1769
- for (let k: u32 = 1; k <= n; k += 1) {
1770
- const i: i32 = toI32(k)
1862
+ // The literal cap and the 61 are as in `findField`.
1863
+ const n: i32 = this.table.count()
1864
+ for (let i: i32 = 1; i <= n && i < 1073741824; i += 1) {
1771
1865
  if (hpackSameAsWindow(name, this.table.bytes, this.table.nameStart(i), this.table.nameLength(i))) {
1772
- return toI32(toU32(HPACK_STATIC_LENGTH) + k)
1866
+ return 61 + i
1773
1867
  }
1774
1868
  }
1775
1869
  return 0