|
1 | 1 | import { deepStrictEqual } from 'assert'; |
2 | | -import { compare } from 'micro-bmark'; |
3 | 2 |
|
4 | 3 | export const onlyNoble = process.argv[2] === 'noble'; |
5 | 4 | export function buf(n) { |
6 | 5 | return new Uint8Array(n).fill(n % 251); |
7 | 6 | } |
8 | 7 |
|
9 | 8 | // type Buffers = ({size: string, samples: number, data: Uint8Array})[]; |
10 | | -export async function crossValidate(buffers, ciphers) { |
| 9 | +export async function crossValidate(title, buffers, ciphers) { |
11 | 10 | // 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])); |
13 | 13 | // const bufs = Object.entries(buffers).map((entry) => entry[1][1]) |
14 | 14 | // const bufs = [...Object.entries(buffers).map((i) => i[1][1])]; |
15 | 15 | // Verify different buffer sizes |
16 | 16 | for (let i = 0; i < 2048; i++) bufs.push(buf(i)); |
17 | 17 | // Verify different subarrays positions |
18 | 18 | // const b2 = buf(2048); |
19 | 19 | // 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 | | -} |
43 | 20 |
|
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 = {}; |
53 | 23 | for (const buf of bufs) { |
54 | 24 | 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})`); |
66 | 37 | } |
| 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})`); |
67 | 41 | } |
| 42 | + const bufName = bufMap.get(buf); |
| 43 | + if (bufName) res[bufName] = encrypted; |
68 | 44 | } |
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; |
109 | 46 | } |
0 commit comments