functionalscript 0.3.6 → 0.3.8
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/com/cpp/module.f.js +0 -1
- package/com/cs/module.f.js +5 -6
- package/com/rust/module.f.d.ts +3 -3
- package/com/rust/module.f.js +24 -29
- package/com/test/build.f.js +2 -2
- package/com/types/module.f.d.ts +1 -1
- package/com/types/module.f.js +1 -2
- package/com/types/testlib.f.js +0 -1
- package/commonjs/build/module.f.d.ts +2 -2
- package/commonjs/build/module.f.js +3 -6
- package/commonjs/build/test.f.js +0 -4
- package/commonjs/module/function/module.f.d.ts +1 -1
- package/commonjs/module/function/module.f.js +1 -1
- package/commonjs/module/module.f.d.ts +1 -1
- package/commonjs/module/module.f.js +2 -3
- package/commonjs/path/module.f.d.ts +5 -5
- package/commonjs/path/module.f.js +6 -8
- package/commonjs/path/test.f.js +1 -2
- package/crypto/prime_field/module.f.d.ts +2 -2
- package/crypto/prime_field/module.f.js +3 -4
- package/crypto/secp/module.f.d.ts +2 -2
- package/crypto/secp/module.f.js +4 -5
- package/crypto/sha2/module.f.d.ts +75 -12
- package/crypto/sha2/module.f.js +246 -139
- package/crypto/sha2/test.f.d.ts +22 -3
- package/crypto/sha2/test.f.js +120 -67
- package/dev/test/module.f.d.ts +1 -1
- package/dev/test/module.f.js +4 -5
- package/dev/test.f.js +0 -4
- package/djs/module.f.d.ts +1 -1
- package/djs/module.f.js +0 -1
- package/djs/parser/module.f.d.ts +1 -1
- package/djs/parser/module.f.js +0 -2
- package/djs/serializer/module.f.d.ts +2 -2
- package/djs/serializer/module.f.js +0 -2
- package/djs/tokenizer/module.f.js +0 -1
- package/fsc/test.f.js +0 -1
- package/html/module.f.d.ts +1 -1
- package/html/module.f.js +15 -11
- package/html/test.f.js +7 -7
- package/js/tokenizer/module.f.d.ts +2 -2
- package/js/tokenizer/module.f.js +1 -2
- package/json/parser/module.f.d.ts +2 -2
- package/json/parser/module.f.js +0 -3
- package/json/serializer/module.f.js +0 -2
- package/json/tokenizer/module.f.js +0 -1
- package/nanvm-lib/tests/test.f.d.ts +28 -0
- package/nanvm-lib/tests/test.f.js +45 -0
- package/package.json +1 -1
- package/text/ascii/module.f.d.ts +1 -1
- package/text/ascii/module.f.js +2 -3
- package/text/ascii/test.f.js +2 -4
- package/text/module.f.d.ts +18 -3
- package/text/module.f.js +22 -6
- package/text/sgr/module.f.js +1 -1
- package/text/test.f.d.ts +4 -1
- package/text/test.f.js +24 -16
- package/text/utf16/module.f.d.ts +5 -6
- package/text/utf16/module.f.js +9 -10
- package/text/utf16/test.f.js +29 -30
- package/text/utf8/module.f.d.ts +8 -5
- package/text/utf8/module.f.js +3 -5
- package/text/utf8/test.f.js +34 -35
- package/types/array/module.f.d.ts +12 -9
- package/types/array/module.f.js +10 -11
- package/types/array/test.f.d.ts +1 -0
- package/types/array/test.f.js +18 -17
- package/types/bigfloat/module.f.d.ts +1 -1
- package/types/bigfloat/module.f.js +7 -8
- package/types/bigfloat/test.f.js +1 -2
- package/types/bigint/module.f.d.ts +20 -11
- package/types/bigint/module.f.js +19 -20
- package/types/bigint/test.f.d.ts +1 -0
- package/types/bigint/test.f.js +7 -2
- package/types/bit_vec/module.f.d.ts +103 -83
- package/types/bit_vec/module.f.js +68 -110
- package/types/bit_vec/test.f.d.ts +20 -8
- package/types/bit_vec/test.f.js +110 -117
- package/types/btree/find/test.f.js +3 -4
- package/types/btree/module.f.d.ts +3 -3
- package/types/btree/module.f.js +2 -5
- package/types/btree/remove/test.f.js +2 -3
- package/types/btree/set/test.f.js +3 -6
- package/types/btree/test.f.js +0 -1
- package/types/btree/types/module.f.d.ts +3 -3
- package/types/byte_set/module.f.d.ts +2 -2
- package/types/byte_set/module.f.js +0 -2
- package/types/function/compare/module.f.d.ts +1 -1
- package/types/function/compare/module.f.js +0 -1
- package/types/function/compare/test.f.js +1 -2
- package/types/function/module.f.d.ts +1 -1
- package/types/function/module.f.js +1 -1
- package/types/function/operator/module.f.d.ts +2 -3
- package/types/function/operator/module.f.js +5 -5
- package/types/function/test.f.js +1 -2
- package/types/list/module.f.d.ts +1 -1
- package/types/list/module.f.js +9 -9
- package/types/list/test.f.js +2 -4
- package/types/map/module.f.d.ts +6 -6
- package/types/map/module.f.js +6 -15
- package/types/map/test.f.js +1 -2
- package/types/monoid/module.f.d.ts +73 -0
- package/types/monoid/module.f.js +42 -0
- package/types/monoid/test.f.d.ts +5 -0
- package/types/monoid/test.f.js +27 -0
- package/types/nibble_set/module.f.d.ts +2 -3
- package/types/nibble_set/test.f.js +1 -2
- package/types/number/test.f.js +1 -2
- package/types/object/module.f.d.ts +5 -5
- package/types/object/module.f.js +2 -4
- package/types/range_map/module.f.d.ts +4 -4
- package/types/range_map/module.f.js +3 -9
- package/types/result/module.f.d.ts +2 -3
- package/types/sorted_list/module.f.d.ts +6 -6
- package/types/sorted_list/module.f.js +2 -6
- package/types/sorted_list/test.f.js +4 -9
- package/types/sorted_set/module.f.d.ts +1 -1
- package/types/sorted_set/module.f.js +2 -5
- package/types/sorted_set/test.f.js +4 -9
- package/types/string/module.f.d.ts +3 -3
- package/types/string/module.f.js +1 -2
- package/types/string/test.f.js +2 -4
- package/types/string_set/module.f.d.ts +1 -1
- package/types/string_set/module.f.js +5 -11
package/crypto/sha2/module.f.js
CHANGED
|
@@ -1,145 +1,252 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
import { mask } from "../../types/bigint/module.f.js";
|
|
2
|
+
import { vec, length, empty, msb } from "../../types/bit_vec/module.f.js";
|
|
3
|
+
const { concat, popFront, front } = msb;
|
|
4
|
+
const lastOne = vec(1n)(1n);
|
|
5
|
+
const base = ({ logBitLen, k, bs0, bs1, ss0, ss1 }) => {
|
|
6
|
+
const bitLength = 1n << logBitLen;
|
|
7
|
+
const rotr = (d) => {
|
|
8
|
+
const r = bitLength - d;
|
|
9
|
+
return (n) => n >> d | n << r;
|
|
10
|
+
};
|
|
11
|
+
const bigSigma = ([a, b, c]) => {
|
|
12
|
+
const ra = rotr(a);
|
|
13
|
+
const rb = rotr(b);
|
|
14
|
+
const rc = rotr(c);
|
|
15
|
+
return (x) => ra(x) ^ rb(x) ^ rc(x);
|
|
16
|
+
};
|
|
17
|
+
const bigSigma0 = bigSigma(bs0);
|
|
18
|
+
const bigSigma1 = bigSigma(bs1);
|
|
19
|
+
const smallSigma = ([a, b, c]) => {
|
|
20
|
+
const ra = rotr(a);
|
|
21
|
+
const rb = rotr(b);
|
|
22
|
+
return (x) => ra(x) ^ rb(x) ^ (x >> c);
|
|
23
|
+
};
|
|
24
|
+
const smallSigma0 = smallSigma(ss0);
|
|
25
|
+
const smallSigma1 = smallSigma(ss1);
|
|
26
|
+
const ch = (x, y, z) => x & y ^ ~x & z;
|
|
27
|
+
const maj = (x, y, z) => x & y ^ x & z ^ y & z;
|
|
28
|
+
const m = mask(bitLength);
|
|
29
|
+
const wi = ([a0, a1, a2, a3]) => (smallSigma1(a0) + a1 + smallSigma0(a2) + a3) & m;
|
|
30
|
+
const nextW = ([w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, wA, wB, wC, wD, wE, wF]) => {
|
|
31
|
+
w0 = wi([wE, w9, w1, w0]);
|
|
32
|
+
w1 = wi([wF, wA, w2, w1]);
|
|
33
|
+
w2 = wi([w0, wB, w3, w2]);
|
|
34
|
+
w3 = wi([w1, wC, w4, w3]);
|
|
35
|
+
w4 = wi([w2, wD, w5, w4]);
|
|
36
|
+
w5 = wi([w3, wE, w6, w5]);
|
|
37
|
+
w6 = wi([w4, wF, w7, w6]);
|
|
38
|
+
w7 = wi([w5, w0, w8, w7]);
|
|
39
|
+
w8 = wi([w6, w1, w9, w8]);
|
|
40
|
+
w9 = wi([w7, w2, wA, w9]);
|
|
41
|
+
wA = wi([w8, w3, wB, wA]);
|
|
42
|
+
wB = wi([w9, w4, wC, wB]);
|
|
43
|
+
wC = wi([wA, w5, wD, wC]);
|
|
44
|
+
wD = wi([wB, w6, wE, wD]);
|
|
45
|
+
wE = wi([wC, w7, wF, wE]);
|
|
46
|
+
wF = wi([wD, w8, w0, wF]);
|
|
47
|
+
return [w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, wA, wB, wC, wD, wE, wF];
|
|
48
|
+
};
|
|
49
|
+
const kLength = k.length;
|
|
50
|
+
const compressV16 = ([a0, b0, c0, d0, e0, f0, g0, h0]) => (data) => {
|
|
51
|
+
let w = data;
|
|
52
|
+
let a = a0;
|
|
53
|
+
let b = b0;
|
|
54
|
+
let c = c0;
|
|
55
|
+
let d = d0;
|
|
56
|
+
let e = e0;
|
|
57
|
+
let f = f0;
|
|
58
|
+
let g = g0;
|
|
59
|
+
let h = h0;
|
|
60
|
+
let i = 0;
|
|
61
|
+
while (true) {
|
|
62
|
+
const ki = k[i];
|
|
63
|
+
for (let j = 0; j < 16; ++j) {
|
|
64
|
+
const t1 = h + bigSigma1(e) + ch(e, f, g) + ki[j] + w[j];
|
|
65
|
+
const t2 = bigSigma0(a) + maj(a, b, c);
|
|
66
|
+
h = g;
|
|
67
|
+
g = f;
|
|
68
|
+
f = e;
|
|
69
|
+
e = (d + t1) & m;
|
|
70
|
+
d = c;
|
|
71
|
+
c = b;
|
|
72
|
+
b = a;
|
|
73
|
+
a = (t1 + t2) & m;
|
|
74
|
+
}
|
|
75
|
+
++i;
|
|
76
|
+
if (i === kLength) {
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
w = nextW(w);
|
|
16
80
|
}
|
|
17
|
-
|
|
18
|
-
|
|
81
|
+
return [
|
|
82
|
+
(a0 + a) & m,
|
|
83
|
+
(b0 + b) & m,
|
|
84
|
+
(c0 + c) & m,
|
|
85
|
+
(d0 + d) & m,
|
|
86
|
+
(e0 + e) & m,
|
|
87
|
+
(f0 + f) & m,
|
|
88
|
+
(g0 + g) & m,
|
|
89
|
+
(h0 + h) & m,
|
|
90
|
+
];
|
|
91
|
+
};
|
|
92
|
+
const at = (u) => (i) => (u >> (i << logBitLen)) & m;
|
|
93
|
+
const compress = (i) => (u) => {
|
|
94
|
+
const a = at(u);
|
|
95
|
+
return compressV16(i)([
|
|
96
|
+
a(15n),
|
|
97
|
+
a(14n),
|
|
98
|
+
a(13n),
|
|
99
|
+
a(12n),
|
|
100
|
+
a(11n),
|
|
101
|
+
a(10n),
|
|
102
|
+
a(9n),
|
|
103
|
+
a(8n),
|
|
104
|
+
a(7n),
|
|
105
|
+
a(6n),
|
|
106
|
+
a(5n),
|
|
107
|
+
a(4n),
|
|
108
|
+
a(3n),
|
|
109
|
+
a(2n),
|
|
110
|
+
a(1n),
|
|
111
|
+
a(0n),
|
|
112
|
+
]);
|
|
113
|
+
};
|
|
114
|
+
const chunkLength = bitLength << 4n; // * 16
|
|
115
|
+
const fromV8 = (a) => a.reduce((p, v) => (p << bitLength) | v);
|
|
116
|
+
const lastChunkLength = chunkLength - 65n;
|
|
117
|
+
return {
|
|
118
|
+
bitLength,
|
|
119
|
+
chunkLength,
|
|
120
|
+
compress,
|
|
121
|
+
fromV8,
|
|
122
|
+
append: (state) => (v) => {
|
|
123
|
+
let { remainder, hash, len } = state;
|
|
124
|
+
remainder = concat(remainder)(v);
|
|
125
|
+
let remainderLen = length(remainder);
|
|
126
|
+
while (remainderLen >= chunkLength) {
|
|
127
|
+
const [u, nr] = popFront(chunkLength)(remainder);
|
|
128
|
+
hash = compress(hash)(u);
|
|
129
|
+
remainder = nr;
|
|
130
|
+
remainderLen -= chunkLength;
|
|
131
|
+
len += chunkLength;
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
hash,
|
|
135
|
+
len,
|
|
136
|
+
remainder
|
|
137
|
+
};
|
|
138
|
+
},
|
|
139
|
+
end: (hashLength) => {
|
|
140
|
+
const offset = (bitLength << 3n) - hashLength;
|
|
141
|
+
return (state) => {
|
|
142
|
+
const { len, remainder } = state;
|
|
143
|
+
let { hash } = state;
|
|
144
|
+
const rLen = length(remainder);
|
|
145
|
+
let u = front(chunkLength)(concat(remainder)(lastOne));
|
|
146
|
+
// last chunk overflow
|
|
147
|
+
if (rLen > lastChunkLength) {
|
|
148
|
+
hash = compress(hash)(u);
|
|
149
|
+
u = 0n;
|
|
150
|
+
}
|
|
151
|
+
return fromV8(compress(hash)(u | (len + rLen))) >> offset;
|
|
152
|
+
};
|
|
19
153
|
}
|
|
20
|
-
return 0;
|
|
21
154
|
};
|
|
22
|
-
return ({ f, length });
|
|
23
|
-
};
|
|
24
|
-
const rotr = d => {
|
|
25
|
-
const r = 32 - d;
|
|
26
|
-
return n => n >>> d | n << r;
|
|
27
155
|
};
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
};
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
w7 = wi([w5, w0, w8, w7]);
|
|
57
|
-
w8 = wi([w6, w1, w9, w8]);
|
|
58
|
-
w9 = wi([w7, w2, wA, w9]);
|
|
59
|
-
wA = wi([w8, w3, wB, wA]);
|
|
60
|
-
wB = wi([w9, w4, wC, wB]);
|
|
61
|
-
wC = wi([wA, w5, wD, wC]);
|
|
62
|
-
wD = wi([wB, w6, wE, wD]);
|
|
63
|
-
wE = wi([wC, w7, wF, wE]);
|
|
64
|
-
wF = wi([wD, w8, w0, wF]);
|
|
65
|
-
return [w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, wA, wB, wC, wD, wE, wF];
|
|
66
|
-
};
|
|
67
|
-
const k = [
|
|
68
|
-
[
|
|
69
|
-
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
70
|
-
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
71
|
-
],
|
|
72
|
-
[
|
|
73
|
-
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
74
|
-
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
75
|
-
],
|
|
76
|
-
[
|
|
77
|
-
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
78
|
-
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
156
|
+
const sha2 = ({ append, end }, hash, hashLength) => ({
|
|
157
|
+
init: {
|
|
158
|
+
hash,
|
|
159
|
+
len: 0n,
|
|
160
|
+
remainder: empty,
|
|
161
|
+
},
|
|
162
|
+
append,
|
|
163
|
+
end: end(hashLength),
|
|
164
|
+
});
|
|
165
|
+
export const base32 = base({
|
|
166
|
+
logBitLen: 5n,
|
|
167
|
+
k: [
|
|
168
|
+
[
|
|
169
|
+
0x428a2f98n, 0x71374491n, 0xb5c0fbcfn, 0xe9b5dba5n, 0x3956c25bn, 0x59f111f1n, 0x923f82a4n, 0xab1c5ed5n,
|
|
170
|
+
0xd807aa98n, 0x12835b01n, 0x243185ben, 0x550c7dc3n, 0x72be5d74n, 0x80deb1fen, 0x9bdc06a7n, 0xc19bf174n,
|
|
171
|
+
],
|
|
172
|
+
[
|
|
173
|
+
0xe49b69c1n, 0xefbe4786n, 0x0fc19dc6n, 0x240ca1ccn, 0x2de92c6fn, 0x4a7484aan, 0x5cb0a9dcn, 0x76f988dan,
|
|
174
|
+
0x983e5152n, 0xa831c66dn, 0xb00327c8n, 0xbf597fc7n, 0xc6e00bf3n, 0xd5a79147n, 0x06ca6351n, 0x14292967n,
|
|
175
|
+
],
|
|
176
|
+
[
|
|
177
|
+
0x27b70a85n, 0x2e1b2138n, 0x4d2c6dfcn, 0x53380d13n, 0x650a7354n, 0x766a0abbn, 0x81c2c92en, 0x92722c85n,
|
|
178
|
+
0xa2bfe8a1n, 0xa81a664bn, 0xc24b8b70n, 0xc76c51a3n, 0xd192e819n, 0xd6990624n, 0xf40e3585n, 0x106aa070n,
|
|
179
|
+
],
|
|
180
|
+
[
|
|
181
|
+
0x19a4c116n, 0x1e376c08n, 0x2748774cn, 0x34b0bcb5n, 0x391c0cb3n, 0x4ed8aa4an, 0x5b9cca4fn, 0x682e6ff3n,
|
|
182
|
+
0x748f82een, 0x78a5636fn, 0x84c87814n, 0x8cc70208n, 0x90befffan, 0xa4506cebn, 0xbef9a3f7n, 0xc67178f2n,
|
|
183
|
+
],
|
|
79
184
|
],
|
|
80
|
-
[
|
|
81
|
-
|
|
82
|
-
|
|
185
|
+
bs0: [2n, 13n, 22n],
|
|
186
|
+
bs1: [6n, 11n, 25n],
|
|
187
|
+
ss0: [7n, 18n, 3n],
|
|
188
|
+
ss1: [17n, 19n, 10n],
|
|
189
|
+
});
|
|
190
|
+
export const base64 = base({
|
|
191
|
+
logBitLen: 6n,
|
|
192
|
+
k: [
|
|
193
|
+
[
|
|
194
|
+
0x428a2f98d728ae22n, 0x7137449123ef65cdn, 0xb5c0fbcfec4d3b2fn, 0xe9b5dba58189dbbcn,
|
|
195
|
+
0x3956c25bf348b538n, 0x59f111f1b605d019n, 0x923f82a4af194f9bn, 0xab1c5ed5da6d8118n,
|
|
196
|
+
0xd807aa98a3030242n, 0x12835b0145706fben, 0x243185be4ee4b28cn, 0x550c7dc3d5ffb4e2n,
|
|
197
|
+
0x72be5d74f27b896fn, 0x80deb1fe3b1696b1n, 0x9bdc06a725c71235n, 0xc19bf174cf692694n,
|
|
198
|
+
],
|
|
199
|
+
[
|
|
200
|
+
0xe49b69c19ef14ad2n, 0xefbe4786384f25e3n, 0x0fc19dc68b8cd5b5n, 0x240ca1cc77ac9c65n,
|
|
201
|
+
0x2de92c6f592b0275n, 0x4a7484aa6ea6e483n, 0x5cb0a9dcbd41fbd4n, 0x76f988da831153b5n,
|
|
202
|
+
0x983e5152ee66dfabn, 0xa831c66d2db43210n, 0xb00327c898fb213fn, 0xbf597fc7beef0ee4n,
|
|
203
|
+
0xc6e00bf33da88fc2n, 0xd5a79147930aa725n, 0x06ca6351e003826fn, 0x142929670a0e6e70n,
|
|
204
|
+
],
|
|
205
|
+
[
|
|
206
|
+
0x27b70a8546d22ffcn, 0x2e1b21385c26c926n, 0x4d2c6dfc5ac42aedn, 0x53380d139d95b3dfn,
|
|
207
|
+
0x650a73548baf63den, 0x766a0abb3c77b2a8n, 0x81c2c92e47edaee6n, 0x92722c851482353bn,
|
|
208
|
+
0xa2bfe8a14cf10364n, 0xa81a664bbc423001n, 0xc24b8b70d0f89791n, 0xc76c51a30654be30n,
|
|
209
|
+
0xd192e819d6ef5218n, 0xd69906245565a910n, 0xf40e35855771202an, 0x106aa07032bbd1b8n,
|
|
210
|
+
],
|
|
211
|
+
[
|
|
212
|
+
0x19a4c116b8d2d0c8n, 0x1e376c085141ab53n, 0x2748774cdf8eeb99n, 0x34b0bcb5e19b48a8n,
|
|
213
|
+
0x391c0cb3c5c95a63n, 0x4ed8aa4ae3418acbn, 0x5b9cca4f7763e373n, 0x682e6ff3d6b2b8a3n,
|
|
214
|
+
0x748f82ee5defb2fcn, 0x78a5636f43172f60n, 0x84c87814a1f0ab72n, 0x8cc702081a6439ecn,
|
|
215
|
+
0x90befffa23631e28n, 0xa4506cebde82bde9n, 0xbef9a3f7b2c67915n, 0xc67178f2e372532bn,
|
|
216
|
+
],
|
|
217
|
+
[
|
|
218
|
+
0xca273eceea26619cn, 0xd186b8c721c0c207n, 0xeada7dd6cde0eb1en, 0xf57d4f7fee6ed178n,
|
|
219
|
+
0x06f067aa72176fban, 0x0a637dc5a2c898a6n, 0x113f9804bef90daen, 0x1b710b35131c471bn,
|
|
220
|
+
0x28db77f523047d84n, 0x32caab7b40c72493n, 0x3c9ebe0a15c9bebcn, 0x431d67c49c100d4cn,
|
|
221
|
+
0x4cc5d4becb3e42b6n, 0x597f299cfc657e2an, 0x5fcb6fab3ad6faecn, 0x6c44198c4a475817n,
|
|
222
|
+
],
|
|
83
223
|
],
|
|
84
|
-
]
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
++i;
|
|
114
|
-
w = nextW(w);
|
|
115
|
-
}
|
|
116
|
-
return [
|
|
117
|
-
(a0 + a) | 0,
|
|
118
|
-
(b0 + b) | 0,
|
|
119
|
-
(c0 + c) | 0,
|
|
120
|
-
(d0 + d) | 0,
|
|
121
|
-
(e0 + e) | 0,
|
|
122
|
-
(f0 + f) | 0,
|
|
123
|
-
(g0 + g) | 0,
|
|
124
|
-
(h0 + h) | 0,
|
|
125
|
-
];
|
|
126
|
-
};
|
|
127
|
-
const compute = init => input => bitsCount => {
|
|
128
|
-
const { f, length } = padding(input)(bitsCount);
|
|
129
|
-
let result = init;
|
|
130
|
-
const chunkCount = length / 16;
|
|
131
|
-
for (let i = 0; i < chunkCount; i++) {
|
|
132
|
-
const s = i * 16;
|
|
133
|
-
result = compress(result)([
|
|
134
|
-
f(s + 0), f(s + 1), f(s + 2), f(s + 3), f(s + 4), f(s + 5), f(s + 6), f(s + 7),
|
|
135
|
-
f(s + 8), f(s + 9), f(s + 10), f(s + 11), f(s + 12), f(s + 13), f(s + 14), f(s + 15)
|
|
136
|
-
]);
|
|
137
|
-
}
|
|
138
|
-
return result;
|
|
139
|
-
};
|
|
140
|
-
const init256 = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];
|
|
141
|
-
export const computeSha256 = compute(init256);
|
|
142
|
-
const init224 = [0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4];
|
|
143
|
-
export const computeSha224 = compute(init224);
|
|
144
|
-
export const compress256 = compress(init256);
|
|
145
|
-
export const compress224 = compress(init224);
|
|
224
|
+
bs0: [28n, 34n, 39n],
|
|
225
|
+
bs1: [14n, 18n, 41n],
|
|
226
|
+
ss0: [1n, 8n, 7n],
|
|
227
|
+
ss1: [19n, 61n, 6n],
|
|
228
|
+
});
|
|
229
|
+
/** SHA-256 */
|
|
230
|
+
export const sha256 = sha2(base32, [0x6a09e667n, 0xbb67ae85n, 0x3c6ef372n, 0xa54ff53an, 0x510e527fn, 0x9b05688cn, 0x1f83d9abn, 0x5be0cd19n], 256n);
|
|
231
|
+
/** SHA-224 */
|
|
232
|
+
export const sha224 = sha2(base32, [0xc1059ed8n, 0x367cd507n, 0x3070dd17n, 0xf70e5939n, 0xffc00b31n, 0x68581511n, 0x64f98fa7n, 0xbefa4fa4n], 224n);
|
|
233
|
+
/** SHA-512 */
|
|
234
|
+
export const sha512 = sha2(base64, [
|
|
235
|
+
0x6a09e667f3bcc908n, 0xbb67ae8584caa73bn, 0x3c6ef372fe94f82bn, 0xa54ff53a5f1d36f1n,
|
|
236
|
+
0x510e527fade682d1n, 0x9b05688c2b3e6c1fn, 0x1f83d9abfb41bd6bn, 0x5be0cd19137e2179n,
|
|
237
|
+
], 512n);
|
|
238
|
+
/** SHA-384 */
|
|
239
|
+
export const sha384 = sha2(base64, [
|
|
240
|
+
0xcbbb9d5dc1059ed8n, 0x629a292a367cd507n, 0x9159015a3070dd17n, 0x152fecd8f70e5939n,
|
|
241
|
+
0x67332667ffc00b31n, 0x8eb44a8768581511n, 0xdb0c2e0d64f98fa7n, 0x47b5481dbefa4fa4n,
|
|
242
|
+
], 384n);
|
|
243
|
+
/** SHA-512/256 */
|
|
244
|
+
export const sha512x256 = sha2(base64, [
|
|
245
|
+
0x22312194fc2bf72cn, 0x9f555fa3c84c64c2n, 0x2393b86b6f53b151n, 0x963877195940eabdn,
|
|
246
|
+
0x96283ee2a88effe3n, 0xbe5e1e2553863992n, 0x2b0199fc2c85b8aan, 0x0eb72ddc81c52ca2n,
|
|
247
|
+
], 256n);
|
|
248
|
+
/** SHA-512/224 */
|
|
249
|
+
export const sha512x224 = sha2(base64, [
|
|
250
|
+
0x8c3d37c819544da2n, 0x73e1996689dcd4d6n, 0x1dfab7ae32ff9c82n, 0x679dd514582f9fcfn,
|
|
251
|
+
0x0f6d2b697bd44da8n, 0x77e36f7304c48942n, 0x3f9d85a86a1d36c8n, 0x1112e6ad91d692a1n,
|
|
252
|
+
], 224n);
|
package/crypto/sha2/test.f.d.ts
CHANGED
|
@@ -1,9 +1,28 @@
|
|
|
1
1
|
declare const _default: {
|
|
2
|
-
|
|
2
|
+
base: {
|
|
3
|
+
b32: () => {
|
|
4
|
+
s256: () => void;
|
|
5
|
+
s224: () => void;
|
|
6
|
+
};
|
|
7
|
+
b64: () => {
|
|
8
|
+
s512: () => void;
|
|
9
|
+
s385: () => void;
|
|
10
|
+
s512x256: () => void;
|
|
11
|
+
s512x224: () => void;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
sha2: {
|
|
3
15
|
sha256: () => void;
|
|
4
16
|
sha224: () => void;
|
|
17
|
+
sha512: () => void;
|
|
18
|
+
sha384: () => void;
|
|
19
|
+
sha512x256: () => void;
|
|
20
|
+
sha512x224: () => void;
|
|
21
|
+
};
|
|
22
|
+
utf8: (() => void)[];
|
|
23
|
+
fill: () => {
|
|
24
|
+
8: () => void;
|
|
25
|
+
16: () => void;
|
|
5
26
|
};
|
|
6
|
-
sha224compress: (() => void)[];
|
|
7
|
-
sha256: (() => void)[];
|
|
8
27
|
};
|
|
9
28
|
export default _default;
|
package/crypto/sha2/test.f.js
CHANGED
|
@@ -1,82 +1,135 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
// {
|
|
16
|
-
// const result = _.padding([0x11111110])(31)
|
|
17
|
-
// console.log(result.map(toHexString))
|
|
18
|
-
// }
|
|
19
|
-
// {
|
|
20
|
-
// const result = _.padding([0x11111110])(32)
|
|
21
|
-
// console.log(result.map(toHexString))
|
|
22
|
-
// }
|
|
1
|
+
import { msbUtf8 } from "../../text/module.f.js";
|
|
2
|
+
import { empty, msb, vec } from "../../types/bit_vec/module.f.js";
|
|
3
|
+
import { repeat } from "../../types/monoid/module.f.js";
|
|
4
|
+
import { base32, base64, sha224, sha256, sha384, sha512, sha512x224, sha512x256, } from "./module.f.js";
|
|
5
|
+
const { concat: beConcat } = msb;
|
|
6
|
+
const checkEmpty = ({ init, end }) => (x) => {
|
|
7
|
+
const result = end(init);
|
|
8
|
+
if (result !== x) {
|
|
9
|
+
throw [result, x];
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
// https://en.wikipedia.org/wiki/SHA-2#Test_vectors
|
|
13
|
+
//
|
|
14
|
+
// https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing
|
|
23
15
|
export default {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
16
|
+
base: {
|
|
17
|
+
b32: () => {
|
|
18
|
+
const { fromV8, compress, chunkLength } = base32;
|
|
19
|
+
const e = 1n << (chunkLength - 1n);
|
|
20
|
+
return {
|
|
21
|
+
s256: () => {
|
|
22
|
+
const result = fromV8(compress(sha256.init.hash)(e));
|
|
23
|
+
const x = 0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855n;
|
|
24
|
+
if (result !== x) {
|
|
25
|
+
throw [result, x];
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
s224: () => {
|
|
29
|
+
const result = fromV8(compress(sha224.init.hash)(e)) >> 32n;
|
|
30
|
+
const x = 0xd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42fn;
|
|
31
|
+
if (result !== x) {
|
|
32
|
+
throw [result, x];
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
};
|
|
31
36
|
},
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
b64: () => {
|
|
38
|
+
const { fromV8, compress, chunkLength } = base64;
|
|
39
|
+
const e = 1n << (chunkLength - 1n);
|
|
40
|
+
return {
|
|
41
|
+
s512: () => {
|
|
42
|
+
const result = fromV8(compress(sha512.init.hash)(e));
|
|
43
|
+
const x = 0xcf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3en;
|
|
44
|
+
if (result !== x) {
|
|
45
|
+
throw [result, x];
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
s385: () => {
|
|
49
|
+
const result = fromV8(compress(sha384.init.hash)(e)) >> 128n;
|
|
50
|
+
const x = 0x38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95bn;
|
|
51
|
+
if (result !== x) {
|
|
52
|
+
throw [result, x];
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
s512x256: () => {
|
|
56
|
+
const result = fromV8(base64.compress(sha512x256.init.hash)(e)) >> 256n;
|
|
57
|
+
const x = 0xc672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967an;
|
|
58
|
+
if (result !== x) {
|
|
59
|
+
throw [result, x];
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
s512x224: () => {
|
|
63
|
+
const result = fromV8(compress(sha512x224.init.hash)(e)) >> 288n;
|
|
64
|
+
const x = 0x6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4n;
|
|
65
|
+
if (result !== x) {
|
|
66
|
+
throw [result, x];
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
};
|
|
38
70
|
}
|
|
39
71
|
},
|
|
40
|
-
|
|
41
|
-
() =>
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
sha256: [
|
|
72
|
+
sha2: {
|
|
73
|
+
sha256: () => checkEmpty(sha256)(0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855n),
|
|
74
|
+
sha224: () => checkEmpty(sha224)(0xd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42fn),
|
|
75
|
+
sha512: () => checkEmpty(sha512)(0xcf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3en),
|
|
76
|
+
sha384: () => checkEmpty(sha384)(0x38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95bn),
|
|
77
|
+
sha512x256: () => checkEmpty(sha512x256)(0xc672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967an),
|
|
78
|
+
sha512x224: () => checkEmpty(sha512x224)(0x6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4n),
|
|
79
|
+
},
|
|
80
|
+
utf8: [
|
|
50
81
|
() => {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
82
|
+
const s = msbUtf8("The quick brown fox jumps over the lazy dog");
|
|
83
|
+
let state = sha224.init;
|
|
84
|
+
state = sha224.append(state)(s);
|
|
85
|
+
const h = sha224.end(state);
|
|
86
|
+
if (h !== 0x730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525n) {
|
|
87
|
+
throw h;
|
|
56
88
|
}
|
|
57
89
|
},
|
|
58
90
|
() => {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
91
|
+
const s = msbUtf8("The quick brown fox jumps over the lazy dog.");
|
|
92
|
+
let state = sha224.init;
|
|
93
|
+
state = sha224.append(state)(s);
|
|
94
|
+
const h = sha224.end(state);
|
|
95
|
+
if (h !== 0x619cba8e8e05826e9b8c519c0a5c68f4fb653e8a3d8aa04bb2c8cd4cn) {
|
|
96
|
+
throw h;
|
|
64
97
|
}
|
|
65
98
|
},
|
|
66
99
|
() => {
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
throw result[0];
|
|
100
|
+
const s = msbUtf8("hello world");
|
|
101
|
+
if (s !== 0x168656c6c6f20776f726c64n) {
|
|
102
|
+
throw s;
|
|
71
103
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
if (result !== '["3138bb9b","c78df27c","473ecfd1","410f7bd4","5ebac1f5","9cf3ff9c","fe4db77a","ab7aedd3"]') {
|
|
78
|
-
throw result;
|
|
104
|
+
let state = sha256.init;
|
|
105
|
+
state = sha256.append(state)(s);
|
|
106
|
+
const h = sha256.end(state);
|
|
107
|
+
if (h !== 0xb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9n) {
|
|
108
|
+
throw h;
|
|
79
109
|
}
|
|
80
110
|
}
|
|
81
|
-
]
|
|
111
|
+
],
|
|
112
|
+
fill: () => {
|
|
113
|
+
const times = repeat({ identity: empty, operation: beConcat })(vec(32n)(0x31313131n));
|
|
114
|
+
return {
|
|
115
|
+
8: () => {
|
|
116
|
+
const r = times(8n);
|
|
117
|
+
let state = sha256.init;
|
|
118
|
+
state = sha256.append(state)(r);
|
|
119
|
+
const h = sha256.end(state);
|
|
120
|
+
if (h >> 224n !== 0x8a83665fn) {
|
|
121
|
+
throw h;
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
16: () => {
|
|
125
|
+
const r = times(16n);
|
|
126
|
+
let state = sha256.init;
|
|
127
|
+
state = sha256.append(state)(r);
|
|
128
|
+
const h = sha256.end(state);
|
|
129
|
+
if (h !== 0x3138bb9bc78df27c473ecfd1410f7bd45ebac1f59cf3ff9cfe4db77aab7aedd3n) {
|
|
130
|
+
throw h;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}
|
|
82
135
|
};
|
package/dev/test/module.f.d.ts
CHANGED
package/dev/test/module.f.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { entries, fold } from "../../types/list/module.f.js";
|
|
2
2
|
import { reset, fgGreen, fgRed, bold } from "../../text/sgr/module.f.js";
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const timeFormat = a => {
|
|
3
|
+
const isTest = (s) => s.endsWith('test.f.mjs') || s.endsWith('test.f.js') || s.endsWith('test.f.ts');
|
|
4
|
+
const addPass = (delta) => (ts) => ({ ...ts, time: ts.time + delta, pass: ts.pass + 1 });
|
|
5
|
+
const addFail = (delta) => (ts) => ({ ...ts, time: ts.time + delta, fail: ts.fail + 1 });
|
|
6
|
+
const timeFormat = (a) => {
|
|
8
7
|
const y = Math.round(a * 10_000).toString();
|
|
9
8
|
const yl = 5 - y.length;
|
|
10
9
|
const x = '0'.repeat(yl > 0 ? yl : 0) + y;
|