@amritk/nish-x86_64-linux 0.49.1 → 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 (91) 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 +12 -6
  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 +16 -9
  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/yaml-json.ts +238 -106
  89. package/std/yaml-lines.ts +47 -27
  90. package/std/yaml-write.ts +58 -24
  91. package/std/yaml.ts +808 -395
@@ -47,50 +47,51 @@
47
47
  */
48
48
 
49
49
  /**
50
- * All ones when `lo <= c <= hi`, else zero, without a branch. `c` is a byte or
51
- * a sextet, so neither subtraction could leave `i32`; they are written in
52
- * `u32`, which wraps by definition, because a signed one would be checked for
53
- * an overflow it cannot have, and the check is a branch on the secret. The
54
- * sign bit `>> 31` reads is the same either way. `base64urlCharOf` and
55
- * `base64urlSextetOf` do their sums in `u32` for the same reason.
50
+ * All ones when `lo <= c <= hi`, else zero, without a branch. `c` is a
51
+ * character code or a sextet, below 2^16; `d` is `c` narrowed to that so the
52
+ * compiler proves neither subtraction leaves `i32`. A checked subtraction it
53
+ * could not prove would be a branch on the secret. `base64urlCharOf` and
54
+ * `base64urlSextetOf` narrow their argument the same way.
56
55
  */
57
- const base64urlRangeMask = (c: i32, lo: i32, hi: i32): i32 =>
58
- toI32((toU32(lo) - 1 - toU32(c)) & (toU32(c) - toU32(hi) - 1)) >> 31
56
+ const base64urlRangeMask = (c: i32, lo: integer<0, 255>, hi: integer<0, 255>): i32 => {
57
+ const d: i32 = c & 0xffff
58
+ return ((lo - 1 - d) & (d - hi - 1)) >> 31
59
+ }
59
60
 
60
61
  /**
61
- * The URL-alphabet character for the sextet `v` (`0 <= v <= 63`).
62
- *
62
+ * The URL-alphabet character for the sextet `v`. Every caller passes `0 <= v
63
+ * <= 63`; `v` is masked to its low six bits, so a wider value aliases.
63
64
  * It starts from `'A' + v` and adds, for each range `v` has passed, the step
64
65
  * from the previous range's first character to this one's: 26 lands on `a`, 52
65
66
  * on `0`, 62 on `-` and 63 on `_`.
66
67
  */
67
- const base64urlCharOf = (v: i32): i32 =>
68
- toI32(
68
+ const base64urlCharOf = (v: i32): i32 => {
69
+ const s: i32 = v & 63
70
+ return (
71
+ s +
69
72
  65 +
70
- toU32(v) +
71
- toU32(base64urlRangeMask(v, 26, 63) & 6) -
72
- toU32(base64urlRangeMask(v, 52, 63) & 75) -
73
- toU32(base64urlRangeMask(v, 62, 63) & 13) +
74
- toU32(base64urlRangeMask(v, 63, 63) & 49)
73
+ (base64urlRangeMask(s, 26, 63) & 6) -
74
+ (base64urlRangeMask(s, 52, 63) & 75) -
75
+ (base64urlRangeMask(s, 62, 63) & 13) +
76
+ (base64urlRangeMask(s, 63, 63) & 49)
75
77
  )
78
+ }
76
79
 
77
80
  /**
78
- * The sextet the byte `c` stands for, or `-1` when `c` is not in the alphabet.
79
- * Each range contributes its value under its own mask, and a byte in none of
80
- * them has every bit set by the final OR.
81
+ * The sextet the character code `code` (a `charCodeAt` or a byte, below 2^16:
82
+ * it is masked to 16 bits, so a wider value aliases) stands for, or `-1`. Each
83
+ * range adds its value under its own mask; none of them sets every bit.
81
84
  */
82
- const base64urlSextetOf = (c: i32): i32 => {
85
+ const base64urlSextetOf = (code: i32): i32 => {
86
+ // A character code is 16 bits: the mask proves each sum below in range.
87
+ const c: i32 = code & 0xffff
83
88
  const upper: i32 = base64urlRangeMask(c, 65, 90)
84
89
  const lower: i32 = base64urlRangeMask(c, 97, 122)
85
90
  const digit: i32 = base64urlRangeMask(c, 48, 57)
86
91
  const dash: i32 = base64urlRangeMask(c, 45, 45)
87
92
  const underscore: i32 = base64urlRangeMask(c, 95, 95)
88
93
  const value: i32 =
89
- (upper & toI32(toU32(c) - 65)) |
90
- (lower & toI32(toU32(c) - 71)) |
91
- (digit & toI32(toU32(c) + 4)) |
92
- (dash & 62) |
93
- (underscore & 63)
94
+ (upper & (c - 65)) | (lower & (c - 71)) | (digit & (c + 4)) | (dash & 62) | (underscore & 63)
94
95
  return value | ~(upper | lower | digit | dash | underscore)
95
96
  }
96
97
 
@@ -177,9 +178,16 @@ export const base64urlDecode = (text: string): u8[] | null => {
177
178
  return null
178
179
  }
179
180
  // Three bytes per full group of four, and one fewer than the characters in a
180
- // short final group; `n * 3 / 4` would overflow for a text over 700 MB. The
181
- // sum is a `u32`, exact for any `n` here.
182
- const out: u8[] = new Array<u8>(toU32(n >> 2) * 3 + (tail === 0 ? 0 : toU32(tail) - 1))
181
+ // short final group; `n * 3 / 4` would overflow for a text over 700 MB.
182
+ // Both parts are `i32`s the bounds prover reads as non-negative, and their
183
+ // sum, at most three quarters of `n`, as in range, so the allocation is
184
+ // proven rather than checked.
185
+ let short: i32 = 0
186
+ if (tail > 1) {
187
+ short = tail - 1
188
+ }
189
+ const whole: i32 = (n >> 2) * 3
190
+ const out: u8[] = new Array<u8>(whole + short)
183
191
  // All ones once any character is outside the alphabet (a sextet of `-1`
184
192
  // shifted right by 31), and non-zero once the text ends on a bit that
185
193
  // encodes no byte.
@@ -244,32 +252,31 @@ export const base64urlDecode = (text: string): u8[] | null => {
244
252
  // ---------------------------------------------------------------------------
245
253
 
246
254
  /**
247
- * The standard-alphabet character for the sextet `v`: the same steps as
248
- * `base64urlCharOf` until 62, which lands on `+` and 63 on `/`.
255
+ * The standard-alphabet character for the sextet `v` (`0 <= v <= 63`, masked as
256
+ * `base64urlCharOf` masks it): its steps until 62, which lands on `+`, 63 `/`.
249
257
  */
250
- const base64StdPadCharOf = (v: i32): i32 =>
251
- toI32(
258
+ const base64StdPadCharOf = (v: i32): i32 => {
259
+ const s: i32 = v & 63
260
+ return (
261
+ s +
252
262
  65 +
253
- toU32(v) +
254
- toU32(base64urlRangeMask(v, 26, 63) & 6) -
255
- toU32(base64urlRangeMask(v, 52, 63) & 75) -
256
- toU32(base64urlRangeMask(v, 62, 63) & 15) +
257
- toU32(base64urlRangeMask(v, 63, 63) & 3)
263
+ (base64urlRangeMask(s, 26, 63) & 6) -
264
+ (base64urlRangeMask(s, 52, 63) & 75) -
265
+ (base64urlRangeMask(s, 62, 63) & 15) +
266
+ (base64urlRangeMask(s, 63, 63) & 3)
258
267
  )
268
+ }
259
269
 
260
- /** The sextet the byte `c` stands for in standard base64, or `-1` when it is not in the alphabet. */
261
- const base64StdPadSextetOf = (c: i32): i32 => {
270
+ /** The sextet the character code `code` (below 2^16, masked as `base64urlSextetOf` masks it) stands for in standard base64, or `-1`. */
271
+ const base64StdPadSextetOf = (code: i32): i32 => {
272
+ // A character code is 16 bits: the mask proves each sum below in range.
273
+ const c: i32 = code & 0xffff
262
274
  const upper: i32 = base64urlRangeMask(c, 65, 90)
263
275
  const lower: i32 = base64urlRangeMask(c, 97, 122)
264
276
  const digit: i32 = base64urlRangeMask(c, 48, 57)
265
277
  const plus: i32 = base64urlRangeMask(c, 43, 43)
266
278
  const slash: i32 = base64urlRangeMask(c, 47, 47)
267
- const value: i32 =
268
- (upper & toI32(toU32(c) - 65)) |
269
- (lower & toI32(toU32(c) - 71)) |
270
- (digit & toI32(toU32(c) + 4)) |
271
- (plus & 62) |
272
- (slash & 63)
279
+ const value: i32 = (upper & (c - 65)) | (lower & (c - 71)) | (digit & (c + 4)) | (plus & 62) | (slash & 63)
273
280
  return value | ~(upper | lower | digit | plus | slash)
274
281
  }
275
282
 
@@ -327,13 +334,11 @@ export const base64StdPadEncodeInto = (data: u8[], start: i32, length: i32, out:
327
334
  if (start < 0 || length < 0 || start > toI32(data.length) - length || length > 1610612733) {
328
335
  panic("base64StdPadEncodeInto: bytes outside their buffer")
329
336
  }
330
- // The text's length, and the room after `at`, as `u32`s: exact, since
331
- // `length` is at most 1610612733 and `at` at most `out.length`.
332
- const textLength: u32 = ((toU32(length) + 2) / 3) * 4
333
- if (at < 0 || at > toI32(out.length) || textLength > toU32(toI32(out.length)) - toU32(at)) {
337
+ // The text's length fits an `i32`, since `length` is at most 1610612733.
338
+ const textLength: i32 = ((length + 2) / 3) * 4
339
+ if (at < 0 || at > toI32(out.length) || textLength > toI32(out.length) - at) {
334
340
  panic("base64StdPadEncodeInto: text that does not fit its buffer")
335
341
  }
336
- const end: i32 = toI32(toU32(at) + textLength)
337
342
  // `k` runs through `data[start .. start + length)` and `j` through the text;
338
343
  // the bounds on them only let the prover see what the tests above checked.
339
344
  let j: i32 = at
@@ -352,11 +357,14 @@ export const base64StdPadEncodeInto = (data: u8[], start: i32, length: i32, out:
352
357
  if (bits > 0) {
353
358
  j = base64Put(out, j, toU8(base64StdPadCharOf((acc << toI32(6 - bits)) & 63)))
354
359
  }
355
- while (j >= 0 && j < end && j < toI32(out.length)) {
360
+ // The `=`s that bring the text to a multiple of four characters: two after
361
+ // one byte left over, one after two, none after none.
362
+ const padCount: i32 = ((length % 3) * 2) % 3
363
+ for (let q: i32 = 0; q < padCount && j >= 0 && j < toI32(out.length); q++) {
356
364
  out[j] = 61
357
365
  j += 1
358
366
  }
359
- return end
367
+ return j
360
368
  }
361
369
 
362
370
  /**
@@ -376,21 +384,25 @@ export const base64StdPadDecodedLength = (text: u8[], start: i32, length: i32):
376
384
  if (text.length > 2147483647) {
377
385
  panic("base64StdPadDecodedLength: a buffer longer than 2^31 - 1 bytes")
378
386
  }
379
- if (start < 0 || length < 0 || start > toI32(text.length) - length) {
387
+ // The room before the range's end; a negative `length` panics below.
388
+ const before: i32 = length < 0 ? -1 : toI32(text.length) - length
389
+ if (start < 0 || length < 0 || start > before) {
380
390
  panic("base64StdPadDecodedLength: text outside its buffer")
381
391
  }
382
- if ((length & 3) !== 0) {
392
+ // What follows the range in `text`, never negative after the test above.
393
+ const rest: i32 = before - start
394
+ if ((length & 3) !== 0 || rest < 0) {
383
395
  return -1
384
396
  }
385
- // The text's last character, a `u32` sum that is exact once the range is
386
- // inside `text`; a length that is a multiple of four and not 0 is at least 4,
397
+ // The text's last character, `start + length - 1` counted back from the
398
+ // buffer's end; a length that is a multiple of four and not 0 is at least 4,
387
399
  // so the character before it is in the range too.
388
- const last: i32 = toI32(toU32(start) + toU32(length) - 1)
389
- let pad: u32 = 0
400
+ const last: i32 = toI32(text.length) - rest - 1
401
+ let pad: integer<0, 2> = 0
390
402
  if (length > 0 && last >= 1 && last < toI32(text.length) && toI32(text[last]) === 61) {
391
403
  pad = toI32(text[last - 1]) === 61 ? 2 : 1
392
404
  }
393
- const chars: i32 = toI32(toU32(length) - pad)
405
+ const chars: i32 = length - pad
394
406
  let bad: i32 = 0
395
407
  let acc: i32 = 0
396
408
  let bits: u32 = 0
@@ -409,7 +421,7 @@ export const base64StdPadDecodedLength = (text: u8[], start: i32, length: i32):
409
421
  if (bad !== 0) {
410
422
  return -1
411
423
  }
412
- return toI32(toU32(length >> 2) * 3 - pad)
424
+ return (length >> 2) * 3 - pad
413
425
  }
414
426
 
415
427
  /**
@@ -434,13 +446,28 @@ export const base64StdPadDecode = (text: string): u8[] | null => {
434
446
  if ((n & 3) !== 0) {
435
447
  return null
436
448
  }
437
- // A length that is a multiple of four and not 0 is at least 4.
438
- let pad: u32 = 0
439
- if (n >= 4 && toI32(text.charCodeAt(n - 1)) === 61) {
449
+ // The empty text spells no bytes; any other length that is a multiple of
450
+ // four is at least 4.
451
+ if (n < 4) {
452
+ return new Array<u8>(0)
453
+ }
454
+ let pad: integer<0, 2> = 0
455
+ if (toI32(text.charCodeAt(n - 1)) === 61) {
440
456
  pad = toI32(text.charCodeAt(n - 2)) === 61 ? 2 : 1
441
457
  }
442
- const chars: i32 = toI32(toU32(n) - pad)
443
- const out: u8[] = new Array<u8>(toU32(n >> 2) * 3 - pad)
458
+ // Three bytes per group before the last, and three less the padding in the
459
+ // last: two `i32`s the bounds prover reads as non-negative, so the
460
+ // allocation is proven rather than checked.
461
+ let last: i32 = 3
462
+ if (pad === 1) {
463
+ last = 2
464
+ }
465
+ if (pad === 2) {
466
+ last = 1
467
+ }
468
+ const whole: i32 = ((n >> 2) - 1) * 3
469
+ const out: u8[] = new Array<u8>(whole + last)
470
+ const chars: i32 = n - pad
444
471
  let bad: i32 = 0
445
472
  let acc: i32 = 0
446
473
  let bits: u32 = 0
@@ -189,38 +189,42 @@ interface BcryptParsed {
189
189
 
190
190
  /**
191
191
  * All ones when `lo <= c <= hi`, else zero, without a branch: the encoding
192
- * reads the digest, which is the stored hash when the password is right. The
193
- * sums are in `u32`, as in `base64urlRangeMask` (std/crypto/base64url.ts), so
194
- * no overflow check branches on them.
192
+ * reads the digest, which is the stored hash when the password is right. As in
193
+ * `base64urlRangeMask` (std/crypto/base64url.ts), `c` is narrowed to the
194
+ * character codes it is, below 2^16, so the compiler proves both subtractions
195
+ * fit; a checked subtraction it could not prove would branch on `c`.
195
196
  */
196
- const bcryptRangeMask = (c: i32, lo: i32, hi: i32): i32 =>
197
- toI32((toU32(lo) - 1 - toU32(c)) & (toU32(c) - toU32(hi) - 1)) >> 31
197
+ const bcryptRangeMask = (c: i32, lo: integer<0, 255>, hi: integer<0, 255>): i32 => {
198
+ const d: i32 = c & 0xffff
199
+ return ((lo - 1 - d) & (d - hi - 1)) >> 31
200
+ }
198
201
 
199
202
  /**
200
- * The bcrypt alphabet's character for the sextet `v`, `./A-Za-z0-9`: `.` + `v`,
201
- * plus the step to each range `v` has reached (2 lands on `A`, 28 on `a`, 54
202
- * on `0`).
203
+ * The bcrypt alphabet's character for the sextet `v` (every caller passes `0 <=
204
+ * v <= 63`; it is masked to six bits, so a wider value aliases): `.` + `v`, plus
205
+ * the step to each range `v` has reached (2 lands on `A`, 28 on `a`, 54 `0`).
203
206
  */
204
- const bcryptCharOf = (v: i32): u8 =>
205
- toU8(
206
- 46 +
207
- toU32(v) +
208
- toU32(bcryptRangeMask(v, 2, 63) & 17) +
209
- toU32(bcryptRangeMask(v, 28, 63) & 6) -
210
- toU32(bcryptRangeMask(v, 54, 63) & 75)
207
+ const bcryptCharOf = (v: i32): u8 => {
208
+ // Narrowed to the sextet it is, so the compiler proves the sum fits.
209
+ const s: i32 = v & 63
210
+ return toU8(
211
+ s +
212
+ 46 +
213
+ (bcryptRangeMask(s, 2, 63) & 17) +
214
+ (bcryptRangeMask(s, 28, 63) & 6) -
215
+ (bcryptRangeMask(s, 54, 63) & 75)
211
216
  )
217
+ }
212
218
 
213
219
  /** The sextet of a bcrypt alphabet character, or -1 for any other byte, without a branch. */
214
- const bcryptSextetOf = (c: i32): i32 => {
220
+ const bcryptSextetOf = (c: integer<0, 65535>): i32 => {
215
221
  const punct: i32 = bcryptRangeMask(c, 46, 47)
216
222
  const upper: i32 = bcryptRangeMask(c, 65, 90)
217
223
  const lower: i32 = bcryptRangeMask(c, 97, 122)
218
224
  const digit: i32 = bcryptRangeMask(c, 48, 57)
219
- const value: i32 =
220
- (punct & toI32(toU32(c) - 46)) |
221
- (upper & toI32(toU32(c) - 63)) |
222
- (lower & toI32(toU32(c) - 69)) |
223
- (digit & toI32(toU32(c) + 6))
225
+ // `c` is a character code, so each offset is proven to fit and no overflow
226
+ // check branches on it.
227
+ const value: i32 = (punct & (c - 46)) | (upper & (c - 63)) | (lower & (c - 69)) | (digit & (c + 6))
224
228
  return value | ~(punct | upper | lower | digit)
225
229
  }
226
230
 
@@ -1212,7 +1212,7 @@ const hmacResume256 = (h: Sha256, mid: u64[], at: i32): void => {
1212
1212
  state[i] = toU32(mid[p])
1213
1213
  p += 1
1214
1214
  }
1215
- h.total = toI64(SHA256_BLOCK)
1215
+ h.total = toU64(SHA256_BLOCK)
1216
1216
  h.finished = false
1217
1217
  }
1218
1218
 
package/std/crypto/jwt.ts CHANGED
@@ -662,7 +662,7 @@ const jwtHexValue = (b: i32): i32 => {
662
662
  }
663
663
 
664
664
  /** How many bytes code point `cp` takes in UTF-8. */
665
- const jwtUtf8Width = (cp: i32): i32 => {
665
+ const jwtUtf8Width = (cp: i32): integer<1, 4> => {
666
666
  if (cp < 0x80) {
667
667
  return 1
668
668
  }
@@ -676,7 +676,7 @@ const jwtUtf8Width = (cp: i32): i32 => {
676
676
  }
677
677
 
678
678
  /** Byte `k` of the `width`-byte UTF-8 form of `cp`. */
679
- const jwtUtf8Byte = (cp: i32, width: i32, k: i32): i32 => {
679
+ const jwtUtf8Byte = (cp: i32, width: integer<1, 4>, k: integer<0, 3>): i32 => {
680
680
  if (width === 1) {
681
681
  return cp
682
682
  }
@@ -686,8 +686,8 @@ const jwtUtf8Byte = (cp: i32, width: i32, k: i32): i32 => {
686
686
  } else if (width === 3) {
687
687
  lead = 0xe0
688
688
  }
689
- // Unsigned: `k` is below `width`, so the count cannot wrap.
690
- const shift: i32 = toI32((toU32(width) - toU32(k) - toU32(1)) * toU32(6))
689
+ // `k` is below `width`, so the shift is a whole number of six-bit groups.
690
+ const shift: i32 = (width - k - 1) * 6
691
691
  const tail: i32 = 0x80
692
692
  return k === 0 ? lead | (cp >> shift) : tail | ((cp >> shift) & 0x3f)
693
693
  }
@@ -1049,9 +1049,9 @@ class JwtJson {
1049
1049
  if (lo < 0xdc00 || lo > 0xdfff) {
1050
1050
  return -1
1051
1051
  }
1052
- // Unsigned: each surrogate's low ten bits, so the sum stays below 0x110000.
1053
- const plane: u32 = toU32(0x10000)
1054
- return toI32(plane + (toU32(hi & 0x3ff) << toU32(10)) + toU32(lo & 0x3ff))
1052
+ // Each surrogate's low ten bits, so the sum stays below 0x110000.
1053
+ const high: i32 = (hi & 0x3ff) * 1024
1054
+ return high + (lo & 0x3ff) + 0x10000
1055
1055
  }
1056
1056
 
1057
1057
  /** The four hex digits at `i` as a number, or `-1`. */
@@ -1189,7 +1189,7 @@ class JwtJson {
1189
1189
  continue
1190
1190
  }
1191
1191
  const cp: i32 = this.escapeCodePoint(i)
1192
- const width: i32 = jwtUtf8Width(cp)
1192
+ const width: integer<1, 4> = jwtUtf8Width(cp)
1193
1193
  for (let k: i32 = 0; k < width; k += 1) {
1194
1194
  if (o >= n) {
1195
1195
  return false
@@ -1228,7 +1228,7 @@ class JwtJson {
1228
1228
  continue
1229
1229
  }
1230
1230
  const cp: i32 = this.escapeCodePoint(i)
1231
- const width: i32 = jwtUtf8Width(cp)
1231
+ const width: integer<1, 4> = jwtUtf8Width(cp)
1232
1232
  for (let k: i32 = 0; k < width; k += 1) {
1233
1233
  if (o >= 0 && o < toI32(out.length)) {
1234
1234
  out[o] = toU8(jwtUtf8Byte(cp, width, k))
@@ -1253,9 +1253,18 @@ class JwtJson {
1253
1253
  if (!this.isInteger()) {
1254
1254
  return toI64(0)
1255
1255
  }
1256
- // Unsigned: 0 - 2^63 wraps to the bits of -2^63, and every smaller
1257
- // magnitude to its negation.
1258
- return this.at(this.tokStart) === JWT_CH_MINUS ? toI64(toU64(0) - this.value) : toI64(this.value)
1256
+ const magnitude: u64 = this.value
1257
+ if (this.at(this.tokStart) !== JWT_CH_MINUS || magnitude === toU64(0)) {
1258
+ return toI64(magnitude)
1259
+ }
1260
+ // A negative number's magnitude is at most 2^63 (the scan's limit), so one
1261
+ // less fits an `i64` and its negation less one is the value, -2^63 included.
1262
+ const lessMagnitude: u64 = magnitude - toU64(1)
1263
+ const less: i64 = toI64(lessMagnitude)
1264
+ if (less < 0) {
1265
+ return toI64(0)
1266
+ }
1267
+ return -less - 1
1259
1268
  }
1260
1269
 
1261
1270
  /**
@@ -1719,17 +1728,21 @@ export class JwtVerifier {
1719
1728
  return JWT_ERR_CLAIMS
1720
1729
  }
1721
1730
  const leeway: i64 = jwtClamp(this.leeway, toI64(0), toI64(JWT_MAX_LEEWAY))
1722
- // Unsigned: `now` is held to [0, 2^62] and `leeway` to [0, 2^31 - 1], so
1723
- // both sums and the difference fit an `i64`, and the wrapped bits are theirs.
1724
- const early: i64 = toI64(toU64(now) - toU64(leeway))
1725
- const late: i64 = toI64(toU64(now) + toU64(leeway))
1726
- if (this.hasExp && !(early < this.exp)) {
1731
+ // `verify` holds `now` to [0, 2^62]: the floors here let src/bounds.ts
1732
+ // prove `now - leeway`, and a claim is compared past `now + leeway` as
1733
+ // its distance from `now`, which cannot overflow.
1734
+ if (now < 0 || leeway < 0) {
1735
+ return JWT_ERR_CLAIMS
1736
+ }
1737
+ if (this.hasExp && !(now - leeway < this.exp)) {
1727
1738
  return JWT_ERR_EXPIRED
1728
1739
  }
1729
- if (this.hasNbf && this.nbf > late) {
1740
+ const nbf: i64 = this.nbf
1741
+ if (this.hasNbf && nbf > now && nbf - now > leeway) {
1730
1742
  return JWT_ERR_NOT_YET
1731
1743
  }
1732
- if (this.hasIat && this.iat > late) {
1744
+ const iat: i64 = this.iat
1745
+ if (this.hasIat && iat > now && iat - now > leeway) {
1733
1746
  return JWT_ERR_IAT
1734
1747
  }
1735
1748
  if (issuer !== null && !issOk) {
@@ -157,7 +157,7 @@
157
157
  * (`docs/wp26-stdlib.md` §3e).
158
158
  */
159
159
  import { Secret, expose, exposeWith, secret, wipe } from "nish:secret"
160
- import { SHA256_SIZE, Sha256, sha256 } from "nish/crypto/sha256"
160
+ import { SHA256_SIZE, Sha256, sha256, sha256Absorbed } from "nish/crypto/sha256"
161
161
  import { timingSafeEqual } from "nish/crypto/ct"
162
162
 
163
163
  /** The length in bytes of a private key: one scalar, big-endian. */
@@ -3733,9 +3733,12 @@ const p256BaseSelect = (
3733
3733
  const u: u32 = (digit ^ ctEq(top, 0)) & low
3734
3734
  p256SetZero(out.x)
3735
3735
  p256SetZero(out.y)
3736
+ // `c` is a comb below P256_COMBS (11) and `j` an entry below
3737
+ // P256_COMB_ENTRIES (32), which the masks keep: they let src/bounds.ts prove
3738
+ // the offset, P256_BASE_ENTRY (16) words an entry, in range.
3739
+ const first: i32 = (c & 15) * (P256_COMB_ENTRIES * P256_BASE_ENTRY)
3736
3740
  for (let j: i32 = 0; j < entries; j++) {
3737
- // In u32: `c` is a comb below P256_COMBS, so the offset is inside the table.
3738
- const at: i32 = toI32((toU32(c) * toU32(entries) + toU32(j)) * toU32(P256_BASE_ENTRY))
3741
+ const at: i32 = first + (j & 31) * P256_BASE_ENTRY
3739
3742
  p256BaseMove(out, table, at, ctEq(toU32(j), u))
3740
3743
  }
3741
3744
  p256SetZero(s.t1)
@@ -4001,16 +4004,10 @@ const p256WipeHash = (h: Sha256): void => {
4001
4004
  const p256Mac = (k: u8[], a: u8[], b: u8[], c: u8[], d: u8[]): u8[] => {
4002
4005
  const ipad: u8[] = p256PadBlock(k, 0x36)
4003
4006
  const opad: u8[] = p256PadBlock(k, 0x5c)
4004
- const inner = new Sha256()
4005
- inner.update(ipad, P256_FROM, toI32(ipad.length))
4006
- inner.update(a, P256_FROM, toI32(a.length))
4007
- inner.update(b, P256_FROM, toI32(b.length))
4008
- inner.update(c, P256_FROM, toI32(c.length))
4009
- inner.update(d, P256_FROM, toI32(d.length))
4007
+ const inner: Sha256 = sha256Absorbed(ipad, a, b, c, d)
4010
4008
  const innerDigest: u8[] = inner.digest()
4011
- const outer = new Sha256()
4012
- outer.update(opad, P256_FROM, toI32(opad.length))
4013
- outer.update(innerDigest, P256_FROM, toI32(innerDigest.length))
4009
+ const empty: u8[] = new Array<u8>(0)
4010
+ const outer: Sha256 = sha256Absorbed(opad, innerDigest, empty, empty, empty)
4014
4011
  const tag: u8[] = outer.digest()
4015
4012
  wipe(ipad)
4016
4013
  wipe(opad)
@@ -202,11 +202,14 @@ const pkiRootsSplit = (load: PkiRootsLoad, text: string): void => {
202
202
  if (toI32(text.charCodeAt(lineEnd)) === 10) {
203
203
  break
204
204
  }
205
- // Unsigned: lineEnd is below text.length, so it cannot wrap.
206
- lineEnd = toI32(toU32(lineEnd) + 1)
205
+ // Below text.length, so the next index fits an `i32`.
206
+ lineEnd = lineEnd + 1
207
+ }
208
+ let next: i32 = n
209
+ if (lineEnd >= 0 && lineEnd < n) {
210
+ // Below n, so the next index fits an `i32`.
211
+ next = lineEnd + 1
207
212
  }
208
- // Unsigned: lineEnd is below n on that side, so it cannot wrap.
209
- const next: i32 = lineEnd < n ? toI32(toU32(lineEnd) + 1) : n
210
213
  // A carriage return, space or tab before the line feed is no part of the
211
214
  // line, so a BEGIN with trailing whitespace still opens a block and is
212
215
  // counted when `pemToDer` refuses it, rather than lost unseen.