@amritk/nish-x86_64-linux 0.46.0 → 0.48.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 (46) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +29 -18
  3. package/package.json +1 -1
  4. package/runtime/gfx/README.md +55 -0
  5. package/runtime/gfx/nish-gfx.c +1063 -0
  6. package/runtime/gfx/nish-gfx.h +221 -0
  7. package/runtime/gfx/nish-gfx.overrides.json +14 -0
  8. package/runtime/nish.d.ts +22 -5
  9. package/runtime/nish.h +1 -0
  10. package/runtime/nish.mjs +1 -0
  11. package/runtime/runtime-bytes.c +167 -7
  12. package/runtime/runtime-net.c +122 -52
  13. package/runtime/runtime-uring.c +150 -69
  14. package/runtime/sqlite/nish-sqlite.c +166 -39
  15. package/scripts/build.sh +88 -3
  16. package/std/README.md +2 -2
  17. package/std/base64.ts +28 -30
  18. package/std/compress/deflate.ts +1371 -704
  19. package/std/crypto/aes.ts +81 -45
  20. package/std/crypto/bcrypt-batch.ts +23 -4
  21. package/std/crypto/bcrypt.ts +36 -11
  22. package/std/crypto/chacha20poly1305.ts +93 -52
  23. package/std/crypto/x25519.ts +7 -4
  24. package/std/host/game.ts +52 -20
  25. package/std/host/ring.ts +115 -31
  26. package/std/host/text.ts +9 -3
  27. package/std/json.ts +83 -0
  28. package/std/math.ts +161 -131
  29. package/std/net/handler.ts +24 -6
  30. package/std/net/http-server.ts +358 -141
  31. package/std/net/http1-server.ts +44 -13
  32. package/std/net/prefork.ts +1 -2
  33. package/std/net/static.ts +7 -2
  34. package/std/random.ts +34 -51
  35. package/std/raster.ts +377 -113
  36. package/std/regex.ts +46 -15
  37. package/std/release.ts +19 -13
  38. package/std/sqlite.ts +380 -16
  39. package/std/synth.ts +20 -3
  40. package/std/temporal.ts +740 -363
  41. package/std/term-io.ts +81 -33
  42. package/std/term-pixels.ts +287 -112
  43. package/std/term-probe.ts +7 -3
  44. package/std/term-rate.ts +142 -66
  45. package/std/term.ts +782 -406
  46. package/std/testing.ts +14 -7
package/std/crypto/aes.ts CHANGED
@@ -491,20 +491,26 @@ const aesMixColumns = (q: u64[]): void => {
491
491
  q[7] = t6 ^ n7 ^ aesRowAfterNext(t7)
492
492
  }
493
493
 
494
- /** AddRoundKey (FIPS 197 §5.1.4): the eight words of `rk` from `at` into the state. */
494
+ /**
495
+ * AddRoundKey (FIPS 197 §5.1.4): the eight words of `rk` from `at` into the
496
+ * state. Its callers pass `8 * round` for a round of at most 14, so `at` is at
497
+ * most 112 and the mask `at & 127` changes nothing; it bounds `base + 7`, so
498
+ * the sum cannot overflow, with no branch.
499
+ */
495
500
  const aesAddRoundKey = (q: u64[], rk: u64[], at: i32): void => {
501
+ const base: i32 = at & 127
496
502
  // The first and last key words and the last plane first: their checked
497
503
  // reads prove every other index below.
498
- const k0: u64 = rk[at]
499
- const k7: u64 = rk[at + 7]
504
+ const k0: u64 = rk[base]
505
+ const k7: u64 = rk[base + 7]
500
506
  const q7: u64 = q[7]
501
507
  q[0] = q[0] ^ k0
502
- q[1] = q[1] ^ rk[at + 1]
503
- q[2] = q[2] ^ rk[at + 2]
504
- q[3] = q[3] ^ rk[at + 3]
505
- q[4] = q[4] ^ rk[at + 4]
506
- q[5] = q[5] ^ rk[at + 5]
507
- q[6] = q[6] ^ rk[at + 6]
508
+ q[1] = q[1] ^ rk[base + 1]
509
+ q[2] = q[2] ^ rk[base + 2]
510
+ q[3] = q[3] ^ rk[base + 3]
511
+ q[4] = q[4] ^ rk[base + 4]
512
+ q[5] = q[5] ^ rk[base + 5]
513
+ q[6] = q[6] ^ rk[base + 6]
508
514
  q[7] = q7 ^ k7
509
515
  }
510
516
 
@@ -529,17 +535,20 @@ export const aesBitslicedRound = (q: u64[], rk: u64[], at: i32): void => {
529
535
 
530
536
  /**
531
537
  * The cipher (FIPS 197 §5.1) on the four blocks of `buf[0 .. 64)`, in place,
532
- * under the bitsliced round keys `rk`. `q` is scratch.
538
+ * under the bitsliced round keys `rk`. `q` is scratch. `rounds` is 10 or 14,
539
+ * so `rounds & 15` is `rounds`: the mask lets the bounds prover see that
540
+ * neither `8 * round` nor `8 * last` can overflow.
533
541
  */
534
542
  const aesEncryptBatch = (rk: u64[], rounds: i32, buf: u8[], q: u64[]): void => {
535
543
  aesPack(q, buf)
536
544
  aesAddRoundKey(q, rk, 0)
537
- for (let round: i32 = 1; round < rounds; round++) {
545
+ const last: i32 = rounds & 15
546
+ for (let round: i32 = 1; round < last; round++) {
538
547
  aesBitslicedRound(q, rk, 8 * round)
539
548
  }
540
549
  aesSbox(q)
541
550
  aesShiftRows(q)
542
- aesAddRoundKey(q, rk, 8 * rounds)
551
+ aesAddRoundKey(q, rk, 8 * last)
543
552
  aesUnpack(buf, q)
544
553
  }
545
554
 
@@ -570,26 +579,34 @@ const aesSubWord = (w: u32, q: u64[]): u32 => {
570
579
  return out
571
580
  }
572
581
 
573
- /** Four bytes of `data` from `at` as a big-endian word; the caller has checked the window. */
582
+ /**
583
+ * Four bytes of `data` from `at` as a big-endian word, or 0 for a window
584
+ * outside `data`, which no caller passes: each has checked it.
585
+ */
574
586
  const aesLoad32 = (data: u8[], at: i32): u32 => {
575
- // The last byte first: every caller's `at` is non-negative, so its checked
576
- // read proves the other three.
577
- const b3: u32 = toU32(data[at + 3])
587
+ if (at < 0 || at > toI32(data.length) - 4) {
588
+ return 0
589
+ }
578
590
  return (
579
591
  (toU32(data[at]) << toU32(24)) |
580
592
  (toU32(data[at + 1]) << toU32(16)) |
581
593
  (toU32(data[at + 2]) << toU32(8)) |
582
- b3
594
+ toU32(data[at + 3])
583
595
  )
584
596
  }
585
597
 
586
- /** `w` big-endian into `out[at .. at + 4)`; the caller has checked the window. */
598
+ /**
599
+ * `w` big-endian into `out[at .. at + 4)`, or nothing for a window outside
600
+ * `out`, which no caller passes: each has checked it.
601
+ */
587
602
  const aesStore32 = (out: u8[], at: i32, w: u32): void => {
588
- // The first and last bytes first: their checked stores prove the two between.
603
+ if (at < 0 || at > toI32(out.length) - 4) {
604
+ return
605
+ }
589
606
  out[at] = toU8(w >> toU32(24))
590
- out[at + 3] = toU8(w)
591
607
  out[at + 1] = toU8(w >> toU32(16))
592
608
  out[at + 2] = toU8(w >> toU32(8))
609
+ out[at + 3] = toU8(w)
593
610
  }
594
611
 
595
612
  /**
@@ -612,8 +629,9 @@ const aesScheduleInto = (key: u8[], rounds: i32, rk: u64[]): void => {
612
629
  const nk: integer<4, 8> = aes128 ? 4 : 8
613
630
  const total: integer<44, 60> = aes128 ? 44 : 60
614
631
  const lastRound: integer<10, 14> = aes128 ? 10 : 14
615
- // Sized for AES-256; AES-128 uses the first 44 words.
616
- const w: u32[] = new Array<u32>(60)
632
+ // Sized for AES-256 and rounded up to 64 so a mask bounds its index;
633
+ // AES-128 uses the first 44 words.
634
+ const w: u32[] = new Array<u32>(64)
617
635
  const q: u64[] = new Array<u64>(8)
618
636
  const keyLength: i32 = toI32(key.length)
619
637
  // The key's own Nk words.
@@ -638,7 +656,9 @@ const aesScheduleInto = (key: u8[], rounds: i32, rk: u64[]): void => {
638
656
  } else if (nk > 6 && pos === 4) {
639
657
  temp = aesSubWord(temp, q)
640
658
  }
641
- const word: u32 = w[i - nk] ^ temp
659
+ // `i - nk` is in [0, 56): the mask is a no-op the bounds prover can read.
660
+ const back: i32 = (i - nk) & 63
661
+ const word: u32 = w[back] ^ temp
642
662
  w[i] = word
643
663
  prev = word
644
664
  }
@@ -683,13 +703,20 @@ const aesLoad64 = (data: u8[], at: i32, end: i32): u64 => {
683
703
  return v
684
704
  }
685
705
 
686
- /** `v` big-endian into `out[at .. at + 8)`; a byte outside `out` is dropped. */
706
+ /**
707
+ * `v` big-endian into `out[at .. at + 8)`, or nothing for a window outside
708
+ * `out`, which no caller passes: each writes inside an array it sized.
709
+ */
687
710
  const aesStore64 = (out: u8[], at: i32, v: u64): void => {
711
+ if (at < 0 || at > toI32(out.length) - 8) {
712
+ return
713
+ }
688
714
  const length: i32 = toI32(out.length)
715
+ let to: i32 = at
689
716
  for (let i: i32 = 0; i < 8; i++) {
690
- const to: i32 = at + i
691
- if (to >= 0 && to < length) {
717
+ if (to < length) {
692
718
  out[to] = toU8(v >> toU64((7 - i) * 8))
719
+ to = to + 1
693
720
  }
694
721
  }
695
722
  }
@@ -823,9 +850,11 @@ export const aesKeyInto = (key: u8[], out: AesKey): boolean => {
823
850
  out.rounds = rounds
824
851
  const h: u8[] = new Array<u8>(AES_BLOCK)
825
852
  aesEncryptOne(out, new Array<u8>(AES_BLOCK), h)
826
- // The second word first: its checked store proves the first.
827
- hash[1] = aesLoad64(h, 8, 16)
828
- hash[0] = aesLoad64(h, 0, 8)
853
+ // Checked above; tested again here, where the bounds prover can see it.
854
+ if (toI32(hash.length) > 1) {
855
+ hash[0] = aesLoad64(h, 0, 8)
856
+ hash[1] = aesLoad64(h, 8, 16)
857
+ }
829
858
  wipe(h)
830
859
  return true
831
860
  }
@@ -1173,9 +1202,12 @@ const aesGcmTag = (
1173
1202
  ghashLengths(y, hHi, hLo, aadLength, textLength)
1174
1203
  const mask: u8[] = new Array<u8>(AES_BLOCK)
1175
1204
  aesEncryptOne(key, j0, mask)
1176
- // The second word first: its checked read proves the first.
1177
- y[1] = y[1] ^ aesLoad64(mask, 8, 16)
1178
- y[0] = y[0] ^ aesLoad64(mask, 0, 16)
1205
+ // Every caller passes two words; tested here, where the bounds prover can
1206
+ // see it past the calls above.
1207
+ if (toI32(y.length) > 1) {
1208
+ y[0] = y[0] ^ aesLoad64(mask, 0, 16)
1209
+ y[1] = y[1] ^ aesLoad64(mask, 8, 16)
1210
+ }
1179
1211
  wipe(mask)
1180
1212
  }
1181
1213
 
@@ -1215,7 +1247,7 @@ export const aesGcmSeal = (key: AesKey, iv: u8[], aad: u8[], plaintext: u8[]): u
1215
1247
  // is still an array length.
1216
1248
  const h: u64[] = key.h
1217
1249
  if (
1218
- length > 0x7fffffef ||
1250
+ length > 2147483631 ||
1219
1251
  toI32(h.length) !== 2 ||
1220
1252
  !aesKeyUsable(key) ||
1221
1253
  toI32(iv.length) === 0 ||
@@ -1231,10 +1263,11 @@ export const aesGcmSeal = (key: AesKey, iv: u8[], aad: u8[], plaintext: u8[]): u
1231
1263
  aesGcmCtr(key, j0, plaintext, out, length)
1232
1264
  const tag: u64[] = new Array<u64>(2)
1233
1265
  aesGcmTag(tag, key, hHi, hLo, j0, aad, out, length)
1234
- // The second word first: its checked read proves the first.
1235
- const tagLo: u64 = tag[1]
1236
- aesStore64(out, length, tag[0])
1237
- aesStore64(out, length + 8, tagLo)
1266
+ // Two words, as made above; tested where the bounds prover can see it.
1267
+ if (toI32(tag.length) > 1) {
1268
+ aesStore64(out, length, tag[0])
1269
+ aesStore64(out, length + 8, tag[1])
1270
+ }
1238
1271
  wipe(j0)
1239
1272
  return out
1240
1273
  }
@@ -1277,14 +1310,17 @@ export const aesGcmOpen = (key: AesKey, iv: u8[], aad: u8[], sealed: u8[]): u8[]
1277
1310
  const j0: u8[] = aesGcmJ0(key, hHi, hLo, iv)
1278
1311
  const tag: u64[] = new Array<u64>(2)
1279
1312
  aesGcmTag(tag, key, hHi, hLo, j0, aad, sealed, length)
1280
- // The second word first: its checked read proves the first.
1281
- const tagLo: u64 = tag[1]
1282
- const same: u64 = aesGcmTagMask(
1283
- tag[0],
1284
- tagLo,
1285
- aesLoad64(sealed, length, total),
1286
- aesLoad64(sealed, length + 8, total)
1287
- )
1313
+ // Two words, as made above; tested where the bounds prover can see it, and
1314
+ // a tag it could not read matches nothing.
1315
+ let same: u64 = 0
1316
+ if (toI32(tag.length) > 1) {
1317
+ same = aesGcmTagMask(
1318
+ tag[0],
1319
+ tag[1],
1320
+ aesLoad64(sealed, length, total),
1321
+ aesLoad64(sealed, length + 8, total)
1322
+ )
1323
+ }
1288
1324
  wipe(tag)
1289
1325
  let out: u8[] | null = null
1290
1326
  if ((same & hashKeySet) !== toU64(0)) {
@@ -222,6 +222,9 @@ const bcryptBatchLane = (share: BcryptBatchShare, list: i32[], n: i32, i: i32):
222
222
  return null
223
223
  }
224
224
  const at: i32 = list[i]
225
+ if (at < 0 || at >= toI32(share.passwords.length)) {
226
+ return null
227
+ }
225
228
  return share.passwords[at]
226
229
  }
227
230
 
@@ -535,7 +538,9 @@ export class BcryptBatcher {
535
538
  * @returns The count; 0 once `run` has handed out every answer.
536
539
  */
537
540
  pending(): i32 {
538
- return toI32(this.kinds.length) - this.head + toI32(this.early.length)
541
+ // Summed as an `i64`, where three array lengths cannot overflow; the
542
+ // count itself is at most the two queues' lengths.
543
+ return toI32(toI64(this.kinds.length) - toI64(this.head) + toI64(this.early.length))
539
544
  }
540
545
 
541
546
  /**
@@ -625,8 +630,18 @@ export class BcryptBatcher {
625
630
  if (queued >= this.width || head >= toI32(this.arrived.length)) {
626
631
  return 0
627
632
  }
628
- const waited: i64 = now - this.arrived[head]
629
- return waited >= this.holdNanos ? 0 : this.holdNanos - waited
633
+ // Both clocks are `monotonicNanos()` readings, never negative; a request
634
+ // that reads as arriving after `now` has waited no time.
635
+ const arrived: i64 = this.arrived[head]
636
+ const hold: i64 = this.holdNanos
637
+ if (now < 0 || arrived < 0 || hold <= 0) {
638
+ return 0
639
+ }
640
+ const waited: i64 = now - arrived
641
+ if (waited < 0) {
642
+ return hold
643
+ }
644
+ return waited >= hold ? 0 : hold - waited
630
645
  }
631
646
 
632
647
  /**
@@ -722,7 +737,11 @@ export class BcryptBatcher {
722
737
  held.wipe()
723
738
  }
724
739
  }
725
- this.head = this.head + taken
740
+ // `taken` is at most what was queued past `head`, so this is at most the
741
+ // queue's length; summed as an `i64` and capped there for the prover.
742
+ const kinds: i32 = toI32(this.kinds.length)
743
+ const after: i64 = toI64(head) + toI64(taken)
744
+ this.head = after < toI64(kinds) ? toI32(after) : kinds
726
745
  this.compact()
727
746
  return answers
728
747
  }
@@ -1103,7 +1103,7 @@ const bcryptBody2 = (st: u32[]): void => {
1103
1103
  let r0: u32 = 0
1104
1104
  let l1: u32 = 0
1105
1105
  let r1: u32 = 0
1106
- for (let i: i32 = 0; i + 1 < 1042; i += 2) {
1106
+ for (let i: i32 = 0; i < 1041; i += 2) {
1107
1107
  l0 ^= st[0]
1108
1108
  l1 ^= st[1042]
1109
1109
  for (let n: i32 = 1; n < 17; n += 2) {
@@ -1152,7 +1152,7 @@ const bcryptBody3 = (st: u32[]): void => {
1152
1152
  let r1: u32 = 0
1153
1153
  let l2: u32 = 0
1154
1154
  let r2: u32 = 0
1155
- for (let i: i32 = 0; i + 1 < 1042; i += 2) {
1155
+ for (let i: i32 = 0; i < 1041; i += 2) {
1156
1156
  l0 ^= st[0]
1157
1157
  l1 ^= st[1042]
1158
1158
  l2 ^= st[2084]
@@ -1252,7 +1252,7 @@ const bcryptBody4 = (st: u32[]): void => {
1252
1252
  let r2: u32 = 0
1253
1253
  let l3: u32 = 0
1254
1254
  let r3: u32 = 0
1255
- for (let i: i32 = 0; i + 1 < 1042; i += 2) {
1255
+ for (let i: i32 = 0; i < 1041; i += 2) {
1256
1256
  l0 ^= st[0]
1257
1257
  l1 ^= st[1042]
1258
1258
  l2 ^= st[2084]
@@ -1353,7 +1353,7 @@ const bcryptBody5 = (st: u32[]): void => {
1353
1353
  let r3: u32 = 0
1354
1354
  let l4: u32 = 0
1355
1355
  let r4: u32 = 0
1356
- for (let i: i32 = 0; i + 1 < 1042; i += 2) {
1356
+ for (let i: i32 = 0; i < 1041; i += 2) {
1357
1357
  l0 ^= st[0]
1358
1358
  l1 ^= st[1042]
1359
1359
  l2 ^= st[2084]
@@ -1476,7 +1476,7 @@ const bcryptBody6 = (st: u32[]): void => {
1476
1476
  let r4: u32 = 0
1477
1477
  let l5: u32 = 0
1478
1478
  let r5: u32 = 0
1479
- for (let i: i32 = 0; i + 1 < 1042; i += 2) {
1479
+ for (let i: i32 = 0; i < 1041; i += 2) {
1480
1480
  l0 ^= st[0]
1481
1481
  l1 ^= st[1042]
1482
1482
  l2 ^= st[2084]
@@ -1615,9 +1615,14 @@ const bcryptBodyN = (st: u32[], width: i32): void => {
1615
1615
  }
1616
1616
  }
1617
1617
 
1618
- /** `bcryptEncrypt` under the state at `at`: one block, `lr[0]` and `lr[1]` in place. */
1618
+ /**
1619
+ * `bcryptEncrypt` under the state at `at`: one block, `lr[0]` and `lr[1]` in
1620
+ * place. `st` is a batch state and `at` one of its six slots, `1042 * k` for
1621
+ * `k` up to 5; anything else leaves `lr` as it was. The bounds are literals
1622
+ * so the bounds prover reads every S-box index below from them.
1623
+ */
1619
1624
  const bcryptEncryptAt = (st: u32[], at: i32, lr: u32[]): void => {
1620
- if (at < 0 || at > toI32(st.length) - 1042) {
1625
+ if (at < 0 || at > 5210 || toI32(st.length) < 6252) {
1621
1626
  return
1622
1627
  }
1623
1628
  if (lr.length < 2) {
@@ -2277,8 +2282,11 @@ const bcryptCalibrateTime = (width: i32): i64 => {
2277
2282
  const p5: Secret<u8[]> | null = width > 5 ? bcryptCalibrateKey(5) : null
2278
2283
  const start: i64 = monotonicNanos()
2279
2284
  const made: Result<string[], string> = bcryptHashN(p0, p1, p2, p3, p4, p5, salts, BCRYPT_MIN_COST)
2280
- const took: i64 = monotonicNanos() - start
2281
- if (made.isOk() && (best < 0 || took < best)) {
2285
+ const end: i64 = monotonicNanos()
2286
+ // Monotonic readings are never negative, nor `end` before `start`; a run
2287
+ // that reads otherwise is not counted.
2288
+ const took: i64 = start >= 0 && end >= 0 && end >= start ? end - start : -1
2289
+ if (made.isOk() && took >= 0 && (best < 0 || took < best)) {
2282
2290
  best = took
2283
2291
  }
2284
2292
  wipe(p0)
@@ -2302,6 +2310,24 @@ const bcryptCalibrateTime = (width: i32): i64 => {
2302
2310
  return best < 0 ? BCRYPT_CALIBRATE_NEVER : best
2303
2311
  }
2304
2312
 
2313
+ /**
2314
+ * Whether `took` nanoseconds for `width` passwords is less per password than
2315
+ * `bestTime` for `best`, without dividing. Every time is a
2316
+ * `bcryptCalibrateTime`, at most an hour, and every width 1 to 6; outside
2317
+ * those it answers `false`. Inside them each product is below 2^45, so the
2318
+ * unsigned multiplies, which cannot panic, never wrap either: the bounds
2319
+ * prover's ranges stop at 2^31 and could not prove a signed one.
2320
+ */
2321
+ const bcryptCalibrateFaster = (took: i64, width: i32, bestTime: i64, best: i32): boolean => {
2322
+ if (took < 0 || took > 3600000000000 || bestTime < 0 || bestTime > 3600000000000) {
2323
+ return false
2324
+ }
2325
+ if (width < 1 || width > 6 || best < 1 || best > 6) {
2326
+ return false
2327
+ }
2328
+ return toU64(took) * toU64(best) < toU64(bestTime) * toU64(width)
2329
+ }
2330
+
2305
2331
  /**
2306
2332
  * The batch width, 1 to 6, that hashes fastest per password on this core:
2307
2333
  * each width timed on throwaway passwords at cost 4, the least time of a few
@@ -2317,8 +2343,7 @@ export const bcryptCalibrate = (): i32 => {
2317
2343
  let bestTime: i64 = bcryptCalibrateTime(1)
2318
2344
  for (let w: i32 = 2; w <= 6; w++) {
2319
2345
  const took: i64 = bcryptCalibrateTime(w)
2320
- // took / w < bestTime / best, without dividing.
2321
- if (took * toI64(best) < bestTime * toI64(w)) {
2346
+ if (bcryptCalibrateFaster(took, w, bestTime, best)) {
2322
2347
  best = w
2323
2348
  bestTime = took
2324
2349
  }
@@ -98,15 +98,19 @@ const POLY1305_HIGH_BIT: i32 = 0x1000000
98
98
  const chacha20Rotl = (x: u32, n: u32): u32 => (x << n) | (x >>> (32 - n))
99
99
 
100
100
  /**
101
- * The little-endian `u32` at `buf[at]` to `buf[at + 3]`. The last byte is read
102
- * first, so that its bounds check covers the other three; every caller passes
103
- * a window inside `buf`. `poly1305Le64` and `chacha20Poly1305TagMatch` read
104
- * theirs the same way. The copy `tests/cases/ct_asm_chacha20poly1305` holds to
105
- * the disassembly check reads with `uncheckedGet` and `wrappingAdd` instead,
106
- * because a bounds or overflow check is a branch that check refuses; the ones
107
- * here test only the public offset.
101
+ * The little-endian `u32` at `buf[at]` to `buf[at + 3]`, or 0 for a window
102
+ * outside `buf`, which no caller passes: the guard lets the range prover
103
+ * discharge every index, whatever order the bytes are read in.
104
+ * `poly1305Le64` guards its eight bytes the same way.
105
+ * The copy `tests/cases/ct_asm_chacha20poly1305` holds to the disassembly
106
+ * check reads with `uncheckedGet` and `wrappingAdd` instead, because a bounds
107
+ * or overflow check is a branch that check refuses; the ones here test only
108
+ * the public offset.
108
109
  */
109
110
  const chacha20Word = (buf: u8[], at: i32): u32 => {
111
+ if (at < 0 || at > toI32(buf.length) - 4) {
112
+ return 0
113
+ }
110
114
  const b3: u32 = toU32(buf[at + 3])
111
115
  return toU32(buf[at]) | (toU32(buf[at + 1]) << 8) | (toU32(buf[at + 2]) << 16) | (b3 << 24)
112
116
  }
@@ -250,35 +254,57 @@ const chacha20Core = (state: u32[], out: u32[]): void => {
250
254
  }
251
255
 
252
256
  /**
253
- * `dst[dstAt + i] = src[srcAt + i] ^ keystream` for `len` bytes, the keystream
254
- * running from `state`'s counter on, and the counter left one past the last
255
- * block used (RFC 8439 §2.4). The caller has checked every window and that
256
- * the counter does not wrap. The last keystream block is wiped before it
257
- * returns; `state` is the caller's to wipe.
257
+ * `dst[dstAt + i] = src[srcAt + i] ^ keystream[i]` for each `i` below `len`,
258
+ * the keystream running from `state`'s counter on, and the counter left one
259
+ * past the last block used (RFC 8439 §2.4). The caller has checked both
260
+ * windows and that the counter does not wrap; a window that ran short would
261
+ * end the loop there rather than panic. The last keystream block is wiped
262
+ * before it returns; `state` is the caller's to wipe.
258
263
  */
259
264
  const chacha20XorInto = (state: u32[], src: u8[], srcAt: i32, len: i32, dst: u8[], dstAt: i32): void => {
260
265
  const keystream: u32[] = new Array<u32>(16)
261
- let done: i32 = 0
262
- while (done < len) {
266
+ // `n` bytes done, read from `s` and written to `d`. Each cursor moves only
267
+ // under a guard that bounds it, so the range prover discharges every index
268
+ // and sum; a window the caller checked passes every guard.
269
+ let n: i32 = 0
270
+ let s: i32 = srcAt
271
+ let d: i32 = dstAt
272
+ while (n < len && s >= 0 && s < toI32(src.length) && d >= 0 && d < toI32(dst.length)) {
263
273
  chacha20Core(state, keystream)
264
- state[12] = state[12] + 1
265
- const take: i32 = len - done < 64 ? len - done : 64
274
+ if (toI32(state.length) > 12) {
275
+ state[12] = state[12] + 1
276
+ }
266
277
  for (let w: i32 = 0; w < 16 && w < toI32(keystream.length); w++) {
267
278
  const word: u32 = keystream[w]
268
- const at: i32 = done + 4 * w
269
- if (at + 4 <= done + take) {
279
+ if (
280
+ n <= 2147483643 &&
281
+ n + 4 <= len &&
282
+ s >= 0 &&
283
+ s <= toI32(src.length) - 4 &&
284
+ d >= 0 &&
285
+ d <= toI32(dst.length) - 4
286
+ ) {
270
287
  // A whole word, the common case: four bytes with fixed shifts.
271
- dst[dstAt + at] = src[srcAt + at] ^ toU8(word)
272
- dst[dstAt + at + 1] = src[srcAt + at + 1] ^ toU8(word >>> 8)
273
- dst[dstAt + at + 2] = src[srcAt + at + 2] ^ toU8(word >>> 16)
274
- dst[dstAt + at + 3] = src[srcAt + at + 3] ^ toU8(word >>> 24)
288
+ dst[d] = src[s] ^ toU8(word)
289
+ dst[d + 1] = src[s + 1] ^ toU8(word >>> 8)
290
+ dst[d + 2] = src[s + 2] ^ toU8(word >>> 16)
291
+ dst[d + 3] = src[s + 3] ^ toU8(word >>> 24)
292
+ n = n + 4
293
+ s = s + 4
294
+ d = d + 4
275
295
  } else {
276
- for (let k: i32 = 0; at + k < done + take; k++) {
277
- dst[dstAt + at + k] = src[srcAt + at + k] ^ toU8(word >>> toU32(8 * k))
296
+ for (
297
+ let k: i32 = 0;
298
+ k < 4 && n < len && s >= 0 && s < toI32(src.length) && d >= 0 && d < toI32(dst.length);
299
+ k++
300
+ ) {
301
+ dst[d] = src[s] ^ toU8(word >>> toU32(8 * k))
302
+ n = n + 1
303
+ s = s + 1
304
+ d = d + 1
278
305
  }
279
306
  }
280
307
  }
281
- done = done + take
282
308
  }
283
309
  wipe(keystream)
284
310
  }
@@ -395,8 +421,11 @@ export const chacha20 = (key: u8[], counter: u32, nonce: u8[], data: u8[]): u8[]
395
421
  return out
396
422
  }
397
423
 
398
- /** The little-endian `u64` at `buf[at]` to `buf[at + 7]`. */
424
+ /** The little-endian `u64` at `buf[at]` to `buf[at + 7]`, or 0 for a window outside `buf`, as in `chacha20Word`. */
399
425
  const poly1305Le64 = (buf: u8[], at: i32): u64 => {
426
+ if (at < 0 || at > toI32(buf.length) - 8) {
427
+ return 0
428
+ }
400
429
  const b7: u64 = toU64(buf[at + 7])
401
430
  return (
402
431
  toU64(buf[at]) |
@@ -451,7 +480,7 @@ const poly1305Clamp = (key: u8[], r: u64[]): void => {
451
480
  */
452
481
  const poly1305Block = (h: u64[], r: u64[], m: u8[], at: i32, high: u64): void => {
453
482
  const lo: u64 = poly1305Le64(m, at)
454
- const hi: u64 = poly1305Le64(m, toI32(toU32(at) + 8))
483
+ const hi: u64 = poly1305Le64(m, at + 8)
455
484
  const h0: u64 = h[0] + (lo & 0x3ffffff)
456
485
  const h1: u64 = h[1] + ((lo >>> 26) & 0x3ffffff)
457
486
  const h2: u64 = h[2] + (((lo >>> 52) | (hi << 12)) & 0x3ffffff)
@@ -567,23 +596,22 @@ const poly1305Finish = (h: u64[], key: u8[], tag: u8[]): void => {
567
596
  }
568
597
 
569
598
  /**
570
- * Feeds the `len` bytes of `buf` from `at` to the accumulator `h`, sixteen at
599
+ * Feeds the first `len` bytes of `buf` to the accumulator `h`, sixteen at
571
600
  * a time. A short last block is padded one of two ways: `zeroPad` is the
572
601
  * AEAD's (RFC 8439 §2.8, zeros up to 16 bytes and the block counted as whole),
573
602
  * and otherwise it is §2.5.1's, a 0x01 byte after the message and zeros after
574
603
  * that, with no bit at 2^128.
575
604
  */
576
- const poly1305Absorb = (h: u64[], r: u64[], buf: u8[], at: i32, len: i32, zeroPad: boolean): void => {
577
- const whole: i32 = len >> 4
578
- for (let i: i32 = 0; i < whole; i++) {
579
- poly1305Block(h, r, buf, at + 16 * i, toU64(POLY1305_HIGH_BIT))
605
+ const poly1305Absorb = (h: u64[], r: u64[], buf: u8[], len: i32, zeroPad: boolean): void => {
606
+ const whole: i32 = (len >> 4) & 0x7ffffff
607
+ const end: i32 = 16 * whole
608
+ for (let off: i32 = 0; off < end; off += 16) {
609
+ poly1305Block(h, r, buf, off, toU64(POLY1305_HIGH_BIT))
580
610
  }
581
611
  const tail: i32 = len & 15
582
612
  if (tail > 0) {
583
613
  const last: u8[] = new Array<u8>(16)
584
- // Where the tail starts, summed in `u32`: it is inside `buf`, so the sum
585
- // cannot wrap, and the loop stops at the end of `buf` all the same.
586
- let k: i32 = toI32(toU32(at) + (toU32(whole) << 4))
614
+ let k: i32 = end
587
615
  for (let i: i32 = 0; i < tail && i < toI32(last.length) && k >= 0 && k < toI32(buf.length); i++) {
588
616
  last[i] = buf[k]
589
617
  k = k + 1
@@ -615,7 +643,7 @@ export const poly1305 = (key: u8[], msg: u8[]): u8[] | null => {
615
643
  const h: u64[] = new Array<u64>(5)
616
644
  const r: u64[] = new Array<u64>(5)
617
645
  poly1305Clamp(key, r)
618
- poly1305Absorb(h, r, msg, 0, toI32(msg.length), false)
646
+ poly1305Absorb(h, r, msg, toI32(msg.length), false)
619
647
  // `POLY1305_TAG_SIZE`, written out because a literal length is what proves
620
648
  // `poly1305Finish`'s stores in range.
621
649
  const tag: u8[] = new Array<u8>(16)
@@ -648,17 +676,17 @@ export const poly1305KeyGen = (key: u8[], nonce: u8[]): u8[] | null => {
648
676
  }
649
677
 
650
678
  /**
651
- * The AEAD tag of RFC 8439 §2.8 over `aad` and the `len` bytes of ciphertext
652
- * at `ct[at]`: Poly1305 under `polyKey` of the AAD and the ciphertext, each
653
- * zero-padded to 16 bytes, and then both lengths as little-endian `u64`s.
679
+ * The AEAD tag of RFC 8439 §2.8 over `aad` and the first `len` bytes of `ct`:
680
+ * Poly1305 under `polyKey` of the AAD and the ciphertext, each zero-padded to
681
+ * 16 bytes, and then both lengths as little-endian `u64`s.
654
682
  */
655
- const chacha20Poly1305Tag = (polyKey: u8[], aad: u8[], ct: u8[], at: i32, len: i32): u8[] => {
683
+ const chacha20Poly1305Tag = (polyKey: u8[], aad: u8[], ct: u8[], len: i32): u8[] => {
656
684
  const aadLen: i32 = toI32(aad.length)
657
685
  const h: u64[] = new Array<u64>(5)
658
686
  const r: u64[] = new Array<u64>(5)
659
687
  poly1305Clamp(polyKey, r)
660
- poly1305Absorb(h, r, aad, 0, aadLen, true)
661
- poly1305Absorb(h, r, ct, at, len, true)
688
+ poly1305Absorb(h, r, aad, aadLen, true)
689
+ poly1305Absorb(h, r, ct, len, true)
662
690
  const lengths: u8[] = new Array<u8>(16)
663
691
  for (let i: i32 = 0; i < 4; i++) {
664
692
  lengths[i] = toU8(aadLen >>> (8 * i))
@@ -681,7 +709,8 @@ const chacha20Poly1305Tag = (polyKey: u8[], aad: u8[], ct: u8[], at: i32, len: i
681
709
  * first difference is.
682
710
  *
683
711
  * Written out byte by byte and without a length check so that `tests/cases/ct_asm_chacha20poly1305` can hold a copy of it to the
684
- * disassembly check; the caller has checked that `sealed` holds the window.
712
+ * disassembly check. Its one caller passes the received tag copied into a
713
+ * 16-byte array, at 0, so the range prover discharges every index.
685
714
  */
686
715
  const chacha20Poly1305TagMatch = (tag: u8[], sealed: u8[], at: i32): u32 => {
687
716
  const last: u32 = toU32(tag[15] ^ sealed[at + 15])
@@ -723,24 +752,25 @@ const chacha20Poly1305TagMatch = (tag: u8[], sealed: u8[], at: i32): u32 => {
723
752
  export const chacha20Poly1305Seal = (key: u8[], nonce: u8[], aad: u8[], plaintext: u8[]): u8[] | null => {
724
753
  const len: i32 = toI32(plaintext.length)
725
754
  // 2^31 - 17: the longest plaintext whose sealed form, sixteen bytes longer,
726
- // is still an array length.
727
- const longest: i32 = 0x7fffffef
755
+ // is still an array length. Written in decimal, since the range prover
756
+ // reads a decimal bound and not a hex one.
757
+ if (len > 2147483631) {
758
+ return null
759
+ }
728
760
  if (
729
761
  toI32(key.length) !== CHACHA20_KEY_SIZE ||
730
762
  toI32(nonce.length) !== CHACHA20POLY1305_NONCE_SIZE ||
731
- len > longest ||
732
763
  !chacha20CounterFits(1, len)
733
764
  ) {
734
765
  return null
735
766
  }
736
- // Summed in `u32`: `len` is at most `longest`, so the sum is at most
737
- // 2^31 - 1 and reads back as the same `i32`.
738
- const total: i32 = toI32(toU32(len) + toU32(POLY1305_TAG_SIZE))
767
+ // `len` is at most 2^31 - 17, so the sum is at most 2^31 - 1.
768
+ const total: i32 = len + POLY1305_TAG_SIZE
739
769
  const state: u32[] = chacha20State(key, 0, nonce, 0)
740
770
  const polyKey: u8[] = chacha20Poly1305Setup(state)
741
771
  const sealed: u8[] = new Array<u8>(total)
742
772
  chacha20XorInto(state, plaintext, 0, len, sealed, 0)
743
- const tag: u8[] = chacha20Poly1305Tag(polyKey, aad, sealed, 0, len)
773
+ const tag: u8[] = chacha20Poly1305Tag(polyKey, aad, sealed, len)
744
774
  let k: i32 = len
745
775
  for (let i: i32 = 0; i < toI32(tag.length) && k >= 0 && k < toI32(sealed.length); i++) {
746
776
  sealed[k] = tag[i]
@@ -783,10 +813,21 @@ export const chacha20Poly1305Open = (key: u8[], nonce: u8[], aad: u8[], sealed:
783
813
  }
784
814
  const state: u32[] = chacha20State(key, 0, nonce, 0)
785
815
  const polyKey: u8[] = chacha20Poly1305Setup(state)
786
- const tag: u8[] = chacha20Poly1305Tag(polyKey, aad, sealed, 0, len)
816
+ const tag: u8[] = chacha20Poly1305Tag(polyKey, aad, sealed, len)
817
+ // The received tag, copied out so that the compare reads two 16-byte arrays
818
+ // at offsets the range prover can see; `len` is `total - 16`, so every
819
+ // guard holds.
820
+ const received: u8[] = new Array<u8>(16)
821
+ let j: i32 = len
822
+ for (let i: i32 = 0; i < 16; i++) {
823
+ if (j >= 0 && j < toI32(sealed.length)) {
824
+ received[i] = sealed[j]
825
+ j = j + 1
826
+ }
827
+ }
787
828
  // The one branch on the comparison, and it is on the answer the caller is
788
829
  // about to learn anyway.
789
- const match: u32 = chacha20Poly1305TagMatch(tag, sealed, len)
830
+ const match: u32 = chacha20Poly1305TagMatch(tag, received, 0)
790
831
  wipe(polyKey)
791
832
  wipe(tag)
792
833
  let plaintext: u8[] | null = null