@theqrl/dilithium5 0.0.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/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # `@theqrl/dilithium5`
2
+
3
+ > TODO: description
4
+
5
+ ## Usage
6
+
7
+ ```
8
+ const dilithium5 = require('@theqrl/dilithium5');
9
+
10
+ // TODO: DEMONSTRATE API
11
+ ```
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@theqrl/dilithium5",
3
+ "version": "0.0.1",
4
+ "description": "Dilithium-5 cryptography",
5
+ "keywords": [
6
+ "dilithium",
7
+ "post-quantum",
8
+ "cryptography"
9
+ ],
10
+ "author": "QRL contributors <info@theqrl.org> (https://theqrl.org)",
11
+ "homepage": "https://github.com/theQRL/qrypto.js#readme",
12
+ "license": "MIT",
13
+ "main": "src/sign.js",
14
+ "type": "module",
15
+ "directories": {
16
+ "lib": "src",
17
+ "test": "test"
18
+ },
19
+ "files": [
20
+ "src"
21
+ ],
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/theQRL/qrypto.js.git"
28
+ },
29
+ "scripts": {
30
+ "test": "../../node_modules/mocha/bin/mocha.js",
31
+ "lint-check": "eslint 'src/**/*.js' 'test/**/*.js'",
32
+ "lint": "eslint --fix 'src/**/*.js' 'test/**/*.js'",
33
+ "report-coverage": "c8 --reporter=text-lcov npm run test > coverage.lcov"
34
+ },
35
+ "bugs": {
36
+ "url": "https://github.com/theQRL/qrypto.js/issues"
37
+ },
38
+ "devDependencies": {
39
+ "chai": "^4.3.7",
40
+ "codecov": "^3.8.3",
41
+ "eslint": "^8.33.0",
42
+ "eslint-config-airbnb": "^19.0.4",
43
+ "eslint-config-prettier": "^8.6.0",
44
+ "eslint-plugin-import": "^2.27.5",
45
+ "eslint-plugin-prettier": "^4.2.1",
46
+ "esm": "^3.2.25",
47
+ "mocha": "^10.2.0",
48
+ "c8": "^7.13.0",
49
+ "prettier": "^2.8.3"
50
+ }
51
+ }
package/src/const.js ADDED
@@ -0,0 +1,60 @@
1
+ export const Shake128Rate = 168;
2
+ export const Shake256Rate = 136;
3
+ export const Stream128BlockBytes = Shake128Rate;
4
+ export const Stream256BlockBytes = Shake256Rate;
5
+
6
+ export const SeedBytes = 32;
7
+ export const CRHBytes = 64;
8
+ export const N = 256;
9
+ export const Q = 8380417;
10
+ export const QInv = 58728449;
11
+ export const D = 13;
12
+
13
+ export const K = 8;
14
+ export const L = 7;
15
+ export const ETA = 2;
16
+ export const TAU = 60;
17
+ export const BETA = 120;
18
+ export const GAMMA1 = 1 << 19;
19
+ export const GAMMA2 = Math.floor((Q - 1) / 32);
20
+ export const OMEGA = 75;
21
+
22
+ export const PolyT1PackedBytes = 320;
23
+ export const PolyT0PackedBytes = 416;
24
+ export const PolyETAPackedBytes = 96;
25
+ export const PolyZPackedBytes = 640;
26
+ export const PolyVecHPackedBytes = OMEGA + K;
27
+ export const PolyW1PackedBytes = 128;
28
+
29
+ export const CryptoPublicKeyBytes = SeedBytes + K * PolyT1PackedBytes;
30
+ export const CryptoSecretKeyBytes =
31
+ 3 * SeedBytes + L * PolyETAPackedBytes + K * PolyETAPackedBytes + K * PolyT0PackedBytes;
32
+ export const CryptoBytes = SeedBytes + L * PolyZPackedBytes + PolyVecHPackedBytes;
33
+
34
+ export const PolyUniformNBlocks = Math.floor((768 + Stream128BlockBytes - 1) / Stream128BlockBytes);
35
+ export const PolyUniformETANBlocks = Math.floor((136 + Stream256BlockBytes - 1) / Stream256BlockBytes);
36
+ export const PolyUniformGamma1NBlocks = Math.floor((PolyZPackedBytes + Stream256BlockBytes - 1) / Stream256BlockBytes);
37
+
38
+ export const zetas = [
39
+ 0, 25847, -2608894, -518909, 237124, -777960, -876248, 466468, 1826347, 2353451, -359251, -2091905, 3119733, -2884855,
40
+ 3111497, 2680103, 2725464, 1024112, -1079900, 3585928, -549488, -1119584, 2619752, -2108549, -2118186, -3859737,
41
+ -1399561, -3277672, 1757237, -19422, 4010497, 280005, 2706023, 95776, 3077325, 3530437, -1661693, -3592148, -2537516,
42
+ 3915439, -3861115, -3043716, 3574422, -2867647, 3539968, -300467, 2348700, -539299, -1699267, -1643818, 3505694,
43
+ -3821735, 3507263, -2140649, -1600420, 3699596, 811944, 531354, 954230, 3881043, 3900724, -2556880, 2071892, -2797779,
44
+ -3930395, -1528703, -3677745, -3041255, -1452451, 3475950, 2176455, -1585221, -1257611, 1939314, -4083598, -1000202,
45
+ -3190144, -3157330, -3632928, 126922, 3412210, -983419, 2147896, 2715295, -2967645, -3693493, -411027, -2477047,
46
+ -671102, -1228525, -22981, -1308169, -381987, 1349076, 1852771, -1430430, -3343383, 264944, 508951, 3097992, 44288,
47
+ -1100098, 904516, 3958618, -3724342, -8578, 1653064, -3249728, 2389356, -210977, 759969, -1316856, 189548, -3553272,
48
+ 3159746, -1851402, -2409325, -177440, 1315589, 1341330, 1285669, -1584928, -812732, -1439742, -3019102, -3881060,
49
+ -3628969, 3839961, 2091667, 3407706, 2316500, 3817976, -3342478, 2244091, -2446433, -3562462, 266997, 2434439,
50
+ -1235728, 3513181, -3520352, -3759364, -1197226, -3193378, 900702, 1859098, 909542, 819034, 495491, -1613174, -43260,
51
+ -522500, -655327, -3122442, 2031748, 3207046, -3556995, -525098, -768622, -3595838, 342297, 286988, -2437823, 4108315,
52
+ 3437287, -3342277, 1735879, 203044, 2842341, 2691481, -2590150, 1265009, 4055324, 1247620, 2486353, 1595974, -3767016,
53
+ 1250494, 2635921, -3548272, -2994039, 1869119, 1903435, -1050970, -1333058, 1237275, -3318210, -1430225, -451100,
54
+ 1312455, 3306115, -1962642, -1279661, 1917081, -2546312, -1374803, 1500165, 777191, 2235880, 3406031, -542412,
55
+ -2831860, -1671176, -1846953, -2584293, -3724270, 594136, -3776993, -2013608, 2432395, 2454455, -164721, 1957272,
56
+ 3369112, 185531, -1207385, -3183426, 162844, 1616392, 3014001, 810149, 1652634, -3694233, -1799107, -3038916, 3523897,
57
+ 3866901, 269760, 2213111, -975884, 1717735, 472078, -426683, 1723600, -1803090, 1910376, -1667432, -1104333, -260646,
58
+ -3833893, -2939036, -2235985, -420899, -2286327, 183443, -976891, 1612842, -3545687, -554416, 3919660, -48306,
59
+ -1362209, 3937738, 1400424, -846154, 1976782,
60
+ ];
package/src/fips202.js ADDED
@@ -0,0 +1,430 @@
1
+ import { Shake128Rate, Shake256Rate } from './const.js';
2
+
3
+ const NRounds = 24;
4
+
5
+ const KeccakFRoundConstants = BigUint64Array.from([
6
+ 0x0000000000000001n,
7
+ 0x0000000000008082n,
8
+ 0x800000000000808an,
9
+ 0x8000000080008000n,
10
+ 0x000000000000808bn,
11
+ 0x0000000080000001n,
12
+ 0x8000000080008081n,
13
+ 0x8000000000008009n,
14
+ 0x000000000000008an,
15
+ 0x0000000000000088n,
16
+ 0x0000000080008009n,
17
+ 0x000000008000000an,
18
+ 0x000000008000808bn,
19
+ 0x800000000000008bn,
20
+ 0x8000000000008089n,
21
+ 0x8000000000008003n,
22
+ 0x8000000000008002n,
23
+ 0x8000000000000080n,
24
+ 0x000000000000800an,
25
+ 0x800000008000000an,
26
+ 0x8000000080008081n,
27
+ 0x8000000000008080n,
28
+ 0x0000000080000001n,
29
+ 0x8000000080008008n,
30
+ ]);
31
+
32
+ export class KeccakState {
33
+ constructor() {
34
+ this.s = new BigUint64Array(25);
35
+ this.pos = 0;
36
+ }
37
+ }
38
+
39
+ function ROL(a, offset) {
40
+ return BigInt.asUintN(64, BigInt.asUintN(64, a << offset) ^ (a >> (64n - offset)));
41
+ }
42
+
43
+ function load64(x, xOffset) {
44
+ let r = BigInt(0);
45
+
46
+ for (let i = 0; i < 8; i++) r = BigInt.asUintN(64, r | BigInt.asUintN(64, BigInt(x[xOffset + i]) << BigInt(8 * i)));
47
+
48
+ return r;
49
+ }
50
+
51
+ function store64(x, xOffset, u) {
52
+ for (let i = 0; i < 8; i++) x[xOffset + i] = Number((u >> BigInt(8 * i)) & 0xffn);
53
+ }
54
+
55
+ function KeccakF1600StatePermute(state) {
56
+ //copyFromState(A, state)
57
+ let Aba = state[0];
58
+ let Abe = state[1];
59
+ let Abi = state[2];
60
+ let Abo = state[3];
61
+ let Abu = state[4];
62
+ let Aga = state[5];
63
+ let Age = state[6];
64
+ let Agi = state[7];
65
+ let Ago = state[8];
66
+ let Agu = state[9];
67
+ let Aka = state[10];
68
+ let Ake = state[11];
69
+ let Aki = state[12];
70
+ let Ako = state[13];
71
+ let Aku = state[14];
72
+ let Ama = state[15];
73
+ let Ame = state[16];
74
+ let Ami = state[17];
75
+ let Amo = state[18];
76
+ let Amu = state[19];
77
+ let Asa = state[20];
78
+ let Ase = state[21];
79
+ let Asi = state[22];
80
+ let Aso = state[23];
81
+ let Asu = state[24];
82
+
83
+ for (let round = 0; round < NRounds; round += 2) {
84
+ // prepareTheta
85
+ let BCa = BigInt.asUintN(64, Aba ^ Aga ^ Aka ^ Ama ^ Asa);
86
+ let BCe = BigInt.asUintN(64, Abe ^ Age ^ Ake ^ Ame ^ Ase);
87
+ let BCi = BigInt.asUintN(64, Abi ^ Agi ^ Aki ^ Ami ^ Asi);
88
+ let BCo = BigInt.asUintN(64, Abo ^ Ago ^ Ako ^ Amo ^ Aso);
89
+ let BCu = BigInt.asUintN(64, Abu ^ Agu ^ Aku ^ Amu ^ Asu);
90
+
91
+ //thetaRhoPiChiIotaPrepareTheta(round, A, E)
92
+ let Da = BigInt.asUintN(64, BCu ^ ROL(BCe, 1n));
93
+ let De = BigInt.asUintN(64, BCa ^ ROL(BCi, 1n));
94
+ let Di = BigInt.asUintN(64, BCe ^ ROL(BCo, 1n));
95
+ let Do = BigInt.asUintN(64, BCi ^ ROL(BCu, 1n));
96
+ let Du = BigInt.asUintN(64, BCo ^ ROL(BCa, 1n));
97
+
98
+ Aba = BigInt.asUintN(64, Aba ^ Da);
99
+ BCa = Aba;
100
+ Age = BigInt.asUintN(64, Age ^ De);
101
+ BCe = ROL(Age, 44n);
102
+ Aki = BigInt.asUintN(64, Aki ^ Di);
103
+ BCi = ROL(Aki, 43n);
104
+ Amo = BigInt.asUintN(64, Amo ^ Do);
105
+ BCo = ROL(Amo, 21n);
106
+ Asu = BigInt.asUintN(64, Asu ^ Du);
107
+ BCu = ROL(Asu, 14n);
108
+ let Eba = BigInt.asUintN(64, BCa ^ (~BCe & BCi));
109
+ Eba = BigInt.asUintN(64, Eba ^ KeccakFRoundConstants[round]);
110
+ let Ebe = BigInt.asUintN(64, BCe ^ (~BCi & BCo));
111
+ let Ebi = BigInt.asUintN(64, BCi ^ (~BCo & BCu));
112
+ let Ebo = BigInt.asUintN(64, BCo ^ (~BCu & BCa));
113
+ let Ebu = BigInt.asUintN(64, BCu ^ (~BCa & BCe));
114
+
115
+ Abo = BigInt.asUintN(64, Abo ^ Do);
116
+ BCa = ROL(Abo, 28n);
117
+ Agu = BigInt.asUintN(64, Agu ^ Du);
118
+ BCe = ROL(Agu, 20n);
119
+ Aka = BigInt.asUintN(64, Aka ^ Da);
120
+ BCi = ROL(Aka, 3n);
121
+ Ame = BigInt.asUintN(64, Ame ^ De);
122
+ BCo = ROL(Ame, 45n);
123
+ Asi = BigInt.asUintN(64, Asi ^ Di);
124
+ BCu = ROL(Asi, 61n);
125
+ let Ega = BigInt.asUintN(64, BCa ^ (~BCe & BCi));
126
+ let Ege = BigInt.asUintN(64, BCe ^ (~BCi & BCo));
127
+ let Egi = BigInt.asUintN(64, BCi ^ (~BCo & BCu));
128
+ let Ego = BigInt.asUintN(64, BCo ^ (~BCu & BCa));
129
+ let Egu = BigInt.asUintN(64, BCu ^ (~BCa & BCe));
130
+
131
+ Abe = BigInt.asUintN(64, Abe ^ De);
132
+ BCa = ROL(Abe, 1n);
133
+ Agi = BigInt.asUintN(64, Agi ^ Di);
134
+ BCe = ROL(Agi, 6n);
135
+ Ako = BigInt.asUintN(64, Ako ^ Do);
136
+ BCi = ROL(Ako, 25n);
137
+ Amu = BigInt.asUintN(64, Amu ^ Du);
138
+ BCo = ROL(Amu, 8n);
139
+ Asa = BigInt.asUintN(64, Asa ^ Da);
140
+ BCu = ROL(Asa, 18n);
141
+ let Eka = BigInt.asUintN(64, BCa ^ (~BCe & BCi));
142
+ let Eke = BigInt.asUintN(64, BCe ^ (~BCi & BCo));
143
+ let Eki = BigInt.asUintN(64, BCi ^ (~BCo & BCu));
144
+ let Eko = BigInt.asUintN(64, BCo ^ (~BCu & BCa));
145
+ let Eku = BigInt.asUintN(64, BCu ^ (~BCa & BCe));
146
+
147
+ Abu = BigInt.asUintN(64, Abu ^ Du);
148
+ BCa = ROL(Abu, 27n);
149
+ Aga = BigInt.asUintN(64, Aga ^ Da);
150
+ BCe = ROL(Aga, 36n);
151
+ Ake = BigInt.asUintN(64, Ake ^ De);
152
+ BCi = ROL(Ake, 10n);
153
+ Ami = BigInt.asUintN(64, Ami ^ Di);
154
+ BCo = ROL(Ami, 15n);
155
+ Aso = BigInt.asUintN(64, Aso ^ Do);
156
+ BCu = ROL(Aso, 56n);
157
+ let Ema = BigInt.asUintN(64, BCa ^ (~BCe & BCi));
158
+ let Eme = BigInt.asUintN(64, BCe ^ (~BCi & BCo));
159
+ let Emi = BigInt.asUintN(64, BCi ^ (~BCo & BCu));
160
+ let Emo = BigInt.asUintN(64, BCo ^ (~BCu & BCa));
161
+ let Emu = BigInt.asUintN(64, BCu ^ (~BCa & BCe));
162
+
163
+ Abi = BigInt.asUintN(64, Abi ^ Di);
164
+ BCa = ROL(Abi, 62n);
165
+ Ago = BigInt.asUintN(64, Ago ^ Do);
166
+ BCe = ROL(Ago, 55n);
167
+ Aku = BigInt.asUintN(64, Aku ^ Du);
168
+ BCi = ROL(Aku, 39n);
169
+ Ama = BigInt.asUintN(64, Ama ^ Da);
170
+ BCo = ROL(Ama, 41n);
171
+ Ase = BigInt.asUintN(64, Ase ^ De);
172
+ BCu = ROL(Ase, 2n);
173
+ let Esa = BigInt.asUintN(64, BCa ^ (~BCe & BCi));
174
+ let Ese = BigInt.asUintN(64, BCe ^ (~BCi & BCo));
175
+ let Esi = BigInt.asUintN(64, BCi ^ (~BCo & BCu));
176
+ let Eso = BigInt.asUintN(64, BCo ^ (~BCu & BCa));
177
+ let Esu = BigInt.asUintN(64, BCu ^ (~BCa & BCe));
178
+
179
+ // prepareTheta
180
+ BCa = BigInt.asUintN(64, Eba ^ Ega ^ Eka ^ Ema ^ Esa);
181
+ BCe = BigInt.asUintN(64, Ebe ^ Ege ^ Eke ^ Eme ^ Ese);
182
+ BCi = BigInt.asUintN(64, Ebi ^ Egi ^ Eki ^ Emi ^ Esi);
183
+ BCo = BigInt.asUintN(64, Ebo ^ Ego ^ Eko ^ Emo ^ Eso);
184
+ BCu = BigInt.asUintN(64, Ebu ^ Egu ^ Eku ^ Emu ^ Esu);
185
+
186
+ //thetaRhoPiChiIotaPrepareTheta(round+1, E, A)
187
+ Da = BigInt.asUintN(64, BCu ^ ROL(BCe, 1n));
188
+ De = BigInt.asUintN(64, BCa ^ ROL(BCi, 1n));
189
+ Di = BigInt.asUintN(64, BCe ^ ROL(BCo, 1n));
190
+ Do = BigInt.asUintN(64, BCi ^ ROL(BCu, 1n));
191
+ Du = BigInt.asUintN(64, BCo ^ ROL(BCa, 1n));
192
+
193
+ Eba = BigInt.asUintN(64, Eba ^ Da);
194
+ BCa = Eba;
195
+ Ege = BigInt.asUintN(64, Ege ^ De);
196
+ BCe = ROL(Ege, 44n);
197
+ Eki = BigInt.asUintN(64, Eki ^ Di);
198
+ BCi = ROL(Eki, 43n);
199
+ Emo = BigInt.asUintN(64, Emo ^ Do);
200
+ BCo = ROL(Emo, 21n);
201
+ Esu = BigInt.asUintN(64, Esu ^ Du);
202
+ BCu = ROL(Esu, 14n);
203
+ Aba = BigInt.asUintN(64, BCa ^ (~BCe & BCi));
204
+ Aba = BigInt.asUintN(64, Aba ^ KeccakFRoundConstants[round + 1]);
205
+ Abe = BigInt.asUintN(64, BCe ^ (~BCi & BCo));
206
+ Abi = BigInt.asUintN(64, BCi ^ (~BCo & BCu));
207
+ Abo = BigInt.asUintN(64, BCo ^ (~BCu & BCa));
208
+ Abu = BigInt.asUintN(64, BCu ^ (~BCa & BCe));
209
+
210
+ Ebo = BigInt.asUintN(64, Ebo ^ Do);
211
+ BCa = ROL(Ebo, 28n);
212
+ Egu = BigInt.asUintN(64, Egu ^ Du);
213
+ BCe = ROL(Egu, 20n);
214
+ Eka = BigInt.asUintN(64, Eka ^ Da);
215
+ BCi = ROL(Eka, 3n);
216
+ Eme = BigInt.asUintN(64, Eme ^ De);
217
+ BCo = ROL(Eme, 45n);
218
+ Esi = BigInt.asUintN(64, Esi ^ Di);
219
+ BCu = ROL(Esi, 61n);
220
+ Aga = BigInt.asUintN(64, BCa ^ (~BCe & BCi));
221
+ Age = BigInt.asUintN(64, BCe ^ (~BCi & BCo));
222
+ Agi = BigInt.asUintN(64, BCi ^ (~BCo & BCu));
223
+ Ago = BigInt.asUintN(64, BCo ^ (~BCu & BCa));
224
+ Agu = BigInt.asUintN(64, BCu ^ (~BCa & BCe));
225
+
226
+ Ebe = BigInt.asUintN(64, Ebe ^ De);
227
+ BCa = ROL(Ebe, 1n);
228
+ Egi = BigInt.asUintN(64, Egi ^ Di);
229
+ BCe = ROL(Egi, 6n);
230
+ Eko = BigInt.asUintN(64, Eko ^ Do);
231
+ BCi = ROL(Eko, 25n);
232
+ Emu = BigInt.asUintN(64, Emu ^ Du);
233
+ BCo = ROL(Emu, 8n);
234
+ Esa = BigInt.asUintN(64, Esa ^ Da);
235
+ BCu = ROL(Esa, 18n);
236
+ Aka = BigInt.asUintN(64, BCa ^ (~BCe & BCi));
237
+ Ake = BigInt.asUintN(64, BCe ^ (~BCi & BCo));
238
+ Aki = BigInt.asUintN(64, BCi ^ (~BCo & BCu));
239
+ Ako = BigInt.asUintN(64, BCo ^ (~BCu & BCa));
240
+ Aku = BigInt.asUintN(64, BCu ^ (~BCa & BCe));
241
+
242
+ Ebu = BigInt.asUintN(64, Ebu ^ Du);
243
+ BCa = ROL(Ebu, 27n);
244
+ Ega = BigInt.asUintN(64, Ega ^ Da);
245
+ BCe = ROL(Ega, 36n);
246
+ Eke = BigInt.asUintN(64, Eke ^ De);
247
+ BCi = ROL(Eke, 10n);
248
+ Emi = BigInt.asUintN(64, Emi ^ Di);
249
+ BCo = ROL(Emi, 15n);
250
+ Eso = BigInt.asUintN(64, Eso ^ Do);
251
+ BCu = ROL(Eso, 56n);
252
+ Ama = BigInt.asUintN(64, BCa ^ (~BCe & BCi));
253
+ Ame = BigInt.asUintN(64, BCe ^ (~BCi & BCo));
254
+ Ami = BigInt.asUintN(64, BCi ^ (~BCo & BCu));
255
+ Amo = BigInt.asUintN(64, BCo ^ (~BCu & BCa));
256
+ Amu = BigInt.asUintN(64, BCu ^ (~BCa & BCe));
257
+
258
+ Ebi = BigInt.asUintN(64, Ebi ^ Di);
259
+ BCa = ROL(Ebi, 62n);
260
+ Ego = BigInt.asUintN(64, Ego ^ Do);
261
+ BCe = ROL(Ego, 55n);
262
+ Eku = BigInt.asUintN(64, Eku ^ Du);
263
+ BCi = ROL(Eku, 39n);
264
+ Ema = BigInt.asUintN(64, Ema ^ Da);
265
+ BCo = ROL(Ema, 41n);
266
+ Ese = BigInt.asUintN(64, Ese ^ De);
267
+ BCu = ROL(Ese, 2n);
268
+ Asa = BigInt.asUintN(64, BCa ^ (~BCe & BCi));
269
+ Ase = BigInt.asUintN(64, BCe ^ (~BCi & BCo));
270
+ Asi = BigInt.asUintN(64, BCi ^ (~BCo & BCu));
271
+ Aso = BigInt.asUintN(64, BCo ^ (~BCu & BCa));
272
+ Asu = BigInt.asUintN(64, BCu ^ (~BCa & BCe));
273
+ }
274
+
275
+ state[0] = Aba;
276
+ state[1] = Abe;
277
+ state[2] = Abi;
278
+ state[3] = Abo;
279
+ state[4] = Abu;
280
+ state[5] = Aga;
281
+ state[6] = Age;
282
+ state[7] = Agi;
283
+ state[8] = Ago;
284
+ state[9] = Agu;
285
+ state[10] = Aka;
286
+ state[11] = Ake;
287
+ state[12] = Aki;
288
+ state[13] = Ako;
289
+ state[14] = Aku;
290
+ state[15] = Ama;
291
+ state[16] = Ame;
292
+ state[17] = Ami;
293
+ state[18] = Amo;
294
+ state[19] = Amu;
295
+ state[20] = Asa;
296
+ state[21] = Ase;
297
+ state[22] = Asi;
298
+ state[23] = Aso;
299
+ state[24] = Asu;
300
+ }
301
+
302
+ function keccakInit(s) {
303
+ for (let i = 0; i < 25; i++) s[i] = 0n;
304
+ }
305
+
306
+ function keccakAbsorb(s, pos, r, input) {
307
+ let inLen = input.length;
308
+ let i;
309
+ let inputOffset = 0;
310
+ while (pos + inLen >= r) {
311
+ for (i = pos; i < r; i++)
312
+ s[Math.floor(i / 8)] = BigInt.asUintN(
313
+ 64,
314
+ s[Math.floor(i / 8)] ^ (BigInt(input[inputOffset++]) << BigInt(8 * (i % 8)))
315
+ );
316
+ inLen -= r - pos;
317
+ KeccakF1600StatePermute(s);
318
+ pos = 0;
319
+ }
320
+
321
+ for (i = pos; i < pos + inLen; i++) {
322
+ s[Math.floor(i / 8)] = BigInt.asUintN(
323
+ 64,
324
+ s[Math.floor(i / 8)] ^ (BigInt(input[inputOffset++]) << BigInt(8 * (i % 8)))
325
+ );
326
+ }
327
+
328
+ return i;
329
+ }
330
+
331
+ function keccakFinalize(s, pos, r, p) {
332
+ s[Math.floor(pos / 8)] = BigInt.asUintN(64, s[Math.floor(pos / 8)] ^ (BigInt(p) << BigInt(8 * (pos % 8))));
333
+ s[Math.floor(r / 8) - 1] = BigInt.asUintN(64, s[Math.floor(r / 8) - 1] ^ (1n << 63n));
334
+ }
335
+
336
+ function keccakSqueeze(out, s, pos, r) {
337
+ let outLen = out.length;
338
+ let outputOffset = 0;
339
+ let i = 0;
340
+
341
+ while (outLen) {
342
+ if (pos === r) {
343
+ KeccakF1600StatePermute(s);
344
+ pos = 0;
345
+ }
346
+ for (i = pos; i < r && i < pos + outLen; i++) out[outputOffset++] = s[Math.floor(i / 8)] >> BigInt(8 * (i % 8));
347
+ outLen -= i - pos;
348
+ pos = i;
349
+ }
350
+
351
+ return pos;
352
+ }
353
+
354
+ function keccakAbsorbOnce(s, r, input, p) {
355
+ let inLen = input.length;
356
+ let inputOffset = 0;
357
+ let i;
358
+
359
+ for (i = 0; i < 25; i++) s[i] = 0;
360
+
361
+ while (inLen >= r) {
362
+ for (i = 0; i < Math.floor(r / 8); i++) s[i] = BigInt.asUintN(64, s[i] ^ load64(input, inputOffset + 8 * i));
363
+ inputOffset += r;
364
+ inLen -= r;
365
+ KeccakF1600StatePermute(s);
366
+ }
367
+
368
+ for (i = 0; i < inLen; i++)
369
+ s[Math.floor(i / 8)] = BigInt.asUintN(
370
+ 64,
371
+ s[Math.floor(i / 8)] ^ (BigInt(input[inputOffset + i]) << BigInt(8 * (i % 8)))
372
+ );
373
+
374
+ s[Math.floor(i / 8)] = BigInt.asUintN(64, s[Math.floor(i / 8)] ^ (BigInt(p) << BigInt(8 * (i % 8))));
375
+ s[Math.floor((r - 1) / 8)] = BigInt.asUintN(64, s[Math.floor((r - 1) / 8)] ^ (1n << 63n));
376
+ }
377
+
378
+ function keccakSqueezeBlocks(output, outputOffset, nBlocks, s, r) {
379
+ while (nBlocks) {
380
+ KeccakF1600StatePermute(s);
381
+ for (let i = 0; i < Math.floor(r / 8); i++) store64(output, outputOffset + 8 * i, s[i]);
382
+ outputOffset += r;
383
+ nBlocks -= 1;
384
+ }
385
+ }
386
+
387
+ export function shake128Init(state) {
388
+ keccakInit(state.s);
389
+ state.pos = 0;
390
+ }
391
+
392
+ export function shake128Absorb(state, input) {
393
+ state.pos = keccakAbsorb(state.s, state.pos, Shake128Rate, input);
394
+ }
395
+
396
+ export function shake128Finalize(state) {
397
+ keccakFinalize(state.s, state.pos, Shake128Rate, 0x1f);
398
+ state.pos = Shake128Rate;
399
+ }
400
+
401
+ export function shake128Squeeze(out, state) {
402
+ state.pos = keccakSqueeze(out, state.s, state.pos, Shake128Rate);
403
+ }
404
+
405
+ export function shake128AbsorbOnce(state, input) {
406
+ keccakAbsorbOnce(state.s, Shake128Rate, input, 0x1f);
407
+ state.pos = Shake128Rate;
408
+ }
409
+
410
+ export function shake128SqueezeBlocks(out, outputOffset, nBlocks, state) {
411
+ keccakSqueezeBlocks(out, outputOffset, nBlocks, state.s, Shake128Rate);
412
+ }
413
+
414
+ export function shake256Init(state) {
415
+ keccakInit(state.s);
416
+ state.pos = 0;
417
+ }
418
+
419
+ export function shake256Absorb(state, input) {
420
+ state.pos = keccakAbsorb(state.s, state.pos, Shake256Rate, input);
421
+ }
422
+
423
+ export function shake256Finalize(state) {
424
+ keccakFinalize(state.s, state.pos, Shake256Rate, 0x1f);
425
+ state.pos = Shake256Rate;
426
+ }
427
+
428
+ export function shake256SqueezeBlocks(out, outputOffset, nBlocks, state) {
429
+ keccakSqueezeBlocks(out, outputOffset, nBlocks, state.s, Shake256Rate);
430
+ }
package/src/ntt.js ADDED
@@ -0,0 +1,40 @@
1
+ import { N, zetas } from './const.js';
2
+ import { montgomeryReduce } from './reduce.js';
3
+
4
+ export function ntt(a) {
5
+ let k = 0;
6
+ let j = 0;
7
+
8
+ for (let len = 128; len > 0; len >>= 1) {
9
+ for (let start = 0; start < N; start = j + len) {
10
+ const zeta = zetas[++k];
11
+ for (j = start; j < start + len; ++j) {
12
+ const t = Number(montgomeryReduce(BigInt.asIntN(64, BigInt(zeta) * BigInt(a[j + len]))));
13
+ a[j + len] = a[j] - t;
14
+ a[j] = a[j] + t;
15
+ }
16
+ }
17
+ }
18
+ }
19
+
20
+ export function invNTTToMont(a) {
21
+ const f = 41978n; // mont^2/256
22
+ let j = 0;
23
+ let k = 256;
24
+
25
+ for (let len = 1; len < N; len <<= 1) {
26
+ for (let start = 0; start < N; start = j + len) {
27
+ const zeta = BigInt.asIntN(32, BigInt(-zetas[--k]));
28
+ for (j = start; j < start + len; ++j) {
29
+ const t = a[j];
30
+ a[j] = t + a[j + len];
31
+ a[j + len] = t - a[j + len];
32
+ a[j + len] = Number(montgomeryReduce(BigInt.asIntN(64, zeta * BigInt(a[j + len]))));
33
+ }
34
+ }
35
+ }
36
+
37
+ for (let j = 0; j < N; ++j) {
38
+ a[j] = Number(montgomeryReduce(BigInt.asIntN(64, f * BigInt(a[j]))));
39
+ }
40
+ }