Skip to content

Commit

Permalink
Refactor a bit, improve tree-shaking
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Sep 21, 2023
1 parent 30fc73f commit 9918efc
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 25 deletions.
11 changes: 4 additions & 7 deletions src/_micro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
8 changes: 4 additions & 4 deletions src/_poly1305.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -216,7 +216,7 @@ class Poly1305 implements Hash<Poly1305> {
}
}
update(data: Input): this {
assert.exists(this);
aexists(this);
const { buffer, blockLen } = this;
data = toBytes(data);
const len = data.length;
Expand Down Expand Up @@ -245,8 +245,8 @@ class Poly1305 implements Hash<Poly1305> {
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;
Expand Down
2 changes: 1 addition & 1 deletion src/_polyval.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
22 changes: 11 additions & 11 deletions src/_salsa.ts
Original file line number Diff line number Diff line change
@@ -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';

/*
Expand Down Expand Up @@ -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 (
Expand All @@ -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])
Expand Down
4 changes: 2 additions & 2 deletions src/salsa.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 9918efc

Please sign in to comment.