Skip to content

Commit

Permalink
Refactor: remove backticks to be parser-friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Nov 22, 2024
1 parent 5d13515 commit ca022b3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
33 changes: 17 additions & 16 deletions src/_assert.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/_polyval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class GHASH implements Hash<GHASH> {
}
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;
Expand Down
20 changes: 10 additions & 10 deletions src/aes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
);
}
}
Expand Down Expand Up @@ -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);
}
};

/**
Expand Down Expand Up @@ -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')
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/ff1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 4 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -251,7 +250,7 @@ export const wrapCipher = <C extends CipherCons<any>, 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);
},
};
Expand Down

0 comments on commit ca022b3

Please sign in to comment.