Skip to content

Commit bd2c113

Browse files
committed
Update ci scripts. Update benchmark library
1 parent aeadebb commit bd2c113

14 files changed

Lines changed: 682 additions & 795 deletions

File tree

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ on:
44
types: [created]
55
jobs:
66
release-js:
7-
name: "jsbt v0.3.0" # Should match commit below
8-
uses: paulmillr/jsbt/.github/workflows/release.yml@973650a225c0344aa5f993a6cd63835a262077e9
7+
name: 'jsbt v0.3.1' # Should match commit below
8+
uses: paulmillr/jsbt/.github/workflows/release.yml@c45f03360e0171b138f04568d2fdd35d7bbc0d35
99
with:
1010
build-path: test/build
1111
secrets:

.github/workflows/test-js.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ on:
44
- pull_request
55
jobs:
66
test-js:
7-
name: "jsbt v0.3.0" # Should match commit below
8-
uses: paulmillr/jsbt/.github/workflows/test-js.yml@973650a225c0344aa5f993a6cd63835a262077e9
7+
name: 'jsbt v0.3.1' # Should match commit below
8+
uses: paulmillr/jsbt/.github/workflows/test-js.yml@c45f03360e0171b138f04568d2fdd35d7bbc0d35

benchmark/_utils.js

Lines changed: 23 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,46 @@
11
import { deepStrictEqual } from 'assert';
2-
import { compare } from 'micro-bmark';
32

43
export const onlyNoble = process.argv[2] === 'noble';
54
export function buf(n) {
65
return new Uint8Array(n).fill(n % 251);
76
}
87

98
// type Buffers = ({size: string, samples: number, data: Uint8Array})[];
10-
export async function crossValidate(buffers, ciphers) {
9+
export async function crossValidate(title, buffers, ciphers) {
1110
// Verify that things we bench actually work
12-
const bufs = buffers.map((buf) => buf.data);
11+
const bufs = Object.values(buffers);
12+
const bufMap = new Map(Object.entries(buffers).map(([k, v]) => [v, k]));
1313
// const bufs = Object.entries(buffers).map((entry) => entry[1][1])
1414
// const bufs = [...Object.entries(buffers).map((i) => i[1][1])];
1515
// Verify different buffer sizes
1616
for (let i = 0; i < 2048; i++) bufs.push(buf(i));
1717
// Verify different subarrays positions
1818
// const b2 = buf(2048);
1919
// for (let i = 0; i < 2048; i++) bufs.push(b2.subarray(i));
20-
for (const buf of bufs) {
21-
const b = buf.slice();
22-
// ciphers
23-
for (let [k, libs] of Object.entries(ciphers)) {
24-
// Skip some buffers for block ciphers without padding
25-
if (libs.opts.blockSize && b.length % libs.opts.blockSize) continue;
26-
let encrypted;
27-
for (const [lib, fn] of Object.entries(libs)) {
28-
if (lib === 'opts') continue;
29-
if (encrypted === undefined) {
30-
encrypted = await fn.encrypt(buf, libs.opts);
31-
} else {
32-
const cur = await fn.encrypt(buf, libs.opts);
33-
deepStrictEqual(encrypted, cur, `encrypt verify (${lib})`);
34-
}
35-
deepStrictEqual(buf, b, `encrypt mutates buffer (${lib})`);
36-
const res = await fn.decrypt(encrypted, libs.opts);
37-
deepStrictEqual(res, buf, `decrypt verify (${lib})`);
38-
}
39-
}
40-
}
41-
console.log('Libraries cross-validated against each other correctly');
42-
}
4320

44-
export async function validateHashes(buffers, HASHES) {
45-
// Verify that things we bench actually work
46-
// const bufs = [...Object.entries(buffers).map((i) => i[1][1])];
47-
const bufs = buffers.map((buf) => buf.data);
48-
// Verify different buffer sizes
49-
for (let i = 0; i < 2048; i++) bufs.push(buf(i));
50-
// Verify different subarrays positions
51-
// const b2 = buf(2048);
52-
//for (let i = 0; i < 2048; i++) bufs.push(b2.subarray(i));
21+
// Return encrypted values for buffers for decrypt test
22+
const res = {};
5323
for (const buf of bufs) {
5424
const b = buf.slice();
55-
// hashes
56-
for (let [k, libs] of Object.entries(HASHES)) {
57-
let value;
58-
for (const [lib, fn] of Object.entries(libs)) {
59-
if (lib === 'opts') continue;
60-
if (value === undefined) value = fn(buf, libs.opts);
61-
else {
62-
const cur = fn(buf, libs.opts);
63-
deepStrictEqual(value, cur, `hash verify (${lib})`);
64-
}
65-
deepStrictEqual(buf, b, `hash mutates buffer (${lib})`);
25+
// ciphers
26+
let encrypted;
27+
const opts = ciphers.options;
28+
// Skip some buffers for block ciphers without padding
29+
if (opts.blockSize && b.length % opts.blockSize) continue;
30+
for (let [lib, fn] of Object.entries(ciphers)) {
31+
if (lib === 'options') continue;
32+
if (encrypted === undefined) {
33+
encrypted = await fn.encrypt(buf, opts);
34+
} else {
35+
const cur = await fn.encrypt(buf, opts);
36+
deepStrictEqual(encrypted, cur, `${title}: encrypt verify (${lib})`);
6637
}
38+
deepStrictEqual(buf, b, `${title}: encrypt mutates buffer (${lib})`);
39+
const res = await fn.decrypt(encrypted, opts);
40+
deepStrictEqual(res, buf, `${title}: decrypt verify (${lib})`);
6741
}
42+
const bufName = bufMap.get(buf);
43+
if (bufName) res[bufName] = encrypted;
6844
}
69-
console.log('Libraries cross-validated against each other correctly');
70-
}
71-
72-
export async function benchmarkOnlyNoble(buffers, ciphers) {
73-
const nobleImpls = [];
74-
// chacha20_poly1305: {
75-
// opts: { key: buf(32), nonce: buf(12) },
76-
// noble: {
77-
for (const [algoName, implementations] of Object.entries(ciphers)) {
78-
const { opts } = implementations;
79-
let implementation = implementations.noble;
80-
if (implementation) {
81-
nobleImpls.push({ algoName, implementation, opts });
82-
}
83-
}
84-
for (const { size, samples, data: buf } of buffers) {
85-
await compare(
86-
`encrypt (${size})`,
87-
samples,
88-
Object.fromEntries(
89-
nobleImpls.map((impl) => [impl.algoName, () => impl.implementation.encrypt(buf, impl.opts)])
90-
)
91-
);
92-
}
93-
}
94-
95-
export async function benchmarkAllLibraries(buffers, ciphers) {
96-
for (let [algoName, libraries] of Object.entries(ciphers)) {
97-
console.log(`==== ${algoName} ====`);
98-
const { opts } = libraries;
99-
for (const { size, samples, data: buf } of buffers) {
100-
const libs = Object.entries(libraries).filter(([lib, _]) => lib !== 'opts');
101-
const firstLibrary = libs[0][1];
102-
const encrypted = await firstLibrary.encrypt(buf, opts);
103-
const encrypts = libs.map(([lib, fn]) => [lib, () => fn.encrypt(buf, opts)]);
104-
const decrypts = libs.map(([lib, fn]) => [lib, () => fn.decrypt(encrypted, opts)]);
105-
await compare(`${algoName} (encrypt, ${size})`, samples, Object.fromEntries(encrypts));
106-
await compare(`${algoName} (decrypt, ${size})`, samples, Object.fromEntries(decrypts));
107-
}
108-
}
45+
return res;
10946
}

benchmark/aead.js

Lines changed: 0 additions & 136 deletions
This file was deleted.

0 commit comments

Comments
 (0)