oxedyne/fe2o3/fe2o3_crypto/js/test/pad.test.mjs
3.1 KiB, 1 run
created by r1870400018:76441, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | // pad.test.mjs -- `../pad.mjs` against `fe2o3_crypto/tests/data/padme_vectors.txt`, the vectors |
| 2 | // the Rust `tests/pad.rs` checks, made by an oracle written from arXiv 1806.03160 alone. |
| 3 | // |
| 4 | // node test/pad.test.mjs |
| 5 | import { readFileSync } from 'node:fs'; |
| 6 | import { dirname, join } from 'node:path'; |
| 7 | import { fileURLToPath } from 'node:url'; |
| 8 | import { PREFIX_LEN, framedLen, pad, padRule, padmeLen, unpad } from '../pad.mjs'; |
| 9 | |
| 10 | let passes = 0, failures = 0; |
| 11 | function check(name, cond, detail) { |
| 12 | if (cond) { passes += 1; console.log(' ok ' + name); } |
| 13 | else { failures += 1; console.log(' FAIL ' + name + (detail ? ' -- ' + detail : '')); } |
| 14 | } |
| 15 | function throws(fn) { try { fn(); return false; } catch (e) { return true; } } |
| 16 | |
| 17 | const DATA = join(dirname(fileURLToPath(import.meta.url)), '..', '..', 'tests', 'data'); |
| 18 | const lines = readFileSync(join(DATA, 'padme_vectors.txt'), 'utf8').split('\n').filter((l) => l && !l.startsWith('#')); |
| 19 | |
| 20 | { |
| 21 | let seen = 0, bad = 0, first = ''; |
| 22 | for (const l of lines) { |
| 23 | const f = l.split(' '); |
| 24 | if (f[0] !== 'padme') continue; |
| 25 | seen += 1; |
| 26 | const got = padmeLen(BigInt(f[1])); |
| 27 | if (got !== BigInt(f[2])) { bad += 1; first = first || l + ' got ' + got; } |
| 28 | if (BigInt(f[2]) <= BigInt(Number.MAX_SAFE_INTEGER) && padmeLen(Number(f[1])) !== Number(f[2])) { bad += 1; first = first || l + ' (Number)'; } |
| 29 | } |
| 30 | check('padmeLen matches every padme vector (' + seen + ')', seen >= 1000 && bad === 0, bad + ' bad, first ' + first); |
| 31 | } |
| 32 | |
| 33 | { |
| 34 | let seen = 0, bad = 0, first = ''; |
| 35 | for (const l of lines) { |
| 36 | const f = l.split(' '); |
| 37 | if (f[0] !== 'frame') continue; |
| 38 | seen += 1; |
| 39 | const rule = padRule({ rule: f[1], floor: Number(f[2]), block: Number(f[3]) }); |
| 40 | const n = Number(f[4]), want = Number(f[5]); |
| 41 | let ok = framedLen(rule, n) === want; |
| 42 | if (ok && n < 5000) { |
| 43 | const body = Uint8Array.from({ length: n }, (_, i) => (i * 7 + 1) & 0xff); |
| 44 | const fr = pad(rule, body); |
| 45 | const b = unpad(fr); |
| 46 | ok = fr.length === want && b.length === n && b.every((x, i) => x === body[i]); |
| 47 | } |
| 48 | if (!ok) { bad += 1; first = first || l + ' got ' + framedLen(rule, n); } |
| 49 | } |
| 50 | check('framedLen, pad and unpad match every frame vector (' + seen + ')', seen >= 1000 && bad === 0, bad + ' bad, first ' + first); |
| 51 | } |
| 52 | |
| 53 | { |
| 54 | const rule = padRule(); |
| 55 | const fr = pad(rule, Uint8Array.of(1, 2, 3)); |
| 56 | check('the default rule frames 3 bytes as 00000003 010203 and zeros to the 256-byte floor', fr.length === 256 && fr[3] === 3 && fr[4] === 1 && fr[6] === 3 && fr.slice(7).every((x) => x === 0)); |
| 57 | const lie = Uint8Array.from(fr); lie[3] = 200; lie[2] = 1; |
| 58 | check('unpad refuses a prefix longer than the frame', throws(() => unpad(lie))); |
| 59 | const dirty = Uint8Array.from(fr); dirty[255] = 1; |
| 60 | check('unpad refuses a fill that is not all zeros', throws(() => unpad(dirty))); |
| 61 | check('unpad refuses a frame shorter than its prefix', throws(() => unpad(new Uint8Array(PREFIX_LEN - 1)))); |
| 62 | check('padRule refuses an unknown rule and an empty block', throws(() => padRule({ rule: 'nope' })) && throws(() => padRule({ rule: 'block', block: 0 }))); |
| 63 | } |
| 64 | |
| 65 | console.log('\npad: ' + passes + ' ok, ' + failures + ' failed'); |
| 66 | process.exit(failures ? 1 : 0); |