@amritk/nish-aarch64-linux 0.42.0 → 0.45.1

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 (124) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +287 -129
  3. package/package.json +1 -1
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +24 -0
  7. package/runtime/core.mjs +21 -0
  8. package/runtime/nish.d.ts +167 -24
  9. package/runtime/nish.mjs +7 -1
  10. package/runtime/runtime-cross.c +9 -0
  11. package/runtime/runtime-net.c +51 -13
  12. package/runtime/runtime-region.c +13 -0
  13. package/runtime/runtime-text.c +11 -0
  14. package/runtime/runtime-uring.c +83 -25
  15. package/runtime/runtime-wasm.c +3 -0
  16. package/runtime/runtime.c +329 -6
  17. package/runtime/sqlite/README.md +16 -3
  18. package/runtime/sqlite/nish-sqlite.c +940 -29
  19. package/scripts/build.sh +111 -29
  20. package/std/README.md +6 -6
  21. package/std/array.ts +10 -2
  22. package/std/bytes.ts +239 -26
  23. package/std/cli-args.ts +1072 -606
  24. package/std/cli-flags.ts +180 -98
  25. package/std/cli.ts +21 -13
  26. package/std/collections.ts +245 -127
  27. package/std/compress/checksum.ts +32 -37
  28. package/std/compress/deflate.ts +1102 -542
  29. package/std/compress/gzip.ts +290 -145
  30. package/std/compress/zlib.ts +57 -21
  31. package/std/crypto/aes.ts +153 -75
  32. package/std/crypto/base64url.ts +163 -39
  33. package/std/crypto/bcrypt-batch.ts +164 -90
  34. package/std/crypto/bcrypt.ts +185 -107
  35. package/std/crypto/chacha20poly1305.ts +84 -68
  36. package/std/crypto/ct.ts +9 -7
  37. package/std/crypto/ed25519.ts +64 -27
  38. package/std/crypto/hkdf.ts +105 -63
  39. package/std/crypto/hmac.ts +95 -89
  40. package/std/crypto/jwt.ts +248 -126
  41. package/std/crypto/p256.ts +758 -444
  42. package/std/crypto/p384.ts +20 -13
  43. package/std/crypto/pki-roots.ts +95 -38
  44. package/std/crypto/pki.ts +164 -60
  45. package/std/crypto/rsa.ts +266 -223
  46. package/std/crypto/scram.ts +141 -0
  47. package/std/crypto/sha1.ts +157 -57
  48. package/std/crypto/sha256.ts +502 -422
  49. package/std/crypto/sha512.ts +204 -135
  50. package/std/crypto/x509-random.ts +9 -3
  51. package/std/crypto/x509.ts +331 -171
  52. package/std/host/ring.ts +1 -1
  53. package/std/host/text.ts +1 -1
  54. package/std/json-equal.ts +35 -19
  55. package/std/json-format.ts +215 -108
  56. package/std/json-number.ts +126 -72
  57. package/std/json-pointer.ts +42 -13
  58. package/std/json-tokenizer.ts +498 -307
  59. package/std/json-typed.ts +115 -36
  60. package/std/json-value.ts +341 -152
  61. package/std/json.ts +300 -158
  62. package/std/net/buffer-pool.ts +17 -7
  63. package/std/net/dns-config.ts +427 -236
  64. package/std/net/dns-message.ts +114 -47
  65. package/std/net/dns-resolver.ts +736 -237
  66. package/std/net/dns.ts +205 -52
  67. package/std/net/fetch.ts +320 -184
  68. package/std/net/grpc.ts +948 -0
  69. package/std/net/handler.ts +1276 -218
  70. package/std/net/hpack.ts +395 -180
  71. package/std/net/http-compress.ts +502 -65
  72. package/std/net/http-fields.ts +85 -49
  73. package/std/net/http-server.ts +1764 -603
  74. package/std/net/http1-client-codec.ts +372 -161
  75. package/std/net/http1-codec.ts +665 -331
  76. package/std/net/http1-server.ts +881 -269
  77. package/std/net/http2-cleartext.ts +165 -95
  78. package/std/net/http2-frame.ts +232 -89
  79. package/std/net/http2-tls.ts +42 -12
  80. package/std/net/http2.ts +327 -101
  81. package/std/net/http3-frame.ts +68 -33
  82. package/std/net/http3-server.ts +672 -206
  83. package/std/net/http3.ts +669 -191
  84. package/std/net/oidc.ts +63 -24
  85. package/std/net/outbound.ts +207 -142
  86. package/std/net/postgres-async.ts +1223 -470
  87. package/std/net/postgres-carrier.ts +575 -372
  88. package/std/net/postgres.ts +1521 -632
  89. package/std/net/prefork.ts +254 -98
  90. package/std/net/protobuf.ts +1017 -0
  91. package/std/net/qpack.ts +143 -92
  92. package/std/net/quic-conn-ack.ts +104 -22
  93. package/std/net/quic-conn-cid.ts +66 -26
  94. package/std/net/quic-conn-params.ts +13 -7
  95. package/std/net/quic-datagram.ts +90 -27
  96. package/std/net/quic-frame.ts +324 -121
  97. package/std/net/quic-listener.ts +206 -91
  98. package/std/net/quic-packet.ts +289 -163
  99. package/std/net/quic-recovery.ts +279 -128
  100. package/std/net/quic-stream.ts +466 -167
  101. package/std/net/quic.ts +584 -258
  102. package/std/net/router.ts +450 -99
  103. package/std/net/serve.ts +661 -240
  104. package/std/net/static.ts +1458 -0
  105. package/std/net/tls/client.ts +140 -45
  106. package/std/net/tls/codec.ts +510 -260
  107. package/std/net/tls/record-client.ts +268 -76
  108. package/std/net/tls/record-server.ts +236 -79
  109. package/std/net/tls/record.ts +173 -47
  110. package/std/net/tls/schedule.ts +58 -39
  111. package/std/net/tls/ticket.ts +295 -103
  112. package/std/net/tls/verify.ts +1 -1
  113. package/std/net/tls-tcp.ts +218 -75
  114. package/std/net/tls.ts +260 -152
  115. package/std/net/upstream.ts +283 -86
  116. package/std/net/url.ts +277 -102
  117. package/std/net/websocket.ts +532 -209
  118. package/std/net/webtransport.ts +755 -436
  119. package/std/regex.ts +1659 -829
  120. package/std/sqlite.ts +325 -0
  121. package/std/sync/ring.ts +320 -156
  122. package/std/sync/shm.ts +53 -6
  123. package/std/text.ts +285 -135
  124. package/std/threads.ts +22 -2
package/std/crypto/jwt.ts CHANGED
@@ -227,7 +227,7 @@ const jwtSignatureSize = (key: JwtKey): i32 => {
227
227
  }
228
228
  const rsa: RsaPublicKey | null = key.rsa
229
229
  if (rsa !== null) {
230
- return (rsa.bits + 7) >> 3
230
+ return toI32((toI64(rsa.bits) + toI64(7)) >> toI64(3))
231
231
  }
232
232
  if (alg === JWT_ALG_ES256) {
233
233
  return P256_SIGNATURE_SIZE
@@ -238,7 +238,13 @@ const jwtSignatureSize = (key: JwtKey): i32 => {
238
238
  return 64
239
239
  }
240
240
 
241
- const jwtCopy = (a: u8[]): u8[] => jwtSlice(a, toI32(0), toI32(a.length))
241
+ const jwtCopy = (a: u8[]): u8[] => {
242
+ const out: u8[] = []
243
+ for (let k: i32 = 0; k < toI32(a.length); k += 1) {
244
+ out.push(a[k])
245
+ }
246
+ return out
247
+ }
242
248
 
243
249
  const jwtLength = (a: u8[]): i32 => toI32(a.length)
244
250
 
@@ -400,9 +406,12 @@ const jwtDerInteger = (out: u8[], v: u8[]): void => {
400
406
  }
401
407
  start = i + 1
402
408
  }
403
- const pad: boolean = start < toI32(v.length) && (toI32(v[start]) & 0x80) !== 0
409
+ let pad: boolean = false
410
+ if (start >= 0 && start < toI32(v.length)) {
411
+ pad = (toI32(v[start]) & 0x80) !== 0
412
+ }
404
413
  out.push(toU8(2))
405
- jwtDerLength(out, toI32(v.length) - start + (pad ? 1 : 0))
414
+ jwtDerLength(out, toI32(toI64(v.length) - toI64(start) + toI64(pad ? 1 : 0)))
406
415
  if (pad) {
407
416
  out.push(toU8(0))
408
417
  }
@@ -452,7 +461,13 @@ export const jwtEcKey = (alg: i32, point: u8[]): JwtKey | null => {
452
461
  } else if (alg === JWT_ALG_ES384) {
453
462
  size = P384_POINT_SIZE
454
463
  }
455
- if (size === 0 || toI32(point.length) !== size || toI32(point[0]) !== 4) {
464
+ if (size === 0 || toI32(point.length) !== size) {
465
+ return null
466
+ }
467
+ if (toI32(point.length) < 1) {
468
+ return null
469
+ }
470
+ if (toI32(point[0]) !== 4) {
456
471
  return null
457
472
  }
458
473
  return jwtPublicKey(alg, null, null, jwtCopy(point), false)
@@ -533,31 +548,45 @@ const jwtSextet = (c: i32): i32 => {
533
548
  * `out` holds.
534
549
  */
535
550
  const jwtDecode = (src: u8[], off: i32, len: i32, out: u8[]): i32 => {
551
+ if (off < 0 || len < 0 || toI64(off) + toI64(len) > toI64(src.length)) {
552
+ return -1
553
+ }
554
+ // The test above again, in the form the prover reads for `src[k]`.
555
+ const stop: i32 = toI32(toI64(off) + toI64(len))
556
+ if (stop > toI32(src.length)) {
557
+ return -1
558
+ }
536
559
  const tail: i32 = len & 3
537
560
  if (tail === 1) {
538
561
  return -1
539
562
  }
540
- const size: i32 = (len >> 2) * 3 + (tail === 0 ? 0 : tail - 1)
541
- if (size > toI32(out.length)) {
563
+ // Unsigned: `len` is not negative, and three bytes per four characters fit a `u64`.
564
+ let extra: i32 = 0
565
+ if (tail !== 0) {
566
+ extra = tail - 1
567
+ }
568
+ const size: u64 = toU64(toU32(len >> 2)) * toU64(3) + toU64(toU32(extra))
569
+ if (size > toU64(toU32(toI32(out.length)))) {
542
570
  return -1
543
571
  }
544
- let acc: i32 = 0
545
- let bits: i32 = 0
572
+ // Unsigned: `bits` stays below 14 and `acc` below 2^24, and neither can wrap.
573
+ let acc: u32 = toU32(0)
574
+ let bits: u32 = toU32(0)
546
575
  let at: i32 = 0
547
- for (let i: i32 = 0; i < len; i += 1) {
548
- const v: i32 = jwtSextet(toI32(src[off + i]))
576
+ for (let k: i32 = off; k < stop; k += 1) {
577
+ const v: i32 = jwtSextet(toI32(src[k]))
549
578
  if (v < 0) {
550
579
  return -1
551
580
  }
552
- acc = ((acc << 6) | v) & 0xffffff
553
- bits += 6
554
- if (bits >= 8 && at >= 0 && at < toI32(out.length)) {
555
- bits -= 8
556
- out[at] = toU8((acc >> bits) & 255)
581
+ acc = ((acc << toU32(6)) | toU32(v)) & toU32(0xffffff)
582
+ bits += toU32(6)
583
+ if (bits >= toU32(8) && at >= 0 && at < toI32(out.length)) {
584
+ bits -= toU32(8)
585
+ out[at] = toU8((acc >> bits) & toU32(255))
557
586
  at += 1
558
587
  }
559
588
  }
560
- if ((acc & ((1 << bits) - 1)) !== 0) {
589
+ if ((acc & ((toU32(1) << bits) - toU32(1))) !== toU32(0)) {
561
590
  return -1
562
591
  }
563
592
  return at
@@ -657,7 +686,8 @@ const jwtUtf8Byte = (cp: i32, width: i32, k: i32): i32 => {
657
686
  } else if (width === 3) {
658
687
  lead = 0xe0
659
688
  }
660
- const shift: i32 = (width - 1 - k) * 6
689
+ // Unsigned: `k` is below `width`, so the count cannot wrap.
690
+ const shift: i32 = toI32((toU32(width) - toU32(k) - toU32(1)) * toU32(6))
661
691
  const tail: i32 = 0x80
662
692
  return k === 0 ? lead | (cp >> shift) : tail | ((cp >> shift) & 0x3f)
663
693
  }
@@ -669,8 +699,8 @@ const jwtUtf8Byte = (cp: i32, width: i32, k: i32): i32 => {
669
699
  * strings compare decoded (`keyEquals`), so an escaped name is the name.
670
700
  */
671
701
  class JwtJson {
672
- /** The current integer's value, negated, when `integral` holds. */
673
- private value: i64
702
+ /** The current integer's magnitude, when `integral` holds. */
703
+ private value: u64
674
704
  private src: u8[]
675
705
  private readonly stack: u8[]
676
706
  private pos: i32
@@ -689,9 +719,13 @@ class JwtJson {
689
719
 
690
720
  /** A reader for values nested at most `maxDepth` containers deep; `reset` gives it a window. */
691
721
  constructor(src: u8[], maxDepth: i32) {
692
- this.value = toI64(0)
722
+ this.value = toU64(0)
693
723
  this.src = src
694
- this.stack = new Array<u8>(maxDepth)
724
+ const stack: u8[] = []
725
+ for (let i: i32 = 0; i < maxDepth; i += 1) {
726
+ stack.push(toU8(0))
727
+ }
728
+ this.stack = stack
695
729
  this.pos = 0
696
730
  this.end = 0
697
731
  this.depth = 0
@@ -707,12 +741,15 @@ class JwtJson {
707
741
  /** @internal Starts over on `src[off, off + len)`; a window outside `src` fails the first `next()`. */
708
742
  reset(src: u8[], off: i32, len: i32): void {
709
743
  this.src = src
744
+ const inside: boolean = off >= 0 && len >= 0 && len <= toI32(src.length) - off
710
745
  this.pos = off
711
- this.end = off + len
746
+ // Inside, `off + len` is at most `src.length`; outside, the reader is
747
+ // failed and `end` is never read.
748
+ this.end = inside ? toI32(toI64(off) + toI64(len)) : off
712
749
  this.depth = 0
713
750
  this.want = JWT_JSON_WANT_VALUE
714
751
  this.kind = JWT_JSON_ERR
715
- this.failed = off < 0 || len < 0 || len > toI32(src.length) - off ? JWT_JSON_ERR : 0
752
+ this.failed = inside ? 0 : JWT_JSON_ERR
716
753
  this.integral = false
717
754
  }
718
755
 
@@ -721,6 +758,21 @@ class JwtJson {
721
758
  return i >= 0 && i < this.end && i < toI32(this.src.length) ? toI32(this.src[i]) : -1
722
759
  }
723
760
 
761
+ /**
762
+ * `a + b` for a position or a count. A sum past the `i32` range fails the
763
+ * reader, which `next()`, `scanString`, `keyEquals` and `stringInto` then
764
+ * answer as a refusal, and gives the largest `i32`, a position past every
765
+ * window, so the loop that asked ends.
766
+ */
767
+ private step(a: i32, b: i32): i32 {
768
+ const sum: i64 = toI64(a) + toI64(b)
769
+ if (sum > toI64(2147483647) || sum < toI64(-2147483648)) {
770
+ this.fail()
771
+ return 2147483647
772
+ }
773
+ return toI32(sum)
774
+ }
775
+
724
776
  /** Fails the reader for good. */
725
777
  private fail(): i32 {
726
778
  this.failed = JWT_JSON_ERR
@@ -732,13 +784,30 @@ class JwtJson {
732
784
  private skipBlanks(): void {
733
785
  let b: i32 = this.at(this.pos)
734
786
  while (b === JWT_CH_SPACE || b === JWT_CH_TAB || b === JWT_CH_NEWLINE || b === JWT_CH_RETURN) {
735
- this.pos += 1
787
+ this.pos = this.step(this.pos, toI32(1))
736
788
  b = this.at(this.pos)
737
789
  }
738
790
  }
739
791
 
792
+ /** The kind of the innermost open container, or `-1` with none open. */
793
+ private top(): i32 {
794
+ const d: i32 = this.depth
795
+ const stack: u8[] = this.stack
796
+ if (d < 1 || d > toI32(stack.length)) {
797
+ return -1
798
+ }
799
+ return toI32(stack[d - 1])
800
+ }
801
+
740
802
  /** @internal The token after the current one, or `JWT_JSON_ERR`, answered again on every later call. */
741
803
  next(): i32 {
804
+ const kind: i32 = this.advance()
805
+ // A step that failed the reader refuses this token too.
806
+ return this.failed !== 0 ? this.failed : kind
807
+ }
808
+
809
+ /** `next()`'s token, before the check for a failed step. */
810
+ private advance(): i32 {
742
811
  if (this.failed !== 0) {
743
812
  return this.failed
744
813
  }
@@ -754,9 +823,13 @@ class JwtJson {
754
823
  }
755
824
  if (want === JWT_JSON_WANT_COMMA_OR_CLOSE) {
756
825
  if (b === JWT_CH_COMMA) {
757
- this.pos += 1
826
+ this.pos = this.step(this.pos, toI32(1))
758
827
  this.skipBlanks()
759
- return toI32(this.stack[this.depth - 1]) === JWT_JSON_IN_OBJECT ? this.readKey() : this.readValue()
828
+ const top: i32 = this.top()
829
+ if (top < 0) {
830
+ return this.fail()
831
+ }
832
+ return top === JWT_JSON_IN_OBJECT ? this.readKey() : this.readValue()
760
833
  }
761
834
  return this.readClose(b)
762
835
  }
@@ -774,17 +847,19 @@ class JwtJson {
774
847
 
775
848
  /** `b`, the closer of the innermost container, popped. */
776
849
  private readClose(b: i32): i32 {
777
- if (this.depth === 0) {
850
+ const top: i32 = this.top()
851
+ const d: i32 = this.depth
852
+ if (top < 0 || d < 1) {
778
853
  return this.fail()
779
854
  }
780
- const inObject: boolean = toI32(this.stack[this.depth - 1]) === JWT_JSON_IN_OBJECT
855
+ const inObject: boolean = top === JWT_JSON_IN_OBJECT
781
856
  if (b !== (inObject ? JWT_CH_CLOSE_BRACE : JWT_CH_CLOSE_BRACKET)) {
782
857
  return this.fail()
783
858
  }
784
859
  this.tokStart = this.pos
785
- this.pos += 1
860
+ this.pos = this.step(this.pos, toI32(1))
786
861
  this.tokEnd = this.pos
787
- this.depth -= 1
862
+ this.depth = d - 1
788
863
  this.afterValue()
789
864
  this.kind = inObject ? JWT_JSON_OBJECT_END : JWT_JSON_ARRAY_END
790
865
  return this.kind
@@ -804,7 +879,7 @@ class JwtJson {
804
879
  if (this.at(this.pos) !== JWT_CH_COLON) {
805
880
  return this.fail()
806
881
  }
807
- this.pos += 1
882
+ this.pos = this.step(this.pos, toI32(1))
808
883
  this.want = JWT_JSON_WANT_VALUE
809
884
  this.kind = JWT_JSON_KEY
810
885
  return JWT_JSON_KEY
@@ -814,14 +889,16 @@ class JwtJson {
814
889
  private readValue(): i32 {
815
890
  const b: i32 = this.at(this.pos)
816
891
  if (b === JWT_CH_OPEN_BRACE || b === JWT_CH_OPEN_BRACKET) {
817
- if (this.depth >= toI32(this.stack.length)) {
892
+ const d: i32 = this.depth
893
+ const stack: u8[] = this.stack
894
+ if (d < 0 || d >= toI32(stack.length)) {
818
895
  return this.fail()
819
896
  }
820
897
  const inObject: boolean = b === JWT_CH_OPEN_BRACE
821
- this.stack[this.depth] = toU8(inObject ? JWT_JSON_IN_OBJECT : JWT_JSON_IN_ARRAY)
822
- this.depth += 1
898
+ stack[d] = toU8(inObject ? JWT_JSON_IN_OBJECT : JWT_JSON_IN_ARRAY)
899
+ this.depth = d + 1
823
900
  this.tokStart = this.pos
824
- this.pos += 1
901
+ this.pos = this.step(this.pos, toI32(1))
825
902
  this.tokEnd = this.pos
826
903
  this.want = inObject ? JWT_JSON_WANT_KEY_OR_CLOSE : JWT_JSON_WANT_VALUE_OR_CLOSE
827
904
  this.kind = inObject ? JWT_JSON_OBJECT_BEGIN : JWT_JSON_ARRAY_BEGIN
@@ -847,77 +924,83 @@ class JwtJson {
847
924
  private scanWord(word: string): boolean {
848
925
  const n: i32 = toI32(word.length)
849
926
  for (let i: i32 = 0; i < n; i += 1) {
850
- if (this.at(this.pos + i) !== toI32(word.charCodeAt(i))) {
927
+ if (this.at(this.step(this.pos, i)) !== toI32(word.charCodeAt(i))) {
851
928
  return false
852
929
  }
853
930
  }
854
931
  this.tokStart = this.pos
855
- this.pos += n
932
+ this.pos = this.step(this.pos, n)
856
933
  this.tokEnd = this.pos
857
934
  return true
858
935
  }
859
936
 
860
- /** The digits at the cursor, accumulated negated into `value`; how many, and `integral` cleared on overflow. */
861
- private scanDigits(bound: i64): i32 {
862
- const ten: i64 = toI64(10)
863
- let n: i32 = 0
937
+ /**
938
+ * The digits at the cursor, accumulated into the magnitude `value`, which
939
+ * may not pass `limit`; whether there was one, and `integral` cleared past
940
+ * the limit. The magnitude is unsigned and checked before each step, so it
941
+ * never wraps.
942
+ */
943
+ private scanDigits(limit: u64): boolean {
944
+ const ten: u64 = toU64(10)
945
+ let any: boolean = false
864
946
  let b: i32 = this.at(this.pos)
865
- while (b >= JWT_CH_0 && b <= JWT_CH_9) {
866
- const digit: i64 = toI64(b - JWT_CH_0)
947
+ while (b >= 48 && b <= 57) {
948
+ // `b` is an ASCII digit (JWT_CH_0 to JWT_CH_9): its low four bits are its value.
949
+ const digit: u64 = toU64(toU32(b & 15))
867
950
  if (this.integral) {
868
- if (this.value < (bound + digit) / ten) {
951
+ if (this.value > (limit - digit) / ten) {
869
952
  this.integral = false
870
953
  } else {
871
- this.value = this.value * ten - digit
954
+ this.value = this.value * ten + digit
872
955
  }
873
956
  }
874
- n += 1
875
- this.pos += 1
957
+ any = true
958
+ this.pos = this.step(this.pos, toI32(1))
876
959
  b = this.at(this.pos)
877
960
  }
878
- return n
961
+ return any
879
962
  }
880
963
 
881
964
  /**
882
965
  * A number of RFC 8259 §6: `-? (0 | [1-9][0-9]*) (. [0-9]+)? ([eE] [+-]? [0-9]+)?`.
883
966
  * `integral` says whether it has neither fraction nor exponent and fits an
884
- * `i64` for its sign; accumulating negated reaches -2^63, where a positive
885
- * number stops at 2^63 - 1.
967
+ * `i64` for its sign: a magnitude up to 2^63 when negative, 2^63 - 1 when
968
+ * not.
886
969
  */
887
970
  private scanNumber(): boolean {
888
971
  this.tokStart = this.pos
889
972
  const negative: boolean = this.at(this.pos) === JWT_CH_MINUS
890
973
  if (negative) {
891
- this.pos += 1
974
+ this.pos = this.step(this.pos, toI32(1))
892
975
  }
893
- // -2^63 has no literal (NL2055): -2^31 shifted up 32 bits. A bare literal
894
- // would be a double under Node, where an `i64` is a BigInt.
895
- const min: i64 = toI64(-2147483648) << toI64(32)
896
- const bound: i64 = negative ? min : min + toI64(1)
897
- this.value = toI64(0)
976
+ // 2^63 has no literal (NL2055): 1 shifted up 63 bits. A bare literal
977
+ // would be a double under Node, where a `u64` is a BigInt.
978
+ const most: u64 = toU64(1) << toU64(63)
979
+ const limit: u64 = negative ? most : most - toU64(1)
980
+ this.value = toU64(0)
898
981
  this.integral = true
899
982
  const first: i32 = this.at(this.pos)
900
983
  if (first === JWT_CH_0) {
901
- this.pos += 1
902
- } else if (this.scanDigits(bound) === 0) {
984
+ this.pos = this.step(this.pos, toI32(1))
985
+ } else if (!this.scanDigits(limit)) {
903
986
  return false
904
987
  }
905
988
  if (this.at(this.pos) === JWT_CH_DOT) {
906
- this.pos += 1
989
+ this.pos = this.step(this.pos, toI32(1))
907
990
  this.integral = false
908
- if (this.scanDigits(bound) === 0) {
991
+ if (!this.scanDigits(limit)) {
909
992
  return false
910
993
  }
911
994
  }
912
995
  const e: i32 = this.at(this.pos)
913
996
  if (e === 101 || e === 69) {
914
- this.pos += 1
997
+ this.pos = this.step(this.pos, toI32(1))
915
998
  this.integral = false
916
999
  const sign: i32 = this.at(this.pos)
917
1000
  if (sign === JWT_CH_PLUS || sign === JWT_CH_MINUS) {
918
- this.pos += 1
1001
+ this.pos = this.step(this.pos, toI32(1))
919
1002
  }
920
- if (this.scanDigits(bound) === 0) {
1003
+ if (!this.scanDigits(limit)) {
921
1004
  return false
922
1005
  }
923
1006
  }
@@ -930,7 +1013,7 @@ class JwtJson {
930
1013
  * joined, or `-1` for one RFC 8259 §7 refuses or a lone surrogate.
931
1014
  */
932
1015
  private escapeCodePoint(i: i32): i32 {
933
- const c: i32 = this.at(i + 1)
1016
+ const c: i32 = this.at(this.step(i, toI32(1)))
934
1017
  if (c === JWT_CH_QUOTE || c === JWT_CH_BACKSLASH || c === 47) {
935
1018
  return c
936
1019
  }
@@ -952,29 +1035,30 @@ class JwtJson {
952
1035
  if (c !== 117) {
953
1036
  return -1
954
1037
  }
955
- const hi: i32 = this.hex4(i + 2)
1038
+ const hi: i32 = this.hex4(this.step(i, toI32(2)))
956
1039
  if (hi < 0 || (hi >= 0xdc00 && hi <= 0xdfff)) {
957
1040
  return -1
958
1041
  }
959
1042
  if (hi < 0xd800 || hi > 0xdbff) {
960
1043
  return hi
961
1044
  }
962
- if (this.at(i + 6) !== JWT_CH_BACKSLASH || this.at(i + 7) !== 117) {
1045
+ if (this.at(this.step(i, toI32(6))) !== JWT_CH_BACKSLASH || this.at(this.step(i, toI32(7))) !== 117) {
963
1046
  return -1
964
1047
  }
965
- const lo: i32 = this.hex4(i + 8)
1048
+ const lo: i32 = this.hex4(this.step(i, toI32(8)))
966
1049
  if (lo < 0xdc00 || lo > 0xdfff) {
967
1050
  return -1
968
1051
  }
969
- const plane: i32 = 0x10000
970
- return plane + ((hi - 0xd800) << 10) + (lo - 0xdc00)
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))
971
1055
  }
972
1056
 
973
1057
  /** The four hex digits at `i` as a number, or `-1`. */
974
1058
  private hex4(i: i32): i32 {
975
1059
  let v: i32 = 0
976
1060
  for (let k: i32 = 0; k < 4; k += 1) {
977
- const d: i32 = jwtHexValue(this.at(i + k))
1061
+ const d: i32 = jwtHexValue(this.at(this.step(i, k)))
978
1062
  if (d < 0) {
979
1063
  return -1
980
1064
  }
@@ -985,7 +1069,7 @@ class JwtJson {
985
1069
 
986
1070
  /** How many bytes the escape at `i` takes, given the code point it decodes to. */
987
1071
  private escapeLength(i: i32, cp: i32): i32 {
988
- if (this.at(i + 1) !== 117) {
1072
+ if (this.at(this.step(i, toI32(1))) !== 117) {
989
1073
  return 2
990
1074
  }
991
1075
  return cp >= 0x10000 ? 12 : 6
@@ -1014,12 +1098,12 @@ class JwtJson {
1014
1098
  } else {
1015
1099
  return -1
1016
1100
  }
1017
- const second: i32 = this.at(i + 1)
1101
+ const second: i32 = this.at(this.step(i, toI32(1)))
1018
1102
  if (second < lo || second > hi) {
1019
1103
  return -1
1020
1104
  }
1021
1105
  for (let k: i32 = 2; k <= n; k += 1) {
1022
- const c: i32 = this.at(i + k)
1106
+ const c: i32 = this.at(this.step(i, k))
1023
1107
  if (c < 0x80 || c > 0xbf) {
1024
1108
  return -1
1025
1109
  }
@@ -1030,7 +1114,7 @@ class JwtJson {
1030
1114
  /** The string at the cursor, checked, stepped over, its decoded length in `textBytes`. */
1031
1115
  private scanString(): boolean {
1032
1116
  this.tokStart = this.pos
1033
- let i: i32 = this.pos + 1
1117
+ let i: i32 = this.step(this.pos, toI32(1))
1034
1118
  let decoded: i32 = 0
1035
1119
  while (true) {
1036
1120
  const b: i32 = this.at(i)
@@ -1046,24 +1130,24 @@ class JwtJson {
1046
1130
  if (cp < 0) {
1047
1131
  return false
1048
1132
  }
1049
- decoded += jwtUtf8Width(cp)
1050
- i += this.escapeLength(i, cp)
1133
+ decoded = this.step(decoded, jwtUtf8Width(cp))
1134
+ i = this.step(i, this.escapeLength(i, cp))
1051
1135
  } else if (b < 0x80) {
1052
- decoded += 1
1053
- i += 1
1136
+ decoded = this.step(decoded, toI32(1))
1137
+ i = this.step(i, toI32(1))
1054
1138
  } else {
1055
1139
  const tail: i32 = this.utf8Tail(i)
1056
1140
  if (tail < 0) {
1057
1141
  return false
1058
1142
  }
1059
- decoded += tail + 1
1060
- i += tail + 1
1143
+ decoded = this.step(decoded, this.step(tail, toI32(1)))
1144
+ i = this.step(i, this.step(tail, toI32(1)))
1061
1145
  }
1062
1146
  }
1063
- this.pos = i + 1
1147
+ this.pos = this.step(i, toI32(1))
1064
1148
  this.tokEnd = this.pos
1065
1149
  this.textBytes = decoded
1066
- return true
1150
+ return this.failed === 0
1067
1151
  }
1068
1152
 
1069
1153
  /** Whether the current token is a key or a string. */
@@ -1088,44 +1172,50 @@ class JwtJson {
1088
1172
  if (!this.isText() || n !== this.textBytes) {
1089
1173
  return false
1090
1174
  }
1091
- const end: i32 = this.tokEnd - 1
1092
- let i: i32 = this.tokStart + 1
1175
+ const end: i32 = this.step(this.tokEnd, toI32(-1))
1176
+ let i: i32 = this.step(this.tokStart, toI32(1))
1093
1177
  let o: i32 = 0
1094
1178
  while (i < end) {
1095
1179
  const b: i32 = this.at(i)
1096
1180
  if (b !== JWT_CH_BACKSLASH) {
1097
- if (o >= n || b !== toI32(name.charCodeAt(o))) {
1181
+ if (o >= n) {
1182
+ return false
1183
+ }
1184
+ if (b !== toI32(name.charCodeAt(o))) {
1098
1185
  return false
1099
1186
  }
1100
1187
  o += 1
1101
- i += 1
1188
+ i = this.step(i, toI32(1))
1102
1189
  continue
1103
1190
  }
1104
1191
  const cp: i32 = this.escapeCodePoint(i)
1105
1192
  const width: i32 = jwtUtf8Width(cp)
1106
1193
  for (let k: i32 = 0; k < width; k += 1) {
1107
- if (o >= n || jwtUtf8Byte(cp, width, k) !== toI32(name.charCodeAt(o))) {
1194
+ if (o >= n) {
1195
+ return false
1196
+ }
1197
+ if (jwtUtf8Byte(cp, width, k) !== toI32(name.charCodeAt(o))) {
1108
1198
  return false
1109
1199
  }
1110
1200
  o += 1
1111
1201
  }
1112
- i += this.escapeLength(i, cp)
1202
+ i = this.step(i, this.escapeLength(i, cp))
1113
1203
  }
1114
- return o === n
1204
+ return o === n && this.failed === 0
1115
1205
  }
1116
1206
 
1117
1207
  /**
1118
1208
  * Writes the current key's or string's decoded bytes to `out` from `at` and
1119
1209
  * answers how many; `-1`, writing nothing, when the token is neither or
1120
- * `out` has too little room.
1210
+ * `out` has too little room, and `-1` when a step failed the reader.
1121
1211
  * @internal
1122
1212
  */
1123
1213
  stringInto(out: u8[], at: i32): i32 {
1124
- if (!this.isText() || at < 0 || at > toI32(out.length) - this.textBytes) {
1214
+ if (!this.isText() || at < 0 || toI64(at) > toI64(out.length) - toI64(this.textBytes)) {
1125
1215
  return -1
1126
1216
  }
1127
- const end: i32 = this.tokEnd - 1
1128
- let i: i32 = this.tokStart + 1
1217
+ const end: i32 = this.step(this.tokEnd, toI32(-1))
1218
+ let i: i32 = this.step(this.tokStart, toI32(1))
1129
1219
  let o: i32 = at
1130
1220
  while (i < end) {
1131
1221
  const b: i32 = this.at(i)
@@ -1133,8 +1223,8 @@ class JwtJson {
1133
1223
  if (o >= 0 && o < toI32(out.length)) {
1134
1224
  out[o] = toU8(b)
1135
1225
  }
1136
- o += 1
1137
- i += 1
1226
+ o = this.step(o, toI32(1))
1227
+ i = this.step(i, toI32(1))
1138
1228
  continue
1139
1229
  }
1140
1230
  const cp: i32 = this.escapeCodePoint(i)
@@ -1143,11 +1233,14 @@ class JwtJson {
1143
1233
  if (o >= 0 && o < toI32(out.length)) {
1144
1234
  out[o] = toU8(jwtUtf8Byte(cp, width, k))
1145
1235
  }
1146
- o += 1
1236
+ o = this.step(o, toI32(1))
1147
1237
  }
1148
- i += this.escapeLength(i, cp)
1238
+ i = this.step(i, this.escapeLength(i, cp))
1149
1239
  }
1150
- return o - at
1240
+ if (this.failed !== 0) {
1241
+ return -1
1242
+ }
1243
+ return toI32(toI64(o) - toI64(at))
1151
1244
  }
1152
1245
 
1153
1246
  /** @internal Whether the current token is a number without fraction or exponent that fits an `i64`. */
@@ -1160,7 +1253,9 @@ class JwtJson {
1160
1253
  if (!this.isInteger()) {
1161
1254
  return toI64(0)
1162
1255
  }
1163
- return this.at(this.tokStart) === JWT_CH_MINUS ? this.value : -this.value
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)
1164
1259
  }
1165
1260
 
1166
1261
  /**
@@ -1180,7 +1275,11 @@ class JwtJson {
1180
1275
  if (k !== JWT_JSON_OBJECT_BEGIN && k !== JWT_JSON_ARRAY_BEGIN) {
1181
1276
  return k
1182
1277
  }
1183
- const outer: i32 = this.depth - 1
1278
+ const d: i32 = this.depth
1279
+ if (d < 1) {
1280
+ return this.fail()
1281
+ }
1282
+ const outer: i32 = d - 1
1184
1283
  while (true) {
1185
1284
  k = this.next()
1186
1285
  if (k < 0 || this.depth === outer) {
@@ -1200,13 +1299,17 @@ const jwtClamp = (v: i64, lo: i64, hi: i64): i64 => {
1200
1299
  return v > hi ? hi : v
1201
1300
  }
1202
1301
 
1203
- /** The bytes `[off, off + len)` of `src` as a fresh array. */
1204
- const jwtSlice = (src: u8[], off: i32, len: i32): u8[] => {
1205
- const out: u8[] = new Array<u8>(len)
1206
- for (let i: i32 = 0; i < len && i < toI32(out.length); i += 1) {
1207
- out[i] = src[off + i]
1302
+ /** The bytes `[off, off + len)` of `src` as a fresh array, or `null` for a window outside `src`. */
1303
+ const jwtSlice = (src: u8[], off: i32, len: i32): u8[] | null => {
1304
+ if (off < 0 || len < 0 || toI64(off) + toI64(len) > toI64(src.length)) {
1305
+ return null
1208
1306
  }
1209
- return out
1307
+ // The test above again, in the form the prover reads for `src[k]`.
1308
+ const stop: i32 = toI32(toI64(off) + toI64(len))
1309
+ if (stop > toI32(src.length)) {
1310
+ return null
1311
+ }
1312
+ return src.filter((_b: u8, k: number): boolean => toI32(k) >= off && toI32(k) < stop)
1210
1313
  }
1211
1314
 
1212
1315
  /**
@@ -1218,8 +1321,11 @@ const jwtSlice = (src: u8[], off: i32, len: i32): u8[] => {
1218
1321
  * Only HMAC verifies allocation-free (`hmacKeyedVerify`).
1219
1322
  */
1220
1323
  const jwtVerifyPublic = (key: JwtKey, src: u8[], off: i32, len: i32, sig: u8[], sigLen: i32): boolean => {
1221
- const msg: u8[] = jwtSlice(src, off, len)
1222
- const s: u8[] = jwtSlice(sig, 0, sigLen)
1324
+ const msg: u8[] | null = jwtSlice(src, off, len)
1325
+ const s: u8[] | null = jwtSlice(sig, 0, sigLen)
1326
+ if (msg === null || s === null) {
1327
+ return false
1328
+ }
1223
1329
  const alg: i32 = key.alg
1224
1330
  const rsa: RsaPublicKey | null = key.rsa
1225
1331
  if (rsa !== null) {
@@ -1394,7 +1500,8 @@ export class JwtVerifier {
1394
1500
  }
1395
1501
  let d1: i32 = -1
1396
1502
  let d2: i32 = -1
1397
- for (let i: i32 = off; i < off + len && i < toI32(src.length); i += 1) {
1503
+ const stop: i32 = toI32(toI64(off) + toI64(len))
1504
+ for (let i: i32 = off; i < stop && i < toI32(src.length); i += 1) {
1398
1505
  if (toI32(src[i]) === JWT_DOT) {
1399
1506
  if (d1 < 0) {
1400
1507
  d1 = i
@@ -1409,7 +1516,7 @@ export class JwtVerifier {
1409
1516
  return JWT_ERR_FORMAT
1410
1517
  }
1411
1518
  const part: u8[] = this.part
1412
- const headerLen: i32 = jwtDecode(src, off, d1 - off, part)
1519
+ const headerLen: i32 = jwtDecode(src, off, toI32(toI64(d1) - toI64(off)), part)
1413
1520
  if (headerLen <= 0) {
1414
1521
  return JWT_ERR_FORMAT
1415
1522
  }
@@ -1417,13 +1524,14 @@ export class JwtVerifier {
1417
1524
  if (header !== JWT_OK) {
1418
1525
  return header
1419
1526
  }
1420
- const sigText: i32 = off + len - d2 - 1
1527
+ const sigText: i64 = toI64(stop) - toI64(d2) - toI64(1)
1421
1528
  const sigSize: i32 = jwtSignatureSize(key)
1422
- if (sigText > ((JWT_MAX_SIGNATURE + 2) / 3) * 4) {
1529
+ // 1368 is the base64url length of JWT_MAX_SIGNATURE (1024) bytes: ((1024 + 2) / 3) * 4.
1530
+ if (sigText > toI64(1368)) {
1423
1531
  return JWT_ERR_SIGNATURE
1424
1532
  }
1425
1533
  const sig: u8[] = this.sig
1426
- const sigLen: i32 = jwtDecode(src, d2 + 1, sigText, sig)
1534
+ const sigLen: i32 = jwtDecode(src, toI32(toI64(d2) + toI64(1)), toI32(sigText), sig)
1427
1535
  if (sigLen < 0) {
1428
1536
  return JWT_ERR_FORMAT
1429
1537
  }
@@ -1437,13 +1545,18 @@ export class JwtVerifier {
1437
1545
  }
1438
1546
  const good: boolean =
1439
1547
  hmac !== null
1440
- ? hmacKeyedVerify(hmac, src, off, d2 - off, sig, 0)
1441
- : jwtVerifyPublic(key, src, off, d2 - off, sig, sigLen)
1548
+ ? hmacKeyedVerify(hmac, src, off, toI32(toI64(d2) - toI64(off)), sig, 0)
1549
+ : jwtVerifyPublic(key, src, off, toI32(toI64(d2) - toI64(off)), sig, sigLen)
1442
1550
  if (!good) {
1443
1551
  return JWT_ERR_SIGNATURE
1444
1552
  }
1445
1553
  const claims: u8[] = this.claims
1446
- const claimsLen: i32 = jwtDecode(src, d1 + 1, d2 - d1 - 1, claims)
1554
+ const claimsLen: i32 = jwtDecode(
1555
+ src,
1556
+ toI32(toI64(d1) + toI64(1)),
1557
+ toI32(toI64(d2) - toI64(d1) - toI64(1)),
1558
+ claims
1559
+ )
1447
1560
  if (claimsLen < 0) {
1448
1561
  return JWT_ERR_FORMAT
1449
1562
  }
@@ -1606,13 +1719,17 @@ export class JwtVerifier {
1606
1719
  return JWT_ERR_CLAIMS
1607
1720
  }
1608
1721
  const leeway: i64 = jwtClamp(this.leeway, toI64(0), toI64(JWT_MAX_LEEWAY))
1609
- if (this.hasExp && !(now - leeway < this.exp)) {
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)) {
1610
1727
  return JWT_ERR_EXPIRED
1611
1728
  }
1612
- if (this.hasNbf && this.nbf > now + leeway) {
1729
+ if (this.hasNbf && this.nbf > late) {
1613
1730
  return JWT_ERR_NOT_YET
1614
1731
  }
1615
- if (this.hasIat && this.iat > now + leeway) {
1732
+ if (this.hasIat && this.iat > late) {
1616
1733
  return JWT_ERR_IAT
1617
1734
  }
1618
1735
  if (issuer !== null && !issOk) {
@@ -1646,7 +1763,8 @@ export class JwtVerifier {
1646
1763
  return -1
1647
1764
  }
1648
1765
  let d1: i32 = -1
1649
- for (let i: i32 = off; i < off + len && i < toI32(src.length) && d1 < 0; i += 1) {
1766
+ const stop: i32 = toI32(toI64(off) + toI64(len))
1767
+ for (let i: i32 = off; i < stop && i < toI32(src.length) && d1 < 0; i += 1) {
1650
1768
  if (toI32(src[i]) === JWT_DOT) {
1651
1769
  d1 = i
1652
1770
  }
@@ -1654,7 +1772,7 @@ export class JwtVerifier {
1654
1772
  if (d1 < 0) {
1655
1773
  return -1
1656
1774
  }
1657
- const n: i32 = jwtDecode(src, off, d1 - off, this.part)
1775
+ const n: i32 = jwtDecode(src, off, toI32(toI64(d1) - toI64(off)), this.part)
1658
1776
  if (n <= 0) {
1659
1777
  return -1
1660
1778
  }
@@ -1720,9 +1838,9 @@ const jwtIsAscii = (text: string): boolean => {
1720
1838
  /** The code units of `text` as bytes; the caller has checked they are ASCII. */
1721
1839
  const jwtBytesOf = (text: string): u8[] => {
1722
1840
  const n: i32 = toI32(text.length)
1723
- const out: u8[] = new Array<u8>(n)
1724
- for (let i: i32 = 0; i < n && i < toI32(out.length); i += 1) {
1725
- out[i] = toU8(text.charCodeAt(i))
1841
+ const out: u8[] = []
1842
+ for (let i: i32 = 0; i < n; i += 1) {
1843
+ out.push(toU8(text.charCodeAt(i)))
1726
1844
  }
1727
1845
  return out
1728
1846
  }
@@ -1757,7 +1875,11 @@ export const jwtSignKid = (key: JwtKey, kid: string | null, claims: u8[]): strin
1757
1875
  const hmac: HmacKey | null = key.hmac
1758
1876
  let sig: u8[] | null = null
1759
1877
  if (hmac !== null) {
1760
- const tag: u8[] = new Array<u8>(hmac.size())
1878
+ const tag: u8[] = []
1879
+ const size: i32 = hmac.size()
1880
+ for (let i: i32 = 0; i < size; i += 1) {
1881
+ tag.push(toU8(0))
1882
+ }
1761
1883
  hmacKeyedInto(hmac, msg, 0, toI32(msg.length), tag, 0)
1762
1884
  sig = tag
1763
1885
  } else if (key.alg === JWT_ALG_ES256) {