From ca022b3c056f93f8daf7248a807f4dc2bdbff3bd Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Fri, 22 Nov 2024 18:19:59 +0000 Subject: [PATCH] Refactor: remove backticks to be parser-friendly --- src/_assert.ts | 33 +++++++++++++++++---------------- src/_polyval.ts | 2 +- src/aes.ts | 20 ++++++++++---------- src/ff1.ts | 2 +- src/utils.ts | 9 ++++----- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/_assert.ts b/src/_assert.ts index f77263c..d899162 100644 --- a/src/_assert.ts +++ b/src/_assert.ts @@ -1,48 +1,49 @@ function anumber(n: number) { - if (!Number.isSafeInteger(n) || n < 0) throw new Error(`positive integer expected, not ${n}`); + if (!Number.isSafeInteger(n) || n < 0) throw new Error('positive integer expected, got ' + n); } -function abool(b: boolean) { - if (typeof b !== 'boolean') throw new Error(`boolean expected, not ${b}`); -} - -export function isBytes(a: unknown): a is Uint8Array { +// copied from utils +function isBytes(a: unknown): a is Uint8Array { return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array'); } function abytes(b: Uint8Array | undefined, ...lengths: number[]) { if (!isBytes(b)) throw new Error('Uint8Array expected'); if (lengths.length > 0 && !lengths.includes(b.length)) - throw new Error(`Uint8Array expected of length ${lengths}, not of length=${b.length}`); + throw new Error('Uint8Array expected of length ' + lengths + ', got length=' + b.length); } -export type Hash = { +type Hash = { (data: Uint8Array): Uint8Array; blockLen: number; outputLen: number; create: any; }; -function ahash(hash: Hash) { - if (typeof hash !== 'function' || typeof hash.create !== 'function') - throw new Error('hash must be wrapped by utils.wrapConstructor'); - anumber(hash.outputLen); - anumber(hash.blockLen); +function ahash(h: Hash) { + if (typeof h !== 'function' || typeof h.create !== 'function') + throw new Error('Hash should be wrapped by utils.wrapConstructor'); + anumber(h.outputLen); + anumber(h.blockLen); } function aexists(instance: any, checkFinished = true) { if (instance.destroyed) throw new Error('Hash instance has been destroyed'); if (checkFinished && instance.finished) throw new Error('Hash#digest() has already been called'); } - function aoutput(out: any, instance: any) { abytes(out); const min = instance.outputLen; if (out.length < min) { - throw new Error(`digestInto() expects output buffer of length at least ${min}`); + throw new Error('digestInto() expects output buffer of length at least ' + min); } } -export { anumber, abool, abytes, ahash, aexists, aoutput }; +function abool(b: boolean) { + if (typeof b !== 'boolean') throw new Error(`boolean expected, not ${b}`); +} + +export { anumber, abool, abytes, abytes as bytes, ahash, aexists, aoutput, isBytes }; + const assert = { number: anumber, bool: abool, diff --git a/src/_polyval.ts b/src/_polyval.ts index dfce35a..056429e 100644 --- a/src/_polyval.ts +++ b/src/_polyval.ts @@ -90,7 +90,7 @@ class GHASH implements Hash { } const W = estimateWindow(expectedLength || 1024); if (![1, 2, 4, 8].includes(W)) - throw new Error(`ghash: wrong window size=${W}, should be 2, 4 or 8`); + throw new Error('ghash: invalid window size, expected 2, 4 or 8'); this.W = W; const bits = 128; // always 128 bits; const windows = bits / W; diff --git a/src/aes.ts b/src/aes.ts index e7cc7c3..4306a51 100644 --- a/src/aes.ts +++ b/src/aes.ts @@ -122,7 +122,7 @@ export function expandKeyLE(key: Uint8Array): Uint32Array { abytes(key); const len = key.length; if (![16, 24, 32].includes(len)) - throw new Error(`aes: wrong key size: should be 16, 24 or 32, got: ${len}`); + throw new Error('aes: invalid key size, should be 16, 24 or 32, got ' + len); const { sbox2 } = tableEncoding; const toClean = []; if (!isAligned32(key)) toClean.push((key = copyBytes(key))); @@ -230,9 +230,9 @@ function getDst(len: number, output?: Uint8Array): Uint8Array { abytes(output); if (output.length < len) throw new Error( - `aes: wrong destination length, expected at least ${len}, got: ${output.length}` + 'aes: invalid destination length, expected at least ' + len + ', got: ' + output.length ); - if (!isAligned32(output)) throw new Error('unaligned output'); + if (!isAligned32(output)) throw new Error('destination must not be unaligned'); return output; } @@ -351,7 +351,7 @@ function validateBlockDecrypt(data: Uint8Array) { abytes(data); if (data.length % BLOCK_SIZE !== 0) { throw new Error( - `aes-(cbc/ecb).decrypt ciphertext should consist of blocks with size ${BLOCK_SIZE}` + 'aes-(cbc/ecb).decrypt ciphertext should consist of blocks with size ' + BLOCK_SIZE ); } } @@ -642,8 +642,10 @@ export const gcm = wrapCipher( ); const limit = (name: string, min: number, max: number) => (value: number) => { - if (!Number.isSafeInteger(value) || min > value || value > max) - throw new Error(`${name}: invalid value=${value}, must be [${min}..${max}]`); + if (!Number.isSafeInteger(value) || min > value || value > max) { + const minmax = '[' + min + '..' + max + ']'; + throw new Error('' + name + ': expected value in range ' + minmax + ', got ' + value); + } }; /** @@ -749,11 +751,9 @@ export const siv = wrapCipher( } ); -function isBytes32(a: unknown): a is Uint8Array { +function isBytes32(a: unknown): a is Uint32Array { return ( - a != null && - typeof a === 'object' && - (a instanceof Uint32Array || a.constructor.name === 'Uint32Array') + a instanceof Uint32Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint32Array') ); } diff --git a/src/ff1.ts b/src/ff1.ts index 781d1cb..23661da 100644 --- a/src/ff1.ts +++ b/src/ff1.ts @@ -24,7 +24,7 @@ function NUMradix(radix: number, data: number[]): bigint { } function getRound(radix: number, key: Uint8Array, tweak: Uint8Array, x: number[]) { - if (radix > 2 ** 16 - 1) throw new Error(`Invalid radix: ${radix}`); + if (radix > 2 ** 16 - 1) throw new Error('invalid radix ' + radix); // radix**minlen ≥ 100 const minLen = Math.ceil(Math.log(100) / Math.log(radix)); const maxLen = 2 ** 32 - 1; diff --git a/src/utils.ts b/src/utils.ts index 46d269f..0a93b87 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -69,8 +69,7 @@ export function hexToBytes(hex: string): Uint8Array { export function hexToNumber(hex: string): bigint { if (typeof hex !== 'string') throw new Error('hex string expected, got ' + typeof hex); - // Big Endian - return BigInt(hex === '' ? '0' : `0x${hex}`); + return BigInt(hex === '' ? '0' : '0x' + hex); // Big Endian } // BE: Big Endian, LE: Little Endian @@ -109,7 +108,7 @@ declare const TextDecoder: any; * @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99]) */ export function utf8ToBytes(str: string): Uint8Array { - if (typeof str !== 'string') throw new Error(`string expected, got ${typeof str}`); + if (typeof str !== 'string') throw new Error('string expected'); return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809 } @@ -129,7 +128,7 @@ export type Input = Uint8Array | string; export function toBytes(data: Input): Uint8Array { if (typeof data === 'string') data = utf8ToBytes(data); else if (isBytes(data)) data = copyBytes(data); - else throw new Error(`Uint8Array expected, got ${typeof data}`); + else throw new Error('Uint8Array expected, got ' + typeof data); return data; } @@ -251,7 +250,7 @@ export const wrapCipher = , P extends CipherParams>( decrypt(data: Uint8Array, output?: Uint8Array) { abytes(data); if (tagl && data.length < tagl) - throw new Error(`ciphertext is smaller than tagLength=${tagl}`); + throw new Error('invalid ciphertext length: smaller than tagLength=' + tagl); return (cipher as CipherWithOutput).decrypt(data, output); }, };