@amritk/nish-x86_64-linux 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/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +233 -159
  4. package/package.json +1 -1
  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/qpack.ts CHANGED
@@ -551,32 +551,31 @@ const qpackPushBytes = (out: u8[], src: u8[], off: i32, len: i32): void => {
551
551
  }
552
552
 
553
553
  /**
554
- * Appends `value` as a §4.1.1 integer with a `prefixBits`-bit prefix, the first
555
- * byte's high bits being `flags`. A value outside 0 to 2^62 - 1, or a prefix
556
- * outside 1 to 8 bits, panics: both are the program's choice.
554
+ * Appends `value` as a §4.1.1 integer whose prefix is the low bits of the
555
+ * first byte, `max` being the prefix with every bit set (63 for a 6-bit
556
+ * prefix) and `flags` the bits above it. Every caller passes a literal
557
+ * `max` and a value that is a count, an index or a checked stream ID, so
558
+ * the integer is one §4.1.1 can carry; a `u64` cannot be negative.
557
559
  */
558
- const qpackPushInteger = (out: u8[], flags: i32, prefixBits: i32, value: i64): void => {
559
- if (prefixBits < 1 || prefixBits > 8 || value < 0 || value > QPACK_MAX_INTEGER) {
560
- panic(`qpackPushInteger: a value of ${value} with a ${prefixBits}-bit prefix`)
561
- }
562
- const max: i32 = (toI32(255) >> (toI32(8) - prefixBits)) & toI32(255)
563
- if (value < toI64(max)) {
560
+ const qpackPushInteger = (out: u8[], flags: i32, max: i32, value: u64): void => {
561
+ const prefix: u64 = toU64(max)
562
+ if (value < prefix) {
564
563
  out.push(toU8(flags | toI32(value)))
565
564
  return
566
565
  }
567
566
  out.push(toU8(flags | max))
568
- let rest: i64 = value - toI64(max)
569
- while (rest >= toI64(128)) {
570
- out.push(toU8(toI32(rest & toI64(127)) | 128))
571
- rest = rest >> toI64(7)
567
+ let rest: u64 = value - prefix
568
+ while (rest >= toU64(128)) {
569
+ out.push(toU8(toI32(rest & toU64(127)) | 128))
570
+ rest = rest >> toU64(7)
572
571
  }
573
572
  out.push(toU8(toI32(rest)))
574
573
  }
575
574
 
576
575
  /**
577
- * Appends `src[off, off + len)` as a §4.1.2 string literal with a
578
- * `prefixBits`-bit prefix: the Huffman flag is the prefix's top bit, the
579
- * length the `prefixBits - 1` bits below it, and `flags` the bits above.
576
+ * Appends `src[off, off + len)` as a §4.1.2 string literal: the Huffman
577
+ * flag is the bit `huffmanBit`, the length an integer whose prefix is `max`
578
+ * (every bit below the flag set), and `flags` the bits above.
580
579
  * When `huffman` is true the string is Huffman-coded if that makes it
581
580
  * shorter, and sent raw otherwise, as an empty string always is.
582
581
  */
@@ -584,20 +583,20 @@ const qpackPushString = (
584
583
  h: HpackHuffman,
585
584
  out: u8[],
586
585
  flags: i32,
587
- prefixBits: i32,
586
+ huffmanBit: i32,
587
+ max: i32,
588
588
  src: u8[],
589
589
  off: i32,
590
590
  len: i32,
591
591
  huffman: boolean
592
592
  ): void => {
593
- const lengthBits: i32 = prefixBits - 1
594
593
  const coded: i32 = huffman ? hpackHuffmanLength(h, src, off, len) : len
595
594
  if (coded < len) {
596
- qpackPushInteger(out, flags | (1 << lengthBits), lengthBits, toI64(coded))
595
+ qpackPushInteger(out, flags | huffmanBit, max, toU64(coded))
597
596
  hpackHuffmanEncode(h, src, off, len, out)
598
597
  return
599
598
  }
600
- qpackPushInteger(out, flags, lengthBits, toI64(len))
599
+ qpackPushInteger(out, flags, max, toU64(len))
601
600
  qpackPushBytes(out, src, off, len)
602
601
  }
603
602
 
@@ -610,7 +609,10 @@ const qpackPushString = (
610
609
  * @param streamId The ID of the request stream whose section the peer's encoder may forget, 0 to 2^62 - 1.
611
610
  */
612
611
  export const qpackPushStreamCancellation = (out: u8[], streamId: i64): void => {
613
- qpackPushInteger(out, 64, 6, streamId)
612
+ if (streamId < 0 || streamId > QPACK_MAX_INTEGER) {
613
+ panic(`qpackPushStreamCancellation: a stream ID of ${streamId}, outside 0 to 2^62 - 1`)
614
+ }
615
+ qpackPushInteger(out, 64, 63, toU64(streamId))
614
616
  }
615
617
 
616
618
  /** The byte at `d.at` of the section being decoded, or -1 when the section has none left there. */
@@ -1193,11 +1195,50 @@ export class QpackEncoder {
1193
1195
  ): void {
1194
1196
  qpackCheckWindow("QpackEncoder.encodeField", name, nameOff, nameLen)
1195
1197
  qpackCheckWindow("QpackEncoder.encodeField", value, valueOff, valueLen)
1198
+ this.encodeLine(out, name, nameOff, nameLen, value, valueOff, valueLen, neverIndexed, huffman)
1199
+ }
1200
+
1201
+ /**
1202
+ * `encodeField` for a field whose name and value are each a whole array,
1203
+ * which has no window to check and so cannot panic.
1204
+ * @param out The buffer the section is being written to; the field line goes after what it holds.
1205
+ * @param name The name, all of it; only read, and lowercase to match a static entry.
1206
+ * @param value The value, all of it; only read.
1207
+ * @param neverIndexed Whether to send the field as a literal with the N bit set.
1208
+ * @param huffman Whether a string may be Huffman-coded; it is only when that is shorter.
1209
+ */
1210
+ encodeWholeField(out: u8[], name: u8[], value: u8[], neverIndexed: boolean, huffman: boolean): void {
1211
+ const zero: i32 = 0
1212
+ this.encodeLine(
1213
+ out,
1214
+ name,
1215
+ zero,
1216
+ toI32(name.length),
1217
+ value,
1218
+ zero,
1219
+ toI32(value.length),
1220
+ neverIndexed,
1221
+ huffman
1222
+ )
1223
+ }
1224
+
1225
+ /** The field line `encodeField` appends, for windows the caller has checked. */
1226
+ private encodeLine(
1227
+ out: u8[],
1228
+ name: u8[],
1229
+ nameOff: i32,
1230
+ nameLen: i32,
1231
+ value: u8[],
1232
+ valueOff: i32,
1233
+ valueLen: i32,
1234
+ neverIndexed: boolean,
1235
+ huffman: boolean
1236
+ ): void {
1196
1237
  let nameIndex: i32 = -1
1197
1238
  for (let i: i32 = 0; i < QPACK_STATIC_LENGTH; i += 1) {
1198
1239
  if (qpackSameText(name, nameOff, nameLen, qpackStaticName(i))) {
1199
1240
  if (!neverIndexed && qpackSameText(value, valueOff, valueLen, qpackStaticValue(i))) {
1200
- qpackPushInteger(out, 192, 6, toI64(i))
1241
+ qpackPushInteger(out, 192, 63, toU64(i))
1201
1242
  return
1202
1243
  }
1203
1244
  if (nameIndex < 0) {
@@ -1212,11 +1253,11 @@ export class QpackEncoder {
1212
1253
  }
1213
1254
  // `01 N 1` for a static name reference, `001 N` for a literal name.
1214
1255
  if (nameIndex >= 0) {
1215
- qpackPushInteger(out, neverIndexed ? 112 : 80, 4, toI64(nameIndex))
1256
+ qpackPushInteger(out, neverIndexed ? 112 : 80, 15, toU64(nameIndex))
1216
1257
  } else {
1217
- qpackPushString(this.huffman, out, neverIndexed ? 48 : 32, 4, name, nameOff, nameLen, huffman)
1258
+ qpackPushString(this.huffman, out, neverIndexed ? 48 : 32, 8, 7, name, nameOff, nameLen, huffman)
1218
1259
  }
1219
- qpackPushString(this.huffman, out, 0, 8, value, valueOff, valueLen, huffman)
1260
+ qpackPushString(this.huffman, out, 0, 128, 127, value, valueOff, valueLen, huffman)
1220
1261
  }
1221
1262
 
1222
1263
  /**
@@ -41,13 +41,31 @@ import { quicPushAck, quicPushAckEcn, quicPutAck, quicPutAckEcn } from "nish/net
41
41
  /** The most disjoint ranges a `QuicAckRanges` keeps, and so the most an ACK frame it writes lists. */
42
42
  export const QUIC_ACK_MAX_RANGES: i32 = 32
43
43
 
44
+ /**
45
+ * One more than `n`, or -1 when `n` is below 0 or already `QUIC_MAX_VARINT`,
46
+ * the largest packet number or count a varint carries (RFC 9000 §16).
47
+ * Counted down from `QUIC_MAX_VARINT`, so the sum is one `src/bounds.ts`
48
+ * proves, never a wrap.
49
+ */
50
+ const quicAckNext = (n: i64): i64 => {
51
+ if (n < 0) {
52
+ return -1
53
+ }
54
+ const left: i64 = QUIC_MAX_VARINT - n
55
+ if (left < 1) {
56
+ return -1
57
+ }
58
+ return QUIC_MAX_VARINT - (left - 1)
59
+ }
60
+
44
61
  /**
45
62
  * One more than the ECN count `n`, or `n` once it is the largest a varint
46
63
  * carries, since an ACK_ECN frame could not write a larger one.
47
64
  */
48
- const quicAckCountUp = (n: i64): i64 =>
49
- // Below `QUIC_MAX_VARINT`, so the unsigned sum cannot wrap.
50
- n < QUIC_MAX_VARINT ? toI64(toU64(n) + 1) : n
65
+ const quicAckCountUp = (n: i64): i64 => {
66
+ const next: i64 = quicAckNext(n)
67
+ return next < 0 ? n : next
68
+ }
51
69
 
52
70
  /**
53
71
  * The packet numbers received in one space. `ranges` holds `count` pairs
@@ -188,8 +206,8 @@ export class QuicAckRanges {
188
206
  while (k < this.count && this.low(k) > pn) {
189
207
  k += 1
190
208
  }
191
- // `pn` is at most 2^62 - 1 here, so one past it cannot wrap.
192
- const joinsAbove: boolean = k > 0 && this.low(k - 1) === toI64(toU64(pn) + 1)
209
+ // One past `pn`, or -1 at `QUIC_MAX_VARINT`, which no range starts at.
210
+ const joinsAbove: boolean = k > 0 && this.low(k - 1) === quicAckNext(pn)
193
211
  const joinsBelow: boolean = k < this.count && this.high(k) === pn - 1
194
212
  if (joinsAbove && joinsBelow) {
195
213
  // `pn` closes the hole between two ranges: merge them into the upper one.
@@ -268,8 +286,10 @@ export class QuicAckRanges {
268
286
  this.set(j, this.low(j - 1), this.high(j - 1))
269
287
  }
270
288
  this.set(k, pn, pn)
271
- // `count` is below `QUIC_ACK_MAX_RANGES` here, so one more cannot wrap.
272
- this.count = toI32(toU32(count) + 1)
289
+ if (count < 0 || count >= QUIC_ACK_MAX_RANGES) {
290
+ return
291
+ }
292
+ this.count = count + 1
273
293
  }
274
294
 
275
295
  /**
@@ -249,8 +249,13 @@ export class QuicCidTable {
249
249
  entry.used = true
250
250
  // Sequence 0 travels in the long header's SCID, never in a frame.
251
251
  entry.announced = sequence === 0
252
- // Below `QUIC_MAX_VARINT`, so one more cannot wrap.
253
- this.nextLocal = toI64(toU64(sequence) + 1)
252
+ // Counted down from `QUIC_MAX_VARINT`: `sequence` is below it, so one
253
+ // more is a sum `src/bounds.ts` proves. The -1 at `QUIC_MAX_VARINT` is
254
+ // never reached: `addLocal` issues at most `QUIC_CONN_MAX_ISSUED_CIDS`
255
+ // IDs, far below it, so `retireLocal`'s `sequence >= nextLocal` keeps
256
+ // its meaning.
257
+ const left: i64 = QUIC_MAX_VARINT - sequence
258
+ this.nextLocal = left < 1 ? -1 : QUIC_MAX_VARINT - (left - 1)
254
259
  return sequence
255
260
  }
256
261
 
@@ -531,15 +536,16 @@ export class QuicCidTable {
531
536
  }
532
537
 
533
538
  /**
534
- * The ring slot `k` places after `retireHead`, or 0 for an empty ring. Unsigned, because the head and
535
- * `k` are each below 2^31, so the sum cannot wrap.
539
+ * The ring slot `k` places after `retireHead`, or 0 for an empty ring or a negative head or `k`.
540
+ * Summed in i64, where two i32 values cannot overflow.
536
541
  */
537
542
  private retireSlot(k: i32): i32 {
538
- const size: u32 = toU32(this.retirePending.length)
539
- if (size === 0) {
543
+ const size: i64 = toI64(this.retirePending.length)
544
+ const head: i32 = this.retireHead
545
+ if (size <= 0 || head < 0 || k < 0) {
540
546
  return 0
541
547
  }
542
- return toI32((toU32(this.retireHead) + toU32(k)) % size)
548
+ return toI32((toI64(head) + toI64(k)) % size)
543
549
  }
544
550
 
545
551
  /**
@@ -100,15 +100,16 @@ export class QuicDatagramQueue {
100
100
  }
101
101
 
102
102
  /**
103
- * The entry `j` places after `head`, wrapped around the ring; 0 for a ring of no entries.
104
- * Unsigned, because `head` and `j` are each below 2^31, so the sum cannot wrap.
103
+ * The entry `j` places after `head`, wrapped around the ring; 0 for a ring of no entries or a
104
+ * negative head or `j`. Summed in i64, where two i32 values cannot overflow.
105
105
  */
106
106
  private slot(j: i32): i32 {
107
- const entries: u32 = toU32(this.lengths.length)
108
- if (entries === 0) {
107
+ const entries: i64 = toI64(this.lengths.length)
108
+ const head: i32 = this.head
109
+ if (entries <= 0 || head < 0 || j < 0) {
109
110
  return 0
110
111
  }
111
- return toI32((toU32(this.head) + toU32(j)) % entries)
112
+ return toI32((toI64(head) + toI64(j)) % entries)
112
113
  }
113
114
 
114
115
  /** Empties the ring, for a connection slot reused for another peer. */
@@ -371,6 +371,48 @@ const quicFrameReset = (frame: QuicFrame, at: i32): void => {
371
371
  frame.resetTokenStart = at
372
372
  }
373
373
 
374
+ /**
375
+ * The Gap field of an ACK frame (§19.3.1) between a range whose smallest
376
+ * packet number is `above` and the next range down, whose largest is
377
+ * `largest`: the packets missing between them, less one. -1 when either is
378
+ * below 0 or the ranges touch or are out of order, so no range is below zero.
379
+ */
380
+ const quicAckGap = (above: i64, largest: i64): i64 => {
381
+ if (above < 2 || largest < 0) {
382
+ return -1
383
+ }
384
+ const room: i64 = above - 2
385
+ return largest > room ? -1 : room - largest
386
+ }
387
+
388
+ /**
389
+ * The ACK Range Length of `[smallest, largest]` (§19.3.1): how many packet
390
+ * numbers below `largest` the range holds. -1 when `smallest` is below 0 or
391
+ * above `largest`.
392
+ */
393
+ const quicAckSpan = (smallest: i64, largest: i64): i64 => {
394
+ if (smallest < 0 || largest < 0 || largest < smallest) {
395
+ return -1
396
+ }
397
+ return largest - smallest
398
+ }
399
+
400
+ /**
401
+ * The next count up from the varint `n`, or -1 when `n` is below 0 or already
402
+ * `QUIC_MAX_VARINT`. Counted down from `QUIC_MAX_VARINT`, so the sum is one
403
+ * `src/bounds.ts` proves, never a wrap.
404
+ */
405
+ const quicFrameNext = (n: i64): i64 => {
406
+ if (n < 0) {
407
+ return -1
408
+ }
409
+ const left: i64 = QUIC_MAX_VARINT - n
410
+ if (left < 1) {
411
+ return -1
412
+ }
413
+ return QUIC_MAX_VARINT - (left - 1)
414
+ }
415
+
374
416
  /**
375
417
  * The ranges of an ACK frame (§19.3.1): the first range down from `largest`,
376
418
  * then each Gap and ACK Range Length pair below it. A range that reaches
@@ -384,9 +426,8 @@ const quicFrameReadAck = (r: QuicFrameReader, d: u8[], frame: QuicFrame, ecn: bo
384
426
  if (r.failed) {
385
427
  return QUIC_ERROR_FRAME_ENCODING
386
428
  }
387
- // Varints are below 2^62, so the difference is exact in wrapping u64 and a
388
- // negative answer means the range reached below zero.
389
- let smallest: i64 = toI64(toU64(frame.largest) - toU64(first))
429
+ // `largest - first`, or -1 for a First ACK Range reaching below zero.
430
+ let smallest: i64 = quicAckSpan(first, frame.largest)
390
431
  if (smallest < 0) {
391
432
  return QUIC_ERROR_FRAME_ENCODING
392
433
  }
@@ -400,15 +441,13 @@ const quicFrameReadAck = (r: QuicFrameReader, d: u8[], frame: QuicFrame, ecn: bo
400
441
  if (r.failed) {
401
442
  return QUIC_ERROR_FRAME_ENCODING
402
443
  }
403
- // The next range's largest is two below this one's smallest, less the gap.
404
- // `smallest` is 0 or more and the varints below 2^62, so wrapping u64
405
- // is exact and a negative answer is a range below zero.
406
- const top: i64 = toI64(toU64(smallest) - toU64(gap) - 2)
407
- if (top < 0) {
408
- return QUIC_ERROR_FRAME_ENCODING
409
- }
410
- const bottom: i64 = toI64(toU64(top) - toU64(length))
411
- if (bottom < 0) {
444
+ // The next range's largest is two below this one's smallest, less the
445
+ // gap (`smallest - 2 - gap`), and its smallest `length` below that
446
+ // (`top - length`): the subtractions the writer's helpers do, read back.
447
+ // Either is -1 for a range below zero.
448
+ const top: i64 = quicAckGap(smallest, gap)
449
+ const bottom: i64 = quicAckSpan(length, top)
450
+ if (top < 0 || bottom < 0) {
412
451
  return QUIC_ERROR_FRAME_ENCODING
413
452
  }
414
453
  if (stored < QUIC_FRAME_MAX_ACK_RANGES) {
@@ -419,8 +458,15 @@ const quicFrameReadAck = (r: QuicFrameReader, d: u8[], frame: QuicFrame, ecn: bo
419
458
  smallest = bottom
420
459
  }
421
460
  frame.ackRangeCount = stored
422
- // `count` is a varint, below 2^62.
423
- frame.ackRangeTotal = toI64(toU64(count) + 1)
461
+ // `count` is a varint, at most `QUIC_MAX_VARINT`, and the total is one
462
+ // more. A count of `QUIC_MAX_VARINT` lists more ranges than a frame holds:
463
+ // it is refused here as FRAME_ENCODING, where the loop above would have
464
+ // refused it later as a short read or a range below zero.
465
+ const total: i64 = quicFrameNext(count)
466
+ if (total < 0) {
467
+ return QUIC_ERROR_FRAME_ENCODING
468
+ }
469
+ frame.ackRangeTotal = total
424
470
  if (ecn) {
425
471
  frame.ect0 = quicFrameVarint(r, d)
426
472
  frame.ect1 = quicFrameVarint(r, d)
@@ -863,8 +909,7 @@ export const quicAckSize = (ranges: i64[], rangeCount: i32, ackDelay: i64): i32
863
909
  if (size > 2147483615) {
864
910
  return 0
865
911
  }
866
- // `smallest` is 0 or more and at most `largest`, so the difference is exact.
867
- const span: i64 = toI64(toU64(largest) - toU64(smallest))
912
+ const span: i64 = quicAckSpan(smallest, largest)
868
913
  if (k === 0) {
869
914
  size =
870
915
  size +
@@ -873,9 +918,8 @@ export const quicAckSize = (ranges: i64[], rangeCount: i32, ackDelay: i64): i32
873
918
  quicVarintSize(toI64(rangeCount - 1)) +
874
919
  quicVarintSize(span)
875
920
  } else {
876
- // Gap: the packets missing between this range and the one above, less one (§19.3.1). Both
877
- // are 0 to 2^62 − 1, so wrapping u64 is exact and a negative gap is ranges out of order.
878
- const gap: i64 = toI64(toU64(previousSmallest) - toU64(largest) - 2)
921
+ // A negative gap is ranges that touch or are out of order.
922
+ const gap: i64 = quicAckGap(previousSmallest, largest)
879
923
  if (gap < 0) {
880
924
  return 0
881
925
  }
@@ -918,8 +962,8 @@ export const quicPutAck = (
918
962
  let previousSmallest: i64 = 0
919
963
  for (let k: i32 = 0; k < rangeCount; k += 1) {
920
964
  // `quicAckSize` held `rangeCount` to half the array and checked every
921
- // pair, so the slots fit, the guard below never skips a pair, and the
922
- // differences are exact in wrapping u64.
965
+ // pair, so the slots fit, the guard below never skips a pair, and no
966
+ // gap or span is the -1 that would refuse the varint.
923
967
  const pair: i32 = toI32(toI64(k) * 2)
924
968
  const next: i32 = toI32(toI64(pair) + 1)
925
969
  if (pair >= 0 && pair < toI32(ranges.length) && next >= 0 && next < toI32(ranges.length)) {
@@ -930,9 +974,9 @@ export const quicPutAck = (
930
974
  cursor = quicFramePutVarint(buf, cursor, ackDelay)
931
975
  cursor = quicFramePutVarint(buf, cursor, toI64(rangeCount) - 1)
932
976
  } else {
933
- cursor = quicFramePutVarint(buf, cursor, toI64(toU64(previousSmallest) - toU64(largest) - 2))
977
+ cursor = quicFramePutVarint(buf, cursor, quicAckGap(previousSmallest, largest))
934
978
  }
935
- cursor = quicFramePutVarint(buf, cursor, toI64(toU64(largest) - toU64(smallest)))
979
+ cursor = quicFramePutVarint(buf, cursor, quicAckSpan(smallest, largest))
936
980
  previousSmallest = smallest
937
981
  }
938
982
  }
@@ -167,6 +167,7 @@ import {
167
167
  quicWorkerByte,
168
168
  quicWorkerCheck,
169
169
  } from "nish/net/quic"
170
+ import { wrappingAdd } from "nish:unsafe"
170
171
 
171
172
  // ---- What `handle` answers ----------------------------------------------------
172
173
 
@@ -961,7 +962,8 @@ export class QuicListener {
961
962
  /** Takes one stateless reset from the rate limit's budget, topping it up first. Answers whether one was there. */
962
963
  private takeReset(): boolean {
963
964
  // `now` never falls below 0, where it starts, and `refilled` is never
964
- // after it, so the u64 sums below are the i64 ones.
965
+ // after it, so the gap between them is a difference of two values at or
966
+ // above 0, which cannot overflow.
965
967
  const now: i64 = this.now
966
968
  let refilled: i64 = this.resetRefilled
967
969
  let budget: i32 = this.resetBudget
@@ -969,15 +971,18 @@ export class QuicListener {
969
971
  refilled = now
970
972
  budget = QUIC_LISTENER_RESET_BURST
971
973
  }
972
- if (now > refilled) {
974
+ if (now >= 0 && refilled >= 0 && now > refilled) {
975
+ const gap: i64 = now - refilled
973
976
  // 100 is `QUIC_LISTENER_RESET_INTERVAL`, as a literal the prover reads.
974
- const earned: u64 = (toU64(now) - toU64(refilled)) / toU64(100)
975
- let k: u64 = 0
976
- while (k < earned && budget >= 0 && budget < QUIC_LISTENER_RESET_BURST) {
977
+ let earned: i64 = gap / 100
978
+ while (earned > 0 && budget >= 0 && budget < QUIC_LISTENER_RESET_BURST) {
977
979
  budget += 1
978
- k += toU64(1)
980
+ earned -= 1
979
981
  }
980
- refilled = toI64(toU64(refilled) + earned * toU64(QUIC_LISTENER_RESET_INTERVAL))
982
+ // Advanced by every whole interval in the gap: `now` less what is left
983
+ // over, which is at or above 0 because the gap is.
984
+ const rest: i64 = gap % 100
985
+ refilled = rest >= 0 ? now - rest : now
981
986
  }
982
987
  this.resetRefilled = refilled
983
988
  if (budget <= 0) {
@@ -1070,16 +1075,18 @@ export const quicListenerTakePaced = (conn: QuicConnection, now: i64): u8[] | nu
1070
1075
  }
1071
1076
 
1072
1077
  /**
1073
- * `now + delay` for a pacer's delay, which is never below 0: summed in `u64`,
1074
- * where it cannot overflow, and held at the largest `i64` when a `now` near it
1075
- * would carry past; no caller's clock comes near.
1078
+ * `now + delay` for a pacer's delay, which is never below 0, held at the
1079
+ * largest `i64` when a `now` near it would carry past; no caller's clock comes
1080
+ * near. The sum is a `wrappingAdd` because the wrap is how that carry is seen:
1081
+ * a `now` at or above 0 plus a positive delay wraps below 0 only on overflow,
1082
+ * and then the answer is the cap, never the wrapped value.
1076
1083
  */
1077
1084
  const quicListenerLater = (now: i64, delay: i64): i64 => {
1078
1085
  if (delay <= 0) {
1079
1086
  return now
1080
1087
  }
1081
- const sum: i64 = toI64(toU64(now) + toU64(delay))
1082
- return now >= 0 && sum < 0 ? toI64((toU64(0) - toU64(1)) >> toU64(1)) : sum
1088
+ const sum: i64 = wrappingAdd(now, delay)
1089
+ return now >= 0 && sum < 0 ? toI64(-1) >>> toI64(1) : sum
1083
1090
  }
1084
1091
 
1085
1092
  /**
@@ -95,6 +95,7 @@
95
95
  * (`docs/wp26-stdlib.md` §3e).
96
96
  */
97
97
  import { QUIC_MAX_VARINT } from "nish/net/quic-packet"
98
+ import { wrappingAdd, wrappingMul, wrappingSub } from "nish:unsafe"
98
99
 
99
100
  /** The Initial space's index, which is `TLS_LEVEL_INITIAL`. */
100
101
  export const QUIC_RECOVERY_INITIAL: i32 = 0
@@ -225,18 +226,18 @@ const QUIC_RECOVERY_CUBIC_SCALE: i64 = 5000000000
225
226
  const quicRecoveryMax = (a: i64, b: i64): i64 => (a > b ? a : b)
226
227
 
227
228
  /** 2^63 − 1, the largest `i64`, which a sum or product past it is held at. */
228
- const quicRecoveryTop = (): i64 => toI64((toU64(1) << toU64(63)) - toU64(1))
229
+ const quicRecoveryTop = (): i64 => toI64(-1) >>> toI64(1)
229
230
 
230
231
  /**
231
232
  * `a + b`, held at 2^63 − 1 when the true sum is past it and at −2^63 when
232
233
  * it is below. Recovery's times, durations and byte counts are far inside
233
234
  * an `i64`, so a held sum is one no clock or window reaches: a deadline that
234
- * never comes, a window nothing fills. Taken in `u64`, whose `+` wraps, and
235
- * read back as the `i64` with the same bits, which is the true sum whenever
236
- * that fits.
235
+ * never comes, a window nothing fills. The wrap is how the overflow is
236
+ * seen, not an answer: `wrappingAdd` is the true sum whenever that fits, and
237
+ * its sign crosses only when it does not, which is held instead.
237
238
  */
238
239
  const quicRecoverySum = (a: i64, b: i64): i64 => {
239
- const sum: i64 = toI64(toU64(a) + toU64(b))
240
+ const sum: i64 = wrappingAdd(a, b)
240
241
  if (a >= 0 && b >= 0 && sum < 0) {
241
242
  return quicRecoveryTop()
242
243
  }
@@ -248,7 +249,7 @@ const quicRecoverySum = (a: i64, b: i64): i64 => {
248
249
 
249
250
  /** `a − b`, held at 2^63 − 1 / −2^63 where the true difference passes either. */
250
251
  const quicRecoveryLess = (a: i64, b: i64): i64 => {
251
- const diff: i64 = toI64(toU64(a) - toU64(b))
252
+ const diff: i64 = wrappingSub(a, b)
252
253
  if (a >= 0 && b < 0 && diff < 0) {
253
254
  return quicRecoveryTop()
254
255
  }
@@ -264,8 +265,9 @@ const quicRecoveryStep = (n: i32): i32 => (n >= 2147483647 ? n : n + 1)
264
265
  /**
265
266
  * `a × b` for two non-negative times, durations or byte counts, held at
266
267
  * 2^63 − 1 when the true product is past it, as `quicRecoverySum` holds a
267
- * sum. Below that it is taken in `u64` and is exact. A negative operand
268
- * counts as 0: every caller passes values it has kept at 0 or above.
268
+ * sum. Below that, with `b` at most 2^63 − 1 over `a`, the product fits, so
269
+ * `wrappingMul` is exact and never wraps. A negative operand counts as 0:
270
+ * every caller passes values it has kept at 0 or above.
269
271
  */
270
272
  const quicRecoveryProduct = (a: i64, b: i64): i64 => {
271
273
  if (a <= 0 || b <= 0) {
@@ -274,7 +276,7 @@ const quicRecoveryProduct = (a: i64, b: i64): i64 => {
274
276
  if (b > quicRecoveryTop() / a) {
275
277
  return quicRecoveryTop()
276
278
  }
277
- return toI64(toU64(a) * toU64(b))
279
+ return wrappingMul(a, b)
278
280
  }
279
281
 
280
282
  /** `x` clamped to −`QUIC_RECOVERY_CUBIC_SPAN` .. `QUIC_RECOVERY_CUBIC_SPAN`. */
@@ -118,6 +118,7 @@ import {
118
118
  } from "nish/net/quic-frame"
119
119
  import { BufferPool } from "nish/net/buffer-pool"
120
120
  import { copyBytes } from "nish/bytes"
121
+ import { wrappingAdd, wrappingSub } from "nish:unsafe"
121
122
 
122
123
  // ---- What the stream calls answer -----------------------------------------------
123
124
 
@@ -408,16 +409,17 @@ const quicStreamZeros = (count: i32): i32[] => {
408
409
  }
409
410
 
410
411
  /** 2^63 − 1, the largest `i64`, which a sum past it is held at. */
411
- const quicStreamTop = (): i64 => toI64((toU64(1) << toU64(63)) - toU64(1))
412
+ const quicStreamTop = (): i64 => toI64(-1) >>> toI64(1)
412
413
 
413
414
  /**
414
415
  * `a + b`, held at 2^63 − 1 when the true sum is past it and at −2^63 when it is below. Stream
415
416
  * offsets, counts and limits are varints, below 2^62, so a held sum is one no peer can send:
416
- * a limit that is never reached. Taken in `u64`, whose `+` wraps, and read back as the `i64`
417
- * with the same bits, which is the true sum whenever that fits.
417
+ * a limit that is never reached. The wrap is how the overflow is seen, not an answer:
418
+ * `wrappingAdd` is the true sum whenever that fits, and its sign crosses only when it does
419
+ * not, which is held instead.
418
420
  */
419
421
  const quicStreamSum = (a: i64, b: i64): i64 => {
420
- const sum: i64 = toI64(toU64(a) + toU64(b))
422
+ const sum: i64 = wrappingAdd(a, b)
421
423
  if (a >= 0 && b >= 0 && sum < 0) {
422
424
  return quicStreamTop()
423
425
  }
@@ -429,7 +431,7 @@ const quicStreamSum = (a: i64, b: i64): i64 => {
429
431
 
430
432
  /** `a − b`, held at 2^63 − 1 / −2^63 where the true difference passes either, as `quicStreamSum` holds a sum. */
431
433
  const quicStreamLess = (a: i64, b: i64): i64 => {
432
- const diff: i64 = toI64(toU64(a) - toU64(b))
434
+ const diff: i64 = wrappingSub(a, b)
433
435
  if (a >= 0 && b < 0 && diff < 0) {
434
436
  return quicStreamTop()
435
437
  }