A "from scratch" Elliptic Curve Cryptography implementation in TypeScript using the Deno JavaScript runtime.
This repository includes code for the Elliptic-Curve Diffie-Hellman (ECDH) key agreement protocol as well as the Elliptic-Curve Digital Signature Algorithm (ECDSA). Both are implemented on top of the secp256k1 curve that's also used in Bitcoin and Ethereum.
In addition to a "raw" ECDSA implementation there's also one that can be used to sign messages and transact on the Ethereum Blockchain. To do so easily, an Ethers v6 compliant Signer was implemented which wraps the ECDSA functionalities.
Note that the code you'll find here wasn't audited and is definitely not constant time. You should therefore use this codebase as an educational resource which was my intention when I wrote it.
The best way to understand the code is to dive into the *.test.ts
or script files and see how different parts of the code interact.
All the resources I've studied to write this implementation can be found in the Useful Resources section.
git clone <url>
deno test
deno task dev
The following are scripts you can run to test the implementation.
Some scripts require you to get some test ETH on Sepolia. There are various faucets available online you can use to get access to testnet ETH.
# Generate a new private- and public key pair.
deno task keys
# Display information for an existing key pair.
PRIVATE_KEY=0x1234567890 deno task keys
# Get the account balance on the Sepolia test network.
PRIVATE_KEY=0x1234567890 deno task balance
# Sign a message.
PRIVATE_KEY=0x1234567890 deno task sign
# Send ETH to another address on the Sepolia test network.
PRIVATE_KEY=0x1234567890 deno task send
deno init
deno info
deno doc [<path>]
deno repl
deno bench <path>
deno compile [-A] <path>
deno fmt [<path>]
deno lint [<path>]
deno test [<path>]
deno run <path>
deno task dev
- Wikipedia - Elliptic Curve Cryptography
- Wikipedia - Elliptic-Curve Diffie-Hellman
- Wikipedia - Elliptic Curve Digital Signature Algorithm
- Philipp Muens - Elliptic-Curve Diffie-Hellman
- Anirudha Bose - Roll your own crypto*
- Andrea Corbellini - Elliptic Curve Cryptography: a gentle introduction
- Andrea Corbellini - Elliptic Curve Cryptography: finite fields and discrete logarithms
- Andrea Corbellini - Elliptic Curve Cryptography: ECDH and ECDSA
- Andrea Corbellini - Elliptic Curve Cryptography: breaking security and a comparison with RSA
- Paul Miller - Learning fast elliptic-curve cryptography
- Hyperelliptic - Short Weierstrass curves
- Bitcoin Wiki - Secp256k1
- Lucas Henning - The Dark Side of the Elliptic Curve - Signing Ethereum Transactions with AWS KMS in JavaScript