@amritk/nish-aarch64-linux 0.35.0 → 0.36.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 (119) hide show
  1. package/INSTALL.md +6 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +9 -2
  4. package/package.json +1 -1
  5. package/runtime/nish.d.ts +4612 -430
  6. package/runtime/vite.d.ts +30 -0
  7. package/runtime/vite.mjs +150 -0
  8. package/scripts/build.sh +108 -13
  9. package/std/base64.ts +34 -5
  10. package/std/bytes.ts +256 -40
  11. package/std/cli-args.ts +296 -70
  12. package/std/cli-flags.ts +184 -19
  13. package/std/cli.ts +475 -54
  14. package/std/collections.ts +121 -23
  15. package/std/compress/checksum.ts +44 -14
  16. package/std/compress/deflate.ts +224 -111
  17. package/std/compress/gzip.ts +217 -53
  18. package/std/compress/zlib.ts +63 -7
  19. package/std/crypto/aes.ts +80 -9
  20. package/std/crypto/base64url.ts +22 -0
  21. package/std/crypto/bcrypt-batch.ts +88 -21
  22. package/std/crypto/bcrypt.ts +60 -0
  23. package/std/crypto/chacha20poly1305.ts +53 -0
  24. package/std/crypto/ct.ts +32 -13
  25. package/std/crypto/ed25519.ts +47 -0
  26. package/std/crypto/hkdf.ts +198 -13
  27. package/std/crypto/hmac.ts +235 -31
  28. package/std/crypto/jwt.ts +192 -63
  29. package/std/crypto/p256.ts +159 -16
  30. package/std/crypto/p384.ts +28 -3
  31. package/std/crypto/pki-roots.ts +32 -2
  32. package/std/crypto/pki.ts +81 -15
  33. package/std/crypto/rsa.ts +33 -3
  34. package/std/crypto/sha1.ts +3 -0
  35. package/std/crypto/sha256.ts +24 -1
  36. package/std/crypto/sha512.ts +99 -14
  37. package/std/crypto/x25519.ts +152 -14
  38. package/std/crypto/x509-random.ts +10 -0
  39. package/std/crypto/x509.ts +67 -7
  40. package/std/json-equal.ts +19 -0
  41. package/std/json-format.ts +112 -13
  42. package/std/json-number.ts +64 -14
  43. package/std/json-pointer.ts +23 -1
  44. package/std/json-tokenizer.ts +100 -29
  45. package/std/json-typed.ts +713 -66
  46. package/std/json-value.ts +400 -124
  47. package/std/json.ts +138 -10
  48. package/std/map.ts +25 -7
  49. package/std/math.ts +36 -4
  50. package/std/net/buffer-pool.ts +42 -11
  51. package/std/net/dns-config.ts +109 -23
  52. package/std/net/dns-message.ts +62 -7
  53. package/std/net/dns-resolver.ts +247 -53
  54. package/std/net/dns.ts +203 -35
  55. package/std/net/fetch.ts +151 -61
  56. package/std/net/handler.ts +278 -80
  57. package/std/net/hpack.ts +262 -52
  58. package/std/net/http-compress.ts +125 -15
  59. package/std/net/http-fields.ts +85 -9
  60. package/std/net/http1-client-codec.ts +100 -19
  61. package/std/net/http1-codec.ts +235 -27
  62. package/std/net/http1-server.ts +343 -85
  63. package/std/net/http2-frame.ts +272 -35
  64. package/std/net/http2-tls.ts +135 -34
  65. package/std/net/http2.ts +247 -106
  66. package/std/net/http3-frame.ts +154 -17
  67. package/std/net/http3-server.ts +246 -96
  68. package/std/net/http3.ts +307 -115
  69. package/std/net/outbound.ts +37 -3
  70. package/std/net/postgres-carrier.ts +144 -8
  71. package/std/net/postgres.ts +723 -103
  72. package/std/net/prefork.ts +164 -31
  73. package/std/net/qpack.ts +105 -18
  74. package/std/net/quic-conn-ack.ts +59 -12
  75. package/std/net/quic-conn-cid.ts +124 -27
  76. package/std/net/quic-conn-params.ts +70 -10
  77. package/std/net/quic-datagram.ts +43 -3
  78. package/std/net/quic-frame.ts +286 -12
  79. package/std/net/quic-listener.ts +101 -32
  80. package/std/net/quic-packet.ts +302 -21
  81. package/std/net/quic-recovery.ts +228 -44
  82. package/std/net/quic-stream.ts +353 -91
  83. package/std/net/quic.ts +489 -171
  84. package/std/net/router.ts +87 -40
  85. package/std/net/tls/client.ts +166 -51
  86. package/std/net/tls/codec.ts +413 -56
  87. package/std/net/tls/record-client.ts +110 -55
  88. package/std/net/tls/record-server.ts +159 -59
  89. package/std/net/tls/record.ts +109 -12
  90. package/std/net/tls/schedule.ts +235 -26
  91. package/std/net/tls/ticket.ts +159 -40
  92. package/std/net/tls/verify.ts +49 -3
  93. package/std/net/tls-tcp.ts +250 -45
  94. package/std/net/tls.ts +221 -59
  95. package/std/net/upstream.ts +81 -45
  96. package/std/net/url.ts +97 -13
  97. package/std/net/websocket.ts +122 -12
  98. package/std/net/webtransport.ts +220 -89
  99. package/std/pair.ts +10 -2
  100. package/std/random.ts +34 -9
  101. package/std/regex.ts +352 -198
  102. package/std/region.ts +70 -15
  103. package/std/secret.ts +52 -11
  104. package/std/sort.ts +17 -2
  105. package/std/sqlite.ts +256 -35
  106. package/std/sync/ring.ts +205 -35
  107. package/std/sync/shm.ts +194 -16
  108. package/std/term-io.ts +137 -64
  109. package/std/term-pixels.ts +144 -19
  110. package/std/term-probe.ts +66 -25
  111. package/std/term-rate.ts +128 -57
  112. package/std/term.ts +332 -121
  113. package/std/testing.ts +130 -32
  114. package/std/text.ts +73 -7
  115. package/std/threads.ts +229 -41
  116. package/std/yaml-json.ts +94 -82
  117. package/std/yaml-lines.ts +47 -15
  118. package/std/yaml-write.ts +24 -16
  119. package/std/yaml.ts +764 -384
@@ -100,6 +100,9 @@ const sha1Compress = (h: u32[], w: u32[], src: u8[], at: i32): void => {
100
100
  * An array longer than 2^31 - 1 bytes panics, for the reason `sha256` gives:
101
101
  * under `--number-mode f64`, `toI32` of a longer length saturates, and the
102
102
  * answer would be the digest of a prefix.
103
+ *
104
+ * @param data The whole message; it is read, not changed.
105
+ * @returns A fresh 20-byte digest.
103
106
  */
104
107
  export const sha1 = (data: u8[]): u8[] => {
105
108
  if (data.length > 2147483647) {
@@ -435,6 +435,8 @@ export class Sha256 {
435
435
  * The message length absorbed so far, in bytes. An `i64` because §5.1.1
436
436
  * appends the length in bits as a 64-bit number, and a transcript can pass
437
437
  * 2^31 bytes where an `i32` would wrap.
438
+ *
439
+ * @internal Set by `nish/crypto/hmac` and `nish/net/postgres` when they wipe, copy or restart a hasher.
438
440
  */
439
441
  total: i64 = 0
440
442
  /** H_0 .. H_7, the eight words of the intermediate hash value (§6.2.2 step 4). */
@@ -447,10 +449,19 @@ export class Sha256 {
447
449
  * it once rather than once per block.
448
450
  */
449
451
  schedule: u32[]
450
- /** How many bytes of `block` are live, always below `SHA256_BLOCK` between calls. */
452
+ /**
453
+ * How many bytes of `block` are live, always below `SHA256_BLOCK` between calls.
454
+ *
455
+ * @internal Set by `nish/crypto/hmac` and `nish/net/postgres` when they wipe, copy or restart a hasher.
456
+ */
451
457
  fill: i32 = 0
458
+ /** @internal Set by `nish/crypto/hmac` and `nish/net/postgres` when they wipe, copy or restart a hasher. */
452
459
  finished: boolean = false
453
460
 
461
+ /**
462
+ * A hasher at the start of an empty message (FIPS 180-4 §5.3.3's initial hash
463
+ * value).
464
+ */
454
465
  constructor() {
455
466
  // H^(0) of FIPS 180-4 §5.3.3: the first 32 bits of the fractional parts
456
467
  // of the square roots of the first eight primes.
@@ -472,6 +483,10 @@ export class Sha256 {
472
483
  * Absorbs `data[off .. off + len)`. A window outside `data` panics rather
473
484
  * than hashing a short read, since a digest over the wrong bytes looks
474
485
  * exactly like a digest over the right ones.
486
+ *
487
+ * @param data The buffer holding the window; it is read, not kept.
488
+ * @param off Where the window starts in `data`.
489
+ * @param len How many bytes to absorb; 0 absorbs nothing.
475
490
  */
476
491
  update(data: u8[], off: i32, len: i32): void {
477
492
  if (this.finished) {
@@ -513,6 +528,9 @@ export class Sha256 {
513
528
  * An independent hasher at the same point in the same message: updating or
514
529
  * digesting either one leaves the other as it was. This is how a caller
515
530
  * takes the hash of a prefix and keeps going.
531
+ *
532
+ * @returns A new hasher with its own state, block and schedule; it shares no
533
+ * array with this one.
516
534
  */
517
535
  copy(): Sha256 {
518
536
  if (this.finished) {
@@ -539,6 +557,8 @@ export class Sha256 {
539
557
  /**
540
558
  * The 32-byte message digest (FIPS 180-4 §6.2.2, the final H^(N) in
541
559
  * big-endian order), in a fresh array. Ends the computation.
560
+ *
561
+ * @returns A fresh 32-byte digest, which the caller owns.
542
562
  */
543
563
  digest(): u8[] {
544
564
  if (this.finished) {
@@ -590,6 +610,9 @@ export class Sha256 {
590
610
  * hashing `toI32(data.length)` bytes would answer the digest of a prefix: one
591
611
  * digest for every message that shares its first 2^31 - 1 bytes. The length
592
612
  * is compared as the `number` it is, before any `toI32`.
613
+ *
614
+ * @param data The whole message; it is read, not changed.
615
+ * @returns A fresh 32-byte digest.
593
616
  */
594
617
  export const sha256 = (data: u8[]): u8[] => {
595
618
  if (data.length > 2147483647) {
@@ -1,17 +1,28 @@
1
1
  /**
2
2
  * `nish/crypto/sha512` — SHA-512 and SHA-384, written from FIPS 180-4.
3
3
  *
4
- * Both hashes are one compression function over eight 64-bit words: SHA-384
5
- * starts from its own initial value (§5.3.4) and keeps the first 48 bytes of
6
- * the result (§6.5), SHA-512 starts from §5.3.5 and keeps all 64. So one
7
- * engine does the work, and `Sha512` and `Sha384` are the two ways to start it.
4
+ * import { sha512, sha384, Sha512 } from "nish/crypto/sha512";
8
5
  *
9
- * A hasher streams: `update` any number of windows, `copy` to take an
10
- * independent hasher at the same point (TLS 1.3 hashes its transcript and
11
- * reads intermediate hashes of it), then `digest` once. The work depends on how
12
- * many bytes arrive and never on their values: there is no branch and no table
13
- * index on message data, so a secret message (an HMAC key, a transcript) is
14
- * hashed in time that says nothing about it but its length.
6
+ * const whole: u8[] = sha512(message); // 64 bytes; sha384 answers 48
7
+ *
8
+ * const h = new Sha512();
9
+ * h.update(first, 0, toI32(first.length));
10
+ * h.update(second, 0, toI32(second.length));
11
+ * const all: u8[] = h.digest();
12
+ *
13
+ * **One engine, two hashes.** Both hashes are one compression function over
14
+ * eight 64-bit words: SHA-384 starts from its own initial value (§5.3.4) and
15
+ * keeps the first 48 bytes of the result (§6.5), SHA-512 starts from §5.3.5 and
16
+ * keeps all 64. So one engine does the work, and `Sha512` and `Sha384` are the
17
+ * two ways to start it.
18
+ *
19
+ * **Time says only the length.** A hasher streams: `update` any number of
20
+ * windows, `copy` to take an independent hasher at the same point (TLS 1.3
21
+ * hashes its transcript and reads intermediate hashes of it), then `digest`
22
+ * once. The work depends on how many bytes arrive and never on their values:
23
+ * there is no branch and no table index on message data, so a secret message
24
+ * (an HMAC key, a transcript) is hashed in time that says nothing about it but
25
+ * its length.
15
26
  *
16
27
  * The constants are the fractional parts of the cube roots of the first eighty
17
28
  * primes (§4.2.3) and of the square roots of the first sixteen (§5.3.4, §5.3.5),
@@ -276,8 +287,11 @@ class Sha512Engine {
276
287
  block: u8[]
277
288
  /** The message schedule W_0 .. W_79, kept so a block allocates nothing. */
278
289
  schedule: u64[]
290
+ /** @internal Set by `nish/crypto/hmac` when it wipes, copies or restarts a hasher. */
279
291
  count: u64 = 0
292
+ /** @internal Set by `nish/crypto/hmac` when it wipes, copies or restarts a hasher. */
280
293
  filled: i32 = 0
294
+ /** @internal Set by `nish/crypto/hmac` when it wipes, copies or restarts a hasher. */
281
295
  finished: boolean = false
282
296
 
283
297
  constructor(initial: u64[]) {
@@ -287,7 +301,7 @@ class Sha512Engine {
287
301
  }
288
302
 
289
303
  /** One application of §6.4.2 to the block at `src[off]` .. `src[off + 127]`. */
290
- compress(src: u8[], off: i32): void {
304
+ private compress(src: u8[], off: i32): void {
291
305
  const w = this.schedule
292
306
  const h = this.state
293
307
  const wLen: i32 = toI32(w.length)
@@ -358,6 +372,7 @@ class Sha512Engine {
358
372
  h[7] = h[7] + hh
359
373
  }
360
374
 
375
+ /** @internal Called by `Sha512` and `Sha384`, which hold the engine. */
361
376
  absorb(data: u8[], off: i32, len: i32): void {
362
377
  if (this.finished) {
363
378
  panic("sha512: update after digest")
@@ -397,6 +412,8 @@ class Sha512Engine {
397
412
  /**
398
413
  * Makes `twin` this engine at the same point. `twin` is a fresh engine of the
399
414
  * same hash, so its arrays are reused rather than allocated a second time.
415
+ *
416
+ * @internal Called by `Sha512` and `Sha384`, which hold the engine.
400
417
  */
401
418
  copyInto(twin: Sha512Engine): void {
402
419
  for (let i: i32 = 0; i < 8; i++) {
@@ -410,7 +427,11 @@ class Sha512Engine {
410
427
  twin.finished = this.finished
411
428
  }
412
429
 
413
- /** Pads (§5.1.2), compresses what is left, and answers the first `size` bytes of H(N) (§6.4.2, §6.5). */
430
+ /**
431
+ * Pads (§5.1.2), compresses what is left, and answers the first `size` bytes of H(N) (§6.4.2, §6.5).
432
+ *
433
+ * @internal Called by `Sha512` and `Sha384`, which hold the engine.
434
+ */
414
435
  finish(size: i32): u8[] {
415
436
  if (this.finished) {
416
437
  panic("sha512: digest taken twice")
@@ -446,23 +467,52 @@ class Sha512Engine {
446
467
  * another `update` or `digest` panics.
447
468
  */
448
469
  export class Sha512 {
470
+ /**
471
+ * The engine the hash runs on. `nish/crypto/hmac` and `nish/crypto/ed25519`
472
+ * reach into it to wipe a hasher, and the tests to check the wipe; a caller
473
+ * has no use for it.
474
+ */
449
475
  engine: Sha512Engine
450
476
 
477
+ /**
478
+ * A hasher at the start of an empty message (FIPS 180-4 §5.3.5's initial hash
479
+ * value).
480
+ */
451
481
  constructor() {
452
482
  this.engine = new Sha512Engine(sha512Initial())
453
483
  }
454
484
 
485
+ /**
486
+ * Absorbs `data[off .. off + len)`. A window outside `data`, or an update
487
+ * after `digest`, panics.
488
+ *
489
+ * @param data The buffer holding the window; it is read, not kept.
490
+ * @param off Where the window starts in `data`.
491
+ * @param len How many bytes to absorb; 0 absorbs nothing.
492
+ */
455
493
  update(data: u8[], off: i32, len: i32): void {
456
494
  this.engine.absorb(data, off, len)
457
495
  }
458
496
 
459
- /** An independent hasher at the same point: updating either leaves the other alone. */
497
+ /**
498
+ * An independent hasher at the same point: updating either leaves the other
499
+ * alone.
500
+ *
501
+ * @returns A new hasher with its own engine; it shares no array with this
502
+ * one.
503
+ */
460
504
  copy(): Sha512 {
461
505
  const twin = new Sha512()
462
506
  this.engine.copyInto(twin.engine)
463
507
  return twin
464
508
  }
465
509
 
510
+ /**
511
+ * Pads the message (§5.1.2) and answers its digest. Ends the computation: a
512
+ * second `digest` panics.
513
+ *
514
+ * @returns A fresh 64-byte digest, which the caller owns.
515
+ */
466
516
  digest(): u8[] {
467
517
  return this.engine.finish(SHA512_SIZE)
468
518
  }
@@ -473,23 +523,52 @@ export class Sha512 {
473
523
  * fresh 48-byte array.
474
524
  */
475
525
  export class Sha384 {
526
+ /**
527
+ * The engine the hash runs on. `nish/crypto/hmac` and `nish/crypto/ed25519`
528
+ * reach into it to wipe a hasher, and the tests to check the wipe; a caller
529
+ * has no use for it.
530
+ */
476
531
  engine: Sha512Engine
477
532
 
533
+ /**
534
+ * A hasher at the start of an empty message (FIPS 180-4 §5.3.4's initial hash
535
+ * value).
536
+ */
478
537
  constructor() {
479
538
  this.engine = new Sha512Engine(sha384Initial())
480
539
  }
481
540
 
541
+ /**
542
+ * Absorbs `data[off .. off + len)`. A window outside `data`, or an update
543
+ * after `digest`, panics.
544
+ *
545
+ * @param data The buffer holding the window; it is read, not kept.
546
+ * @param off Where the window starts in `data`.
547
+ * @param len How many bytes to absorb; 0 absorbs nothing.
548
+ */
482
549
  update(data: u8[], off: i32, len: i32): void {
483
550
  this.engine.absorb(data, off, len)
484
551
  }
485
552
 
486
- /** An independent hasher at the same point: updating either leaves the other alone. */
553
+ /**
554
+ * An independent hasher at the same point: updating either leaves the other
555
+ * alone.
556
+ *
557
+ * @returns A new hasher with its own engine; it shares no array with this
558
+ * one.
559
+ */
487
560
  copy(): Sha384 {
488
561
  const twin = new Sha384()
489
562
  this.engine.copyInto(twin.engine)
490
563
  return twin
491
564
  }
492
565
 
566
+ /**
567
+ * Pads the message (§5.1.2) and answers its digest, the first 48 bytes of the
568
+ * final hash value (§6.5). Ends the computation: a second `digest` panics.
569
+ *
570
+ * @returns A fresh 48-byte digest, which the caller owns.
571
+ */
493
572
  digest(): u8[] {
494
573
  return this.engine.finish(SHA384_SIZE)
495
574
  }
@@ -500,6 +579,9 @@ export class Sha384 {
500
579
  * longer than 2^31 - 1 bytes panics, for the reason `sha256` gives: a longer
501
580
  * length does not survive `toI32` under `--number-mode f64`, and the digest
502
581
  * would be of a prefix.
582
+ *
583
+ * @param data The whole message; it is read, not changed.
584
+ * @returns A fresh 64-byte digest.
503
585
  */
504
586
  export const sha512 = (data: u8[]): u8[] => {
505
587
  if (data.length > 2147483647) {
@@ -513,6 +595,9 @@ export const sha512 = (data: u8[]): u8[] => {
513
595
  /**
514
596
  * The SHA-384 digest of all of `data`, as a fresh 48-byte array. An array
515
597
  * longer than 2^31 - 1 bytes panics, as in `sha512`.
598
+ *
599
+ * @param data The whole message; it is read, not changed.
600
+ * @returns A fresh 48-byte digest.
516
601
  */
517
602
  export const sha384 = (data: u8[]): u8[] => {
518
603
  if (data.length > 2147483647) {
@@ -1,6 +1,26 @@
1
1
  /**
2
2
  * `nish/crypto/x25519` — the X25519 Diffie-Hellman function of RFC 7748.
3
3
  *
4
+ * import { X25519_SIZE, x25519, x25519Base } from "nish/crypto/x25519";
5
+ * import { timingSafeEqual } from "nish/crypto/ct";
6
+ * import { Secret, expose, secret, wipe } from "nish:secret";
7
+ *
8
+ * const isZero = (s: u8[]): boolean => timingSafeEqual(s, new Array<u8>(X25519_SIZE))
9
+ *
10
+ * const raw: u8[] = new Array<u8>(X25519_SIZE)
11
+ * crypto.getRandomValues(raw) // any 32 bytes are a private key
12
+ * const mine: Secret<u8[]> = secret(raw)
13
+ * const myPublic: u8[] | null = x25519Base(mine) // 32 bytes, sent to the peer
14
+ * const peerPublic: u8[] = new Array<u8>(X25519_SIZE) // the peer's 32 bytes, received
15
+ * const shared: Secret<u8[]> | null = x25519(mine, peerPublic)
16
+ * wipe(mine)
17
+ * if (shared !== null) {
18
+ * if (!expose(shared, isZero)) { // a low-order peer key gives zero
19
+ * // derive keys from `shared` inside `expose`
20
+ * }
21
+ * wipe(shared)
22
+ * }
23
+ *
4
24
  * Written from the specification (RFC 7748 §4.1, §5 and §6.1), in this
5
25
  * module's own structure: nothing here is ported from another implementation.
6
26
  * The radix-2^25.5 limbs and the shape of the inversion chain are the ideas
@@ -84,14 +104,39 @@ export const X25519_SIZE: i32 = 32
84
104
  const X25519_A24: i64 = 121665
85
105
 
86
106
  /**
87
- * `a + b`, `a - b` and `a * b` on limbs: the `u64` arithmetic, read back as
88
- * `i64`. Every bound this module states keeps the true result inside `i64`,
89
- * so the answer is the signed one bit for bit; the arithmetic is unsigned
90
- * because signed arithmetic is checked for an overflow, and a check is a
91
- * branch on a secret limb. Each is one instruction once LLVM inlines it.
107
+ * `a + b` on limbs: the `u64` sum, read back as `i64`; `f25519Sub64` and
108
+ * `f25519Mul64` are its difference and product. Every bound this module states
109
+ * keeps the true result inside `i64`, so the answer is the signed one bit for
110
+ * bit; the arithmetic is unsigned because signed arithmetic is checked for an
111
+ * overflow, and a check is a branch on a secret limb. Each is one
112
+ * instruction once LLVM inlines it.
113
+ *
114
+ * @param a The first limb.
115
+ * @param b The second limb.
116
+ * @returns The sum, which the caller's bounds keep inside `i64`.
117
+ *
118
+ * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
92
119
  */
93
120
  export const f25519Add64 = (a: i64, b: i64): i64 => toI64(toU64(a) + toU64(b))
121
+ /**
122
+ * `a - b` on limbs, unsigned and read back as `i64`, as `f25519Add64` explains.
123
+ *
124
+ * @param a The limb subtracted from.
125
+ * @param b The limb subtracted.
126
+ * @returns The difference, which the caller's bounds keep inside `i64`.
127
+ *
128
+ * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
129
+ */
94
130
  export const f25519Sub64 = (a: i64, b: i64): i64 => toI64(toU64(a) - toU64(b))
131
+ /**
132
+ * `a * b` on limbs, unsigned and read back as `i64`, as `f25519Add64` explains.
133
+ *
134
+ * @param a The first limb.
135
+ * @param b The second limb.
136
+ * @returns The product, which the caller's bounds keep inside `i64`.
137
+ *
138
+ * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
139
+ */
95
140
  export const f25519Mul64 = (a: i64, b: i64): i64 => toI64(toU64(a) * toU64(b))
96
141
 
97
142
  /** The width of limb `i`: 26 bits for an even limb, 25 for an odd one. */
@@ -103,20 +148,40 @@ const f25519Width = (i: i32): i64 => {
103
148
  /** The mask of limb `i`'s bits, `2^width - 1`. */
104
149
  const f25519LimbMask = (i: i32): i64 => f25519Sub64(toI64(1) << f25519Width(i), toI64(1))
105
150
 
106
- /** A fresh field element, zero. */
151
+ /**
152
+ * A fresh field element, zero.
153
+ *
154
+ * @returns A new ten-limb element, every limb zero.
155
+ *
156
+ * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
157
+ */
107
158
  export const f25519Zero = (): i64[] => {
108
159
  const f: i64[] = new Array<i64>(10)
109
160
  return f
110
161
  }
111
162
 
112
- /** A fresh field element holding the small constant `n`. */
163
+ /**
164
+ * A fresh field element holding the small constant `n`.
165
+ *
166
+ * @param n The constant, held in limb 0.
167
+ * @returns A new element whose value is `n`.
168
+ *
169
+ * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
170
+ */
113
171
  export const f25519Small = (n: i64): i64[] => {
114
172
  const f: i64[] = f25519Zero()
115
173
  f[0] = n
116
174
  return f
117
175
  }
118
176
 
119
- /** `out = f`, limb by limb. */
177
+ /**
178
+ * `out = f`, limb by limb.
179
+ *
180
+ * @param out Where the limbs go.
181
+ * @param f The element copied.
182
+ *
183
+ * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
184
+ */
120
185
  export const f25519Copy = (out: i64[], f: i64[]): void => {
121
186
  if (toI32(out.length) < 10 || toI32(f.length) < 10) {
122
187
  return
@@ -157,6 +222,10 @@ const f25519CarryChain = (h: i64[]): void => {
157
222
  * Each carry is `h >> width` (arithmetic, so a floor) and each remainder is
158
223
  * `h & (2^width - 1)`, which is the matching non-negative remainder for a
159
224
  * negative limb too, without shifting a negative number left.
225
+ *
226
+ * @param h The element, carried in place.
227
+ *
228
+ * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
160
229
  */
161
230
  export const f25519Carry = (h: i64[]): void => {
162
231
  if (toI32(h.length) < 10) {
@@ -174,6 +243,12 @@ export const f25519Carry = (h: i64[]): void => {
174
243
  /**
175
244
  * `out = f + g`, not carried. With both reduced (limbs below 2^26 in
176
245
  * magnitude) the sum's limbs are below 2^27, which `f25519Mul` accepts.
246
+ *
247
+ * @param out Where the sum goes; it may be `f` or `g`.
248
+ * @param f The first addend, reduced.
249
+ * @param g The second addend, reduced.
250
+ *
251
+ * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
177
252
  */
178
253
  export const f25519Add = (out: i64[], f: i64[], g: i64[]): void => {
179
254
  if (toI32(out.length) < 10 || toI32(f.length) < 10 || toI32(g.length) < 10) {
@@ -188,6 +263,12 @@ export const f25519Add = (out: i64[], f: i64[], g: i64[]): void => {
188
263
  * `out = f - g`, not carried. Signed limbs need no bias to stay positive; with
189
264
  * both reduced the difference's limbs are below 2^27 in magnitude, which
190
265
  * `f25519Mul` accepts.
266
+ *
267
+ * @param out Where the difference goes; it may be `f` or `g`.
268
+ * @param f The element subtracted from, reduced.
269
+ * @param g The element subtracted, reduced.
270
+ *
271
+ * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
191
272
  */
192
273
  export const f25519Sub = (out: i64[], f: i64[], g: i64[]): void => {
193
274
  if (toI32(out.length) < 10 || toI32(f.length) < 10 || toI32(g.length) < 10) {
@@ -213,6 +294,12 @@ export const f25519Sub = (out: i64[], f: i64[], g: i64[]): void => {
213
294
  * own, so no intermediate is larger. Giving `f25519Mul` anything wider — two unreduced
214
295
  * sums added again, say — breaks this bound, which is why every caller carries
215
296
  * or multiplies before it adds a second time.
297
+ *
298
+ * @param out Where the product goes; it may be `f` or `g`.
299
+ * @param f The first factor.
300
+ * @param g The second factor.
301
+ *
302
+ * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
216
303
  */
217
304
  export const f25519Mul = (out: i64[], f: i64[], g: i64[]): void => {
218
305
  if (toI32(f.length) < 10 || toI32(g.length) < 10) {
@@ -245,12 +332,27 @@ export const f25519Mul = (out: i64[], f: i64[], g: i64[]): void => {
245
332
  f25519Copy(out, h)
246
333
  }
247
334
 
248
- /** `out = f * f`, reduced; the same bounds as `f25519Mul`. */
335
+ /**
336
+ * `out = f * f`, reduced; the same bounds as `f25519Mul`.
337
+ *
338
+ * @param out Where the square goes; it may be `f`.
339
+ * @param f The element squared.
340
+ *
341
+ * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
342
+ */
249
343
  export const f25519Square = (out: i64[], f: i64[]): void => {
250
344
  f25519Mul(out, f, f)
251
345
  }
252
346
 
253
- /** `out = f` squared `n` times in a row (`n` at least 1), reduced. */
347
+ /**
348
+ * `out = f` squared `n` times in a row (`n` at least 1), reduced.
349
+ *
350
+ * @param out Where the result goes; it may be `f`.
351
+ * @param f The element squared.
352
+ * @param n How many squarings.
353
+ *
354
+ * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
355
+ */
254
356
  export const f25519SquareTimes = (out: i64[], f: i64[], n: i32): void => {
255
357
  f25519Square(out, f)
256
358
  for (let i: i32 = 1; i < n; i++) {
@@ -280,6 +382,12 @@ const f25519MulA24 = (out: i64[], f: i64[]): void => {
280
382
  * 5, 10, 20, 40, 50, 100, 200 and 250 from the rule
281
383
  * `z^(2^(a+b) - 1) = (z^(2^a - 1))^(2^b) * z^(2^b - 1)`: 251 squarings and 11
282
384
  * multiplications, the same for every `z`. `out` and `z11` may not be `z`.
385
+ *
386
+ * @param out Where `z^(2^250 - 1)` goes.
387
+ * @param z11 Where `z^11` goes.
388
+ * @param z The base, reduced.
389
+ *
390
+ * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
283
391
  */
284
392
  export const f25519Pow250 = (out: i64[], z11: i64[], z: i64[]): void => {
285
393
  const z2: i64[] = f25519Zero()
@@ -322,6 +430,11 @@ export const f25519Pow250 = (out: i64[], z11: i64[], z: i64[]): void => {
322
430
  * followed by `01011`: `f25519Pow250`, then five squarings and a
323
431
  * multiplication by `z^11` — 256 squarings and 12 multiplications, the same
324
432
  * for every `z`.
433
+ *
434
+ * @param out Where the inverse goes.
435
+ * @param z The element inverted, reduced.
436
+ *
437
+ * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
325
438
  */
326
439
  export const f25519Invert = (out: i64[], z: i64[]): void => {
327
440
  const z11: i64[] = f25519Zero()
@@ -336,6 +449,12 @@ export const f25519Invert = (out: i64[], z: i64[]): void => {
336
449
  * Swaps `f` and `g` when `bit` is 1 and leaves them when it is 0, with the
337
450
  * same instructions either way: the mask is all ones or all zeros, and the
338
451
  * swap is the masked xor of the two.
452
+ *
453
+ * @param f The first element, swapped in place.
454
+ * @param g The second element, swapped in place.
455
+ * @param bit 1 to swap, 0 to leave; a secret, so no branch reads it.
456
+ *
457
+ * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
339
458
  */
340
459
  export const f25519Swap = (f: i64[], g: i64[], bit: i64): void => {
341
460
  if (toI32(f.length) < 10 || toI32(g.length) < 10) {
@@ -357,6 +476,11 @@ export const f25519Swap = (f: i64[], g: i64[], bit: i64): void => {
357
476
  * lands in `[0, 2^width)` and the element is reduced. A value in
358
477
  * `[p, 2^255)` is neither refused nor adjusted: §5 has it processed as its
359
478
  * residue, and the arithmetic does that by itself.
479
+ *
480
+ * @param bytes The 32-byte little-endian encoding.
481
+ * @returns A new reduced element, or zero for fewer than 32 bytes.
482
+ *
483
+ * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
360
484
  */
361
485
  export const f25519Decode = (bytes: u8[]): i64[] => {
362
486
  const f: i64[] = new Array<i64>(10)
@@ -398,6 +522,11 @@ export const f25519Decode = (bytes: u8[]): i64[] => {
398
522
  * carry out of bit 255 tells the two apart without a branch — it is 1 exactly
399
523
  * when the value is at least p — so the element adds `19 * q` and drops bit
400
524
  * 255, which is subtracting `p * q`.
525
+ *
526
+ * @param f The element, reduced.
527
+ * @returns Its 32 canonical little-endian bytes.
528
+ *
529
+ * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
401
530
  */
402
531
  export const f25519Encode = (f: i64[]): u8[] => {
403
532
  const h: i64[] = new Array<i64>(10)
@@ -554,10 +683,16 @@ const x25519Exposed = (scalar: u8[], u: u8[]): u8[] | null => {
554
683
  *
555
684
  * Answers `null` unless both arguments are exactly `X25519_SIZE` bytes. An
556
685
  * all-zero answer is **returned, not refused**: it is what a low-order `u`
557
- * gives, and RFC 7748 §6.1 leaves checking for it to the protocol. Nothing in
558
- * this tree makes that check for you yet (TLS 1.3, WP34 T1, is planned to), so
559
- * every caller doing a key exchange must refuse an all-zero answer itself —
560
- * inside `expose`, with `timingSafeEqual` against 32 zero bytes.
686
+ * gives, and RFC 7748 §6.1 leaves checking for it to the protocol.
687
+ * `nish/net/tls` makes that check on both sides of its handshake; any other
688
+ * caller doing a key exchange must refuse an all-zero answer itself — inside
689
+ * `expose`, with `timingSafeEqual` against 32 zero bytes, as the module's
690
+ * example does.
691
+ *
692
+ * @param scalar The private key, 32 bytes, clamped on a copy.
693
+ * @param u The peer's u-coordinate (its public key), 32 bytes.
694
+ * @returns The shared secret, 32 bytes and possibly all zero, or `null` for an
695
+ * argument of the wrong length.
561
696
  */
562
697
  export const x25519 = (scalar: Secret<u8[]>, u: u8[]): Secret<u8[]> | null => {
563
698
  const shared: u8[] | null = exposeWith(scalar, u, x25519Exposed)
@@ -571,6 +706,9 @@ export const x25519 = (scalar: Secret<u8[]>, u: u8[]): Secret<u8[]> | null => {
571
706
  * `x25519(scalar, 9)`: the public key for the private key `scalar`, since 9 is
572
707
  * curve25519's base point (RFC 7748 §4.1, §6.1), and public, so plain bytes.
573
708
  * Answers `null` unless `scalar` is `X25519_SIZE` bytes.
709
+ *
710
+ * @param scalar The private key, 32 bytes.
711
+ * @returns The public key, 32 bytes, or `null` for a key of the wrong length.
574
712
  */
575
713
  export const x25519Base = (scalar: Secret<u8[]>): u8[] | null => {
576
714
  const base: u8[] = new Array<u8>(32)
@@ -29,6 +29,8 @@ import { X509_MAX_SERIAL, x509MintSelfSigned } from "nish/crypto/x509"
29
29
  * so every key is equally likely; a draw is refused with odds below 2^-32. A
30
30
  * refused draw is wiped before the next, and the caller wipes the one
31
31
  * returned.
32
+ *
33
+ * @returns The 32-byte scalar, big-endian, in a new `Secret` the caller owns and wipes.
32
34
  */
33
35
  export const x509DrawP256Key = (): Secret<u8[]> => {
34
36
  while (true) {
@@ -48,6 +50,8 @@ export const x509DrawP256Key = (): Secret<u8[]> => {
48
50
  * so the INTEGER is positive, never zero, and always twenty octets long — 158
49
51
  * random bits, past the 64 the CA/Browser Forum asks of a serial. A serial is
50
52
  * public, written into the certificate, so it is a plain array.
53
+ *
54
+ * @returns The 20 serial octets, big-endian, in a fresh array.
51
55
  */
52
56
  export const x509DrawSerial = (): u8[] => {
53
57
  const serial: u8[] = new Array<u8>(X509_MAX_SERIAL)
@@ -62,6 +66,12 @@ export const x509DrawSerial = (): u8[] => {
62
66
  * the mint answers `null` for. The key stays the caller's, since the server
63
67
  * that presents the certificate signs its handshakes with it; draw one with
64
68
  * `x509DrawP256Key`.
69
+ *
70
+ * @param priv The 32-byte P-256 private scalar; read, and left for the caller to wipe.
71
+ * @param commonName The subject's and issuer's common name, 1 to 64 bytes of UTF-8 without a NUL.
72
+ * @param notBeforeMs When validity starts, in milliseconds since 1970-01-01 UTC.
73
+ * @param days How long it is valid, 1 to `X509_MAX_DAYS`.
74
+ * @returns The certificate's DER, a fresh array, or `null`.
65
75
  */
66
76
  export const x509MintSelfSignedDrawn = (
67
77
  priv: Secret<u8[]>,