@amritk/nish-x86_64-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
@@ -50,7 +50,7 @@
50
50
  * bits, which Nish has no integer for, so each comes from its own helper:
51
51
  * the sum, difference or low product by wrapping arithmetic, the carry or
52
52
  * borrow from the top bits of the operands and the answer, and the high
53
- * product from four 32-by-32 products (`p256FiatMulxU64Hi`). Either way only
53
+ * product from the `mulHigh` builtin. Either way only
54
54
  * the outputs fiat goes on to read are declared. And each function opens
55
55
  * with a length guard, so that the literal indices after it compile with no
56
56
  * bounds check.
@@ -157,7 +157,7 @@
157
157
  * (`docs/wp26-stdlib.md` §3e).
158
158
  */
159
159
  import { Secret, expose, exposeWith, secret, wipe } from "nish:secret"
160
- import { SHA256_BLOCK, SHA256_SIZE, Sha256, sha256 } from "nish/crypto/sha256"
160
+ import { SHA256_SIZE, Sha256, sha256 } from "nish/crypto/sha256"
161
161
  import { timingSafeEqual } from "nish/crypto/ct"
162
162
 
163
163
  /** The length in bytes of a private key: one scalar, big-endian. */
@@ -169,6 +169,12 @@ export const P256_POINT_SIZE: i32 = 65
169
169
  /** The length in bytes of a signature: `r` then `s`, 32 big-endian bytes each. */
170
170
  export const P256_SIGNATURE_SIZE: i32 = 64
171
171
 
172
+ /**
173
+ * The most RFC 6979 candidates `p256SignSha256Into` draws before it refuses a
174
+ * scratch that cannot sign: one is used up only by a 2^-31 event.
175
+ */
176
+ const P256_SIGN_ROUNDS: i32 = 64
177
+
172
178
  /** A typed zero for the offsets below: a bare literal is an `f64` under `--number-mode f64`. */
173
179
  const P256_FROM: i32 = 0
174
180
 
@@ -231,25 +237,6 @@ const p256FiatSubborrowxU64 = (arg1: u64, arg2: u64, arg3: u64): u64 => arg2 - a
231
237
  const p256FiatSubborrowxU64Out2 = (arg2: u64, arg3: u64, out1: u64): u64 =>
232
238
  ((~arg2 & arg3) | (~(arg2 ^ arg3) & out1)) >> 63
233
239
 
234
- /**
235
- * fiat's `mulx_u64`, its `out2`: the high half of the 128-bit product, from
236
- * four 32-by-32 products, since Nish has no wider integer. The low half,
237
- * fiat's `out1`, is the wrapping `arg1 * arg2`. `mid` is at most
238
- * 3 * (2^32 - 1), so it cannot wrap.
239
- */
240
- const p256FiatMulxU64Hi = (arg1: u64, arg2: u64): u64 => {
241
- const a0: u64 = arg1 & 0xffffffff
242
- const a1: u64 = arg1 >> 32
243
- const b0: u64 = arg2 & 0xffffffff
244
- const b1: u64 = arg2 >> 32
245
- const p00: u64 = a0 * b0
246
- const p01: u64 = a0 * b1
247
- const p10: u64 = a1 * b0
248
- const p11: u64 = a1 * b1
249
- const mid: u64 = (p00 >> 32) + (p01 & 0xffffffff) + (p10 & 0xffffffff)
250
- return p11 + (p01 >> 32) + (p10 >> 32) + (mid >> 32)
251
- }
252
-
253
240
  /** fiat's `cmovznz_u64`: `arg2` when `arg1` is zero and `arg3` otherwise, as `p256FiatCmovznzU32` does. */
254
241
  const p256FiatCmovznzU64 = (arg1: u64, arg2: u64, arg3: u64): u64 => ctSelect(ctEq(arg1, 0), arg2, arg3)
255
242
 
@@ -271,13 +258,13 @@ const p256FiatMul = (out1: u32[], arg1: u32[], arg2: u32[]): void => {
271
258
  const x3: u64 = arg1Limb3
272
259
  const x4: u64 = arg1Limb0
273
260
  const x5: u64 = x4 * arg2Limb3
274
- const x6: u64 = p256FiatMulxU64Hi(x4, arg2Limb3)
261
+ const x6: u64 = mulHigh(x4, arg2Limb3)
275
262
  const x7: u64 = x4 * arg2Limb2
276
- const x8: u64 = p256FiatMulxU64Hi(x4, arg2Limb2)
263
+ const x8: u64 = mulHigh(x4, arg2Limb2)
277
264
  const x9: u64 = x4 * arg2Limb1
278
- const x10: u64 = p256FiatMulxU64Hi(x4, arg2Limb1)
265
+ const x10: u64 = mulHigh(x4, arg2Limb1)
279
266
  const x11: u64 = x4 * arg2Limb0
280
- const x12: u64 = p256FiatMulxU64Hi(x4, arg2Limb0)
267
+ const x12: u64 = mulHigh(x4, arg2Limb0)
281
268
  const x13: u64 = p256FiatAddcarryxU64(0x0, x12, x9)
282
269
  const x14: u64 = p256FiatAddcarryxU64Out2(x12, x9, x13)
283
270
  const x15: u64 = p256FiatAddcarryxU64(x14, x10, x7)
@@ -286,11 +273,11 @@ const p256FiatMul = (out1: u32[], arg1: u32[], arg2: u32[]): void => {
286
273
  const x18: u64 = p256FiatAddcarryxU64Out2(x8, x5, x17)
287
274
  const x19: u64 = x18 + x6
288
275
  const x20: u64 = x11 * p256FiatWord64(0xffffffff, 0x1)
289
- const x21: u64 = p256FiatMulxU64Hi(x11, p256FiatWord64(0xffffffff, 0x1))
276
+ const x21: u64 = mulHigh(x11, p256FiatWord64(0xffffffff, 0x1))
290
277
  const x22: u64 = x11 * 0xffffffff
291
- const x23: u64 = p256FiatMulxU64Hi(x11, 0xffffffff)
278
+ const x23: u64 = mulHigh(x11, 0xffffffff)
292
279
  const x24: u64 = x11 * p256FiatWord64(0xffffffff, 0xffffffff)
293
- const x25: u64 = p256FiatMulxU64Hi(x11, p256FiatWord64(0xffffffff, 0xffffffff))
280
+ const x25: u64 = mulHigh(x11, p256FiatWord64(0xffffffff, 0xffffffff))
294
281
  const x26: u64 = p256FiatAddcarryxU64(0x0, x25, x22)
295
282
  const x27: u64 = p256FiatAddcarryxU64Out2(x25, x22, x26)
296
283
  const x28: u64 = x27 + x23
@@ -305,13 +292,13 @@ const p256FiatMul = (out1: u32[], arg1: u32[], arg2: u32[]): void => {
305
292
  const x37: u64 = p256FiatAddcarryxU64(x36, x19, x21)
306
293
  const x38: u64 = p256FiatAddcarryxU64Out2(x19, x21, x37)
307
294
  const x39: u64 = x1 * arg2Limb3
308
- const x40: u64 = p256FiatMulxU64Hi(x1, arg2Limb3)
295
+ const x40: u64 = mulHigh(x1, arg2Limb3)
309
296
  const x41: u64 = x1 * arg2Limb2
310
- const x42: u64 = p256FiatMulxU64Hi(x1, arg2Limb2)
297
+ const x42: u64 = mulHigh(x1, arg2Limb2)
311
298
  const x43: u64 = x1 * arg2Limb1
312
- const x44: u64 = p256FiatMulxU64Hi(x1, arg2Limb1)
299
+ const x44: u64 = mulHigh(x1, arg2Limb1)
313
300
  const x45: u64 = x1 * arg2Limb0
314
- const x46: u64 = p256FiatMulxU64Hi(x1, arg2Limb0)
301
+ const x46: u64 = mulHigh(x1, arg2Limb0)
315
302
  const x47: u64 = p256FiatAddcarryxU64(0x0, x46, x43)
316
303
  const x48: u64 = p256FiatAddcarryxU64Out2(x46, x43, x47)
317
304
  const x49: u64 = p256FiatAddcarryxU64(x48, x44, x41)
@@ -330,11 +317,11 @@ const p256FiatMul = (out1: u32[], arg1: u32[], arg2: u32[]): void => {
330
317
  const x62: u64 = p256FiatAddcarryxU64(x61, x38, x53)
331
318
  const x63: u64 = p256FiatAddcarryxU64Out2(x38, x53, x62)
332
319
  const x64: u64 = x54 * p256FiatWord64(0xffffffff, 0x1)
333
- const x65: u64 = p256FiatMulxU64Hi(x54, p256FiatWord64(0xffffffff, 0x1))
320
+ const x65: u64 = mulHigh(x54, p256FiatWord64(0xffffffff, 0x1))
334
321
  const x66: u64 = x54 * 0xffffffff
335
- const x67: u64 = p256FiatMulxU64Hi(x54, 0xffffffff)
322
+ const x67: u64 = mulHigh(x54, 0xffffffff)
336
323
  const x68: u64 = x54 * p256FiatWord64(0xffffffff, 0xffffffff)
337
- const x69: u64 = p256FiatMulxU64Hi(x54, p256FiatWord64(0xffffffff, 0xffffffff))
324
+ const x69: u64 = mulHigh(x54, p256FiatWord64(0xffffffff, 0xffffffff))
338
325
  const x70: u64 = p256FiatAddcarryxU64(0x0, x69, x66)
339
326
  const x71: u64 = p256FiatAddcarryxU64Out2(x69, x66, x70)
340
327
  const x72: u64 = x71 + x67
@@ -350,13 +337,13 @@ const p256FiatMul = (out1: u32[], arg1: u32[], arg2: u32[]): void => {
350
337
  const x82: u64 = p256FiatAddcarryxU64Out2(x62, x65, x81)
351
338
  const x83: u64 = x82 + x63
352
339
  const x84: u64 = x2 * arg2Limb3
353
- const x85: u64 = p256FiatMulxU64Hi(x2, arg2Limb3)
340
+ const x85: u64 = mulHigh(x2, arg2Limb3)
354
341
  const x86: u64 = x2 * arg2Limb2
355
- const x87: u64 = p256FiatMulxU64Hi(x2, arg2Limb2)
342
+ const x87: u64 = mulHigh(x2, arg2Limb2)
356
343
  const x88: u64 = x2 * arg2Limb1
357
- const x89: u64 = p256FiatMulxU64Hi(x2, arg2Limb1)
344
+ const x89: u64 = mulHigh(x2, arg2Limb1)
358
345
  const x90: u64 = x2 * arg2Limb0
359
- const x91: u64 = p256FiatMulxU64Hi(x2, arg2Limb0)
346
+ const x91: u64 = mulHigh(x2, arg2Limb0)
360
347
  const x92: u64 = p256FiatAddcarryxU64(0x0, x91, x88)
361
348
  const x93: u64 = p256FiatAddcarryxU64Out2(x91, x88, x92)
362
349
  const x94: u64 = p256FiatAddcarryxU64(x93, x89, x86)
@@ -375,11 +362,11 @@ const p256FiatMul = (out1: u32[], arg1: u32[], arg2: u32[]): void => {
375
362
  const x107: u64 = p256FiatAddcarryxU64(x106, x83, x98)
376
363
  const x108: u64 = p256FiatAddcarryxU64Out2(x83, x98, x107)
377
364
  const x109: u64 = x99 * p256FiatWord64(0xffffffff, 0x1)
378
- const x110: u64 = p256FiatMulxU64Hi(x99, p256FiatWord64(0xffffffff, 0x1))
365
+ const x110: u64 = mulHigh(x99, p256FiatWord64(0xffffffff, 0x1))
379
366
  const x111: u64 = x99 * 0xffffffff
380
- const x112: u64 = p256FiatMulxU64Hi(x99, 0xffffffff)
367
+ const x112: u64 = mulHigh(x99, 0xffffffff)
381
368
  const x113: u64 = x99 * p256FiatWord64(0xffffffff, 0xffffffff)
382
- const x114: u64 = p256FiatMulxU64Hi(x99, p256FiatWord64(0xffffffff, 0xffffffff))
369
+ const x114: u64 = mulHigh(x99, p256FiatWord64(0xffffffff, 0xffffffff))
383
370
  const x115: u64 = p256FiatAddcarryxU64(0x0, x114, x111)
384
371
  const x116: u64 = p256FiatAddcarryxU64Out2(x114, x111, x115)
385
372
  const x117: u64 = x116 + x112
@@ -395,13 +382,13 @@ const p256FiatMul = (out1: u32[], arg1: u32[], arg2: u32[]): void => {
395
382
  const x127: u64 = p256FiatAddcarryxU64Out2(x107, x110, x126)
396
383
  const x128: u64 = x127 + x108
397
384
  const x129: u64 = x3 * arg2Limb3
398
- const x130: u64 = p256FiatMulxU64Hi(x3, arg2Limb3)
385
+ const x130: u64 = mulHigh(x3, arg2Limb3)
399
386
  const x131: u64 = x3 * arg2Limb2
400
- const x132: u64 = p256FiatMulxU64Hi(x3, arg2Limb2)
387
+ const x132: u64 = mulHigh(x3, arg2Limb2)
401
388
  const x133: u64 = x3 * arg2Limb1
402
- const x134: u64 = p256FiatMulxU64Hi(x3, arg2Limb1)
389
+ const x134: u64 = mulHigh(x3, arg2Limb1)
403
390
  const x135: u64 = x3 * arg2Limb0
404
- const x136: u64 = p256FiatMulxU64Hi(x3, arg2Limb0)
391
+ const x136: u64 = mulHigh(x3, arg2Limb0)
405
392
  const x137: u64 = p256FiatAddcarryxU64(0x0, x136, x133)
406
393
  const x138: u64 = p256FiatAddcarryxU64Out2(x136, x133, x137)
407
394
  const x139: u64 = p256FiatAddcarryxU64(x138, x134, x131)
@@ -420,11 +407,11 @@ const p256FiatMul = (out1: u32[], arg1: u32[], arg2: u32[]): void => {
420
407
  const x152: u64 = p256FiatAddcarryxU64(x151, x128, x143)
421
408
  const x153: u64 = p256FiatAddcarryxU64Out2(x128, x143, x152)
422
409
  const x154: u64 = x144 * p256FiatWord64(0xffffffff, 0x1)
423
- const x155: u64 = p256FiatMulxU64Hi(x144, p256FiatWord64(0xffffffff, 0x1))
410
+ const x155: u64 = mulHigh(x144, p256FiatWord64(0xffffffff, 0x1))
424
411
  const x156: u64 = x144 * 0xffffffff
425
- const x157: u64 = p256FiatMulxU64Hi(x144, 0xffffffff)
412
+ const x157: u64 = mulHigh(x144, 0xffffffff)
426
413
  const x158: u64 = x144 * p256FiatWord64(0xffffffff, 0xffffffff)
427
- const x159: u64 = p256FiatMulxU64Hi(x144, p256FiatWord64(0xffffffff, 0xffffffff))
414
+ const x159: u64 = mulHigh(x144, p256FiatWord64(0xffffffff, 0xffffffff))
428
415
  const x160: u64 = p256FiatAddcarryxU64(0x0, x159, x156)
429
416
  const x161: u64 = p256FiatAddcarryxU64Out2(x159, x156, x160)
430
417
  const x162: u64 = x161 + x157
@@ -477,13 +464,13 @@ const p256FiatSquare = (out1: u32[], arg1: u32[]): void => {
477
464
  const x3: u64 = arg1Limb3
478
465
  const x4: u64 = arg1Limb0
479
466
  const x5: u64 = x4 * arg1Limb3
480
- const x6: u64 = p256FiatMulxU64Hi(x4, arg1Limb3)
467
+ const x6: u64 = mulHigh(x4, arg1Limb3)
481
468
  const x7: u64 = x4 * arg1Limb2
482
- const x8: u64 = p256FiatMulxU64Hi(x4, arg1Limb2)
469
+ const x8: u64 = mulHigh(x4, arg1Limb2)
483
470
  const x9: u64 = x4 * arg1Limb1
484
- const x10: u64 = p256FiatMulxU64Hi(x4, arg1Limb1)
471
+ const x10: u64 = mulHigh(x4, arg1Limb1)
485
472
  const x11: u64 = x4 * arg1Limb0
486
- const x12: u64 = p256FiatMulxU64Hi(x4, arg1Limb0)
473
+ const x12: u64 = mulHigh(x4, arg1Limb0)
487
474
  const x13: u64 = p256FiatAddcarryxU64(0x0, x12, x9)
488
475
  const x14: u64 = p256FiatAddcarryxU64Out2(x12, x9, x13)
489
476
  const x15: u64 = p256FiatAddcarryxU64(x14, x10, x7)
@@ -492,11 +479,11 @@ const p256FiatSquare = (out1: u32[], arg1: u32[]): void => {
492
479
  const x18: u64 = p256FiatAddcarryxU64Out2(x8, x5, x17)
493
480
  const x19: u64 = x18 + x6
494
481
  const x20: u64 = x11 * p256FiatWord64(0xffffffff, 0x1)
495
- const x21: u64 = p256FiatMulxU64Hi(x11, p256FiatWord64(0xffffffff, 0x1))
482
+ const x21: u64 = mulHigh(x11, p256FiatWord64(0xffffffff, 0x1))
496
483
  const x22: u64 = x11 * 0xffffffff
497
- const x23: u64 = p256FiatMulxU64Hi(x11, 0xffffffff)
484
+ const x23: u64 = mulHigh(x11, 0xffffffff)
498
485
  const x24: u64 = x11 * p256FiatWord64(0xffffffff, 0xffffffff)
499
- const x25: u64 = p256FiatMulxU64Hi(x11, p256FiatWord64(0xffffffff, 0xffffffff))
486
+ const x25: u64 = mulHigh(x11, p256FiatWord64(0xffffffff, 0xffffffff))
500
487
  const x26: u64 = p256FiatAddcarryxU64(0x0, x25, x22)
501
488
  const x27: u64 = p256FiatAddcarryxU64Out2(x25, x22, x26)
502
489
  const x28: u64 = x27 + x23
@@ -511,13 +498,13 @@ const p256FiatSquare = (out1: u32[], arg1: u32[]): void => {
511
498
  const x37: u64 = p256FiatAddcarryxU64(x36, x19, x21)
512
499
  const x38: u64 = p256FiatAddcarryxU64Out2(x19, x21, x37)
513
500
  const x39: u64 = x1 * arg1Limb3
514
- const x40: u64 = p256FiatMulxU64Hi(x1, arg1Limb3)
501
+ const x40: u64 = mulHigh(x1, arg1Limb3)
515
502
  const x41: u64 = x1 * arg1Limb2
516
- const x42: u64 = p256FiatMulxU64Hi(x1, arg1Limb2)
503
+ const x42: u64 = mulHigh(x1, arg1Limb2)
517
504
  const x43: u64 = x1 * arg1Limb1
518
- const x44: u64 = p256FiatMulxU64Hi(x1, arg1Limb1)
505
+ const x44: u64 = mulHigh(x1, arg1Limb1)
519
506
  const x45: u64 = x1 * arg1Limb0
520
- const x46: u64 = p256FiatMulxU64Hi(x1, arg1Limb0)
507
+ const x46: u64 = mulHigh(x1, arg1Limb0)
521
508
  const x47: u64 = p256FiatAddcarryxU64(0x0, x46, x43)
522
509
  const x48: u64 = p256FiatAddcarryxU64Out2(x46, x43, x47)
523
510
  const x49: u64 = p256FiatAddcarryxU64(x48, x44, x41)
@@ -536,11 +523,11 @@ const p256FiatSquare = (out1: u32[], arg1: u32[]): void => {
536
523
  const x62: u64 = p256FiatAddcarryxU64(x61, x38, x53)
537
524
  const x63: u64 = p256FiatAddcarryxU64Out2(x38, x53, x62)
538
525
  const x64: u64 = x54 * p256FiatWord64(0xffffffff, 0x1)
539
- const x65: u64 = p256FiatMulxU64Hi(x54, p256FiatWord64(0xffffffff, 0x1))
526
+ const x65: u64 = mulHigh(x54, p256FiatWord64(0xffffffff, 0x1))
540
527
  const x66: u64 = x54 * 0xffffffff
541
- const x67: u64 = p256FiatMulxU64Hi(x54, 0xffffffff)
528
+ const x67: u64 = mulHigh(x54, 0xffffffff)
542
529
  const x68: u64 = x54 * p256FiatWord64(0xffffffff, 0xffffffff)
543
- const x69: u64 = p256FiatMulxU64Hi(x54, p256FiatWord64(0xffffffff, 0xffffffff))
530
+ const x69: u64 = mulHigh(x54, p256FiatWord64(0xffffffff, 0xffffffff))
544
531
  const x70: u64 = p256FiatAddcarryxU64(0x0, x69, x66)
545
532
  const x71: u64 = p256FiatAddcarryxU64Out2(x69, x66, x70)
546
533
  const x72: u64 = x71 + x67
@@ -556,13 +543,13 @@ const p256FiatSquare = (out1: u32[], arg1: u32[]): void => {
556
543
  const x82: u64 = p256FiatAddcarryxU64Out2(x62, x65, x81)
557
544
  const x83: u64 = x82 + x63
558
545
  const x84: u64 = x2 * arg1Limb3
559
- const x85: u64 = p256FiatMulxU64Hi(x2, arg1Limb3)
546
+ const x85: u64 = mulHigh(x2, arg1Limb3)
560
547
  const x86: u64 = x2 * arg1Limb2
561
- const x87: u64 = p256FiatMulxU64Hi(x2, arg1Limb2)
548
+ const x87: u64 = mulHigh(x2, arg1Limb2)
562
549
  const x88: u64 = x2 * arg1Limb1
563
- const x89: u64 = p256FiatMulxU64Hi(x2, arg1Limb1)
550
+ const x89: u64 = mulHigh(x2, arg1Limb1)
564
551
  const x90: u64 = x2 * arg1Limb0
565
- const x91: u64 = p256FiatMulxU64Hi(x2, arg1Limb0)
552
+ const x91: u64 = mulHigh(x2, arg1Limb0)
566
553
  const x92: u64 = p256FiatAddcarryxU64(0x0, x91, x88)
567
554
  const x93: u64 = p256FiatAddcarryxU64Out2(x91, x88, x92)
568
555
  const x94: u64 = p256FiatAddcarryxU64(x93, x89, x86)
@@ -581,11 +568,11 @@ const p256FiatSquare = (out1: u32[], arg1: u32[]): void => {
581
568
  const x107: u64 = p256FiatAddcarryxU64(x106, x83, x98)
582
569
  const x108: u64 = p256FiatAddcarryxU64Out2(x83, x98, x107)
583
570
  const x109: u64 = x99 * p256FiatWord64(0xffffffff, 0x1)
584
- const x110: u64 = p256FiatMulxU64Hi(x99, p256FiatWord64(0xffffffff, 0x1))
571
+ const x110: u64 = mulHigh(x99, p256FiatWord64(0xffffffff, 0x1))
585
572
  const x111: u64 = x99 * 0xffffffff
586
- const x112: u64 = p256FiatMulxU64Hi(x99, 0xffffffff)
573
+ const x112: u64 = mulHigh(x99, 0xffffffff)
587
574
  const x113: u64 = x99 * p256FiatWord64(0xffffffff, 0xffffffff)
588
- const x114: u64 = p256FiatMulxU64Hi(x99, p256FiatWord64(0xffffffff, 0xffffffff))
575
+ const x114: u64 = mulHigh(x99, p256FiatWord64(0xffffffff, 0xffffffff))
589
576
  const x115: u64 = p256FiatAddcarryxU64(0x0, x114, x111)
590
577
  const x116: u64 = p256FiatAddcarryxU64Out2(x114, x111, x115)
591
578
  const x117: u64 = x116 + x112
@@ -601,13 +588,13 @@ const p256FiatSquare = (out1: u32[], arg1: u32[]): void => {
601
588
  const x127: u64 = p256FiatAddcarryxU64Out2(x107, x110, x126)
602
589
  const x128: u64 = x127 + x108
603
590
  const x129: u64 = x3 * arg1Limb3
604
- const x130: u64 = p256FiatMulxU64Hi(x3, arg1Limb3)
591
+ const x130: u64 = mulHigh(x3, arg1Limb3)
605
592
  const x131: u64 = x3 * arg1Limb2
606
- const x132: u64 = p256FiatMulxU64Hi(x3, arg1Limb2)
593
+ const x132: u64 = mulHigh(x3, arg1Limb2)
607
594
  const x133: u64 = x3 * arg1Limb1
608
- const x134: u64 = p256FiatMulxU64Hi(x3, arg1Limb1)
595
+ const x134: u64 = mulHigh(x3, arg1Limb1)
609
596
  const x135: u64 = x3 * arg1Limb0
610
- const x136: u64 = p256FiatMulxU64Hi(x3, arg1Limb0)
597
+ const x136: u64 = mulHigh(x3, arg1Limb0)
611
598
  const x137: u64 = p256FiatAddcarryxU64(0x0, x136, x133)
612
599
  const x138: u64 = p256FiatAddcarryxU64Out2(x136, x133, x137)
613
600
  const x139: u64 = p256FiatAddcarryxU64(x138, x134, x131)
@@ -626,11 +613,11 @@ const p256FiatSquare = (out1: u32[], arg1: u32[]): void => {
626
613
  const x152: u64 = p256FiatAddcarryxU64(x151, x128, x143)
627
614
  const x153: u64 = p256FiatAddcarryxU64Out2(x128, x143, x152)
628
615
  const x154: u64 = x144 * p256FiatWord64(0xffffffff, 0x1)
629
- const x155: u64 = p256FiatMulxU64Hi(x144, p256FiatWord64(0xffffffff, 0x1))
616
+ const x155: u64 = mulHigh(x144, p256FiatWord64(0xffffffff, 0x1))
630
617
  const x156: u64 = x144 * 0xffffffff
631
- const x157: u64 = p256FiatMulxU64Hi(x144, 0xffffffff)
618
+ const x157: u64 = mulHigh(x144, 0xffffffff)
632
619
  const x158: u64 = x144 * p256FiatWord64(0xffffffff, 0xffffffff)
633
- const x159: u64 = p256FiatMulxU64Hi(x144, p256FiatWord64(0xffffffff, 0xffffffff))
620
+ const x159: u64 = mulHigh(x144, p256FiatWord64(0xffffffff, 0xffffffff))
634
621
  const x160: u64 = p256FiatAddcarryxU64(0x0, x159, x156)
635
622
  const x161: u64 = p256FiatAddcarryxU64Out2(x159, x156, x160)
636
623
  const x162: u64 = x161 + x157
@@ -2118,11 +2105,7 @@ const p256CurveB = (): u32[] => {
2118
2105
  * The plain 1, `[1, 0, …, 0]`. A Montgomery multiply by it divides by R, which
2119
2106
  * takes an element out of the Montgomery domain: fiat's `from_montgomery`.
2120
2107
  */
2121
- const p256PlainOne = (): u32[] => {
2122
- const out: u32[] = p256Limbs()
2123
- out[0] = 1
2124
- return out
2125
- }
2108
+ const p256PlainOne = (): u32[] => [1, 0, 0, 0, 0, 0, 0, 0]
2126
2109
 
2127
2110
  /** `out = a`, limb by limb. */
2128
2111
  const p256Copy = (out: u32[], a: u32[]): void => {
@@ -2136,41 +2119,112 @@ const p256Copy = (out: u32[], a: u32[]): void => {
2136
2119
 
2137
2120
  /**
2138
2121
  * The plain limbs of the 32 big-endian bytes `bytes[at .. at + 32)`, limb 0
2139
- * the least significant. A window outside `bytes` leaves `out` as it was;
2140
- * every caller has checked the length first.
2122
+ * the least significant. Answers `false`, having read and written nothing,
2123
+ * for a window outside `bytes` or an `out` shorter than 8 limbs (a scratch
2124
+ * array an importer resized), and every caller refuses on it.
2141
2125
  */
2142
- const p256LimbsFromBytes = (out: u32[], bytes: u8[], at: i32): void => {
2143
- const length: i32 = toI32(bytes.length)
2144
- if (toI32(out.length) < 8 || at < 0 || at > length - 32) {
2145
- return
2146
- }
2147
- for (let i: i32 = 0; i < 8; i++) {
2148
- let limb: u32 = 0
2149
- for (let b: i32 = 0; b < 4; b++) {
2150
- const j: i32 = at + 28 - 4 * i + b
2151
- if (j >= 0 && j < length) {
2152
- limb = (limb << 8) | toU32(bytes[j])
2153
- }
2154
- }
2155
- out[i] = limb
2126
+ const p256LimbsFromBytes = (out: u32[], bytes: u8[], at: i32): boolean => {
2127
+ if (toI32(out.length) < 8 || at < 0 || at > toI32(bytes.length) - 32) {
2128
+ return false
2156
2129
  }
2130
+ // Unrolled so each index is the checked `at` plus a constant, which the
2131
+ // bounds prover discharges from the one check above.
2132
+ out[0] =
2133
+ (toU32(bytes[at + 28]) << 24) |
2134
+ (toU32(bytes[at + 29]) << 16) |
2135
+ (toU32(bytes[at + 30]) << 8) |
2136
+ toU32(bytes[at + 31])
2137
+ out[1] =
2138
+ (toU32(bytes[at + 24]) << 24) |
2139
+ (toU32(bytes[at + 25]) << 16) |
2140
+ (toU32(bytes[at + 26]) << 8) |
2141
+ toU32(bytes[at + 27])
2142
+ out[2] =
2143
+ (toU32(bytes[at + 20]) << 24) |
2144
+ (toU32(bytes[at + 21]) << 16) |
2145
+ (toU32(bytes[at + 22]) << 8) |
2146
+ toU32(bytes[at + 23])
2147
+ out[3] =
2148
+ (toU32(bytes[at + 16]) << 24) |
2149
+ (toU32(bytes[at + 17]) << 16) |
2150
+ (toU32(bytes[at + 18]) << 8) |
2151
+ toU32(bytes[at + 19])
2152
+ out[4] =
2153
+ (toU32(bytes[at + 12]) << 24) |
2154
+ (toU32(bytes[at + 13]) << 16) |
2155
+ (toU32(bytes[at + 14]) << 8) |
2156
+ toU32(bytes[at + 15])
2157
+ out[5] =
2158
+ (toU32(bytes[at + 8]) << 24) |
2159
+ (toU32(bytes[at + 9]) << 16) |
2160
+ (toU32(bytes[at + 10]) << 8) |
2161
+ toU32(bytes[at + 11])
2162
+ out[6] =
2163
+ (toU32(bytes[at + 4]) << 24) |
2164
+ (toU32(bytes[at + 5]) << 16) |
2165
+ (toU32(bytes[at + 6]) << 8) |
2166
+ toU32(bytes[at + 7])
2167
+ out[7] =
2168
+ (toU32(bytes[at]) << 24) |
2169
+ (toU32(bytes[at + 1]) << 16) |
2170
+ (toU32(bytes[at + 2]) << 8) |
2171
+ toU32(bytes[at + 3])
2172
+ return true
2157
2173
  }
2158
2174
 
2159
- /** The plain limbs `a` as 32 big-endian bytes at `out[at .. at + 32)`. */
2160
- const p256LimbsToBytes = (out: u8[], at: i32, a: u32[]): void => {
2161
- const length: i32 = toI32(out.length)
2162
- if (toI32(a.length) < 8 || at < 0 || at > length - 32) {
2163
- return
2164
- }
2165
- for (let i: i32 = 0; i < 8; i++) {
2166
- const limb: u32 = a[i]
2167
- for (let b: i32 = 0; b < 4; b++) {
2168
- const j: i32 = at + 31 - 4 * i - b
2169
- if (j >= 0 && j < length) {
2170
- out[j] = toU8(limb >> toU32(8 * b))
2171
- }
2172
- }
2175
+ /**
2176
+ * The plain limbs `a` as 32 big-endian bytes at `out[at .. at + 32)`. Answers
2177
+ * `false`, having written nothing, for an `a` shorter than 8 limbs or a
2178
+ * window outside `out` (a scratch array an importer resized), and every
2179
+ * caller refuses on it.
2180
+ */
2181
+ const p256LimbsToBytes = (out: u8[], at: i32, a: u32[]): boolean => {
2182
+ if (toI32(a.length) < 8 || at < 0 || at > toI32(out.length) - 32) {
2183
+ return false
2173
2184
  }
2185
+ // Unrolled so each index is the checked `at` plus a constant, which the
2186
+ // bounds prover discharges from the one check above.
2187
+ const a0: u32 = a[0]
2188
+ out[at + 28] = toU8(a0 >> 24)
2189
+ out[at + 29] = toU8(a0 >> 16)
2190
+ out[at + 30] = toU8(a0 >> 8)
2191
+ out[at + 31] = toU8(a0)
2192
+ const a1: u32 = a[1]
2193
+ out[at + 24] = toU8(a1 >> 24)
2194
+ out[at + 25] = toU8(a1 >> 16)
2195
+ out[at + 26] = toU8(a1 >> 8)
2196
+ out[at + 27] = toU8(a1)
2197
+ const a2: u32 = a[2]
2198
+ out[at + 20] = toU8(a2 >> 24)
2199
+ out[at + 21] = toU8(a2 >> 16)
2200
+ out[at + 22] = toU8(a2 >> 8)
2201
+ out[at + 23] = toU8(a2)
2202
+ const a3: u32 = a[3]
2203
+ out[at + 16] = toU8(a3 >> 24)
2204
+ out[at + 17] = toU8(a3 >> 16)
2205
+ out[at + 18] = toU8(a3 >> 8)
2206
+ out[at + 19] = toU8(a3)
2207
+ const a4: u32 = a[4]
2208
+ out[at + 12] = toU8(a4 >> 24)
2209
+ out[at + 13] = toU8(a4 >> 16)
2210
+ out[at + 14] = toU8(a4 >> 8)
2211
+ out[at + 15] = toU8(a4)
2212
+ const a5: u32 = a[5]
2213
+ out[at + 8] = toU8(a5 >> 24)
2214
+ out[at + 9] = toU8(a5 >> 16)
2215
+ out[at + 10] = toU8(a5 >> 8)
2216
+ out[at + 11] = toU8(a5)
2217
+ const a6: u32 = a[6]
2218
+ out[at + 4] = toU8(a6 >> 24)
2219
+ out[at + 5] = toU8(a6 >> 16)
2220
+ out[at + 6] = toU8(a6 >> 8)
2221
+ out[at + 7] = toU8(a6)
2222
+ const a7: u32 = a[7]
2223
+ out[at] = toU8(a7 >> 24)
2224
+ out[at + 1] = toU8(a7 >> 16)
2225
+ out[at + 2] = toU8(a7 >> 8)
2226
+ out[at + 3] = toU8(a7)
2227
+ return true
2174
2228
  }
2175
2229
 
2176
2230
  /**
@@ -2744,9 +2798,14 @@ const p256TableStore = (table: u32[], at: i32, a: u32[]): void => {
2744
2798
  if (toI32(a.length) < 8 || at < 0 || at > toI32(table.length) - 8) {
2745
2799
  return
2746
2800
  }
2747
- for (let i: i32 = 0; i < 8; i++) {
2748
- table[at + i] = a[i]
2749
- }
2801
+ table[at] = a[0]
2802
+ table[at + 1] = a[1]
2803
+ table[at + 2] = a[2]
2804
+ table[at + 3] = a[3]
2805
+ table[at + 4] = a[4]
2806
+ table[at + 5] = a[5]
2807
+ table[at + 6] = a[6]
2808
+ table[at + 7] = a[7]
2750
2809
  }
2751
2810
 
2752
2811
  /** Table entry `i` (limbs `24i` to `24i + 23`, X then Y then Z) = `p`. */
@@ -2784,18 +2843,17 @@ const p256TableBuild = (
2784
2843
  * way, at an offset that is the caller's loop position, never the secret.
2785
2844
  */
2786
2845
  const p256TableMove = (out: u32[], table: u32[], at: i32, hit: u32): void => {
2787
- const p: u32 = toU32(at)
2788
2846
  if (toI32(out.length) < 8 || at < 0 || at > toI32(table.length) - 8) {
2789
2847
  return
2790
2848
  }
2791
- out[0] = p256FiatCmovznzU32(hit, out[0], table[p])
2792
- out[1] = p256FiatCmovznzU32(hit, out[1], table[p + 1])
2793
- out[2] = p256FiatCmovznzU32(hit, out[2], table[p + 2])
2794
- out[3] = p256FiatCmovznzU32(hit, out[3], table[p + 3])
2795
- out[4] = p256FiatCmovznzU32(hit, out[4], table[p + 4])
2796
- out[5] = p256FiatCmovznzU32(hit, out[5], table[p + 5])
2797
- out[6] = p256FiatCmovznzU32(hit, out[6], table[p + 6])
2798
- out[7] = p256FiatCmovznzU32(hit, out[7], table[p + 7])
2849
+ out[0] = p256FiatCmovznzU32(hit, out[0], table[at])
2850
+ out[1] = p256FiatCmovznzU32(hit, out[1], table[at + 1])
2851
+ out[2] = p256FiatCmovznzU32(hit, out[2], table[at + 2])
2852
+ out[3] = p256FiatCmovznzU32(hit, out[3], table[at + 3])
2853
+ out[4] = p256FiatCmovznzU32(hit, out[4], table[at + 4])
2854
+ out[5] = p256FiatCmovznzU32(hit, out[5], table[at + 5])
2855
+ out[6] = p256FiatCmovznzU32(hit, out[6], table[at + 6])
2856
+ out[7] = p256FiatCmovznzU32(hit, out[7], table[at + 7])
2799
2857
  }
2800
2858
 
2801
2859
  /**
@@ -2822,29 +2880,31 @@ const p256TableSelect = (out: P256ProjectivePoint, table: u32[], digit: u32): vo
2822
2880
  * the top nibble down; their positions are public, their values reach only
2823
2881
  * `p256TableSelect`. Every window does four doublings, one table read and one
2824
2882
  * complete addition, so a zero digit or a leading run of zeros costs what any
2825
- * other digit does.
2883
+ * other digit does. Answers `false`, writing nothing, for a `k` of any other
2884
+ * length.
2826
2885
  */
2827
2886
  const p256ScalarMult = (
2828
2887
  out: P256ProjectivePoint,
2829
2888
  k: u8[],
2830
2889
  p: P256ProjectivePoint,
2831
2890
  s: P256PointScratch
2832
- ): void => {
2891
+ ): boolean => {
2892
+ // Refused before anything is allocated or written, so a refusal leaves
2893
+ // nothing of the scalar to wipe.
2894
+ if (toI32(k.length) !== 32) {
2895
+ return false
2896
+ }
2833
2897
  // The accumulator, the selected entry and the table follow the scalar's
2834
2898
  // digits, so all three are wiped before it returns.
2835
2899
  const table: u32[] = new Array<u32>(384)
2836
2900
  const acc = new P256ProjectivePoint()
2837
2901
  const entry: P256ProjectivePoint = s.entry
2838
- const kLength: i32 = toI32(k.length)
2839
- if (kLength < 32) {
2840
- return
2841
- }
2842
2902
  p256TableBuild(table, p, s, acc)
2843
2903
  p256SetIdentity(acc)
2844
2904
  for (let i: i32 = 0; i < 64; i++) {
2845
2905
  // Digit i is the high nibble of byte i / 2 when i is even, the low when odd.
2846
2906
  const at: i32 = i >> 1
2847
- const byte: u32 = at >= 0 && at < kLength ? toU32(k[at]) : 0
2907
+ const byte: u32 = toU32(k[at])
2848
2908
  const digit: u32 = (byte >> ((1 - toU32(i & 1)) << 2)) & 15
2849
2909
  p256PointDouble(acc, acc, s)
2850
2910
  p256PointDouble(acc, acc, s)
@@ -2857,6 +2917,7 @@ const p256ScalarMult = (
2857
2917
  wipe(table)
2858
2918
  p256WipePoint(acc)
2859
2919
  p256WipePoint(entry)
2920
+ return true
2860
2921
  }
2861
2922
 
2862
2923
  /** The base point G (SEC 2 §2.4.2), in the Montgomery domain with Z = 1. */
@@ -3575,16 +3636,20 @@ const P256_BASE_ENTRY: i32 = 16
3575
3636
  * is first taken mod n by a subtraction kept or dropped by fiat's
3576
3637
  * conditional move; the halving is a scalar Montgomery multiply by
3577
3638
  * `2^-1 R`. Every step runs whatever `k` is. `s.t0` (`k - n`) and `s.t1`
3578
- * (the constants) are left to the caller's `p256WipeScratch`.
3639
+ * (the constants) are left to the caller's `p256WipeScratch`. Answers `false`,
3640
+ * having read nothing, when `k` or `m` is too short to hold a scalar.
3579
3641
  */
3580
- const p256BaseRecode = (m: u32[], k: u8[], s: P256PointScratch): void => {
3581
- p256LimbsFromBytes(m, k, 0)
3642
+ const p256BaseRecode = (m: u32[], k: u8[], s: P256PointScratch): boolean => {
3643
+ if (!p256LimbsFromBytes(m, k, 0)) {
3644
+ return false
3645
+ }
3582
3646
  p256SetOrder(s.t1)
3583
3647
  p256FiatSelectznz(m, p256SubBorrow(s.t0, m, s.t1), s.t0, m)
3584
3648
  p256BaseOffset(s.t1)
3585
3649
  p256FiatScalarAdd(m, m, s.t1)
3586
3650
  p256BaseHalf(s.t1)
3587
3651
  p256FiatScalarMul(m, m, s.t1)
3652
+ return true
3588
3653
  }
3589
3654
 
3590
3655
  /**
@@ -3599,13 +3664,15 @@ const p256BaseDigit = (m: u32[], c: i32, i: i32): u32 => {
3599
3664
  }
3600
3665
  let digit: u32 = 0
3601
3666
  for (let j: i32 = 0; j < P256_COMB_TEETH; j++) {
3602
- const bit: i32 = (c * P256_COMB_TEETH + j) * P256_COMB_SPACING + i
3603
- const limb: i32 = bit >> 5
3604
- // `limb` is below 16 (the comb covers fewer than 512 bits), so
3667
+ // In u32: the caller's `c` and `i` are its loop positions, so `bit` is
3668
+ // below 512 and no sum wraps.
3669
+ const bit: u32 = (toU32(c) * toU32(P256_COMB_TEETH) + toU32(j)) * toU32(P256_COMB_SPACING) + toU32(i)
3670
+ const limb: u32 = bit >> 5
3671
+ // `limb` is below 16 (the comb covers fewer than 512 bits), so the u32
3605
3672
  // `(limb >> 3) - 1` is all ones below 8 and 0 past the top, with no branch.
3606
- const at: i32 = limb & 7
3607
- const word: u32 = m[at] & toU32((limb >> 3) - 1)
3608
- digit = digit | (((word >> toU32(bit & 31)) & 1) << toU32(j))
3673
+ const at: i32 = toI32(limb & 7)
3674
+ const word: u32 = m[at] & ((limb >> 3) - 1)
3675
+ digit = digit | (((word >> (bit & 31)) & 1) << toU32(j))
3609
3676
  }
3610
3677
  return digit
3611
3678
  }
@@ -3620,14 +3687,27 @@ const p256BaseDigit = (m: u32[], c: i32, i: i32): u32 => {
3620
3687
  const p256BaseMove = (out: P256ProjectivePoint, table: u32[], at: i32, mask: u32): void => {
3621
3688
  const x: u32[] = out.x
3622
3689
  const y: u32[] = out.y
3623
- if (toI32(x.length) < 8 || toI32(y.length) < 8 || at < 0 || at > toI32(table.length) - P256_BASE_ENTRY) {
3690
+ if (toI32(x.length) < 8 || toI32(y.length) < 8 || at < 0 || at > toI32(table.length) - 16) {
3624
3691
  return
3625
3692
  }
3626
- const p: u32 = toU32(at)
3627
- for (let i: u32 = 0; i < 8; i++) {
3628
- x[i] = x[i] | (table[p + i] & mask)
3629
- y[i] = y[i] | (table[p + 8 + i] & mask)
3630
- }
3693
+ // Each offset is `at` plus a constant below 16, inside the entry the check
3694
+ // above found.
3695
+ x[0] = x[0] | (table[at] & mask)
3696
+ x[1] = x[1] | (table[at + 1] & mask)
3697
+ x[2] = x[2] | (table[at + 2] & mask)
3698
+ x[3] = x[3] | (table[at + 3] & mask)
3699
+ x[4] = x[4] | (table[at + 4] & mask)
3700
+ x[5] = x[5] | (table[at + 5] & mask)
3701
+ x[6] = x[6] | (table[at + 6] & mask)
3702
+ x[7] = x[7] | (table[at + 7] & mask)
3703
+ y[0] = y[0] | (table[at + 8] & mask)
3704
+ y[1] = y[1] | (table[at + 9] & mask)
3705
+ y[2] = y[2] | (table[at + 10] & mask)
3706
+ y[3] = y[3] | (table[at + 11] & mask)
3707
+ y[4] = y[4] | (table[at + 12] & mask)
3708
+ y[5] = y[5] | (table[at + 13] & mask)
3709
+ y[6] = y[6] | (table[at + 14] & mask)
3710
+ y[7] = y[7] | (table[at + 15] & mask)
3631
3711
  }
3632
3712
 
3633
3713
  /**
@@ -3654,7 +3734,9 @@ const p256BaseSelect = (
3654
3734
  p256SetZero(out.x)
3655
3735
  p256SetZero(out.y)
3656
3736
  for (let j: i32 = 0; j < entries; j++) {
3657
- p256BaseMove(out, table, (c * entries + j) * P256_BASE_ENTRY, ctEq(toU32(j), u))
3737
+ // In u32: `c` is a comb below P256_COMBS, so the offset is inside the table.
3738
+ const at: i32 = toI32((toU32(c) * toU32(entries) + toU32(j)) * toU32(P256_BASE_ENTRY))
3739
+ p256BaseMove(out, table, at, ctEq(toU32(j), u))
3658
3740
  }
3659
3741
  p256SetZero(s.t1)
3660
3742
  p256FiatSub(s.t0, s.t1, out.y)
@@ -3725,9 +3807,9 @@ const p256PointAddMixed = (p: P256ProjectivePoint, q: P256ProjectivePoint, s: P2
3725
3807
  * `p256ScalarMult(out, k, G, s)`, which tests/link/crypto_p256_agree checks
3726
3808
  * bit for bit.
3727
3809
  */
3728
- const p256BaseMult = (out: P256ProjectivePoint, k: u8[], s: P256PointScratch): void => {
3810
+ const p256BaseMult = (out: P256ProjectivePoint, k: u8[], s: P256PointScratch): boolean => {
3729
3811
  const table: u32[] = p256BaseTable()
3730
- p256BaseMultWith(out, k, s, table)
3812
+ return p256BaseMultWith(out, k, s, table)
3731
3813
  }
3732
3814
 
3733
3815
  /**
@@ -3737,14 +3819,15 @@ const p256BaseMult = (out: P256ProjectivePoint, k: u8[], s: P256PointScratch): v
3737
3819
  * scalar in `s.comb` and the entry selected into `s.entry` follow `k`, so
3738
3820
  * both are wiped before this returns, and `s.sum` with the rest of `s` by
3739
3821
  * the caller's `p256WipeScratch`; the table is a function of G alone, and
3740
- * kept.
3822
+ * kept. Answers `false`, with `out` untouched, for a `k` shorter than 32
3823
+ * bytes, an `s.comb` too short to recode it into, or a table not
3824
+ * `p256BaseTable()`'s length; every caller refuses on it.
3741
3825
  */
3742
- const p256BaseMultWith = (out: P256ProjectivePoint, k: u8[], s: P256PointScratch, table: u32[]): void => {
3743
- if (toI32(k.length) < 32) {
3744
- return
3745
- }
3826
+ const p256BaseMultWith = (out: P256ProjectivePoint, k: u8[], s: P256PointScratch, table: u32[]): boolean => {
3746
3827
  const m: u32[] = s.comb
3747
- p256BaseRecode(m, k, s)
3828
+ if (toI32(table.length) !== P256_COMBS * P256_COMB_ENTRIES * P256_BASE_ENTRY || !p256BaseRecode(m, k, s)) {
3829
+ return false
3830
+ }
3748
3831
  p256SetIdentity(out)
3749
3832
  for (let i: i32 = P256_COMB_SPACING - 1; i >= 0; i--) {
3750
3833
  if (i < P256_COMB_SPACING - 1) {
@@ -3758,6 +3841,7 @@ const p256BaseMultWith = (out: P256ProjectivePoint, k: u8[], s: P256PointScratch
3758
3841
  }
3759
3842
  wipe(m)
3760
3843
  p256WipePoint(s.entry)
3844
+ return true
3761
3845
  }
3762
3846
 
3763
3847
  /**
@@ -3788,8 +3872,9 @@ const p256DecodePoint = (bytes: u8[]): P256ProjectivePoint | null => {
3788
3872
  const prime: u32[] = p256Prime()
3789
3873
  const r2: u32[] = p256FieldR2()
3790
3874
  const p = new P256ProjectivePoint()
3791
- p256LimbsFromBytes(p.x, bytes, 1)
3792
- p256LimbsFromBytes(p.y, bytes, 33)
3875
+ if (!p256LimbsFromBytes(p.x, bytes, 1) || !p256LimbsFromBytes(p.y, bytes, 33)) {
3876
+ return null
3877
+ }
3793
3878
  if (p256Below(p.x, prime) === 0 || p256Below(p.y, prime) === 0) {
3794
3879
  return null
3795
3880
  }
@@ -3814,8 +3899,11 @@ const p256DecodePoint = (bytes: u8[]): P256ProjectivePoint | null => {
3814
3899
  return p
3815
3900
  }
3816
3901
 
3817
- /** `p` as its 65-byte uncompressed SEC 1 encoding; `p` is never the identity here. */
3818
- const p256EncodePoint = (p: P256ProjectivePoint): u8[] => {
3902
+ /**
3903
+ * `p` as its 65-byte uncompressed SEC 1 encoding; `p` is never the identity
3904
+ * here. Answers `null` for a coordinate shorter than 8 limbs.
3905
+ */
3906
+ const p256EncodePoint = (p: P256ProjectivePoint): u8[] | null => {
3819
3907
  const zInverse: u32[] = p256Limbs()
3820
3908
  const one: u32[] = p256PlainOne()
3821
3909
  const x: u32[] = p256Limbs()
@@ -3827,8 +3915,9 @@ const p256EncodePoint = (p: P256ProjectivePoint): u8[] => {
3827
3915
  p256FiatMul(y, y, one)
3828
3916
  const out: u8[] = new Array<u8>(65)
3829
3917
  out[0] = 4
3830
- p256LimbsToBytes(out, 1, x)
3831
- p256LimbsToBytes(out, 33, y)
3918
+ if (!p256LimbsToBytes(out, 1, x) || !p256LimbsToBytes(out, 33, y)) {
3919
+ return null
3920
+ }
3832
3921
  return out
3833
3922
  }
3834
3923
 
@@ -3842,7 +3931,9 @@ const p256ScalarInRange = (bytes: u8[]): boolean => {
3842
3931
  return false
3843
3932
  }
3844
3933
  const a: u32[] = p256Limbs()
3845
- p256LimbsFromBytes(a, bytes, 0)
3934
+ if (!p256LimbsFromBytes(a, bytes, 0)) {
3935
+ return false
3936
+ }
3846
3937
  const below: u32 = p256Below(a, p256Order())
3847
3938
  const zero: u32 = ctEq(p256FiatNonzero(a), 0) & 1
3848
3939
  wipe(a) // a private key's limbs, when `bytes` is one
@@ -3853,19 +3944,28 @@ const p256ScalarInRange = (bytes: u8[]): boolean => {
3853
3944
  * bits2int of RFC 6979 §2.3.2 for qlen = 256, then reduced mod n: the leftmost
3854
3945
  * 256 bits of `digest` as an integer, as FIPS 186-4 §6.4 takes a hash, and
3855
3946
  * then n subtracted once if it is at least n. A digest shorter than 32 bytes
3856
- * is its own value, zero-extended on the left. Answers plain limbs.
3947
+ * is its own value, zero-extended on the left. Answers plain limbs, and
3948
+ * `false`, with `out` untouched, for an `out` shorter than 8 limbs.
3857
3949
  */
3858
- const p256DigestScalar = (out: u32[], digest: u8[]): void => {
3950
+ const p256DigestScalar = (out: u32[], digest: u8[]): boolean => {
3859
3951
  const length: i32 = toI32(digest.length)
3860
- const bytes: u8[] = new Array<u8>(32)
3861
- const take: i32 = length < 32 ? length : 32
3862
- const skip: i32 = 32 - take
3863
- for (let i: i32 = 0; i < take && i < length && skip + i < 32; i++) {
3864
- bytes[skip + i] = digest[i]
3952
+ let take: i32 = 32
3953
+ if (length < 32) {
3954
+ take = length
3955
+ }
3956
+ const bytes: u8[] = []
3957
+ for (let i: i32 = take; i < 32; i++) {
3958
+ bytes.push(0)
3959
+ }
3960
+ for (let i: i32 = 0; i < take && i < toI32(digest.length); i++) {
3961
+ bytes.push(digest[i])
3865
3962
  }
3866
3963
  const a: u32[] = p256Limbs()
3867
- p256LimbsFromBytes(a, bytes, 0)
3964
+ if (toI32(out.length) < 8 || !p256LimbsFromBytes(a, bytes, 0)) {
3965
+ return false
3966
+ }
3868
3967
  p256ReduceOnce(out, a, p256Order())
3968
+ return true
3869
3969
  }
3870
3970
 
3871
3971
  /** RFC 2104's key block for SHA-256: `k` zero-extended to 64 bytes, each byte XOR `pad`. */
@@ -3946,12 +4046,17 @@ const p256NonceValue = (k: u8[], v: u8[]): u8[] => {
3946
4046
  * again through step h.3, as the RFC says. Whether a candidate is below n is a
3947
4047
  * branch on the secret, and the one the RFC prescribes: it is taken once in
3948
4048
  * about 2^32 signatures and says only that a discarded value was large.
4049
+ *
4050
+ * Answers `null` for a `priv` or `z` too short to hold a scalar, with every
4051
+ * value derived from the key wiped as on the signing exit.
3949
4052
  */
3950
- const p256SignScalar = (priv: u8[], z: u32[]): u8[] => {
4053
+ const p256SignScalar = (priv: u8[], z: u32[]): u8[] | null => {
3951
4054
  const n: u32[] = p256Order()
3952
4055
  const r2: u32[] = p256ScalarR2()
3953
4056
  const h1: u8[] = new Array<u8>(32)
3954
- p256LimbsToBytes(h1, 0, z)
4057
+ if (!p256LimbsToBytes(h1, 0, z)) {
4058
+ return null
4059
+ }
3955
4060
  const empty: u8[] = new Array<u8>(0)
3956
4061
  let v: u8[] = new Array<u8>(32)
3957
4062
  let k: u8[] = new Array<u8>(32)
@@ -3974,7 +4079,7 @@ const p256SignScalar = (priv: u8[], z: u32[]): u8[] => {
3974
4079
  v = next
3975
4080
 
3976
4081
  const dM: u32[] = p256Limbs()
3977
- p256LimbsFromBytes(dM, priv, 0)
4082
+ let failed: boolean = !p256LimbsFromBytes(dM, priv, 0)
3978
4083
  p256FiatScalarMul(dM, dM, r2)
3979
4084
  const scratch = new P256PointScratch()
3980
4085
  const point = new P256ProjectivePoint()
@@ -3982,45 +4087,53 @@ const p256SignScalar = (priv: u8[], z: u32[]): u8[] => {
3982
4087
  const r: u32[] = p256Limbs()
3983
4088
  const kM: u32[] = p256Limbs()
3984
4089
  const s: u32[] = p256Limbs()
3985
- while (true) {
4090
+ let sig: u8[] | null = null
4091
+ while (!failed && sig === null) {
3986
4092
  next = p256NonceValue(k, v) // step h.2
3987
4093
  wipe(v)
3988
4094
  v = next
3989
4095
  if (p256ScalarInRange(v)) {
3990
- p256BaseMult(point, v, scratch)
3991
- p256AffineX(x, point)
3992
- p256ReduceOnce(r, x, n)
3993
- // s = k^-1 (z + r d) mod n. A Montgomery multiply of a plain value by
3994
- // one in the Montgomery domain answers a plain value, so only d and
3995
- // k^-1 are converted: r d and then (z + r d) k^-1 come out plain.
3996
- p256LimbsFromBytes(kM, v, 0)
3997
- p256FiatScalarMul(kM, kM, r2)
3998
- p256ScalarInverse(kM, kM)
3999
- p256FiatScalarMul(s, r, dM)
4000
- p256FiatScalarAdd(s, s, z)
4001
- p256FiatScalarMul(s, s, kM)
4002
- if (p256FiatNonzero(r) !== 0 && p256FiatNonzero(s) !== 0) {
4003
- const sig: u8[] = new Array<u8>(64)
4004
- p256LimbsToBytes(sig, 0, r)
4005
- p256LimbsToBytes(sig, 32, s)
4006
- // ECC-2: the private key's limbs, the nonce and its inverse, RFC
4007
- // 6979's K and V, the nonce point and the scratch it was built in.
4008
- wipe(k)
4009
- wipe(v)
4010
- wipe(dM)
4011
- wipe(kM)
4012
- p256WipePoint(point)
4013
- p256WipeScratch(scratch)
4014
- return sig
4096
+ // A 32-byte `v` and 8-limb `kM` are what both take, so neither refuses
4097
+ // here; a refusal still leaves through the wipes below.
4098
+ failed = !p256BaseMult(point, v, scratch) || !p256LimbsFromBytes(kM, v, 0)
4099
+ if (!failed) {
4100
+ p256AffineX(x, point)
4101
+ p256ReduceOnce(r, x, n)
4102
+ // s = k^-1 (z + r d) mod n. A Montgomery multiply of a plain value by
4103
+ // one in the Montgomery domain answers a plain value, so only d and
4104
+ // k^-1 are converted: r d and then (z + r d) k^-1 come out plain.
4105
+ p256FiatScalarMul(kM, kM, r2)
4106
+ p256ScalarInverse(kM, kM)
4107
+ p256FiatScalarMul(s, r, dM)
4108
+ p256FiatScalarAdd(s, s, z)
4109
+ p256FiatScalarMul(s, s, kM)
4110
+ if (p256FiatNonzero(r) !== 0 && p256FiatNonzero(s) !== 0) {
4111
+ const encoded: u8[] = new Array<u8>(64)
4112
+ failed = !p256LimbsToBytes(encoded, 0, r) || !p256LimbsToBytes(encoded, 32, s)
4113
+ if (!failed) {
4114
+ sig = encoded
4115
+ }
4116
+ }
4015
4117
  }
4016
4118
  }
4017
- next = p256NonceKey(k, v, 0, empty, empty) // step h.3
4018
- wipe(k)
4019
- k = next
4020
- next = p256NonceValue(k, v)
4021
- wipe(v)
4022
- v = next
4119
+ if (!failed && sig === null) {
4120
+ next = p256NonceKey(k, v, 0, empty, empty) // step h.3
4121
+ wipe(k)
4122
+ k = next
4123
+ next = p256NonceValue(k, v)
4124
+ wipe(v)
4125
+ v = next
4126
+ }
4023
4127
  }
4128
+ // ECC-2, on every exit: the private key's limbs, the nonce and its inverse,
4129
+ // RFC 6979's K and V, the nonce point and the scratch it was built in.
4130
+ wipe(k)
4131
+ wipe(v)
4132
+ wipe(dM)
4133
+ wipe(kM)
4134
+ p256WipePoint(point)
4135
+ p256WipeScratch(scratch)
4136
+ return sig
4024
4137
  }
4025
4138
 
4026
4139
  /**
@@ -4044,9 +4157,12 @@ const p256PublicKeyExposed = (priv: u8[]): u8[] | null => {
4044
4157
  }
4045
4158
  const point = new P256ProjectivePoint()
4046
4159
  const scratch = new P256PointScratch()
4047
- p256BaseMult(point, priv, scratch)
4160
+ const multiplied: boolean = p256BaseMult(point, priv, scratch)
4048
4161
  p256WipeScratch(scratch)
4049
- const encoded: u8[] = p256EncodePoint(point)
4162
+ let encoded: u8[] | null = null
4163
+ if (multiplied) {
4164
+ encoded = p256EncodePoint(point)
4165
+ }
4050
4166
  p256WipePoint(point)
4051
4167
  return encoded
4052
4168
  }
@@ -4089,7 +4205,11 @@ const p256EcdhExposed = (priv: u8[], peerPoint: u8[]): u8[] | null => {
4089
4205
  }
4090
4206
  const point = new P256ProjectivePoint()
4091
4207
  const scratch = new P256PointScratch()
4092
- p256ScalarMult(point, priv, peer, scratch)
4208
+ // A 32-byte key is all `p256ScalarInRange` takes, so this refusal leaves
4209
+ // `point` and `scratch` as they were made, holding nothing of the key.
4210
+ if (!p256ScalarMult(point, priv, peer, scratch)) {
4211
+ return null
4212
+ }
4093
4213
  p256WipeScratch(scratch)
4094
4214
  const infinity: boolean = p256FiatNonzero(point.z) === 0
4095
4215
  // 1 / Z, over limbs wiped below.
@@ -4099,11 +4219,11 @@ const p256EcdhExposed = (priv: u8[], peerPoint: u8[]): u8[] | null => {
4099
4219
  p256FiatMul(x, point.x, acc)
4100
4220
  p256FiatMul(x, x, p256PlainOne())
4101
4221
  const out: u8[] = new Array<u8>(P256_SCALAR_SIZE)
4102
- p256LimbsToBytes(out, 0, x)
4222
+ const written: boolean = p256LimbsToBytes(out, 0, x)
4103
4223
  wipe(x)
4104
4224
  wipe(acc)
4105
4225
  p256WipePoint(point)
4106
- if (infinity) {
4226
+ if (infinity || !written) {
4107
4227
  wipe(out)
4108
4228
  return null
4109
4229
  }
@@ -4131,19 +4251,12 @@ const p256SignExposed = (priv: u8[], digest: u8[]): u8[] | null => {
4131
4251
  return null
4132
4252
  }
4133
4253
  const z: u32[] = p256Limbs()
4134
- p256DigestScalar(z, digest)
4254
+ if (!p256DigestScalar(z, digest)) {
4255
+ return null
4256
+ }
4135
4257
  return p256SignScalar(priv, z)
4136
4258
  }
4137
4259
 
4138
- /** The width of `p256Verify`'s window: its digits are odd, in [-15, 15]. */
4139
- const P256_WNAF_WIDTH: i32 = 5
4140
-
4141
- /** The bits of a scalar `p256Wnaf` reads; its form has one more digit, for the last carry. */
4142
- const P256_WNAF_BITS: i32 = 256
4143
-
4144
- /** The number of odd multiples `p256OddMultiples` builds: 1, 3, ..., 15 times a point. */
4145
- const P256_WNAF_TABLE: i32 = 8
4146
-
4147
4260
  /**
4148
4261
  * The width-5 non-adjacent form of the 32 big-endian bytes `k`: `out[i]`, for
4149
4262
  * `i` from 0 to 256, is zero or an odd digit in [-15, 15], `k` is the sum of
@@ -4153,37 +4266,36 @@ const P256_WNAF_TABLE: i32 = 8
4153
4266
  * public wherever this is used.
4154
4267
  */
4155
4268
  const p256Wnaf = (out: i32[], k: u8[]): void => {
4156
- if (toI32(out.length) <= P256_WNAF_BITS || toI32(k.length) < 32) {
4157
- return
4158
- }
4269
+ // A scalar's 256 bits and a window 5 wide (its digits are odd, in [-15, 15]).
4270
+ // `p256DoubleMultPublic` makes `out` at 257 digits and its callers `k` at 32 bytes.
4159
4271
  let carry: i32 = 0
4160
4272
  let bit: i32 = 0
4161
- while (bit < P256_WNAF_BITS) {
4273
+ while (bit < 256) {
4162
4274
  if (p256ByteBit(k, bit) === carry) {
4163
4275
  bit++
4164
4276
  continue
4165
4277
  }
4166
- const left: i32 = P256_WNAF_BITS - bit
4167
- const now: i32 = left < P256_WNAF_WIDTH ? left : P256_WNAF_WIDTH
4168
- let word: i32 = carry
4169
- for (let j: i32 = 0; j < now; j++) {
4170
- word = word + (p256ByteBit(k, bit + j) << j)
4171
- }
4172
- carry = (word >> (P256_WNAF_WIDTH - 1)) & 1
4173
- if (bit >= 0 && bit < toI32(out.length)) {
4174
- out[bit] = word - (carry << P256_WNAF_WIDTH)
4278
+ // A window that would run past bit 255 takes only the bits below it.
4279
+ let bits: i32 = 0
4280
+ for (let j: i32 = 0; j < 5; j++) {
4281
+ const at: i32 = bit + j
4282
+ if (at < 256) {
4283
+ bits = bits | (p256ByteBit(k, at) << j)
4284
+ }
4175
4285
  }
4176
- bit = bit + now
4286
+ // The window's bits and the carry in: at most 31 + 1.
4287
+ const word: i32 = (bits & 31) + (carry & 1)
4288
+ carry = (word >> 4) & 1
4289
+ out[bit] = word - (carry & 1) * 32
4290
+ bit = bit + 5
4177
4291
  }
4178
- out[P256_WNAF_BITS] = carry
4292
+ out[256] = carry
4179
4293
  }
4180
4294
 
4181
- /** Bit `i` of the 32 big-endian bytes `k`, 0 the least significant. */
4295
+ /** Bit `i`, 0 to 255, of the 32 big-endian bytes `k`, 0 the least significant. */
4182
4296
  const p256ByteBit = (k: u8[], i: i32): i32 => {
4183
- const at: i32 = (P256_WNAF_BITS >> 3) - 1 - (i >> 3)
4184
- if (at < 0 || at >= toI32(k.length)) {
4185
- return 0
4186
- }
4297
+ const byte: i32 = i >> 3
4298
+ const at: i32 = 31 - byte
4187
4299
  return toI32((toU32(k[at]) >> toU32(i & 7)) & 1)
4188
4300
  }
4189
4301
 
@@ -4193,13 +4305,11 @@ const p256OddMultiples = (
4193
4305
  p: P256ProjectivePoint,
4194
4306
  s: P256PointScratch
4195
4307
  ): void => {
4196
- if (toI32(table.length) < P256_WNAF_TABLE) {
4197
- return
4198
- }
4308
+ p256PointCopy(table[0], p)
4199
4309
  const twice = new P256ProjectivePoint()
4200
4310
  p256PointDouble(twice, p, s)
4201
- p256PointCopy(table[0], p)
4202
- for (let j: i32 = 1; j < toI32(table.length); j++) {
4311
+ // 1, 3, ..., 15 times `p`: the 8 entries `p256DoubleMultPublic` makes `table` with.
4312
+ for (let j: i32 = 1; j < 8; j++) {
4203
4313
  p256PointAdd(table[j], table[j - 1], twice, s)
4204
4314
  }
4205
4315
  }
@@ -4208,6 +4318,8 @@ const p256OddMultiples = (
4208
4318
  * `acc += digit * p`, `digit` odd in [-15, 15], read from `p256OddMultiples`'
4209
4319
  * `table`; a negative digit adds the entry's negation (X : -Y : Z), built in
4210
4320
  * `neg`. Variable time: which entry is read, and its sign, follow the digit.
4321
+ * Answers `false`, adding nothing, for a digit outside [-16, 15], which
4322
+ * `p256Wnaf` does not write.
4211
4323
  */
4212
4324
  const p256WnafAdd = (
4213
4325
  acc: P256ProjectivePoint,
@@ -4215,20 +4327,24 @@ const p256WnafAdd = (
4215
4327
  digit: i32,
4216
4328
  neg: P256ProjectivePoint,
4217
4329
  s: P256PointScratch
4218
- ): void => {
4219
- const at: i32 = (digit < 0 ? -digit : digit) >> 1
4220
- if (at < 0 || at >= toI32(table.length)) {
4221
- return
4330
+ ): boolean => {
4331
+ // For an odd digit, (|digit| - 1) / 2: digit >> 1 when it is positive, and
4332
+ // ~digit >> 1 when it is negative, since ~digit = -digit - 1.
4333
+ const at: i32 = (digit ^ (digit >> 31)) >> 1
4334
+ // 7 is the last of the 8 entries the caller made `table` with.
4335
+ if (at < 0 || at > 7) {
4336
+ return false
4222
4337
  }
4223
4338
  if (digit > 0) {
4224
4339
  p256PointAdd(acc, acc, table[at], s)
4225
- return
4340
+ return true
4226
4341
  }
4227
4342
  p256Copy(neg.x, table[at].x)
4228
4343
  p256SetZero(neg.y)
4229
4344
  p256FiatSub(neg.y, neg.y, table[at].y)
4230
4345
  p256Copy(neg.z, table[at].z)
4231
4346
  p256PointAdd(acc, acc, neg, s)
4347
+ return true
4232
4348
  }
4233
4349
 
4234
4350
  /**
@@ -4238,7 +4354,7 @@ const p256WnafAdd = (
4238
4354
  * nonzero. The doublings before the first nonzero digit are skipped. **Not
4239
4355
  * constant time**: the additions, their table reads and their signs follow
4240
4356
  * the scalars' digits, so this is only for public scalars and points, as in
4241
- * `p256Verify`.
4357
+ * `p256Verify`. Answers `false` when `p256WnafAdd` refuses a digit.
4242
4358
  */
4243
4359
  const p256DoubleMultPublic = (
4244
4360
  out: P256ProjectivePoint,
@@ -4246,35 +4362,56 @@ const p256DoubleMultPublic = (
4246
4362
  b: u8[],
4247
4363
  q: P256ProjectivePoint,
4248
4364
  s: P256PointScratch
4249
- ): void => {
4250
- const wa: i32[] = new Array<i32>(P256_WNAF_BITS + 1)
4251
- const wb: i32[] = new Array<i32>(P256_WNAF_BITS + 1)
4365
+ ): boolean => {
4366
+ // 257 digits: a scalar's 256 bits and the carry out.
4367
+ const wa: i32[] = new Array<i32>(257)
4368
+ const wb: i32[] = new Array<i32>(257)
4252
4369
  p256Wnaf(wa, a)
4253
4370
  p256Wnaf(wb, b)
4254
- const tableG: P256ProjectivePoint[] = []
4255
- const tableQ: P256ProjectivePoint[] = []
4256
- for (let j: i32 = 0; j < P256_WNAF_TABLE; j++) {
4257
- tableG.push(new P256ProjectivePoint())
4258
- tableQ.push(new P256ProjectivePoint())
4259
- }
4371
+ // 1, 3, ..., 15 times each point.
4372
+ const tableG: P256ProjectivePoint[] = [
4373
+ new P256ProjectivePoint(),
4374
+ new P256ProjectivePoint(),
4375
+ new P256ProjectivePoint(),
4376
+ new P256ProjectivePoint(),
4377
+ new P256ProjectivePoint(),
4378
+ new P256ProjectivePoint(),
4379
+ new P256ProjectivePoint(),
4380
+ new P256ProjectivePoint(),
4381
+ ]
4382
+ const tableQ: P256ProjectivePoint[] = [
4383
+ new P256ProjectivePoint(),
4384
+ new P256ProjectivePoint(),
4385
+ new P256ProjectivePoint(),
4386
+ new P256ProjectivePoint(),
4387
+ new P256ProjectivePoint(),
4388
+ new P256ProjectivePoint(),
4389
+ new P256ProjectivePoint(),
4390
+ new P256ProjectivePoint(),
4391
+ ]
4260
4392
  p256OddMultiples(tableG, p256Generator(), s)
4261
4393
  p256OddMultiples(tableQ, q, s)
4262
4394
  const neg = new P256ProjectivePoint()
4263
4395
  p256SetIdentity(out)
4264
4396
  let started: boolean = false
4265
- for (let i: i32 = P256_WNAF_BITS; i >= 0; i--) {
4397
+ for (let i: i32 = 256; i >= 0; i--) {
4266
4398
  if (started) {
4267
4399
  p256PointDouble(out, out, s)
4268
4400
  }
4269
- if (i < toI32(wa.length) && wa[i] !== 0) {
4270
- p256WnafAdd(out, tableG, wa[i], neg, s)
4401
+ if (wa[i] !== 0) {
4402
+ if (!p256WnafAdd(out, tableG, wa[i], neg, s)) {
4403
+ return false
4404
+ }
4271
4405
  started = true
4272
4406
  }
4273
- if (i < toI32(wb.length) && wb[i] !== 0) {
4274
- p256WnafAdd(out, tableQ, wb[i], neg, s)
4407
+ if (wb[i] !== 0) {
4408
+ if (!p256WnafAdd(out, tableQ, wb[i], neg, s)) {
4409
+ return false
4410
+ }
4275
4411
  started = true
4276
4412
  }
4277
4413
  }
4414
+ return true
4278
4415
  }
4279
4416
 
4280
4417
  /**
@@ -4321,9 +4458,13 @@ export const p256Verify = (pub: u8[], digest: u8[], sig: u8[]): boolean => {
4321
4458
  const r: u32[] = p256Limbs()
4322
4459
  const w: u32[] = p256Limbs()
4323
4460
  const z: u32[] = p256Limbs()
4324
- p256LimbsFromBytes(r, rBytes, 0)
4325
- p256LimbsFromBytes(w, sBytes, 0)
4326
- p256DigestScalar(z, digest)
4461
+ if (
4462
+ !p256LimbsFromBytes(r, rBytes, 0) ||
4463
+ !p256LimbsFromBytes(w, sBytes, 0) ||
4464
+ !p256DigestScalar(z, digest)
4465
+ ) {
4466
+ return false
4467
+ }
4327
4468
  // w = s^-1 in the Montgomery domain; u1 = z w and u2 = r w multiply a
4328
4469
  // plain value by it, so they come out plain.
4329
4470
  p256FiatScalarMul(w, w, r2)
@@ -4334,12 +4475,15 @@ export const p256Verify = (pub: u8[], digest: u8[], sig: u8[]): boolean => {
4334
4475
  p256FiatScalarMul(u2, r, w)
4335
4476
  const u1Bytes: u8[] = new Array<u8>(32)
4336
4477
  const u2Bytes: u8[] = new Array<u8>(32)
4337
- p256LimbsToBytes(u1Bytes, 0, u1)
4338
- p256LimbsToBytes(u2Bytes, 0, u2)
4478
+ if (!p256LimbsToBytes(u1Bytes, 0, u1) || !p256LimbsToBytes(u2Bytes, 0, u2)) {
4479
+ return false
4480
+ }
4339
4481
 
4340
4482
  const scratch = new P256PointScratch()
4341
4483
  const sum = new P256ProjectivePoint()
4342
- p256DoubleMultPublic(sum, u1Bytes, u2Bytes, q, scratch)
4484
+ if (!p256DoubleMultPublic(sum, u1Bytes, u2Bytes, q, scratch)) {
4485
+ return false
4486
+ }
4343
4487
  if (p256FiatNonzero(sum.z) === 0) {
4344
4488
  return false
4345
4489
  }
@@ -4347,8 +4491,7 @@ export const p256Verify = (pub: u8[], digest: u8[], sig: u8[]): boolean => {
4347
4491
  p256AffineX(x, sum)
4348
4492
  p256ReduceOnce(x, x, n)
4349
4493
  const vBytes: u8[] = new Array<u8>(32)
4350
- p256LimbsToBytes(vBytes, 0, x)
4351
- return timingSafeEqual(vBytes, rBytes)
4494
+ return p256LimbsToBytes(vBytes, 0, x) && timingSafeEqual(vBytes, rBytes)
4352
4495
  }
4353
4496
 
4354
4497
  /**
@@ -4394,18 +4537,15 @@ export const p256VerifySha256 = (pub: u8[], msg: u8[], sig: u8[]): boolean =>
4394
4537
  // steps e, g and h, the nonce's multiple of G and its inverse — runs on the
4395
4538
  // scratch outside `expose`, on values derived from the key but not the key.
4396
4539
 
4397
- /** The shift of a word's top byte, typed for `--number-mode f64`. */
4398
- const P256_WORD_TOP: i32 = 24
4399
-
4400
4540
  /** RFC 6979 §3.2 steps d and f hash `V || sep || x || h1`: 32 + 1 + 32 + 32 bytes. */
4401
4541
  const P256_NONCE_MESSAGE: i32 = 97
4402
4542
 
4403
4543
  /** ROTR^n(x) of FIPS 180-4 §3.2, `0 < n < 32`. */
4404
4544
  const p256Rotr = (x: u32, n: u32): u32 => (x >>> n) | (x << (32 - n))
4405
4545
 
4406
- /** The big-endian word at `buf[at .. at + 4)`; the caller has checked the window. */
4407
- const p256LoadWord = (buf: u8[], at: i32): u32 =>
4408
- (toU32(buf[at]) << 24) | (toU32(buf[at + 1]) << 16) | (toU32(buf[at + 2]) << 8) | toU32(buf[at + 3])
4546
+ /** The big-endian word whose bytes, most significant first, are `b0` to `b3`. */
4547
+ const p256Word = (b0: u8, b1: u8, b2: u8, b3: u8): u32 =>
4548
+ (toU32(b0) << 24) | (toU32(b1) << 16) | (toU32(b2) << 8) | toU32(b3)
4409
4549
 
4410
4550
  /**
4411
4551
  * FIPS 180-4 §6.2.2 steps 1 to 4 for the block at `src[at .. at + 64)`, folded
@@ -4414,74 +4554,96 @@ const p256LoadWord = (buf: u8[], at: i32): u32 =>
4414
4554
  */
4415
4555
  const p256ShaCompress = (h: u32[], w: u32[], kt: u32[], src: u8[], at: i32): void => {
4416
4556
  if (
4417
- toI32(h.length) >= 8 &&
4418
- toI32(w.length) >= 64 &&
4419
- toI32(kt.length) >= 64 &&
4420
- at >= 0 &&
4421
- at <= toI32(src.length) - SHA256_BLOCK
4557
+ toI32(h.length) < 8 ||
4558
+ toI32(w.length) < 64 ||
4559
+ toI32(kt.length) < 64 ||
4560
+ at < 0 ||
4561
+ at > toI32(src.length) - 64
4422
4562
  ) {
4423
- for (let t: i32 = 0; t < 16; t++) {
4424
- w[t] = p256LoadWord(src, at + 4 * t)
4425
- }
4426
- for (let t: i32 = 16; t < 64; t++) {
4427
- const w2: u32 = w[t - 2]
4428
- const w15: u32 = w[t - 15]
4429
- const sigma1: u32 = p256Rotr(w2, 17) ^ p256Rotr(w2, 19) ^ (w2 >>> 10)
4430
- const sigma0: u32 = p256Rotr(w15, 7) ^ p256Rotr(w15, 18) ^ (w15 >>> 3)
4431
- w[t] = sigma1 + w[t - 7] + sigma0 + w[t - 16]
4432
- }
4433
- let a: u32 = h[0]
4434
- let b: u32 = h[1]
4435
- let c: u32 = h[2]
4436
- let d: u32 = h[3]
4437
- let e: u32 = h[4]
4438
- let f: u32 = h[5]
4439
- let g: u32 = h[6]
4440
- let hh: u32 = h[7]
4441
- for (let t: i32 = 0; t < 64; t++) {
4442
- const bigSigma1: u32 = p256Rotr(e, 6) ^ p256Rotr(e, 11) ^ p256Rotr(e, 25)
4443
- const choose: u32 = (e & f) ^ (~e & g)
4444
- const t1: u32 = hh + bigSigma1 + choose + kt[t] + w[t]
4445
- const bigSigma0: u32 = p256Rotr(a, 2) ^ p256Rotr(a, 13) ^ p256Rotr(a, 22)
4446
- const majority: u32 = (a & b) ^ (a & c) ^ (b & c)
4447
- hh = g
4448
- g = f
4449
- f = e
4450
- e = d + t1
4451
- d = c
4452
- c = b
4453
- b = a
4454
- a = t1 + bigSigma0 + majority
4455
- }
4456
- h[0] += a
4457
- h[1] += b
4458
- h[2] += c
4459
- h[3] += d
4460
- h[4] += e
4461
- h[5] += f
4462
- h[6] += g
4463
- h[7] += hh
4563
+ return
4564
+ }
4565
+ // Each offset is `at` plus a constant below 64, inside the block the check
4566
+ // above found.
4567
+ w[0] = p256Word(src[at], src[at + 1], src[at + 2], src[at + 3])
4568
+ w[1] = p256Word(src[at + 4], src[at + 5], src[at + 6], src[at + 7])
4569
+ w[2] = p256Word(src[at + 8], src[at + 9], src[at + 10], src[at + 11])
4570
+ w[3] = p256Word(src[at + 12], src[at + 13], src[at + 14], src[at + 15])
4571
+ w[4] = p256Word(src[at + 16], src[at + 17], src[at + 18], src[at + 19])
4572
+ w[5] = p256Word(src[at + 20], src[at + 21], src[at + 22], src[at + 23])
4573
+ w[6] = p256Word(src[at + 24], src[at + 25], src[at + 26], src[at + 27])
4574
+ w[7] = p256Word(src[at + 28], src[at + 29], src[at + 30], src[at + 31])
4575
+ w[8] = p256Word(src[at + 32], src[at + 33], src[at + 34], src[at + 35])
4576
+ w[9] = p256Word(src[at + 36], src[at + 37], src[at + 38], src[at + 39])
4577
+ w[10] = p256Word(src[at + 40], src[at + 41], src[at + 42], src[at + 43])
4578
+ w[11] = p256Word(src[at + 44], src[at + 45], src[at + 46], src[at + 47])
4579
+ w[12] = p256Word(src[at + 48], src[at + 49], src[at + 50], src[at + 51])
4580
+ w[13] = p256Word(src[at + 52], src[at + 53], src[at + 54], src[at + 55])
4581
+ w[14] = p256Word(src[at + 56], src[at + 57], src[at + 58], src[at + 59])
4582
+ w[15] = p256Word(src[at + 60], src[at + 61], src[at + 62], src[at + 63])
4583
+ for (let t: i32 = 16; t < 64; t++) {
4584
+ const w2: u32 = w[t - 2]
4585
+ const w15: u32 = w[t - 15]
4586
+ const sigma1: u32 = p256Rotr(w2, 17) ^ p256Rotr(w2, 19) ^ (w2 >>> 10)
4587
+ const sigma0: u32 = p256Rotr(w15, 7) ^ p256Rotr(w15, 18) ^ (w15 >>> 3)
4588
+ w[t] = sigma1 + w[t - 7] + sigma0 + w[t - 16]
4589
+ }
4590
+ let a: u32 = h[0]
4591
+ let b: u32 = h[1]
4592
+ let c: u32 = h[2]
4593
+ let d: u32 = h[3]
4594
+ let e: u32 = h[4]
4595
+ let f: u32 = h[5]
4596
+ let g: u32 = h[6]
4597
+ let hh: u32 = h[7]
4598
+ for (let t: i32 = 0; t < 64; t++) {
4599
+ const bigSigma1: u32 = p256Rotr(e, 6) ^ p256Rotr(e, 11) ^ p256Rotr(e, 25)
4600
+ const choose: u32 = (e & f) ^ (~e & g)
4601
+ const t1: u32 = hh + bigSigma1 + choose + kt[t] + w[t]
4602
+ const bigSigma0: u32 = p256Rotr(a, 2) ^ p256Rotr(a, 13) ^ p256Rotr(a, 22)
4603
+ const majority: u32 = (a & b) ^ (a & c) ^ (b & c)
4604
+ hh = g
4605
+ g = f
4606
+ f = e
4607
+ e = d + t1
4608
+ d = c
4609
+ c = b
4610
+ b = a
4611
+ a = t1 + bigSigma0 + majority
4464
4612
  }
4613
+ h[0] += a
4614
+ h[1] += b
4615
+ h[2] += c
4616
+ h[3] += d
4617
+ h[4] += e
4618
+ h[5] += f
4619
+ h[6] += g
4620
+ h[7] += hh
4465
4621
  }
4466
4622
 
4467
4623
  /**
4468
- * The SHA-256 of `data[off .. off + len)` into `out[at .. at + 32)`, on the
4469
- * caller's hash value `h`, schedule `w` and `tail` (128 bytes, where the last
4470
- * part block is padded as §5.1.1 says). Whole blocks are compressed where
4471
- * they lie, so the message is never copied. `out` may be `data`: the digest is
4472
- * written once every byte has been read. The caller has checked both windows.
4624
+ * The SHA-256 of `data[off .. off + len)`, left in the hash value `h` (8
4625
+ * words), on the caller's schedule `w`, round constants `kt` and `tail` (128
4626
+ * bytes, where the last part block is padded as §5.1.1 says). Whole blocks are
4627
+ * compressed where they lie, so the message is never copied. Answers `false`,
4628
+ * having hashed nothing, unless the work arrays have the sizes
4629
+ * `new P256SignScratch()` gives them, since an importer can replace its
4630
+ * public fields.
4631
+ *
4632
+ * A window outside `data` is refused the same way, before any byte is read,
4633
+ * so it never hashes bytes `data` does not hold.
4473
4634
  */
4474
- const p256ShaInto = (
4475
- out: u8[],
4476
- at: i32,
4477
- data: u8[],
4478
- off: i32,
4479
- len: i32,
4480
- kt: u32[],
4481
- h: u32[],
4482
- w: u32[],
4483
- tail: u8[]
4484
- ): void => {
4635
+ const p256ShaInto = (data: u8[], off: i32, len: i32, kt: u32[], h: u32[], w: u32[], tail: u8[]): boolean => {
4636
+ if (
4637
+ toI32(kt.length) !== 64 ||
4638
+ toI32(h.length) !== 8 ||
4639
+ toI32(w.length) !== 64 ||
4640
+ toI32(tail.length) !== 128 ||
4641
+ off < 0 ||
4642
+ len < 0 ||
4643
+ toI64(off) + toI64(len) > toI64(data.length)
4644
+ ) {
4645
+ return false
4646
+ }
4485
4647
  p256SetLimbs(
4486
4648
  h,
4487
4649
  0x6a09e667,
@@ -4493,45 +4655,60 @@ const p256ShaInto = (
4493
4655
  0x1f83d9ab,
4494
4656
  0x5be0cd19
4495
4657
  )
4496
- let from: i32 = off
4497
- while (len - (from - off) >= SHA256_BLOCK) {
4498
- p256ShaCompress(h, w, kt, data, from)
4499
- from = from + SHA256_BLOCK
4658
+ // Whole blocks: len >> 6 of them (SHA256_BLOCK is 64), at most 2^25, so
4659
+ // each start, off + 64 b, is below off + len, inside `data`.
4660
+ const blocks: i32 = len >> 6
4661
+ for (let b: i32 = 0; b < blocks; b++) {
4662
+ p256ShaCompress(h, w, kt, data, toI32(toI64(off) + toI64(b) * 64))
4500
4663
  }
4501
- const rest: i32 = len - (from - off)
4502
- const tailLength: i32 = toI32(tail.length)
4503
- const dataLength: i32 = toI32(data.length)
4504
- for (let i: i32 = 0; i < tailLength; i++) {
4664
+ const rest: i32 = len & 63
4665
+ for (let i: i32 = 0; i < 128; i++) {
4505
4666
  tail[i] = 0
4506
4667
  }
4507
- for (let i: i32 = 0; i < rest && i < tailLength && from + i >= 0 && from + i < dataLength; i++) {
4508
- tail[i] = data[from + i]
4668
+ // The part block is data[off + len - rest .. off + len), inside `data` by
4669
+ // the window check above, so the loop's bounds on `k` never end it early.
4670
+ let copied: i32 = 0
4671
+ const dataLength: i32 = toI32(data.length)
4672
+ for (
4673
+ let k: i32 = toI32(toI64(off) + toI64(len) - toI64(rest));
4674
+ k >= 0 && k < dataLength && copied < rest;
4675
+ k++
4676
+ ) {
4677
+ tail[copied] = data[k]
4678
+ copied++
4509
4679
  }
4510
- if (rest >= 0 && rest < tailLength) {
4511
- tail[rest] = 0x80
4680
+ tail[rest] = 0x80
4681
+ // One padded block, or two when the 8-byte length no longer fits (rest >= 56).
4682
+ let end: i32 = 128
4683
+ if (rest < 56) {
4684
+ end = 64
4512
4685
  }
4513
- const end: i32 = rest < SHA256_BLOCK - 8 ? SHA256_BLOCK : 2 * SHA256_BLOCK
4514
- const bits: u64 = toU64(len) << 3
4686
+ const bits: u64 = toU64(toU32(len)) << 3
4515
4687
  for (let i: i32 = 0; i < 8; i++) {
4516
4688
  const j: i32 = end - 1 - i
4517
- if (j >= 0 && j < tailLength) {
4518
- tail[j] = toU8(bits >>> toU64(8 * i))
4519
- }
4689
+ tail[j] = toU8(bits >>> toU64(8 * i))
4520
4690
  }
4521
4691
  p256ShaCompress(h, w, kt, tail, 0)
4522
- if (end > SHA256_BLOCK) {
4523
- p256ShaCompress(h, w, kt, tail, SHA256_BLOCK)
4692
+ if (end > 64) {
4693
+ p256ShaCompress(h, w, kt, tail, 64)
4524
4694
  }
4525
- const outLength: i32 = toI32(out.length)
4526
- for (let i: i32 = 0; i < 8 && i < toI32(h.length); i++) {
4695
+ return true
4696
+ }
4697
+
4698
+ /** The digest `p256ShaInto` left in `h`, big-endian, into `out[0 .. 32)`; `false`, writing nothing, for another size. */
4699
+ const p256DigestInto = (out: u8[], h: u32[]): boolean => {
4700
+ if (toI32(out.length) !== 32 || toI32(h.length) !== 8) {
4701
+ return false
4702
+ }
4703
+ for (let i: i32 = 0; i < 8; i++) {
4527
4704
  const word: u32 = h[i]
4528
4705
  for (let b: i32 = 0; b < 4; b++) {
4529
- const j: i32 = at + 4 * i + b
4530
- if (j >= 0 && j < outLength) {
4531
- out[j] = toU8(word >>> toU32(P256_WORD_TOP - 8 * b))
4532
- }
4706
+ const j: i32 = 4 * i + b
4707
+ const shift: i32 = 8 * b
4708
+ out[j] = toU8(word >>> toU32(24 - shift))
4533
4709
  }
4534
4710
  }
4711
+ return true
4535
4712
  }
4536
4713
 
4537
4714
  /**
@@ -4540,6 +4717,10 @@ const p256ShaInto = (
4540
4717
  * its digest put where the outer hash of `K XOR opad || inner` reads it. `out`
4541
4718
  * may be `key` or `message`, which are read first. Every work array the key
4542
4719
  * reached — `block`, `h`, `w` and `tail` — is wiped before it returns (ECC-2).
4720
+ * Answers `false`, writing nothing, unless the arrays have the sizes
4721
+ * `new P256SignScratch()` gives them: an importer can replace the scratch's
4722
+ * public fields, and the signer stops on the refusal rather than go on with a
4723
+ * `K` or `V` this call did not compute.
4543
4724
  */
4544
4725
  const p256HmacInto = (
4545
4726
  out: u8[],
@@ -4551,39 +4732,66 @@ const p256HmacInto = (
4551
4732
  w: u32[],
4552
4733
  tail: u8[],
4553
4734
  block: u8[]
4554
- ): void => {
4555
- const blockLength: i32 = toI32(block.length)
4556
- const keyLength: i32 = toI32(key.length)
4557
- const messageLength: i32 = toI32(message.length)
4558
- if (blockLength < SHA256_BLOCK + len || keyLength > SHA256_BLOCK || len > messageLength) {
4559
- panic("p256HmacInto: a key, message or block outside its bounds")
4735
+ ): boolean => {
4736
+ if (toI32(out.length) !== 32 || toI32(key.length) !== 32 || toI32(kt.length) !== 64) {
4737
+ return false
4738
+ }
4739
+ if (toI32(h.length) !== 8 || toI32(w.length) !== 64 || toI32(tail.length) !== 128) {
4740
+ return false
4741
+ }
4742
+ if (toI32(block.length) !== 161 || len < 0 || len > 97 || len > toI32(message.length)) {
4743
+ return false
4744
+ }
4745
+ for (let i: i32 = 0; i < 32; i++) {
4746
+ block[i] = key[i] ^ 0x36
4747
+ }
4748
+ for (let i: i32 = 32; i < 64; i++) {
4749
+ block[i] = 0x36
4750
+ }
4751
+ for (let i: i32 = 0; i < len; i++) {
4752
+ const k: i32 = 64 + i
4753
+ block[k] = message[i]
4560
4754
  }
4561
- for (let i: i32 = 0; i < SHA256_BLOCK && i < blockLength; i++) {
4562
- block[i] = i < keyLength ? key[i] ^ 0x36 : 0x36
4755
+ const inner: boolean = p256ShaInto(block, P256_FROM, 64 + len, kt, h, w, tail)
4756
+ // The inner digest follows the padded key, where the outer hash reads it.
4757
+ for (let i: i32 = 0; i < 8; i++) {
4758
+ const word: u32 = h[i]
4759
+ for (let b: i32 = 0; b < 4; b++) {
4760
+ const four: i32 = 4 * i
4761
+ const j: i32 = 64 + four + b
4762
+ const shift: i32 = 8 * b
4763
+ block[j] = toU8(word >>> toU32(24 - shift))
4764
+ }
4563
4765
  }
4564
- for (let i: i32 = 0; i < len && i < messageLength && SHA256_BLOCK + i < blockLength; i++) {
4565
- block[SHA256_BLOCK + i] = message[i]
4766
+ for (let i: i32 = 0; i < 32; i++) {
4767
+ block[i] = key[i] ^ 0x5c
4566
4768
  }
4567
- p256ShaInto(block, SHA256_BLOCK, block, P256_FROM, SHA256_BLOCK + len, kt, h, w, tail)
4568
- for (let i: i32 = 0; i < SHA256_BLOCK && i < blockLength; i++) {
4569
- block[i] = i < keyLength ? key[i] ^ 0x5c : 0x5c
4769
+ for (let i: i32 = 32; i < 64; i++) {
4770
+ block[i] = 0x5c
4570
4771
  }
4571
- p256ShaInto(out, P256_FROM, block, P256_FROM, SHA256_BLOCK + SHA256_SIZE, kt, h, w, tail)
4772
+ // The outer message: the 64-byte padded key, then the 32-byte inner digest.
4773
+ const outerLength: i32 = 96
4774
+ const outer: boolean = p256ShaInto(block, P256_FROM, outerLength, kt, h, w, tail)
4775
+ const done: boolean = p256DigestInto(out, h)
4572
4776
  wipe(block)
4573
4777
  wipe(h)
4574
4778
  wipe(w)
4575
4779
  wipe(tail)
4780
+ // Each hash's window lies inside the 161-byte block, so neither refuses here;
4781
+ // the answer still carries their refusals rather than drop them.
4782
+ return inner && outer && done
4576
4783
  }
4577
4784
 
4578
- /** `out[0 .. n) = from[0 .. n)`. */
4579
- const p256CopyBytes = (out: u8[], from: u8[], n: i32): void => {
4580
- for (let i: i32 = 0; i < n && i < toI32(out.length) && i < toI32(from.length); i++) {
4581
- out[i] = from[i]
4785
+ /** Every byte of `out` set to `value`. */
4786
+ const p256FillBytes = (out: u8[], value: u8): void => {
4787
+ for (let i: i32 = 0; i < toI32(out.length); i++) {
4788
+ out[i] = value
4582
4789
  }
4583
4790
  }
4584
4791
 
4585
4792
  /** Whether `[at, at + len)` is a window inside an array of `length` elements. */
4586
- const p256WindowFits = (length: i32, at: i32, len: i32): boolean => at >= 0 && len >= 0 && at <= length - len
4793
+ const p256WindowFits = (length: i32, at: i32, len: i32): boolean =>
4794
+ at >= 0 && len >= 0 && toI64(at) <= toI64(length) - toI64(len)
4587
4795
 
4588
4796
  /**
4589
4797
  * Everything `p256SignSha256Into` signs in, made once and reused: RFC 6979's
@@ -4727,9 +4935,10 @@ export class P256SignScratch {
4727
4935
  ]
4728
4936
  this.hash = p256Limbs()
4729
4937
  this.schedule = new Array<u32>(64)
4730
- this.tail = new Array<u8>(2 * SHA256_BLOCK)
4731
- this.block = new Array<u8>(SHA256_BLOCK + P256_NONCE_MESSAGE)
4732
- this.message = new Array<u8>(SHA256_SIZE + 1)
4938
+ // 2 * SHA256_BLOCK, SHA256_BLOCK + P256_NONCE_MESSAGE and SHA256_SIZE + 1.
4939
+ this.tail = new Array<u8>(128)
4940
+ this.block = new Array<u8>(161)
4941
+ this.message = new Array<u8>(33)
4733
4942
  this.k = new Array<u8>(SHA256_SIZE)
4734
4943
  this.v = new Array<u8>(SHA256_SIZE)
4735
4944
  this.h1 = new Array<u8>(SHA256_SIZE)
@@ -4745,29 +4954,36 @@ export class P256SignScratch {
4745
4954
  }
4746
4955
  }
4747
4956
 
4748
- /** `out = HMAC_K(message[0 .. len))` with the scratch's `K` and work arrays. */
4749
- const p256SignMac = (sc: P256SignScratch, out: u8[], message: u8[], len: i32): void =>
4957
+ /** `out = HMAC_K(message[0 .. len))` with the scratch's `K` and work arrays; `false` as `p256HmacInto` answers it. */
4958
+ const p256SignMac = (sc: P256SignScratch, out: u8[], message: u8[], len: i32): boolean =>
4750
4959
  p256HmacInto(out, sc.k, message, len, sc.kt, sc.hash, sc.schedule, sc.tail, sc.block)
4751
4960
 
4752
4961
  /**
4753
4962
  * HMAC_K(V || sep || x || h1) for RFC 6979 §3.2 steps d and f, on the plain
4754
4963
  * key `x`, for `exposeWith` to run: `V || sep` is the scratch's `message`,
4755
4964
  * which is only read, as the rest of the scratch is; the message and the
4756
- * hashing's work arrays are fresh and wiped, and the new `K` is the answer.
4965
+ * hashing's work arrays are fresh and wiped, and the new `K` is the answer,
4966
+ * or an empty array when `p256HmacInto` refuses its arrays.
4757
4967
  */
4758
4968
  const p256NonceKeyExposed = (priv: u8[], sc: P256SignScratch): u8[] => {
4759
- const message: u8[] = new Array<u8>(P256_NONCE_MESSAGE)
4760
- p256CopyBytes(message, sc.message, SHA256_SIZE + 1)
4761
- for (let i: i32 = 0; i < SHA256_SIZE && i < toI32(priv.length) && i < toI32(sc.h1.length); i++) {
4762
- const j: i32 = SHA256_SIZE + 1 + i
4763
- const l: i32 = j + SHA256_SIZE
4764
- if (j >= 0 && l >= 0 && j < toI32(message.length) && l < toI32(message.length)) {
4765
- message[j] = priv[i]
4766
- message[l] = sc.h1[i]
4767
- }
4969
+ const separated: u8[] = sc.message
4970
+ const h1: u8[] = sc.h1
4971
+ if (toI32(priv.length) !== 32 || toI32(separated.length) !== 33 || toI32(h1.length) !== 32) {
4972
+ return new Array<u8>(0)
4973
+ }
4974
+ const message: u8[] = new Array<u8>(97)
4975
+ for (let i: i32 = 0; i < 33; i++) {
4976
+ message[i] = separated[i]
4977
+ }
4978
+ // V || sep is 33 bytes; the key, then h1, follow it.
4979
+ for (let i: i32 = 0; i < 32; i++) {
4980
+ const j: i32 = 33 + i
4981
+ const l: i32 = 65 + i
4982
+ message[j] = priv[i]
4983
+ message[l] = h1[i]
4768
4984
  }
4769
4985
  const key: u8[] = new Array<u8>(SHA256_SIZE)
4770
- p256HmacInto(
4986
+ const made: boolean = p256HmacInto(
4771
4987
  key,
4772
4988
  sc.k,
4773
4989
  message,
@@ -4775,10 +4991,14 @@ const p256NonceKeyExposed = (priv: u8[], sc: P256SignScratch): u8[] => {
4775
4991
  sc.kt,
4776
4992
  p256Limbs(),
4777
4993
  new Array<u32>(64),
4778
- new Array<u8>(2 * SHA256_BLOCK),
4779
- new Array<u8>(SHA256_BLOCK + P256_NONCE_MESSAGE)
4994
+ new Array<u8>(128),
4995
+ new Array<u8>(161)
4780
4996
  )
4781
4997
  wipe(message)
4998
+ if (!made) {
4999
+ // A refusal wrote nothing, so `key` still holds its zeros.
5000
+ return new Array<u8>(0)
5001
+ }
4782
5002
  return key
4783
5003
  }
4784
5004
 
@@ -4787,11 +5007,14 @@ const p256NonceKeyExposed = (priv: u8[], sc: P256SignScratch): u8[] => {
4787
5007
  * with `p256SignScalar`'s arithmetic: `d` taken into the Montgomery domain,
4788
5008
  * multiplied by the plain `r`, `z` added, and the sum multiplied by `k^-1`,
4789
5009
  * which is in the Montgomery domain, so `s` comes out plain. The key's limbs
4790
- * are wiped; `s` is half of the signature, and public.
5010
+ * are wiped; `s` is half of the signature, and public. Answers `null` for a
5011
+ * key too short to hold a scalar.
4791
5012
  */
4792
- const p256SignSExposed = (priv: u8[], sc: P256SignScratch): u32[] => {
5013
+ const p256SignSExposed = (priv: u8[], sc: P256SignScratch): u32[] | null => {
4793
5014
  const dM: u32[] = p256Limbs()
4794
- p256LimbsFromBytes(dM, priv, 0)
5015
+ if (!p256LimbsFromBytes(dM, priv, 0)) {
5016
+ return null
5017
+ }
4795
5018
  p256FiatScalarMul(dM, dM, sc.r2)
4796
5019
  const s: u32[] = p256Limbs()
4797
5020
  p256FiatScalarMul(s, sc.r, dM)
@@ -4801,18 +5024,73 @@ const p256SignSExposed = (priv: u8[], sc: P256SignScratch): u32[] => {
4801
5024
  return s
4802
5025
  }
4803
5026
 
4804
- /** `sc.message = V || sep`, the start of every key update's message (RFC 6979 §3.2 steps d, f and h.3). */
4805
- const p256NonceSeparated = (sc: P256SignScratch, sep: u8): void => {
4806
- p256CopyBytes(sc.message, sc.v, SHA256_SIZE)
4807
- sc.message[SHA256_SIZE] = sep
5027
+ /**
5028
+ * `sc.message = V || sep`, the start of every key update's message (RFC 6979
5029
+ * §3.2 steps d, f and h.3); `false`, writing nothing, for a `message` or `V`
5030
+ * an importer resized.
5031
+ */
5032
+ const p256NonceSeparated = (sc: P256SignScratch, sep: u8): boolean => {
5033
+ const message: u8[] = sc.message
5034
+ const v: u8[] = sc.v
5035
+ if (toI32(message.length) !== 33 || toI32(v.length) !== 32) {
5036
+ return false
5037
+ }
5038
+ for (let i: i32 = 0; i < 32; i++) {
5039
+ message[i] = v[i]
5040
+ }
5041
+ message[32] = sep
5042
+ return true
4808
5043
  }
4809
5044
 
4810
- /** Steps d and f: `K` replaced by what the exposed step answers, which is wiped once copied. */
4811
- const p256SignNonceKey = (sc: P256SignScratch, priv: Secret<u8[]>, sep: u8): void => {
4812
- p256NonceSeparated(sc, sep)
5045
+ /**
5046
+ * Steps d and f: `K` replaced by what the exposed step answers, which is wiped
5047
+ * once copied; `false`, `K` untouched, when that step answered no key.
5048
+ */
5049
+ const p256SignNonceKey = (sc: P256SignScratch, priv: Secret<u8[]>, sep: u8): boolean => {
5050
+ if (!p256NonceSeparated(sc, sep)) {
5051
+ return false
5052
+ }
4813
5053
  const next: u8[] = exposeWith(priv, sc, p256NonceKeyExposed)
4814
- p256CopyBytes(sc.k, next, SHA256_SIZE)
5054
+ const k: u8[] = sc.k
5055
+ let made: boolean = false
5056
+ if (toI32(next.length) === 32 && toI32(k.length) === 32) {
5057
+ for (let i: i32 = 0; i < 32; i++) {
5058
+ k[i] = next[i]
5059
+ }
5060
+ made = true
5061
+ }
4815
5062
  wipe(next)
5063
+ return made
5064
+ }
5065
+
5066
+ /** Whether each of `p`'s coordinates holds 8 limbs. */
5067
+ const p256PointSized = (p: P256ProjectivePoint): boolean =>
5068
+ toI32(p.x.length) === 8 && toI32(p.y.length) === 8 && toI32(p.z.length) === 8
5069
+
5070
+ /**
5071
+ * Whether every limb array the signing loop computes in holds 8 limbs. The
5072
+ * field arithmetic reads and writes as many limbs as an array has, so a
5073
+ * resized one would compute a wrong `r` or `s` and sign with it; the signer
5074
+ * refuses such a scratch first.
5075
+ */
5076
+ const p256SignScratchSized = (sc: P256SignScratch): boolean => {
5077
+ const s: P256PointScratch = sc.points
5078
+ const scalars: boolean =
5079
+ toI32(sc.z.length) === 8 &&
5080
+ toI32(sc.r.length) === 8 &&
5081
+ toI32(sc.x.length) === 8 &&
5082
+ toI32(sc.kM.length) === 8 &&
5083
+ toI32(sc.n.length) === 8 &&
5084
+ toI32(sc.r2.length) === 8
5085
+ const points: boolean =
5086
+ toI32(s.t0.length) === 8 &&
5087
+ toI32(s.t1.length) === 8 &&
5088
+ toI32(s.t2.length) === 8 &&
5089
+ toI32(s.t3.length) === 8 &&
5090
+ toI32(s.t4.length) === 8 &&
5091
+ toI32(s.comb.length) === 8 &&
5092
+ toI32(s.b.length) === 8
5093
+ return scalars && points && p256PointSized(sc.point) && p256PointSized(s.sum) && p256PointSized(s.entry)
4816
5094
  }
4817
5095
 
4818
5096
  /** Zero every secret the scratch held: `K`, `V`, `k^-1`, the nonce point and its scratch (ECC-2). */
@@ -4844,7 +5122,10 @@ const p256WipeSignScratch = (sc: P256SignScratch): void => {
4844
5122
  * @param len The message's length in bytes.
4845
5123
  * @param out The array the signature is written into.
4846
5124
  * @param at Where the 64 bytes start in `out`.
4847
- * @returns `true` once the signature is written, `false` for a refused key.
5125
+ * @returns `true` once the signature is written, `false` for a refused key,
5126
+ * for a scratch whose arrays a hashing step refuses (which
5127
+ * `new P256SignScratch()` never builds), or when RFC 6979 finds no usable
5128
+ * nonce within 64 rounds.
4848
5129
  */
4849
5130
  export const p256SignSha256Into = (
4850
5131
  sc: P256SignScratch,
@@ -4870,37 +5151,70 @@ export const p256SignSha256Into = (
4870
5151
  if (!expose(priv, p256ScalarInRange)) {
4871
5152
  return false
4872
5153
  }
4873
- p256ShaInto(sc.h1, P256_FROM, msg, off, len, sc.kt, sc.hash, sc.schedule, sc.tail)
4874
- p256DigestScalar(sc.z, sc.h1)
4875
- p256LimbsToBytes(sc.h1, 0, sc.z)
4876
- for (let i: i32 = 0; i < SHA256_SIZE && i < toI32(sc.v.length) && i < toI32(sc.k.length); i++) {
4877
- sc.v[i] = 1
4878
- sc.k[i] = 0
5154
+ // Each hashing step answers `false` only for a scratch whose arrays an
5155
+ // importer resized; the signer then stops rather than derive a nonce from
5156
+ // stale state. Every way out passes the one wipe below.
5157
+ // A resized `h1` or `z`, or a limb array the loop computes in, fails a step
5158
+ // here, and the signer answers false through the one wipe below, as every
5159
+ // later step's refusal does.
5160
+ let ok: boolean =
5161
+ p256SignScratchSized(sc) &&
5162
+ p256ShaInto(msg, off, len, sc.kt, sc.hash, sc.schedule, sc.tail) &&
5163
+ p256DigestInto(sc.h1, sc.hash) &&
5164
+ p256DigestScalar(sc.z, sc.h1) &&
5165
+ p256LimbsToBytes(sc.h1, 0, sc.z)
5166
+ if (ok) {
5167
+ p256FillBytes(sc.v, 1)
5168
+ p256FillBytes(sc.k, 0)
4879
5169
  }
4880
- p256SignNonceKey(sc, priv, 0) // step d
4881
- p256SignMac(sc, sc.v, sc.v, SHA256_SIZE) // step e
4882
- p256SignNonceKey(sc, priv, 1) // step f
4883
- p256SignMac(sc, sc.v, sc.v, SHA256_SIZE) // step g
4884
- while (true) {
4885
- p256SignMac(sc, sc.v, sc.v, SHA256_SIZE) // step h.2
4886
- if (p256ScalarInRange(sc.v)) {
4887
- p256BaseMultWith(sc.point, sc.v, sc.points, sc.table)
4888
- p256AffineX(sc.x, sc.point)
4889
- p256ReduceOnce(sc.r, sc.x, sc.n)
4890
- p256LimbsFromBytes(sc.kM, sc.v, 0)
4891
- p256FiatScalarMul(sc.kM, sc.kM, sc.r2)
4892
- p256ScalarInverse(sc.kM, sc.kM)
4893
- const s: u32[] = exposeWith(priv, sc, p256SignSExposed)
4894
- if (p256FiatNonzero(sc.r) !== 0 && p256FiatNonzero(s) !== 0) {
4895
- p256LimbsToBytes(out, at, sc.r)
4896
- p256LimbsToBytes(out, at + 32, s)
4897
- p256WipeSignScratch(sc)
4898
- return true
5170
+ ok =
5171
+ ok &&
5172
+ p256SignNonceKey(sc, priv, 0) && // step d
5173
+ p256SignMac(sc, sc.v, sc.v, SHA256_SIZE) && // step e
5174
+ p256SignNonceKey(sc, priv, 1) && // step f
5175
+ p256SignMac(sc, sc.v, sc.v, SHA256_SIZE) // step g
5176
+ let signed: boolean = false
5177
+ let rounds: i32 = 0
5178
+ while (ok && !signed) {
5179
+ ok = p256SignMac(sc, sc.v, sc.v, SHA256_SIZE) // step h.2
5180
+ if (ok && p256ScalarInRange(sc.v)) {
5181
+ ok = p256BaseMultWith(sc.point, sc.v, sc.points, sc.table)
5182
+ if (ok) {
5183
+ p256AffineX(sc.x, sc.point)
5184
+ p256ReduceOnce(sc.r, sc.x, sc.n)
5185
+ ok = p256LimbsFromBytes(sc.kM, sc.v, 0)
5186
+ }
5187
+ if (ok) {
5188
+ p256FiatScalarMul(sc.kM, sc.kM, sc.r2)
5189
+ p256ScalarInverse(sc.kM, sc.kM)
5190
+ const s: u32[] | null = exposeWith(priv, sc, p256SignSExposed)
5191
+ if (s === null) {
5192
+ ok = false
5193
+ } else if (p256FiatNonzero(sc.r) !== 0 && p256FiatNonzero(s) !== 0) {
5194
+ // The window was checked: at + 64 fits in `out`, an i32.
5195
+ ok = p256LimbsToBytes(out, at, sc.r) && p256LimbsToBytes(out, toI32(toI64(at) + 32), s)
5196
+ signed = ok
5197
+ }
5198
+ }
5199
+ }
5200
+ if (ok && !signed) {
5201
+ // A sound scratch retries with chance under 2^-31 a round, so the cap
5202
+ // is never met by one: a limb array or constant an importer resized or
5203
+ // overwrote can make r or s zero on every round, and is refused here
5204
+ // rather than looped on.
5205
+ if (rounds < P256_SIGN_ROUNDS) {
5206
+ rounds += 1
5207
+ } else {
5208
+ ok = false
4899
5209
  }
5210
+ // step h.3: K = HMAC_K(V || 0x00), then V = HMAC_K(V).
5211
+ ok =
5212
+ ok &&
5213
+ p256NonceSeparated(sc, 0) &&
5214
+ p256SignMac(sc, sc.k, sc.message, SHA256_SIZE + 1) &&
5215
+ p256SignMac(sc, sc.v, sc.v, SHA256_SIZE)
4900
5216
  }
4901
- // step h.3: K = HMAC_K(V || 0x00), then V = HMAC_K(V).
4902
- p256NonceSeparated(sc, 0)
4903
- p256SignMac(sc, sc.k, sc.message, SHA256_SIZE + 1)
4904
- p256SignMac(sc, sc.v, sc.v, SHA256_SIZE)
4905
5217
  }
5218
+ p256WipeSignScratch(sc)
5219
+ return signed
4906
5220
  }