Skip to content

Commit

Permalink
Refactor to CJS for web3-based module compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jplomas committed Mar 29, 2023
1 parent 854a9ff commit 95b0120
Show file tree
Hide file tree
Showing 16 changed files with 331 additions and 178 deletions.
4 changes: 1 addition & 3 deletions packages/dilithium5/package.json
@@ -1,6 +1,6 @@
{
"name": "@theqrl/dilithium5",
"version": "0.0.6",
"version": "0.0.9",
"description": "Dilithium-5 cryptography",
"keywords": [
"dilithium",
Expand All @@ -11,7 +11,6 @@
"homepage": "https://github.com/theQRL/qrypto.js#readme",
"license": "MIT",
"main": "src/index.js",
"type": "module",
"directories": {
"lib": "src",
"test": "test"
Expand Down Expand Up @@ -44,7 +43,6 @@
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"esm": "^3.2.25",
"mocha": "^10.2.0",
"prettier": "^2.8.3"
},
Expand Down
97 changes: 65 additions & 32 deletions packages/dilithium5/src/const.js
@@ -1,41 +1,40 @@
export const Shake128Rate = 168;
export const Shake256Rate = 136;
export const Stream128BlockBytes = Shake128Rate;
export const Stream256BlockBytes = Shake256Rate;
const Shake128Rate = 168;
const Shake256Rate = 136;
const Stream128BlockBytes = Shake128Rate;
const Stream256BlockBytes = Shake256Rate;

export const SeedBytes = 32;
export const CRHBytes = 64;
export const N = 256;
export const Q = 8380417;
export const QInv = 58728449;
export const D = 13;
const SeedBytes = 32;
const CRHBytes = 64;
const N = 256;
const Q = 8380417;
const QInv = 58728449;
const D = 13;

export const K = 8;
export const L = 7;
export const ETA = 2;
export const TAU = 60;
export const BETA = 120;
export const GAMMA1 = 1 << 19;
export const GAMMA2 = Math.floor((Q - 1) / 32);
export const OMEGA = 75;
const K = 8;
const L = 7;
const ETA = 2;
const TAU = 60;
const BETA = 120;
const GAMMA1 = 1 << 19;
const GAMMA2 = Math.floor((Q - 1) / 32);
const OMEGA = 75;

export const PolyT1PackedBytes = 320;
export const PolyT0PackedBytes = 416;
export const PolyETAPackedBytes = 96;
export const PolyZPackedBytes = 640;
export const PolyVecHPackedBytes = OMEGA + K;
export const PolyW1PackedBytes = 128;
const PolyT1PackedBytes = 320;
const PolyT0PackedBytes = 416;
const PolyETAPackedBytes = 96;
const PolyZPackedBytes = 640;
const PolyVecHPackedBytes = OMEGA + K;
const PolyW1PackedBytes = 128;

export const CryptoPublicKeyBytes = SeedBytes + K * PolyT1PackedBytes;
export const CryptoSecretKeyBytes =
3 * SeedBytes + L * PolyETAPackedBytes + K * PolyETAPackedBytes + K * PolyT0PackedBytes;
export const CryptoBytes = SeedBytes + L * PolyZPackedBytes + PolyVecHPackedBytes;
const CryptoPublicKeyBytes = SeedBytes + K * PolyT1PackedBytes;
const CryptoSecretKeyBytes = 3 * SeedBytes + L * PolyETAPackedBytes + K * PolyETAPackedBytes + K * PolyT0PackedBytes;
const CryptoBytes = SeedBytes + L * PolyZPackedBytes + PolyVecHPackedBytes;

export const PolyUniformNBlocks = Math.floor((768 + Stream128BlockBytes - 1) / Stream128BlockBytes);
export const PolyUniformETANBlocks = Math.floor((136 + Stream256BlockBytes - 1) / Stream256BlockBytes);
export const PolyUniformGamma1NBlocks = Math.floor((PolyZPackedBytes + Stream256BlockBytes - 1) / Stream256BlockBytes);
const PolyUniformNBlocks = Math.floor((768 + Stream128BlockBytes - 1) / Stream128BlockBytes);
const PolyUniformETANBlocks = Math.floor((136 + Stream256BlockBytes - 1) / Stream256BlockBytes);
const PolyUniformGamma1NBlocks = Math.floor((PolyZPackedBytes + Stream256BlockBytes - 1) / Stream256BlockBytes);

export const zetas = [
const zetas = [
0, 25847, -2608894, -518909, 237124, -777960, -876248, 466468, 1826347, 2353451, -359251, -2091905, 3119733, -2884855,
3111497, 2680103, 2725464, 1024112, -1079900, 3585928, -549488, -1119584, 2619752, -2108549, -2118186, -3859737,
-1399561, -3277672, 1757237, -19422, 4010497, 280005, 2706023, 95776, 3077325, 3530437, -1661693, -3592148, -2537516,
Expand All @@ -58,3 +57,37 @@ export const zetas = [
-3833893, -2939036, -2235985, -420899, -2286327, 183443, -976891, 1612842, -3545687, -554416, 3919660, -48306,
-1362209, 3937738, 1400424, -846154, 1976782,
];

module.exports = {
Shake128Rate,
Shake256Rate,
Stream128BlockBytes,
Stream256BlockBytes,
SeedBytes,
CRHBytes,
N,
Q,
QInv,
D,
K,
L,
ETA,
TAU,
BETA,
GAMMA1,
GAMMA2,
OMEGA,
PolyT1PackedBytes,
PolyT0PackedBytes,
PolyETAPackedBytes,
PolyZPackedBytes,
PolyVecHPackedBytes,
PolyW1PackedBytes,
CryptoPublicKeyBytes,
CryptoSecretKeyBytes,
CryptoBytes,
PolyUniformNBlocks,
PolyUniformETANBlocks,
PolyUniformGamma1NBlocks,
zetas,
};
40 changes: 28 additions & 12 deletions packages/dilithium5/src/fips202.js
@@ -1,4 +1,4 @@
import { Shake128Rate, Shake256Rate } from './const.js';
const { Shake128Rate, Shake256Rate } = require('./const.js');

const NRounds = 24;

Expand Down Expand Up @@ -29,7 +29,7 @@ const KeccakFRoundConstants = BigUint64Array.from([
0x8000000080008008n,
]);

export class KeccakState {
class KeccakState {
constructor() {
this.s = new BigUint64Array(25);
this.pos = 0;
Expand Down Expand Up @@ -384,47 +384,63 @@ function keccakSqueezeBlocks(output, outputOffset, nBlocks, s, r) {
}
}

export function shake128Init(state) {
function shake128Init(state) {
keccakInit(state.s);
state.pos = 0;
}

export function shake128Absorb(state, input) {
function shake128Absorb(state, input) {
state.pos = keccakAbsorb(state.s, state.pos, Shake128Rate, input);
}

export function shake128Finalize(state) {
function shake128Finalize(state) {
keccakFinalize(state.s, state.pos, Shake128Rate, 0x1f);
state.pos = Shake128Rate;
}

export function shake128Squeeze(out, state) {
function shake128Squeeze(out, state) {
state.pos = keccakSqueeze(out, state.s, state.pos, Shake128Rate);
}

export function shake128AbsorbOnce(state, input) {
function shake128AbsorbOnce(state, input) {
keccakAbsorbOnce(state.s, Shake128Rate, input, 0x1f);
state.pos = Shake128Rate;
}

export function shake128SqueezeBlocks(out, outputOffset, nBlocks, state) {
function shake128SqueezeBlocks(out, outputOffset, nBlocks, state) {
keccakSqueezeBlocks(out, outputOffset, nBlocks, state.s, Shake128Rate);
}

export function shake256Init(state) {
function shake256Init(state) {
keccakInit(state.s);
state.pos = 0;
}

export function shake256Absorb(state, input) {
function shake256Absorb(state, input) {
state.pos = keccakAbsorb(state.s, state.pos, Shake256Rate, input);
}

export function shake256Finalize(state) {
function shake256Finalize(state) {
keccakFinalize(state.s, state.pos, Shake256Rate, 0x1f);
state.pos = Shake256Rate;
}

export function shake256SqueezeBlocks(out, outputOffset, nBlocks, state) {
function shake256SqueezeBlocks(out, outputOffset, nBlocks, state) {
keccakSqueezeBlocks(out, outputOffset, nBlocks, state.s, Shake256Rate);
}

module.exports = {
KeccakState,
shake128Init,
shake128Absorb,
shake128Finalize,
shake128Squeeze,
shake128AbsorbOnce,
shake128SqueezeBlocks,
shake256Init,
shake256Absorb,
shake256Finalize,
shake256SqueezeBlocks,
NRounds,
KeccakFRoundConstants,
};
23 changes: 13 additions & 10 deletions packages/dilithium5/src/index.js
@@ -1,10 +1,13 @@
export * from './const.js';
export * from './poly.js';
export * from './polyvec.js';
export * from './packing.js';
export * from './reduce.js';
export * from './rounding.js';
export * from './symmetric-shake.js';
export * from './ntt.js';
export * from './fips202.js';
export * from './sign.js';
/* eslint-disable global-require */
module.exports = {
...require('./const.js'),
...require('./poly.js'),
...require('./polyvec.js'),
...require('./packing.js'),
...require('./reduce.js'),
...require('./rounding.js'),
...require('./symmetric-shake.js'),
...require('./ntt.js'),
...require('./fips202.js'),
...require('./sign.js'),
};
13 changes: 9 additions & 4 deletions packages/dilithium5/src/ntt.js
@@ -1,7 +1,7 @@
import { N, zetas } from './const.js';
import { montgomeryReduce } from './reduce.js';
const { N, zetas } = require('./const.js');
const { montgomeryReduce } = require('./reduce.js');

export function ntt(a) {
function ntt(a) {
let k = 0;
let j = 0;

Expand All @@ -17,7 +17,7 @@ export function ntt(a) {
}
}

export function invNTTToMont(a) {
function invNTTToMont(a) {
const f = 41978n; // mont^2/256
let j = 0;
let k = 256;
Expand All @@ -38,3 +38,8 @@ export function invNTTToMont(a) {
a[j] = Number(montgomeryReduce(BigInt.asIntN(64, f * BigInt(a[j]))));
}
}

module.exports = {
ntt,
invNTTToMont,
};
29 changes: 19 additions & 10 deletions packages/dilithium5/src/packing.js
@@ -1,4 +1,4 @@
import {
const {
K,
L,
N,
Expand All @@ -8,8 +8,8 @@ import {
PolyT1PackedBytes,
PolyZPackedBytes,
SeedBytes,
} from './const.js';
import {
} = require('./const.js');
const {
polyEtaPack,
polyEtaUnpack,
polyT0Pack,
Expand All @@ -18,9 +18,9 @@ import {
polyT1Unpack,
polyZPack,
polyZUnpack,
} from './poly.js';
} = require('./poly.js');

export function packPk(pkp, rho, t1) {
function packPk(pkp, rho, t1) {
const pk = pkp;
for (let i = 0; i < SeedBytes; ++i) {
pk[i] = rho[i];
Expand All @@ -30,7 +30,7 @@ export function packPk(pkp, rho, t1) {
}
}

export function unpackPk(rhop, t1, pk) {
function unpackPk(rhop, t1, pk) {
const rho = rhop;
for (let i = 0; i < SeedBytes; ++i) {
rho[i] = pk[i];
Expand All @@ -41,7 +41,7 @@ export function unpackPk(rhop, t1, pk) {
}
}

export function packSk(skp, rho, tr, key, t0, s1, s2) {
function packSk(skp, rho, tr, key, t0, s1, s2) {
let skOffset = 0;
const sk = skp;
for (let i = 0; i < SeedBytes; ++i) {
Expand Down Expand Up @@ -74,7 +74,7 @@ export function packSk(skp, rho, tr, key, t0, s1, s2) {
}
}

export function unpackSk(rhoP, trP, keyP, t0, s1, s2, sk) {
function unpackSk(rhoP, trP, keyP, t0, s1, s2, sk) {
let skOffset = 0;
const rho = rhoP;
const tr = trP;
Expand Down Expand Up @@ -109,7 +109,7 @@ export function unpackSk(rhoP, trP, keyP, t0, s1, s2, sk) {
}
}

export function packSig(sigP, c, z, h) {
function packSig(sigP, c, z, h) {
let sigOffset = 0;
const sig = sigP;
for (let i = 0; i < SeedBytes; ++i) {
Expand Down Expand Up @@ -138,7 +138,7 @@ export function packSig(sigP, c, z, h) {
}
}

export function unpackSig(cP, z, hP, sig) {
function unpackSig(cP, z, hP, sig) {
let sigOffset = 0;
const c = cP;
const h = hP;
Expand Down Expand Up @@ -183,3 +183,12 @@ export function unpackSig(cP, z, hP, sig) {

return 0;
}

module.exports = {
packPk,
unpackPk,
packSk,
unpackSk,
packSig,
unpackSig,
};

0 comments on commit 95b0120

Please sign in to comment.