Skip to content

Commit

Permalink
test: 2x speed-up
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Jan 15, 2025
1 parent a4f13a6 commit f79f7cc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
21 changes: 11 additions & 10 deletions test/blake.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import { blake3 } from '../esm/blake3.js';
import { hexToBytes, bytesToHex, utf8ToBytes, concatBytes } from '../esm/utils.js';
import { TYPE_TEST, pattern, json } from './utils.js';

const blake2_vectors = json('./vectors/blake2-kat.json');
const blake2_python = json('./vectors/blake2-python.json');
const blake3_vectors = json('./vectors/blake3.json');
const blake1_vectors = [
{
input: new Uint8Array(0),
Expand Down Expand Up @@ -125,7 +122,8 @@ describe('blake', () => {
throws(() => blake256.create({ salt: new Uint8Array(0) }));
});
should('Blake2 vectors', () => {
for (const v of blake2_vectors) {
const blake2_kat_vectors = json('./vectors/blake2-kat.json');
for (const v of blake2_kat_vectors) {
const hash = { blake2s: blake2s, blake2b: blake2b }[v.hash];
if (!hash) continue;
const [input, exp] = [v.in, v.out].map(hexToBytes);
Expand All @@ -135,6 +133,7 @@ describe('blake', () => {
});
// NodeJS blake2 doesn't support personalization and salt, so we generated vectors using python: see vectors/blake2-gen.py
should('Blake2 python', () => {
const blake2_python = json('./vectors/blake2-python.json');
for (const v of blake2_python) {
const hash = { blake2s: blake2s, blake2b: blake2b }[v.hash];
const opt = { dkLen: v.dkLen };
Expand Down Expand Up @@ -242,7 +241,15 @@ describe('blake', () => {
for (const dkLen of TYPE_TEST.int) throws(() => blake3('test', { dkLen }));
});

should('not allow using both key + context', () => {
// not allow specifying both key / context
throws(() => {
blake3('test', { context: 'string', key: 'key' });
});
});

should('vectors', () => {
const blake3_vectors = json('./vectors/blake3.json');
for (let i = 0; i < blake3_vectors.cases.length; i++) {
const v = blake3_vectors.cases[i];
const res_hash = blake3(pattern(0xfa, v.input_len), { dkLen: v.hash.length / 2 });
Expand Down Expand Up @@ -279,12 +286,6 @@ describe('blake', () => {
for (let i = 0; i < 512; i++) out.push(hashxof.xof(i));
deepStrictEqual(concatBytes(...out), bigOut, 'xof check against fixed size');
});

should('not allow specifying both key / context', () => {
throws(() => {
blake3('test', { context: blake3_vectors.context_string, key: blake3_vectors.key });
});
});
});
});

Expand Down
9 changes: 5 additions & 4 deletions test/keccak.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ import { readFileSync } from 'node:fs';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

const GEN_VECTORS = jsonGZ('vectors/sha3-addons.json.gz').v;
// Generated from test cases of KeccakPRG in XKCP
const PRG_VECTORS = jsonGZ('vectors/sha3-addon-keccak-prg.json.gz');

const _dirname = dirname(fileURLToPath(import.meta.url));
const isBun = !!process.versions.bun;

Expand Down Expand Up @@ -219,6 +215,9 @@ describe('sha3-addons', () => {
});

should('keccakprg', () => {
// Generated from test cases of KeccakPRG in XKCP
const PRG_VECTORS = jsonGZ('vectors/sha3-addon-keccak-prg.json.gz');

for (let i = 0; i < PRG_VECTORS.length; i++) {
const v = PRG_VECTORS[i];
const input = fromHex(v.input);
Expand Down Expand Up @@ -336,6 +335,8 @@ describe('sha3-addons', () => {
});

should('various vectors for cshake, hmac, k12, p, t', () => {
const GEN_VECTORS = jsonGZ('vectors/sha3-addons.json.gz').v;

const tupleData = (hex) => {
const data = hex ? fromHex(hex) : new Uint8Array([]);
const tuples = [];
Expand Down
16 changes: 6 additions & 10 deletions test/slow-big.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { deepStrictEqual } from 'node:assert';
import { scryptSync } from 'node:crypto';
import { describe, should } from 'micro-should';
import { scryptSync as nodeScryptSync } from 'node:crypto';
import { should } from 'micro-should';
import { HASHES } from './hashes.test.js';
import { bytes, integer, gen, RANDOM, serializeCase, executeKDFTests } from './generator.js';
import { RANDOM, executeKDFTests } from './generator.js';
import { sha256 } from '../esm/sha256.js';
import { sha512 } from '../esm/sha512.js';
import { cshake128 } from '../esm/sha3-addons.js';
import { hmac } from '../esm/hmac.js';
import { hkdf } from '../esm/hkdf.js';
import { pbkdf2, pbkdf2Async } from '../esm/pbkdf2.js';
import { scrypt, scryptAsync } from '../esm/scrypt.js';
import { argon2i, argon2d, argon2id } from '../esm/argon2.js';
import { bytesToHex, hexToBytes } from '../esm/utils.js';
import { json, pattern } from './utils.js';

const argon2_vectors = json('./vectors/argon2.json');

const KB = 1024;
const MB = 1024 * KB;
Expand Down Expand Up @@ -108,7 +104,7 @@ const opts_2gb = [
for (const opts of opts_2gb) {
should(`Scrypt (2GB): ${opts}`, async () => {
const exp = Uint8Array.from(
scryptSync(PASSWORD, SALT, 32, {
nodeScryptSync(PASSWORD, SALT, 32, {
...opts,
maxmem: 16 * 1024 ** 3,
})
Expand All @@ -123,7 +119,7 @@ for (const opts of opts_2gb) {
should('Scrypt (4GB)', async () => {
const opts = { N: 2 ** 15, r: 1024, p: 1 };
const exp = Uint8Array.from(
scryptSync(PASSWORD, SALT, 32, {
nodeScryptSync(PASSWORD, SALT, 32, {
...opts,
maxmem: 4 * 1024 ** 3 + 128 * 1024 + 128 * 1024 * 2, // 8 GB (V) + 128kb (B) + 256kb (XY)
})
Expand Down Expand Up @@ -220,7 +216,7 @@ if (supports5GB) {
should('Scrypt (16GB)', async () => {
const opts = { N: 2 ** 24, r: 8, p: 1 };
const exp = Uint8Array.from(
scryptSync(PASSWORD, SALT, 32, {
nodeScryptSync(PASSWORD, SALT, 32, {
...opts,
maxmem: 17 * GB,
})
Expand Down

0 comments on commit f79f7cc

Please sign in to comment.