Skip to content

Commit

Permalink
Use typescript erasableSyntaxOnly: true
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Feb 1, 2025
1 parent f3c6095 commit da370b6
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 49 deletions.
9 changes: 7 additions & 2 deletions src/_blake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ export abstract class BLAKE<T extends BLAKE<T>> extends Hash<T> {
protected pos: number = 0;
protected finished = false;
protected destroyed = false;
readonly blockLen: number;
readonly outputLen: number;

constructor(
readonly blockLen: number,
public outputLen: number,
blockLen: number,
outputLen: number,
opts: BlakeOpts | undefined = {},
keyLen: number,
saltLen: number,
Expand All @@ -70,6 +72,8 @@ export abstract class BLAKE<T extends BLAKE<T>> extends Hash<T> {
throw new Error('salt must be undefined or ' + saltLen);
if (opts.personalization !== undefined && opts.personalization.length !== persLen)
throw new Error('personalization must be undefined or ' + persLen);
this.blockLen = blockLen;
this.outputLen = outputLen;
this.buffer = new Uint8Array(blockLen);
this.buffer32 = u32(this.buffer);
}
Expand Down Expand Up @@ -139,6 +143,7 @@ export abstract class BLAKE<T extends BLAKE<T>> extends Hash<T> {
to.length = length;
to.finished = finished;
to.destroyed = destroyed;
// @ts-ignore
to.outputLen = outputLen;
to.buffer.set(buffer);
to.pos = pos;
Expand Down
17 changes: 11 additions & 6 deletions src/_md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export abstract class HashMD<T extends HashMD<T>> extends Hash<T> {
protected abstract set(...args: number[]): void;
abstract destroy(): void;
protected abstract roundClean(): void;

readonly blockLen: number;
readonly outputLen: number;
readonly padOffset: number;
readonly isLE: boolean;

// For partial updates less than block size
protected buffer: Uint8Array;
protected view: DataView;
Expand All @@ -51,13 +57,12 @@ export abstract class HashMD<T extends HashMD<T>> extends Hash<T> {
protected pos = 0;
protected destroyed = false;

constructor(
readonly blockLen: number,
public outputLen: number,
readonly padOffset: number,
readonly isLE: boolean
) {
constructor(blockLen: number, outputLen: number, padOffset: number, isLE: boolean) {
super();
this.blockLen = blockLen;
this.outputLen = outputLen;
this.padOffset = padOffset;
this.isLE = isLE;
this.buffer = new Uint8Array(blockLen);
this.view = createView(this.buffer);
}
Expand Down
29 changes: 18 additions & 11 deletions src/blake1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,27 @@ abstract class Blake1<T extends Blake1<T>> extends Hash<T> {
abstract compress(view: DataView, offset: number, withLength?: boolean): void;
protected abstract get(): number[];
protected abstract set(...args: number[]): void;

readonly blockLen: number;
readonly outputLen: number;
private lengthFlag: number;
private counterLen: number;
protected constants: Uint32Array;

constructor(
readonly blockLen: number,
readonly outputLen: number,
private lengthFlag: number,
private counterLen: number,
blockLen: number,
outputLen: number,
lengthFlag: number,
counterLen: number,
saltLen: number,
protected constants: Uint32Array,
constants: Uint32Array,
opts: BlakeOpts = {}
) {
super();
this.blockLen = blockLen;
this.outputLen = outputLen;
this.lengthFlag = lengthFlag;
this.counterLen = counterLen;
this.buffer = new Uint8Array(blockLen);
this.view = createView(this.buffer);
if (opts.salt) {
Expand All @@ -73,6 +84,7 @@ abstract class Blake1<T extends Blake1<T>> extends Hash<T> {
}
} else {
this.salt = EMPTY_SALT;
this.constants = constants;
}
}
update(data: Input): this {
Expand Down Expand Up @@ -385,12 +397,7 @@ class Blake1_64 extends Blake1<Blake1_64> {
private v6h: number;
private v7l: number;
private v7h: number;
constructor(
public outputLen: number,
IV: Uint32Array,
lengthFlag: number,
opts: BlakeOpts = {}
) {
constructor(outputLen: number, IV: Uint32Array, lengthFlag: number, opts: BlakeOpts = {}) {
super(128, outputLen, lengthFlag, 16, 8, C512, opts);
this.v0l = IV[0] | 0;
this.v0h = IV[1] | 0;
Expand Down
22 changes: 11 additions & 11 deletions src/blake3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ import {
} from './utils.js';

// Flag bitset
const enum B3_Flags {
CHUNK_START = 1 << 0,
CHUNK_END = 1 << 1,
PARENT = 1 << 2,
ROOT = 1 << 3,
KEYED_HASH = 1 << 4,
DERIVE_KEY_CONTEXT = 1 << 5,
DERIVE_KEY_MATERIAL = 1 << 6,
}
const B3_Flags = {
CHUNK_START: 1 << 0,
CHUNK_END: 1 << 1,
PARENT: 1 << 2,
ROOT: 1 << 3,
KEYED_HASH: 1 << 4,
DERIVE_KEY_CONTEXT: 1 << 5,
DERIVE_KEY_MATERIAL: 1 << 6,
} as const;

const SIGMA: Uint8Array = /* @__PURE__ */ (() => {
const Id = Array.from({ length: 16 }, (_, i) => i);
Expand Down Expand Up @@ -72,8 +72,8 @@ export class BLAKE3 extends BLAKE<BLAKE3> implements HashXOF<BLAKE3> {
private enableXOF = true;

constructor(opts: Blake3Opts = {}, flags = 0) {
super(64, opts.dkLen === undefined ? 32 : opts.dkLen, {}, Number.MAX_SAFE_INTEGER, 0, 0);
this.outputLen = opts.dkLen === undefined ? 32 : opts.dkLen;
const olen = opts.dkLen === undefined ? 32 : opts.dkLen;
super(64, olen, {}, Number.MAX_SAFE_INTEGER, 0, 0);
anumber(this.outputLen);
if (opts.key !== undefined && opts.context !== undefined)
throw new Error('Blake3: only key or context can be specified at same time');
Expand Down
7 changes: 3 additions & 4 deletions src/sha256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export class SHA256 extends HashMD<SHA256> {
protected G: number = SHA256_IV[6] | 0;
protected H: number = SHA256_IV[7] | 0;

constructor() {
super(64, 32, 8, false);
constructor(outputLen: number = 32) {
super(64, outputLen, 8, false);
}
protected get(): [number, number, number, number, number, number, number, number] {
const { A, B, C, D, E, F, G, H } = this;
Expand Down Expand Up @@ -125,8 +125,7 @@ class SHA224 extends SHA256 {
protected G = 0x64f98fa7 | 0;
protected H = 0xbefa4fa4 | 0;
constructor() {
super();
this.outputLen = 28;
super(28);
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/sha3-addons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,20 @@ type ParallelOpts = cShakeOpts & { blockLen?: number };

export class ParallelHash extends Keccak implements HashXOF<ParallelHash> {
private leafHash?: Hash<Keccak>;
protected leafCons: () => Hash<Keccak>;
private chunkPos = 0; // Position of current block in chunk
private chunksDone = 0; // How many chunks we already have
private chunkLen: number;
constructor(
blockLen: number,
outputLen: number,
protected leafCons: () => Hash<Keccak>,
leafCons: () => Hash<Keccak>,
enableXOF: boolean,
opts: ParallelOpts = {}
) {
super(blockLen, 0x1f, outputLen, enableXOF);
cshakePers(this, { NISTfn: 'ParallelHash', personalization: opts.personalization });
this.leafCons = leafCons;
let { blockLen: B } = opts;
B ||= 8;
anumber(B);
Expand Down Expand Up @@ -344,17 +346,19 @@ const EMPTY = new Uint8Array([]);
export class KangarooTwelve extends Keccak implements HashXOF<KangarooTwelve> {
readonly chunkLen = 8192;
private leafHash?: Keccak;
protected leafLen: number;
private personalization: Uint8Array;
private chunkPos = 0; // Position of current block in chunk
private chunksDone = 0; // How many chunks we already have
constructor(
blockLen: number,
protected leafLen: number,
leafLen: number,
outputLen: number,
rounds: number,
opts: KangarooOpts
) {
super(blockLen, 0x07, outputLen, true, rounds);
this.leafLen = leafLen;
const { personalization } = opts;
this.personalization = toBytesOptional(personalization);
}
Expand Down
22 changes: 17 additions & 5 deletions src/sha3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,27 @@ export class Keccak extends Hash<Keccak> implements HashXOF<Keccak> {
protected finished = false;
protected state32: Uint32Array;
protected destroyed = false;

public blockLen: number;
public suffix: number;
public outputLen: number;
protected enableXOF = false;
protected rounds: number;

// NOTE: we accept arguments in bytes instead of bits here.
constructor(
public blockLen: number,
public suffix: number,
public outputLen: number,
protected enableXOF = false,
protected rounds: number = 24
blockLen: number,
suffix: number,
outputLen: number,
enableXOF = false,
rounds: number = 24
) {
super();
this.blockLen = blockLen;
this.suffix = suffix;
this.outputLen = outputLen;
this.enableXOF = enableXOF;
this.rounds = rounds;
// Can be passed from user as dkLen
anumber(outputLen);
// 1600 = 5x5 matrix of 64bit. 1600 bits === 200 bytes
Expand Down
13 changes: 5 additions & 8 deletions src/sha512.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export class SHA512 extends HashMD<SHA512> {
protected Hh: number = 0x5be0cd19 | 0;
protected Hl: number = 0x137e2179 | 0;

constructor() {
super(128, 64, 16, false);
constructor(outputLen: number = 64) {
super(128, outputLen, 16, false);
}
// prettier-ignore
protected get(): [
Expand Down Expand Up @@ -192,8 +192,7 @@ export class SHA512_224 extends SHA512 {
protected Hl: number = 0x91d692a1 | 0;

constructor() {
super();
this.outputLen = 28;
super(28);
}
}

Expand All @@ -217,8 +216,7 @@ export class SHA512_256 extends SHA512 {
protected Hl: number = 0x81c52ca2 | 0;

constructor() {
super();
this.outputLen = 32;
super(32);
}
}

Expand All @@ -242,8 +240,7 @@ export class SHA384 extends SHA512 {
protected Hl: number = 0xbefa4fa4 | 0;

constructor() {
super();
this.outputLen = 48;
super(48);
}
}

Expand Down

0 comments on commit da370b6

Please sign in to comment.