From: Chris Duncan Date: Thu, 13 Mar 2025 15:23:17 +0000 (-0700) Subject: Return actual hash result from compute shader in addition to nonce value in order... X-Git-Tag: v3.1.0~8 X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=ba45ec1a2845ff269ff2ddae9a008c80955326a7;p=nano-pow.git Return actual hash result from compute shader in addition to nonce value in order to assist with debugging. --- diff --git a/src/classes/gpu.ts b/src/classes/gpu.ts index e91d182..9c33c15 100644 --- a/src/classes/gpu.ts +++ b/src/classes/gpu.ts @@ -47,11 +47,11 @@ export class NanoPowGpu { if (this.#device == null) throw new Error(`WebGPU device failed to load.`) // Create buffers for writing GPU calculations and reading from Javascript this.#gpuBuffer = this.#device.createBuffer({ - size: 16, + size: 32, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC }) this.#cpuBuffer = this.#device.createBuffer({ - size: 16, + size: 32, usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ }) this.#uboBuffer = this.#device.createBuffer({ @@ -188,7 +188,7 @@ export class NanoPowGpu { passEncoder.end() // Copy 8-byte nonce and 4-byte found flag from GPU to CPU for reading - commandEncoder.copyBufferToBuffer(this.#gpuBuffer, 0, this.#cpuBuffer, 0, 12) + commandEncoder.copyBufferToBuffer(this.#gpuBuffer, 0, this.#cpuBuffer, 0, 32) // End computation by passing array of command buffers to command queue for execution this.#device.queue.submit([commandEncoder.finish()]) @@ -258,10 +258,11 @@ export class NanoPowGpu { const random0 = Math.floor(Math.random() * 0xffffffff) const random1 = Math.floor(Math.random() * 0xffffffff) const seed = (BigInt(random0) << 32n) | BigInt(random1) - if (this.#debug) console.log(`seed: ${seed}`) + if (this.#debug) console.log('seed', seed.toString(16).padStart(16, '0')) const data = await this.#dispatch(this.#searchPipeline, seed, hash, threshold, effort) nonce = data.getBigUint64(0, true) this.#busy = !data.getUint32(8) + if (this.#debug) console.log('result', data.getBigUint64(16).toString(16).padStart(16, '0')) times.push(performance.now() - start) } while (this.#busy) if (this.#debug) this.#logAverages(times) @@ -310,10 +311,11 @@ export class NanoPowGpu { } const seed = BigInt(`0x${work}`) - if (this.#debug) console.log(`work: ${work}`) + if (this.#debug) console.log('work', work) const data = await this.#dispatch(this.#validatePipeline, seed, hash, threshold, 1) const nonce = data.getBigUint64(0, true) const found = !!data.getUint32(8) + if (this.#debug) console.log('result', data.getBigUint64(16).toString(16).padStart(16, '0')) this.#busy = false if (this.#debug) console.log('nonce', nonce, nonce.toString(16).padStart(16, '0')) if (found && work !== nonce.toString(16).padStart(16, '0')) throw new Error(`Nonce (${nonce.toString(16).padStart(16, '0')}) found but does not match work (${work})`) diff --git a/src/shaders/compute.wgsl b/src/shaders/compute.wgsl index e93382b..32d14b9 100644 --- a/src/shaders/compute.wgsl +++ b/src/shaders/compute.wgsl @@ -16,7 +16,8 @@ struct UBO { */ struct WORK { nonce: vec2, - found: atomic + found: atomic, + result: vec2 }; @group(0) @binding(1) var work: WORK; @@ -1664,6 +1665,7 @@ fn main(id: vec3) { if ((BLAKE2B_IV[0u].y ^ v01.y ^ v89.y) >= ubo.threshold && atomicLoad(&work.found) == 0u) { atomicStore(&work.found, 1u); work.nonce = m0; + work.result = (BLAKE2B_INIT[0u] ^ v01.xy ^ v89.xy); } return; }