@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.
- package/bin/nish +0 -0
- package/docs/AI.md +287 -129
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +24 -0
- package/runtime/core.mjs +21 -0
- package/runtime/nish.d.ts +167 -24
- package/runtime/nish.mjs +7 -1
- package/runtime/runtime-cross.c +9 -0
- package/runtime/runtime-net.c +51 -13
- package/runtime/runtime-region.c +13 -0
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +83 -25
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +329 -6
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +111 -29
- package/std/README.md +6 -6
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +163 -39
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +141 -0
- package/std/crypto/sha1.ts +157 -57
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1276 -218
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -603
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +881 -269
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +672 -206
- package/std/net/http3.ts +669 -191
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1223 -470
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1521 -632
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +104 -22
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +324 -121
- package/std/net/quic-listener.ts +206 -91
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +584 -258
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +661 -240
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +532 -209
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +325 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +285 -135
- package/std/threads.ts +22 -2
package/std/crypto/sha256.ts
CHANGED
|
@@ -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).
|
|
33
|
-
*
|
|
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
|
|
116
|
-
*
|
|
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
|
|
120
|
-
//
|
|
121
|
-
//
|
|
122
|
-
//
|
|
123
|
-
// `
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
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
|
|
429
|
-
*
|
|
430
|
-
*
|
|
431
|
-
* has to go
|
|
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
|
-
|
|
500
|
-
|
|
501
|
-
const
|
|
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
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
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(
|
|
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 (
|
|
520
|
-
sha256Compress(
|
|
521
|
-
at +=
|
|
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
|
|
543
|
-
//
|
|
544
|
-
|
|
545
|
-
|
|
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
|
-
|
|
552
|
-
|
|
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`
|
|
574
|
-
*
|
|
575
|
-
* digest that fits.
|
|
576
|
-
* `update` after
|
|
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):
|
|
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
|
-
|
|
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
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
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
|
-
|
|
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
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
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
|
|
637
|
-
*
|
|
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
|
-
|
|
651
|
-
hasher.update(data, from, toI32(data.length))
|
|
731
|
+
hasher.updateAll(data)
|
|
652
732
|
return hasher.digest()
|
|
653
733
|
}
|