@theqrl/dilithium5 0.0.9 → 0.1.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/src/polyvec.js DELETED
@@ -1,248 +0,0 @@
1
- const {
2
- Poly,
3
- polyAdd,
4
- polyCAddQ,
5
- polyChkNorm,
6
- polyDecompose,
7
- polyInvNTTToMont,
8
- polyMakeHint,
9
- polyNTT,
10
- polyPointWiseMontgomery,
11
- polyPower2round,
12
- polyReduce,
13
- polyShiftL,
14
- polySub,
15
- polyUniform,
16
- polyUniformEta,
17
- polyUniformGamma1,
18
- polyUseHint,
19
- polyW1Pack,
20
- } = require('./poly.js');
21
- const { CRHBytes, K, L, PolyW1PackedBytes, SeedBytes } = require('./const.js');
22
-
23
- class PolyVecK {
24
- constructor() {
25
- this.vec = new Array(K).fill().map((_) => new Poly());
26
- }
27
- }
28
-
29
- class PolyVecL {
30
- constructor() {
31
- this.vec = new Array(L).fill().map((_) => new Poly());
32
- }
33
-
34
- copy(polyVecL) {
35
- for (let i = L - 1; i >= 0; i--) {
36
- this.vec[i].copy(polyVecL.vec[i]);
37
- }
38
- }
39
- }
40
-
41
- function polyVecMatrixExpand(mat, rho) {
42
- if (rho.length !== SeedBytes) {
43
- throw new Error(`invalid rho length ${rho.length} | Expected length ${SeedBytes}`);
44
- }
45
- for (let i = 0; i < K; ++i) {
46
- for (let j = 0; j < L; ++j) {
47
- polyUniform(mat[i].vec[j], rho, (i << 8) + j);
48
- }
49
- }
50
- }
51
-
52
- function polyVecMatrixPointWiseMontgomery(t, mat, v) {
53
- for (let i = 0; i < K; ++i) {
54
- polyVecLPointWiseAccMontgomery(t.vec[i], mat[i], v);
55
- }
56
- }
57
-
58
- function polyVecLUniformEta(v, seed, nonce) {
59
- if (seed.length !== CRHBytes) {
60
- throw new Error(`invalid seed length ${seed.length} | Expected length ${CRHBytes}`);
61
- }
62
- for (let i = 0; i < L; i++) {
63
- polyUniformEta(v.vec[i], seed, nonce++);
64
- }
65
- }
66
-
67
- function polyVecLUniformGamma1(v, seed, nonce) {
68
- if (seed.length !== CRHBytes) {
69
- throw new Error(`invalid seed length ${seed.length} | Expected length ${CRHBytes}`);
70
- }
71
- for (let i = 0; i < L; i++) {
72
- polyUniformGamma1(v.vec[i], seed, L * nonce + i);
73
- }
74
- }
75
-
76
- function polyVecLReduce(v) {
77
- for (let i = 0; i < L; i++) {
78
- polyReduce(v.vec[i]);
79
- }
80
- }
81
-
82
- function polyVecLAdd(w, u, v) {
83
- for (let i = 0; i < L; ++i) {
84
- polyAdd(w.vec[i], u.vec[i], v.vec[i]);
85
- }
86
- }
87
-
88
- function polyVecLNTT(v) {
89
- for (let i = 0; i < L; ++i) {
90
- polyNTT(v.vec[i]);
91
- }
92
- }
93
-
94
- function polyVecLInvNTTToMont(v) {
95
- for (let i = 0; i < L; ++i) {
96
- polyInvNTTToMont(v.vec[i]);
97
- }
98
- }
99
-
100
- function polyVecLPointWisePolyMontgomery(r, a, v) {
101
- for (let i = 0; i < L; ++i) {
102
- polyPointWiseMontgomery(r.vec[i], a, v.vec[i]);
103
- }
104
- }
105
-
106
- function polyVecLPointWiseAccMontgomery(w, u, v) {
107
- const t = new Poly();
108
- polyPointWiseMontgomery(w, u.vec[0], v.vec[0]);
109
- for (let i = 1; i < L; i++) {
110
- polyPointWiseMontgomery(t, u.vec[i], v.vec[i]);
111
- polyAdd(w, w, t);
112
- }
113
- }
114
-
115
- function polyVecLChkNorm(v, bound) {
116
- for (let i = 0; i < L; i++) {
117
- if (polyChkNorm(v.vec[i], bound) !== 0) {
118
- return 1;
119
- }
120
- }
121
- return 0;
122
- }
123
-
124
- function polyVecKUniformEta(v, seed, nonce) {
125
- for (let i = 0; i < K; ++i) {
126
- polyUniformEta(v.vec[i], seed, nonce++);
127
- }
128
- }
129
-
130
- function polyVecKReduce(v) {
131
- for (let i = 0; i < K; ++i) {
132
- polyReduce(v.vec[i]);
133
- }
134
- }
135
-
136
- function polyVecKCAddQ(v) {
137
- for (let i = 0; i < K; ++i) {
138
- polyCAddQ(v.vec[i]);
139
- }
140
- }
141
-
142
- function polyVecKAdd(w, u, v) {
143
- for (let i = 0; i < K; ++i) {
144
- polyAdd(w.vec[i], u.vec[i], v.vec[i]);
145
- }
146
- }
147
-
148
- function polyVecKSub(w, u, v) {
149
- for (let i = 0; i < K; ++i) {
150
- polySub(w.vec[i], u.vec[i], v.vec[i]);
151
- }
152
- }
153
-
154
- function polyVecKShiftL(v) {
155
- for (let i = 0; i < K; ++i) {
156
- polyShiftL(v.vec[i]);
157
- }
158
- }
159
-
160
- function polyVecKNTT(v) {
161
- for (let i = 0; i < K; i++) {
162
- polyNTT(v.vec[i]);
163
- }
164
- }
165
-
166
- function polyVecKInvNTTToMont(v) {
167
- for (let i = 0; i < K; i++) {
168
- polyInvNTTToMont(v.vec[i]);
169
- }
170
- }
171
-
172
- function polyVecKPointWisePolyMontgomery(r, a, v) {
173
- for (let i = 0; i < K; i++) {
174
- polyPointWiseMontgomery(r.vec[i], a, v.vec[i]);
175
- }
176
- }
177
-
178
- function polyVecKChkNorm(v, bound) {
179
- for (let i = 0; i < K; i++) {
180
- if (polyChkNorm(v.vec[i], bound) !== 0) {
181
- return 1;
182
- }
183
- }
184
- return 0;
185
- }
186
-
187
- function polyVecKPower2round(v1, v0, v) {
188
- for (let i = 0; i < K; i++) {
189
- polyPower2round(v1.vec[i], v0.vec[i], v.vec[i]);
190
- }
191
- }
192
-
193
- function polyVecKDecompose(v1, v0, v) {
194
- for (let i = 0; i < K; i++) {
195
- polyDecompose(v1.vec[i], v0.vec[i], v.vec[i]);
196
- }
197
- }
198
-
199
- function polyVecKMakeHint(h, v0, v1) {
200
- let s = 0;
201
- for (let i = 0; i < K; i++) {
202
- s += polyMakeHint(h.vec[i], v0.vec[i], v1.vec[i]);
203
- }
204
- return s;
205
- }
206
-
207
- function polyVecKUseHint(w, u, h) {
208
- for (let i = 0; i < K; ++i) {
209
- polyUseHint(w.vec[i], u.vec[i], h.vec[i]);
210
- }
211
- }
212
-
213
- function polyVecKPackW1(r, w1) {
214
- for (let i = 0; i < K; ++i) {
215
- polyW1Pack(r, i * PolyW1PackedBytes, w1.vec[i]);
216
- }
217
- }
218
-
219
- module.exports = {
220
- polyVecLUniformEta,
221
- polyVecLUniformGamma1,
222
- polyVecLReduce,
223
- polyVecLAdd,
224
- polyVecLNTT,
225
- polyVecLInvNTTToMont,
226
- polyVecLPointWisePolyMontgomery,
227
- polyVecLPointWiseAccMontgomery,
228
- polyVecLChkNorm,
229
- polyVecKUniformEta,
230
- polyVecKReduce,
231
- polyVecKCAddQ,
232
- polyVecKAdd,
233
- polyVecKSub,
234
- polyVecKShiftL,
235
- polyVecKNTT,
236
- polyVecKInvNTTToMont,
237
- polyVecKPointWisePolyMontgomery,
238
- polyVecKChkNorm,
239
- polyVecKPower2round,
240
- polyVecKDecompose,
241
- polyVecKMakeHint,
242
- polyVecKUseHint,
243
- polyVecKPackW1,
244
- polyVecMatrixPointWiseMontgomery,
245
- PolyVecL,
246
- PolyVecK,
247
- polyVecMatrixExpand,
248
- };
package/src/reduce.js DELETED
@@ -1,25 +0,0 @@
1
- const { Q, QInv } = require('./const.js');
2
-
3
- function montgomeryReduce(a) {
4
- let t = BigInt.asIntN(32, BigInt.asIntN(64, BigInt.asIntN(32, a)) * BigInt(QInv));
5
- t = BigInt.asIntN(32, (a - t * BigInt(Q)) >> 32n);
6
- return t;
7
- }
8
-
9
- function reduce32(a) {
10
- let t = (a + (1 << 22)) >> 23;
11
- t = a - t * Q;
12
- return t;
13
- }
14
-
15
- function cAddQ(a) {
16
- let ar = a;
17
- ar += (ar >> 31) & Q;
18
- return ar;
19
- }
20
-
21
- module.exports = {
22
- montgomeryReduce,
23
- reduce32,
24
- cAddQ,
25
- };
package/src/rounding.js DELETED
@@ -1,42 +0,0 @@
1
- const { D, GAMMA2, Q } = require('./const.js');
2
-
3
- function power2round(a0p, i, a) {
4
- const a0 = a0p;
5
- const a1 = (a + (1 << (D - 1)) - 1) >> D;
6
- a0[i] = a - (a1 << D);
7
- return a1;
8
- }
9
-
10
- function decompose(a0p, i, a) {
11
- const a0 = a0p;
12
- let a1 = (a + 127) >> 7;
13
- a1 = (a1 * 1025 + (1 << 21)) >> 22;
14
- a1 &= 15;
15
-
16
- a0[i] = a - a1 * 2 * GAMMA2;
17
- a0[i] -= (((Q - 1) / 2 - a0[i]) >> 31) & Q;
18
- return a1;
19
- }
20
-
21
- function makeHint(a0, a1) {
22
- if (a0 > GAMMA2 || a0 < -GAMMA2 || (a0 === -GAMMA2 && a1 !== 0)) return 1;
23
-
24
- return 0;
25
- }
26
-
27
- function useHint(a, hint) {
28
- const a0 = new Int32Array(1);
29
- const a1 = decompose(a0, 0, a);
30
-
31
- if (hint === 0) return a1;
32
-
33
- if (a0[0] > 0) return (a1 + 1) & 15;
34
- return (a1 - 1) & 15;
35
- }
36
-
37
- module.exports = {
38
- power2round,
39
- decompose,
40
- makeHint,
41
- useHint,
42
- };
package/src/sign.js DELETED
@@ -1,330 +0,0 @@
1
- const randomBytes = require('randombytes'); // eslint-disable-line import/no-extraneous-dependencies
2
- const { SHAKE } = require('sha3'); // eslint-disable-line import/no-extraneous-dependencies
3
-
4
- const {
5
- PolyVecK,
6
- polyVecKAdd,
7
- polyVecKCAddQ,
8
- polyVecKChkNorm,
9
- polyVecKDecompose,
10
- polyVecKInvNTTToMont,
11
- polyVecKMakeHint,
12
- polyVecKNTT,
13
- polyVecKPackW1,
14
- polyVecKPointWisePolyMontgomery,
15
- polyVecKPower2round,
16
- polyVecKReduce,
17
- polyVecKShiftL,
18
- polyVecKSub,
19
- polyVecKUniformEta,
20
- polyVecKUseHint,
21
- PolyVecL,
22
- polyVecLAdd,
23
- polyVecLChkNorm,
24
- polyVecLInvNTTToMont,
25
- polyVecLNTT,
26
- polyVecLPointWisePolyMontgomery,
27
- polyVecLReduce,
28
- polyVecLUniformEta,
29
- polyVecLUniformGamma1,
30
- polyVecMatrixExpand,
31
- polyVecMatrixPointWiseMontgomery,
32
- } = require('./polyvec.js');
33
- const {
34
- BETA,
35
- CRHBytes,
36
- CryptoBytes,
37
- CryptoPublicKeyBytes,
38
- CryptoSecretKeyBytes,
39
- GAMMA1,
40
- GAMMA2,
41
- K,
42
- L,
43
- OMEGA,
44
- PolyW1PackedBytes,
45
- SeedBytes,
46
- } = require('./const.js');
47
- const { Poly, polyChallenge, polyNTT } = require('./poly.js');
48
- const { packPk, packSig, packSk, unpackPk, unpackSig, unpackSk } = require('./packing.js');
49
-
50
- function cryptoSignKeypair(passedSeed, pk, sk) {
51
- try {
52
- if (pk.length !== CryptoPublicKeyBytes) {
53
- throw new Error(`invalid pk length ${pk.length} | Expected length ${CryptoPublicKeyBytes}`);
54
- }
55
- if (sk.length !== CryptoSecretKeyBytes) {
56
- throw new Error(`invalid sk length ${sk.length} | Expected length ${CryptoSecretKeyBytes}`);
57
- }
58
- } catch (e) {
59
- if (e instanceof TypeError) {
60
- throw new Error(`pk/sk cannot be null`);
61
- } else {
62
- throw new Error(`${e.message}`);
63
- }
64
- }
65
- // eslint-disable-next-line no-unused-vars
66
- const mat = new Array(K).fill().map((_) => new PolyVecL());
67
- const s1 = new PolyVecL();
68
- const s2 = new PolyVecK();
69
- const t1 = new PolyVecK();
70
- const t0 = new PolyVecK();
71
-
72
- // Get randomness for rho, rhoPrime and key
73
- const seed = passedSeed || randomBytes(SeedBytes);
74
-
75
- const state = new SHAKE(256);
76
- let outputLength = 2 * SeedBytes + CRHBytes;
77
- state.update(seed);
78
- const seedBuf = state.digest({ buffer: Buffer.alloc(outputLength) });
79
- const rho = seedBuf.slice(0, SeedBytes);
80
- const rhoPrime = seedBuf.slice(SeedBytes, SeedBytes + CRHBytes);
81
- const key = seedBuf.slice(SeedBytes + CRHBytes);
82
-
83
- // Expand matrix
84
- polyVecMatrixExpand(mat, rho);
85
-
86
- // Sample short vectors s1 and s2
87
- polyVecLUniformEta(s1, rhoPrime, 0);
88
- polyVecKUniformEta(s2, rhoPrime, L);
89
-
90
- // Matrix-vector multiplication
91
- const s1hat = new PolyVecL();
92
- s1hat.copy(s1);
93
- polyVecLNTT(s1hat);
94
- polyVecMatrixPointWiseMontgomery(t1, mat, s1hat);
95
- polyVecKReduce(t1);
96
- polyVecKInvNTTToMont(t1);
97
-
98
- // Add error vector s2
99
- polyVecKAdd(t1, t1, s2);
100
-
101
- // Extract t1 and write public key
102
- polyVecKCAddQ(t1);
103
- polyVecKPower2round(t1, t0, t1);
104
- packPk(pk, rho, t1);
105
-
106
- // Compute H(rho, t1) and write secret key
107
- const hasher = new SHAKE(256);
108
- outputLength = SeedBytes;
109
- hasher.update(Buffer.from(pk, 'hex'));
110
- const tr = new Uint8Array(hasher.digest());
111
- packSk(sk, rho, tr, key, t0, s1, s2);
112
-
113
- return seed;
114
- }
115
-
116
- function cryptoSignSignature(sig, m, sk, randomizedSigning) {
117
- if (sk.length !== CryptoSecretKeyBytes) {
118
- throw new Error(`invalid sk length ${sk.length} | Expected length ${CryptoSecretKeyBytes}`);
119
- }
120
-
121
- const rho = new Uint8Array(SeedBytes);
122
- const tr = new Uint8Array(SeedBytes);
123
- const key = new Uint8Array(SeedBytes);
124
- let rhoPrime = new Uint8Array(CRHBytes);
125
- let nonce = 0;
126
- let state = null;
127
- const mat = Array(K)
128
- .fill()
129
- // eslint-disable-next-line no-unused-vars
130
- .map((_) => new PolyVecL());
131
- const s1 = new PolyVecL();
132
- const y = new PolyVecL();
133
- const z = new PolyVecL();
134
- const t0 = new PolyVecK();
135
- const s2 = new PolyVecK();
136
- const w1 = new PolyVecK();
137
- const w0 = new PolyVecK();
138
- const h = new PolyVecK();
139
- const cp = new Poly();
140
-
141
- unpackSk(rho, tr, key, t0, s1, s2, sk);
142
-
143
- state = new SHAKE(256);
144
- let outputLength = CRHBytes;
145
- state.update(Buffer.from(tr, 'hex'));
146
- state.update(Buffer.from(m, 'hex'));
147
- const mu = new Uint8Array(state.digest({ buffer: Buffer.alloc(outputLength) }));
148
-
149
- if (randomizedSigning) rhoPrime = new Uint8Array(randomBytes(CRHBytes));
150
- else {
151
- state = new SHAKE(256);
152
- outputLength = CRHBytes;
153
- state.update(Buffer.from(key, 'hex'));
154
- state.update(Buffer.from(mu, 'hex'));
155
- rhoPrime.set(state.digest({ buffer: Buffer.alloc(outputLength) }));
156
- }
157
-
158
- polyVecMatrixExpand(mat, rho);
159
- polyVecLNTT(s1);
160
- polyVecKNTT(s2);
161
- polyVecKNTT(t0);
162
-
163
- // eslint-disable-next-line no-constant-condition
164
- while (true) {
165
- polyVecLUniformGamma1(y, rhoPrime, nonce++);
166
- // Matrix-vector multiplication
167
- z.copy(y);
168
- polyVecLNTT(z);
169
- polyVecMatrixPointWiseMontgomery(w1, mat, z);
170
- polyVecKReduce(w1);
171
- polyVecKInvNTTToMont(w1);
172
-
173
- // Decompose w and call the random oracle
174
- polyVecKCAddQ(w1);
175
- polyVecKDecompose(w1, w0, w1);
176
- polyVecKPackW1(sig, w1);
177
-
178
- state = new SHAKE(256);
179
- outputLength = SeedBytes;
180
- state.update(Buffer.from(mu, 'hex'));
181
- state.update(Buffer.from(sig.slice(0, K * PolyW1PackedBytes)), 'hex');
182
- sig.set(state.digest({ buffer: Buffer.alloc(outputLength) }));
183
-
184
- polyChallenge(cp, sig);
185
- polyNTT(cp);
186
-
187
- // Compute z, reject if it reveals secret
188
- polyVecLPointWisePolyMontgomery(z, cp, s1);
189
- polyVecLInvNTTToMont(z);
190
- polyVecLAdd(z, z, y);
191
- polyVecLReduce(z);
192
- if (polyVecLChkNorm(z, GAMMA1 - BETA) !== 0) {
193
- continue;
194
- }
195
-
196
- polyVecKPointWisePolyMontgomery(h, cp, s2);
197
- polyVecKInvNTTToMont(h);
198
- polyVecKSub(w0, w0, h);
199
- polyVecKReduce(w0);
200
- if (polyVecKChkNorm(w0, GAMMA2 - BETA) !== 0) {
201
- continue;
202
- }
203
-
204
- polyVecKPointWisePolyMontgomery(h, cp, t0);
205
- polyVecKInvNTTToMont(h);
206
- polyVecKReduce(h);
207
- if (polyVecKChkNorm(h, GAMMA2) !== 0) {
208
- continue;
209
- }
210
-
211
- polyVecKAdd(w0, w0, h);
212
- const n = polyVecKMakeHint(h, w0, w1);
213
- if (n > OMEGA) {
214
- continue;
215
- }
216
-
217
- packSig(sig, sig, z, h);
218
- return 0;
219
- }
220
- }
221
-
222
- function cryptoSign(msg, sk, randomizedSigning) {
223
- const sm = new Uint8Array(CryptoBytes + msg.length);
224
- const mLen = msg.length;
225
- for (let i = 0; i < mLen; ++i) {
226
- sm[CryptoBytes + mLen - 1 - i] = msg[mLen - 1 - i];
227
- }
228
- const result = cryptoSignSignature(sm, msg, sk, randomizedSigning);
229
-
230
- if (result !== 0) {
231
- throw new Error('failed to sign');
232
- }
233
- return sm;
234
- }
235
-
236
- function cryptoSignVerify(sig, m, pk) {
237
- let i;
238
- const buf = new Uint8Array(K * PolyW1PackedBytes);
239
- const rho = new Uint8Array(SeedBytes);
240
- const mu = new Uint8Array(CRHBytes);
241
- const c = new Uint8Array(SeedBytes);
242
- const c2 = new Uint8Array(SeedBytes);
243
- const cp = new Poly();
244
- // eslint-disable-next-line no-unused-vars
245
- const mat = new Array(K).fill().map((_) => new PolyVecL());
246
- const z = new PolyVecL();
247
- const t1 = new PolyVecK();
248
- const w1 = new PolyVecK();
249
- const h = new PolyVecK();
250
-
251
- if (sig.length !== CryptoBytes) {
252
- return false;
253
- }
254
- if (pk.length !== CryptoPublicKeyBytes) {
255
- return false;
256
- }
257
-
258
- unpackPk(rho, t1, pk);
259
- if (unpackSig(c, z, h, sig)) {
260
- return false;
261
- }
262
- if (polyVecLChkNorm(z, GAMMA1 - BETA)) {
263
- return false;
264
- }
265
-
266
- /* Compute CRH(H(rho, t1), msg) */
267
- let state = new SHAKE(256);
268
- let outputLength = SeedBytes;
269
- state.update(pk.slice(0, CryptoPublicKeyBytes));
270
- mu.set(state.digest({ buffer: Buffer.alloc(outputLength) }));
271
-
272
- state = new SHAKE(256);
273
- outputLength = CRHBytes;
274
- state.update(Buffer.from(mu.slice(0, SeedBytes), 'hex'));
275
- state.update(Buffer.from(m, 'hex'));
276
- mu.set(state.digest({ buffer: Buffer.alloc(outputLength) }));
277
-
278
- /* Matrix-vector multiplication; compute Az - c2^dt1 */
279
- polyChallenge(cp, c);
280
- polyVecMatrixExpand(mat, rho);
281
-
282
- polyVecLNTT(z);
283
- polyVecMatrixPointWiseMontgomery(w1, mat, z);
284
-
285
- polyNTT(cp);
286
- polyVecKShiftL(t1);
287
- polyVecKNTT(t1);
288
- polyVecKPointWisePolyMontgomery(t1, cp, t1);
289
-
290
- polyVecKSub(w1, w1, t1);
291
- polyVecKReduce(w1);
292
- polyVecKInvNTTToMont(w1);
293
-
294
- /* Reconstruct w1 */
295
- polyVecKCAddQ(w1);
296
- polyVecKUseHint(w1, w1, h);
297
- polyVecKPackW1(buf, w1);
298
-
299
- /* Call random oracle and verify challenge */
300
- state = new SHAKE(256);
301
- outputLength = SeedBytes;
302
- state.update(Buffer.from(mu, 'hex'));
303
- state.update(Buffer.from(buf, 'hex'));
304
- c2.set(state.digest({ buffer: Buffer.alloc(outputLength) }));
305
-
306
- for (i = 0; i < SeedBytes; ++i) if (c[i] !== c2[i]) return false;
307
- return true;
308
- }
309
-
310
- function cryptoSignOpen(sm, pk) {
311
- if (sm.length < CryptoBytes) {
312
- return undefined;
313
- }
314
-
315
- const sig = sm.slice(0, CryptoBytes);
316
- const msg = sm.slice(CryptoBytes);
317
- if (!cryptoSignVerify(sig, msg, pk)) {
318
- return undefined;
319
- }
320
-
321
- return msg;
322
- }
323
-
324
- module.exports = {
325
- cryptoSignKeypair,
326
- cryptoSignSignature,
327
- cryptoSign,
328
- cryptoSignVerify,
329
- cryptoSignOpen,
330
- };
@@ -1,42 +0,0 @@
1
- const {
2
- shake128Absorb,
3
- shake128Finalize,
4
- shake128Init,
5
- shake256Absorb,
6
- shake256Finalize,
7
- shake256Init,
8
- } = require('./fips202.js');
9
- const { CRHBytes, SeedBytes } = require('./const.js');
10
-
11
- function dilithiumShake128StreamInit(state, seed, nonce) {
12
- if (seed.length !== SeedBytes) {
13
- throw new Error(`invalid seed length ${seed.length} | expected ${SeedBytes}`);
14
- }
15
- const t = new Uint8Array(2);
16
- t[0] = nonce & 0xff;
17
- t[1] = nonce >> 8;
18
-
19
- shake128Init(state);
20
- shake128Absorb(state, seed);
21
- shake128Absorb(state, t);
22
- shake128Finalize(state);
23
- }
24
-
25
- function dilithiumShake256StreamInit(state, seed, nonce) {
26
- if (seed.length !== CRHBytes) {
27
- throw new Error(`invalid seed length ${seed.length} | expected ${CRHBytes}`);
28
- }
29
- const t = new Uint8Array(2);
30
- t[0] = nonce & 0xff;
31
- t[1] = nonce >> 8;
32
-
33
- shake256Init(state);
34
- shake256Absorb(state, seed);
35
- shake256Absorb(state, t);
36
- shake256Finalize(state);
37
- }
38
-
39
- module.exports = {
40
- dilithiumShake128StreamInit,
41
- dilithiumShake256StreamInit,
42
- };