functionalscript 0.0.300 → 0.0.304

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.300",
3
+ "version": "0.0.304",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/sha2/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ const array = require('../types/array')
2
+
1
3
  /**
2
4
  * @typedef {{
3
5
  * readonly f: (i: number) => number
@@ -5,30 +7,37 @@
5
7
  * }} HashInput
6
8
  */
7
9
 
8
- /**
9
- * @typedef {readonly[number, number, number, number, number, number, number, number]} Hash8
10
- */
10
+ /** @typedef {array.Array8<number>} Hash8 */
11
11
 
12
- /** @type {(input: number) => (pos: number) => number} */
12
+ /** @typedef {array.Array16<number>} Array16 */
13
+
14
+ /** @type {(input: number) => (pos: number) => number} */
13
15
  const appendOne = input => pos => input | (1 << 31 - pos)
14
16
 
15
- /** @type {(input: number) => (pos: number) => number} */
16
- const unsignedMod = a => b => (a % b + b) % b
17
+ /** @type {(input: number) => (pos: number) => number} */
18
+ const mod = a => b => (a % b + b) % b
17
19
 
18
20
  /** @type {(input: readonly number[]) => (bits: number) => HashInput} */
19
- const padding = input => bitsCount =>
20
- {
21
+ const padding = input => bitsCount => {
21
22
  const appendBlockIndex = (bitsCount / 32) | 0
22
- const length = (bitsCount + unsignedMod(447 - bitsCount)(512) + 65) / 32
23
+ const length = (bitsCount + mod(447 - bitsCount)(512) + 65) / 32
23
24
  /** @type {(i: number) => number} */
24
25
  const f = i =>
25
26
  i < appendBlockIndex ?
26
27
  input[i] :
27
28
  i === appendBlockIndex ?
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})
29
+ (appendBlockIndex >= input.length ? 0x8000_0000 : appendOne(input[appendBlockIndex])(bitsCount % 32)) :
30
+ i === length - 2 ?
31
+ (bitsCount / 0x1_0000_0000) | 0 :
32
+ i === length - 1 ?
33
+ bitsCount % 0x1_0000_0000 : 0
34
+ return ({ f, length })
35
+ }
36
+
37
+ /** @type {(d: number) => (n: number) => number} */
38
+ const rotr = d => {
39
+ const r = 32 - d
40
+ return n => n >>> d | n << r
32
41
  }
33
42
 
34
43
  /** @type {(x: number) => (y: number) => (z: number) => number} */
@@ -37,26 +46,32 @@ const ch = x => y => z => x & y ^ ~x & z
37
46
  /** @type {(x: number) => (y: number) => (z: number) => number} */
38
47
  const maj = x => y => z => x & y ^ x & z ^ y & z
39
48
 
40
- /** @type {(n: number) => (d: number) => number} */
41
- const rotr = n => d => n >>> d | n << (32-d)
49
+ /** @type {(d: number) => (n: number) => number} */
50
+ const shr = d => n => n >>> d
42
51
 
43
- /** @type {(n: number) => (d: number) => number} */
44
- const shr = n => d => n >>> d
52
+ /** @type {(a: number) => (b: number) => (c: number) => (x: number) => number} */
53
+ const bigSigma = a => b => c => {
54
+ const ra = rotr(a)
55
+ const rb = rotr(b)
56
+ const rc = rotr(c)
57
+ return x => ra(x) ^ rb(x) ^ rc(x)
58
+ }
45
59
 
46
- /** @type {(x: number) => number} */
47
- const bsig0 = x => rotr(x)(2) ^ rotr(x)(13) ^ rotr(x)(22)
60
+ const bigSigma0 = bigSigma(2)(13)(22)
48
61
 
49
- /** @type {(x: number) => number} */
50
- const bsig1 = x => rotr(x)(6) ^ rotr(x)(11) ^ rotr(x)(25)
62
+ const bigSigma1 = bigSigma(6)(11)(25)
51
63
 
52
- /** @type {(x: number) => number} */
53
- const ssig0 = x => rotr(x)(7) ^ rotr(x)(18) ^ shr(x)(3)
64
+ /** @type {(a: number) => (b: number) => (c: number) => (x: number) => number} */
65
+ const smallSigma = a => b => c => {
66
+ const ra = rotr(a)
67
+ const rb = rotr(b)
68
+ const sc = shr(c)
69
+ return x => ra(x) ^ rb(x) ^ sc(x)
70
+ }
54
71
 
55
- /** @type {(x: number) => number} */
56
- const ssig1 = x => rotr(x)(17) ^ rotr(x)(19) ^ shr(x)(10)
72
+ const smallSigma0 = smallSigma(7)(18)(3)
57
73
 
58
- /** @type {(x: number) => number} */
59
- const mod2pow32 = x => x % 0x100000000
74
+ const smallSigma1 = smallSigma(17)(19)(10)
60
75
 
61
76
  /** @type {Hash8} */
62
77
  const init256 = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]
@@ -67,82 +82,105 @@ const computeSha256 = input => bitsCount => compute(input)(bitsCount)(init256)
67
82
  /** @type {Hash8} */
68
83
  const init224 = [0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4]
69
84
 
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 =>
75
- {
76
- const padded = padding(input)(bitsCount)
77
-
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]
86
-
87
- const k = [
85
+ const k = [
86
+ [
88
87
  0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
89
- 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
88
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174],
89
+ [
90
90
  0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
91
- 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
91
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967],
92
+ [
92
93
  0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
93
- 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
94
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070],
95
+ [
94
96
  0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
95
- 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
96
- ];
97
-
98
- let w = new Array(64)
99
- const chunkCount = padded.length / 16
100
- for(let i = 0; i < chunkCount; i++)
101
- {
102
- for(let t = 0; t < 16; t++)
103
- {
104
- w[t] = padded.f(t + i * 16)
105
- }
97
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2],
98
+ ];
106
99
 
107
- for(let t = 16; t < 64; t++)
108
- {
109
- w[t] = mod2pow32(ssig1(w[t - 2]) + w[t - 7] + ssig0(w[t-15]) + w[t - 16])
110
- }
100
+ /** @type {(input: readonly number[]) => (bitsCount: number) => Hash8} */
101
+ const computeSha224 = input => bitsCount => compute(input)(bitsCount)(init224)
102
+
103
+ /** @type {(a: array.Array4<number>) => number} */
104
+ const wi = a => (smallSigma1(a[0]) + a[1] + smallSigma0(a[2]) + a[3]) | 0
105
+
106
+ /** @type {(input: Array16) => Array16} */
107
+ const nextW = w => {
108
+ const _0 = wi([w[14], w[ 9], w[ 1], w[ 0]])
109
+ const _1 = wi([w[15], w[10], w[ 2], w[ 1]])
110
+ const _2 = wi([ _0, w[11], w[ 3], w[ 2]])
111
+ const _3 = wi([ _1, w[12], w[ 4], w[ 3]])
112
+ const _4 = wi([ _2, w[13], w[ 5], w[ 4]])
113
+ const _5 = wi([ _3, w[14], w[ 6], w[ 5]])
114
+ const _6 = wi([ _4, w[15], w[ 7], w[ 6]])
115
+ const _7 = wi([ _5, _0, w[ 8], w[ 7]])
116
+ const _8 = wi([ _6, _1, w[ 9], w[ 8]])
117
+ const _9 = wi([ _7, _2, w[10], w[ 9]])
118
+ const _A = wi([ _8, _3, w[11], w[10]])
119
+ const _B = wi([ _9, _4, w[12], w[11]])
120
+ const _C = wi([ _A, _5, w[13], w[12]])
121
+ const _D = wi([ _B, _6, w[14], w[13]])
122
+ const _E = wi([ _C, _7, w[15], w[14]])
123
+ const _F = wi([ _D, _8, _0, w[15]])
124
+ return [_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _A, _B, _C, _D, _E, _F]
125
+ }
111
126
 
112
- let a = h0
113
- let b = h1
114
- let c = h2
115
- let d = h3
116
- let e = h4
117
- let f = h5
118
- let g = h6
119
- let h = h7
120
-
121
- for(let t = 0; t < 64; t++)
122
- {
123
- let t1 = mod2pow32(h + bsig1(e) + ch(e)(f)(g) + k[t] + w[t])
124
- let t2 = mod2pow32(bsig0(a) + maj(a)(b)(c))
127
+ /** @type {(init: Hash8) => (data: Array16) => Hash8} */
128
+ const compress = init => data => {
129
+ let w = data
130
+
131
+ let a = init[0]
132
+ let b = init[1]
133
+ let c = init[2]
134
+ let d = init[3]
135
+ let e = init[4]
136
+ let f = init[5]
137
+ let g = init[6]
138
+ let h = init[7]
139
+
140
+ for (let i = 0; i < 4; ++i) {
141
+ const ki = k[i]
142
+ for (let j = 0; j < 16; ++j) {
143
+ const t1 = h + bigSigma1(e) + ch(e)(f)(g) + ki[j] + w[j]
144
+ const t2 = bigSigma0(a) + maj(a)(b)(c)
125
145
  h = g
126
146
  g = f
127
147
  f = e
128
- e = mod2pow32(d + t1)
148
+ e = (d + t1) | 0
129
149
  d = c
130
150
  c = b
131
151
  b = a
132
- a = mod2pow32(t1 + t2)
152
+ a = (t1 + t2) | 0
133
153
  }
154
+ w = nextW(w)
155
+ }
156
+
157
+ return [
158
+ (init[0] + a) | 0,
159
+ (init[1] + b) | 0,
160
+ (init[2] + c) | 0,
161
+ (init[3] + d) | 0,
162
+ (init[4] + e) | 0,
163
+ (init[5] + f) | 0,
164
+ (init[6] + g) | 0,
165
+ (init[7] + h) | 0,
166
+ ]
167
+ }
168
+
169
+ /** @type {(input: readonly number[]) => (bitsCount: number) => (init: Hash8) => Hash8} */
170
+ const compute = input => bitsCount => init => {
171
+ const { f, length } = padding(input)(bitsCount)
172
+
173
+ let result = init
134
174
 
135
- h0 = mod2pow32(h0 + a)
136
- h1 = mod2pow32(h1 + b)
137
- h2 = mod2pow32(h2+ c)
138
- h3 = mod2pow32(h3 + d)
139
- h4 = mod2pow32(h4 + e)
140
- h5 = mod2pow32(h5 + f)
141
- h6 = mod2pow32(h6 + g)
142
- h7 = mod2pow32(h7 + h)
175
+ const chunkCount = length / 16
176
+ for (let i = 0; i < chunkCount; i++) {
177
+ const s = i * 16
178
+ result = compress(result)([
179
+ f(s + 0), f(s + 1), f(s + 2), f(s + 3), f(s + 4), f(s + 5), f(s + 6), f(s + 7),
180
+ f(s + 8), f(s + 9), f(s + 10), f(s + 11), f(s + 12), f(s + 13), f(s + 14), f(s + 15)])
143
181
  }
144
182
 
145
- return [h0, h1, h2, h3, h4, h5, h6, h7]
183
+ return result
146
184
  }
147
185
 
148
186
  module.exports = {
package/sha2/test.js CHANGED
@@ -1,9 +1,13 @@
1
1
  const _ = require('.')
2
2
  const json = require('../json')
3
3
  const { sort } = require('../types/object')
4
+ const list = require('../types/list')
5
+
6
+ /** @type {(a: number) => number} */
7
+ const toU32 = x => (x + 0x1_0000_0000) % 0x1_0000_0000
4
8
 
5
9
  /** @type {(a: number) => string} */
6
- const toHexString = x => x >= 0 ? x.toString(16).padStart(8, '0') : (x + 0x100000000).toString(16).padStart(8, '0')
10
+ const toHexString = x => toU32(x).toString(16).padStart(8, '0')
7
11
 
8
12
  /** @type {(a: readonly json.Unknown[]) => string} */
9
13
  const stringify = a => json.stringify(sort)(a)
@@ -50,7 +54,7 @@ const stringify = a => json.stringify(sort)(a)
50
54
  {
51
55
  const input = Array(8).fill(0x31313131)
52
56
  const result = _.computeSha256(input)(256)
53
- if (result[0] !== 0x8a83665f) { throw result[0] }
57
+ if (toU32(result[0]) !== 0x8a83665f) { throw result[0] }
54
58
  }
55
59
 
56
60
  {
@@ -47,6 +47,18 @@ const seq = require('../list')
47
47
  * @typedef {readonly[T, T, T, T, T]} Array5
48
48
  */
49
49
 
50
+ /**
51
+ * @template T
52
+ * @typedef {readonly[T, T, T, T, T, T, T, T]} Array8
53
+ */
54
+
55
+ /**
56
+ * @template T
57
+ * @typedef {readonly[T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T]} Array16
58
+ */
59
+
60
+ /** @typedef {0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15} Index16 */
61
+
50
62
  /**
51
63
  * @template T
52
64
  * @typedef {Array1<T>| Array2<T> | Array3<T> | Array4<T> | Array5<T>} Array1_5