This repository is a port of VDFs by POA Network to WebAssemnbly, so that it runs in any modern browser or Node.js environment.
TypeScript support is also present.
NOTE: Performance is very bad right now, more than an order of magnitude slower than native code.
npm install @subspace/vdf
Node.js:
const createVdf = require('@subspace/vdf');
// Or TypeScript:
// import createVdf = require('@subspace/vdf');
createVdf()
.then((vdfInstance) => {
const iterations = 3;
const challenge = Buffer.from('aa', 'hex');
const intSizeBits = 2048;
const isPietrzak = false;
const res = vdfInstance.generate(iterations, challenge, intSizeBits, isPietrzak);
console.log(Buffer.from(res).toString('hex'));
console.log(vdfInstance.verify(3, challenge, res, intSizeBits, isPietrzak));
});
Browser:
requirejs(['@subspace/vdf'], function (createVdf) {
createVdf()
.then((vdfInstance) => {
const iterations = 3;
const challenge = Uint8Array.of(170);
const intSizeBits = 2048;
const isPietrzak = false;
const res = vdfInstance.generate(iterations, challenge, intSizeBits, isPietrzak);
console.log(
res.reduce(
(str, byte) => {
return str + byte.toString(16).padStart(2, '0');
},
''
)
);
console.log(vdfInstance.verify(3, challenge, res, intSizeBits, isPietrzak));
});
});
Creates a new instance of Noise library
options
- Options object that will be passed to underlying Emscripten module (optional)Generates VDF proof for a given challenge and number of iterations
iterations
- number of VDF iterationschallenge
- VDF challengeintSizeBits
- the length of the prime numbers generatedisPietrzak
- if Pietrzak VDF is used when true
and Wesolowski VDF is used when false
Returns binary proof, may throw Error
exception when failed to generate proof.
Generates VDF proof for a given challenge and number of iterations
iterations
- number of VDF iterationschallenge
- VDF challengeproof
- previously generated proof that needs to be verifiedintSizeBits
- the length of the prime numbers generatedisPietrzak
- if Pietrzak VDF is used when true
and Wesolowski VDF is used when false
Returns true
if proof
is correct, may throw Error
exception on incorrect input.
Even though this repo contains vdf.js
and vdf.wasm
, you may still want to build it from source yourself for testing purposes or for publishing updates to NPM.
Instructions below work on Linux (and maybe on macOS, but not tested).
You'll need at least following:
First, make sure you have Rust, Cargo and wasm32-unknown-emscripten target installed:
rustup target add wasm32-unknown-emscripten
This will compile GMP dependency and Rust code to vdf.js
and vdf.wasm
files under src
.
./build.sh
Before publishing to NPM it is necessary to build TypeScript to JavaScript and create minified version of the JavaScript output:
npm run build
To run tests use:
npm test
NOTE: We can't run tests against upstream repo's vectors yet because of performance issues, so instead we have a very basic test case to make sure it behaves as expected.
Feel free to create issues and send pull requests (for big changes create an issue first and link it from the PR), they are highly appreciated!
MIT, see license.txt