@aidc-toolkit/utility 1.0.26-beta → 1.0.28-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -54,12 +54,12 @@ export abstract class Transformer {
54
54
  /**
55
55
  * Transformers cache, mapping a domain to another map, which maps an optional tweak to a transformer.
56
56
  */
57
- private static readonly TRANSFORMER_MAPS_MAP = new Map<bigint, Map<bigint | undefined, Transformer>>();
57
+ static readonly #TRANSFORMER_MAPS_MAP = new Map<bigint, Map<bigint | undefined, Transformer>>();
58
58
 
59
59
  /**
60
60
  * Domain.
61
61
  */
62
- private readonly _domain: bigint;
62
+ readonly #domain: bigint;
63
63
 
64
64
  /**
65
65
  * Constructor.
@@ -68,9 +68,9 @@ export abstract class Transformer {
68
68
  * Domain.
69
69
  */
70
70
  constructor(domain: number | bigint) {
71
- this._domain = BigInt(domain);
71
+ this.#domain = BigInt(domain);
72
72
 
73
- if (this._domain <= 0n) {
73
+ if (this.#domain <= 0n) {
74
74
  throw new RangeError(i18nextUtility.t("Transformer.domainMustBeGreaterThanZero", {
75
75
  domain
76
76
  }));
@@ -95,11 +95,11 @@ export abstract class Transformer {
95
95
  static get(domain: number | bigint, tweak?: number | bigint): Transformer {
96
96
  const domainN = BigInt(domain);
97
97
 
98
- let transformersMap = Transformer.TRANSFORMER_MAPS_MAP.get(domainN);
98
+ let transformersMap = Transformer.#TRANSFORMER_MAPS_MAP.get(domainN);
99
99
 
100
100
  if (transformersMap === undefined) {
101
101
  transformersMap = new Map();
102
- Transformer.TRANSFORMER_MAPS_MAP.set(domainN, transformersMap);
102
+ Transformer.#TRANSFORMER_MAPS_MAP.set(domainN, transformersMap);
103
103
  }
104
104
 
105
105
  const tweakN = tweak === undefined ? undefined : BigInt(tweak);
@@ -118,7 +118,7 @@ export abstract class Transformer {
118
118
  * Get the domain.
119
119
  */
120
120
  get domain(): bigint {
121
- return this._domain;
121
+ return this.#domain;
122
122
  }
123
123
 
124
124
  /**
@@ -127,7 +127,7 @@ export abstract class Transformer {
127
127
  * @param value
128
128
  * Value.
129
129
  */
130
- private validate(value: bigint): void {
130
+ #validate(value: bigint): void {
131
131
  if (value < 0n) {
132
132
  throw new RangeError(i18nextUtility.t("Transformer.valueMustBeGreaterThanOrEqualToZero", {
133
133
  value
@@ -162,10 +162,10 @@ export abstract class Transformer {
162
162
  * @returns
163
163
  * Transformed value.
164
164
  */
165
- private validateDoForward(value: number | bigint): bigint {
165
+ #validateDoForward(value: number | bigint): bigint {
166
166
  const valueN = BigInt(value);
167
167
 
168
- this.validate(valueN);
168
+ this.#validate(valueN);
169
169
 
170
170
  return this.doForward(valueN);
171
171
  }
@@ -185,8 +185,8 @@ export abstract class Transformer {
185
185
  * @returns
186
186
  * Transformed value.
187
187
  */
188
- private validateDoForwardCallback<TOutput>(transformerCallback: IndexedCallback<bigint, TOutput>, value: number | bigint, index?: number): TOutput {
189
- return transformerCallback(this.validateDoForward(value), index);
188
+ #validateDoForwardCallback<TOutput>(transformerCallback: IndexedCallback<bigint, TOutput>, value: number | bigint, index?: number): TOutput {
189
+ return transformerCallback(this.#validateDoForward(value), index);
190
190
  };
191
191
 
192
192
  /**
@@ -231,7 +231,7 @@ export abstract class Transformer {
231
231
  let result: bigint | TOutput | Iterable<bigint> | Iterable<TOutput>;
232
232
 
233
233
  if (typeof valueOrValues !== "object") {
234
- result = transformerCallback === undefined ? this.validateDoForward(valueOrValues) : this.validateDoForwardCallback(transformerCallback, valueOrValues);
234
+ result = transformerCallback === undefined ? this.#validateDoForward(valueOrValues) : this.#validateDoForwardCallback(transformerCallback, valueOrValues);
235
235
  } else if (valueOrValues instanceof Sequence) {
236
236
  if (valueOrValues.minimumValue < 0n) {
237
237
  throw new RangeError(i18nextUtility.t("Transformer.minimumValueMustBeGreaterThanOrEqualToZero", {
@@ -248,7 +248,7 @@ export abstract class Transformer {
248
248
 
249
249
  result = transformerCallback === undefined ? mapIterable(valueOrValues, value => this.doForward(value)) : mapIterable(valueOrValues, (value, index) => transformerCallback(this.doForward(value), index));
250
250
  } else {
251
- result = transformerCallback === undefined ? mapIterable(valueOrValues, value => this.validateDoForward(value)) : mapIterable(valueOrValues, (value, index) => this.validateDoForwardCallback(transformerCallback, value, index));
251
+ result = transformerCallback === undefined ? mapIterable(valueOrValues, value => this.#validateDoForward(value)) : mapIterable(valueOrValues, (value, index) => this.#validateDoForwardCallback(transformerCallback, value, index));
252
252
  }
253
253
 
254
254
  // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Type determination is handled above.
@@ -278,7 +278,7 @@ export abstract class Transformer {
278
278
  reverse(transformedValue: number | bigint): bigint {
279
279
  const transformedValueN = BigInt(transformedValue);
280
280
 
281
- this.validate(transformedValueN);
281
+ this.#validate(transformedValueN);
282
282
 
283
283
  return this.doReverse(transformedValueN);
284
284
  }
@@ -328,41 +328,41 @@ export class EncryptionTransformer extends Transformer {
328
328
  /**
329
329
  * Individual bits, pre-calculated for performance.
330
330
  */
331
- private static readonly BITS = new Uint8Array([
331
+ static readonly #BITS = new Uint8Array([
332
332
  0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
333
333
  ]);
334
334
 
335
335
  /**
336
336
  * Inverse individual bits, pre-calculated for performance.
337
337
  */
338
- private static readonly INVERSE_BITS = new Uint8Array([
338
+ static readonly #INVERSE_BITS = new Uint8Array([
339
339
  0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F
340
340
  ]);
341
341
 
342
342
  /**
343
343
  * Number of bytes covered by the domain.
344
344
  */
345
- private readonly _domainBytes: number;
345
+ readonly #domainBytes: number;
346
346
 
347
347
  /**
348
348
  * Xor bytes array generated from the domain and tweak.
349
349
  */
350
- private readonly _xorBytes: Uint8Array;
350
+ readonly #xorBytes: Uint8Array;
351
351
 
352
352
  /**
353
353
  * Bits array generated from the domain and tweak.
354
354
  */
355
- private readonly _bits: Uint8Array;
355
+ readonly #bits: Uint8Array;
356
356
 
357
357
  /**
358
358
  * Inverse bits array generated from the domain and tweak.
359
359
  */
360
- private readonly _inverseBits: Uint8Array;
360
+ readonly #inverseBits: Uint8Array;
361
361
 
362
362
  /**
363
363
  * Number of rounds (length of arrays) generated from the domain and tweak.
364
364
  */
365
- private readonly _rounds: number;
365
+ readonly #rounds: number;
366
366
 
367
367
  /**
368
368
  * Constructor.
@@ -389,7 +389,7 @@ export class EncryptionTransformer extends Transformer {
389
389
  domainBytes++;
390
390
  }
391
391
 
392
- this._domainBytes = domainBytes;
392
+ this.#domainBytes = domainBytes;
393
393
 
394
394
  const xorBytes = new Array<number>();
395
395
  const bits = new Array<number>();
@@ -404,29 +404,29 @@ export class EncryptionTransformer extends Transformer {
404
404
  const bitNumber = Number(BigInt.asUintN(3, reducedKey));
405
405
 
406
406
  // Bits are applied in reverse order so that they don't correlate directly with the key bytes at the same index.
407
- bits.push(EncryptionTransformer.BITS[bitNumber]);
408
- inverseBits.push(EncryptionTransformer.INVERSE_BITS[bitNumber]);
407
+ bits.push(EncryptionTransformer.#BITS[bitNumber]);
408
+ inverseBits.push(EncryptionTransformer.#INVERSE_BITS[bitNumber]);
409
409
  }
410
410
 
411
411
  // Domains occupying a single byte will not shuffle and will map all values to themselves for very small domains.
412
412
  if (domainBytes === 1) {
413
413
  // Determine the lowest possible mask that will cover all values in the domain.
414
- const domainMask = EncryptionTransformer.BITS.filter(bit => bit < domain).reduce((accumulator, bit) => accumulator | bit, 0);
414
+ const domainMask = EncryptionTransformer.#BITS.filter(bit => bit < domain).reduce((accumulator, bit) => accumulator | bit, 0);
415
415
 
416
416
  // Reduce all xor bytes to a single byte and strip higher bits.
417
- this._xorBytes = new Uint8Array([xorBytes.reduce((accumulator, xorByte) => accumulator ^ xorByte, 0) & domainMask]);
417
+ this.#xorBytes = new Uint8Array([xorBytes.reduce((accumulator, xorByte) => accumulator ^ xorByte, 0) & domainMask]);
418
418
 
419
419
  // Bits and inverse bits are irrelevant as there will be no shuffling; choose first bit arbitrarily.
420
- this._bits = new Uint8Array([EncryptionTransformer.BITS[0]]);
421
- this._inverseBits = new Uint8Array([EncryptionTransformer.INVERSE_BITS[0]]);
420
+ this.#bits = new Uint8Array([EncryptionTransformer.#BITS[0]]);
421
+ this.#inverseBits = new Uint8Array([EncryptionTransformer.#INVERSE_BITS[0]]);
422
422
 
423
423
  // Everything will be done in one round.
424
- this._rounds = 1;
424
+ this.#rounds = 1;
425
425
  } else {
426
- this._xorBytes = new Uint8Array(xorBytes);
427
- this._bits = new Uint8Array(bits);
428
- this._inverseBits = new Uint8Array(inverseBits);
429
- this._rounds = xorBytes.length;
426
+ this.#xorBytes = new Uint8Array(xorBytes);
427
+ this.#bits = new Uint8Array(bits);
428
+ this.#inverseBits = new Uint8Array(inverseBits);
429
+ this.#rounds = xorBytes.length;
430
430
  }
431
431
  }
432
432
 
@@ -439,11 +439,11 @@ export class EncryptionTransformer extends Transformer {
439
439
  * @returns
440
440
  * Big-endian byte array equivalent to the value.
441
441
  */
442
- private valueToBytes(value: bigint): Uint8Array {
443
- const bytes = new Uint8Array(this._domainBytes);
442
+ #valueToBytes(value: bigint): Uint8Array {
443
+ const bytes = new Uint8Array(this.#domainBytes);
444
444
 
445
445
  // Build byte array in reverse order to get as big-endian.
446
- for (let index = this._domainBytes - 1, reducedValue = value; index >= 0 && reducedValue !== 0n; index--, reducedValue >>= 8n) {
446
+ for (let index = this.#domainBytes - 1, reducedValue = value; index >= 0 && reducedValue !== 0n; index--, reducedValue >>= 8n) {
447
447
  bytes[index] = Number(BigInt.asUintN(8, reducedValue));
448
448
  }
449
449
 
@@ -459,7 +459,7 @@ export class EncryptionTransformer extends Transformer {
459
459
  * @returns
460
460
  * Value.
461
461
  */
462
- private static bytesToValue(bytes: Uint8Array): bigint {
462
+ static #bytesToValue(bytes: Uint8Array): bigint {
463
463
  return bytes.reduce((accumulator, byte) => accumulator << 8n | BigInt(byte), 0n);
464
464
  }
465
465
 
@@ -493,7 +493,7 @@ export class EncryptionTransformer extends Transformer {
493
493
  * @returns
494
494
  * Shuffled byte array.
495
495
  */
496
- private shuffle(bytes: Uint8Array, round: number, forward: boolean): Uint8Array {
496
+ #shuffle(bytes: Uint8Array, round: number, forward: boolean): Uint8Array {
497
497
  const bytesLength = bytes.length;
498
498
 
499
499
  const determinants = new Uint8Array(bytesLength);
@@ -501,7 +501,7 @@ export class EncryptionTransformer extends Transformer {
501
501
  const shuffleIndexes1 = new Array<number>();
502
502
  const shuffleIndexes0 = new Array<number>();
503
503
 
504
- const bit = this._bits[round];
504
+ const bit = this.#bits[round];
505
505
 
506
506
  bytes.forEach((byte, index) => {
507
507
  const determinant = byte & bit;
@@ -512,7 +512,7 @@ export class EncryptionTransformer extends Transformer {
512
512
  (determinant !== 0 ? shuffleIndexes1 : shuffleIndexes0).push(index);
513
513
  });
514
514
 
515
- const inverseBit = this._inverseBits[round];
515
+ const inverseBit = this.#inverseBits[round];
516
516
 
517
517
  const shuffleBytes = new Uint8Array(bytesLength);
518
518
 
@@ -560,8 +560,8 @@ export class EncryptionTransformer extends Transformer {
560
560
  * @returns
561
561
  * Xored byte array.
562
562
  */
563
- private xor(bytes: Uint8Array, round: number, forward: boolean): Uint8Array {
564
- let cumulativeXorByte = this._xorBytes[round];
563
+ #xor(bytes: Uint8Array, round: number, forward: boolean): Uint8Array {
564
+ let cumulativeXorByte = this.#xorBytes[round];
565
565
 
566
566
  return bytes.map((byte) => {
567
567
  const xorByte = byte ^ cumulativeXorByte;
@@ -576,17 +576,17 @@ export class EncryptionTransformer extends Transformer {
576
576
  * @inheritDoc
577
577
  */
578
578
  protected doForward(value: bigint): bigint {
579
- let bytes = this.valueToBytes(value);
579
+ let bytes = this.#valueToBytes(value);
580
580
  let transformedValue: bigint;
581
581
 
582
582
  // Loop repeats until transformed value is within domain.
583
583
  do {
584
584
  // Forward operation is shuffle then xor for the number of rounds.
585
- for (let round = 0; round < this._rounds; round++) {
586
- bytes = this.xor(this.shuffle(bytes, round, true), round, true);
585
+ for (let round = 0; round < this.#rounds; round++) {
586
+ bytes = this.#xor(this.#shuffle(bytes, round, true), round, true);
587
587
  }
588
588
 
589
- transformedValue = EncryptionTransformer.bytesToValue(bytes);
589
+ transformedValue = EncryptionTransformer.#bytesToValue(bytes);
590
590
  } while (transformedValue >= this.domain);
591
591
 
592
592
  return transformedValue;
@@ -596,17 +596,17 @@ export class EncryptionTransformer extends Transformer {
596
596
  * @inheritDoc
597
597
  */
598
598
  protected doReverse(transformedValue: bigint): bigint {
599
- let bytes = this.valueToBytes(transformedValue);
599
+ let bytes = this.#valueToBytes(transformedValue);
600
600
  let value: bigint;
601
601
 
602
602
  // Loop repeats until value is within domain.
603
603
  do {
604
604
  // Reverse operation is xor then shuffle for the number of rounds in reverse.
605
- for (let round = this._rounds - 1; round >= 0; round--) {
606
- bytes = this.shuffle(this.xor(bytes, round, false), round, false);
605
+ for (let round = this.#rounds - 1; round >= 0; round--) {
606
+ bytes = this.#shuffle(this.#xor(bytes, round, false), round, false);
607
607
  }
608
608
 
609
- value = EncryptionTransformer.bytesToValue(bytes);
609
+ value = EncryptionTransformer.#bytesToValue(bytes);
610
610
  } while (value >= this.domain);
611
611
 
612
612
  return value;
@@ -120,11 +120,6 @@ describe("Encryption", () => {
120
120
  });
121
121
 
122
122
  test("Byte boundary", () => {
123
- expect((Transformer.get(256n, 1n) as EncryptionTransformer)["_domainBytes"]).toBe(1);
124
- expect((Transformer.get(257n, 1n) as EncryptionTransformer)["_domainBytes"]).toBe(2);
125
- expect((Transformer.get(65536n, 1n) as EncryptionTransformer)["_domainBytes"]).toBe(2);
126
- expect((Transformer.get(65537n, 1n) as EncryptionTransformer)["_domainBytes"]).toBe(3);
127
-
128
123
  testTransformer(256, 1);
129
124
  testTransformer(257, 1);
130
125
  });