This library is under development, and, like the secp256k1 C library (through secp256k1-sys Rust crate) it depends on, this is a research effort to determine an optimal API for end-users of the bitcoinjs ecosystem.
npm install tiny-secp256k1yarn add tiny-secp256k1Previous version of tiny-secp256k1 implement C++ addon through NAN (Native Abstractions for Node.js) and elliptic as fallback when addon can not be built or in browser-like environement.
Current version use Rust crate (which use C library) compiled to WebAssembly. With Wasm same code executed in any environment. Wasm is faster than elliptic but slower than node bindings (results in PR or you can run own benchmark in benches directory).
For building locally you need C/C++ toolchain, Rust nightly version and wasm-opt from binaryen.
rustup is a recommended way to install Rust. You also will need wasm32-unknown-unknown target.
rustup toolchain install nightly --target wasm32-unknown-unknown --component clippy --component rustfmt
After installing development dependencies with npm you can build Wasm:
make build-wasm
or run tests:
make test
Alternative way is to use Docker:
% docker build -t tiny-secp256k1 .
% docker run -it --rm -v `pwd`:/tiny-secp256k1 -w /tiny-secp256k1 tiny-secp256k1
# npm install --unsafe-perm
# make test
# make clean
tiny-secp256k1 includes two examples. First is simple script for Node.js which generate random data and print arguments and methods results. Second is React app.
React app is builded in GitHub Actions on each commit to master branch and uploaded to gh-pages branch, which is always available online: https://bitcoinjs.github.io/tiny-secp256k1/
isPoint :: Buffer -> BoolReturns false if
Ais not encoded with a sequence tag of0x02,0x03or0x04A.xis not in[1...p - 1]A.yis not in[1...p - 1]
isPointCompressed :: Buffer -> BoolReturns false if the signature is not compressed.
isPrivate :: Buffer -> BoolReturns false if
dis not 256-bit, ordis not in[1..order - 1]
pointAdd :: Buffer -> Buffer [-> Bool] -> Maybe BufferReturns null if result is at infinity.
Expected Pointif!isPoint(A)Expected Pointif!isPoint(B)
pointAddScalar :: Buffer -> Buffer [-> Bool] -> Maybe BufferReturns null if result is at infinity.
Expected Pointif!isPoint(A)Expected Tweakiftweakis not in[0...order - 1]
pointCompress :: Buffer -> Bool -> BufferExpected Pointif!isPoint(A)
pointFromScalar :: Buffer [-> Bool] -> Maybe BufferReturns null if result is at infinity.
Expected Privateif!isPrivate(d)
pointMultiply :: Buffer -> Buffer [-> Bool] -> Maybe BufferReturns null if result is at infinity.
Expected Pointif!isPoint(A)Expected Tweakiftweakis not in[0...order - 1]
privateAdd :: Buffer -> Buffer -> Maybe BufferReturns null if result is equal to 0.
Expected Privateif!isPrivate(d)Expected Tweakiftweakis not in[0...order - 1]
privateSub :: Buffer -> Buffer -> Maybe BufferReturns null if result is equal to 0.
Expected Privateif!isPrivate(d)Expected Tweakiftweakis not in[0...order - 1]
sign :: Buffer -> Buffer [-> Buffer] -> BufferReturns normalized signatures, each of (r, s) values are guaranteed to less than order / 2.
Uses RFC6979.
Adds e as Added Entropy to the deterministic k generation.
Expected Privateif!isPrivate(d)Expected Scalarifhis not 256-bitExpected Extra Data (32 bytes)ifeis not 256-bit
verify :: Buffer -> Buffer -> Buffer -> BoolReturns false if any of (r, s) values are equal to 0, or if the signature is rejected.
If strict is true, valid signatures with any of (r, s) values greater than order / 2 are rejected.
Expected Pointif!isPoint(Q)Expected Signatureifsignaturehas any (r, s) values not in range[0...order - 1]Expected Scalarifhis not 256-bit
This library uses the native library secp256k1 by the bitcoin-core developers through Rust crate secp256k1-sys, including derivatives of its tests and test vectors.