Proof-of-work generation and validation with WebGPU/WebGL for Nano cryptocurrency.
NanoPow uses WebGPU to generate proof-of-work nonces meeting the requirements of the Nano cryptocurrency. WebGPU is cutting edge technology, so for browsers which do not yet support it, a WebGL 2.0 implementation is also included.
All calculations take place client-side, so nonces can be generated offline and cached for the next transaction block. For more information about the proof-of-work equation defined by Nano, see https://docs.nano.org/integration-guides/work-generation/#work-calculation-details
npm i nano-pow
The easiest way to use NanoPow is to import it directly. Based on the features available in the environment, NanoPow will try to use its most performant API.
The following two import statements are equivalent, and both are provided to accomodate project style preferences:
import { NanoPow } from 'nano-pow'
// OR
import NanoPow from 'nano-pow'
A specific API can be explicitly imported:
import { NanoPowGpu, NanoPowGl } from 'nano-pow'
Any of these options can also be imported into the global namespace by targeting
dist/global.min.js
. For example:
<script type="module" src="https://cdn.jsdelivr.net/npm/nano-pow@latest/dist/global.min.js"></script>
<script type="module">
console.log(NanoPow)
</script>
// `hash` is a 64-char hex string
const hash = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
// `threshold` is optional and defaults to "0xFFFFFFF8" for send/change blocks
const work = await NanoPow.search(hash, threshold)
// Result is a 16-char hex string
// `work` is a 16-char hex string
const work = 'fedcba0987654321'
// `hash` is a 64-char hex string
const hash = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
// `threshold` is optional and defaults to "0xFFFFFFF8" for send/change blocks
const isValid = await NanoPow.validate(work, hash, threshold)
// Result is a boolean
The work
field in a Nano transaction block contains an 8-byte nonce that
satisfies the following equation:
𝘣𝘭𝘢𝘬𝘦2𝘣(𝘯𝘰𝘯𝘤𝘦 || 𝘣𝘭𝘰𝘤𝘬𝘩𝘢𝘴𝘩) ≥ 𝘵𝘩𝘳𝘦𝘴𝘩𝘰𝘭𝘥
- 𝘣𝘭𝘢𝘬𝘦2𝘣() is the cryptographic hash function BLAKE2b.
- 𝘯𝘰𝘯𝘤𝘦, an 8-byte value, is generated for the transaction.
- || is concatenation.
- 𝘣𝘭𝘰𝘤𝘬𝘩𝘢𝘴𝘩, a 32-byte value, is either the public key of brand new accounts without transactions or the hash of the most recent block in the account chain for all other accounts.
- 𝘵𝘩𝘳𝘦𝘴𝘩𝘰𝘭𝘥 is 0xFFFFFFF800000000 for send/change blocks and 0xFFFFFE0000000000 for receive/open/epoch blocks.
The threshold is implemented in code as only the first 32 bits due to WGSL only supporting u32 integer types, but the result is the same. For example, if checking whether a two-digit number xx > 55, and it is known that the first digit is 6, it is also automatically known that xx is greater than 55.
The default threshold used is the send/change difficulty. Any other threshold can be specified in practice. Also, NanoPow technically compares the nonce to see if it is only greater than, and not necessarily equal to, the threshold, but the difference is trivial for a range of 2⁶⁴-1 nonces.
The BLAKE2b implementation has been optimized to the extreme for this package due to the very narrow use case to which it is applied. The compute shader is consequently immense, but the goal is to squeeze every last bit of speed and performance out of it.
test.html
in the source repository contains some basic tests to compare the
speed of this tool. Feel free to check out how your system fares.
- Clone source
- Enter the directory
- Install dev dependencies
- Compile, minify, and bundle
git clone https://zoso.dev/nano-pow.git
cd nano-pow
npm i
npm run build
If you find this package helpful, please consider tipping the developer.
nano_1zosoqs47yt47bnfg7sdf46kj7asn58b7uzm9ek95jw7ccatq37898u1zoso