]> zoso.dev Git - libnemo.git/commitdiff
Fix hash processing loop ending early and rely on WebGL to use appropriate endianness...
authorChris Duncan <chris@zoso.dev>
Sun, 15 Dec 2024 03:29:49 +0000 (19:29 -0800)
committerChris Duncan <chris@zoso.dev>
Sun, 15 Dec 2024 03:29:49 +0000 (19:29 -0800)
src/lib/workers/pow.ts

index 5378f8798c18a507b60776e2e96d5e2c3787e9cd..fe91f37c8e4919d145c3754ae5b8750de3640c0f 100644 (file)
@@ -66,15 +66,11 @@ export class Pow {
                        throw new Error(`invalid_hash ${hashHex}`)
                }
                const view = new DataView(new ArrayBuffer(32))
-               for (let i = 0; i < view.byteLength; i += 2) {
+               for (let i = 0; i < 64; 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)
+               return new Uint32Array(view.buffer)
        }
 
        /*
@@ -96,7 +92,7 @@ export class Pow {
                if (!/^[A-F-a-f0-9]{64}$/.test(hashHex)) throw new Error(`invalid_hash ${hashHex}`)
                if (this.gl == null) throw new Error('webgl2_required')
                this.gl.clearColor(0, 0, 0, 1)
-               const blockHashComponents: Uint32Array = this.processHash(hashHex)
+               const hashBytes: Uint32Array = this.processHash(hashHex)
 
                // Vertext Shader
                const vsSource = `#version 300 es
@@ -127,6 +123,8 @@ export class Pow {
                uniform uvec4 u_work1;
                // Precalculated block hash components
                uniform uint blockHash[8];
+               // Threshold is 0xfffffff8 for send/change blocks and 0xfffffe for all else
+               uniform uint threshold;
 
                // Defined separately from uint v[32] below as the original value is required
                // to calculate the second uint32 of the digest for threshold comparison
@@ -238,14 +236,14 @@ export class Pow {
                        m[1] = (u_work1.r ^ (u_work1.g << 8) ^ (u_work1.b << 16) ^ (u_work1.a << 24));
 
                        // Block hash
-                       m[2] = 0x${reverseHex.slice(56, 64)}u;
-                       m[3] = 0x${reverseHex.slice(48, 56)}u;
-                       m[4] = 0x${reverseHex.slice(40, 48)}u;
-                       m[5] = 0x${reverseHex.slice(32, 40)}u;
-                       m[6] = 0x${reverseHex.slice(24, 32)}u;
-                       m[7] = 0x${reverseHex.slice(16, 24)}u;
-                       m[8] = 0x${reverseHex.slice(8, 16)}u;
-                       m[9] = 0x${reverseHex.slice(0, 8)}u;
+                       m[2] = blockHash[0];
+                       m[3] = blockHash[1];
+                       m[4] = blockHash[2];
+                       m[5] = blockHash[3];
+                       m[6] = blockHash[4];
+                       m[7] = blockHash[5];
+                       m[8] = blockHash[6];
+                       m[9] = blockHash[7];
 
                        // twelve rounds of mixing
                        for(i=0;i<12;i++) {
@@ -261,7 +259,7 @@ export class Pow {
 
                        // Threshold test, first 4 bytes not significant,
                        //  only calculate digest of the second 4 bytes
-                       if((BLAKE2B_IV32_1 ^ v[1] ^ v[17]) > ${threshold}u) {
+                       if((BLAKE2B_IV32_1 ^ v[1] ^ v[17]) > threshold) {
                                // Success found, return pixel data so work value can be constructed
                                fragColor = vec4(
                                        float(x_index + 1u)/255., // +1 to distinguish from 0 (unsuccessful) pixels
@@ -330,6 +328,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")
+               const thresholdLocation = this.gl.getUniformLocation(program, "threshold")
 
                // Draw output until success or progressCallback says to stop
                let n = 0
@@ -343,7 +342,8 @@ export class Pow {
 
                        this.gl.uniform4uiv(work0Location, this.work0)
                        this.gl.uniform4uiv(work1Location, this.work1)
-                       this.gl.uniform1uiv(blockHashLocation, blockHashComponents)
+                       this.gl.uniform1uiv(blockHashLocation, hashBytes)
+                       this.gl.uniform1ui(thresholdLocation, threshold)
 
                        this.gl.clear(this.gl.COLOR_BUFFER_BIT)
                        this.gl.drawArrays(this.gl.TRIANGLES, 0, 6)