functionalscript 0.0.299 → 0.0.300
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/package.json +1 -1
- package/{types/crypto → sha2}/index.js +47 -84
- package/{types/crypto → sha2}/test.js +9 -6
- package/test.js +1 -1
package/package.json
CHANGED
|
@@ -1,119 +1,88 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @typedef {{
|
|
3
|
-
* readonly
|
|
3
|
+
* readonly f: (i: number) => number
|
|
4
4
|
* readonly length: number
|
|
5
5
|
* }} HashInput
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* @typedef {readonly[number, number, number, number, number, number, number, number]}
|
|
9
|
+
* @typedef {readonly[number, number, number, number, number, number, number, number]} Hash8
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
/** @type {(input: number) => (pos: number) => number} */
|
|
13
|
-
const appendOne = input => pos =>
|
|
14
|
-
{
|
|
15
|
-
return input | (1 << 31 - pos)
|
|
16
|
-
}
|
|
13
|
+
const appendOne = input => pos => input | (1 << 31 - pos)
|
|
17
14
|
|
|
18
15
|
/** @type {(input: number) => (pos: number) => number} */
|
|
19
|
-
const unsignedMod = a => b =>
|
|
20
|
-
{
|
|
21
|
-
return (a % b + b) % b
|
|
22
|
-
}
|
|
16
|
+
const unsignedMod = a => b => (a % b + b) % b
|
|
23
17
|
|
|
24
|
-
/** @type {(input: number[]) => (
|
|
25
|
-
const padding = input =>
|
|
18
|
+
/** @type {(input: readonly number[]) => (bits: number) => HashInput} */
|
|
19
|
+
const padding = input => bitsCount =>
|
|
26
20
|
{
|
|
27
|
-
const appendBlockIndex =
|
|
28
|
-
|
|
29
|
-
const k = unsignedMod(447 - length)(512)
|
|
30
|
-
//console.log(k)
|
|
31
|
-
const outputLength = length + k + 65
|
|
32
|
-
//console.log(outputLength)
|
|
33
|
-
let o = new Array(outputLength / 32)
|
|
34
|
-
//console.log(o.length)
|
|
21
|
+
const appendBlockIndex = (bitsCount / 32) | 0
|
|
22
|
+
const length = (bitsCount + unsignedMod(447 - bitsCount)(512) + 65) / 32
|
|
35
23
|
/** @type {(i: number) => number} */
|
|
36
24
|
const f = i =>
|
|
37
25
|
i < appendBlockIndex ?
|
|
38
26
|
input[i] :
|
|
39
27
|
i === appendBlockIndex ?
|
|
40
|
-
(appendBlockIndex >= input.length ? 0x80000000 : appendOne(input[appendBlockIndex])(
|
|
41
|
-
i ===
|
|
42
|
-
i ===
|
|
43
|
-
|
|
44
|
-
{
|
|
45
|
-
o[i] = f(i)
|
|
46
|
-
}
|
|
47
|
-
return o;
|
|
28
|
+
(appendBlockIndex >= input.length ? 0x80000000 : appendOne(input[appendBlockIndex])(bitsCount % 32)) :
|
|
29
|
+
i === length - 2 ? (bitsCount / 0x100000000) | 0 :
|
|
30
|
+
i === length - 1 ? bitsCount % 0x100000000 : 0
|
|
31
|
+
return ({f, length})
|
|
48
32
|
}
|
|
49
33
|
|
|
50
34
|
/** @type {(x: number) => (y: number) => (z: number) => number} */
|
|
51
|
-
const ch = x => y => z =>
|
|
52
|
-
{
|
|
53
|
-
return x & y ^ ~x & z
|
|
54
|
-
}
|
|
35
|
+
const ch = x => y => z => x & y ^ ~x & z
|
|
55
36
|
|
|
56
37
|
/** @type {(x: number) => (y: number) => (z: number) => number} */
|
|
57
|
-
const maj = x => y => z =>
|
|
58
|
-
{
|
|
59
|
-
return x & y ^ x & z ^ y & z
|
|
60
|
-
}
|
|
38
|
+
const maj = x => y => z => x & y ^ x & z ^ y & z
|
|
61
39
|
|
|
62
40
|
/** @type {(n: number) => (d: number) => number} */
|
|
63
|
-
const rotr = n => d =>
|
|
64
|
-
{
|
|
65
|
-
return n >>> d | n << (32-d)
|
|
66
|
-
}
|
|
41
|
+
const rotr = n => d => n >>> d | n << (32-d)
|
|
67
42
|
|
|
68
43
|
/** @type {(n: number) => (d: number) => number} */
|
|
69
|
-
const shr = n => d =>
|
|
70
|
-
{
|
|
71
|
-
return n >>> d
|
|
72
|
-
}
|
|
44
|
+
const shr = n => d => n >>> d
|
|
73
45
|
|
|
74
46
|
/** @type {(x: number) => number} */
|
|
75
|
-
const bsig0 = x =>
|
|
76
|
-
{
|
|
77
|
-
return rotr(x)(2) ^ rotr(x)(13) ^ rotr(x)(22)
|
|
78
|
-
}
|
|
47
|
+
const bsig0 = x => rotr(x)(2) ^ rotr(x)(13) ^ rotr(x)(22)
|
|
79
48
|
|
|
80
49
|
/** @type {(x: number) => number} */
|
|
81
|
-
const bsig1 = x =>
|
|
82
|
-
{
|
|
83
|
-
return rotr(x)(6) ^ rotr(x)(11) ^ rotr(x)(25)
|
|
84
|
-
}
|
|
50
|
+
const bsig1 = x => rotr(x)(6) ^ rotr(x)(11) ^ rotr(x)(25)
|
|
85
51
|
|
|
86
52
|
/** @type {(x: number) => number} */
|
|
87
|
-
const ssig0 = x =>
|
|
88
|
-
{
|
|
89
|
-
return rotr(x)(7) ^ rotr(x)(18) ^ shr(x)(3)
|
|
90
|
-
}
|
|
53
|
+
const ssig0 = x => rotr(x)(7) ^ rotr(x)(18) ^ shr(x)(3)
|
|
91
54
|
|
|
92
55
|
/** @type {(x: number) => number} */
|
|
93
|
-
const ssig1 = x =>
|
|
94
|
-
{
|
|
95
|
-
return rotr(x)(17) ^ rotr(x)(19) ^ shr(x)(10)
|
|
96
|
-
}
|
|
56
|
+
const ssig1 = x => rotr(x)(17) ^ rotr(x)(19) ^ shr(x)(10)
|
|
97
57
|
|
|
98
58
|
/** @type {(x: number) => number} */
|
|
99
|
-
const mod2pow32 = x =>
|
|
100
|
-
{
|
|
101
|
-
return x % 4294967296
|
|
102
|
-
}
|
|
59
|
+
const mod2pow32 = x => x % 0x100000000
|
|
103
60
|
|
|
104
|
-
/** @type {
|
|
105
|
-
const
|
|
61
|
+
/** @type {Hash8} */
|
|
62
|
+
const init256 = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]
|
|
63
|
+
|
|
64
|
+
/** @type {(input: readonly number[]) => (bitsCount: number) => Hash8} */
|
|
65
|
+
const computeSha256 = input => bitsCount => compute(input)(bitsCount)(init256)
|
|
66
|
+
|
|
67
|
+
/** @type {Hash8} */
|
|
68
|
+
const init224 = [0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4]
|
|
69
|
+
|
|
70
|
+
/** @type {(input: readonly number[]) => (bitsCount: number) => Hash8} */
|
|
71
|
+
const computeSha224 = input => bitsCount => compute(input)(bitsCount)(init224)
|
|
72
|
+
|
|
73
|
+
/** @type {(input: readonly number[]) => (bitsCount: number) => (init: Hash8) => Hash8} */
|
|
74
|
+
const compute = input => bitsCount => init =>
|
|
106
75
|
{
|
|
107
|
-
const padded = padding(input)(
|
|
76
|
+
const padded = padding(input)(bitsCount)
|
|
108
77
|
|
|
109
|
-
let h0 =
|
|
110
|
-
let h1 =
|
|
111
|
-
let h2 =
|
|
112
|
-
let h3 =
|
|
113
|
-
let h4 =
|
|
114
|
-
let h5 =
|
|
115
|
-
let h6 =
|
|
116
|
-
let h7 =
|
|
78
|
+
let h0 = init[0]
|
|
79
|
+
let h1 = init[1]
|
|
80
|
+
let h2 = init[2]
|
|
81
|
+
let h3 = init[3]
|
|
82
|
+
let h4 = init[4]
|
|
83
|
+
let h5 = init[5]
|
|
84
|
+
let h6 = init[6]
|
|
85
|
+
let h7 = init[7]
|
|
117
86
|
|
|
118
87
|
const k = [
|
|
119
88
|
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
@@ -126,19 +95,13 @@ const computeSha256 = input => length =>
|
|
|
126
95
|
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
127
96
|
];
|
|
128
97
|
|
|
129
|
-
// /** @type {(a: number) => string} */
|
|
130
|
-
// const toHexString = x =>
|
|
131
|
-
// {
|
|
132
|
-
// return x >= 0 ? x.toString(16).padStart(8, '0') : (x + 0x100000000).toString(16).padStart(8, '0')
|
|
133
|
-
// }
|
|
134
|
-
|
|
135
98
|
let w = new Array(64)
|
|
136
99
|
const chunkCount = padded.length / 16
|
|
137
100
|
for(let i = 0; i < chunkCount; i++)
|
|
138
101
|
{
|
|
139
102
|
for(let t = 0; t < 16; t++)
|
|
140
103
|
{
|
|
141
|
-
w[t] = padded
|
|
104
|
+
w[t] = padded.f(t + i * 16)
|
|
142
105
|
}
|
|
143
106
|
|
|
144
107
|
for(let t = 16; t < 64; t++)
|
|
@@ -146,8 +109,6 @@ const computeSha256 = input => length =>
|
|
|
146
109
|
w[t] = mod2pow32(ssig1(w[t - 2]) + w[t - 7] + ssig0(w[t-15]) + w[t - 16])
|
|
147
110
|
}
|
|
148
111
|
|
|
149
|
-
//console.log(w.map(toHexString))
|
|
150
|
-
|
|
151
112
|
let a = h0
|
|
152
113
|
let b = h1
|
|
153
114
|
let c = h2
|
|
@@ -189,4 +150,6 @@ module.exports = {
|
|
|
189
150
|
padding,
|
|
190
151
|
/** @readonly */
|
|
191
152
|
computeSha256,
|
|
153
|
+
/** @readonly */
|
|
154
|
+
computeSha224
|
|
192
155
|
}
|
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
const _ = require('.')
|
|
2
|
-
const json = require('
|
|
3
|
-
const { sort } = require('../object')
|
|
2
|
+
const json = require('../json')
|
|
3
|
+
const { sort } = require('../types/object')
|
|
4
4
|
|
|
5
5
|
/** @type {(a: number) => string} */
|
|
6
|
-
const toHexString = x =>
|
|
7
|
-
{
|
|
8
|
-
return x >= 0 ? x.toString(16).padStart(8, '0') : (x + 0x100000000).toString(16).padStart(8, '0')
|
|
9
|
-
}
|
|
6
|
+
const toHexString = x => x >= 0 ? x.toString(16).padStart(8, '0') : (x + 0x100000000).toString(16).padStart(8, '0')
|
|
10
7
|
|
|
11
8
|
/** @type {(a: readonly json.Unknown[]) => string} */
|
|
12
9
|
const stringify = a => json.stringify(sort)(a)
|
|
@@ -37,6 +34,12 @@ const stringify = a => json.stringify(sort)(a)
|
|
|
37
34
|
if (result !== '["e3b0c442","98fc1c14","9afbf4c8","996fb924","27ae41e4","649b934c","a495991b","7852b855"]') { throw result }
|
|
38
35
|
}
|
|
39
36
|
|
|
37
|
+
{
|
|
38
|
+
const hash = _.computeSha224([])(0)
|
|
39
|
+
const result = stringify(hash.map(toHexString))
|
|
40
|
+
if (result !== '["d14a028c","2a3a2bc9","476102bb","288234c4","15a2b01f","828ea62a","c5b3e42f","bdd387cb"]') { throw result }
|
|
41
|
+
}
|
|
42
|
+
|
|
40
43
|
{
|
|
41
44
|
//[0x68656C6C, 0x6F20776F, 0x726C6400] represents phrase 'hello world'
|
|
42
45
|
const hash = _.computeSha256([0x68656C6C, 0x6F20776F, 0x726C6400])(88)
|
package/test.js
CHANGED