@amritk/nish-aarch64-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/net/tls.ts CHANGED
@@ -2109,11 +2109,12 @@ export class TlsServer {
2109
2109
  using _scope = arena()
2110
2110
  const psk: u8[] = expose(pool.psk, tlsHeldClone)
2111
2111
  const early: u8[] = tlsZeros(h)
2112
- tlsPskEarlySecretInto(this.kdf, h, psk, early)
2112
+ const extracted: boolean = tlsPskEarlySecretInto(this.kdf, h, psk, early)
2113
2113
  secureZero(psk)
2114
2114
  exposeWith(pool.early, early, tlsHeldCopyIn)
2115
2115
  const binderKey: u8[] = tlsZeros(h)
2116
- tlsBinderKeyInto(this.kdf, h, early, binderKey)
2116
+ // A derivation that answers `false` fails the binder, so the hello is `decrypt_error` (row 9).
2117
+ const keyed: boolean = extracted && tlsBinderKeyInto(this.kdf, h, early, binderKey)
2117
2118
  secureZero(early)
2118
2119
  exposeWith(pool.binderKey, binderKey, tlsHeldCopyIn)
2119
2120
  const verified: boolean = tlsBinderVerify(
@@ -2129,7 +2130,7 @@ export class TlsServer {
2129
2130
  secureZero(binderKey)
2130
2131
  pool.zero(TLS_SECRET_PSK)
2131
2132
  pool.zero(TLS_SECRET_BINDER_KEY)
2132
- return verified
2133
+ return keyed && verified
2133
2134
  }
2134
2135
 
2135
2136
  /** The salt a resumed handshake secret is extracted under, `Derive-Secret(early, "derived", "")`, into `salt`. */
@@ -2137,7 +2138,9 @@ export class TlsServer {
2137
2138
  const pool: TlsSecretPool = this.secrets()
2138
2139
  using _scope = arena()
2139
2140
  const early: u8[] = expose(pool.early, tlsHeldClone)
2140
- tlsHandshakeSaltInto(this.kdf, this.hashLength, early, this.salt)
2141
+ if (!tlsHandshakeSaltInto(this.kdf, this.hashLength, early, this.salt)) {
2142
+ this.transcript.refuse()
2143
+ }
2141
2144
  secureZero(early)
2142
2145
  }
2143
2146
 
@@ -2425,7 +2428,9 @@ export class TlsServer {
2425
2428
  */
2426
2429
  private deriveInto(prk: u8[], label: string, dst: Secret<u8[]>): void {
2427
2430
  const out: u8[] = tlsZeros(this.hashLength)
2428
- tlsDeriveSecretInto(this.kdf, this.hashLength, prk, label, this.hashes, TLS_FROM, out)
2431
+ if (!tlsDeriveSecretInto(this.kdf, this.hashLength, prk, label, this.hashes, TLS_FROM, out)) {
2432
+ this.transcript.refuse()
2433
+ }
2429
2434
  exposeWith(dst, out, tlsHeldCopyIn)
2430
2435
  secureZero(out)
2431
2436
  }
@@ -2505,7 +2510,19 @@ export class TlsServer {
2505
2510
  const body: i32 = tlsSum(at, 4)
2506
2511
  this.transcript.hashInto(this.hashes, TLS_FROM)
2507
2512
  const serverHandshake: u8[] = expose(pool.serverHandshake, tlsHeldClone)
2508
- tlsFinishedVerifyDataInto(this.kdf, h, serverHandshake, this.hashes, TLS_FROM, this.outputHandshake, body)
2513
+ if (
2514
+ !tlsFinishedVerifyDataInto(
2515
+ this.kdf,
2516
+ h,
2517
+ serverHandshake,
2518
+ this.hashes,
2519
+ TLS_FROM,
2520
+ this.outputHandshake,
2521
+ body
2522
+ )
2523
+ ) {
2524
+ this.transcript.refuse()
2525
+ }
2509
2526
  secureZero(serverHandshake)
2510
2527
  const finishedEnd: i32 = tlsWriteFinished(this.outputHandshake, at, this.outputHandshake, body, h)
2511
2528
  this.sent(TLS_LEVEL_HANDSHAKE, at, finishedEnd)
@@ -2523,14 +2540,18 @@ export class TlsServer {
2523
2540
  this.transcript.hashInto(this.hashes, TLS_FROM)
2524
2541
  const handshake: u8[] = expose(pool.handshake, tlsHeldClone)
2525
2542
  const master: u8[] = tlsZeros(h)
2526
- tlsMasterSecretInto(this.kdf, h, handshake, master)
2543
+ if (!tlsMasterSecretInto(this.kdf, h, handshake, master)) {
2544
+ this.transcript.refuse()
2545
+ }
2527
2546
  secureZero(handshake)
2528
2547
  this.deriveInto(master, "c ap traffic", pool.clientApplication)
2529
2548
  this.deriveInto(master, "s ap traffic", pool.serverApplication)
2530
2549
  this.deriveInto(master, "exp master", pool.exporter)
2531
2550
  const clientHandshake: u8[] = expose(pool.clientHandshake, tlsHeldClone)
2532
2551
  const finished: u8[] = pool.clientFinished
2533
- tlsFinishedVerifyDataInto(this.kdf, h, clientHandshake, this.hashes, TLS_FROM, finished, TLS_FROM)
2552
+ if (!tlsFinishedVerifyDataInto(this.kdf, h, clientHandshake, this.hashes, TLS_FROM, finished, TLS_FROM)) {
2553
+ this.transcript.refuse()
2554
+ }
2534
2555
  secureZero(clientHandshake)
2535
2556
  // The only client Finished `handleFinished` accepts is the one just
2536
2557
  // computed, so the transcript through it is known now (RFC 8446 §4.6.1),
@@ -2580,12 +2601,12 @@ export class TlsServer {
2580
2601
  return written
2581
2602
  }
2582
2603
 
2583
- /** The resumption PSK for nonce `ticketNonce`, into `issuing.psk` (§4.6.1). */
2584
- private resumptionPsk(): void {
2604
+ /** The resumption PSK for nonce `ticketNonce`, into `issuing.psk` (§4.6.1), and whether it was derived. */
2605
+ private resumptionPsk(): boolean {
2585
2606
  const pool: TlsSecretPool = this.secrets()
2586
2607
  using _scope = arena()
2587
2608
  const resumption: u8[] = expose(pool.resumption, tlsHeldClone)
2588
- tlsResumptionPskInto(
2609
+ const derived: boolean = tlsResumptionPskInto(
2589
2610
  this.kdf,
2590
2611
  this.hashLength,
2591
2612
  resumption,
@@ -2595,6 +2616,7 @@ export class TlsServer {
2595
2616
  this.issuing.psk
2596
2617
  )
2597
2618
  secureZero(resumption)
2619
+ return derived
2598
2620
  }
2599
2621
 
2600
2622
  /** `issue`'s ticket: sealed into `ticketScratch`, its PSK zeroed, and the NewSessionTicket written. */
@@ -2606,7 +2628,10 @@ export class TlsServer {
2606
2628
  if (!this.resumed) {
2607
2629
  issuing.chainStart = now
2608
2630
  }
2609
- this.resumptionPsk()
2631
+ if (!this.resumptionPsk()) {
2632
+ secureZero(issuing.psk)
2633
+ return false
2634
+ }
2610
2635
  const length: i32 = tlsTicketSeal(key, info, issuing, now, this.ticketScratch)
2611
2636
  secureZero(issuing.psk)
2612
2637
  if (length === 0) {
@@ -2649,6 +2674,11 @@ export class TlsServer {
2649
2674
  if (length !== this.hashLength) {
2650
2675
  return TLS_ALERT_DECODE_ERROR
2651
2676
  }
2677
+ // A transcript window refused on the way here left a hash that covers
2678
+ // less than the handshake: no connection is made on it.
2679
+ if (this.transcript.refused()) {
2680
+ return TLS_ALERT_INTERNAL_ERROR
2681
+ }
2652
2682
  const pool: TlsSecretPool = this.secrets()
2653
2683
  if (!timingSafeEqualAt(this.input, 4, pool.clientFinished, TLS_FROM, this.hashLength)) {
2654
2684
  return TLS_ALERT_DECRYPT_ERROR
@@ -859,25 +859,20 @@ const upstreamParser = (limits: UpstreamLimits): Http1ResponseParser => {
859
859
  return p
860
860
  }
861
861
 
862
- /**
863
- * `n` zero bytes, none when `n` is below 1. Filled by `push`, so no `i32`
864
- * length is computed for `new Array`.
865
- */
862
+ /** `n` zero bytes, none when `n` is below 1: one zeroed allocation, not a `push` per byte. */
866
863
  const upstreamBytes = (n: i32): u8[] => {
867
- const a: u8[] = []
868
- for (let i: i32 = 0; i < n; i += 1) {
869
- a.push(0)
864
+ if (n <= 0) {
865
+ return []
870
866
  }
871
- return a
867
+ return new Array<u8>(n)
872
868
  }
873
869
 
874
- /** Room for `n` slot indexes, filled the way `upstreamBytes` is. */
870
+ /** Room for `n` zeroed slot indexes, none when `n` is below 1, allocated the way `upstreamBytes` is. */
875
871
  const upstreamSlots = (n: i32): i32[] => {
876
- const a: i32[] = []
877
- for (let i: i32 = 0; i < n; i += 1) {
878
- a.push(0)
872
+ if (n <= 0) {
873
+ return []
879
874
  }
880
- return a
875
+ return new Array<i32>(n)
881
876
  }
882
877
 
883
878
  /**
@@ -1020,13 +1015,17 @@ const upstreamAppend = (r: UpstreamResponse, data: u8[], off: i32, len: i32): bo
1020
1015
  if (length > toI32(old.length)) {
1021
1016
  return false
1022
1017
  }
1023
- // Built by `push`, so no `i32` length is computed for `new Array`.
1024
- const grown: u8[] = []
1025
- for (let i: i32 = 0; i < length && i < toI32(old.length); i += 1) {
1026
- grown.push(old[i])
1018
+ // One zeroed allocation and one bulk copy; the guard proves the length.
1019
+ if (size > toU32(2147483647)) {
1020
+ return false
1021
+ }
1022
+ const room: i32 = toI32(size)
1023
+ if (room <= 0) {
1024
+ return false
1027
1025
  }
1028
- while (toU32(grown.length) < size) {
1029
- grown.push(0)
1026
+ const grown: u8[] = new Array<u8>(room)
1027
+ if (!tryCopyBytes(grown, 0, old, 0, length)) {
1028
+ return false
1030
1029
  }
1031
1030
  r.body = grown
1032
1031
  }
package/std/net/url.ts CHANGED
@@ -712,13 +712,18 @@ const urlBytesOf = (s: string): u8[] | null => {
712
712
 
713
713
  /** `percentDecode` of `src[off, off + len)` as a new string, or null on an error or when the result is not UTF-8. */
714
714
  const urlDecodeString = (src: u8[], off: i32, len: i32, flags: i32): string | null => {
715
+ // Both callers, `urlDecode` and `urlQueryGet`, cut the window from
716
+ // `src.length`, so this never answers; it is what proves `len` an array
717
+ // length, and a window outside `src` is text nobody can decode.
718
+ if (off < 0 || len < 0 || len > toI32(src.length) - off) {
719
+ return null
720
+ }
715
721
  // `len` bytes of room; decoding never makes the text longer.
716
722
  const out: u8[] = new Array<u8>(len)
717
- // A window outside `src` is a caller contract, kept as `percentDecode`'s
718
- // panic rather than answered as null: both callers, `urlDecode` and
719
- // `urlQueryGet`, cut the window from `src.length`.
720
- const n: i32 = percentDecode(src, off, len, out, 0, flags)
721
- if (n < 0 || !urlIsUtf8(out, 0, n)) {
723
+ const n: i32 = tryPercentDecode(src, off, len, out, 0, flags)
724
+ // A count is at most `out.length`, which `tryPercentDecode` never writes
725
+ // past; the last test is spelled out for the proof.
726
+ if (n < 0 || !urlIsUtf8(out, 0, n) || n > toI32(out.length)) {
722
727
  return null
723
728
  }
724
729
  return String.fromBytes(out, 0, n)
@@ -812,15 +817,26 @@ export class Url {
812
817
  hasAuthority: boolean = false
813
818
  }
814
819
 
815
- /** `bytes[a, b)` as a string with A to Z lower-cased. */
816
- const urlLowerString = (bytes: u8[], a: i32, b: i32): string => {
817
- // Callers pass `0 <= a <= b`; a window the other way round is empty.
818
- let width: i32 = 0
819
- if (b > a && a >= 0) {
820
- width = b - a
820
+ /**
821
+ * `bytes[a, b)` as a string with A to Z lower-cased, or null when the window
822
+ * starts below 0 or ends past `bytes`, which no caller's does: each cuts it
823
+ * from `bytes.length`. A window the other way round is empty.
824
+ */
825
+ const urlLowerString = (bytes: u8[], a: i32, b: i32): string | null => {
826
+ const n: i32 = toI32(bytes.length)
827
+ if (a < 0 || b > n) {
828
+ return null
829
+ }
830
+ if (b <= a) {
831
+ return ""
832
+ }
833
+ // `0 <= a < b <= n`, so the difference fits and is above 0; the prover
834
+ // reads that only from the clamp, which is never taken.
835
+ let width: i32 = b - a
836
+ if (width < 0) {
837
+ width = 0
821
838
  }
822
839
  const out: u8[] = new Array<u8>(width)
823
- const n: i32 = toI32(bytes.length)
824
840
  let j: i32 = 0
825
841
  let i: i32 = a
826
842
  while (j < toI32(out.length) && i >= 0 && i < n) {
@@ -833,6 +849,18 @@ const urlLowerString = (bytes: u8[], a: i32, b: i32): string => {
833
849
  return String.fromBytes(out, 0, toI32(out.length))
834
850
  }
835
851
 
852
+ /**
853
+ * `bytes[a, b)` as a string, or null when the window is not inside `bytes`,
854
+ * which no caller's is: each cuts it with `urlFind`, which answers inside the
855
+ * window it was given. The test is what proves the read.
856
+ */
857
+ const urlSlice = (bytes: u8[], a: i32, b: i32): string | null => {
858
+ if (a < 0 || b < a || b > toI32(bytes.length)) {
859
+ return null
860
+ }
861
+ return String.fromBytes(bytes, a, b)
862
+ }
863
+
836
864
  /** The first `c` in `bytes[a, b)`, or `b`. */
837
865
  const urlFind = (bytes: u8[], a: i32, b: i32, c: i32): i32 => {
838
866
  let i: i32 = a
@@ -1019,9 +1047,17 @@ const urlReadAuthority = (bytes: u8[], a: i32, b: i32, u: Url): boolean => {
1019
1047
  return false
1020
1048
  }
1021
1049
  const colon: i32 = urlFind(bytes, a, at, URL_COLON)
1022
- u.username = String.fromBytes(bytes, a, colon)
1050
+ const username: string | null = urlSlice(bytes, a, colon)
1051
+ if (username === null) {
1052
+ return false
1053
+ }
1054
+ u.username = username
1023
1055
  if (colon < at) {
1024
- u.password = String.fromBytes(bytes, colon + 1, at)
1056
+ const password: string | null = urlSlice(bytes, colon + 1, at)
1057
+ if (password === null) {
1058
+ return false
1059
+ }
1060
+ u.password = password
1025
1061
  }
1026
1062
  h = at + 1
1027
1063
  }
@@ -1031,11 +1067,16 @@ const urlReadAuthority = (bytes: u8[], a: i32, b: i32, u: Url): boolean => {
1031
1067
  }
1032
1068
  // `h` lies inside the window, which lies inside `bytes`; the bounds are
1033
1069
  // spelled out for the proof.
1070
+ let host: string | null = null
1034
1071
  if (h >= 0 && h < b && h < toI32(bytes.length) && toI32(bytes[h]) === URL_OPEN_BRACKET) {
1035
- u.host = urlLowerString(bytes, h + 1, portAt - 1)
1072
+ host = urlLowerString(bytes, h + 1, portAt - 1)
1036
1073
  } else {
1037
- u.host = urlLowerString(bytes, h, portAt)
1074
+ host = urlLowerString(bytes, h, portAt)
1075
+ }
1076
+ if (host === null) {
1077
+ return false
1038
1078
  }
1079
+ u.host = host
1039
1080
  if (portAt < b && portAt + 1 < b) {
1040
1081
  const port: i32 = urlPortOf(bytes, portAt + 1, b)
1041
1082
  if (port < 0) {
@@ -1158,15 +1199,23 @@ const urlReadReference = (s: string): Url | null => {
1158
1199
  i += 1
1159
1200
  }
1160
1201
  // `urlFind` answers an index at most `n`, so the tests against the length
1161
- // below always hold; they are spelled out for the proof.
1202
+ // below, and `urlSlice`'s, always hold; they are spelled out for the proof.
1162
1203
  const u: Url = new Url()
1163
1204
  const hash: i32 = urlFind(bytes, 0, n, URL_HASH)
1164
1205
  if (hash < n) {
1165
- u.fragment = String.fromBytes(bytes, hash + 1, n)
1206
+ const fragment: string | null = urlSlice(bytes, hash + 1, n)
1207
+ if (fragment === null) {
1208
+ return null
1209
+ }
1210
+ u.fragment = fragment
1166
1211
  }
1167
1212
  const question: i32 = urlFind(bytes, 0, hash, URL_QUESTION)
1168
1213
  if (question < hash) {
1169
- u.query = String.fromBytes(bytes, question + 1, hash)
1214
+ const query: string | null = urlSlice(bytes, question + 1, hash)
1215
+ if (query === null) {
1216
+ return null
1217
+ }
1218
+ u.query = query
1170
1219
  }
1171
1220
  let pos: i32 = 0
1172
1221
  if (question > 0 && toI32(bytes.length) > 0 && urlIsAlpha(toI32(bytes[0]))) {
@@ -1179,7 +1228,11 @@ const urlReadReference = (s: string): Url | null => {
1179
1228
  j += 1
1180
1229
  }
1181
1230
  if (j < question && j < toI32(bytes.length) && toI32(bytes[j]) === URL_COLON) {
1182
- u.scheme = urlLowerString(bytes, 0, j)
1231
+ const scheme: string | null = urlLowerString(bytes, 0, j)
1232
+ if (scheme === null) {
1233
+ return null
1234
+ }
1235
+ u.scheme = scheme
1183
1236
  pos = j + 1
1184
1237
  }
1185
1238
  }
@@ -1197,7 +1250,11 @@ const urlReadReference = (s: string): Url | null => {
1197
1250
  }
1198
1251
  pos = end
1199
1252
  }
1200
- u.path = String.fromBytes(bytes, pos, question)
1253
+ const path: string | null = urlSlice(bytes, pos, question)
1254
+ if (path === null) {
1255
+ return null
1256
+ }
1257
+ u.path = path
1201
1258
  return u
1202
1259
  }
1203
1260
 
@@ -190,22 +190,14 @@ const websocketAdd = (a: i32, b: i32): i32 => toI32(toI64(a) + toI64(b))
190
190
  const websocketSub = (a: i32, b: i32): i32 => toI32(toI64(a) - toI64(b))
191
191
 
192
192
  /**
193
- * A zeroed array of `n` bytes, or of none when `n` is below zero. Up to
194
- * 65535 bytes is one allocation; past that the array grows a byte at a time
195
- * from a 65535-byte start, which an `i32` length cannot make fail.
193
+ * A zeroed array of `n` bytes, or of none when `n` is below zero: one
194
+ * allocation, its length proven non-negative by the guard.
196
195
  */
197
196
  const websocketBytes = (n: i32): u8[] => {
198
197
  if (n < 0) {
199
198
  return []
200
199
  }
201
- if (n <= 65535) {
202
- return new Array<u8>(toU16(n))
203
- }
204
- const out: u8[] = new Array<u8>(65535)
205
- for (let k: i32 = 65535; k < n; k += 1) {
206
- out.push(0)
207
- }
208
- return out
200
+ return new Array<u8>(n)
209
201
  }
210
202
 
211
203
  /**
@@ -312,10 +312,32 @@ export const wtCodeFromHttp3 = (code: i64): i64 => {
312
312
  return toI64(shifted - Math.floor(shifted / 31.0))
313
313
  }
314
314
 
315
- /** Panics unless `value` lies in `[low, high]`: a cap out of range is the program's mistake. */
316
- const webtransportCheckCap = (what: string, value: i32, low: i32, high: i32): void => {
317
- if (value < low || value > high) {
318
- panic(`WebTransport: ${what} of ${value}, outside ${low} to ${high}`)
315
+ /** Why `value`, the cap `what`, lies outside `[low, high]`, or `""` when it lies inside. */
316
+ const webtransportCapFault = (what: string, value: i32, low: i32, high: i32): string =>
317
+ value < low || value > high ? `WebTransport: ${what} of ${value}, outside ${low} to ${high}` : ""
318
+
319
+ /** Why `config` cannot make a layer, or `""` when it can: the first cap out of range. */
320
+ const webtransportConfigFault = (config: WebTransportConfig): string => {
321
+ const streams: string = webtransportCapFault("maxStreams", config.maxStreams, 1, WT_MAX_CAP)
322
+ if (streams.length > 0) {
323
+ return streams
324
+ }
325
+ const pending: string = webtransportCapFault("maxPending", config.maxPending, 0, WT_MAX_CAP)
326
+ if (pending.length > 0) {
327
+ return pending
328
+ }
329
+ const idle: string = webtransportCapFault("idleTimeout", config.idleTimeout, 1, WT_MAX_IDLE)
330
+ if (idle.length > 0) {
331
+ return idle
332
+ }
333
+ return webtransportCapFault("maxDatagramsPerSecond", config.maxDatagramsPerSecond, 1, WT_MAX_RATE)
334
+ }
335
+
336
+ /** Panics unless `config` can make a layer: a cap out of range is the program's mistake. */
337
+ const webtransportCheckConfig = (config: WebTransportConfig): void => {
338
+ const fault: string = webtransportConfigFault(config)
339
+ if (fault.length > 0) {
340
+ panic(fault)
319
341
  }
320
342
  }
321
343
 
@@ -407,12 +429,19 @@ export class WebTransportWaiting {
407
429
  private heads: i32[]
408
430
  private tails: i32[]
409
431
  private salt: i64 = 0
432
+ /** Whether `salt` holds its 4 bytes from the entropy source; drawn by the first `add` that finds the source answering. */
433
+ private salted: boolean = false
434
+ /** The 4 bytes `drawSalt` reads into, made once here so an `add` the source refuses allocates nothing. */
435
+ private entropy: u8[]
410
436
  private freeHead: i32 = -1
411
437
  /** How many streams are waiting now. */
412
438
  count: i32 = 0
413
439
 
414
440
  /**
415
- * Makes an empty room, its hash salted with 4 bytes from `crypto.getRandomValues`.
441
+ * Makes an empty room. Its hash is salted with 4 bytes from
442
+ * `crypto.tryGetRandomValues`, drawn by the first `add`, so a room that
443
+ * no stream ever waits in draws nothing; `add` answers false while the
444
+ * source fails.
416
445
  * @param size How many streams may wait at once; 0 or less makes room for 1.
417
446
  */
418
447
  constructor(size: i32) {
@@ -424,12 +453,31 @@ export class WebTransportWaiting {
424
453
  const buckets: i32 = wtBuckets(n)
425
454
  this.heads = wtI32s(buckets)
426
455
  this.tails = wtI32s(buckets)
427
- const entropy: u8[] = wtBytes(4)
428
- crypto.getRandomValues(entropy)
456
+ this.entropy = wtBytes(4)
457
+ this.reset()
458
+ }
459
+
460
+ /**
461
+ * Draws the salt if it is not drawn yet. Only an empty room draws it, so
462
+ * no entry was filed under another salt; the room starts empty and every
463
+ * `add` before the draw answers false.
464
+ * @returns Whether the room is salted: false while the entropy source fails.
465
+ */
466
+ private drawSalt(): boolean {
467
+ if (this.salted) {
468
+ return true
469
+ }
470
+ const entropy: u8[] = this.entropy
471
+ if (!crypto.tryGetRandomValues(entropy)) {
472
+ return false
473
+ }
474
+ let salt: i64 = 0
429
475
  for (let j: i32 = 0; j < toI32(entropy.length); j++) {
430
- this.salt = (this.salt << toI64(8)) | toI64(toI32(entropy[j]))
476
+ salt = (salt << toI64(8)) | toI64(toI32(entropy[j]))
431
477
  }
432
- this.reset()
478
+ this.salt = salt
479
+ this.salted = true
480
+ return true
433
481
  }
434
482
 
435
483
  /** @internal Empties the room; called by `WebTransport` in this module, not for callers. */
@@ -452,10 +500,15 @@ export class WebTransportWaiting {
452
500
  }
453
501
 
454
502
  /**
455
- * Files stream `stream`, waiting for session `session`; answers whether there was room.
503
+ * Files stream `stream`, waiting for session `session`; answers whether
504
+ * there was room, false too when the salt is not drawn and the entropy
505
+ * source fails.
456
506
  * @internal Called by `WebTransport` in this module; not for callers.
457
507
  */
458
508
  add(stream: i64, session: i64): boolean {
509
+ if (!this.drawSalt()) {
510
+ return false
511
+ }
459
512
  const next: i32[] = this.next
460
513
  const prev: i32[] = this.prev
461
514
  const heads: i32[] = this.heads
@@ -743,23 +796,21 @@ export class WebTransport {
743
796
  closedByPeer: boolean = false
744
797
 
745
798
  /**
746
- * The WebTransport layer of `h3`, whose `Http3Config` must have
747
- * `webtransportSessions` above 0. A cap out of range panics.
799
+ * The WebTransport layer of `h3`, one session for each of its `Http3Config`'s
800
+ * `webtransportSessions`. Over a connection with WebTransport off (0) the
801
+ * layer has no sessions and answers every request for one 501, as for a
802
+ * `:protocol` it does not serve; `openWebTransport` answers that as an `Err`.
803
+ * A cap out of range panics, a contract of this constructor: `openWebTransport`
804
+ * answers it as an `Err`.
748
805
  * @param config The caps: `maxStreams` 1 to 4096, `maxPending` 0 to 4096, `idleTimeout` 1 to
749
806
  * 86,400,000 and `maxDatagramsPerSecond` 1 to 1,048,576; kept, not copied.
750
807
  * @param h3 The connection this layer reads and writes, kept for the layer's life; its restarts
751
808
  * restart this layer too.
752
809
  */
753
810
  constructor(config: WebTransportConfig, h3: Http3Connection) {
811
+ webtransportCheckConfig(config)
812
+ // `Http3Connection` holds the count to 0 through `H3_MAX_SESSIONS`.
754
813
  const sessions: i32 = h3.config.webtransportSessions
755
- // `Http3Connection` already holds the count to `H3_MAX_SESSIONS`; what can be wrong here is a connection with WebTransport off.
756
- if (sessions < 1) {
757
- panic("WebTransport: the Http3Connection has WebTransport off (webtransportSessions 0)")
758
- }
759
- webtransportCheckCap("maxStreams", config.maxStreams, 1, WT_MAX_CAP)
760
- webtransportCheckCap("maxPending", config.maxPending, 0, WT_MAX_CAP)
761
- webtransportCheckCap("idleTimeout", config.idleTimeout, 1, WT_MAX_IDLE)
762
- webtransportCheckCap("maxDatagramsPerSecond", config.maxDatagramsPerSecond, 1, WT_MAX_RATE)
763
814
  this.config = config
764
815
  this.h3 = h3
765
816
  this.quic = h3.quic
@@ -1205,7 +1256,7 @@ export class WebTransport {
1205
1256
  }
1206
1257
  let status: i32 = 0
1207
1258
  const peer = h3.peer
1208
- if (!httpFieldIs(f.protocol, "webtransport")) {
1259
+ if (!httpFieldIs(f.protocol, "webtransport") || toI32(this.table.length) === 0) {
1209
1260
  status = 501
1210
1261
  } else if (
1211
1262
  !httpFieldIs(f.scheme, "https") ||
@@ -1988,3 +2039,26 @@ export class WebTransport {
1988
2039
  return result === QUIC_DATAGRAM_ERR_FULL ? H3_AGAIN : H3_TOO_LARGE
1989
2040
  }
1990
2041
  }
2042
+
2043
+ /**
2044
+ * A layer as `new WebTransport(config, h3)` makes one, with a cap out of
2045
+ * range, which that constructor panics on, and a connection with WebTransport
2046
+ * off, over which that constructor's layer would refuse every session,
2047
+ * answered as an `Err`.
2048
+ * @param config The caps, as the constructor reads them; kept, not copied.
2049
+ * @param h3 The connection the layer reads and writes, kept for the layer's life.
2050
+ * @returns `Ok` with the layer, or `Err` saying which cap is out of range or that `h3` has WebTransport off.
2051
+ */
2052
+ export const openWebTransport = (
2053
+ config: WebTransportConfig,
2054
+ h3: Http3Connection
2055
+ ): Result<WebTransport, string> => {
2056
+ if (h3.config.webtransportSessions < 1) {
2057
+ return Err("WebTransport: the Http3Connection has WebTransport off (webtransportSessions 0)")
2058
+ }
2059
+ const fault: string = webtransportConfigFault(config)
2060
+ if (fault.length > 0) {
2061
+ return Err(fault)
2062
+ }
2063
+ return Ok(new WebTransport(config, h3))
2064
+ }
package/std/raster.ts CHANGED
@@ -686,7 +686,12 @@ export class RasterTrail {
686
686
  * @param capacity - the points it remembers, at least one
687
687
  */
688
688
  constructor(capacity: i32) {
689
- const n: i32 = Math.max(capacity, 1)
689
+ // Written as a test, not `Math.max`, so the prover sees `n` is
690
+ // positive at each `new Array` below.
691
+ let n: i32 = 1
692
+ if (capacity > 1) {
693
+ n = capacity
694
+ }
690
695
  this.xs = new Array<f64>(n)
691
696
  this.xs.fill(0.0)
692
697
  this.ys = new Array<f64>(n)
@@ -840,7 +845,12 @@ export class RasterParticles {
840
845
  * @param capacity - the particles it holds at once, at least one
841
846
  */
842
847
  constructor(capacity: i32) {
843
- const n: i32 = Math.max(capacity, 1)
848
+ // Written as a test, not `Math.max`, so the prover sees `n` is
849
+ // positive at each `new Array` below.
850
+ let n: i32 = 1
851
+ if (capacity > 1) {
852
+ n = capacity
853
+ }
844
854
  this.xs = new Array<f64>(n)
845
855
  this.xs.fill(0.0)
846
856
  this.ys = new Array<f64>(n)
@@ -201,7 +201,12 @@ export class Framebuffer {
201
201
  const area: i32 = pixelsArea("Framebuffer", width, height)
202
202
  this.width = width
203
203
  this.height = height
204
- this.pixels = new Array<i32>(area)
204
+ // `area` is never negative here; the test lets the prover see it.
205
+ let n: i32 = 0
206
+ if (area > 0) {
207
+ n = area
208
+ }
209
+ this.pixels = new Array<i32>(n)
205
210
  this.pixels.fill(0)
206
211
  }
207
212
 
@@ -216,7 +221,12 @@ export class Framebuffer {
216
221
  if (width !== this.width || height !== this.height) {
217
222
  this.width = width
218
223
  this.height = height
219
- this.pixels = new Array<i32>(area)
224
+ // `area` is never negative here; the test lets the prover see it.
225
+ let n: i32 = 0
226
+ if (area > 0) {
227
+ n = area
228
+ }
229
+ this.pixels = new Array<i32>(n)
220
230
  }
221
231
  this.pixels.fill(0)
222
232
  }
@@ -381,6 +391,12 @@ export const scaleFramebuffer = (src: Framebuffer, divisor: i32, dst: Framebuffe
381
391
  }
382
392
  const w: i32 = pixelsCeilDiv(src.width, divisor)
383
393
  const h: i32 = pixelsCeilDiv(src.height, divisor)
394
+ // Neither side is negative or larger than `src`'s, so only a `src` whose
395
+ // fields were set past its pixels gets here; the test is the size check
396
+ // `resize` makes, stated at the call so the call itself is proven.
397
+ if (w < 0 || h < 0 || toI64(w) * toI64(h) > toI64(2147483647)) {
398
+ panic("scaleFramebuffer: more than 2^31 - 1 pixels")
399
+ }
384
400
  dst.resize(w, h)
385
401
  const from: i32[] = src.pixels
386
402
  const to: i32[] = dst.pixels
package/std/text.ts CHANGED
@@ -378,9 +378,6 @@ export const splitLimit = (text: string, sep: string, limit: i32): string[] => {
378
378
  return parts
379
379
  }
380
380
 
381
- /** The most digits `toFixed` takes after the point; JavaScript throws a `RangeError` past it. */
382
- const TO_FIXED_MAX_DIGITS: i32 = 100
383
-
384
381
  /** Where `toFixed` stops writing fixed notation and answers `String(x)`, as JavaScript does. */
385
382
  const TO_FIXED_LIMIT: f64 = 1e21
386
383
 
@@ -479,7 +476,10 @@ const fixedDigitsAdd = (digits: u8[], doubling: boolean, amount: i32): void => {
479
476
  * 1e21 in size.
480
477
  */
481
478
  export const toFixed = (x: f64, digits: i32): string => {
482
- if (digits < 0 || digits > TO_FIXED_MAX_DIGITS) {
479
+ // `digits * digits > 100 * 100` is `digits > 100` for a `digits` not below
480
+ // zero; the cap 10000 is the caller contract's product of 100 and 100
481
+ // (`src/caller-contract.ts`), so a call with a constant count proves it.
482
+ if (digits < 0 || toI64(digits) * toI64(digits) > toI64(10000)) {
483
483
  panic("toFixed: digits must be 0 to 100")
484
484
  }
485
485
  // biome-ignore lint/suspicious/noSelfCompare: NaN is the only value that is not itself; Nish has no Number.isNaN
@@ -905,7 +905,7 @@ const shiftTextAsciiRange = (text: string, low: i32, high: i32, toUpper: boolean
905
905
  bytes.push(toU8(code >= low && code <= high ? moved : code))
906
906
  i += 1
907
907
  }
908
- return String.fromBytes(bytes, 0, length)
908
+ return String.fromBytes(bytes, 0, toI32(bytes.length))
909
909
  }
910
910
  const parts: string[] = []
911
911
  let start: i32 = 0