cafe-utility 26.0.0 → 26.1.1

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.
Files changed (3) hide show
  1. package/index.d.ts +4 -3
  2. package/index.js +26 -22
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -503,6 +503,7 @@ declare function uint256ToNumber(bytes: Uint8Array, endian: 'LE' | 'BE'): bigint
503
503
  declare function sliceBytes(bytes: Uint8Array, lengths: number[]): Uint8Array[];
504
504
  declare function partition(bytes: Uint8Array, size: number): Uint8Array[];
505
505
  declare function keccak256(bytes: Uint8Array): Uint8Array;
506
+ declare function sha3_256(bytes: Uint8Array): Uint8Array;
506
507
  interface Uint8ArrayIO {
507
508
  max: () => number;
508
509
  }
@@ -523,12 +524,11 @@ declare class Uint8ArrayWriter implements Uint8ArrayIO {
523
524
  declare class Chunk {
524
525
  span: bigint;
525
526
  writer: Uint8ArrayWriter;
526
- hashFn: (a: Uint8Array, b: Uint8Array) => Uint8Array;
527
- constructor(capacity: number, hashFn: (a: Uint8Array, b: Uint8Array) => Uint8Array, span?: bigint);
527
+ constructor(capacity: number, span?: bigint);
528
528
  build(): Uint8Array;
529
529
  hash(): Uint8Array;
530
530
  }
531
- declare function merkleStart(capacity: number, hashFn: (a: Uint8Array, b: Uint8Array) => Uint8Array): Chunk[];
531
+ declare function merkleStart(capacity: number): Chunk[];
532
532
  declare function merkleAppend(levels: Chunk[], data: Uint8Array, onChunk: (chunk: Chunk) => Promise<void>, level?: number): Promise<Chunk[]>;
533
533
  declare function merkleFinalize(levels: Chunk[], onChunk: (chunk: Chunk) => Promise<void>, level?: number): Promise<Chunk>;
534
534
  type Playbook<T> = {
@@ -611,6 +611,7 @@ export declare const Binary: {
611
611
  uint256ToNumber: typeof uint256ToNumber;
612
612
  sliceBytes: typeof sliceBytes;
613
613
  keccak256: typeof keccak256;
614
+ sha3_256: typeof sha3_256;
614
615
  };
615
616
  export declare const Random: {
616
617
  intBetween: typeof intBetween;
package/index.js CHANGED
@@ -2308,22 +2308,22 @@ function bytesToNumbers(n) {
2308
2308
  for (let t = 0; t < n.length; t += 4) e.push(n[t] | (n[t + 1] << 8) | (n[t + 2] << 16) | (n[t + 3] << 24))
2309
2309
  return e
2310
2310
  }
2311
- function divideToBlocks(n) {
2311
+ function divideToBlocks(n, e) {
2312
2312
  if (!n.length) {
2313
- const r = new Uint8Array(136)
2314
- return (r[0] = 1), (r[135] = 128), [bytesToNumbers(r)]
2313
+ const o = new Uint8Array(136)
2314
+ return (o[0] = e), (o[135] = 128), [bytesToNumbers(o)]
2315
2315
  }
2316
- const e = partition(n, 136),
2317
- t = e[e.length - 1]
2318
- if (t.length < 136) {
2319
- const r = new Uint8Array(136)
2320
- r.set(t), (r[t.length] = 1), (r[135] |= 128), (e[e.length - 1] = r)
2316
+ const t = partition(n, 136),
2317
+ r = t[t.length - 1]
2318
+ if (r.length < 136) {
2319
+ const o = new Uint8Array(136)
2320
+ o.set(r), (o[r.length] = e), (o[135] |= 128), (t[t.length - 1] = o)
2321
2321
  }
2322
- if (t.length === 136) {
2323
- const r = new Uint8Array(136)
2324
- ;(r[0] = 1), (r[135] = 128), e.push(r)
2322
+ if (r.length === 136) {
2323
+ const o = new Uint8Array(136)
2324
+ ;(o[0] = e), (o[135] = 128), t.push(o)
2325
2325
  }
2326
- return e.map(bytesToNumbers)
2326
+ return t.map(bytesToNumbers)
2327
2327
  }
2328
2328
  function absorb(n, e) {
2329
2329
  for (const t of e) {
@@ -2369,7 +2369,10 @@ function squeeze(n) {
2369
2369
  ])
2370
2370
  }
2371
2371
  function keccak256(n) {
2372
- return squeeze(absorb(new Array(50).fill(0), divideToBlocks(n)))
2372
+ return squeeze(absorb(new Array(50).fill(0), divideToBlocks(n, 1)))
2373
+ }
2374
+ function sha3_256(n) {
2375
+ return squeeze(absorb(new Array(50).fill(0), divideToBlocks(n, 6)))
2373
2376
  }
2374
2377
  class Uint8ArrayReader {
2375
2378
  constructor(e) {
@@ -2396,25 +2399,25 @@ class Uint8ArrayWriter {
2396
2399
  }
2397
2400
  }
2398
2401
  class Chunk {
2399
- constructor(e, t, r = 0n) {
2400
- ;(this.span = r), (this.writer = new Uint8ArrayWriter(new Uint8Array(e))), (this.hashFn = t)
2402
+ constructor(e, t = 0n) {
2403
+ ;(this.span = t), (this.writer = new Uint8ArrayWriter(new Uint8Array(e)))
2401
2404
  }
2402
2405
  build() {
2403
2406
  return concatBytes(numberToUint64(this.span, 'LE'), this.writer.buffer)
2404
2407
  }
2405
2408
  hash() {
2406
- const e = log2Reduce(partition(this.writer.buffer, 32), this.hashFn)
2407
- return this.hashFn(numberToUint64(this.span, 'LE'), e)
2409
+ const e = log2Reduce(partition(this.writer.buffer, 32), keccak256)
2410
+ return keccak256(concatBytes(numberToUint64(this.span, 'LE'), e))
2408
2411
  }
2409
2412
  }
2410
- function merkleStart(n, e) {
2411
- return [new Chunk(n, e)]
2413
+ function merkleStart(n) {
2414
+ return [new Chunk(n)]
2412
2415
  }
2413
2416
  async function merkleElevate(n, e, t) {
2414
2417
  await e(n[t]),
2415
- n[t + 1] || n.push(new Chunk(n[t].writer.buffer.length, n[t].hashFn, n[t].span)),
2418
+ n[t + 1] || n.push(new Chunk(n[t].writer.buffer.length, n[t].span)),
2416
2419
  merkleAppend(n, n[t].hash(), e, t + 1),
2417
- (n[t] = new Chunk(n[t].writer.buffer.length, n[t].hashFn))
2420
+ (n[t] = new Chunk(n[t].writer.buffer.length))
2418
2421
  }
2419
2422
  async function merkleAppend(n, e, t, r = 0) {
2420
2423
  n[r].writer.max() === 0 && (await merkleElevate(n, t, r))
@@ -2723,7 +2726,8 @@ class AsyncQueue {
2723
2726
  numberToUint256,
2724
2727
  uint256ToNumber,
2725
2728
  sliceBytes,
2726
- keccak256
2729
+ keccak256,
2730
+ sha3_256
2727
2731
  }),
2728
2732
  (exports.Random = {
2729
2733
  intBetween,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "26.0.0",
3
+ "version": "26.1.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {