]> zoso.dev Git - libnemo.git/commitdiff
Initial implementation of precalculated block hash components.
authorChris Duncan <chris@zoso.dev>
Fri, 13 Dec 2024 23:53:00 +0000 (15:53 -0800)
committerChris Duncan <chris@zoso.dev>
Fri, 13 Dec 2024 23:53:00 +0000 (15:53 -0800)
src/lib/workers/pow.ts

index 11fc25ac7648bd45fdc32c3b9e4ad690a58e7807..710b8d9c3fbd7c7dd095a07da8f1afb79b13ee38 100644 (file)
@@ -86,6 +86,12 @@ export class Pow {
                if (this.gl == null) throw new Error('webgl2_required')
                this.gl.clearColor(0, 0, 0, 1)
 
+               const blockHashArray: number[] = []
+               for (let i = 0; i < 8; i++) {
+                       blockHashArray.push(parseInt(reverseHex.slice(i * 8, (i + 1) * 8), 16))
+               }
+               const blockHashComponents: Uint32Array = new Uint32Array(blockHashArray)
+
                // Vertext Shader
                const vsSource = `#version 300 es
                precision highp float;
@@ -113,6 +119,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
@@ -169,10 +177,6 @@ export class Pow {
                        v[a] = o0;
                        v[a + 1] = o1;
                }
-               // Sets v[a,a+1] += v[b,b+1]
-               //void add_uint64 (int a, int b) {
-                       //add_uint64(a, v[b], v[b+1]);
-               //}
 
                // G Mixing function
                void B2B_G (int a, int b, int c, int d, int ix, int iy) {
@@ -228,14 +232,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++) {
@@ -319,10 +323,13 @@ 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
                const frameTimes: number[] = []
                const draw = (): void => {
+                       n++
                        if (this.gl == null) throw new Error('webgl2_required')
                        performance.mark('start')
                        crypto.getRandomValues(this.work0)
@@ -330,6 +337,7 @@ export class Pow {
 
                        this.gl.uniform4uiv(work0Location, this.work0)
                        this.gl.uniform4uiv(work1Location, this.work1)
+                       this.gl.uniform1uiv(blockHashLocation, new Uint32Array(blockHashComponents))
 
                        this.gl.clear(this.gl.COLOR_BUFFER_BIT)
                        this.gl.drawArrays(this.gl.TRIANGLES, 0, 6)
@@ -342,6 +350,7 @@ export class Pow {
                                        frameTimes.push(performance.measure('draw', 'start', 'end').duration)
                                        performance.clearMarks()
                                        console.log(`average frame time: ${(frameTimes.reduce((a, b) => a + b)) / frameTimes.length} ms`)
+                                       console.log(`frames calculated: ${n}`)
                                        const hex = this.hexify(this.work1) + this.hexify([
                                                pixels[i + 2],
                                                pixels[i + 3],