Skip to content

Commit

Permalink
refactor: deduplicate base digest and digest64
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewkeil committed Jan 6, 2025
1 parent 92a9cd0 commit 58a8a6b
Showing 1 changed file with 13 additions and 76 deletions.
89 changes: 13 additions & 76 deletions packages/as-sha256/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import SHA256 from "./sha256.js";
export {HashObject, byteArrayToHashObject, hashObjectToByteArray, byteArrayIntoHashObject, SHA256};

class BaseAssemblyScriptSha256Hasher {
private ctx: WasmSimdContext | WasmContext;
private wasmInputValue!: number;
private wasmOutputValue!: number;
private inputUint8Array!: Uint8Array;
private outputUint8Array32!: Uint8Array;

constructor() {
this.ctx = newInstance(false);
protected ctx: WasmSimdContext | WasmContext;
protected wasmInputValue!: number;
protected wasmOutputValue!: number;
protected inputUint8Array!: Uint8Array;
protected outputUint8Array32!: Uint8Array;

constructor(useSimd: boolean) {
this.ctx = newInstance(useSimd);
this.wasmInputValue = this.ctx.input.value;
this.wasmOutputValue = this.ctx.output.value;
this.inputUint8Array = new Uint8Array(this.ctx.memory.buffer, this.wasmInputValue, this.ctx.INPUT_LENGTH);
Expand Down Expand Up @@ -82,23 +82,19 @@ class BaseAssemblyScriptSha256Hasher {
}
}

const baseHasher = new BaseAssemblyScriptSha256Hasher();
const baseHasher = new BaseAssemblyScriptSha256Hasher(false);

const digest = baseHasher.digest.bind(baseHasher);
const digest64 = baseHasher.digest64.bind(baseHasher);

export {digest, digest64};

export class AssemblyScriptSha256Hasher {
private ctx!: WasmSimdContext | WasmContext;
export class AssemblyScriptSha256Hasher extends BaseAssemblyScriptSha256Hasher {
private hasSimd!: boolean;
private wasmInputValue!: number;
private wasmOutputValue!: number;
private inputUint8Array!: Uint8Array;
private outputUint8Array!: Uint8Array;
private outputUint8Array32!: Uint8Array;
private inputUint32Array!: Uint32Array;
private constructor(useSimd: boolean) {
constructor(useSimd: boolean) {
super(useSimd);
this.initializeInstance(useSimd);
}

Expand All @@ -108,6 +104,7 @@ export class AssemblyScriptSha256Hasher {
}

reinitializeInstance(useSimd: boolean): boolean {
this.ctx = newInstance(useSimd);
this.initializeInstance(useSimd);
return this.simdEnabled();
}
Expand All @@ -116,37 +113,6 @@ export class AssemblyScriptSha256Hasher {
return Boolean(this.ctx.HAS_SIMD.valueOf());
}

/**
* Sha256 for data of any length
*/
digest(data: Uint8Array): Uint8Array {
if (data.length === 64) {
return this.digest64(data);
}

if (data.length <= this.ctx.INPUT_LENGTH) {
this.inputUint8Array.set(data);
this.ctx.digest(data.length);
return this.allocDigest();
}

this.ctx.init();
this.update(data);
return this.final();
}

/**
* Sha256 for 64byte ArrayBuffer
*/
digest64(data: Uint8Array): Uint8Array {
if (data.length === 64) {
this.inputUint8Array.set(data);
this.ctx.digest64(this.wasmInputValue, this.wasmOutputValue);
return this.allocDigest();
}
throw new Error("InvalidLengthForDigest64");
}

digest2Bytes32(bytes1: Uint8Array, bytes2: Uint8Array): Uint8Array {
if (bytes1.length !== 32 || bytes2.length !== 32) {
throw new Error("InvalidLengthForDigest64");
Expand Down Expand Up @@ -412,7 +378,6 @@ export class AssemblyScriptSha256Hasher {
}

private initializeInstance(useSimd: boolean): void {
this.ctx = newInstance(useSimd);
this.hasSimd = this.simdEnabled();
this.wasmInputValue = this.ctx.input.value;
this.wasmOutputValue = this.ctx.output.value;
Expand All @@ -423,34 +388,6 @@ export class AssemblyScriptSha256Hasher {
this.inputUint32Array = new Uint32Array(this.ctx.memory.buffer, this.wasmInputValue, this.ctx.INPUT_LENGTH);
}

private update(data: Uint8Array): void {
const INPUT_LENGTH = this.ctx.INPUT_LENGTH;
if (data.length > INPUT_LENGTH) {
for (let i = 0; i < data.length; i += INPUT_LENGTH) {
const sliced = data.subarray(i, i + INPUT_LENGTH);
this.inputUint8Array.set(sliced);
this.ctx.update(this.wasmInputValue, sliced.length);
}
} else {
this.inputUint8Array.set(data);
this.ctx.update(this.wasmInputValue, data.length);
}
}

private final(): Uint8Array {
this.ctx.final(this.wasmOutputValue);
return this.allocDigest();
}

/**
* allocate memory and copy result
*/
private allocDigest(): Uint8Array {
const out = allocUnsafe(32);
out.set(this.outputUint8Array32);
return out;
}

/**
* allocate memory and copy result at offset
*/
Expand Down

0 comments on commit 58a8a6b

Please sign in to comment.