Skip to content

Commit da370b6

Browse files
committedFeb 1, 2025··
Use typescript erasableSyntaxOnly: true
1 parent f3c6095 commit da370b6

File tree

8 files changed

+78
-49
lines changed

8 files changed

+78
-49
lines changed
 

‎src/_blake.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ export abstract class BLAKE<T extends BLAKE<T>> extends Hash<T> {
5050
protected pos: number = 0;
5151
protected finished = false;
5252
protected destroyed = false;
53+
readonly blockLen: number;
54+
readonly outputLen: number;
5355

5456
constructor(
55-
readonly blockLen: number,
56-
public outputLen: number,
57+
blockLen: number,
58+
outputLen: number,
5759
opts: BlakeOpts | undefined = {},
5860
keyLen: number,
5961
saltLen: number,
@@ -70,6 +72,8 @@ export abstract class BLAKE<T extends BLAKE<T>> extends Hash<T> {
7072
throw new Error('salt must be undefined or ' + saltLen);
7173
if (opts.personalization !== undefined && opts.personalization.length !== persLen)
7274
throw new Error('personalization must be undefined or ' + persLen);
75+
this.blockLen = blockLen;
76+
this.outputLen = outputLen;
7377
this.buffer = new Uint8Array(blockLen);
7478
this.buffer32 = u32(this.buffer);
7579
}
@@ -139,6 +143,7 @@ export abstract class BLAKE<T extends BLAKE<T>> extends Hash<T> {
139143
to.length = length;
140144
to.finished = finished;
141145
to.destroyed = destroyed;
146+
// @ts-ignore
142147
to.outputLen = outputLen;
143148
to.buffer.set(buffer);
144149
to.pos = pos;

‎src/_md.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ export abstract class HashMD<T extends HashMD<T>> extends Hash<T> {
4343
protected abstract set(...args: number[]): void;
4444
abstract destroy(): void;
4545
protected abstract roundClean(): void;
46+
47+
readonly blockLen: number;
48+
readonly outputLen: number;
49+
readonly padOffset: number;
50+
readonly isLE: boolean;
51+
4652
// For partial updates less than block size
4753
protected buffer: Uint8Array;
4854
protected view: DataView;
@@ -51,13 +57,12 @@ export abstract class HashMD<T extends HashMD<T>> extends Hash<T> {
5157
protected pos = 0;
5258
protected destroyed = false;
5359

54-
constructor(
55-
readonly blockLen: number,
56-
public outputLen: number,
57-
readonly padOffset: number,
58-
readonly isLE: boolean
59-
) {
60+
constructor(blockLen: number, outputLen: number, padOffset: number, isLE: boolean) {
6061
super();
62+
this.blockLen = blockLen;
63+
this.outputLen = outputLen;
64+
this.padOffset = padOffset;
65+
this.isLE = isLE;
6166
this.buffer = new Uint8Array(blockLen);
6267
this.view = createView(this.buffer);
6368
}

‎src/blake1.ts

+18-11
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,27 @@ abstract class Blake1<T extends Blake1<T>> extends Hash<T> {
4949
abstract compress(view: DataView, offset: number, withLength?: boolean): void;
5050
protected abstract get(): number[];
5151
protected abstract set(...args: number[]): void;
52+
53+
readonly blockLen: number;
54+
readonly outputLen: number;
55+
private lengthFlag: number;
56+
private counterLen: number;
57+
protected constants: Uint32Array;
58+
5259
constructor(
53-
readonly blockLen: number,
54-
readonly outputLen: number,
55-
private lengthFlag: number,
56-
private counterLen: number,
60+
blockLen: number,
61+
outputLen: number,
62+
lengthFlag: number,
63+
counterLen: number,
5764
saltLen: number,
58-
protected constants: Uint32Array,
65+
constants: Uint32Array,
5966
opts: BlakeOpts = {}
6067
) {
6168
super();
69+
this.blockLen = blockLen;
70+
this.outputLen = outputLen;
71+
this.lengthFlag = lengthFlag;
72+
this.counterLen = counterLen;
6273
this.buffer = new Uint8Array(blockLen);
6374
this.view = createView(this.buffer);
6475
if (opts.salt) {
@@ -73,6 +84,7 @@ abstract class Blake1<T extends Blake1<T>> extends Hash<T> {
7384
}
7485
} else {
7586
this.salt = EMPTY_SALT;
87+
this.constants = constants;
7688
}
7789
}
7890
update(data: Input): this {
@@ -385,12 +397,7 @@ class Blake1_64 extends Blake1<Blake1_64> {
385397
private v6h: number;
386398
private v7l: number;
387399
private v7h: number;
388-
constructor(
389-
public outputLen: number,
390-
IV: Uint32Array,
391-
lengthFlag: number,
392-
opts: BlakeOpts = {}
393-
) {
400+
constructor(outputLen: number, IV: Uint32Array, lengthFlag: number, opts: BlakeOpts = {}) {
394401
super(128, outputLen, lengthFlag, 16, 8, C512, opts);
395402
this.v0l = IV[0] | 0;
396403
this.v0h = IV[1] | 0;

‎src/blake3.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ import {
2828
} from './utils.js';
2929

3030
// Flag bitset
31-
const enum B3_Flags {
32-
CHUNK_START = 1 << 0,
33-
CHUNK_END = 1 << 1,
34-
PARENT = 1 << 2,
35-
ROOT = 1 << 3,
36-
KEYED_HASH = 1 << 4,
37-
DERIVE_KEY_CONTEXT = 1 << 5,
38-
DERIVE_KEY_MATERIAL = 1 << 6,
39-
}
31+
const B3_Flags = {
32+
CHUNK_START: 1 << 0,
33+
CHUNK_END: 1 << 1,
34+
PARENT: 1 << 2,
35+
ROOT: 1 << 3,
36+
KEYED_HASH: 1 << 4,
37+
DERIVE_KEY_CONTEXT: 1 << 5,
38+
DERIVE_KEY_MATERIAL: 1 << 6,
39+
} as const;
4040

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

7474
constructor(opts: Blake3Opts = {}, flags = 0) {
75-
super(64, opts.dkLen === undefined ? 32 : opts.dkLen, {}, Number.MAX_SAFE_INTEGER, 0, 0);
76-
this.outputLen = opts.dkLen === undefined ? 32 : opts.dkLen;
75+
const olen = opts.dkLen === undefined ? 32 : opts.dkLen;
76+
super(64, olen, {}, Number.MAX_SAFE_INTEGER, 0, 0);
7777
anumber(this.outputLen);
7878
if (opts.key !== undefined && opts.context !== undefined)
7979
throw new Error('Blake3: only key or context can be specified at same time');

‎src/sha256.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ export class SHA256 extends HashMD<SHA256> {
4646
protected G: number = SHA256_IV[6] | 0;
4747
protected H: number = SHA256_IV[7] | 0;
4848

49-
constructor() {
50-
super(64, 32, 8, false);
49+
constructor(outputLen: number = 32) {
50+
super(64, outputLen, 8, false);
5151
}
5252
protected get(): [number, number, number, number, number, number, number, number] {
5353
const { A, B, C, D, E, F, G, H } = this;
@@ -125,8 +125,7 @@ class SHA224 extends SHA256 {
125125
protected G = 0x64f98fa7 | 0;
126126
protected H = 0xbefa4fa4 | 0;
127127
constructor() {
128-
super();
129-
this.outputLen = 28;
128+
super(28);
130129
}
131130
}
132131

‎src/sha3-addons.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,20 @@ type ParallelOpts = cShakeOpts & { blockLen?: number };
214214

215215
export class ParallelHash extends Keccak implements HashXOF<ParallelHash> {
216216
private leafHash?: Hash<Keccak>;
217+
protected leafCons: () => Hash<Keccak>;
217218
private chunkPos = 0; // Position of current block in chunk
218219
private chunksDone = 0; // How many chunks we already have
219220
private chunkLen: number;
220221
constructor(
221222
blockLen: number,
222223
outputLen: number,
223-
protected leafCons: () => Hash<Keccak>,
224+
leafCons: () => Hash<Keccak>,
224225
enableXOF: boolean,
225226
opts: ParallelOpts = {}
226227
) {
227228
super(blockLen, 0x1f, outputLen, enableXOF);
228229
cshakePers(this, { NISTfn: 'ParallelHash', personalization: opts.personalization });
230+
this.leafCons = leafCons;
229231
let { blockLen: B } = opts;
230232
B ||= 8;
231233
anumber(B);
@@ -344,17 +346,19 @@ const EMPTY = new Uint8Array([]);
344346
export class KangarooTwelve extends Keccak implements HashXOF<KangarooTwelve> {
345347
readonly chunkLen = 8192;
346348
private leafHash?: Keccak;
349+
protected leafLen: number;
347350
private personalization: Uint8Array;
348351
private chunkPos = 0; // Position of current block in chunk
349352
private chunksDone = 0; // How many chunks we already have
350353
constructor(
351354
blockLen: number,
352-
protected leafLen: number,
355+
leafLen: number,
353356
outputLen: number,
354357
rounds: number,
355358
opts: KangarooOpts
356359
) {
357360
super(blockLen, 0x07, outputLen, true, rounds);
361+
this.leafLen = leafLen;
358362
const { personalization } = opts;
359363
this.personalization = toBytesOptional(personalization);
360364
}

‎src/sha3.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,27 @@ export class Keccak extends Hash<Keccak> implements HashXOF<Keccak> {
107107
protected finished = false;
108108
protected state32: Uint32Array;
109109
protected destroyed = false;
110+
111+
public blockLen: number;
112+
public suffix: number;
113+
public outputLen: number;
114+
protected enableXOF = false;
115+
protected rounds: number;
116+
110117
// NOTE: we accept arguments in bytes instead of bits here.
111118
constructor(
112-
public blockLen: number,
113-
public suffix: number,
114-
public outputLen: number,
115-
protected enableXOF = false,
116-
protected rounds: number = 24
119+
blockLen: number,
120+
suffix: number,
121+
outputLen: number,
122+
enableXOF = false,
123+
rounds: number = 24
117124
) {
118125
super();
126+
this.blockLen = blockLen;
127+
this.suffix = suffix;
128+
this.outputLen = outputLen;
129+
this.enableXOF = enableXOF;
130+
this.rounds = rounds;
119131
// Can be passed from user as dkLen
120132
anumber(outputLen);
121133
// 1600 = 5x5 matrix of 64bit. 1600 bits === 200 bytes

‎src/sha512.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ export class SHA512 extends HashMD<SHA512> {
5959
protected Hh: number = 0x5be0cd19 | 0;
6060
protected Hl: number = 0x137e2179 | 0;
6161

62-
constructor() {
63-
super(128, 64, 16, false);
62+
constructor(outputLen: number = 64) {
63+
super(128, outputLen, 16, false);
6464
}
6565
// prettier-ignore
6666
protected get(): [
@@ -192,8 +192,7 @@ export class SHA512_224 extends SHA512 {
192192
protected Hl: number = 0x91d692a1 | 0;
193193

194194
constructor() {
195-
super();
196-
this.outputLen = 28;
195+
super(28);
197196
}
198197
}
199198

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

219218
constructor() {
220-
super();
221-
this.outputLen = 32;
219+
super(32);
222220
}
223221
}
224222

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

244242
constructor() {
245-
super();
246-
this.outputLen = 48;
243+
super(48);
247244
}
248245
}
249246

0 commit comments

Comments
 (0)
Please sign in to comment.