Skip to content

Commit 8c145e5

Browse files
committed
sha3-addons: add PURE annotations to reduce bundle size
1 parent 7c33eae commit 8c145e5

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

src/sha3-addons.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ const gencShake = (suffix: number, blockLen: number, outputLen: number) =>
5050
cshakePers(new Keccak(blockLen, suffix, chooseLen(opts, outputLen), true), opts)
5151
);
5252

53-
export const cshake128 = gencShake(0x1f, 168, 128 / 8);
54-
export const cshake256 = gencShake(0x1f, 136, 256 / 8);
53+
export const cshake128 = /* @__PURE__ */ (() => gencShake(0x1f, 168, 128 / 8))();
54+
export const cshake256 = /* @__PURE__ */ (() => gencShake(0x1f, 136, 256 / 8))();
5555

5656
class KMAC extends Keccak implements HashXOF<KMAC> {
5757
constructor(
@@ -99,10 +99,10 @@ function genKmac(blockLen: number, outputLen: number, xof = false) {
9999
return kmac;
100100
}
101101

102-
export const kmac128 = genKmac(168, 128 / 8);
103-
export const kmac256 = genKmac(136, 256 / 8);
104-
export const kmac128xof = genKmac(168, 128 / 8, true);
105-
export const kmac256xof = genKmac(136, 256 / 8, true);
102+
export const kmac128 = /* @__PURE__ */ (() => genKmac(168, 128 / 8))();
103+
export const kmac256 = /* @__PURE__ */ (() => genKmac(136, 256 / 8))();
104+
export const kmac128xof = /* @__PURE__ */ (() => genKmac(168, 128 / 8, true))();
105+
export const kmac256xof = /* @__PURE__ */ (() => genKmac(136, 256 / 8, true))();
106106

107107
// TupleHash
108108
// Usage: tuple(['ab', 'cd']) != tuple(['a', 'bcd'])
@@ -142,10 +142,10 @@ function genTuple(blockLen: number, outputLen: number, xof = false) {
142142
return tuple;
143143
}
144144

145-
export const tuplehash128 = genTuple(168, 128 / 8);
146-
export const tuplehash256 = genTuple(136, 256 / 8);
147-
export const tuplehash128xof = genTuple(168, 128 / 8, true);
148-
export const tuplehash256xof = genTuple(136, 256 / 8, true);
145+
export const tuplehash128 = /* @__PURE__ */ (() => genTuple(168, 128 / 8))();
146+
export const tuplehash256 = /* @__PURE__ */ (() => genTuple(136, 256 / 8))();
147+
export const tuplehash128xof = /* @__PURE__ */ (() => genTuple(168, 128 / 8, true))();
148+
export const tuplehash256xof = /* @__PURE__ */ (() => genTuple(136, 256 / 8, true))();
149149

150150
// ParallelHash (same as K12/M14, but without speedup for inputs less 8kb, reduced number of rounds and more simple)
151151
type ParallelOpts = cShakeOpts & { blockLen?: number };
@@ -217,7 +217,7 @@ class ParallelHash extends Keccak implements HashXOF<ParallelHash> {
217217
}
218218
}
219219

220-
function genParallel(
220+
function genPrl(
221221
blockLen: number,
222222
outputLen: number,
223223
leaf: ReturnType<typeof gencShake>,
@@ -236,10 +236,10 @@ function genParallel(
236236
return parallel;
237237
}
238238

239-
export const parallelhash128 = genParallel(168, 128 / 8, cshake128);
240-
export const parallelhash256 = genParallel(136, 256 / 8, cshake256);
241-
export const parallelhash128xof = genParallel(168, 128 / 8, cshake128, true);
242-
export const parallelhash256xof = genParallel(136, 256 / 8, cshake256, true);
239+
export const parallelhash128 = /* @__PURE__ */ (() => genPrl(168, 128 / 8, cshake128))();
240+
export const parallelhash256 = /* @__PURE__ */ (() => genPrl(136, 256 / 8, cshake256))();
241+
export const parallelhash128xof = /* @__PURE__ */ (() => genPrl(168, 128 / 8, cshake128, true))();
242+
export const parallelhash256xof = /* @__PURE__ */ (() => genPrl(136, 256 / 8, cshake256, true))();
243243

244244
// Kangaroo
245245
// Same as NIST rightEncode, but returns [0] for zero string
@@ -327,13 +327,15 @@ class KangarooTwelve extends Keccak implements HashXOF<KangarooTwelve> {
327327
}
328328
}
329329
// Default to 32 bytes, so it can be used without opts
330-
export const k12 = wrapConstructorWithOpts<KangarooTwelve, KangarooOpts>(
331-
(opts: KangarooOpts = {}) => new KangarooTwelve(168, 32, chooseLen(opts, 32), 12, opts)
332-
);
330+
export const k12 = /* @__PURE__ */ (() =>
331+
wrapConstructorWithOpts<KangarooTwelve, KangarooOpts>(
332+
(opts: KangarooOpts = {}) => new KangarooTwelve(168, 32, chooseLen(opts, 32), 12, opts)
333+
))();
333334
// MarsupilamiFourteen
334-
export const m14 = wrapConstructorWithOpts<KangarooTwelve, KangarooOpts>(
335-
(opts: KangarooOpts = {}) => new KangarooTwelve(136, 64, chooseLen(opts, 64), 14, opts)
336-
);
335+
export const m14 = /* @__PURE__ */ (() =>
336+
wrapConstructorWithOpts<KangarooTwelve, KangarooOpts>(
337+
(opts: KangarooOpts = {}) => new KangarooTwelve(136, 64, chooseLen(opts, 64), 14, opts)
338+
))();
337339

338340
// https://keccak.team/files/CSF-0.1.pdf
339341
// + https://github.com/XKCP/XKCP/tree/master/lib/high/Keccak/PRG

0 commit comments

Comments
 (0)