nish 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 (125) hide show
  1. package/README.md +11 -5
  2. package/docs/AI.md +287 -129
  3. package/package.json +9 -9
  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/skills/writing-nish/SKILL.md +12 -4
  21. package/std/README.md +6 -6
  22. package/std/array.ts +10 -2
  23. package/std/bytes.ts +239 -26
  24. package/std/cli-args.ts +1072 -606
  25. package/std/cli-flags.ts +180 -98
  26. package/std/cli.ts +21 -13
  27. package/std/collections.ts +245 -127
  28. package/std/compress/checksum.ts +32 -37
  29. package/std/compress/deflate.ts +1102 -542
  30. package/std/compress/gzip.ts +290 -145
  31. package/std/compress/zlib.ts +57 -21
  32. package/std/crypto/aes.ts +153 -75
  33. package/std/crypto/base64url.ts +163 -39
  34. package/std/crypto/bcrypt-batch.ts +164 -90
  35. package/std/crypto/bcrypt.ts +185 -107
  36. package/std/crypto/chacha20poly1305.ts +84 -68
  37. package/std/crypto/ct.ts +9 -7
  38. package/std/crypto/ed25519.ts +64 -27
  39. package/std/crypto/hkdf.ts +105 -63
  40. package/std/crypto/hmac.ts +95 -89
  41. package/std/crypto/jwt.ts +248 -126
  42. package/std/crypto/p256.ts +758 -444
  43. package/std/crypto/p384.ts +20 -13
  44. package/std/crypto/pki-roots.ts +95 -38
  45. package/std/crypto/pki.ts +164 -60
  46. package/std/crypto/rsa.ts +266 -223
  47. package/std/crypto/scram.ts +141 -0
  48. package/std/crypto/sha1.ts +157 -57
  49. package/std/crypto/sha256.ts +502 -422
  50. package/std/crypto/sha512.ts +204 -135
  51. package/std/crypto/x509-random.ts +9 -3
  52. package/std/crypto/x509.ts +331 -171
  53. package/std/host/ring.ts +1 -1
  54. package/std/host/text.ts +1 -1
  55. package/std/json-equal.ts +35 -19
  56. package/std/json-format.ts +215 -108
  57. package/std/json-number.ts +126 -72
  58. package/std/json-pointer.ts +42 -13
  59. package/std/json-tokenizer.ts +498 -307
  60. package/std/json-typed.ts +115 -36
  61. package/std/json-value.ts +341 -152
  62. package/std/json.ts +300 -158
  63. package/std/net/buffer-pool.ts +17 -7
  64. package/std/net/dns-config.ts +427 -236
  65. package/std/net/dns-message.ts +114 -47
  66. package/std/net/dns-resolver.ts +736 -237
  67. package/std/net/dns.ts +205 -52
  68. package/std/net/fetch.ts +320 -184
  69. package/std/net/grpc.ts +948 -0
  70. package/std/net/handler.ts +1276 -218
  71. package/std/net/hpack.ts +395 -180
  72. package/std/net/http-compress.ts +502 -65
  73. package/std/net/http-fields.ts +85 -49
  74. package/std/net/http-server.ts +1764 -603
  75. package/std/net/http1-client-codec.ts +372 -161
  76. package/std/net/http1-codec.ts +665 -331
  77. package/std/net/http1-server.ts +881 -269
  78. package/std/net/http2-cleartext.ts +165 -95
  79. package/std/net/http2-frame.ts +232 -89
  80. package/std/net/http2-tls.ts +42 -12
  81. package/std/net/http2.ts +327 -101
  82. package/std/net/http3-frame.ts +68 -33
  83. package/std/net/http3-server.ts +672 -206
  84. package/std/net/http3.ts +669 -191
  85. package/std/net/oidc.ts +63 -24
  86. package/std/net/outbound.ts +207 -142
  87. package/std/net/postgres-async.ts +1223 -470
  88. package/std/net/postgres-carrier.ts +575 -372
  89. package/std/net/postgres.ts +1521 -632
  90. package/std/net/prefork.ts +254 -98
  91. package/std/net/protobuf.ts +1017 -0
  92. package/std/net/qpack.ts +143 -92
  93. package/std/net/quic-conn-ack.ts +104 -22
  94. package/std/net/quic-conn-cid.ts +66 -26
  95. package/std/net/quic-conn-params.ts +13 -7
  96. package/std/net/quic-datagram.ts +90 -27
  97. package/std/net/quic-frame.ts +324 -121
  98. package/std/net/quic-listener.ts +206 -91
  99. package/std/net/quic-packet.ts +289 -163
  100. package/std/net/quic-recovery.ts +279 -128
  101. package/std/net/quic-stream.ts +466 -167
  102. package/std/net/quic.ts +584 -258
  103. package/std/net/router.ts +450 -99
  104. package/std/net/serve.ts +661 -240
  105. package/std/net/static.ts +1458 -0
  106. package/std/net/tls/client.ts +140 -45
  107. package/std/net/tls/codec.ts +510 -260
  108. package/std/net/tls/record-client.ts +268 -76
  109. package/std/net/tls/record-server.ts +236 -79
  110. package/std/net/tls/record.ts +173 -47
  111. package/std/net/tls/schedule.ts +58 -39
  112. package/std/net/tls/ticket.ts +295 -103
  113. package/std/net/tls/verify.ts +1 -1
  114. package/std/net/tls-tcp.ts +218 -75
  115. package/std/net/tls.ts +260 -152
  116. package/std/net/upstream.ts +283 -86
  117. package/std/net/url.ts +277 -102
  118. package/std/net/websocket.ts +532 -209
  119. package/std/net/webtransport.ts +755 -436
  120. package/std/regex.ts +1659 -829
  121. package/std/sqlite.ts +325 -0
  122. package/std/sync/ring.ts +320 -156
  123. package/std/sync/shm.ts +53 -6
  124. package/std/text.ts +285 -135
  125. package/std/threads.ts +22 -2
@@ -29,8 +29,9 @@
29
29
  *
30
30
  * Every read is checked: nothing here imports `nish:unsafe`, so hashing never
31
31
  * makes a caller reach `unsafe`, which `--deny unsafe` refuses (the SCRAM in
32
- * `std/net/postgres.ts` is one such caller). `sha256Compress` instead orders
33
- * its reads so that the optimiser can fold a block's bounds checks into one.
32
+ * `std/net/postgres.ts` is one such caller). Each read's index is instead
33
+ * proven in range by the condition the loop or block it sits in runs under, so
34
+ * `sha256Compress` reads a block with no bounds check at all.
34
35
  *
35
36
  * Written from the specification, not ported from another implementation.
36
37
  */
@@ -70,33 +71,6 @@ const sha256Choose = (x: u32, y: u32, z: u32): u32 => z ^ (x & (y ^ z))
70
71
  */
71
72
  const sha256Majority = (x: u32, y: u32, z: u32): u32 => (x & y) ^ (z & (x ^ y))
72
73
 
73
- /**
74
- * The big-endian word at `buf[p .. p + 4)`, as FIPS 180-4 §3.1 orders bytes in
75
- * a word. `p`'s range makes each `p + k` an `add nsw`, with no overflow test.
76
- * The low byte, `buf[p + 3]`, is read first, so its bounds check also covers
77
- * the three reads after it and LLVM drops theirs; once a check further on in
78
- * the buffer covers that one too, nothing lies between the four reads and
79
- * LLVM joins them into one 32-bit load and a byte swap.
80
- *
81
- * The low byte is also the innermost `|` because σ1 shifts a word right by 10,
82
- * and when the low byte is the outermost operand LLVM's instcombine answers
83
- * that shift from the inner three bytes alone, which gives the half-built word
84
- * a second use and leaves words 14 and 15 as four byte loads each.
85
- */
86
- const sha256LoadWord = (buf: u8[], p: integer<0, 2147483643>): u32 =>
87
- toU32(buf[p + 3]) | (toU32(buf[p + 2]) << 8) | (toU32(buf[p + 1]) << 16) | (toU32(buf[p]) << 24)
88
-
89
- /**
90
- * Copies `from[fromAt .. fromAt + n)` to `to[toAt .. toAt + n)`. Every caller
91
- * has already checked both windows, so the indices here are in range; they
92
- * are computed from `k` so the loop has one counter.
93
- */
94
- const sha256CopyBytes = (from: u8[], fromAt: i32, to: u8[], toAt: i32, n: i32): void => {
95
- for (let k: i32 = 0; k < n; k += 1) {
96
- to[toAt + k] = from[fromAt + k]
97
- }
98
- }
99
-
100
74
  /**
101
75
  * Folds the 64-byte block at `src[at .. at + 64)` into the hash value `h`: the
102
76
  * computation of FIPS 180-4 §6.2.2, steps 1 to 4, for one block. `w` is the
@@ -112,309 +86,369 @@ const sha256CopyBytes = (from: u8[], fromAt: i32, to: u8[], toAt: i32, n: i32):
112
86
  * on, each round renames them, so round t + 1 reads as its `a` the variable
113
87
  * round t wrote as its `h`, and after eight rounds the names are back where
114
88
  * they started. Only `d` and `h` of a round are written: `d + T1` is the next
115
- * `e`, and `T1 + T2` the next `a`. The message schedule of step 1 is computed
116
- * one word ahead of the round that reads it, rather than all at once first.
89
+ * `e`, and `T1 + T2` the next `a`. The block's sixteen words are read first,
90
+ * together, and the rest of the message schedule of step 1 is computed one word
91
+ * ahead of the round that reads it.
117
92
  */
118
93
  const sha256Compress = (h: u32[], w: u32[], src: u8[], at: i32): void => {
119
- // The test is written as the condition the body runs under, not as an early
120
- // exit, because that is the shape that proves the constant `w[t]` and `h[i]`
121
- // indices in range and drops their bounds checks. Its last clause, which
122
- // the one before implies for any array an `i32` can index, is what puts
123
- // `at` in `base`'s range with no check.
124
- if (
125
- toI32(h.length) >= 8 &&
126
- toI32(w.length) >= 64 &&
127
- at >= 0 &&
128
- at <= toI32(src.length) - SHA256_BLOCK &&
129
- at <= 2147483583
130
- ) {
94
+ // The tests are written as the conditions the body runs under, not as early
95
+ // exits, because that is the shape that proves every index in range and drops
96
+ // its bounds check: the constant `w[t]` and `h[i]`, and each `src[base + k]`,
97
+ // `k < 64`, from `base < n - 63`. Every caller passes the hasher's own `h`
98
+ // and `w` and a block inside `src`, so they always hold. `at`'s upper bound,
99
+ // which `base < n - 63` implies for any array an `i32` can index, gives
100
+ // `base` a non-negative range, so each `base + k` is an `add nsw`. Each word
101
+ // reads its low byte first, as the innermost `|`: when the low byte is the
102
+ // outermost operand, LLVM's instcombine answers σ1's right shift by 10 from
103
+ // the inner three bytes alone, which gives the half-built word a second use
104
+ // and keeps it four byte loads instead of one load and a byte swap.
105
+ const n: i32 = toI32(src.length)
106
+ if (toI32(h.length) >= 8 && toI32(w.length) >= 64 && at >= 0 && at <= 2147483583) {
131
107
  const base: integer<0, 2147483583> = at
132
- // Word 15 is loaded first. Its first read, the block's last byte, is the
133
- // one bounds check of the block: the checker cannot prove `base + k` in
134
- // range from the guard, but LLVM sees that every other read of the block
135
- // lies below it and drops their checks, so words 0 to 14 are each one
136
- // load and a byte swap. Word 15 stays four byte loads, because its last
137
- // byte's index is the check's.
138
- w[15] = sha256LoadWord(src, base + 60)
139
- // Step 2: the working variables start from the previous hash value.
140
- let a: u32 = h[0]
141
- let b: u32 = h[1]
142
- let c: u32 = h[2]
143
- let d: u32 = h[3]
144
- let e: u32 = h[4]
145
- let f: u32 = h[5]
146
- let g: u32 = h[6]
147
- let hh: u32 = h[7]
148
- // T1 of step 3; T2 is added where it is computed.
149
- let t1: u32 = 0
150
- // Steps 1 and 3: rounds 0 to 15 read the block's sixteen words.
151
- w[0] = sha256LoadWord(src, base)
152
- t1 = hh + sha256BigSigma1(e) + sha256Choose(e, f, g) + 0x428a2f98 + w[0]
153
- d += t1
154
- hh = t1 + sha256BigSigma0(a) + sha256Majority(a, b, c)
155
- w[1] = sha256LoadWord(src, base + 4)
156
- t1 = g + sha256BigSigma1(d) + sha256Choose(d, e, f) + 0x71374491 + w[1]
157
- c += t1
158
- g = t1 + sha256BigSigma0(hh) + sha256Majority(hh, a, b)
159
- w[2] = sha256LoadWord(src, base + 8)
160
- t1 = f + sha256BigSigma1(c) + sha256Choose(c, d, e) + 0xb5c0fbcf + w[2]
161
- b += t1
162
- f = t1 + sha256BigSigma0(g) + sha256Majority(g, hh, a)
163
- w[3] = sha256LoadWord(src, base + 12)
164
- t1 = e + sha256BigSigma1(b) + sha256Choose(b, c, d) + 0xe9b5dba5 + w[3]
165
- a += t1
166
- e = t1 + sha256BigSigma0(f) + sha256Majority(f, g, hh)
167
- w[4] = sha256LoadWord(src, base + 16)
168
- t1 = d + sha256BigSigma1(a) + sha256Choose(a, b, c) + 0x3956c25b + w[4]
169
- hh += t1
170
- d = t1 + sha256BigSigma0(e) + sha256Majority(e, f, g)
171
- w[5] = sha256LoadWord(src, base + 20)
172
- t1 = c + sha256BigSigma1(hh) + sha256Choose(hh, a, b) + 0x59f111f1 + w[5]
173
- g += t1
174
- c = t1 + sha256BigSigma0(d) + sha256Majority(d, e, f)
175
- w[6] = sha256LoadWord(src, base + 24)
176
- t1 = b + sha256BigSigma1(g) + sha256Choose(g, hh, a) + 0x923f82a4 + w[6]
177
- f += t1
178
- b = t1 + sha256BigSigma0(c) + sha256Majority(c, d, e)
179
- w[7] = sha256LoadWord(src, base + 28)
180
- t1 = a + sha256BigSigma1(f) + sha256Choose(f, g, hh) + 0xab1c5ed5 + w[7]
181
- e += t1
182
- a = t1 + sha256BigSigma0(b) + sha256Majority(b, c, d)
183
- w[8] = sha256LoadWord(src, base + 32)
184
- t1 = hh + sha256BigSigma1(e) + sha256Choose(e, f, g) + 0xd807aa98 + w[8]
185
- d += t1
186
- hh = t1 + sha256BigSigma0(a) + sha256Majority(a, b, c)
187
- w[9] = sha256LoadWord(src, base + 36)
188
- t1 = g + sha256BigSigma1(d) + sha256Choose(d, e, f) + 0x12835b01 + w[9]
189
- c += t1
190
- g = t1 + sha256BigSigma0(hh) + sha256Majority(hh, a, b)
191
- w[10] = sha256LoadWord(src, base + 40)
192
- t1 = f + sha256BigSigma1(c) + sha256Choose(c, d, e) + 0x243185be + w[10]
193
- b += t1
194
- f = t1 + sha256BigSigma0(g) + sha256Majority(g, hh, a)
195
- w[11] = sha256LoadWord(src, base + 44)
196
- t1 = e + sha256BigSigma1(b) + sha256Choose(b, c, d) + 0x550c7dc3 + w[11]
197
- a += t1
198
- e = t1 + sha256BigSigma0(f) + sha256Majority(f, g, hh)
199
- w[12] = sha256LoadWord(src, base + 48)
200
- t1 = d + sha256BigSigma1(a) + sha256Choose(a, b, c) + 0x72be5d74 + w[12]
201
- hh += t1
202
- d = t1 + sha256BigSigma0(e) + sha256Majority(e, f, g)
203
- w[13] = sha256LoadWord(src, base + 52)
204
- t1 = c + sha256BigSigma1(hh) + sha256Choose(hh, a, b) + 0x80deb1fe + w[13]
205
- g += t1
206
- c = t1 + sha256BigSigma0(d) + sha256Majority(d, e, f)
207
- w[14] = sha256LoadWord(src, base + 56)
208
- t1 = b + sha256BigSigma1(g) + sha256Choose(g, hh, a) + 0x9bdc06a7 + w[14]
209
- f += t1
210
- b = t1 + sha256BigSigma0(c) + sha256Majority(c, d, e)
211
- t1 = a + sha256BigSigma1(f) + sha256Choose(f, g, hh) + 0xc19bf174 + w[15]
212
- e += t1
213
- a = t1 + sha256BigSigma0(b) + sha256Majority(b, c, d)
214
- // Rounds 16 to 63 extend the schedule one word ahead of the round that reads it.
215
- w[16] = sha256SmallSigma1(w[14]) + w[9] + sha256SmallSigma0(w[1]) + w[0]
216
- t1 = hh + sha256BigSigma1(e) + sha256Choose(e, f, g) + 0xe49b69c1 + w[16]
217
- d += t1
218
- hh = t1 + sha256BigSigma0(a) + sha256Majority(a, b, c)
219
- w[17] = sha256SmallSigma1(w[15]) + w[10] + sha256SmallSigma0(w[2]) + w[1]
220
- t1 = g + sha256BigSigma1(d) + sha256Choose(d, e, f) + 0xefbe4786 + w[17]
221
- c += t1
222
- g = t1 + sha256BigSigma0(hh) + sha256Majority(hh, a, b)
223
- w[18] = sha256SmallSigma1(w[16]) + w[11] + sha256SmallSigma0(w[3]) + w[2]
224
- t1 = f + sha256BigSigma1(c) + sha256Choose(c, d, e) + 0x0fc19dc6 + w[18]
225
- b += t1
226
- f = t1 + sha256BigSigma0(g) + sha256Majority(g, hh, a)
227
- w[19] = sha256SmallSigma1(w[17]) + w[12] + sha256SmallSigma0(w[4]) + w[3]
228
- t1 = e + sha256BigSigma1(b) + sha256Choose(b, c, d) + 0x240ca1cc + w[19]
229
- a += t1
230
- e = t1 + sha256BigSigma0(f) + sha256Majority(f, g, hh)
231
- w[20] = sha256SmallSigma1(w[18]) + w[13] + sha256SmallSigma0(w[5]) + w[4]
232
- t1 = d + sha256BigSigma1(a) + sha256Choose(a, b, c) + 0x2de92c6f + w[20]
233
- hh += t1
234
- d = t1 + sha256BigSigma0(e) + sha256Majority(e, f, g)
235
- w[21] = sha256SmallSigma1(w[19]) + w[14] + sha256SmallSigma0(w[6]) + w[5]
236
- t1 = c + sha256BigSigma1(hh) + sha256Choose(hh, a, b) + 0x4a7484aa + w[21]
237
- g += t1
238
- c = t1 + sha256BigSigma0(d) + sha256Majority(d, e, f)
239
- w[22] = sha256SmallSigma1(w[20]) + w[15] + sha256SmallSigma0(w[7]) + w[6]
240
- t1 = b + sha256BigSigma1(g) + sha256Choose(g, hh, a) + 0x5cb0a9dc + w[22]
241
- f += t1
242
- b = t1 + sha256BigSigma0(c) + sha256Majority(c, d, e)
243
- w[23] = sha256SmallSigma1(w[21]) + w[16] + sha256SmallSigma0(w[8]) + w[7]
244
- t1 = a + sha256BigSigma1(f) + sha256Choose(f, g, hh) + 0x76f988da + w[23]
245
- e += t1
246
- a = t1 + sha256BigSigma0(b) + sha256Majority(b, c, d)
247
- w[24] = sha256SmallSigma1(w[22]) + w[17] + sha256SmallSigma0(w[9]) + w[8]
248
- t1 = hh + sha256BigSigma1(e) + sha256Choose(e, f, g) + 0x983e5152 + w[24]
249
- d += t1
250
- hh = t1 + sha256BigSigma0(a) + sha256Majority(a, b, c)
251
- w[25] = sha256SmallSigma1(w[23]) + w[18] + sha256SmallSigma0(w[10]) + w[9]
252
- t1 = g + sha256BigSigma1(d) + sha256Choose(d, e, f) + 0xa831c66d + w[25]
253
- c += t1
254
- g = t1 + sha256BigSigma0(hh) + sha256Majority(hh, a, b)
255
- w[26] = sha256SmallSigma1(w[24]) + w[19] + sha256SmallSigma0(w[11]) + w[10]
256
- t1 = f + sha256BigSigma1(c) + sha256Choose(c, d, e) + 0xb00327c8 + w[26]
257
- b += t1
258
- f = t1 + sha256BigSigma0(g) + sha256Majority(g, hh, a)
259
- w[27] = sha256SmallSigma1(w[25]) + w[20] + sha256SmallSigma0(w[12]) + w[11]
260
- t1 = e + sha256BigSigma1(b) + sha256Choose(b, c, d) + 0xbf597fc7 + w[27]
261
- a += t1
262
- e = t1 + sha256BigSigma0(f) + sha256Majority(f, g, hh)
263
- w[28] = sha256SmallSigma1(w[26]) + w[21] + sha256SmallSigma0(w[13]) + w[12]
264
- t1 = d + sha256BigSigma1(a) + sha256Choose(a, b, c) + 0xc6e00bf3 + w[28]
265
- hh += t1
266
- d = t1 + sha256BigSigma0(e) + sha256Majority(e, f, g)
267
- w[29] = sha256SmallSigma1(w[27]) + w[22] + sha256SmallSigma0(w[14]) + w[13]
268
- t1 = c + sha256BigSigma1(hh) + sha256Choose(hh, a, b) + 0xd5a79147 + w[29]
269
- g += t1
270
- c = t1 + sha256BigSigma0(d) + sha256Majority(d, e, f)
271
- w[30] = sha256SmallSigma1(w[28]) + w[23] + sha256SmallSigma0(w[15]) + w[14]
272
- t1 = b + sha256BigSigma1(g) + sha256Choose(g, hh, a) + 0x06ca6351 + w[30]
273
- f += t1
274
- b = t1 + sha256BigSigma0(c) + sha256Majority(c, d, e)
275
- w[31] = sha256SmallSigma1(w[29]) + w[24] + sha256SmallSigma0(w[16]) + w[15]
276
- t1 = a + sha256BigSigma1(f) + sha256Choose(f, g, hh) + 0x14292967 + w[31]
277
- e += t1
278
- a = t1 + sha256BigSigma0(b) + sha256Majority(b, c, d)
279
- w[32] = sha256SmallSigma1(w[30]) + w[25] + sha256SmallSigma0(w[17]) + w[16]
280
- t1 = hh + sha256BigSigma1(e) + sha256Choose(e, f, g) + 0x27b70a85 + w[32]
281
- d += t1
282
- hh = t1 + sha256BigSigma0(a) + sha256Majority(a, b, c)
283
- w[33] = sha256SmallSigma1(w[31]) + w[26] + sha256SmallSigma0(w[18]) + w[17]
284
- t1 = g + sha256BigSigma1(d) + sha256Choose(d, e, f) + 0x2e1b2138 + w[33]
285
- c += t1
286
- g = t1 + sha256BigSigma0(hh) + sha256Majority(hh, a, b)
287
- w[34] = sha256SmallSigma1(w[32]) + w[27] + sha256SmallSigma0(w[19]) + w[18]
288
- t1 = f + sha256BigSigma1(c) + sha256Choose(c, d, e) + 0x4d2c6dfc + w[34]
289
- b += t1
290
- f = t1 + sha256BigSigma0(g) + sha256Majority(g, hh, a)
291
- w[35] = sha256SmallSigma1(w[33]) + w[28] + sha256SmallSigma0(w[20]) + w[19]
292
- t1 = e + sha256BigSigma1(b) + sha256Choose(b, c, d) + 0x53380d13 + w[35]
293
- a += t1
294
- e = t1 + sha256BigSigma0(f) + sha256Majority(f, g, hh)
295
- w[36] = sha256SmallSigma1(w[34]) + w[29] + sha256SmallSigma0(w[21]) + w[20]
296
- t1 = d + sha256BigSigma1(a) + sha256Choose(a, b, c) + 0x650a7354 + w[36]
297
- hh += t1
298
- d = t1 + sha256BigSigma0(e) + sha256Majority(e, f, g)
299
- w[37] = sha256SmallSigma1(w[35]) + w[30] + sha256SmallSigma0(w[22]) + w[21]
300
- t1 = c + sha256BigSigma1(hh) + sha256Choose(hh, a, b) + 0x766a0abb + w[37]
301
- g += t1
302
- c = t1 + sha256BigSigma0(d) + sha256Majority(d, e, f)
303
- w[38] = sha256SmallSigma1(w[36]) + w[31] + sha256SmallSigma0(w[23]) + w[22]
304
- t1 = b + sha256BigSigma1(g) + sha256Choose(g, hh, a) + 0x81c2c92e + w[38]
305
- f += t1
306
- b = t1 + sha256BigSigma0(c) + sha256Majority(c, d, e)
307
- w[39] = sha256SmallSigma1(w[37]) + w[32] + sha256SmallSigma0(w[24]) + w[23]
308
- t1 = a + sha256BigSigma1(f) + sha256Choose(f, g, hh) + 0x92722c85 + w[39]
309
- e += t1
310
- a = t1 + sha256BigSigma0(b) + sha256Majority(b, c, d)
311
- w[40] = sha256SmallSigma1(w[38]) + w[33] + sha256SmallSigma0(w[25]) + w[24]
312
- t1 = hh + sha256BigSigma1(e) + sha256Choose(e, f, g) + 0xa2bfe8a1 + w[40]
313
- d += t1
314
- hh = t1 + sha256BigSigma0(a) + sha256Majority(a, b, c)
315
- w[41] = sha256SmallSigma1(w[39]) + w[34] + sha256SmallSigma0(w[26]) + w[25]
316
- t1 = g + sha256BigSigma1(d) + sha256Choose(d, e, f) + 0xa81a664b + w[41]
317
- c += t1
318
- g = t1 + sha256BigSigma0(hh) + sha256Majority(hh, a, b)
319
- w[42] = sha256SmallSigma1(w[40]) + w[35] + sha256SmallSigma0(w[27]) + w[26]
320
- t1 = f + sha256BigSigma1(c) + sha256Choose(c, d, e) + 0xc24b8b70 + w[42]
321
- b += t1
322
- f = t1 + sha256BigSigma0(g) + sha256Majority(g, hh, a)
323
- w[43] = sha256SmallSigma1(w[41]) + w[36] + sha256SmallSigma0(w[28]) + w[27]
324
- t1 = e + sha256BigSigma1(b) + sha256Choose(b, c, d) + 0xc76c51a3 + w[43]
325
- a += t1
326
- e = t1 + sha256BigSigma0(f) + sha256Majority(f, g, hh)
327
- w[44] = sha256SmallSigma1(w[42]) + w[37] + sha256SmallSigma0(w[29]) + w[28]
328
- t1 = d + sha256BigSigma1(a) + sha256Choose(a, b, c) + 0xd192e819 + w[44]
329
- hh += t1
330
- d = t1 + sha256BigSigma0(e) + sha256Majority(e, f, g)
331
- w[45] = sha256SmallSigma1(w[43]) + w[38] + sha256SmallSigma0(w[30]) + w[29]
332
- t1 = c + sha256BigSigma1(hh) + sha256Choose(hh, a, b) + 0xd6990624 + w[45]
333
- g += t1
334
- c = t1 + sha256BigSigma0(d) + sha256Majority(d, e, f)
335
- w[46] = sha256SmallSigma1(w[44]) + w[39] + sha256SmallSigma0(w[31]) + w[30]
336
- t1 = b + sha256BigSigma1(g) + sha256Choose(g, hh, a) + 0xf40e3585 + w[46]
337
- f += t1
338
- b = t1 + sha256BigSigma0(c) + sha256Majority(c, d, e)
339
- w[47] = sha256SmallSigma1(w[45]) + w[40] + sha256SmallSigma0(w[32]) + w[31]
340
- t1 = a + sha256BigSigma1(f) + sha256Choose(f, g, hh) + 0x106aa070 + w[47]
341
- e += t1
342
- a = t1 + sha256BigSigma0(b) + sha256Majority(b, c, d)
343
- w[48] = sha256SmallSigma1(w[46]) + w[41] + sha256SmallSigma0(w[33]) + w[32]
344
- t1 = hh + sha256BigSigma1(e) + sha256Choose(e, f, g) + 0x19a4c116 + w[48]
345
- d += t1
346
- hh = t1 + sha256BigSigma0(a) + sha256Majority(a, b, c)
347
- w[49] = sha256SmallSigma1(w[47]) + w[42] + sha256SmallSigma0(w[34]) + w[33]
348
- t1 = g + sha256BigSigma1(d) + sha256Choose(d, e, f) + 0x1e376c08 + w[49]
349
- c += t1
350
- g = t1 + sha256BigSigma0(hh) + sha256Majority(hh, a, b)
351
- w[50] = sha256SmallSigma1(w[48]) + w[43] + sha256SmallSigma0(w[35]) + w[34]
352
- t1 = f + sha256BigSigma1(c) + sha256Choose(c, d, e) + 0x2748774c + w[50]
353
- b += t1
354
- f = t1 + sha256BigSigma0(g) + sha256Majority(g, hh, a)
355
- w[51] = sha256SmallSigma1(w[49]) + w[44] + sha256SmallSigma0(w[36]) + w[35]
356
- t1 = e + sha256BigSigma1(b) + sha256Choose(b, c, d) + 0x34b0bcb5 + w[51]
357
- a += t1
358
- e = t1 + sha256BigSigma0(f) + sha256Majority(f, g, hh)
359
- w[52] = sha256SmallSigma1(w[50]) + w[45] + sha256SmallSigma0(w[37]) + w[36]
360
- t1 = d + sha256BigSigma1(a) + sha256Choose(a, b, c) + 0x391c0cb3 + w[52]
361
- hh += t1
362
- d = t1 + sha256BigSigma0(e) + sha256Majority(e, f, g)
363
- w[53] = sha256SmallSigma1(w[51]) + w[46] + sha256SmallSigma0(w[38]) + w[37]
364
- t1 = c + sha256BigSigma1(hh) + sha256Choose(hh, a, b) + 0x4ed8aa4a + w[53]
365
- g += t1
366
- c = t1 + sha256BigSigma0(d) + sha256Majority(d, e, f)
367
- w[54] = sha256SmallSigma1(w[52]) + w[47] + sha256SmallSigma0(w[39]) + w[38]
368
- t1 = b + sha256BigSigma1(g) + sha256Choose(g, hh, a) + 0x5b9cca4f + w[54]
369
- f += t1
370
- b = t1 + sha256BigSigma0(c) + sha256Majority(c, d, e)
371
- w[55] = sha256SmallSigma1(w[53]) + w[48] + sha256SmallSigma0(w[40]) + w[39]
372
- t1 = a + sha256BigSigma1(f) + sha256Choose(f, g, hh) + 0x682e6ff3 + w[55]
373
- e += t1
374
- a = t1 + sha256BigSigma0(b) + sha256Majority(b, c, d)
375
- w[56] = sha256SmallSigma1(w[54]) + w[49] + sha256SmallSigma0(w[41]) + w[40]
376
- t1 = hh + sha256BigSigma1(e) + sha256Choose(e, f, g) + 0x748f82ee + w[56]
377
- d += t1
378
- hh = t1 + sha256BigSigma0(a) + sha256Majority(a, b, c)
379
- w[57] = sha256SmallSigma1(w[55]) + w[50] + sha256SmallSigma0(w[42]) + w[41]
380
- t1 = g + sha256BigSigma1(d) + sha256Choose(d, e, f) + 0x78a5636f + w[57]
381
- c += t1
382
- g = t1 + sha256BigSigma0(hh) + sha256Majority(hh, a, b)
383
- w[58] = sha256SmallSigma1(w[56]) + w[51] + sha256SmallSigma0(w[43]) + w[42]
384
- t1 = f + sha256BigSigma1(c) + sha256Choose(c, d, e) + 0x84c87814 + w[58]
385
- b += t1
386
- f = t1 + sha256BigSigma0(g) + sha256Majority(g, hh, a)
387
- w[59] = sha256SmallSigma1(w[57]) + w[52] + sha256SmallSigma0(w[44]) + w[43]
388
- t1 = e + sha256BigSigma1(b) + sha256Choose(b, c, d) + 0x8cc70208 + w[59]
389
- a += t1
390
- e = t1 + sha256BigSigma0(f) + sha256Majority(f, g, hh)
391
- w[60] = sha256SmallSigma1(w[58]) + w[53] + sha256SmallSigma0(w[45]) + w[44]
392
- t1 = d + sha256BigSigma1(a) + sha256Choose(a, b, c) + 0x90befffa + w[60]
393
- hh += t1
394
- d = t1 + sha256BigSigma0(e) + sha256Majority(e, f, g)
395
- w[61] = sha256SmallSigma1(w[59]) + w[54] + sha256SmallSigma0(w[46]) + w[45]
396
- t1 = c + sha256BigSigma1(hh) + sha256Choose(hh, a, b) + 0xa4506ceb + w[61]
397
- g += t1
398
- c = t1 + sha256BigSigma0(d) + sha256Majority(d, e, f)
399
- w[62] = sha256SmallSigma1(w[60]) + w[55] + sha256SmallSigma0(w[47]) + w[46]
400
- t1 = b + sha256BigSigma1(g) + sha256Choose(g, hh, a) + 0xbef9a3f7 + w[62]
401
- f += t1
402
- b = t1 + sha256BigSigma0(c) + sha256Majority(c, d, e)
403
- w[63] = sha256SmallSigma1(w[61]) + w[56] + sha256SmallSigma0(w[48]) + w[47]
404
- t1 = a + sha256BigSigma1(f) + sha256Choose(f, g, hh) + 0xc67178f2 + w[63]
405
- e += t1
406
- a = t1 + sha256BigSigma0(b) + sha256Majority(b, c, d)
407
- // Step 4: the next intermediate hash value.
408
- h[0] += a
409
- h[1] += b
410
- h[2] += c
411
- h[3] += d
412
- h[4] += e
413
- h[5] += f
414
- h[6] += g
415
- h[7] += hh
416
- } else {
417
- panic("Sha256: a block outside its buffer")
108
+ if (base < n - 63) {
109
+ w[0] =
110
+ toU32(src[base + 3]) |
111
+ (toU32(src[base + 2]) << 8) |
112
+ (toU32(src[base + 1]) << 16) |
113
+ (toU32(src[base]) << 24)
114
+ w[1] =
115
+ toU32(src[base + 7]) |
116
+ (toU32(src[base + 6]) << 8) |
117
+ (toU32(src[base + 5]) << 16) |
118
+ (toU32(src[base + 4]) << 24)
119
+ w[2] =
120
+ toU32(src[base + 11]) |
121
+ (toU32(src[base + 10]) << 8) |
122
+ (toU32(src[base + 9]) << 16) |
123
+ (toU32(src[base + 8]) << 24)
124
+ w[3] =
125
+ toU32(src[base + 15]) |
126
+ (toU32(src[base + 14]) << 8) |
127
+ (toU32(src[base + 13]) << 16) |
128
+ (toU32(src[base + 12]) << 24)
129
+ w[4] =
130
+ toU32(src[base + 19]) |
131
+ (toU32(src[base + 18]) << 8) |
132
+ (toU32(src[base + 17]) << 16) |
133
+ (toU32(src[base + 16]) << 24)
134
+ w[5] =
135
+ toU32(src[base + 23]) |
136
+ (toU32(src[base + 22]) << 8) |
137
+ (toU32(src[base + 21]) << 16) |
138
+ (toU32(src[base + 20]) << 24)
139
+ w[6] =
140
+ toU32(src[base + 27]) |
141
+ (toU32(src[base + 26]) << 8) |
142
+ (toU32(src[base + 25]) << 16) |
143
+ (toU32(src[base + 24]) << 24)
144
+ w[7] =
145
+ toU32(src[base + 31]) |
146
+ (toU32(src[base + 30]) << 8) |
147
+ (toU32(src[base + 29]) << 16) |
148
+ (toU32(src[base + 28]) << 24)
149
+ w[8] =
150
+ toU32(src[base + 35]) |
151
+ (toU32(src[base + 34]) << 8) |
152
+ (toU32(src[base + 33]) << 16) |
153
+ (toU32(src[base + 32]) << 24)
154
+ w[9] =
155
+ toU32(src[base + 39]) |
156
+ (toU32(src[base + 38]) << 8) |
157
+ (toU32(src[base + 37]) << 16) |
158
+ (toU32(src[base + 36]) << 24)
159
+ w[10] =
160
+ toU32(src[base + 43]) |
161
+ (toU32(src[base + 42]) << 8) |
162
+ (toU32(src[base + 41]) << 16) |
163
+ (toU32(src[base + 40]) << 24)
164
+ w[11] =
165
+ toU32(src[base + 47]) |
166
+ (toU32(src[base + 46]) << 8) |
167
+ (toU32(src[base + 45]) << 16) |
168
+ (toU32(src[base + 44]) << 24)
169
+ w[12] =
170
+ toU32(src[base + 51]) |
171
+ (toU32(src[base + 50]) << 8) |
172
+ (toU32(src[base + 49]) << 16) |
173
+ (toU32(src[base + 48]) << 24)
174
+ w[13] =
175
+ toU32(src[base + 55]) |
176
+ (toU32(src[base + 54]) << 8) |
177
+ (toU32(src[base + 53]) << 16) |
178
+ (toU32(src[base + 52]) << 24)
179
+ w[14] =
180
+ toU32(src[base + 59]) |
181
+ (toU32(src[base + 58]) << 8) |
182
+ (toU32(src[base + 57]) << 16) |
183
+ (toU32(src[base + 56]) << 24)
184
+ w[15] =
185
+ toU32(src[base + 63]) |
186
+ (toU32(src[base + 62]) << 8) |
187
+ (toU32(src[base + 61]) << 16) |
188
+ (toU32(src[base + 60]) << 24)
189
+ // Step 2: the working variables start from the previous hash value.
190
+ let a: u32 = h[0]
191
+ let b: u32 = h[1]
192
+ let c: u32 = h[2]
193
+ let d: u32 = h[3]
194
+ let e: u32 = h[4]
195
+ let f: u32 = h[5]
196
+ let g: u32 = h[6]
197
+ let hh: u32 = h[7]
198
+ // T1 of step 3; T2 is added where it is computed.
199
+ let t1: u32 = 0
200
+ // Steps 1 and 3: rounds 0 to 15 read the block's sixteen words.
201
+ t1 = hh + sha256BigSigma1(e) + sha256Choose(e, f, g) + 0x428a2f98 + w[0]
202
+ d += t1
203
+ hh = t1 + sha256BigSigma0(a) + sha256Majority(a, b, c)
204
+ t1 = g + sha256BigSigma1(d) + sha256Choose(d, e, f) + 0x71374491 + w[1]
205
+ c += t1
206
+ g = t1 + sha256BigSigma0(hh) + sha256Majority(hh, a, b)
207
+ t1 = f + sha256BigSigma1(c) + sha256Choose(c, d, e) + 0xb5c0fbcf + w[2]
208
+ b += t1
209
+ f = t1 + sha256BigSigma0(g) + sha256Majority(g, hh, a)
210
+ t1 = e + sha256BigSigma1(b) + sha256Choose(b, c, d) + 0xe9b5dba5 + w[3]
211
+ a += t1
212
+ e = t1 + sha256BigSigma0(f) + sha256Majority(f, g, hh)
213
+ t1 = d + sha256BigSigma1(a) + sha256Choose(a, b, c) + 0x3956c25b + w[4]
214
+ hh += t1
215
+ d = t1 + sha256BigSigma0(e) + sha256Majority(e, f, g)
216
+ t1 = c + sha256BigSigma1(hh) + sha256Choose(hh, a, b) + 0x59f111f1 + w[5]
217
+ g += t1
218
+ c = t1 + sha256BigSigma0(d) + sha256Majority(d, e, f)
219
+ t1 = b + sha256BigSigma1(g) + sha256Choose(g, hh, a) + 0x923f82a4 + w[6]
220
+ f += t1
221
+ b = t1 + sha256BigSigma0(c) + sha256Majority(c, d, e)
222
+ t1 = a + sha256BigSigma1(f) + sha256Choose(f, g, hh) + 0xab1c5ed5 + w[7]
223
+ e += t1
224
+ a = t1 + sha256BigSigma0(b) + sha256Majority(b, c, d)
225
+ t1 = hh + sha256BigSigma1(e) + sha256Choose(e, f, g) + 0xd807aa98 + w[8]
226
+ d += t1
227
+ hh = t1 + sha256BigSigma0(a) + sha256Majority(a, b, c)
228
+ t1 = g + sha256BigSigma1(d) + sha256Choose(d, e, f) + 0x12835b01 + w[9]
229
+ c += t1
230
+ g = t1 + sha256BigSigma0(hh) + sha256Majority(hh, a, b)
231
+ t1 = f + sha256BigSigma1(c) + sha256Choose(c, d, e) + 0x243185be + w[10]
232
+ b += t1
233
+ f = t1 + sha256BigSigma0(g) + sha256Majority(g, hh, a)
234
+ t1 = e + sha256BigSigma1(b) + sha256Choose(b, c, d) + 0x550c7dc3 + w[11]
235
+ a += t1
236
+ e = t1 + sha256BigSigma0(f) + sha256Majority(f, g, hh)
237
+ t1 = d + sha256BigSigma1(a) + sha256Choose(a, b, c) + 0x72be5d74 + w[12]
238
+ hh += t1
239
+ d = t1 + sha256BigSigma0(e) + sha256Majority(e, f, g)
240
+ t1 = c + sha256BigSigma1(hh) + sha256Choose(hh, a, b) + 0x80deb1fe + w[13]
241
+ g += t1
242
+ c = t1 + sha256BigSigma0(d) + sha256Majority(d, e, f)
243
+ t1 = b + sha256BigSigma1(g) + sha256Choose(g, hh, a) + 0x9bdc06a7 + w[14]
244
+ f += t1
245
+ b = t1 + sha256BigSigma0(c) + sha256Majority(c, d, e)
246
+ t1 = a + sha256BigSigma1(f) + sha256Choose(f, g, hh) + 0xc19bf174 + w[15]
247
+ e += t1
248
+ a = t1 + sha256BigSigma0(b) + sha256Majority(b, c, d)
249
+ // Rounds 16 to 63 extend the schedule one word ahead of the round that reads it.
250
+ w[16] = sha256SmallSigma1(w[14]) + w[9] + sha256SmallSigma0(w[1]) + w[0]
251
+ t1 = hh + sha256BigSigma1(e) + sha256Choose(e, f, g) + 0xe49b69c1 + w[16]
252
+ d += t1
253
+ hh = t1 + sha256BigSigma0(a) + sha256Majority(a, b, c)
254
+ w[17] = sha256SmallSigma1(w[15]) + w[10] + sha256SmallSigma0(w[2]) + w[1]
255
+ t1 = g + sha256BigSigma1(d) + sha256Choose(d, e, f) + 0xefbe4786 + w[17]
256
+ c += t1
257
+ g = t1 + sha256BigSigma0(hh) + sha256Majority(hh, a, b)
258
+ w[18] = sha256SmallSigma1(w[16]) + w[11] + sha256SmallSigma0(w[3]) + w[2]
259
+ t1 = f + sha256BigSigma1(c) + sha256Choose(c, d, e) + 0x0fc19dc6 + w[18]
260
+ b += t1
261
+ f = t1 + sha256BigSigma0(g) + sha256Majority(g, hh, a)
262
+ w[19] = sha256SmallSigma1(w[17]) + w[12] + sha256SmallSigma0(w[4]) + w[3]
263
+ t1 = e + sha256BigSigma1(b) + sha256Choose(b, c, d) + 0x240ca1cc + w[19]
264
+ a += t1
265
+ e = t1 + sha256BigSigma0(f) + sha256Majority(f, g, hh)
266
+ w[20] = sha256SmallSigma1(w[18]) + w[13] + sha256SmallSigma0(w[5]) + w[4]
267
+ t1 = d + sha256BigSigma1(a) + sha256Choose(a, b, c) + 0x2de92c6f + w[20]
268
+ hh += t1
269
+ d = t1 + sha256BigSigma0(e) + sha256Majority(e, f, g)
270
+ w[21] = sha256SmallSigma1(w[19]) + w[14] + sha256SmallSigma0(w[6]) + w[5]
271
+ t1 = c + sha256BigSigma1(hh) + sha256Choose(hh, a, b) + 0x4a7484aa + w[21]
272
+ g += t1
273
+ c = t1 + sha256BigSigma0(d) + sha256Majority(d, e, f)
274
+ w[22] = sha256SmallSigma1(w[20]) + w[15] + sha256SmallSigma0(w[7]) + w[6]
275
+ t1 = b + sha256BigSigma1(g) + sha256Choose(g, hh, a) + 0x5cb0a9dc + w[22]
276
+ f += t1
277
+ b = t1 + sha256BigSigma0(c) + sha256Majority(c, d, e)
278
+ w[23] = sha256SmallSigma1(w[21]) + w[16] + sha256SmallSigma0(w[8]) + w[7]
279
+ t1 = a + sha256BigSigma1(f) + sha256Choose(f, g, hh) + 0x76f988da + w[23]
280
+ e += t1
281
+ a = t1 + sha256BigSigma0(b) + sha256Majority(b, c, d)
282
+ w[24] = sha256SmallSigma1(w[22]) + w[17] + sha256SmallSigma0(w[9]) + w[8]
283
+ t1 = hh + sha256BigSigma1(e) + sha256Choose(e, f, g) + 0x983e5152 + w[24]
284
+ d += t1
285
+ hh = t1 + sha256BigSigma0(a) + sha256Majority(a, b, c)
286
+ w[25] = sha256SmallSigma1(w[23]) + w[18] + sha256SmallSigma0(w[10]) + w[9]
287
+ t1 = g + sha256BigSigma1(d) + sha256Choose(d, e, f) + 0xa831c66d + w[25]
288
+ c += t1
289
+ g = t1 + sha256BigSigma0(hh) + sha256Majority(hh, a, b)
290
+ w[26] = sha256SmallSigma1(w[24]) + w[19] + sha256SmallSigma0(w[11]) + w[10]
291
+ t1 = f + sha256BigSigma1(c) + sha256Choose(c, d, e) + 0xb00327c8 + w[26]
292
+ b += t1
293
+ f = t1 + sha256BigSigma0(g) + sha256Majority(g, hh, a)
294
+ w[27] = sha256SmallSigma1(w[25]) + w[20] + sha256SmallSigma0(w[12]) + w[11]
295
+ t1 = e + sha256BigSigma1(b) + sha256Choose(b, c, d) + 0xbf597fc7 + w[27]
296
+ a += t1
297
+ e = t1 + sha256BigSigma0(f) + sha256Majority(f, g, hh)
298
+ w[28] = sha256SmallSigma1(w[26]) + w[21] + sha256SmallSigma0(w[13]) + w[12]
299
+ t1 = d + sha256BigSigma1(a) + sha256Choose(a, b, c) + 0xc6e00bf3 + w[28]
300
+ hh += t1
301
+ d = t1 + sha256BigSigma0(e) + sha256Majority(e, f, g)
302
+ w[29] = sha256SmallSigma1(w[27]) + w[22] + sha256SmallSigma0(w[14]) + w[13]
303
+ t1 = c + sha256BigSigma1(hh) + sha256Choose(hh, a, b) + 0xd5a79147 + w[29]
304
+ g += t1
305
+ c = t1 + sha256BigSigma0(d) + sha256Majority(d, e, f)
306
+ w[30] = sha256SmallSigma1(w[28]) + w[23] + sha256SmallSigma0(w[15]) + w[14]
307
+ t1 = b + sha256BigSigma1(g) + sha256Choose(g, hh, a) + 0x06ca6351 + w[30]
308
+ f += t1
309
+ b = t1 + sha256BigSigma0(c) + sha256Majority(c, d, e)
310
+ w[31] = sha256SmallSigma1(w[29]) + w[24] + sha256SmallSigma0(w[16]) + w[15]
311
+ t1 = a + sha256BigSigma1(f) + sha256Choose(f, g, hh) + 0x14292967 + w[31]
312
+ e += t1
313
+ a = t1 + sha256BigSigma0(b) + sha256Majority(b, c, d)
314
+ w[32] = sha256SmallSigma1(w[30]) + w[25] + sha256SmallSigma0(w[17]) + w[16]
315
+ t1 = hh + sha256BigSigma1(e) + sha256Choose(e, f, g) + 0x27b70a85 + w[32]
316
+ d += t1
317
+ hh = t1 + sha256BigSigma0(a) + sha256Majority(a, b, c)
318
+ w[33] = sha256SmallSigma1(w[31]) + w[26] + sha256SmallSigma0(w[18]) + w[17]
319
+ t1 = g + sha256BigSigma1(d) + sha256Choose(d, e, f) + 0x2e1b2138 + w[33]
320
+ c += t1
321
+ g = t1 + sha256BigSigma0(hh) + sha256Majority(hh, a, b)
322
+ w[34] = sha256SmallSigma1(w[32]) + w[27] + sha256SmallSigma0(w[19]) + w[18]
323
+ t1 = f + sha256BigSigma1(c) + sha256Choose(c, d, e) + 0x4d2c6dfc + w[34]
324
+ b += t1
325
+ f = t1 + sha256BigSigma0(g) + sha256Majority(g, hh, a)
326
+ w[35] = sha256SmallSigma1(w[33]) + w[28] + sha256SmallSigma0(w[20]) + w[19]
327
+ t1 = e + sha256BigSigma1(b) + sha256Choose(b, c, d) + 0x53380d13 + w[35]
328
+ a += t1
329
+ e = t1 + sha256BigSigma0(f) + sha256Majority(f, g, hh)
330
+ w[36] = sha256SmallSigma1(w[34]) + w[29] + sha256SmallSigma0(w[21]) + w[20]
331
+ t1 = d + sha256BigSigma1(a) + sha256Choose(a, b, c) + 0x650a7354 + w[36]
332
+ hh += t1
333
+ d = t1 + sha256BigSigma0(e) + sha256Majority(e, f, g)
334
+ w[37] = sha256SmallSigma1(w[35]) + w[30] + sha256SmallSigma0(w[22]) + w[21]
335
+ t1 = c + sha256BigSigma1(hh) + sha256Choose(hh, a, b) + 0x766a0abb + w[37]
336
+ g += t1
337
+ c = t1 + sha256BigSigma0(d) + sha256Majority(d, e, f)
338
+ w[38] = sha256SmallSigma1(w[36]) + w[31] + sha256SmallSigma0(w[23]) + w[22]
339
+ t1 = b + sha256BigSigma1(g) + sha256Choose(g, hh, a) + 0x81c2c92e + w[38]
340
+ f += t1
341
+ b = t1 + sha256BigSigma0(c) + sha256Majority(c, d, e)
342
+ w[39] = sha256SmallSigma1(w[37]) + w[32] + sha256SmallSigma0(w[24]) + w[23]
343
+ t1 = a + sha256BigSigma1(f) + sha256Choose(f, g, hh) + 0x92722c85 + w[39]
344
+ e += t1
345
+ a = t1 + sha256BigSigma0(b) + sha256Majority(b, c, d)
346
+ w[40] = sha256SmallSigma1(w[38]) + w[33] + sha256SmallSigma0(w[25]) + w[24]
347
+ t1 = hh + sha256BigSigma1(e) + sha256Choose(e, f, g) + 0xa2bfe8a1 + w[40]
348
+ d += t1
349
+ hh = t1 + sha256BigSigma0(a) + sha256Majority(a, b, c)
350
+ w[41] = sha256SmallSigma1(w[39]) + w[34] + sha256SmallSigma0(w[26]) + w[25]
351
+ t1 = g + sha256BigSigma1(d) + sha256Choose(d, e, f) + 0xa81a664b + w[41]
352
+ c += t1
353
+ g = t1 + sha256BigSigma0(hh) + sha256Majority(hh, a, b)
354
+ w[42] = sha256SmallSigma1(w[40]) + w[35] + sha256SmallSigma0(w[27]) + w[26]
355
+ t1 = f + sha256BigSigma1(c) + sha256Choose(c, d, e) + 0xc24b8b70 + w[42]
356
+ b += t1
357
+ f = t1 + sha256BigSigma0(g) + sha256Majority(g, hh, a)
358
+ w[43] = sha256SmallSigma1(w[41]) + w[36] + sha256SmallSigma0(w[28]) + w[27]
359
+ t1 = e + sha256BigSigma1(b) + sha256Choose(b, c, d) + 0xc76c51a3 + w[43]
360
+ a += t1
361
+ e = t1 + sha256BigSigma0(f) + sha256Majority(f, g, hh)
362
+ w[44] = sha256SmallSigma1(w[42]) + w[37] + sha256SmallSigma0(w[29]) + w[28]
363
+ t1 = d + sha256BigSigma1(a) + sha256Choose(a, b, c) + 0xd192e819 + w[44]
364
+ hh += t1
365
+ d = t1 + sha256BigSigma0(e) + sha256Majority(e, f, g)
366
+ w[45] = sha256SmallSigma1(w[43]) + w[38] + sha256SmallSigma0(w[30]) + w[29]
367
+ t1 = c + sha256BigSigma1(hh) + sha256Choose(hh, a, b) + 0xd6990624 + w[45]
368
+ g += t1
369
+ c = t1 + sha256BigSigma0(d) + sha256Majority(d, e, f)
370
+ w[46] = sha256SmallSigma1(w[44]) + w[39] + sha256SmallSigma0(w[31]) + w[30]
371
+ t1 = b + sha256BigSigma1(g) + sha256Choose(g, hh, a) + 0xf40e3585 + w[46]
372
+ f += t1
373
+ b = t1 + sha256BigSigma0(c) + sha256Majority(c, d, e)
374
+ w[47] = sha256SmallSigma1(w[45]) + w[40] + sha256SmallSigma0(w[32]) + w[31]
375
+ t1 = a + sha256BigSigma1(f) + sha256Choose(f, g, hh) + 0x106aa070 + w[47]
376
+ e += t1
377
+ a = t1 + sha256BigSigma0(b) + sha256Majority(b, c, d)
378
+ w[48] = sha256SmallSigma1(w[46]) + w[41] + sha256SmallSigma0(w[33]) + w[32]
379
+ t1 = hh + sha256BigSigma1(e) + sha256Choose(e, f, g) + 0x19a4c116 + w[48]
380
+ d += t1
381
+ hh = t1 + sha256BigSigma0(a) + sha256Majority(a, b, c)
382
+ w[49] = sha256SmallSigma1(w[47]) + w[42] + sha256SmallSigma0(w[34]) + w[33]
383
+ t1 = g + sha256BigSigma1(d) + sha256Choose(d, e, f) + 0x1e376c08 + w[49]
384
+ c += t1
385
+ g = t1 + sha256BigSigma0(hh) + sha256Majority(hh, a, b)
386
+ w[50] = sha256SmallSigma1(w[48]) + w[43] + sha256SmallSigma0(w[35]) + w[34]
387
+ t1 = f + sha256BigSigma1(c) + sha256Choose(c, d, e) + 0x2748774c + w[50]
388
+ b += t1
389
+ f = t1 + sha256BigSigma0(g) + sha256Majority(g, hh, a)
390
+ w[51] = sha256SmallSigma1(w[49]) + w[44] + sha256SmallSigma0(w[36]) + w[35]
391
+ t1 = e + sha256BigSigma1(b) + sha256Choose(b, c, d) + 0x34b0bcb5 + w[51]
392
+ a += t1
393
+ e = t1 + sha256BigSigma0(f) + sha256Majority(f, g, hh)
394
+ w[52] = sha256SmallSigma1(w[50]) + w[45] + sha256SmallSigma0(w[37]) + w[36]
395
+ t1 = d + sha256BigSigma1(a) + sha256Choose(a, b, c) + 0x391c0cb3 + w[52]
396
+ hh += t1
397
+ d = t1 + sha256BigSigma0(e) + sha256Majority(e, f, g)
398
+ w[53] = sha256SmallSigma1(w[51]) + w[46] + sha256SmallSigma0(w[38]) + w[37]
399
+ t1 = c + sha256BigSigma1(hh) + sha256Choose(hh, a, b) + 0x4ed8aa4a + w[53]
400
+ g += t1
401
+ c = t1 + sha256BigSigma0(d) + sha256Majority(d, e, f)
402
+ w[54] = sha256SmallSigma1(w[52]) + w[47] + sha256SmallSigma0(w[39]) + w[38]
403
+ t1 = b + sha256BigSigma1(g) + sha256Choose(g, hh, a) + 0x5b9cca4f + w[54]
404
+ f += t1
405
+ b = t1 + sha256BigSigma0(c) + sha256Majority(c, d, e)
406
+ w[55] = sha256SmallSigma1(w[53]) + w[48] + sha256SmallSigma0(w[40]) + w[39]
407
+ t1 = a + sha256BigSigma1(f) + sha256Choose(f, g, hh) + 0x682e6ff3 + w[55]
408
+ e += t1
409
+ a = t1 + sha256BigSigma0(b) + sha256Majority(b, c, d)
410
+ w[56] = sha256SmallSigma1(w[54]) + w[49] + sha256SmallSigma0(w[41]) + w[40]
411
+ t1 = hh + sha256BigSigma1(e) + sha256Choose(e, f, g) + 0x748f82ee + w[56]
412
+ d += t1
413
+ hh = t1 + sha256BigSigma0(a) + sha256Majority(a, b, c)
414
+ w[57] = sha256SmallSigma1(w[55]) + w[50] + sha256SmallSigma0(w[42]) + w[41]
415
+ t1 = g + sha256BigSigma1(d) + sha256Choose(d, e, f) + 0x78a5636f + w[57]
416
+ c += t1
417
+ g = t1 + sha256BigSigma0(hh) + sha256Majority(hh, a, b)
418
+ w[58] = sha256SmallSigma1(w[56]) + w[51] + sha256SmallSigma0(w[43]) + w[42]
419
+ t1 = f + sha256BigSigma1(c) + sha256Choose(c, d, e) + 0x84c87814 + w[58]
420
+ b += t1
421
+ f = t1 + sha256BigSigma0(g) + sha256Majority(g, hh, a)
422
+ w[59] = sha256SmallSigma1(w[57]) + w[52] + sha256SmallSigma0(w[44]) + w[43]
423
+ t1 = e + sha256BigSigma1(b) + sha256Choose(b, c, d) + 0x8cc70208 + w[59]
424
+ a += t1
425
+ e = t1 + sha256BigSigma0(f) + sha256Majority(f, g, hh)
426
+ w[60] = sha256SmallSigma1(w[58]) + w[53] + sha256SmallSigma0(w[45]) + w[44]
427
+ t1 = d + sha256BigSigma1(a) + sha256Choose(a, b, c) + 0x90befffa + w[60]
428
+ hh += t1
429
+ d = t1 + sha256BigSigma0(e) + sha256Majority(e, f, g)
430
+ w[61] = sha256SmallSigma1(w[59]) + w[54] + sha256SmallSigma0(w[46]) + w[45]
431
+ t1 = c + sha256BigSigma1(hh) + sha256Choose(hh, a, b) + 0xa4506ceb + w[61]
432
+ g += t1
433
+ c = t1 + sha256BigSigma0(d) + sha256Majority(d, e, f)
434
+ w[62] = sha256SmallSigma1(w[60]) + w[55] + sha256SmallSigma0(w[47]) + w[46]
435
+ t1 = b + sha256BigSigma1(g) + sha256Choose(g, hh, a) + 0xbef9a3f7 + w[62]
436
+ f += t1
437
+ b = t1 + sha256BigSigma0(c) + sha256Majority(c, d, e)
438
+ w[63] = sha256SmallSigma1(w[61]) + w[56] + sha256SmallSigma0(w[48]) + w[47]
439
+ t1 = a + sha256BigSigma1(f) + sha256Choose(f, g, hh) + 0xc67178f2 + w[63]
440
+ e += t1
441
+ a = t1 + sha256BigSigma0(b) + sha256Majority(b, c, d)
442
+ // Step 4: the next intermediate hash value.
443
+ h[0] += a
444
+ h[1] += b
445
+ h[2] += c
446
+ h[3] += d
447
+ h[4] += e
448
+ h[5] += f
449
+ h[6] += g
450
+ h[7] += hh
451
+ }
418
452
  }
419
453
  }
420
454
 
@@ -425,10 +459,11 @@ const sha256Compress = (h: u32[], w: u32[], src: u8[], at: i32): void => {
425
459
  * `update` may be called any number of times with windows of any length, and
426
460
  * the digest does not depend on where the message was split. `digest` pads the
427
461
  * message (§5.1.1), runs the last block or two, and ends the computation: the
428
- * padding is written into this hasher's own state, so a second `digest`, a
429
- * later `update` or a `copy` would carry the padding as message, and all three
430
- * panic instead of answering a wrong value. `copy` first when the computation
431
- * has to go on.
462
+ * padding is written into this hasher's own state, so a later `update` would
463
+ * carry the padding as message, and it panics instead of answering a wrong
464
+ * value. A second `digest` answers the same digest again, and a `copy` of a
465
+ * finished hasher is finished too. `copy` first when the computation has to go
466
+ * on.
432
467
  */
433
468
  export class Sha256 {
434
469
  /**
@@ -496,67 +531,121 @@ export class Sha256 {
496
531
  if (off < 0 || len < 0 || off > size || len > size - off) {
497
532
  panic("Sha256: the window is outside the buffer")
498
533
  }
499
- this.total += toI64(len)
500
- let at: i32 = off
501
- const end: i32 = off + len
534
+ // §5.1.1 appends the length modulo 2^64, so the count wraps as a `u64`.
535
+ this.total = toI64(toU64(this.total) + toU64(toI64(len)))
536
+ const state: u32[] = this.state
537
+ const schedule: u32[] = this.schedule
502
538
  const block: u8[] = this.block
539
+ const room: i32 = toI32(block.length)
540
+ let at: i32 = off
541
+ let left: i32 = len
542
+ let fill: i32 = this.fill
503
543
  // Top up a partly filled block first, so that the loop below only ever
504
- // starts at a block boundary.
505
- if (this.fill > 0) {
506
- const room: i32 = SHA256_BLOCK - this.fill
507
- const take: i32 = len < room ? len : room
508
- sha256CopyBytes(data, at, block, this.fill, take)
509
- at += take
510
- this.fill += take
511
- if (this.fill < SHA256_BLOCK) {
544
+ // starts at a block boundary. Each loop's condition beyond its count is
545
+ // what proves its indices in range; the window check above makes it hold.
546
+ if (fill > 0) {
547
+ while (left > 0 && fill >= 0 && fill < SHA256_BLOCK && fill < room && at >= 0 && at < size) {
548
+ block[fill] = data[at]
549
+ fill += 1
550
+ at += 1
551
+ left -= 1
552
+ }
553
+ if (fill < SHA256_BLOCK) {
554
+ this.fill = fill
512
555
  return
513
556
  }
514
- sha256Compress(this.state, this.schedule, block, 0)
515
- this.fill = 0
557
+ sha256Compress(state, schedule, block, 0)
516
558
  }
517
559
  // Whole blocks are compressed straight out of the caller's buffer, with no
518
560
  // copy through `block`.
519
- while (end - at >= SHA256_BLOCK) {
520
- sha256Compress(this.state, this.schedule, data, at)
521
- at += SHA256_BLOCK
561
+ while (left >= 64 && at >= 0 && at <= size - 64) {
562
+ sha256Compress(state, schedule, data, at)
563
+ at += 64
564
+ left -= 64
565
+ }
566
+ fill = 0
567
+ while (fill < left && fill < room && at >= 0 && at < size) {
568
+ block[fill] = data[at]
569
+ fill += 1
570
+ at += 1
571
+ }
572
+ this.fill = fill
573
+ }
574
+
575
+ /**
576
+ * Absorbs all of `data`. An array of up to 2^31 - 1 bytes goes through
577
+ * `update`. A longer one (possible under `--number-mode f64`, where `toI32`
578
+ * of its length would saturate and leave a prefix) is walked with a `number`
579
+ * offset instead, a block at a time through `block`, so it is absorbed
580
+ * whole. After `digest` it panics, as `update` does.
581
+ *
582
+ * @param data The message, or the rest of it; it is read, not kept.
583
+ */
584
+ updateAll(data: u8[]): void {
585
+ const n: number = data.length
586
+ if (this.finished || n <= 2147483647) {
587
+ // A finished hasher panics in `update` before it reads the window, so
588
+ // the saturated length of a longer array is never used.
589
+ const from: i32 = 0
590
+ this.update(data, from, toI32(n))
591
+ return
592
+ }
593
+ this.total = toI64(toU64(this.total) + toU64(toI64(n)))
594
+ const state: u32[] = this.state
595
+ const schedule: u32[] = this.schedule
596
+ const block: u8[] = this.block
597
+ const room: i32 = toI32(block.length)
598
+ let fill: i32 = this.fill
599
+ let off: number = 0
600
+ while (true) {
601
+ while (fill >= 0 && fill < SHA256_BLOCK && fill < room && off >= 0 && off < n) {
602
+ block[fill] = data[off]
603
+ fill += 1
604
+ off += 1
605
+ }
606
+ if (fill < SHA256_BLOCK) {
607
+ this.fill = fill
608
+ return
609
+ }
610
+ sha256Compress(state, schedule, block, 0)
611
+ fill = 0
522
612
  }
523
- sha256CopyBytes(data, at, block, 0, end - at)
524
- this.fill = end - at
525
613
  }
526
614
 
527
615
  /**
528
616
  * An independent hasher at the same point in the same message: updating or
529
617
  * digesting either one leaves the other as it was. This is how a caller
530
- * takes the hash of a prefix and keeps going.
618
+ * takes the hash of a prefix and keeps going. A copy of a finished hasher is
619
+ * finished too, and its `digest` is the same digest.
531
620
  *
532
621
  * @returns A new hasher with its own state, block and schedule; it shares no
533
622
  * array with this one.
534
623
  */
535
624
  copy(): Sha256 {
536
- if (this.finished) {
537
- panic("Sha256: copy after digest")
538
- }
539
625
  const twin: Sha256 = new Sha256()
540
626
  const fromState: u32[] = this.state
541
627
  const toState: u32[] = twin.state
542
- // Both are always eight words; the test is what proves the indices below
543
- // in range, as it does in `sha256Compress`.
544
- if (toI32(fromState.length) >= 8 && toI32(toState.length) >= 8) {
545
- for (let i: i32 = 0; i < 8; i += 1) {
546
- toState[i] = fromState[i]
547
- }
548
- } else {
549
- panic("Sha256: a hash value that is not eight words")
628
+ // Both are always eight words, and both blocks `SHA256_BLOCK` bytes; the
629
+ // loop conditions are what prove the indices in range.
630
+ for (let i: i32 = 0; i < 8 && i < toI32(fromState.length) && i < toI32(toState.length); i += 1) {
631
+ toState[i] = fromState[i]
550
632
  }
551
- sha256CopyBytes(this.block, 0, twin.block, 0, this.fill)
552
- twin.fill = this.fill
633
+ const fromBlock: u8[] = this.block
634
+ const toBlock: u8[] = twin.block
635
+ const fill: i32 = this.fill
636
+ for (let i: i32 = 0; i < fill && i < toI32(fromBlock.length) && i < toI32(toBlock.length); i += 1) {
637
+ toBlock[i] = fromBlock[i]
638
+ }
639
+ twin.fill = fill
553
640
  twin.total = this.total
641
+ twin.finished = this.finished
554
642
  return twin
555
643
  }
556
644
 
557
645
  /**
558
646
  * The 32-byte message digest (FIPS 180-4 §6.2.2, the final H^(N) in
559
- * big-endian order), in a fresh array. Ends the computation.
647
+ * big-endian order), in a fresh array. Ends the computation; a second call
648
+ * answers the same digest.
560
649
  *
561
650
  * @returns A fresh 32-byte digest, which the caller owns.
562
651
  */
@@ -570,84 +659,75 @@ export class Sha256 {
570
659
  /**
571
660
  * Writes the 32-byte message digest (FIPS 180-4 §6.2.2, the final H^(N) in
572
661
  * big-endian order) at `out[at .. at + 32)` and ends the computation, with
573
- * nothing allocated. A window outside `out` panics before anything is
574
- * written or the hasher is finished, rather than writing the part of the
575
- * digest that fits. A second `digest` or `digestInto` panics, and so does an
576
- * `update` after it.
662
+ * nothing allocated. A window outside `out` writes nothing, leaves the
663
+ * hasher as it was and answers `false`, rather than writing the part of the
664
+ * digest that fits. Once finished, the hash value is the digest, so a second
665
+ * `digest` or `digestInto` writes the same 32 bytes again; an `update` after
666
+ * it panics.
577
667
  *
578
668
  * @param out The caller's array the digest is written into.
579
669
  * @param at Where the 32 digest bytes start in `out`.
670
+ * @returns `true` once the digest is written; `false`, with `out` and the hasher untouched, when the window lies outside `out`.
580
671
  */
581
- digestInto(out: u8[], at: i32): void {
582
- if (this.finished) {
583
- panic("Sha256: digest called twice")
584
- }
672
+ digestInto(out: u8[], at: i32): boolean {
585
673
  const size: i32 = toI32(out.length)
586
674
  if (at < 0 || at > size - SHA256_SIZE) {
587
- panic("Sha256: the digest window is outside its array")
588
- }
589
- this.finished = true
590
- // §5.1.1: a single 1 bit, then zeros up to 56 bytes into a block, then the
591
- // message length in bits as a big-endian 64-bit number. When fewer than
592
- // eight bytes are left after the 1 bit, the zeros run into a second block.
593
- // `block` is zero past `fill` only in a fresh hasher, so every zero is
594
- // written rather than assumed.
595
- const block: u8[] = this.block
596
- const blockEnd: i32 = toI32(block.length)
597
- const fill: i32 = this.fill
598
- block[fill] = 0x80
599
- for (let i: i32 = fill + 1; i < blockEnd; i += 1) {
600
- block[i] = 0
675
+ return false
601
676
  }
602
- if (fill >= SHA256_BLOCK - 8) {
603
- sha256Compress(this.state, this.schedule, block, 0)
604
- for (let i: i32 = 0; i < blockEnd; i += 1) {
605
- block[i] = 0
677
+ const state: u32[] = this.state
678
+ if (!this.finished) {
679
+ this.finished = true
680
+ // §5.1.1: a single 1 bit, then zeros up to 56 bytes into a block, then
681
+ // the message length in bits as a big-endian 64-bit number. When fewer
682
+ // than eight bytes are left after the 1 bit, the zeros run into a second
683
+ // block. `block` is zero past `fill` only in a fresh hasher, so every
684
+ // zero is written rather than assumed. `fill` is always below
685
+ // `SHA256_BLOCK`, so each loop runs from it to the block's end.
686
+ const schedule: u32[] = this.schedule
687
+ const block: u8[] = this.block
688
+ const room: i32 = toI32(block.length)
689
+ const fill: i32 = this.fill
690
+ for (let i: i32 = fill; i >= 0 && i < room; i += 1) {
691
+ block[i] = i === fill ? 0x80 : 0
606
692
  }
693
+ if (fill >= 56) {
694
+ sha256Compress(state, schedule, block, 0)
695
+ for (let i: i32 = 0; i < room; i += 1) {
696
+ block[i] = 0
697
+ }
698
+ }
699
+ const bits: u64 = toU64(this.total) << 3
700
+ let shift: u64 = 0
701
+ for (let i: i32 = 63; i >= 56 && i < room; i -= 1) {
702
+ block[i] = toU8(bits >>> shift)
703
+ shift += 8
704
+ }
705
+ sha256Compress(state, schedule, block, 0)
607
706
  }
608
- const bits: u64 = toU64(this.total) << 3
609
- for (let i: i32 = 0; i < 8; i += 1) {
610
- block[SHA256_BLOCK - 1 - i] = toU8(bits >>> toU64(8 * i))
611
- }
612
- sha256Compress(this.state, this.schedule, block, 0)
613
- const state: u32[] = this.state
614
- const words: i32 = toI32(state.length)
615
- for (let i: i32 = 0; i < words; i += 1) {
707
+ let p: i32 = at
708
+ for (let i: i32 = 0; i < 8 && i < toI32(state.length) && p >= 0 && p < size - 3; i += 1) {
616
709
  const word: u32 = state[i]
617
- const p: i32 = at + 4 * i
618
- // Always true once the window check above passed; written out so the
619
- // prover sees each byte's index is inside `out`, and a panic rather
620
- // than a skipped write if an edit ever makes it false.
621
- if (p >= 0 && p + 3 < toI32(out.length)) {
622
- out[p] = toU8(word >>> 24)
623
- out[p + 1] = toU8(word >>> 16)
624
- out[p + 2] = toU8(word >>> 8)
625
- out[p + 3] = toU8(word)
626
- } else {
627
- panic("Sha256: a digest word fell outside its window")
628
- }
710
+ out[p] = toU8(word >>> 24)
711
+ out[p + 1] = toU8(word >>> 16)
712
+ out[p + 2] = toU8(word >>> 8)
713
+ out[p + 3] = toU8(word)
714
+ p += 4
629
715
  }
716
+ return true
630
717
  }
631
718
  }
632
719
 
633
720
  /**
634
721
  * The SHA-256 digest of all of `data`, as a fresh 32-byte array.
635
722
  *
636
- * An array longer than 2^31 - 1 bytes panics. A window's length is an `i32`,
637
- * and under `--number-mode f64` `toI32` of a longer length saturates, so
638
- * hashing `toI32(data.length)` bytes would answer the digest of a prefix: one
639
- * digest for every message that shares its first 2^31 - 1 bytes. The length
640
- * is compared as the `number` it is, before any `toI32`.
723
+ * An array longer than 2^31 - 1 bytes (possible under `--number-mode f64`)
724
+ * is hashed whole, never cut to a prefix (`Sha256.updateAll`).
641
725
  *
642
726
  * @param data The whole message; it is read, not changed.
643
727
  * @returns A fresh 32-byte digest.
644
728
  */
645
729
  export const sha256 = (data: u8[]): u8[] => {
646
- if (data.length > 2147483647) {
647
- panic("sha256: a message longer than 2^31 - 1 bytes")
648
- }
649
730
  const hasher: Sha256 = new Sha256()
650
- const from: i32 = 0
651
- hasher.update(data, from, toI32(data.length))
731
+ hasher.updateAll(data)
652
732
  return hasher.digest()
653
733
  }