@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
|
@@ -98,16 +98,17 @@ const POLY1305_HIGH_BIT: i32 = 0x1000000
|
|
|
98
98
|
const chacha20Rotl = (x: u32, n: u32): u32 => (x << n) | (x >>> (32 - n))
|
|
99
99
|
|
|
100
100
|
/**
|
|
101
|
-
* The little-endian `u32` at `buf[at]` to `buf[at + 3]`. The
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
* `
|
|
106
|
-
*
|
|
101
|
+
* The little-endian `u32` at `buf[at]` to `buf[at + 3]`. The last byte is read
|
|
102
|
+
* first, so that its bounds check covers the other three; every caller passes
|
|
103
|
+
* a window inside `buf`. `poly1305Le64` and `chacha20Poly1305TagMatch` read
|
|
104
|
+
* theirs the same way. The copy `tests/cases/ct_asm_chacha20poly1305` holds to
|
|
105
|
+
* the disassembly check reads with `uncheckedGet` and `wrappingAdd` instead,
|
|
106
|
+
* because a bounds or overflow check is a branch that check refuses; the ones
|
|
107
|
+
* here test only the public offset.
|
|
107
108
|
*/
|
|
108
109
|
const chacha20Word = (buf: u8[], at: i32): u32 => {
|
|
109
|
-
const
|
|
110
|
-
return toU32(buf[
|
|
110
|
+
const b3: u32 = toU32(buf[at + 3])
|
|
111
|
+
return toU32(buf[at]) | (toU32(buf[at + 1]) << 8) | (toU32(buf[at + 2]) << 16) | (b3 << 24)
|
|
111
112
|
}
|
|
112
113
|
|
|
113
114
|
/**
|
|
@@ -303,8 +304,8 @@ const chacha20Poly1305Setup = (state: u32[]): u8[] => {
|
|
|
303
304
|
* rather than left to the width of an array length.
|
|
304
305
|
*/
|
|
305
306
|
const chacha20CounterFits = (counter: u32, len: i32): boolean => {
|
|
306
|
-
const blocks:
|
|
307
|
-
return
|
|
307
|
+
const blocks: u64 = (toU64(toU32(len)) + toU64(63)) >>> toU64(6)
|
|
308
|
+
return toU64(counter) + blocks <= toU64(1) << toU64(32)
|
|
308
309
|
}
|
|
309
310
|
|
|
310
311
|
/**
|
|
@@ -322,14 +323,14 @@ const chacha20CounterFits = (counter: u32, len: i32): boolean => {
|
|
|
322
323
|
* for a position outside it.
|
|
323
324
|
*/
|
|
324
325
|
export const chacha20QuarterRound = (state: u32[], a: i32, b: i32, c: i32, d: i32): u32[] | null => {
|
|
325
|
-
const
|
|
326
|
-
if (a < 0 || a >= n || b < 0 || b >= n || c < 0 || c >= n || d < 0 || d >= n) {
|
|
327
|
-
return null
|
|
328
|
-
}
|
|
329
|
-
const x: u32[] = new Array<u32>(n)
|
|
326
|
+
const x: u32[] = new Array<u32>(state.length)
|
|
330
327
|
for (let i: i32 = 0; i < toI32(x.length) && i < toI32(state.length); i++) {
|
|
331
328
|
x[i] = state[i]
|
|
332
329
|
}
|
|
330
|
+
const n: i32 = toI32(x.length)
|
|
331
|
+
if (a < 0 || a >= n || b < 0 || b >= n || c < 0 || c >= n || d < 0 || d >= n) {
|
|
332
|
+
return null
|
|
333
|
+
}
|
|
333
334
|
x[a] = x[a] + x[b]
|
|
334
335
|
x[d] = chacha20Rotl(x[d] ^ x[a], 16)
|
|
335
336
|
x[c] = x[c] + x[d]
|
|
@@ -396,16 +397,16 @@ export const chacha20 = (key: u8[], counter: u32, nonce: u8[], data: u8[]): u8[]
|
|
|
396
397
|
|
|
397
398
|
/** The little-endian `u64` at `buf[at]` to `buf[at + 7]`. */
|
|
398
399
|
const poly1305Le64 = (buf: u8[], at: i32): u64 => {
|
|
399
|
-
const
|
|
400
|
+
const b7: u64 = toU64(buf[at + 7])
|
|
400
401
|
return (
|
|
401
|
-
toU64(buf[
|
|
402
|
-
(toU64(buf[
|
|
403
|
-
(toU64(buf[
|
|
404
|
-
(toU64(buf[
|
|
405
|
-
(toU64(buf[
|
|
406
|
-
(toU64(buf[
|
|
407
|
-
(toU64(buf[
|
|
408
|
-
(
|
|
402
|
+
toU64(buf[at]) |
|
|
403
|
+
(toU64(buf[at + 1]) << 8) |
|
|
404
|
+
(toU64(buf[at + 2]) << 16) |
|
|
405
|
+
(toU64(buf[at + 3]) << 24) |
|
|
406
|
+
(toU64(buf[at + 4]) << 32) |
|
|
407
|
+
(toU64(buf[at + 5]) << 40) |
|
|
408
|
+
(toU64(buf[at + 6]) << 48) |
|
|
409
|
+
(b7 << 56)
|
|
409
410
|
)
|
|
410
411
|
}
|
|
411
412
|
|
|
@@ -541,26 +542,28 @@ const poly1305Finish = (h: u64[], key: u8[], tag: u8[]): void => {
|
|
|
541
542
|
h4 = ctSelect(keep, g4 & 0x3ffffff, h4)
|
|
542
543
|
|
|
543
544
|
const word: u64 = 0xffffffff
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
tag[
|
|
551
|
-
tag[
|
|
552
|
-
tag[
|
|
553
|
-
tag[
|
|
554
|
-
|
|
555
|
-
tag[
|
|
556
|
-
tag[
|
|
557
|
-
tag[
|
|
558
|
-
tag[
|
|
559
|
-
|
|
560
|
-
tag[
|
|
561
|
-
tag[
|
|
562
|
-
tag[
|
|
563
|
-
tag[
|
|
545
|
+
// Every word is summed before the first store, and the last byte is stored
|
|
546
|
+
// first, so that its bounds check covers the other fifteen.
|
|
547
|
+
const f0: u64 = ((h0 | (h1 << 26)) & word) + toU64(chacha20Word(key, 16))
|
|
548
|
+
const f1: u64 = (((h1 >>> 6) | (h2 << 20)) & word) + toU64(chacha20Word(key, 20)) + (f0 >>> 32)
|
|
549
|
+
const f2: u64 = (((h2 >>> 12) | (h3 << 14)) & word) + toU64(chacha20Word(key, 24)) + (f1 >>> 32)
|
|
550
|
+
const f3: u64 = (((h3 >>> 18) | (h4 << 8)) & word) + toU64(chacha20Word(key, 28)) + (f2 >>> 32)
|
|
551
|
+
tag[15] = toU8(f3 >>> 24)
|
|
552
|
+
tag[0] = toU8(f0)
|
|
553
|
+
tag[1] = toU8(f0 >>> 8)
|
|
554
|
+
tag[2] = toU8(f0 >>> 16)
|
|
555
|
+
tag[3] = toU8(f0 >>> 24)
|
|
556
|
+
tag[4] = toU8(f1)
|
|
557
|
+
tag[5] = toU8(f1 >>> 8)
|
|
558
|
+
tag[6] = toU8(f1 >>> 16)
|
|
559
|
+
tag[7] = toU8(f1 >>> 24)
|
|
560
|
+
tag[8] = toU8(f2)
|
|
561
|
+
tag[9] = toU8(f2 >>> 8)
|
|
562
|
+
tag[10] = toU8(f2 >>> 16)
|
|
563
|
+
tag[11] = toU8(f2 >>> 24)
|
|
564
|
+
tag[12] = toU8(f3)
|
|
565
|
+
tag[13] = toU8(f3 >>> 8)
|
|
566
|
+
tag[14] = toU8(f3 >>> 16)
|
|
564
567
|
}
|
|
565
568
|
|
|
566
569
|
/**
|
|
@@ -578,8 +581,12 @@ const poly1305Absorb = (h: u64[], r: u64[], buf: u8[], at: i32, len: i32, zeroPa
|
|
|
578
581
|
const tail: i32 = len & 15
|
|
579
582
|
if (tail > 0) {
|
|
580
583
|
const last: u8[] = new Array<u8>(16)
|
|
581
|
-
|
|
582
|
-
|
|
584
|
+
// Where the tail starts, summed in `u32`: it is inside `buf`, so the sum
|
|
585
|
+
// cannot wrap, and the loop stops at the end of `buf` all the same.
|
|
586
|
+
let k: i32 = toI32(toU32(at) + (toU32(whole) << 4))
|
|
587
|
+
for (let i: i32 = 0; i < tail && i < toI32(last.length) && k >= 0 && k < toI32(buf.length); i++) {
|
|
588
|
+
last[i] = buf[k]
|
|
589
|
+
k = k + 1
|
|
583
590
|
}
|
|
584
591
|
if (zeroPad) {
|
|
585
592
|
poly1305Block(h, r, last, 0, toU64(POLY1305_HIGH_BIT))
|
|
@@ -609,7 +616,9 @@ export const poly1305 = (key: u8[], msg: u8[]): u8[] | null => {
|
|
|
609
616
|
const r: u64[] = new Array<u64>(5)
|
|
610
617
|
poly1305Clamp(key, r)
|
|
611
618
|
poly1305Absorb(h, r, msg, 0, toI32(msg.length), false)
|
|
612
|
-
|
|
619
|
+
// `POLY1305_TAG_SIZE`, written out because a literal length is what proves
|
|
620
|
+
// `poly1305Finish`'s stores in range.
|
|
621
|
+
const tag: u8[] = new Array<u8>(16)
|
|
613
622
|
poly1305Finish(h, key, tag)
|
|
614
623
|
wipe(h)
|
|
615
624
|
wipe(r)
|
|
@@ -656,7 +665,9 @@ const chacha20Poly1305Tag = (polyKey: u8[], aad: u8[], ct: u8[], at: i32, len: i
|
|
|
656
665
|
lengths[8 + i] = toU8(len >>> (8 * i))
|
|
657
666
|
}
|
|
658
667
|
poly1305Block(h, r, lengths, 0, toU64(POLY1305_HIGH_BIT))
|
|
659
|
-
|
|
668
|
+
// `POLY1305_TAG_SIZE`, written out because a literal length is what proves
|
|
669
|
+
// `poly1305Finish`'s stores in range.
|
|
670
|
+
const tag: u8[] = new Array<u8>(16)
|
|
660
671
|
poly1305Finish(h, polyKey, tag)
|
|
661
672
|
wipe(h)
|
|
662
673
|
wipe(r)
|
|
@@ -673,24 +684,24 @@ const chacha20Poly1305Tag = (polyKey: u8[], aad: u8[], ct: u8[], at: i32, len: i
|
|
|
673
684
|
* disassembly check; the caller has checked that `sealed` holds the window.
|
|
674
685
|
*/
|
|
675
686
|
const chacha20Poly1305TagMatch = (tag: u8[], sealed: u8[], at: i32): u32 => {
|
|
676
|
-
const
|
|
687
|
+
const last: u32 = toU32(tag[15] ^ sealed[at + 15])
|
|
677
688
|
const diff: u32 =
|
|
678
|
-
toU32(tag[0] ^ sealed[
|
|
679
|
-
toU32(tag[1] ^ sealed[
|
|
680
|
-
toU32(tag[2] ^ sealed[
|
|
681
|
-
toU32(tag[3] ^ sealed[
|
|
682
|
-
toU32(tag[4] ^ sealed[
|
|
683
|
-
toU32(tag[5] ^ sealed[
|
|
684
|
-
toU32(tag[6] ^ sealed[
|
|
685
|
-
toU32(tag[7] ^ sealed[
|
|
686
|
-
toU32(tag[8] ^ sealed[
|
|
687
|
-
toU32(tag[9] ^ sealed[
|
|
688
|
-
toU32(tag[10] ^ sealed[
|
|
689
|
-
toU32(tag[11] ^ sealed[
|
|
690
|
-
toU32(tag[12] ^ sealed[
|
|
691
|
-
toU32(tag[13] ^ sealed[
|
|
692
|
-
toU32(tag[14] ^ sealed[
|
|
693
|
-
|
|
689
|
+
toU32(tag[0] ^ sealed[at]) |
|
|
690
|
+
toU32(tag[1] ^ sealed[at + 1]) |
|
|
691
|
+
toU32(tag[2] ^ sealed[at + 2]) |
|
|
692
|
+
toU32(tag[3] ^ sealed[at + 3]) |
|
|
693
|
+
toU32(tag[4] ^ sealed[at + 4]) |
|
|
694
|
+
toU32(tag[5] ^ sealed[at + 5]) |
|
|
695
|
+
toU32(tag[6] ^ sealed[at + 6]) |
|
|
696
|
+
toU32(tag[7] ^ sealed[at + 7]) |
|
|
697
|
+
toU32(tag[8] ^ sealed[at + 8]) |
|
|
698
|
+
toU32(tag[9] ^ sealed[at + 9]) |
|
|
699
|
+
toU32(tag[10] ^ sealed[at + 10]) |
|
|
700
|
+
toU32(tag[11] ^ sealed[at + 11]) |
|
|
701
|
+
toU32(tag[12] ^ sealed[at + 12]) |
|
|
702
|
+
toU32(tag[13] ^ sealed[at + 13]) |
|
|
703
|
+
toU32(tag[14] ^ sealed[at + 14]) |
|
|
704
|
+
last
|
|
694
705
|
return ctEq(diff, 0)
|
|
695
706
|
}
|
|
696
707
|
|
|
@@ -722,13 +733,18 @@ export const chacha20Poly1305Seal = (key: u8[], nonce: u8[], aad: u8[], plaintex
|
|
|
722
733
|
) {
|
|
723
734
|
return null
|
|
724
735
|
}
|
|
736
|
+
// Summed in `u32`: `len` is at most `longest`, so the sum is at most
|
|
737
|
+
// 2^31 - 1 and reads back as the same `i32`.
|
|
738
|
+
const total: i32 = toI32(toU32(len) + toU32(POLY1305_TAG_SIZE))
|
|
725
739
|
const state: u32[] = chacha20State(key, 0, nonce, 0)
|
|
726
740
|
const polyKey: u8[] = chacha20Poly1305Setup(state)
|
|
727
|
-
const sealed: u8[] = new Array<u8>(
|
|
741
|
+
const sealed: u8[] = new Array<u8>(total)
|
|
728
742
|
chacha20XorInto(state, plaintext, 0, len, sealed, 0)
|
|
729
743
|
const tag: u8[] = chacha20Poly1305Tag(polyKey, aad, sealed, 0, len)
|
|
730
|
-
|
|
731
|
-
|
|
744
|
+
let k: i32 = len
|
|
745
|
+
for (let i: i32 = 0; i < toI32(tag.length) && k >= 0 && k < toI32(sealed.length); i++) {
|
|
746
|
+
sealed[k] = tag[i]
|
|
747
|
+
k = k + 1
|
|
732
748
|
}
|
|
733
749
|
wipe(state)
|
|
734
750
|
wipe(polyKey)
|
package/std/crypto/ct.ts
CHANGED
|
@@ -81,14 +81,16 @@ export const timingSafeEqualAt = (a: u8[], aOff: i32, b: u8[], bOff: i32, len: i
|
|
|
81
81
|
if (aOff < 0 || bOff < 0 || len < 0 || aOff > aLen - len || bOff > bLen - len) {
|
|
82
82
|
return false
|
|
83
83
|
}
|
|
84
|
-
//
|
|
85
|
-
//
|
|
86
|
-
//
|
|
87
|
-
|
|
88
|
-
|
|
84
|
+
// Two running positions, one per window, rather than `off + k`: the loop
|
|
85
|
+
// condition proves each read in range, and it is the test above that makes
|
|
86
|
+
// the loop run all `len` rounds, so the timing still depends on `len` alone.
|
|
87
|
+
let p: i32 = aOff
|
|
88
|
+
let q: i32 = bOff
|
|
89
89
|
let diff: i32 = 0
|
|
90
|
-
for (let k: i32 = 0; k < len; k++) {
|
|
91
|
-
diff = diff | toI32(a[
|
|
90
|
+
for (let k: i32 = 0; k < len && p >= 0 && p < aLen && q >= 0 && q < bLen; k++) {
|
|
91
|
+
diff = diff | toI32(a[p] ^ b[q])
|
|
92
|
+
p += 1
|
|
93
|
+
q += 1
|
|
92
94
|
}
|
|
93
95
|
return diff === 0
|
|
94
96
|
}
|
package/std/crypto/ed25519.ts
CHANGED
|
@@ -87,7 +87,7 @@
|
|
|
87
87
|
*/
|
|
88
88
|
|
|
89
89
|
import { Secret, expose, exposeWith, wipe } from "nish:secret"
|
|
90
|
-
import {
|
|
90
|
+
import { Sha512 } from "nish/crypto/sha512"
|
|
91
91
|
import {
|
|
92
92
|
f25519Add,
|
|
93
93
|
f25519Add64,
|
|
@@ -126,6 +126,9 @@ const ED25519_HALF: i32 = 32
|
|
|
126
126
|
/** The limbs of one table entry: four field elements of ten. */
|
|
127
127
|
const ED25519_ENTRY: i32 = 40
|
|
128
128
|
|
|
129
|
+
/** The limbs of the table of `1 p` to `8 p`: eight entries. */
|
|
130
|
+
const ED25519_TABLE: i32 = 8 * ED25519_ENTRY
|
|
131
|
+
|
|
129
132
|
/** d = -121665/121666, the curve's constant, little-endian (RFC 8032 §5.1). */
|
|
130
133
|
const ed25519D = (): i64[] => {
|
|
131
134
|
const bytes: u8[] = [
|
|
@@ -355,23 +358,55 @@ const ed25519SetIdentity = (out: Ed25519Point): void => {
|
|
|
355
358
|
f25519Copy(out.t, f25519Zero())
|
|
356
359
|
}
|
|
357
360
|
|
|
361
|
+
/**
|
|
362
|
+
* `dst[j] ^= mask & (dst[j] ^ src[from + j])` for the ten limbs `j`: `src`'s
|
|
363
|
+
* limbs from `from` where `mask` is all ones, `dst` kept where it is zero.
|
|
364
|
+
* `from` and `dst`'s length are tested once, before the walk, and `src`'s
|
|
365
|
+
* length on each limb; every caller here passes all three. The tests read
|
|
366
|
+
* only public lengths and `from`, never `mask`.
|
|
367
|
+
*/
|
|
368
|
+
const ed25519Blend = (dst: i64[], src: i64[], from: i32, mask: i64): void => {
|
|
369
|
+
if (from < 0 || toI32(dst.length) < 10) {
|
|
370
|
+
return
|
|
371
|
+
}
|
|
372
|
+
let k: i32 = from
|
|
373
|
+
for (let j: i32 = 0; j < 10; j++) {
|
|
374
|
+
if (k < toI32(src.length)) {
|
|
375
|
+
dst[j] = dst[j] ^ (mask & (dst[j] ^ src[k]))
|
|
376
|
+
}
|
|
377
|
+
k += 1
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Copies the ten limbs of `src` into `table` from `at`, each index tested
|
|
383
|
+
* against its array as the walk goes.
|
|
384
|
+
*/
|
|
385
|
+
const ed25519Put = (table: i64[], at: i32, src: i64[]): void => {
|
|
386
|
+
let k: i32 = at
|
|
387
|
+
let j: i32 = 0
|
|
388
|
+
while (j < 10 && j < toI32(src.length) && k >= 0 && k < toI32(table.length)) {
|
|
389
|
+
table[k] = src[j]
|
|
390
|
+
k += 1
|
|
391
|
+
j += 1
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
358
395
|
/** Stores the cached `q` as entry `i` of `table`. */
|
|
359
396
|
const ed25519TableStore = (table: i64[], i: i32, q: Ed25519Cached): void => {
|
|
360
|
-
if (
|
|
397
|
+
if (i < 0 || i >= 8) {
|
|
361
398
|
return
|
|
362
399
|
}
|
|
363
400
|
const at: i32 = i * ED25519_ENTRY
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
table[at + 30 + j] = q.z2[j]
|
|
369
|
-
}
|
|
401
|
+
ed25519Put(table, at, q.ypx)
|
|
402
|
+
ed25519Put(table, at + 10, q.ymx)
|
|
403
|
+
ed25519Put(table, at + 20, q.t2d)
|
|
404
|
+
ed25519Put(table, at + 30, q.z2)
|
|
370
405
|
}
|
|
371
406
|
|
|
372
407
|
/** `1 p` to `8 p`, cached, as eight entries of forty limbs. */
|
|
373
408
|
const ed25519Table = (p: Ed25519Point, s: Ed25519Scratch): i64[] => {
|
|
374
|
-
const table: i64[] = new Array<i64>(
|
|
409
|
+
const table: i64[] = new Array<i64>(ED25519_TABLE)
|
|
375
410
|
const one = new Ed25519Cached()
|
|
376
411
|
ed25519ToCached(one, p, s)
|
|
377
412
|
ed25519TableStore(table, 0, one)
|
|
@@ -421,9 +456,6 @@ const ed25519Digits = (k: u8[]): i64[] => {
|
|
|
421
456
|
* by a masked swap and a masked select of the negation.
|
|
422
457
|
*/
|
|
423
458
|
const ed25519Select = (out: Ed25519Cached, table: i64[], digit: i64, s: Ed25519Scratch): void => {
|
|
424
|
-
if (toI32(table.length) < 8 * ED25519_ENTRY || toI32(out.ypx.length) < 10 || toI32(s.a.length) < 10) {
|
|
425
|
-
return
|
|
426
|
-
}
|
|
427
459
|
const sign: i64 = digit >> toI64(63)
|
|
428
460
|
const magnitude: i64 = f25519Sub64(digit ^ sign, sign)
|
|
429
461
|
f25519Copy(out.ypx, f25519Small(toI64(1)))
|
|
@@ -435,18 +467,16 @@ const ed25519Select = (out: Ed25519Cached, table: i64[], digit: i64, s: Ed25519S
|
|
|
435
467
|
// the only difference here whose sign bit is set.
|
|
436
468
|
const hit: i64 = f25519Sub64(magnitude ^ toI64(i + 1), toI64(1)) >> toI64(63)
|
|
437
469
|
const at: i32 = i * ED25519_ENTRY
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
out.z2[j] = out.z2[j] ^ (hit & (out.z2[j] ^ table[at + 30 + j]))
|
|
443
|
-
}
|
|
470
|
+
ed25519Blend(out.ypx, table, at, hit)
|
|
471
|
+
ed25519Blend(out.ymx, table, at + 10, hit)
|
|
472
|
+
ed25519Blend(out.t2d, table, at + 20, hit)
|
|
473
|
+
ed25519Blend(out.z2, table, at + 30, hit)
|
|
444
474
|
}
|
|
445
475
|
f25519Swap(out.ypx, out.ymx, sign & toI64(1))
|
|
446
|
-
for (let j: i32 = 0; j < 10; j++) {
|
|
476
|
+
for (let j: i32 = 0; j < 10 && j < toI32(out.t2d.length) && j < toI32(s.a.length); j++) {
|
|
447
477
|
s.a[j] = f25519Sub64(toI64(0), out.t2d[j])
|
|
448
|
-
out.t2d[j] = out.t2d[j] ^ (sign & (out.t2d[j] ^ s.a[j]))
|
|
449
478
|
}
|
|
479
|
+
ed25519Blend(out.t2d, s.a, 0, sign)
|
|
450
480
|
}
|
|
451
481
|
|
|
452
482
|
/**
|
|
@@ -555,6 +585,9 @@ const ed25519Decode = (bytes: u8[], s: Ed25519Scratch): Ed25519Point | null => {
|
|
|
555
585
|
const y: i64[] = f25519Decode(bytes)
|
|
556
586
|
// f25519Decode takes y modulo p; re-encoding refuses every y of p or more.
|
|
557
587
|
const canonical: u8[] = f25519Encode(y)
|
|
588
|
+
if (toI32(canonical.length) !== 32) {
|
|
589
|
+
return null
|
|
590
|
+
}
|
|
558
591
|
canonical[31] = canonical[31] | (bytes[31] & toU8(0x80))
|
|
559
592
|
if (!ed25519SameBytes(canonical, bytes, ED25519_HALF)) {
|
|
560
593
|
return null
|
|
@@ -590,6 +623,9 @@ const ed25519Decode = (bytes: u8[], s: Ed25519Scratch): Ed25519Point | null => {
|
|
|
590
623
|
}
|
|
591
624
|
|
|
592
625
|
const xBytes: u8[] = f25519Encode(x)
|
|
626
|
+
if (toI32(xBytes.length) !== 32) {
|
|
627
|
+
return null
|
|
628
|
+
}
|
|
593
629
|
if ((xBytes[0] & toU8(1)) !== sign) {
|
|
594
630
|
if (ed25519SameBytes(xBytes, new Array<u8>(32), ED25519_HALF)) {
|
|
595
631
|
return null
|
|
@@ -730,7 +766,8 @@ const ed25519Expand = (seed: u8[]): u8[] => {
|
|
|
730
766
|
hasher.update(seed, ED25519_FROM, ED25519_SEED_SIZE)
|
|
731
767
|
const h: u8[] = hasher.digest()
|
|
732
768
|
ed25519WipeHasher(hasher)
|
|
733
|
-
|
|
769
|
+
// SHA-512's 64 bytes, as a literal the prover reads for h[0] and h[31].
|
|
770
|
+
if (toI32(h.length) < 64) {
|
|
734
771
|
return h
|
|
735
772
|
}
|
|
736
773
|
h[0] = h[0] & toU8(248)
|
|
@@ -780,17 +817,17 @@ const ed25519PublicKeyExposed = (seed: u8[]): u8[] | null => {
|
|
|
780
817
|
* 64 bytes. Signing is deterministic: the same seed and message always give
|
|
781
818
|
* the same signature. Answers `null` unless `seed` is `ED25519_SEED_SIZE`
|
|
782
819
|
* bytes. The public key is derived from the seed again on every call. A
|
|
783
|
-
* message longer than 2^31 - 1 bytes
|
|
820
|
+
* message longer than 2^31 - 1 bytes also answers `null`: its length does
|
|
784
821
|
* not survive `toI32`, and the signature would be of a prefix.
|
|
785
822
|
*
|
|
786
823
|
* @param seed The private key, 32 bytes.
|
|
787
824
|
* @param msg The whole message signed.
|
|
788
825
|
* @returns The 64-byte signature `R || S`, or `null` for a seed of the wrong
|
|
789
|
-
* length.
|
|
826
|
+
* length or a message longer than 2^31 - 1 bytes.
|
|
790
827
|
*/
|
|
791
828
|
export const ed25519Sign = (seed: Secret<u8[]>, msg: u8[]): u8[] | null => {
|
|
792
829
|
if (msg.length > 2147483647) {
|
|
793
|
-
|
|
830
|
+
return null
|
|
794
831
|
}
|
|
795
832
|
return exposeWith(seed, msg, ed25519SignExposed)
|
|
796
833
|
}
|
|
@@ -846,8 +883,8 @@ const ed25519SignExposed = (seed: u8[], msg: u8[]): u8[] | null => {
|
|
|
846
883
|
* `pub` (RFC 8032 §5.1.7): [S]B = R + [k]A with k = SHA-512(R || A || M)
|
|
847
884
|
* mod L, checked as the encoding of [S]B + [k](-A) against R's bytes.
|
|
848
885
|
* Answers `false` for a key or signature of the wrong length, an S of L or
|
|
849
|
-
* more, and a key that does not decode
|
|
850
|
-
* bytes
|
|
886
|
+
* more, and a key that does not decode, and for a message longer than
|
|
887
|
+
* 2^31 - 1 bytes, which `ed25519Sign` refuses too: a check of a prefix would accept the
|
|
851
888
|
* signature for any longer message that shares it. Every value here is
|
|
852
889
|
* public, so the answer may be reached by branches.
|
|
853
890
|
*
|
|
@@ -858,7 +895,7 @@ const ed25519SignExposed = (seed: u8[], msg: u8[]): u8[] | null => {
|
|
|
858
895
|
*/
|
|
859
896
|
export const ed25519Verify = (pub: u8[], msg: u8[], sig: u8[]): boolean => {
|
|
860
897
|
if (msg.length > 2147483647) {
|
|
861
|
-
|
|
898
|
+
return false
|
|
862
899
|
}
|
|
863
900
|
if (toI32(pub.length) !== 32 || toI32(sig.length) !== 64) {
|
|
864
901
|
return false
|