From: Chris Duncan Date: Tue, 10 Dec 2024 23:13:09 +0000 (-0800) Subject: Add counter to pow WebGL main to loop it until a result is found or counter limit... X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=6c158e845405eaba9e1ce64e4445060befb3e3ca;p=libnemo.git Add counter to pow WebGL main to loop it until a result is found or counter limit hit. It seems like a small canvas drawn many times is more efficient in testing. --- diff --git a/src/lib/block.ts b/src/lib/block.ts index 26c8364..19854aa 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -87,7 +87,7 @@ abstract class Block { ? THRESHOLD_SEND : THRESHOLD_RECEIVE } - const [{ work }] = await Pool.work('converge', 'pow', [data]) + const [{ work }] = await Pool.work('pow', [data]) this.work = work } diff --git a/src/lib/pool.ts b/src/lib/pool.ts index 16c8707..8799417 100644 --- a/src/lib/pool.ts +++ b/src/lib/pool.ts @@ -79,9 +79,7 @@ export class Pool { } } - static async work (approach: 'converge' | 'distribute', name: string, data: object[]): Promise { - if (approach !== 'converge' && approach !== 'distribute') - throw new TypeError('Invalid work approach') + static async work (name: string, data: object[]): Promise { if (!Array.isArray(data)) data = [data] return new Promise((resolve, reject) => { const job: Job = { diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index 40104f9..b328638 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -92,7 +92,7 @@ abstract class Wallet { let results = await this.ckd(indexes) const data: any = [] results.forEach(r => data.push({ privateKey: r.privateKey, index: r.index })) - const keypairs: KeyPair[] = await Pool.work('distribute', 'nano-nacl', data) + const keypairs: KeyPair[] = await Pool.work('nano-nacl', data) for (const keypair of keypairs) { if (keypair.privateKey == null) throw new RangeError('Account private key missing') if (keypair.publicKey == null) throw new RangeError('Account public key missing') @@ -421,7 +421,7 @@ export class Bip44Wallet extends Wallet { async ckd (indexes: number[]): Promise { const data: any = [] indexes.forEach(i => data.push({ seed: this.seed, index: i })) - const privateKeys: KeyPair[] = await Pool.work('distribute', 'bip44-cdk', data) + const privateKeys: KeyPair[] = await Pool.work('bip44-cdk', data) return privateKeys } } diff --git a/src/lib/workers/pow.ts b/src/lib/workers/pow.ts index 32eb139..46acdcb 100644 --- a/src/lib/workers/pow.ts +++ b/src/lib/workers/pow.ts @@ -29,8 +29,8 @@ export class Pow { // Both width and height must be multiple of 256, (one byte) // but do not need to be the same, // matching GPU capabilities is the aim - static webglWidth = 256 * 4 - static webglHeight = 256 * 4 + static webglWidth = 256 * 1 + static webglHeight = 256 * 1 static hexify (arr: number[] | Uint8Array): string { let out = '' @@ -189,7 +189,8 @@ export class Pow { v[b + 1] = (xor0 >> 31) ^ (xor1 << 1); } - void main() { + bool found = false; + void find() { int i; uint uv_x = uint(uv_pos.x * ${canvas.width - 1}.); uint uv_y = uint(uv_pos.y * ${canvas.height - 1}.); @@ -231,6 +232,7 @@ export class Pow { // only calculate digest of the second 4 bytes if((BLAKE2B_IV32_1 ^ v[1] ^ v[17]) > ` + threshold + `u) { // Success found, return pixel data so work value can be constructed + found = true; fragColor = vec4( float(x_index + 1u)/255., // +1 to distinguish from 0 (unsuccessful) pixels float(y_index + 1u)/255., // Same as previous @@ -238,6 +240,14 @@ export class Pow { float(y_pos)/255. // Second custom byte ); } + } + + void main() { + int count = 0; + do { + count++; + find(); + } while (!found && count < 200); }` const vertexShader = gl.createShader(gl.VERTEX_SHADER) @@ -318,11 +328,7 @@ export class Pow { gl.clear(gl.COLOR_BUFFER_BIT) gl.drawArrays(gl.TRIANGLES, 0, 6) const pixels = new Uint8Array(gl.drawingBufferWidth * gl.drawingBufferHeight * 4) - performance.mark('readPixels start') gl.readPixels(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight, gl.RGBA, gl.UNSIGNED_BYTE, pixels) - performance.mark('readPixels end') - for (const e of performance.getEntries()) console.dir(e.toJSON()) - performance.clearMarks() // Check the pixels for any success for (let i = 0; i < pixels.length; i += 4) {