tiny-secp256k1

NPM

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.

Installation

npm

bash npm install tiny-secp256k1

yarn

bash yarn add tiny-secp256k1

WebAssembly and Node.js version

Previous 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).

Building

For building locally you need C/C++ toolchain, Rust version >=1.50.0 and wasm-opt from binaryen.

rustup is a recommended way to install Rust. You also will need wasm32-unknown-unknown target.

rustup toolchain install stable --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 -vpwd`:/tiny-secp256k1 -w /tiny-secp256k1 tiny-secp256k1

make build

```

Examples

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/

Documentation

isPoint (A)

haskell isPoint :: Buffer -> Bool

Returns false if

isPointCompressed (A)

haskell isPointCompressed :: Buffer -> Bool

Returns false if the pubkey is not compressed.

isXOnlyPoint (A)

haskell isXOnlyPoint :: Buffer -> Bool

Returns false if the pubkey is not an xOnlyPubkey.

isPrivate (d)

haskell isPrivate :: Buffer -> Bool

Returns false if

pointAdd (A, B[, compressed])

haskell pointAdd :: Buffer -> Buffer [-> Bool] -> Maybe Buffer

Returns null if result is at infinity.

Throws:

pointAddScalar (A, tweak[, compressed])

haskell pointAddScalar :: Buffer -> Buffer [-> Bool] -> Maybe Buffer

Returns null if result is at infinity.

Throws:

pointCompress (A, compressed)

haskell pointCompress :: Buffer -> Bool -> Buffer

Throws:

pointFromScalar (d[, compressed])

haskell pointFromScalar :: Buffer [-> Bool] -> Maybe Buffer

Returns null if result is at infinity.

Throws:

xOnlyPointFromScalar (d)

haskell xOnlyPointFromScalar :: Buffer -> Buffer

Returns the xOnlyPubkey for a given private key

Throws:

xOnlyPointFromPoint (p)

haskell xOnlyPointFromPoint :: Buffer -> Buffer

Returns the xOnlyPubkey for a given DER public key

Throws:

pointMultiply (A, tweak[, compressed])

haskell pointMultiply :: Buffer -> Buffer [-> Bool] -> Maybe Buffer

Returns null if result is at infinity.

Throws:

privateAdd (d, tweak)

haskell privateAdd :: Buffer -> Buffer -> Maybe Buffer

Returns null if result is equal to 0.

Throws:

privateSub (d, tweak)

haskell privateSub :: Buffer -> Buffer -> Maybe Buffer

Returns null if result is equal to 0.

Throws:

xOnlyPointAddTweak (p, tweak)

haskell xOnlyPointAddTweak :: Buffer -> Buffer -> { parity: 1 | 0; xOnlyPubkey: Buffer; }

Returns the tweaked xOnlyPubkey along with the parity bit (number type of 1|0)

Throws:

xOnlyPointAddTweakCheck (p1, p2, tweak[, tweakParity])

haskell xOnlyPointAddTweakCheck :: Buffer -> Buffer -> Buffer [-> 1 | 0] -> Bool

Checks the tweaked pubkey (p2) against the original pubkey (p1) and tweak. This is slightly slower if you include tweakParity, tweakParity will make it faster for aggregation later on.

Throws:

sign (h, d[, e])

haskell sign :: Buffer -> Buffer [-> Buffer] -> Buffer

Returns 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.

Throws:

signSchnorr (h, d[, e])

haskell signSchnorr :: Buffer -> Buffer [-> Buffer] -> Buffer

Returns normalized schnorr signature. Uses BIP340 nonce generation. Adds e as Added Entropy.

Throws:

verify (h, Q, signature[, strict = false])

haskell verify :: Buffer -> Buffer -> Buffer [-> Bool] -> Bool

Returns 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.

Throws:

verifySchnorr (h, Q, signature)

haskell verifySchnorr :: Buffer -> Buffer -> Buffer -> Bool

Returns false if any of (r, s) values are equal to 0, or if the signature is rejected.

Throws:

Credit

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.

LICENSE MIT