interface Window {
NanoPow: typeof NanoPow
}
+ type Bytes = Uint8Array<ArrayBuffer>
}
/**
//! SPDX-License-Identifier: GPL-3.0-or-later
import { NanoPowValidate } from '#lib/validate'
-import { Bytes, Logger } from '#utils'
+import { Logger } from '#utils'
import { WorkGenerateResponse } from 'nano-pow'
const logger = new Logger()
-export async function generate (hash: bigint, difficulty: bigint, debug: boolean): Promise<WorkGenerateResponse> {
+export async function generate (hash: Bytes, difficulty: bigint, debug: boolean): Promise<WorkGenerateResponse> {
logger.isEnabled = debug
LOG: logger.groupStart('NanoPow CPU work_generate')
return new Promise((resolve, reject) => {
const check = () => {
try {
LOG: logger.log('check 0x8000 nonces')
- let result = NanoPowValidate(crypto.getRandomValues(new Uint8Array(8)), Bytes(hash, 32), difficulty, false)
+ let result = NanoPowValidate(crypto.getRandomValues(new Uint8Array(8)), hash, difficulty, false)
for (let i = 0; i < 0x8000; i++) {
- result = NanoPowValidate(crypto.getRandomValues(new Uint8Array(8)), Bytes(hash, 32), difficulty, false)
+ result = NanoPowValidate(crypto.getRandomValues(new Uint8Array(8)), hash, difficulty, false)
if (result.valid === '1') break
}
if (result.valid === '1') {
//! SPDX-License-Identifier: GPL-3.0-or-later
import { NanoPowValidate } from '#lib/validate'
-import { bigintRandom, bigintToHex, Bytes, Logger } from '#utils'
+import { Bytes, BytesToHex, Logger, bigintRandom, bigintToHex } from '#utils'
import { WorkGenerateResponse } from 'nano-pow'
import { NanoPowWasmWorker } from './worker.js'
v?.fill(0n)
}
-async function init (hash: bigint, difficulty: bigint, effort: number): Promise<void> {
- data.hash = bigintToHex(hash, 64)
+async function init (hash: Bytes, difficulty: bigint, effort: number): Promise<void> {
+ data.hash = BytesToHex(hash.buffer, 64)
data.difficulty = bigintToHex(difficulty, 16)
for (let i = workers.length; i < effort; i++) {
/**
* Nano proof-of-work using WebAssembly.
*/
-export async function generate (hash: bigint, difficulty: bigint, effort: number, debug: boolean): Promise<WorkGenerateResponse> {
+export async function generate (hash: Bytes, difficulty: bigint, effort: number, debug: boolean): Promise<WorkGenerateResponse> {
logger.isEnabled = debug
LOG: logger.groupStart('NanoPow WASM work_generate')
LOG: logger.log('generating')
let result = ''
try {
work = await dispatch()
- result = (await NanoPowValidate(Bytes(work, 8), Bytes(hash, 32), difficulty, debug)).difficulty
+ result = (await NanoPowValidate(Bytes(work, 8), hash, difficulty, debug)).difficulty
} catch (err) {
LOG: logger.log(err)
} finally {
}
return {
- hash: bigintToHex(hash, 64),
+ hash: BytesToHex(hash.buffer, 64),
work: bigintToHex(work, 16),
difficulty: result
}
//! SPDX-FileContributor: Ben Green <ben@latenightsketches.com>
//! SPDX-License-Identifier: GPL-3.0-or-later AND MIT
-import { Bytes, Logger, bigintRandom, bigintToHex } from '#utils'
+import { BytesToHex, Logger, bigintRandom, bigintToHex } from '#utils'
import { WorkGenerateResponse } from 'nano-pow'
import { downsampleSource, drawSource, quadSource } from './shaders'
/**
* Nano proof-of-work using WebGL 2.0.
*/
-export async function generate (hash: bigint, difficulty: bigint, effort: number, debug: boolean): Promise<WorkGenerateResponse> {
+export async function generate (hash: Bytes, difficulty: bigint, effort: number, debug: boolean): Promise<WorkGenerateResponse> {
logger.isEnabled = debug
// Set up 60s timeout to prevent long-running calls
let timeout = false
LOG: logger.log('timed out')
}, 60_000)
LOG: logger.groupStart('NanoPow WebGL work_generate')
- LOG: logger.log(`generate('${hash.toString(16)}', '${difficulty.toString(16)}', ${effort}, ${debug})`)
+ LOG: logger.log(`generate('${BytesToHex(hash.buffer)}', '${difficulty.toString(16)}', ${effort}, ${debug})`)
// Start drawing to calculate one nonce per pixel
let found = false
if (isReady === false || effort !== drawEffort || isFirstRetry) {
setup(effort)
}
- init(new Uint32Array(Bytes(hash, 32).buffer), difficulty)
+ init(new Uint32Array(hash.buffer), difficulty)
LOG: logger.log('drawing frame 0')
draw(bigintRandom(), drawFbos[0], queries[0])
draw(bigintRandom(), drawFbos[1], queries[1])
}
return {
- hash: bigintToHex(hash, 64),
+ hash: BytesToHex(hash.buffer, 64),
work: bigintToHex(result.work, 16),
difficulty: bigintToHex(result.difficulty, 16)
}
//! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
//! SPDX-License-Identifier: GPL-3.0-or-later
-import { Bytes, Logger, Queue, bigintRandom, bigintToHex } from '#utils'
+import { BytesToHex, Logger, Queue, bigintRandom, bigintToHex } from '#utils'
import { WorkGenerateResponse } from 'nano-pow'
import { default as NanoPowGpuComputeShader } from './shaders/compute.wgsl'
}
}
-async function init (hash: Uint8Array<ArrayBuffer>, difficulty: bigint): Promise<void> {
+async function init (hash: Bytes, difficulty: bigint): Promise<void> {
LOG: logger.log('variables initializing')
try {
// Save hash data for normal usage and potential recovery efforts
/**
* Nano proof-of-work using WebGPU.
*/
-export async function generate (hash: bigint, difficulty: bigint, effort: number, debug: boolean): Promise<WorkGenerateResponse> {
+export async function generate (hash: Bytes, difficulty: bigint, effort: number, debug: boolean): Promise<WorkGenerateResponse> {
logger.isEnabled = debug
// Set up 60s timeout to prevent long-running calls
let timeout = false
throw new Error('failed to start')
}
}
- await q.add(init, Bytes(hash, 32), difficulty)
+ await q.add(init, hash, difficulty)
// Dispatch initial workgroups and set index
await dispatch(0, bigintRandom(), effort)
let dispatchIndex = 1
}
return {
- hash: bigintToHex(hash, 64),
+ hash: BytesToHex(hash.buffer, 64),
work: bigintToHex(result.work, 16),
difficulty: bigintToHex(result.difficulty, 16)
}
}
}
}
+ const hashBytes = Bytes(bigintHash, 32)
switch (api) {
case 'webgpu': {
- return Cache.store(await NanoPowWebgpu(bigintHash, difficulty, effort, debug))
+ return Cache.store(await NanoPowWebgpu(hashBytes, difficulty, effort, debug))
}
case 'webgl': {
- return Cache.store(await NanoPowWebgl(bigintHash, difficulty, effort, debug))
+ return Cache.store(await NanoPowWebgl(hashBytes, difficulty, effort, debug))
}
case 'wasm': {
- return Cache.store(await NanoPowWasm(bigintHash, difficulty, effort, debug))
+ return Cache.store(await NanoPowWasm(hashBytes, difficulty, effort, debug))
}
default: {
- return Cache.store(await NanoPowCpu(bigintHash, difficulty, debug))
+ return Cache.store(await NanoPowCpu(hashBytes, difficulty, debug))
}
}
} catch (e: any) {
result[0] = (blake2b_IV[0] ^ 0x01010008n ^ v[0] ^ v[8])
}
-function log (work: Uint8Array<ArrayBuffer>, hash: Uint8Array<ArrayBuffer>, difficulty: bigint): void {
+function log (work: Bytes, hash: Bytes, difficulty: bigint): void {
LOG: logger.groupStart('NanoPow CPU work_validate')
LOG: logger.log('NanoPow CPU work_validate', 'work', BytesToHex(work.buffer))
LOG: logger.log('NanoPow CPU work_validate', 'hash', BytesToHex(hash.buffer))
LOG: logger.groupEnd('NanoPow CPU work_validate')
}
-function validate (work: Uint8Array<ArrayBuffer>, hash: Uint8Array<ArrayBuffer>, difficulty: bigint, debug: boolean): WorkValidateResponse {
+function validate (work: Bytes, hash: Bytes, difficulty: bigint, debug: boolean): WorkValidateResponse {
logger.isEnabled = debug
blake2b(work, hash)
log(work, hash, difficulty)
*
* @param {(bigint|number|string)} input
* @param {number} [minLength=1]
- * @returns {Uint8Array<ArrayBuffer>}
+ * @returns {Bytes}
*/
-export function Bytes (input: unknown, minLength: unknown = 1): Uint8Array<ArrayBuffer> {
+export function Bytes (input: unknown, minLength: unknown = 1): Bytes {
if (typeof input === 'number') {
input = BigInt(input | 0)
}
async function run (size, difficulty, effort, api, isOutputShown, isDebug, isSelfCheck) {
// Generate once on load to compile shaders and initialize buffers
- await NanoPow.work_generate(random(), { api, difficulty: 0 })
+ await NanoPow.work_generate(random(), { api, difficulty: '0' })
const type = api
api = type.toLowerCase()
if (isSelfCheck) {