Bitcoin, Bitcoin Cash, and most other cryptocurrencies use the Elliptic Curve Digital Signature Algorithm (ECDSA) to authenticate transactions. ECDSA can be used with a number of different elliptic curves, and Bitcoin’s creator chose the Koblitz curve Secp256k1 for Bitcoin’s digital signature system.Secp256k1 is used by most cryptocurrency software
Most software packages which interact with these systems require Secp256k1 support. Mining software must be able to validate that transactions are properly signed before being included in the blockchain. Cryptocurrency wallets must be able to both verify incoming transactions and sign transactions to spend funds.
A WebAssembly version of libsecp256k1 is now available on GitHub and via the Node.js package manager, NPM:npm install --save bitcoin-ts
The API is purely-functional, fully-typed and documented with TypeScript, and accepts and returns native ES2015 typed arrays. Here’s a short example of Low-S signature verification using the library:
The library is written in TypeScript and documented inline, so capable editors will show documentation as you type:
For more usage information, review the code, the tests, or check out the Secp256k1 Interface in the TypeDocs.Thoroughly tested against existing implementations
Along with standard test vectors and 100% code coverage, the library uses property-based testing to randomly generate thousands of test cases each run, including comparisons of the results against results from two other popular libraries, elliptic and secp256k1-node.
The generated WebAssembly binary is also auditable and deterministic – you can rebuild it easily yourself and verify that the NPM-distributed version is correct. Simply clone the GitHub repo, install the dependencies, and run yarn compile:wasm.
With a 10x performance increase, this may already be a very promising option for non-Node.js applications like wallet apps (Copay, BitPay, Blockchain.info, Bitcoin.com Wallet, Jaxx, and Exodus, to name a few).
Though currently slower than the Node.js-only bindings, for Node.js applications where Secp256k1 performance isn’t a bottleneck, this library might already be preferable since it doesn’t require any native code compilation. The binary is completely portable between operating systems, and the install is nearly instant. For many applications (like in continuous integration tasks) this will already improve performance.And it’s getting much faster 🚀
Finally, it’s important to note that WebAssembly is relatively new, and it’s still getting faster. WebAssembly only began shipping in all major browsers in November of 2017, and browsers have managed to optimize WebAssembly performance in almost every version they’ve shipped.
As WebAssembly matures, we can expect performance of this implementation to approach that of traditionally-compiled low-level software. (And according to some estimates, exceed it.)Future plans for bitcoin-ts
Next up, bitcoin-ts will be building and exposing WebAssembly versions of other cryptographic primitives used in Bitcoin software — SHA-1, SHA-256, SHA-512, RIPEMD-160, and maybe others.
Though work will be ongoing, the library is production-ready, and will be following semantic versioning to keep already-available functionality stable. (Current version: 1.0.3)
If you’d like to support the project, please star bitcoin-ts on GitHub. If you appreciated this post, please share it so others can find it. Thanks for reading!