From 9918efc72b20a3d008018f7ad51098d8d01eefd0 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Thu, 21 Sep 2023 15:20:15 +0000 Subject: [PATCH] Refactor a bit, improve tree-shaking --- src/_micro.ts | 11 ++++------- src/_poly1305.ts | 8 ++++---- src/_polyval.ts | 2 +- src/_salsa.ts | 22 +++++++++++----------- src/salsa.ts | 4 ++-- 5 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/_micro.ts b/src/_micro.ts index 0cf1878..4e22c1e 100644 --- a/src/_micro.ts +++ b/src/_micro.ts @@ -6,17 +6,14 @@ import * as u from './utils.js'; import { salsaBasic } from './_salsa.js'; + // Utils -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}`); -} function bytesToNumberLE(bytes: Uint8Array): bigint { - return hexToNumber(u.bytesToHex(Uint8Array.from(bytes).reverse())); + return u.hexToNumber(u.bytesToHex(Uint8Array.from(bytes).reverse())); } + function numberToBytesLE(n: number | bigint, len: number): Uint8Array { - return u.hexToBytes(n.toString(16).padStart(len * 2, '0')).reverse(); + return u.numberToBytesBE(n, len).reverse(); } const rotl = (a: number, b: number) => (a << b) | (a >>> (32 - b)); diff --git a/src/_poly1305.ts b/src/_poly1305.ts index 70d8f1c..b362ef5 100644 --- a/src/_poly1305.ts +++ b/src/_poly1305.ts @@ -1,5 +1,5 @@ +import { exists as aexists, output as aoutput } from './_assert.js'; import { toBytes, Input, ensureBytes, Hash } from './utils.js'; -import assert from './_assert.js'; // Poly1305 is a fast and parallel secret-key message-authentication code. // https://cr.yp.to/mac.html, https://cr.yp.to/mac/poly1305-20050329.pdf @@ -216,7 +216,7 @@ class Poly1305 implements Hash { } } update(data: Input): this { - assert.exists(this); + aexists(this); const { buffer, blockLen } = this; data = toBytes(data); const len = data.length; @@ -245,8 +245,8 @@ class Poly1305 implements Hash { this.pad.fill(0); } digestInto(out: Uint8Array) { - assert.exists(this); - assert.output(out, this); + aexists(this); + aoutput(out, this); this.finished = true; const { buffer, h } = this; let { pos } = this; diff --git a/src/_polyval.ts b/src/_polyval.ts index 41f5b46..27f9661 100644 --- a/src/_polyval.ts +++ b/src/_polyval.ts @@ -1,4 +1,4 @@ -import { u8, u32, ensureBytes } from './utils.js'; +import { ensureBytes, u8, u32 } from './utils.js'; // AES-SIV polyval, little-endian "mirror image" of AES-GCM GHash // polynomial hash function. Defined in RFC 8452. diff --git a/src/_salsa.ts b/src/_salsa.ts index 85410d4..85ae120 100644 --- a/src/_salsa.ts +++ b/src/_salsa.ts @@ -1,6 +1,6 @@ // Basic utils for salsa-like ciphers // Check out _micro.ts for descriptive documentation. -import assert from './_assert.js'; +import { number as anumber, bytes as abytes, bool as abool } from './_assert.js'; import { u32, utf8ToBytes, checkOpts } from './utils.js'; /* @@ -84,11 +84,11 @@ export const salsaBasic = (opts: SalsaOpts) => { { rounds: 20, counterRight: false, counterLen: 8, allow128bitKeys: true, blockLen: 64 }, opts ); - assert.number(counterLen); - assert.number(rounds); - assert.number(blockLen); - assert.bool(counterRight); - assert.bool(allow128bitKeys); + anumber(counterLen); + anumber(rounds); + anumber(blockLen); + abool(counterRight); + abool(allow128bitKeys); const blockLen32 = blockLen / 4; if (blockLen % 4 !== 0) throw new Error('Salsa/ChaCha: blockLen must be aligned to 4 bytes'); return ( @@ -98,12 +98,12 @@ export const salsaBasic = (opts: SalsaOpts) => { output?: Uint8Array, counter = 0 ): Uint8Array => { - assert.bytes(key); - assert.bytes(nonce); - assert.bytes(data); + abytes(key); + abytes(nonce); + abytes(data); if (!output) output = new Uint8Array(data.length); - assert.bytes(output); - assert.number(counter); + abytes(output); + anumber(counter); // > new Uint32Array([2**32]) // Uint32Array(1) [ 0 ] // > new Uint32Array([2**32-1]) diff --git a/src/salsa.ts b/src/salsa.ts index 3f084b3..3b390b5 100644 --- a/src/salsa.ts +++ b/src/salsa.ts @@ -1,6 +1,6 @@ -import { ensureBytes, u32, equalBytes, Cipher } from './utils.js'; -import { salsaBasic } from './_salsa.js'; +import { Cipher, ensureBytes, equalBytes, u32 } from './utils.js'; import { poly1305 } from './_poly1305.js'; +import { salsaBasic } from './_salsa.js'; // Salsa20 stream cipher was released in 2005. // Salsa's goal was to implement AES replacement that does not rely on S-Boxes,