From b385c33875c09a27ad5498237f54aafa24b74d37 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sun, 15 Dec 2024 00:04:09 -0800 Subject: [PATCH] Pass SIGMA82 into shader as uniform. --- src/lib/workers/pow.ts | 50 +++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/src/lib/workers/pow.ts b/src/lib/workers/pow.ts index a8752ae..c117341 100644 --- a/src/lib/workers/pow.ts +++ b/src/lib/workers/pow.ts @@ -47,13 +47,21 @@ out vec4 fragColor; uniform uvec4 u_work0; // Last 4 bytes remain as generated externally 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; + // Defines canvas size uniform float workload; +// Offsets into the input data buffer for each mixing step. +// They are multiplied by 2 from the original SIGMA values in +// the C reference implementation, which refered to uint64s. +uniform int SIGMA82[192]; + // Defined separately from uint v[32] below as the original value is required // to calculate the second uint32 of the digest for threshold comparison #define BLAKE2B_IV32_1 0x6A09E667u @@ -83,20 +91,6 @@ uint v[32] = uint[32]( // Input data buffer uint m[32]; -// These are offsets into the input data buffer for each mixing step. -// They are multiplied by 2 from the original SIGMA values in -// the C reference implementation, which refered to uint64s. -const int SIGMA82[192] = int[192]( - 0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,28,20,8,16,18,30,26,12,2,24, - 0,4,22,14,10,6,22,16,24,0,10,4,30,26,20,28,6,12,14,2,18,8,14,18,6,2,26, - 24,22,28,4,12,10,20,8,0,30,16,18,0,10,14,4,8,20,30,28,2,22,24,12,16,6, - 26,4,24,12,20,0,22,16,6,8,26,14,10,30,28,2,18,24,10,2,30,28,26,8,20,0, - 14,12,6,18,4,16,22,26,22,14,28,24,2,6,18,10,0,30,8,16,12,4,20,12,30,28, - 18,22,6,0,16,24,4,26,14,2,8,20,10,20,4,16,8,14,12,2,10,30,22,18,28,6,24, - 26,0,0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,28,20,8,16,18,30,26,12, - 2,24,0,4,22,14,10,6 -); - // 64-bit unsigned addition within the compression buffer // Sets v[a,a+1] += b // b0 is the low 32 bits of b, b1 represents the high 32 bits @@ -202,15 +196,24 @@ void main() { static #WORKLOAD: number = 256 * Math.max(1, Math.floor(navigator.hardwareConcurrency / 2)) static #work0 = new Uint8Array(4) static #work1 = new Uint8Array(4) + /** + * Offsets into the input data buffer for each mixing step. They are multiplied + * by 2 from the original SIGMA values in the C reference implementation, which + * refered to uint64s. + */ static #SIGMA82: number[] = [ - 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 28, 20, 8, 16, 18, 30, 26, 12, 2, 24, - 0, 4, 22, 14, 10, 6, 22, 16, 24, 0, 10, 4, 30, 26, 20, 28, 6, 12, 14, 2, 18, 8, 14, 18, 6, 2, 26, - 24, 22, 28, 4, 12, 10, 20, 8, 0, 30, 16, 18, 0, 10, 14, 4, 8, 20, 30, 28, 2, 22, 24, 12, 16, 6, - 26, 4, 24, 12, 20, 0, 22, 16, 6, 8, 26, 14, 10, 30, 28, 2, 18, 24, 10, 2, 30, 28, 26, 8, 20, 0, - 14, 12, 6, 18, 4, 16, 22, 26, 22, 14, 28, 24, 2, 6, 18, 10, 0, 30, 8, 16, 12, 4, 20, 12, 30, 28, - 18, 22, 6, 0, 16, 24, 4, 26, 14, 2, 8, 20, 10, 20, 4, 16, 8, 14, 12, 2, 10, 30, 22, 18, 28, 6, 24, - 26, 0, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 28, 20, 8, 16, 18, 30, 26, 12, - 2, 24, 0, 4, 22, 14, 10, 6 + 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, + 28, 20, 8, 16, 18, 30, 26, 12, 2, 24, 0, 4, 22, 14, 10, 6, + 22, 16, 24, 0, 10, 4, 30, 26, 20, 28, 6, 12, 14, 2, 18, 8, + 14, 18, 6, 2, 26, 24, 22, 28, 4, 12, 10, 20, 8, 0, 30, 16, + 18, 0, 10, 14, 4, 8, 20, 30, 28, 2, 22, 24, 12, 16, 6, 26, + 4, 24, 12, 20, 0, 22, 16, 6, 8, 26, 14, 10, 30, 28, 2, 18, + 24, 10, 2, 30, 28, 26, 8, 20, 0, 14, 12, 6, 18, 4, 16, 22, + 26, 22, 14, 28, 24, 2, 6, 18, 10, 0, 30, 8, 16, 12, 4, 20, + 12, 30, 28, 18, 22, 6, 0, 16, 24, 4, 26, 14, 2, 8, 20, 10, + 20, 4, 16, 8, 14, 12, 2, 10, 30, 22, 18, 28, 6, 24, 26, 0, + 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, + 28, 20, 8, 16, 18, 30, 26, 12, 2, 24, 0, 4, 22, 14, 10, 6 ] static #B2B_G_params: number[] = [] static { @@ -268,6 +271,7 @@ void main() { static #blockHashLocation: WebGLUniformLocation | null static #thresholdLocation: WebGLUniformLocation | null static #workloadLocation: WebGLUniformLocation | null + static #sigma82Location: WebGLUniformLocation | null static #vertexShader: WebGLShader | null static #fragmentShader: WebGLShader | null static #positionBuffer: WebGLBuffer | null @@ -332,6 +336,7 @@ void main() { this.#blockHashLocation = this.#gl.getUniformLocation(this.#program, "blockHash") this.#thresholdLocation = this.#gl.getUniformLocation(this.#program, "threshold") this.#workloadLocation = this.#gl.getUniformLocation(this.#program, "workload") + this.#sigma82Location = this.#gl.getUniformLocation(this.#program, "SIGMA82") } static #calculate (hashHex: string, callback: (nonce: string | PromiseLike) => any, threshold: number): void { @@ -355,6 +360,7 @@ void main() { Pow.#gl.uniform1uiv(Pow.#blockHashLocation, hashBytes) Pow.#gl.uniform1ui(Pow.#thresholdLocation, threshold) Pow.#gl.uniform1f(Pow.#workloadLocation, this.#WORKLOAD - 1) + Pow.#gl.uniform1iv(Pow.#sigma82Location, this.#SIGMA82) Pow.#gl.clear(Pow.#gl.COLOR_BUFFER_BIT) Pow.#gl.drawArrays(Pow.#gl.TRIANGLES, 0, 6) -- 2.34.1