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 msgHash = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
var signature = key.sign(msgHash);
var derSign = signature.toDER();
console.log(key.verify(msgHash, derSign));
var pubPoint = key.getPublic();
var x = pubPoint.getX();
var y = pubPoint.getY();
var pub = pubPoint.encode('hex');
var pub = { x: x.toString('hex'), y: y.toString('hex') };
var pub = { x: x.toBuffer(), y: y.toBuffer() };
var pub = { x: x.toArrayLike(Buffer), y: y.toArrayLike(Buffer) };
var key = ec.keyFromPublic(pub, 'hex');
var signature = '3046022100...';
var signature = new Buffer('...');
var signature = { r: 'b1fc...', s: '9c42...' };
console.log(key.verify(msgHash, signature));
EdDSA
var EdDSA = require('elliptic').eddsa;
var ec = new EdDSA('ed25519');
var key = ec.keyFromSecret('693e3c...');
var msgHash = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
var signature = key.sign(msgHash).toHex();
console.log(key.verify(msgHash, signature));
var pub = '0a1af638...';
var key = ec.keyFromPublic(pub, 'hex');
var signature = '70bed1...';
console.log(key.verify(msgHash, signature));
ECDH
var EC = require('elliptic').ec;
var ec = new EC('curve25519');
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));
three and more members:
var EC = require('elliptic').ec;
var ec = new EC('curve25519');
var A = ec.genKeyPair();
var B = ec.genKeyPair();
var C = ec.genKeyPair();
var AB = A.getPublic().mul(B.getPrivate())
var BC = B.getPublic().mul(C.getPrivate())
var CA = C.getPublic().mul(A.getPrivate())
var ABC = AB.mul(C.getPrivate())
var BCA = BC.mul(A.getPrivate())
var CAB = CA.mul(B.getPrivate())
console.log(ABC.getX().toString(16))
console.log(BCA.getX().toString(16))
console.log(CAB.getX().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
p384
p521
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.