@quenty/ellipticcurvecryptography 1.11.0 → 1.13.0
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/CHANGELOG.md +21 -87
- package/deploy.nevermore.json +10 -0
- package/package.json +2 -2
- package/src/Shared/EllipticCurveCryptography/arith.lua +33 -23
- package/src/Shared/EllipticCurveCryptography/chacha20.lua +31 -20
- package/src/Shared/EllipticCurveCryptography/curve.lua +8 -8
- package/src/Shared/EllipticCurveCryptography/init.lua +49 -40
- package/src/Shared/EllipticCurveCryptography/modp.lua +23 -20
- package/src/Shared/EllipticCurveCryptography/modq.lua +53 -51
- package/src/Shared/EllipticCurveCryptography/random.lua +9 -8
- package/src/Shared/EllipticCurveCryptography/sha256.lua +43 -33
- package/src/Shared/EllipticCurveCryptography/twoPower.lua +4 -4
- package/test/default.project.json +25 -0
- package/test/scripts/Server/ServerMain.server.lua +13 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,170 +3,104 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
-
# [1.
|
|
6
|
+
# [1.13.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/ellipticcurvecryptography@1.12.0...@quenty/ellipticcurvecryptography@1.13.0) (2026-07-14)
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
### Features
|
|
9
9
|
|
|
10
|
+
- **ellipticcurvecryptography:** convert package to --!strict ([48e814a](https://github.com/Quenty/NevermoreEngine/commit/48e814abb5f01f54627dd4f827a287fb98f405bc))
|
|
10
11
|
|
|
12
|
+
# [1.12.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/ellipticcurvecryptography@1.11.0...@quenty/ellipticcurvecryptography@1.12.0) (2026-05-29)
|
|
13
|
+
|
|
14
|
+
**Note:** Version bump only for package @quenty/ellipticcurvecryptography
|
|
11
15
|
|
|
16
|
+
# [1.11.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/ellipticcurvecryptography@1.10.0...@quenty/ellipticcurvecryptography@1.11.0) (2026-04-23)
|
|
12
17
|
|
|
18
|
+
**Note:** Version bump only for package @quenty/ellipticcurvecryptography
|
|
13
19
|
|
|
14
20
|
# [1.10.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/ellipticcurvecryptography@1.9.0...@quenty/ellipticcurvecryptography@1.10.0) (2026-02-20)
|
|
15
21
|
|
|
16
|
-
|
|
17
22
|
### Features
|
|
18
23
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
+
- **cli:** add CI annotation support to game and plugin templates ([#666](https://github.com/Quenty/NevermoreEngine/issues/666)) ([098971b](https://github.com/Quenty/NevermoreEngine/commit/098971bb04dfe40be09f475330b69a48b38c54c5))
|
|
24
25
|
|
|
25
26
|
# [1.9.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/ellipticcurvecryptography@1.8.0...@quenty/ellipticcurvecryptography@1.9.0) (2026-02-19)
|
|
26
27
|
|
|
27
|
-
|
|
28
28
|
### Features
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
- **cli:** add aggregated batch test execution ([58f20e4](https://github.com/Quenty/NevermoreEngine/commit/58f20e437540c597ea445f47d47076c08ea9ca21))
|
|
35
31
|
|
|
36
32
|
# [1.8.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/ellipticcurvecryptography@1.7.3...@quenty/ellipticcurvecryptography@1.8.0) (2026-02-17)
|
|
37
33
|
|
|
38
|
-
|
|
39
34
|
### Features
|
|
40
35
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
36
|
+
- Add CI/CD batch unit testing ([016a366](https://github.com/Quenty/NevermoreEngine/commit/016a3663509c8fffa518b07cfc49ebecba2a1fa6))
|
|
46
37
|
|
|
47
38
|
## [1.7.3](https://github.com/Quenty/NevermoreEngine/compare/@quenty/ellipticcurvecryptography@1.7.2...@quenty/ellipticcurvecryptography@1.7.3) (2026-01-12)
|
|
48
39
|
|
|
49
|
-
|
|
50
40
|
### Bug Fixes
|
|
51
41
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
42
|
+
- Fix dependency installation process for pnpm ([9651b09](https://github.com/Quenty/NevermoreEngine/commit/9651b09af34f252ef3f9c7c539793cc878dd8cba))
|
|
57
43
|
|
|
58
44
|
## [1.7.2](https://github.com/Quenty/NevermoreEngine/compare/@quenty/ellipticcurvecryptography@1.7.1...@quenty/ellipticcurvecryptography@1.7.2) (2026-01-10)
|
|
59
45
|
|
|
60
46
|
**Note:** Version bump only for package @quenty/ellipticcurvecryptography
|
|
61
47
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
48
|
## [1.7.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/ellipticcurvecryptography@1.7.0...@quenty/ellipticcurvecryptography@1.7.1) (2026-01-03)
|
|
67
49
|
|
|
68
50
|
**Note:** Version bump only for package @quenty/ellipticcurvecryptography
|
|
69
51
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
52
|
# [1.7.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/ellipticcurvecryptography@1.6.2...@quenty/ellipticcurvecryptography@1.7.0) (2025-05-10)
|
|
75
53
|
|
|
76
54
|
**Note:** Version bump only for package @quenty/ellipticcurvecryptography
|
|
77
55
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
56
|
## [1.6.2](https://github.com/Quenty/NevermoreEngine/compare/@quenty/ellipticcurvecryptography@1.6.0...@quenty/ellipticcurvecryptography@1.6.2) (2025-04-07)
|
|
83
57
|
|
|
84
|
-
|
|
85
58
|
### Bug Fixes
|
|
86
59
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
60
|
+
- Add types to packages ([2374fb2](https://github.com/Quenty/NevermoreEngine/commit/2374fb2b043cfbe0e9b507b3316eec46a4e353a0))
|
|
61
|
+
- Bump package versions for republishing ([ba47c62](https://github.com/Quenty/NevermoreEngine/commit/ba47c62e32170bf74377b0c658c60b84306dc294))
|
|
93
62
|
|
|
94
63
|
## [1.6.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/ellipticcurvecryptography@1.6.0...@quenty/ellipticcurvecryptography@1.6.1) (2025-04-07)
|
|
95
64
|
|
|
96
|
-
|
|
97
65
|
### Bug Fixes
|
|
98
66
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
67
|
+
- Add types to packages ([2374fb2](https://github.com/Quenty/NevermoreEngine/commit/2374fb2b043cfbe0e9b507b3316eec46a4e353a0))
|
|
104
68
|
|
|
105
69
|
# [1.6.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/ellipticcurvecryptography@1.5.0...@quenty/ellipticcurvecryptography@1.6.0) (2025-04-02)
|
|
106
70
|
|
|
107
|
-
|
|
108
71
|
### Bug Fixes
|
|
109
72
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
73
|
+
- Fix broken tests ([96485ff](https://github.com/Quenty/NevermoreEngine/commit/96485ff69dbba007d13ea4b5fe82f67244fda14c))
|
|
115
74
|
|
|
116
75
|
# [1.5.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/ellipticcurvecryptography@1.4.0...@quenty/ellipticcurvecryptography@1.5.0) (2024-09-25)
|
|
117
76
|
|
|
118
|
-
|
|
119
77
|
### Bug Fixes
|
|
120
78
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
79
|
+
- Rename `:sub()` to `string.sub` ([6874e06](https://github.com/Quenty/NevermoreEngine/commit/6874e06e456d7094a2d7f25a3a7b24a40d77fe3c))
|
|
126
80
|
|
|
127
81
|
# [1.4.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/ellipticcurvecryptography@1.3.0...@quenty/ellipticcurvecryptography@1.4.0) (2024-05-09)
|
|
128
82
|
|
|
129
|
-
|
|
130
83
|
### Bug Fixes
|
|
131
84
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
85
|
+
- Fix .package-lock.json replicating in packages ([75d0efe](https://github.com/Quenty/NevermoreEngine/commit/75d0efeef239f221d93352af71a5b3e930ec23c5))
|
|
137
86
|
|
|
138
87
|
# [1.3.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/ellipticcurvecryptography@1.2.0...@quenty/ellipticcurvecryptography@1.3.0) (2024-05-03)
|
|
139
88
|
|
|
140
|
-
|
|
141
89
|
### Bug Fixes
|
|
142
90
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
91
|
+
- Native calls was erroring stuff ([949c41d](https://github.com/Quenty/NevermoreEngine/commit/949c41de617e38c33da8d7d4351cc9fa34ae529d))
|
|
148
92
|
|
|
149
93
|
# [1.2.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/ellipticcurvecryptography@1.1.0...@quenty/ellipticcurvecryptography@1.2.0) (2023-12-14)
|
|
150
94
|
|
|
151
|
-
|
|
152
95
|
### Performance Improvements
|
|
153
96
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
97
|
+
- Improve performance of elliptic curve crypto module, saving 100ms of startup compute time on server start ([b9b500e](https://github.com/Quenty/NevermoreEngine/commit/b9b500e33b0b297c0510b28ce272c553d19408e6))
|
|
159
98
|
|
|
160
99
|
# 1.1.0 (2023-03-31)
|
|
161
100
|
|
|
162
|
-
|
|
163
101
|
### Features
|
|
164
102
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
103
|
+
- Add EllipticCurveCryptography package ([18e7eb1](https://github.com/Quenty/NevermoreEngine/commit/18e7eb1b0d6cb833c783401f743b4f6258bcc2fc))
|
|
170
104
|
|
|
171
105
|
# v1.1.0 (Fri Mar 31 2023)
|
|
172
106
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quenty/ellipticcurvecryptography",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.13.0",
|
|
4
4
|
"description": "Elliptic curve cryptography forked from BoatBomber, forked from ComputerCraft",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Roblox",
|
|
@@ -35,5 +35,5 @@
|
|
|
35
35
|
"publishConfig": {
|
|
36
36
|
"access": "public"
|
|
37
37
|
},
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "13f0162f4971a77378e55e9b7236aea94f0dd3a8"
|
|
39
39
|
}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
--!
|
|
1
|
+
--!strict
|
|
2
2
|
-- Big integer arithmetic for 168-bit (and 336-bit) numbers
|
|
3
3
|
-- Numbers are represented as little-endian tables of 24-bit integers
|
|
4
4
|
local twoPower = require(script.Parent.twoPower)
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
type BigInt = { number }
|
|
7
|
+
type DoubleInt = { number }
|
|
8
|
+
|
|
9
|
+
local function isEqual(a: BigInt, b: BigInt): boolean
|
|
7
10
|
return a[1] == b[1]
|
|
8
11
|
and a[2] == b[2]
|
|
9
12
|
and a[3] == b[3]
|
|
@@ -13,7 +16,7 @@ local function isEqual(a, b)
|
|
|
13
16
|
and a[7] == b[7]
|
|
14
17
|
end
|
|
15
18
|
|
|
16
|
-
local function compare(a, b)
|
|
19
|
+
local function compare(a: BigInt, b: BigInt): number
|
|
17
20
|
for i = 7, 1, -1 do
|
|
18
21
|
if a[i] > b[i] then
|
|
19
22
|
return 1
|
|
@@ -25,7 +28,7 @@ local function compare(a, b)
|
|
|
25
28
|
return 0
|
|
26
29
|
end
|
|
27
30
|
|
|
28
|
-
local function add(a, b)
|
|
31
|
+
local function add(a: BigInt, b: BigInt): BigInt
|
|
29
32
|
-- c7 may be greater than 2^24 before reduction
|
|
30
33
|
local c1 = a[1] + b[1]
|
|
31
34
|
local c2 = a[2] + b[2]
|
|
@@ -63,7 +66,7 @@ local function add(a, b)
|
|
|
63
66
|
return { c1, c2, c3, c4, c5, c6, c7 }
|
|
64
67
|
end
|
|
65
68
|
|
|
66
|
-
local function sub(a, b)
|
|
69
|
+
local function sub(a: BigInt, b: BigInt): BigInt
|
|
67
70
|
-- c7 may be negative before reduction
|
|
68
71
|
local c1 = a[1] - b[1]
|
|
69
72
|
local c2 = a[2] - b[2]
|
|
@@ -101,7 +104,7 @@ local function sub(a, b)
|
|
|
101
104
|
return { c1, c2, c3, c4, c5, c6, c7 }
|
|
102
105
|
end
|
|
103
106
|
|
|
104
|
-
local function rShift(a)
|
|
107
|
+
local function rShift(a: BigInt): BigInt
|
|
105
108
|
local c1 = a[1]
|
|
106
109
|
local c2 = a[2]
|
|
107
110
|
local c3 = a[3]
|
|
@@ -134,7 +137,7 @@ local function rShift(a)
|
|
|
134
137
|
return { c1, c2, c3, c4, c5, c6, c7 }
|
|
135
138
|
end
|
|
136
139
|
|
|
137
|
-
local function addDouble(a, b)
|
|
140
|
+
local function addDouble(a: DoubleInt, b: DoubleInt): DoubleInt
|
|
138
141
|
-- a and b are 336-bit integers (14 words)
|
|
139
142
|
local c1 = a[1] + b[1]
|
|
140
143
|
local c2 = a[2] + b[2]
|
|
@@ -207,7 +210,7 @@ local function addDouble(a, b)
|
|
|
207
210
|
return { c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14 }
|
|
208
211
|
end
|
|
209
212
|
|
|
210
|
-
local function mult(a, b, half_multiply)
|
|
213
|
+
local function mult(a: BigInt, b: BigInt, half_multiply: boolean): DoubleInt
|
|
211
214
|
local a1, a2, a3, a4, a5, a6, a7 = a[1], a[2], a[3], a[4], a[5], a[6], a[7]
|
|
212
215
|
local b1, b2, b3, b4, b5, b6, b7 = b[1], b[2], b[3], b[4], b[5], b[6], b[7]
|
|
213
216
|
|
|
@@ -218,7 +221,13 @@ local function mult(a, b, half_multiply)
|
|
|
218
221
|
local c5 = a1 * b5 + a2 * b4 + a3 * b3 + a4 * b2 + a5 * b1
|
|
219
222
|
local c6 = a1 * b6 + a2 * b5 + a3 * b4 + a4 * b3 + a5 * b2 + a6 * b1
|
|
220
223
|
local c7 = a1 * b7 + a2 * b6 + a3 * b5 + a4 * b4 + a5 * b3 + a6 * b2 + a7 * b1
|
|
221
|
-
local c8
|
|
224
|
+
local c8: number = 0
|
|
225
|
+
local c9: number = 0
|
|
226
|
+
local c10: number = 0
|
|
227
|
+
local c11: number = 0
|
|
228
|
+
local c12: number = 0
|
|
229
|
+
local c13: number = 0
|
|
230
|
+
local c14: number = 0
|
|
222
231
|
if not half_multiply then
|
|
223
232
|
c8 = a2 * b7 + a3 * b6 + a4 * b5 + a5 * b4 + a6 * b3 + a7 * b2
|
|
224
233
|
c9 = a3 * b7 + a4 * b6 + a5 * b5 + a6 * b4 + a7 * b3
|
|
@@ -277,7 +286,7 @@ local function mult(a, b, half_multiply)
|
|
|
277
286
|
return { c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14 }
|
|
278
287
|
end
|
|
279
288
|
|
|
280
|
-
local function square(a)
|
|
289
|
+
local function square(a: BigInt): DoubleInt
|
|
281
290
|
-- returns a 336-bit integer (14 words)
|
|
282
291
|
local a1, a2, a3, a4, a5, a6, a7 = a[1], a[2], a[3], a[4], a[5], a[6], a[7]
|
|
283
292
|
|
|
@@ -340,8 +349,8 @@ local function square(a)
|
|
|
340
349
|
return { c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14 }
|
|
341
350
|
end
|
|
342
351
|
|
|
343
|
-
local function encodeInt(a)
|
|
344
|
-
local enc = table.create(21)
|
|
352
|
+
local function encodeInt(a: BigInt): { number }
|
|
353
|
+
local enc: { number } = table.create(21)
|
|
345
354
|
|
|
346
355
|
for i = 1, 7 do
|
|
347
356
|
local word = a[i]
|
|
@@ -354,9 +363,9 @@ local function encodeInt(a)
|
|
|
354
363
|
return enc
|
|
355
364
|
end
|
|
356
365
|
|
|
357
|
-
local function decodeInt(enc)
|
|
358
|
-
local a = {}
|
|
359
|
-
local encCopy = table.create(21)
|
|
366
|
+
local function decodeInt(enc: { number }): BigInt
|
|
367
|
+
local a: BigInt = {}
|
|
368
|
+
local encCopy: { number } = table.create(21)
|
|
360
369
|
|
|
361
370
|
for i = 1, 21 do
|
|
362
371
|
local byte = enc[i]
|
|
@@ -379,26 +388,27 @@ local function decodeInt(enc)
|
|
|
379
388
|
return a
|
|
380
389
|
end
|
|
381
390
|
|
|
382
|
-
local function mods(d, w)
|
|
383
|
-
local result = d[1] % twoPower[w]
|
|
391
|
+
local function mods(d: BigInt, w: number): number
|
|
392
|
+
local result = d[1] % (twoPower :: any)[w]
|
|
384
393
|
|
|
385
|
-
if result >= twoPower[w - 1] then
|
|
386
|
-
result -= twoPower[w]
|
|
394
|
+
if result >= (twoPower :: any)[w - 1] then
|
|
395
|
+
result -= (twoPower :: any)[w]
|
|
387
396
|
end
|
|
388
397
|
|
|
389
398
|
return result
|
|
390
399
|
end
|
|
391
400
|
|
|
392
401
|
-- Represents a 168-bit number as the (2^w)-ary Non-Adjacent Form
|
|
393
|
-
local function NAF(d, w)
|
|
394
|
-
local t
|
|
395
|
-
local
|
|
402
|
+
local function NAF(d: BigInt, w: number): { number }
|
|
403
|
+
local t: { number } = {}
|
|
404
|
+
local t_len = 0
|
|
405
|
+
local newD: BigInt = { table.unpack(d) }
|
|
396
406
|
|
|
397
407
|
for _ = 1, 168 do
|
|
398
408
|
if newD[1] % 2 == 1 then
|
|
399
409
|
t_len += 1
|
|
400
410
|
t[t_len] = mods(newD, w)
|
|
401
|
-
newD = sub(newD, { t[
|
|
411
|
+
newD = sub(newD, { t[t_len], 0, 0, 0, 0, 0, 0 })
|
|
402
412
|
else
|
|
403
413
|
t_len += 1
|
|
404
414
|
t[t_len] = 0
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
--!
|
|
1
|
+
--!strict
|
|
2
2
|
--!native
|
|
3
3
|
|
|
4
4
|
-- Chacha20 cipher in ComputerCraft
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
-- http://www.computercraft.info/forums2/index.php?/user/12870-anavrins
|
|
9
9
|
-- http://pastebin.com/GPzf9JSa
|
|
10
10
|
-- Last update: April 17, 2017
|
|
11
|
-
local twoPower = require(script.Parent.twoPower)
|
|
11
|
+
local twoPower: any = require(script.Parent.twoPower)
|
|
12
12
|
local util = require(script.Parent.util)
|
|
13
13
|
|
|
14
14
|
local bxor = bit32.bxor
|
|
@@ -20,13 +20,13 @@ local mod = 2 ^ 32
|
|
|
20
20
|
local tau = table.pack(string.byte("expand 16-byte k", 1, -1))
|
|
21
21
|
local sigma = table.pack(string.byte("expand 32-byte k", 1, -1))
|
|
22
22
|
|
|
23
|
-
local function rotl(n, b)
|
|
23
|
+
local function rotl(n: number, b: number): number
|
|
24
24
|
local s = n / twoPower[32 - b]
|
|
25
25
|
local f = s % 1
|
|
26
26
|
return (s - f) + f * mod
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
local function quarterRound(s, a, b, c, d)
|
|
29
|
+
local function quarterRound(s: { number }, a: number, b: number, c: number, d: number): { number }
|
|
30
30
|
s[a] = (s[a] + s[b]) % mod
|
|
31
31
|
s[d] = rotl(bxor(s[d], s[a]), 16)
|
|
32
32
|
s[c] = (s[c] + s[d]) % mod
|
|
@@ -38,8 +38,8 @@ local function quarterRound(s, a, b, c, d)
|
|
|
38
38
|
return s
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
-
local function hashBlock(state, rnd)
|
|
42
|
-
local s = { table.unpack(state) }
|
|
41
|
+
local function hashBlock(state: { number }, rnd: number): { number }
|
|
42
|
+
local s: { number } = { table.unpack(state) }
|
|
43
43
|
for i = 1, rnd do
|
|
44
44
|
local r = i % 2 == 1
|
|
45
45
|
s = r and quarterRound(s, 1, 5, 9, 13) or quarterRound(s, 1, 6, 11, 16)
|
|
@@ -55,17 +55,17 @@ local function hashBlock(state, rnd)
|
|
|
55
55
|
return s
|
|
56
56
|
end
|
|
57
57
|
|
|
58
|
-
local function LE_toInt(bs, i)
|
|
58
|
+
local function LE_toInt(bs: { number }, i: number): number
|
|
59
59
|
return (bs[i + 1] or 0)
|
|
60
60
|
+ blshift((bs[i + 2] or 0), 8)
|
|
61
61
|
+ blshift((bs[i + 3] or 0), 16)
|
|
62
62
|
+ blshift((bs[i + 4] or 0), 24)
|
|
63
63
|
end
|
|
64
64
|
|
|
65
|
-
local function initState(key, nonce, counter)
|
|
65
|
+
local function initState(key: { number }, nonce: { number }, counter: number): { number }
|
|
66
66
|
local isKey256 = #key == 32
|
|
67
|
-
local const = isKey256 and sigma or tau
|
|
68
|
-
local state = table.create(16)
|
|
67
|
+
local const: { number } = isKey256 and sigma or tau
|
|
68
|
+
local state: { number } = table.create(16)
|
|
69
69
|
|
|
70
70
|
state[1] = LE_toInt(const, 0)
|
|
71
71
|
state[2] = LE_toInt(const, 4)
|
|
@@ -89,8 +89,9 @@ local function initState(key, nonce, counter)
|
|
|
89
89
|
return state
|
|
90
90
|
end
|
|
91
91
|
|
|
92
|
-
local function serialize(state)
|
|
93
|
-
local r
|
|
92
|
+
local function serialize(state: { number }): { number }
|
|
93
|
+
local r: { number } = table.create(16)
|
|
94
|
+
local len_r = 0
|
|
94
95
|
for i = 1, 16 do
|
|
95
96
|
r[len_r + 1] = band(state[i], 0xFF)
|
|
96
97
|
r[len_r + 2] = band(brshift(state[i], 8), 0xFF)
|
|
@@ -102,24 +103,34 @@ local function serialize(state)
|
|
|
102
103
|
return r
|
|
103
104
|
end
|
|
104
105
|
|
|
105
|
-
|
|
106
|
+
export type ByteTable = typeof(setmetatable({} :: { number }, {} :: typeof(util.byteTableMT)))
|
|
107
|
+
|
|
108
|
+
local function crypt(
|
|
109
|
+
data: string | { number },
|
|
110
|
+
key: { number },
|
|
111
|
+
nonce: { number },
|
|
112
|
+
cntr: number?,
|
|
113
|
+
round: number?
|
|
114
|
+
): ByteTable
|
|
106
115
|
assert(type(key) == "table", "ChaCha20: Invalid key format (" .. type(key) .. "), must be table")
|
|
107
116
|
assert(type(nonce) == "table", "ChaCha20: Invalid nonce format (" .. type(nonce) .. "), must be table")
|
|
108
117
|
assert(#key == 16 or #key == 32, "ChaCha20: Invalid key length (" .. #key .. "), must be 16 or 32")
|
|
109
118
|
assert(#nonce == 12, "ChaCha20: Invalid nonce length (" .. #nonce .. "), must be 12")
|
|
110
119
|
|
|
111
|
-
local newData = type(data) == "table" and { table.unpack(data
|
|
112
|
-
|
|
113
|
-
|
|
120
|
+
local newData: { number } = type(data) == "table" and { table.unpack(data :: { number }) }
|
|
121
|
+
or util.stringToByteArray(data :: string)
|
|
122
|
+
local counter = tonumber(cntr) or 1
|
|
123
|
+
local rounds = tonumber(round) or 20
|
|
114
124
|
|
|
115
|
-
local out
|
|
116
|
-
local
|
|
125
|
+
local out: { number } = {}
|
|
126
|
+
local out_len = 0
|
|
127
|
+
local state = initState(key, nonce, counter)
|
|
117
128
|
local blockAmt = math.floor(#newData / 64)
|
|
118
129
|
for i = 0, blockAmt do
|
|
119
|
-
local ks = serialize(hashBlock(state,
|
|
130
|
+
local ks = serialize(hashBlock(state, rounds))
|
|
120
131
|
state[13] = (state[13] + 1) % mod
|
|
121
132
|
|
|
122
|
-
local block = table.create(64)
|
|
133
|
+
local block: { number } = table.create(64)
|
|
123
134
|
for j = 1, 64 do
|
|
124
135
|
block[j] = newData[(i * 64) + j]
|
|
125
136
|
end
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
--!
|
|
1
|
+
--!strict
|
|
2
2
|
--!native
|
|
3
3
|
|
|
4
4
|
--[=[
|
|
@@ -49,7 +49,7 @@ local montgomeryModP = modp.montgomeryModP
|
|
|
49
49
|
local expModP = modp.expModP
|
|
50
50
|
local inverseMontgomeryModQ = modq.inverseMontgomeryModQ
|
|
51
51
|
|
|
52
|
-
local pointMT
|
|
52
|
+
local pointMT: any
|
|
53
53
|
local ZERO = table.create(7, 0)
|
|
54
54
|
local ONE = montgomeryModP({ 1, 0, 0, 0, 0, 0, 0 })
|
|
55
55
|
|
|
@@ -193,9 +193,9 @@ end
|
|
|
193
193
|
local function scalarMult(multiplier, P1)
|
|
194
194
|
-- w = 5
|
|
195
195
|
local naf = NAF(multiplier, 5)
|
|
196
|
-
local PTable = { P1 }
|
|
196
|
+
local PTable: { any } = { P1 }
|
|
197
197
|
local P2 = pointDouble(P1)
|
|
198
|
-
local Q = { table.create(7, 0), { table.unpack(ONE) }, { table.unpack(ONE) } }
|
|
198
|
+
local Q: any = { table.create(7, 0), { table.unpack(ONE) }, { table.unpack(ONE) } }
|
|
199
199
|
|
|
200
200
|
for i = 3, 31, 2 do
|
|
201
201
|
PTable[i] = pointAdd(PTable[i - 2], P2)
|
|
@@ -215,14 +215,14 @@ end
|
|
|
215
215
|
|
|
216
216
|
-- Lookup table 4-ary NAF method for scalar multiplication by G.
|
|
217
217
|
-- Precomputations for the regular NAF method are done before the multiplication.
|
|
218
|
-
local GTable = { G }
|
|
218
|
+
local GTable: { any } = { G }
|
|
219
219
|
for i = 2, 168 do
|
|
220
220
|
GTable[i] = pointDouble(GTable[i - 1])
|
|
221
221
|
end
|
|
222
222
|
|
|
223
223
|
local function scalarMultG(multiplier)
|
|
224
224
|
local naf = NAF(multiplier, 2)
|
|
225
|
-
local Q = { table.create(7, 0), { table.unpack(ONE) }, { table.unpack(ONE) } }
|
|
225
|
+
local Q: any = { table.create(7, 0), { table.unpack(ONE) }, { table.unpack(ONE) } }
|
|
226
226
|
|
|
227
227
|
for i = 1, 168 do
|
|
228
228
|
if naf[i] == 1 then
|
|
@@ -237,7 +237,7 @@ end
|
|
|
237
237
|
|
|
238
238
|
-- Point compression and encoding.
|
|
239
239
|
-- Compresses curve points to 22 bytes.
|
|
240
|
-
local function pointEncode(P1)
|
|
240
|
+
local function pointEncode(P1: any)
|
|
241
241
|
P1 = pointScale(P1)
|
|
242
242
|
local result = {}
|
|
243
243
|
local x, y = P1[1], P1[2]
|
|
@@ -321,7 +321,7 @@ pointMT = {
|
|
|
321
321
|
return pointIsEqual(P1, P2)
|
|
322
322
|
end,
|
|
323
323
|
|
|
324
|
-
__mul = function(P1, s)
|
|
324
|
+
__mul = function(P1: any, s: any)
|
|
325
325
|
if type(P1) == "number" then
|
|
326
326
|
return s * P1
|
|
327
327
|
end
|