What is elliptic?
The elliptic npm package provides a collection of utilities for elliptic curve cryptography. It supports various elliptic curve operations such as key pair generation, digital signature creation and verification, and ECDH (Elliptic Curve Diffie-Hellman) key agreement. This makes it a versatile tool for implementing secure cryptographic functions in Node.js applications.
What are elliptic's main functionalities?
Key Pair Generation
This feature allows the generation of elliptic curve key pairs, which are fundamental for cryptographic operations. The example demonstrates generating a new key pair using the secp256k1 curve, which is widely used in blockchain technologies.
const EC = require('elliptic').ec;
const ec = new EC('secp256k1');
// Generate a new key pair
const keyPair = ec.genKeyPair();
// Get the public and private keys as hex strings
const publicKey = keyPair.getPublic().encode('hex');
const privateKey = keyPair.getPrivate().toString('hex');
Digital Signature
This feature enables the creation and verification of digital signatures, which are crucial for ensuring the integrity and authenticity of data. The code sample shows how to sign a message and then export the signature in DER format.
const EC = require('elliptic').ec;
const ec = new EC('secp256k1');
// Generate keys
const key = ec.genKeyPair();
// Sign a message
const msg = 'Hello world';
const signature = key.sign(msg);
// Export the signature to DER format
const derSign = signature.toDER();
ECDH Key Agreement
This feature supports Elliptic Curve Diffie-Hellman (ECDH), a key agreement protocol that allows two parties to establish a shared secret over an insecure channel. The example demonstrates how two parties can generate a shared secret.
const EC = require('elliptic').ec;
const ec = new EC('curve25519');
// Generate keys for two parties
const key1 = ec.genKeyPair();
const key2 = ec.genKeyPair();
// Derive shared secret
const shared1 = key1.derive(key2.getPublic());
const shared2 = key2.derive(key1.getPublic());
// Shared secrets should be the same
console.log(shared1.toString(16) === shared2.toString(16));
Other packages similar to elliptic
secp256k1
This package focuses specifically on the secp256k1 elliptic curve, offering functionalities similar to elliptic but with optimizations for speed and security. It is often used in Bitcoin and Ethereum projects.
node-forge
node-forge is a comprehensive Node.js library that includes a wide range of cryptographic operations, including those related to elliptic curves. While it covers more than elliptic curve cryptography, it is generally heavier and more complex to use for specific elliptic curve operations.
tweetnacl
tweetnacl is a port of NaCl, the Networking and Cryptography library, to JavaScript. It offers a variety of cryptographic primitives, including elliptic curve cryptography, with a focus on simplicity and small size. It's a good alternative for projects requiring lightweight cryptography solutions.
Elliptic
Fast elliptic-curve cryptography in a plain javascript implementation.
NOTE: Please take a look at http://safecurves.cr.yp.to/ before choosing a curve
for your cryptography operations.
Incentive
ECC is much slower than regular RSA cryptography, the JS implementations are
even more slower.
Benchmarks
$ node benchmarks/index.js
Benchmarking: sign
elliptic
eccjs
------------------------
Fastest is elliptic
========================
Benchmarking: verify
elliptic
eccjs
------------------------
Fastest is elliptic
========================
Benchmarking: gen
elliptic
eccjs
------------------------
Fastest is elliptic
========================
Benchmarking: ecdh
elliptic
------------------------
Fastest is elliptic
========================
API
ECDSA
var EC = require('elliptic').ec;
var ec = new EC('secp256k1');
var key = ec.genKeyPair();
var msg = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
var signature = key.sign(msg);
var derSign = signature.toDER();
console.log(key.verify(msg, derSign));
ECDH
var key1 = ec.genKeyPair();
var key2 = ec.genKeyPair();
var shared1 = key1.derive(key2.getPublic());
var shared2 = key2.derive(key1.getPublic());
console.log('Both shared secrets are BN instances');
console.log(shared1.toString(16));
console.log(shared2.toString(16));
NOTE: .derive()
returns a BN instance.
Supported curves
Elliptic.js support following curve types:
- Short Weierstrass
- Montgomery
- Edwards
- Twisted Edwards
Following curve 'presets' are embedded into the library:
secp256k1
p192
p224
p256
curve25519
ed25519
NOTE: That curve25519
could not be used for ECDSA, use ed25519
instead.
Implementation details
ECDSA is using deterministic k
value generation as per RFC6979. Most of
the curve operations are performed on non-affine coordinates (either projective
or extended), various windowing techniques are used for different cases.
All operations are performed in reduction context using bn.js, hashing is
provided by hash.js
Related projects
- eccrypto: isomorphic implementation of ECDSA, ECDH and ECIES for both
browserify and node (uses
elliptic
for browser and secp256k1-node for
node)
LICENSE
This software is licensed under the MIT License.
Copyright Fedor Indutny, 2014.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.