@amritk/nish-x86_64-linux 0.45.1 → 0.47.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.
@@ -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
@@ -518,6 +518,8 @@ export const f25519Decode = (bytes: u8[]): i64[] => {
518
518
  */
519
519
  const f25519DecodeInto = (f: i64[], bytes: u8[]): void => {
520
520
  let acc: i64 = 0
521
+ // Never above 33. The masks are no-ops on such a count and on a width of 25
522
+ // or 26; they let the range prover bound every sum and difference.
521
523
  let bits: i64 = 0
522
524
  let limb: i32 = 0
523
525
  for (let i: i32 = 0; i < 32; i++) {
@@ -526,13 +528,13 @@ const f25519DecodeInto = (f: i64[], bytes: u8[]): void => {
526
528
  b = b & toI64(0x7f)
527
529
  }
528
530
  acc = acc | (b << bits)
529
- bits = bits + toI64(8)
531
+ bits = (bits & toI64(0x3f)) + toI64(8)
530
532
  if (limb >= 0 && limb < 10) {
531
533
  const width: i64 = f25519Width(limb)
532
534
  if (bits >= width) {
533
535
  f[limb] = acc & f25519LimbMask(limb)
534
536
  acc = acc >> width
535
- bits = bits - width
537
+ bits = (bits & toI64(0x3f)) - (width & toI64(0x1f))
536
538
  limb = limb + 1
537
539
  }
538
540
  }
@@ -582,15 +584,16 @@ const f25519EncodeInto = (out: u8[], f: i64[]): void => {
582
584
  h[9] = h[9] & f25519LimbMask(9)
583
585
 
584
586
  let acc: i64 = 0
587
+ // Below 34, as in `f25519DecodeInto`, whose masks these are.
585
588
  let bits: i64 = 0
586
589
  let at: i32 = 0
587
590
  for (let i: i32 = 0; i < 10; i++) {
588
591
  acc = acc | (h[i] << bits)
589
- bits = bits + f25519Width(i)
592
+ bits = (bits & toI64(0x3f)) + (f25519Width(i) & toI64(0x1f))
590
593
  while (bits >= toI64(8) && at >= 0 && at < 32) {
591
594
  out[at] = toU8(acc & toI64(0xff))
592
595
  acc = acc >> toI64(8)
593
- bits = bits - toI64(8)
596
+ bits = (bits & toI64(0x3f)) - toI64(8)
594
597
  at = at + 1
595
598
  }
596
599
  }
package/std/host/game.ts CHANGED
@@ -148,6 +148,28 @@ export const GAME_SAMPLES = new Float32Array(96000)
148
148
  /** `v` held to `0..most`. */
149
149
  const gameClamp = (v: i32, most: i32): i32 => Math.min(Math.max(v, 0), most)
150
150
 
151
+ /** Word `word` of `info`, which is `GAME_INFO`, held to `0..most`. */
152
+ const gameInfoWord = (info: Int32Array, word: i32, most: i32): i32 => {
153
+ // Never true: `word` is one of the `GAME_INFO_*` indices and `GAME_INFO` is
154
+ // four words for the module's life. The test is what proves the read.
155
+ if (word < 0 || word >= toI32(info.length)) {
156
+ return 0
157
+ }
158
+ return gameClamp(info[word], most)
159
+ }
160
+
161
+ /** Store `a` and `b` in words `at` and `at + 1` of `info`, which is `GAME_INFO`. */
162
+ const gameInfoPair = (info: Int32Array, at: i32, a: i32, b: i32): void => {
163
+ // Never true: `at` is `GAME_INFO_WIDTH` or `GAME_INFO_SAMPLES` and
164
+ // `GAME_INFO` is four words for the module's life. The test is what proves
165
+ // the stores.
166
+ if (at < 0 || at >= toI32(info.length) - 1) {
167
+ return
168
+ }
169
+ info[at] = a
170
+ info[at + 1] = b
171
+ }
172
+
151
173
  /**
152
174
  * Set the frame's size, which the page reads to know how much of `GAME_FRAME`
153
175
  * to show. Each side is held to `0..GAME_MAX_WIDTH` and `0..GAME_MAX_HEIGHT`.
@@ -156,24 +178,27 @@ const gameClamp = (v: i32, most: i32): i32 => Math.min(Math.max(v, 0), most)
156
178
  * @param width - the frame's width in pixels
157
179
  * @param height - the frame's height in pixels
158
180
  */
159
- export const gameSetSize = (width: i32, height: i32): void => {
160
- GAME_INFO[GAME_INFO_WIDTH] = gameClamp(width, GAME_MAX_WIDTH)
161
- GAME_INFO[GAME_INFO_HEIGHT] = gameClamp(height, GAME_MAX_HEIGHT)
162
- }
181
+ export const gameSetSize = (width: i32, height: i32): void =>
182
+ gameInfoPair(
183
+ GAME_INFO,
184
+ GAME_INFO_WIDTH,
185
+ gameClamp(width, GAME_MAX_WIDTH),
186
+ gameClamp(height, GAME_MAX_HEIGHT)
187
+ )
163
188
 
164
189
  /**
165
190
  * The frame's width, as `gameSetSize` set it.
166
191
  *
167
192
  * @returns the width in pixels, held to `0..GAME_MAX_WIDTH` even if the page wrote the word
168
193
  */
169
- export const gameWidth = (): i32 => gameClamp(GAME_INFO[GAME_INFO_WIDTH], GAME_MAX_WIDTH)
194
+ export const gameWidth = (): i32 => gameInfoWord(GAME_INFO, GAME_INFO_WIDTH, GAME_MAX_WIDTH)
170
195
 
171
196
  /**
172
197
  * The frame's height, as `gameSetSize` set it.
173
198
  *
174
199
  * @returns the height in pixels, held to `0..GAME_MAX_HEIGHT` even if the page wrote the word
175
200
  */
176
- export const gameHeight = (): i32 => gameClamp(GAME_INFO[GAME_INFO_HEIGHT], GAME_MAX_HEIGHT)
201
+ export const gameHeight = (): i32 => gameInfoWord(GAME_INFO, GAME_INFO_HEIGHT, GAME_MAX_HEIGHT)
177
202
 
178
203
  /** The next record on `GAME_INPUT` as a `TermEvent`, or null for a kind the reader does not know. */
179
204
  const gameEventOf = (): TermEvent | null => {
@@ -244,6 +269,25 @@ export const gameReadInput = (input: StepInput, nowMs: f64): void => {
244
269
  */
245
270
  export const gamePlaySound = (id: i32): boolean => ringPush(GAME_SOUNDS, id)
246
271
 
272
+ /** Write the first `n` of `samples` into `out`, which is `GAME_SAMPLES`, clipped to `-1..1`. */
273
+ const gameCopySamples = (out: Float32Array, samples: f64[], n: i32): void => {
274
+ // `n` is at most `GAME_SAMPLE_LIMIT`, the length of `GAME_SAMPLES`, and at
275
+ // most `samples.length`: the length tests never end the loop first, and
276
+ // they are what prove the read and the store.
277
+ for (let i: i32 = 0; i < n && i < toI32(samples.length) && i < toI32(out.length); i++) {
278
+ const s: f64 = samples[i]
279
+ let x: f64 = 0.0
280
+ if (s > 1.0) {
281
+ x = 1.0
282
+ } else if (s < -1.0) {
283
+ x = -1.0
284
+ } else if (s >= -1.0) {
285
+ x = s
286
+ }
287
+ out[i] = toF32(x)
288
+ }
289
+ }
290
+
247
291
  /**
248
292
  * Hand one sound's samples to the page: each is clipped to `-1..1` (NaN is
249
293
  * 0) and written to `GAME_SAMPLES` as an `f32`, and `GAME_INFO` records how
@@ -255,19 +299,7 @@ export const gamePlaySound = (id: i32): boolean => ringPush(GAME_SOUNDS, id)
255
299
  */
256
300
  export const gameWriteSamples = (samples: f64[], sampleRate: i32): i32 => {
257
301
  const n: i32 = Math.min(toI32(samples.length), GAME_SAMPLE_LIMIT)
258
- for (let i: i32 = 0; i < n && i < toI32(samples.length); i++) {
259
- const s: f64 = samples[i]
260
- let x: f64 = 0.0
261
- if (s > 1.0) {
262
- x = 1.0
263
- } else if (s < -1.0) {
264
- x = -1.0
265
- } else if (s >= -1.0) {
266
- x = s
267
- }
268
- GAME_SAMPLES[i] = toF32(x)
269
- }
270
- GAME_INFO[GAME_INFO_SAMPLES] = n
271
- GAME_INFO[GAME_INFO_RATE] = sampleRate
302
+ gameCopySamples(GAME_SAMPLES, samples, n)
303
+ gameInfoPair(GAME_INFO, GAME_INFO_SAMPLES, n, sampleRate)
272
304
  return n
273
305
  }