@theqrl/dilithium5 0.0.5 → 0.0.7

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": "@theqrl/dilithium5",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "Dilithium-5 cryptography",
5
5
  "keywords": [
6
6
  "dilithium",
@@ -11,7 +11,6 @@
11
11
  "homepage": "https://github.com/theQRL/qrypto.js#readme",
12
12
  "license": "MIT",
13
13
  "main": "src/index.js",
14
- "type": "module",
15
14
  "directories": {
16
15
  "lib": "src",
17
16
  "test": "test"
package/src/const.js CHANGED
@@ -1,41 +1,40 @@
1
- export const Shake128Rate = 168;
2
- export const Shake256Rate = 136;
3
- export const Stream128BlockBytes = Shake128Rate;
4
- export const Stream256BlockBytes = Shake256Rate;
1
+ const Shake128Rate = 168;
2
+ const Shake256Rate = 136;
3
+ const Stream128BlockBytes = Shake128Rate;
4
+ const Stream256BlockBytes = Shake256Rate;
5
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;
6
+ const SeedBytes = 32;
7
+ const CRHBytes = 64;
8
+ const N = 256;
9
+ const Q = 8380417;
10
+ const QInv = 58728449;
11
+ const D = 13;
12
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;
13
+ const K = 8;
14
+ const L = 7;
15
+ const ETA = 2;
16
+ const TAU = 60;
17
+ const BETA = 120;
18
+ const GAMMA1 = 1 << 19;
19
+ const GAMMA2 = Math.floor((Q - 1) / 32);
20
+ const OMEGA = 75;
21
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;
22
+ const PolyT1PackedBytes = 320;
23
+ const PolyT0PackedBytes = 416;
24
+ const PolyETAPackedBytes = 96;
25
+ const PolyZPackedBytes = 640;
26
+ const PolyVecHPackedBytes = OMEGA + K;
27
+ const PolyW1PackedBytes = 128;
28
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;
29
+ const CryptoPublicKeyBytes = SeedBytes + K * PolyT1PackedBytes;
30
+ const CryptoSecretKeyBytes = 3 * SeedBytes + L * PolyETAPackedBytes + K * PolyETAPackedBytes + K * PolyT0PackedBytes;
31
+ const CryptoBytes = SeedBytes + L * PolyZPackedBytes + PolyVecHPackedBytes;
33
32
 
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);
33
+ const PolyUniformNBlocks = Math.floor((768 + Stream128BlockBytes - 1) / Stream128BlockBytes);
34
+ const PolyUniformETANBlocks = Math.floor((136 + Stream256BlockBytes - 1) / Stream256BlockBytes);
35
+ const PolyUniformGamma1NBlocks = Math.floor((PolyZPackedBytes + Stream256BlockBytes - 1) / Stream256BlockBytes);
37
36
 
38
- export const zetas = [
37
+ const zetas = [
39
38
  0, 25847, -2608894, -518909, 237124, -777960, -876248, 466468, 1826347, 2353451, -359251, -2091905, 3119733, -2884855,
40
39
  3111497, 2680103, 2725464, 1024112, -1079900, 3585928, -549488, -1119584, 2619752, -2108549, -2118186, -3859737,
41
40
  -1399561, -3277672, 1757237, -19422, 4010497, 280005, 2706023, 95776, 3077325, 3530437, -1661693, -3592148, -2537516,
@@ -58,3 +57,37 @@ export const zetas = [
58
57
  -3833893, -2939036, -2235985, -420899, -2286327, 183443, -976891, 1612842, -3545687, -554416, 3919660, -48306,
59
58
  -1362209, 3937738, 1400424, -846154, 1976782,
60
59
  ];
60
+
61
+ module.exports = {
62
+ Shake128Rate,
63
+ Shake256Rate,
64
+ Stream128BlockBytes,
65
+ Stream256BlockBytes,
66
+ SeedBytes,
67
+ CRHBytes,
68
+ N,
69
+ Q,
70
+ QInv,
71
+ D,
72
+ K,
73
+ L,
74
+ ETA,
75
+ TAU,
76
+ BETA,
77
+ GAMMA1,
78
+ GAMMA2,
79
+ OMEGA,
80
+ PolyT1PackedBytes,
81
+ PolyT0PackedBytes,
82
+ PolyETAPackedBytes,
83
+ PolyZPackedBytes,
84
+ PolyVecHPackedBytes,
85
+ PolyW1PackedBytes,
86
+ CryptoPublicKeyBytes,
87
+ CryptoSecretKeyBytes,
88
+ CryptoBytes,
89
+ PolyUniformNBlocks,
90
+ PolyUniformETANBlocks,
91
+ PolyUniformGamma1NBlocks,
92
+ zetas,
93
+ };
package/src/fips202.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Shake128Rate, Shake256Rate } from './const.js';
1
+ const { Shake128Rate, Shake256Rate } = require('./const.js');
2
2
 
3
3
  const NRounds = 24;
4
4
 
@@ -29,7 +29,7 @@ const KeccakFRoundConstants = BigUint64Array.from([
29
29
  0x8000000080008008n,
30
30
  ]);
31
31
 
32
- export class KeccakState {
32
+ class KeccakState {
33
33
  constructor() {
34
34
  this.s = new BigUint64Array(25);
35
35
  this.pos = 0;
@@ -384,47 +384,63 @@ function keccakSqueezeBlocks(output, outputOffset, nBlocks, s, r) {
384
384
  }
385
385
  }
386
386
 
387
- export function shake128Init(state) {
387
+ function shake128Init(state) {
388
388
  keccakInit(state.s);
389
389
  state.pos = 0;
390
390
  }
391
391
 
392
- export function shake128Absorb(state, input) {
392
+ function shake128Absorb(state, input) {
393
393
  state.pos = keccakAbsorb(state.s, state.pos, Shake128Rate, input);
394
394
  }
395
395
 
396
- export function shake128Finalize(state) {
396
+ function shake128Finalize(state) {
397
397
  keccakFinalize(state.s, state.pos, Shake128Rate, 0x1f);
398
398
  state.pos = Shake128Rate;
399
399
  }
400
400
 
401
- export function shake128Squeeze(out, state) {
401
+ function shake128Squeeze(out, state) {
402
402
  state.pos = keccakSqueeze(out, state.s, state.pos, Shake128Rate);
403
403
  }
404
404
 
405
- export function shake128AbsorbOnce(state, input) {
405
+ function shake128AbsorbOnce(state, input) {
406
406
  keccakAbsorbOnce(state.s, Shake128Rate, input, 0x1f);
407
407
  state.pos = Shake128Rate;
408
408
  }
409
409
 
410
- export function shake128SqueezeBlocks(out, outputOffset, nBlocks, state) {
410
+ function shake128SqueezeBlocks(out, outputOffset, nBlocks, state) {
411
411
  keccakSqueezeBlocks(out, outputOffset, nBlocks, state.s, Shake128Rate);
412
412
  }
413
413
 
414
- export function shake256Init(state) {
414
+ function shake256Init(state) {
415
415
  keccakInit(state.s);
416
416
  state.pos = 0;
417
417
  }
418
418
 
419
- export function shake256Absorb(state, input) {
419
+ function shake256Absorb(state, input) {
420
420
  state.pos = keccakAbsorb(state.s, state.pos, Shake256Rate, input);
421
421
  }
422
422
 
423
- export function shake256Finalize(state) {
423
+ function shake256Finalize(state) {
424
424
  keccakFinalize(state.s, state.pos, Shake256Rate, 0x1f);
425
425
  state.pos = Shake256Rate;
426
426
  }
427
427
 
428
- export function shake256SqueezeBlocks(out, outputOffset, nBlocks, state) {
428
+ function shake256SqueezeBlocks(out, outputOffset, nBlocks, state) {
429
429
  keccakSqueezeBlocks(out, outputOffset, nBlocks, state.s, Shake256Rate);
430
430
  }
431
+
432
+ module.exports = {
433
+ KeccakState,
434
+ shake128Init,
435
+ shake128Absorb,
436
+ shake128Finalize,
437
+ shake128Squeeze,
438
+ shake128AbsorbOnce,
439
+ shake128SqueezeBlocks,
440
+ shake256Init,
441
+ shake256Absorb,
442
+ shake256Finalize,
443
+ shake256SqueezeBlocks,
444
+ NRounds,
445
+ KeccakFRoundConstants,
446
+ };
package/src/index.js CHANGED
@@ -1,10 +1,10 @@
1
- export * from './const.js';
2
- export * from './poly.js';
3
- export * from './polyvec.js';
4
- export * from './packing.js';
5
- export * from './reduce.js';
6
- export * from './rounding.js';
7
- export * from './symmetric-shake.js';
8
- export * from './ntt.js';
9
- export * from './fips202.js';
10
- export * from './sign.js';
1
+ // export * from './const.js';
2
+ // export * from './poly.js';
3
+ // export * from './polyvec.js';
4
+ // export * from './packing.js';
5
+ // export * from './reduce.js';
6
+ // export * from './rounding.js';
7
+ // export * from './symmetric-shake.js';
8
+ // export * from './ntt.js';
9
+ // export * from './fips202.js';
10
+ // export * from './sign.js';
package/src/ntt.js CHANGED
@@ -1,7 +1,7 @@
1
- import { N, zetas } from './const.js';
2
- import { montgomeryReduce } from './reduce.js';
1
+ const { N, zetas } = require('./const.js');
2
+ const { montgomeryReduce } = require('./reduce.js');
3
3
 
4
- export function ntt(a) {
4
+ function ntt(a) {
5
5
  let k = 0;
6
6
  let j = 0;
7
7
 
@@ -17,7 +17,7 @@ export function ntt(a) {
17
17
  }
18
18
  }
19
19
 
20
- export function invNTTToMont(a) {
20
+ function invNTTToMont(a) {
21
21
  const f = 41978n; // mont^2/256
22
22
  let j = 0;
23
23
  let k = 256;
@@ -38,3 +38,8 @@ export function invNTTToMont(a) {
38
38
  a[j] = Number(montgomeryReduce(BigInt.asIntN(64, f * BigInt(a[j]))));
39
39
  }
40
40
  }
41
+
42
+ module.exports = {
43
+ ntt,
44
+ invNTTToMont,
45
+ };
package/src/packing.js CHANGED
@@ -1,4 +1,4 @@
1
- import {
1
+ const {
2
2
  K,
3
3
  L,
4
4
  N,
@@ -8,8 +8,8 @@ import {
8
8
  PolyT1PackedBytes,
9
9
  PolyZPackedBytes,
10
10
  SeedBytes,
11
- } from './const.js';
12
- import {
11
+ } = require('./const.js');
12
+ const {
13
13
  polyEtaPack,
14
14
  polyEtaUnpack,
15
15
  polyT0Pack,
@@ -18,9 +18,9 @@ import {
18
18
  polyT1Unpack,
19
19
  polyZPack,
20
20
  polyZUnpack,
21
- } from './poly.js';
21
+ } = require('./poly.js');
22
22
 
23
- export function packPk(pkp, rho, t1) {
23
+ function packPk(pkp, rho, t1) {
24
24
  const pk = pkp;
25
25
  for (let i = 0; i < SeedBytes; ++i) {
26
26
  pk[i] = rho[i];
@@ -30,7 +30,7 @@ export function packPk(pkp, rho, t1) {
30
30
  }
31
31
  }
32
32
 
33
- export function unpackPk(rhop, t1, pk) {
33
+ function unpackPk(rhop, t1, pk) {
34
34
  const rho = rhop;
35
35
  for (let i = 0; i < SeedBytes; ++i) {
36
36
  rho[i] = pk[i];
@@ -41,7 +41,7 @@ export function unpackPk(rhop, t1, pk) {
41
41
  }
42
42
  }
43
43
 
44
- export function packSk(skp, rho, tr, key, t0, s1, s2) {
44
+ function packSk(skp, rho, tr, key, t0, s1, s2) {
45
45
  let skOffset = 0;
46
46
  const sk = skp;
47
47
  for (let i = 0; i < SeedBytes; ++i) {
@@ -74,7 +74,7 @@ export function packSk(skp, rho, tr, key, t0, s1, s2) {
74
74
  }
75
75
  }
76
76
 
77
- export function unpackSk(rhoP, trP, keyP, t0, s1, s2, sk) {
77
+ function unpackSk(rhoP, trP, keyP, t0, s1, s2, sk) {
78
78
  let skOffset = 0;
79
79
  const rho = rhoP;
80
80
  const tr = trP;
@@ -109,7 +109,7 @@ export function unpackSk(rhoP, trP, keyP, t0, s1, s2, sk) {
109
109
  }
110
110
  }
111
111
 
112
- export function packSig(sigP, c, z, h) {
112
+ function packSig(sigP, c, z, h) {
113
113
  let sigOffset = 0;
114
114
  const sig = sigP;
115
115
  for (let i = 0; i < SeedBytes; ++i) {
@@ -138,7 +138,7 @@ export function packSig(sigP, c, z, h) {
138
138
  }
139
139
  }
140
140
 
141
- export function unpackSig(cP, z, hP, sig) {
141
+ function unpackSig(cP, z, hP, sig) {
142
142
  let sigOffset = 0;
143
143
  const c = cP;
144
144
  const h = hP;
@@ -183,3 +183,12 @@ export function unpackSig(cP, z, hP, sig) {
183
183
 
184
184
  return 0;
185
185
  }
186
+
187
+ module.exports = {
188
+ packPk,
189
+ unpackPk,
190
+ packSk,
191
+ unpackSk,
192
+ packSig,
193
+ unpackSig,
194
+ };
package/src/poly.js CHANGED
@@ -1,4 +1,4 @@
1
- import {
1
+ const {
2
2
  D,
3
3
  ETA,
4
4
  GAMMA1,
@@ -12,21 +12,23 @@ import {
12
12
  Stream128BlockBytes,
13
13
  Stream256BlockBytes,
14
14
  TAU,
15
- } from './const.js';
16
- import {
15
+ } = require('./const.js');
16
+
17
+ const {
17
18
  KeccakState,
18
19
  shake128SqueezeBlocks,
19
20
  shake256Absorb,
20
21
  shake256Finalize,
21
22
  shake256Init,
22
23
  shake256SqueezeBlocks,
23
- } from './fips202.js';
24
- import { dilithiumShake128StreamInit, dilithiumShake256StreamInit } from './symmetric-shake.js';
25
- import { invNTTToMont, ntt } from './ntt.js';
26
- import { cAddQ, montgomeryReduce, reduce32 } from './reduce.js';
27
- import { decompose, makeHint, power2round, useHint } from './rounding.js';
24
+ } = require('./fips202.js');
25
+
26
+ const { dilithiumShake128StreamInit, dilithiumShake256StreamInit } = require('./symmetric-shake.js');
27
+ const { invNTTToMont, ntt } = require('./ntt.js');
28
+ const { cAddQ, montgomeryReduce, reduce32 } = require('./reduce.js');
29
+ const { decompose, makeHint, power2round, useHint } = require('./rounding.js');
28
30
 
29
- export class Poly {
31
+ class Poly {
30
32
  constructor() {
31
33
  this.coeffs = new Int32Array(N);
32
34
  }
@@ -38,55 +40,55 @@ export class Poly {
38
40
  }
39
41
  }
40
42
 
41
- export function polyReduce(aP) {
43
+ function polyReduce(aP) {
42
44
  const a = aP;
43
45
  for (let i = 0; i < N; ++i) a.coeffs[i] = reduce32(a.coeffs[i]);
44
46
  }
45
47
 
46
- export function polyCAddQ(aP) {
48
+ function polyCAddQ(aP) {
47
49
  const a = aP;
48
50
  for (let i = 0; i < N; ++i) a.coeffs[i] = cAddQ(a.coeffs[i]);
49
51
  }
50
52
 
51
- export function polyAdd(cP, a, b) {
53
+ function polyAdd(cP, a, b) {
52
54
  const c = cP;
53
55
  for (let i = 0; i < N; ++i) c.coeffs[i] = a.coeffs[i] + b.coeffs[i];
54
56
  }
55
57
 
56
- export function polySub(cP, a, b) {
58
+ function polySub(cP, a, b) {
57
59
  const c = cP;
58
60
  for (let i = 0; i < N; ++i) c.coeffs[i] = a.coeffs[i] - b.coeffs[i];
59
61
  }
60
62
 
61
- export function polyShiftL(aP) {
63
+ function polyShiftL(aP) {
62
64
  const a = aP;
63
65
  for (let i = 0; i < N; ++i) a.coeffs[i] <<= D;
64
66
  }
65
67
 
66
- export function polyNTT(a) {
68
+ function polyNTT(a) {
67
69
  ntt(a.coeffs);
68
70
  }
69
71
 
70
- export function polyInvNTTToMont(a) {
72
+ function polyInvNTTToMont(a) {
71
73
  invNTTToMont(a.coeffs);
72
74
  }
73
75
 
74
- export function polyPointWiseMontgomery(cP, a, b) {
76
+ function polyPointWiseMontgomery(cP, a, b) {
75
77
  const c = cP;
76
78
  for (let i = 0; i < N; ++i) c.coeffs[i] = Number(montgomeryReduce(BigInt(a.coeffs[i]) * BigInt(b.coeffs[i])));
77
79
  }
78
80
 
79
- export function polyPower2round(a1p, a0, a) {
81
+ function polyPower2round(a1p, a0, a) {
80
82
  const a1 = a1p;
81
83
  for (let i = 0; i < N; ++i) a1.coeffs[i] = power2round(a0.coeffs, i, a.coeffs[i]);
82
84
  }
83
85
 
84
- export function polyDecompose(a1p, a0, a) {
86
+ function polyDecompose(a1p, a0, a) {
85
87
  const a1 = a1p;
86
88
  for (let i = 0; i < N; ++i) a1.coeffs[i] = decompose(a0.coeffs, i, a.coeffs[i]);
87
89
  }
88
90
 
89
- export function polyMakeHint(hp, a0, a1) {
91
+ function polyMakeHint(hp, a0, a1) {
90
92
  let s = 0;
91
93
  const h = hp;
92
94
  for (let i = 0; i < N; ++i) {
@@ -97,14 +99,14 @@ export function polyMakeHint(hp, a0, a1) {
97
99
  return s;
98
100
  }
99
101
 
100
- export function polyUseHint(bp, a, h) {
102
+ function polyUseHint(bp, a, h) {
101
103
  const b = bp;
102
104
  for (let i = 0; i < N; ++i) {
103
105
  b.coeffs[i] = useHint(a.coeffs[i], h.coeffs[i]);
104
106
  }
105
107
  }
106
108
 
107
- export function polyChkNorm(a, b) {
109
+ function polyChkNorm(a, b) {
108
110
  if (b > Math.floor((Q - 1) / 8)) {
109
111
  return 1;
110
112
  }
@@ -121,7 +123,7 @@ export function polyChkNorm(a, b) {
121
123
  return 0;
122
124
  }
123
125
 
124
- export function rejUniform(ap, aOffset, len, buf, bufLen) {
126
+ function rejUniform(ap, aOffset, len, buf, bufLen) {
125
127
  let ctr = 0;
126
128
  let pos = 0;
127
129
  const a = ap;
@@ -139,7 +141,7 @@ export function rejUniform(ap, aOffset, len, buf, bufLen) {
139
141
  return ctr;
140
142
  }
141
143
 
142
- export function polyUniform(a, seed, nonce) {
144
+ function polyUniform(a, seed, nonce) {
143
145
  let off = 0;
144
146
  let bufLen = PolyUniformNBlocks * Stream128BlockBytes;
145
147
  const buf = new Uint8Array(PolyUniformNBlocks * Stream128BlockBytes + 2);
@@ -160,7 +162,7 @@ export function polyUniform(a, seed, nonce) {
160
162
  }
161
163
  }
162
164
 
163
- export function rejEta(aP, aOffset, len, buf, bufLen) {
165
+ function rejEta(aP, aOffset, len, buf, bufLen) {
164
166
  let ctr;
165
167
  let pos;
166
168
  let t0;
@@ -185,7 +187,7 @@ export function rejEta(aP, aOffset, len, buf, bufLen) {
185
187
  return ctr;
186
188
  }
187
189
 
188
- export function polyUniformEta(a, seed, nonce) {
190
+ function polyUniformEta(a, seed, nonce) {
189
191
  let ctr;
190
192
  const bufLen = PolyUniformETANBlocks * Stream256BlockBytes;
191
193
  const buf = new Uint8Array(bufLen);
@@ -201,7 +203,7 @@ export function polyUniformEta(a, seed, nonce) {
201
203
  }
202
204
  }
203
205
 
204
- export function polyZUnpack(rP, a, aOffset) {
206
+ function polyZUnpack(rP, a, aOffset) {
205
207
  const r = rP;
206
208
  for (let i = 0; i < N / 2; ++i) {
207
209
  r.coeffs[2 * i] = a[aOffset + 5 * i];
@@ -219,7 +221,7 @@ export function polyZUnpack(rP, a, aOffset) {
219
221
  }
220
222
  }
221
223
 
222
- export function polyUniformGamma1(a, seed, nonce) {
224
+ function polyUniformGamma1(a, seed, nonce) {
223
225
  const buf = new Uint8Array(PolyUniformGamma1NBlocks * Stream256BlockBytes);
224
226
 
225
227
  const state = new KeccakState();
@@ -228,7 +230,7 @@ export function polyUniformGamma1(a, seed, nonce) {
228
230
  polyZUnpack(a, buf, 0);
229
231
  }
230
232
 
231
- export function polyChallenge(cP, seed) {
233
+ function polyChallenge(cP, seed) {
232
234
  let b;
233
235
  let pos;
234
236
  const c = cP;
@@ -265,7 +267,7 @@ export function polyChallenge(cP, seed) {
265
267
  }
266
268
  }
267
269
 
268
- export function polyEtaPack(rP, rOffset, a) {
270
+ function polyEtaPack(rP, rOffset, a) {
269
271
  const t = new Uint8Array(8);
270
272
  const r = rP;
271
273
  for (let i = 0; i < N / 8; ++i) {
@@ -284,7 +286,7 @@ export function polyEtaPack(rP, rOffset, a) {
284
286
  }
285
287
  }
286
288
 
287
- export function polyEtaUnpack(rP, a, aOffset) {
289
+ function polyEtaUnpack(rP, a, aOffset) {
288
290
  const r = rP;
289
291
  for (let i = 0; i < N / 8; ++i) {
290
292
  r.coeffs[8 * i] = (a[aOffset + 3 * i] >> 0) & 7;
@@ -307,7 +309,7 @@ export function polyEtaUnpack(rP, a, aOffset) {
307
309
  }
308
310
  }
309
311
 
310
- export function polyT1Pack(rP, rOffset, a) {
312
+ function polyT1Pack(rP, rOffset, a) {
311
313
  const r = rP;
312
314
  for (let i = 0; i < N / 4; ++i) {
313
315
  r[rOffset + 5 * i] = a.coeffs[4 * i] >> 0;
@@ -318,7 +320,7 @@ export function polyT1Pack(rP, rOffset, a) {
318
320
  }
319
321
  }
320
322
 
321
- export function polyT1Unpack(rP, a, aOffset) {
323
+ function polyT1Unpack(rP, a, aOffset) {
322
324
  const r = rP;
323
325
  for (let i = 0; i < N / 4; ++i) {
324
326
  r.coeffs[4 * i] = ((a[aOffset + 5 * i] >> 0) | (a[aOffset + 5 * i + 1] << 8)) & 0x3ff;
@@ -328,7 +330,7 @@ export function polyT1Unpack(rP, a, aOffset) {
328
330
  }
329
331
  }
330
332
 
331
- export function polyT0Pack(rP, rOffset, a) {
333
+ function polyT0Pack(rP, rOffset, a) {
332
334
  const t = new Uint32Array(8);
333
335
  const r = rP;
334
336
  for (let i = 0; i < N / 8; ++i) {
@@ -364,7 +366,7 @@ export function polyT0Pack(rP, rOffset, a) {
364
366
  }
365
367
  }
366
368
 
367
- export function polyT0Unpack(rP, a, aOffset) {
369
+ function polyT0Unpack(rP, a, aOffset) {
368
370
  const r = rP;
369
371
  for (let i = 0; i < N / 8; ++i) {
370
372
  r.coeffs[8 * i] = a[aOffset + 13 * i];
@@ -414,7 +416,7 @@ export function polyT0Unpack(rP, a, aOffset) {
414
416
  }
415
417
  }
416
418
 
417
- export function polyZPack(rP, rOffset, a) {
419
+ function polyZPack(rP, rOffset, a) {
418
420
  const t = new Uint32Array(4);
419
421
  const r = rP;
420
422
  for (let i = 0; i < N / 2; ++i) {
@@ -430,9 +432,41 @@ export function polyZPack(rP, rOffset, a) {
430
432
  }
431
433
  }
432
434
 
433
- export function polyW1Pack(rP, rOffset, a) {
435
+ function polyW1Pack(rP, rOffset, a) {
434
436
  const r = rP;
435
437
  for (let i = 0; i < N / 2; ++i) {
436
438
  r[rOffset + i] = a.coeffs[2 * i] | (a.coeffs[2 * i + 1] << 4);
437
439
  }
438
440
  }
441
+
442
+ module.exports = {
443
+ polyW1Pack,
444
+ polyZPack,
445
+ polyT0Unpack,
446
+ polyT0Pack,
447
+ polyT1Unpack,
448
+ polyT1Pack,
449
+ polyEtaPack,
450
+ polyEtaUnpack,
451
+ polyChallenge,
452
+ polyUniformGamma1,
453
+ polyZUnpack,
454
+ polyUniformEta,
455
+ rejEta,
456
+ polyUniform,
457
+ rejUniform,
458
+ polyChkNorm,
459
+ polyUseHint,
460
+ polyMakeHint,
461
+ polyDecompose,
462
+ polyPower2round,
463
+ polyPointWiseMontgomery,
464
+ polyInvNTTToMont,
465
+ polyNTT,
466
+ polyShiftL,
467
+ polyAdd,
468
+ polySub,
469
+ polyCAddQ,
470
+ polyReduce,
471
+ Poly,
472
+ };
package/src/polyvec.js CHANGED
@@ -1,4 +1,4 @@
1
- import {
1
+ const {
2
2
  Poly,
3
3
  polyAdd,
4
4
  polyCAddQ,
@@ -17,16 +17,16 @@ import {
17
17
  polyUniformGamma1,
18
18
  polyUseHint,
19
19
  polyW1Pack,
20
- } from './poly.js';
21
- import { CRHBytes, K, L, PolyW1PackedBytes, SeedBytes } from './const.js';
20
+ } = require('./poly.js');
21
+ const { CRHBytes, K, L, PolyW1PackedBytes, SeedBytes } = require('./const.js');
22
22
 
23
- export class PolyVecK {
23
+ class PolyVecK {
24
24
  constructor() {
25
25
  this.vec = new Array(K).fill().map((_) => new Poly());
26
26
  }
27
27
  }
28
28
 
29
- export class PolyVecL {
29
+ class PolyVecL {
30
30
  constructor() {
31
31
  this.vec = new Array(L).fill().map((_) => new Poly());
32
32
  }
@@ -38,7 +38,7 @@ export class PolyVecL {
38
38
  }
39
39
  }
40
40
 
41
- export function polyVecMatrixExpand(mat, rho) {
41
+ function polyVecMatrixExpand(mat, rho) {
42
42
  if (rho.length !== SeedBytes) {
43
43
  throw new Error(`invalid rho length ${rho.length} | Expected length ${SeedBytes}`);
44
44
  }
@@ -49,13 +49,13 @@ export function polyVecMatrixExpand(mat, rho) {
49
49
  }
50
50
  }
51
51
 
52
- export function polyVecMatrixPointWiseMontgomery(t, mat, v) {
52
+ function polyVecMatrixPointWiseMontgomery(t, mat, v) {
53
53
  for (let i = 0; i < K; ++i) {
54
54
  polyVecLPointWiseAccMontgomery(t.vec[i], mat[i], v);
55
55
  }
56
56
  }
57
57
 
58
- export function polyVecLUniformEta(v, seed, nonce) {
58
+ function polyVecLUniformEta(v, seed, nonce) {
59
59
  if (seed.length !== CRHBytes) {
60
60
  throw new Error(`invalid seed length ${seed.length} | Expected length ${CRHBytes}`);
61
61
  }
@@ -64,7 +64,7 @@ export function polyVecLUniformEta(v, seed, nonce) {
64
64
  }
65
65
  }
66
66
 
67
- export function polyVecLUniformGamma1(v, seed, nonce) {
67
+ function polyVecLUniformGamma1(v, seed, nonce) {
68
68
  if (seed.length !== CRHBytes) {
69
69
  throw new Error(`invalid seed length ${seed.length} | Expected length ${CRHBytes}`);
70
70
  }
@@ -73,31 +73,31 @@ export function polyVecLUniformGamma1(v, seed, nonce) {
73
73
  }
74
74
  }
75
75
 
76
- export function polyVecLReduce(v) {
76
+ function polyVecLReduce(v) {
77
77
  for (let i = 0; i < L; i++) {
78
78
  polyReduce(v.vec[i]);
79
79
  }
80
80
  }
81
81
 
82
- export function polyVecLAdd(w, u, v) {
82
+ function polyVecLAdd(w, u, v) {
83
83
  for (let i = 0; i < L; ++i) {
84
84
  polyAdd(w.vec[i], u.vec[i], v.vec[i]);
85
85
  }
86
86
  }
87
87
 
88
- export function polyVecLNTT(v) {
88
+ function polyVecLNTT(v) {
89
89
  for (let i = 0; i < L; ++i) {
90
90
  polyNTT(v.vec[i]);
91
91
  }
92
92
  }
93
93
 
94
- export function polyVecLInvNTTToMont(v) {
94
+ function polyVecLInvNTTToMont(v) {
95
95
  for (let i = 0; i < L; ++i) {
96
96
  polyInvNTTToMont(v.vec[i]);
97
97
  }
98
98
  }
99
99
 
100
- export function polyVecLPointWisePolyMontgomery(r, a, v) {
100
+ function polyVecLPointWisePolyMontgomery(r, a, v) {
101
101
  for (let i = 0; i < L; ++i) {
102
102
  polyPointWiseMontgomery(r.vec[i], a, v.vec[i]);
103
103
  }
@@ -112,7 +112,7 @@ function polyVecLPointWiseAccMontgomery(w, u, v) {
112
112
  }
113
113
  }
114
114
 
115
- export function polyVecLChkNorm(v, bound) {
115
+ function polyVecLChkNorm(v, bound) {
116
116
  for (let i = 0; i < L; i++) {
117
117
  if (polyChkNorm(v.vec[i], bound) !== 0) {
118
118
  return 1;
@@ -121,61 +121,61 @@ export function polyVecLChkNorm(v, bound) {
121
121
  return 0;
122
122
  }
123
123
 
124
- export function polyVecKUniformEta(v, seed, nonce) {
124
+ function polyVecKUniformEta(v, seed, nonce) {
125
125
  for (let i = 0; i < K; ++i) {
126
126
  polyUniformEta(v.vec[i], seed, nonce++);
127
127
  }
128
128
  }
129
129
 
130
- export function polyVecKReduce(v) {
130
+ function polyVecKReduce(v) {
131
131
  for (let i = 0; i < K; ++i) {
132
132
  polyReduce(v.vec[i]);
133
133
  }
134
134
  }
135
135
 
136
- export function polyVecKCAddQ(v) {
136
+ function polyVecKCAddQ(v) {
137
137
  for (let i = 0; i < K; ++i) {
138
138
  polyCAddQ(v.vec[i]);
139
139
  }
140
140
  }
141
141
 
142
- export function polyVecKAdd(w, u, v) {
142
+ function polyVecKAdd(w, u, v) {
143
143
  for (let i = 0; i < K; ++i) {
144
144
  polyAdd(w.vec[i], u.vec[i], v.vec[i]);
145
145
  }
146
146
  }
147
147
 
148
- export function polyVecKSub(w, u, v) {
148
+ function polyVecKSub(w, u, v) {
149
149
  for (let i = 0; i < K; ++i) {
150
150
  polySub(w.vec[i], u.vec[i], v.vec[i]);
151
151
  }
152
152
  }
153
153
 
154
- export function polyVecKShiftL(v) {
154
+ function polyVecKShiftL(v) {
155
155
  for (let i = 0; i < K; ++i) {
156
156
  polyShiftL(v.vec[i]);
157
157
  }
158
158
  }
159
159
 
160
- export function polyVecKNTT(v) {
160
+ function polyVecKNTT(v) {
161
161
  for (let i = 0; i < K; i++) {
162
162
  polyNTT(v.vec[i]);
163
163
  }
164
164
  }
165
165
 
166
- export function polyVecKInvNTTToMont(v) {
166
+ function polyVecKInvNTTToMont(v) {
167
167
  for (let i = 0; i < K; i++) {
168
168
  polyInvNTTToMont(v.vec[i]);
169
169
  }
170
170
  }
171
171
 
172
- export function polyVecKPointWisePolyMontgomery(r, a, v) {
172
+ function polyVecKPointWisePolyMontgomery(r, a, v) {
173
173
  for (let i = 0; i < K; i++) {
174
174
  polyPointWiseMontgomery(r.vec[i], a, v.vec[i]);
175
175
  }
176
176
  }
177
177
 
178
- export function polyVecKChkNorm(v, bound) {
178
+ function polyVecKChkNorm(v, bound) {
179
179
  for (let i = 0; i < K; i++) {
180
180
  if (polyChkNorm(v.vec[i], bound) !== 0) {
181
181
  return 1;
@@ -184,19 +184,19 @@ export function polyVecKChkNorm(v, bound) {
184
184
  return 0;
185
185
  }
186
186
 
187
- export function polyVecKPower2round(v1, v0, v) {
187
+ function polyVecKPower2round(v1, v0, v) {
188
188
  for (let i = 0; i < K; i++) {
189
189
  polyPower2round(v1.vec[i], v0.vec[i], v.vec[i]);
190
190
  }
191
191
  }
192
192
 
193
- export function polyVecKDecompose(v1, v0, v) {
193
+ function polyVecKDecompose(v1, v0, v) {
194
194
  for (let i = 0; i < K; i++) {
195
195
  polyDecompose(v1.vec[i], v0.vec[i], v.vec[i]);
196
196
  }
197
197
  }
198
198
 
199
- export function polyVecKMakeHint(h, v0, v1) {
199
+ function polyVecKMakeHint(h, v0, v1) {
200
200
  let s = 0;
201
201
  for (let i = 0; i < K; i++) {
202
202
  s += polyMakeHint(h.vec[i], v0.vec[i], v1.vec[i]);
@@ -204,14 +204,45 @@ export function polyVecKMakeHint(h, v0, v1) {
204
204
  return s;
205
205
  }
206
206
 
207
- export function polyVecKUseHint(w, u, h) {
207
+ function polyVecKUseHint(w, u, h) {
208
208
  for (let i = 0; i < K; ++i) {
209
209
  polyUseHint(w.vec[i], u.vec[i], h.vec[i]);
210
210
  }
211
211
  }
212
212
 
213
- export function polyVecKPackW1(r, w1) {
213
+ function polyVecKPackW1(r, w1) {
214
214
  for (let i = 0; i < K; ++i) {
215
215
  polyW1Pack(r, i * PolyW1PackedBytes, w1.vec[i]);
216
216
  }
217
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 CHANGED
@@ -1,19 +1,25 @@
1
- import { Q, QInv } from './const.js';
1
+ const { Q, QInv } = require('./const.js');
2
2
 
3
- export function montgomeryReduce(a) {
3
+ function montgomeryReduce(a) {
4
4
  let t = BigInt.asIntN(32, BigInt.asIntN(64, BigInt.asIntN(32, a)) * BigInt(QInv));
5
5
  t = BigInt.asIntN(32, (a - t * BigInt(Q)) >> 32n);
6
6
  return t;
7
7
  }
8
8
 
9
- export function reduce32(a) {
9
+ function reduce32(a) {
10
10
  let t = (a + (1 << 22)) >> 23;
11
11
  t = a - t * Q;
12
12
  return t;
13
13
  }
14
14
 
15
- export function cAddQ(a) {
15
+ function cAddQ(a) {
16
16
  let ar = a;
17
17
  ar += (ar >> 31) & Q;
18
18
  return ar;
19
19
  }
20
+
21
+ module.exports = {
22
+ montgomeryReduce,
23
+ reduce32,
24
+ cAddQ,
25
+ };
package/src/rounding.js CHANGED
@@ -1,13 +1,13 @@
1
- import { D, GAMMA2, Q } from './const.js';
1
+ const { D, GAMMA2, Q } = require('./const.js');
2
2
 
3
- export function power2round(a0p, i, a) {
3
+ function power2round(a0p, i, a) {
4
4
  const a0 = a0p;
5
5
  const a1 = (a + (1 << (D - 1)) - 1) >> D;
6
6
  a0[i] = a - (a1 << D);
7
7
  return a1;
8
8
  }
9
9
 
10
- export function decompose(a0p, i, a) {
10
+ function decompose(a0p, i, a) {
11
11
  const a0 = a0p;
12
12
  let a1 = (a + 127) >> 7;
13
13
  a1 = (a1 * 1025 + (1 << 21)) >> 22;
@@ -18,13 +18,13 @@ export function decompose(a0p, i, a) {
18
18
  return a1;
19
19
  }
20
20
 
21
- export function makeHint(a0, a1) {
21
+ function makeHint(a0, a1) {
22
22
  if (a0 > GAMMA2 || a0 < -GAMMA2 || (a0 === -GAMMA2 && a1 !== 0)) return 1;
23
23
 
24
24
  return 0;
25
25
  }
26
26
 
27
- export function useHint(a, hint) {
27
+ function useHint(a, hint) {
28
28
  const a0 = new Int32Array(1);
29
29
  const a1 = decompose(a0, 0, a);
30
30
 
@@ -33,3 +33,10 @@ export function useHint(a, hint) {
33
33
  if (a0[0] > 0) return (a1 + 1) & 15;
34
34
  return (a1 - 1) & 15;
35
35
  }
36
+
37
+ module.exports = {
38
+ power2round,
39
+ decompose,
40
+ makeHint,
41
+ useHint,
42
+ };
package/src/sign.js CHANGED
@@ -1,7 +1,7 @@
1
- import pkg from 'randombytes'; // eslint-disable-line import/no-extraneous-dependencies
2
- import { SHAKE } from 'sha3'; // eslint-disable-line import/no-extraneous-dependencies
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
3
 
4
- import {
4
+ const {
5
5
  PolyVecK,
6
6
  polyVecKAdd,
7
7
  polyVecKCAddQ,
@@ -29,8 +29,8 @@ import {
29
29
  polyVecLUniformGamma1,
30
30
  polyVecMatrixExpand,
31
31
  polyVecMatrixPointWiseMontgomery,
32
- } from './polyvec.js';
33
- import {
32
+ } = require('./polyvec.js');
33
+ const {
34
34
  BETA,
35
35
  CRHBytes,
36
36
  CryptoBytes,
@@ -43,13 +43,11 @@ import {
43
43
  OMEGA,
44
44
  PolyW1PackedBytes,
45
45
  SeedBytes,
46
- } from './const.js';
47
- import { Poly, polyChallenge, polyNTT } from './poly.js';
48
- import { packPk, packSig, packSk, unpackPk, unpackSig, unpackSk } from './packing.js';
46
+ } = require('./const.js');
47
+ const { Poly, polyChallenge, polyNTT } = require('./poly.js');
48
+ const { packPk, packSig, packSk, unpackPk, unpackSig, unpackSk } = require('./packing.js');
49
49
 
50
- const { randomBytes } = pkg;
51
-
52
- export function cryptoSignKeypair(passedSeed, pk, sk) {
50
+ function cryptoSignKeypair(passedSeed, pk, sk) {
53
51
  try {
54
52
  if (pk.length !== CryptoPublicKeyBytes) {
55
53
  throw new Error(`invalid pk length ${pk.length} | Expected length ${CryptoPublicKeyBytes}`);
@@ -72,7 +70,7 @@ export function cryptoSignKeypair(passedSeed, pk, sk) {
72
70
  const t0 = new PolyVecK();
73
71
 
74
72
  // Get randomness for rho, rhoPrime and key
75
- const seed = passedSeed || new Uint8Array(randomBytes(SeedBytes));
73
+ const seed = passedSeed || randomBytes(SeedBytes);
76
74
 
77
75
  const state = new SHAKE(256);
78
76
  let outputLength = 2 * SeedBytes + CRHBytes;
@@ -115,7 +113,7 @@ export function cryptoSignKeypair(passedSeed, pk, sk) {
115
113
  return seed;
116
114
  }
117
115
 
118
- export function cryptoSignSignature(sig, m, sk, randomizedSigning) {
116
+ function cryptoSignSignature(sig, m, sk, randomizedSigning) {
119
117
  if (sk.length !== CryptoSecretKeyBytes) {
120
118
  throw new Error(`invalid sk length ${sk.length} | Expected length ${CryptoSecretKeyBytes}`);
121
119
  }
@@ -221,7 +219,7 @@ export function cryptoSignSignature(sig, m, sk, randomizedSigning) {
221
219
  }
222
220
  }
223
221
 
224
- export function cryptoSign(msg, sk, randomizedSigning) {
222
+ function cryptoSign(msg, sk, randomizedSigning) {
225
223
  const sm = new Uint8Array(CryptoBytes + msg.length);
226
224
  const mLen = msg.length;
227
225
  for (let i = 0; i < mLen; ++i) {
@@ -235,7 +233,7 @@ export function cryptoSign(msg, sk, randomizedSigning) {
235
233
  return sm;
236
234
  }
237
235
 
238
- export function cryptoSignVerify(sig, m, pk) {
236
+ function cryptoSignVerify(sig, m, pk) {
239
237
  let i;
240
238
  const buf = new Uint8Array(K * PolyW1PackedBytes);
241
239
  const rho = new Uint8Array(SeedBytes);
@@ -309,7 +307,7 @@ export function cryptoSignVerify(sig, m, pk) {
309
307
  return true;
310
308
  }
311
309
 
312
- export function cryptoSignOpen(sm, pk) {
310
+ function cryptoSignOpen(sm, pk) {
313
311
  if (sm.length < CryptoBytes) {
314
312
  return undefined;
315
313
  }
@@ -322,3 +320,11 @@ export function cryptoSignOpen(sm, pk) {
322
320
 
323
321
  return msg;
324
322
  }
323
+
324
+ module.exports = {
325
+ cryptoSignKeypair,
326
+ cryptoSignSignature,
327
+ cryptoSign,
328
+ cryptoSignVerify,
329
+ cryptoSignOpen,
330
+ };
@@ -1,14 +1,14 @@
1
- import {
1
+ const {
2
2
  shake128Absorb,
3
3
  shake128Finalize,
4
4
  shake128Init,
5
5
  shake256Absorb,
6
6
  shake256Finalize,
7
7
  shake256Init,
8
- } from './fips202.js';
9
- import { CRHBytes, SeedBytes } from './const.js';
8
+ } = require('./fips202.js');
9
+ const { CRHBytes, SeedBytes } = require('./const.js');
10
10
 
11
- export function dilithiumShake128StreamInit(state, seed, nonce) {
11
+ function dilithiumShake128StreamInit(state, seed, nonce) {
12
12
  if (seed.length !== SeedBytes) {
13
13
  throw new Error(`invalid seed length ${seed.length} | expected ${SeedBytes}`);
14
14
  }
@@ -22,7 +22,7 @@ export function dilithiumShake128StreamInit(state, seed, nonce) {
22
22
  shake128Finalize(state);
23
23
  }
24
24
 
25
- export function dilithiumShake256StreamInit(state, seed, nonce) {
25
+ function dilithiumShake256StreamInit(state, seed, nonce) {
26
26
  if (seed.length !== CRHBytes) {
27
27
  throw new Error(`invalid seed length ${seed.length} | expected ${CRHBytes}`);
28
28
  }
@@ -35,3 +35,8 @@ export function dilithiumShake256StreamInit(state, seed, nonce) {
35
35
  shake256Absorb(state, t);
36
36
  shake256Finalize(state);
37
37
  }
38
+
39
+ module.exports = {
40
+ dilithiumShake128StreamInit,
41
+ dilithiumShake256StreamInit,
42
+ };