From: Chris Duncan Date: Sat, 14 Dec 2024 10:02:38 +0000 (-0800) Subject: Second attempt at parsing block hash from hex to uint LE. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=e720e0b94adc9cfe39e859223486c108de817d4a;p=libnemo.git Second attempt at parsing block hash from hex to uint LE. --- diff --git a/src/lib/workers/pow.ts b/src/lib/workers/pow.ts index 2166806..5378f87 100644 --- a/src/lib/workers/pow.ts +++ b/src/lib/workers/pow.ts @@ -60,6 +60,23 @@ export class Pow { } return out } + + static processHash (hashHex: string): Uint32Array { + if (!/^[A-F-a-f0-9]{64}$/.test(hashHex)) { + throw new Error(`invalid_hash ${hashHex}`) + } + const view = new DataView(new ArrayBuffer(32)) + for (let i = 0; i < view.byteLength; i += 2) { + const byte = hashHex.slice(i, i + 2) + view.setUint8(i / 2, parseInt(byte, 16)) + } + let hashBytesLE: number[] = [] + for (let i = 0; i < view.byteLength; i += 4) { + hashBytesLE.push(view.getUint32(i, true)) + } + return new Uint32Array(hashBytesLE) + } + /* for (i = 0; i<12; i++) { B2B_G(0, 8, 16, 24, SIGMA82[i * 16 + 0], SIGMA82[i * 16 + 1]); @@ -77,10 +94,9 @@ export class Pow { static calculate (hashHex: string, threshold: number = this.SEND_THRESHOLD, callback: (nonce: string | PromiseLike) => any): void { if (typeof threshold !== 'number') throw new TypeError(`invalid_threshold ${threshold}`) if (!/^[A-F-a-f0-9]{64}$/.test(hashHex)) throw new Error(`invalid_hash ${hashHex}`) - let reverseHex = '' - for (let i = hashHex.length; i > 0; i -= 2) reverseHex += hashHex.slice(i - 2, i) if (this.gl == null) throw new Error('webgl2_required') this.gl.clearColor(0, 0, 0, 1) + const blockHashComponents: Uint32Array = this.processHash(hashHex) // Vertext Shader const vsSource = `#version 300 es @@ -109,6 +125,8 @@ export class Pow { uniform uvec4 u_work0; // Last 4 bytes remain as generated externally uniform uvec4 u_work1; + // Precalculated block hash components + uniform uint blockHash[8]; // Defined separately from uint v[32] below as the original value is required // to calculate the second uint32 of the digest for threshold comparison @@ -311,6 +329,7 @@ export class Pow { const work0Location = this.gl.getUniformLocation(program, 'u_work0') const work1Location = this.gl.getUniformLocation(program, 'u_work1') + const blockHashLocation = this.gl.getUniformLocation(program, "blockHash") // Draw output until success or progressCallback says to stop let n = 0 @@ -324,6 +343,7 @@ export class Pow { this.gl.uniform4uiv(work0Location, this.work0) this.gl.uniform4uiv(work1Location, this.work1) + this.gl.uniform1uiv(blockHashLocation, blockHashComponents) this.gl.clear(this.gl.COLOR_BUFFER_BIT) this.gl.drawArrays(this.gl.TRIANGLES, 0, 6)