]> zoso.dev Git - nano-pow.git/commitdiff
Return actual hash result from compute shader in addition to nonce value in order...
authorChris Duncan <chris@zoso.dev>
Thu, 13 Mar 2025 15:23:17 +0000 (08:23 -0700)
committerChris Duncan <chris@zoso.dev>
Thu, 13 Mar 2025 15:23:17 +0000 (08:23 -0700)
src/classes/gpu.ts
src/shaders/compute.wgsl

index e91d1823ae78fd0025fef61e9ab18adc242b096f..9c33c150bd77ce681c73bcfef37c58a26de4f3b2 100644 (file)
@@ -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})`)
index e93382b9f2d9ed993c804b5a056bb219c81e12eb..32d14b9d4120dafff333951ccb00e7eef88c9aab 100644 (file)
@@ -16,7 +16,8 @@ struct UBO {
 */
 struct WORK {
        nonce: vec2<u32>,
-       found: atomic<u32>
+       found: atomic<u32>,
+       result: vec2<u32>
 };
 @group(0) @binding(1) var<storage, read_write> work: WORK;
 
@@ -1664,6 +1665,7 @@ fn main(id: vec3<u32>) {
        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;
 }