- Notifications
You must be signed in to change notification settings - Fork507
Fast Elliptic Curve Cryptography in plain javascript
indutny/elliptic
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Fast elliptic-curve cryptography in a plain javascript implementation.
NOTE: Please take a look athttp://safecurves.cr.yp.to/ before choosing a curvefor your cryptography operations.
ECC is much slower than regular RSA cryptography, the JS implementations areeven more slower.
$ node benchmarks/index.jsBenchmarking: signelliptic#sign x 262 ops/sec ±0.51% (177 runs sampled)eccjs#sign x 55.91 ops/sec ±0.90% (144 runs sampled)------------------------Fastest is elliptic#sign========================Benchmarking: verifyelliptic#verify x 113 ops/sec ±0.50% (166 runs sampled)eccjs#verify x 48.56 ops/sec ±0.36% (125 runs sampled)------------------------Fastest is elliptic#verify========================Benchmarking: genelliptic#gen x 294 ops/sec ±0.43% (176 runs sampled)eccjs#gen x 62.25 ops/sec ±0.63% (129 runs sampled)------------------------Fastest is elliptic#gen========================Benchmarking: ecdhelliptic#ecdh x 136 ops/sec ±0.85% (156 runs sampled)------------------------Fastest is elliptic#ecdh========================
varEC=require('elliptic').ec;// Create and initialize EC context// (better do it once and reuse it)varec=newEC('secp256k1');// Generate keysvarkey=ec.genKeyPair();// Sign the message's hash (input must be an array, or a hex-string)varmsgHash=[0,1,2,3,4,5,6,7,8,9,10];varsignature=key.sign(msgHash);// Export DER encoded signature in ArrayvarderSign=signature.toDER();// Verify signatureconsole.log(key.verify(msgHash,derSign));// CHECK WITH NO PRIVATE KEYvarpubPoint=key.getPublic();varx=pubPoint.getX();vary=pubPoint.getY();// Public Key MUST be either:// 1) '04' + hex string of x + hex string of y; or// 2) object with two hex string properties (x and y); or// 3) object with two buffer properties (x and y)varpub=pubPoint.encode('hex');// case 1varpub={x:x.toString('hex'),y:y.toString('hex')};// case 2varpub={x:x.toBuffer(),y:y.toBuffer()};// case 3varpub={x:x.toArrayLike(Buffer),y:y.toArrayLike(Buffer)};// case 3// Import public keyvarkey=ec.keyFromPublic(pub,'hex');// Signature MUST be either:// 1) DER-encoded signature as hex-string; or// 2) DER-encoded signature as buffer; or// 3) object with two hex-string properties (r and s); or// 4) object with two buffer properties (r and s)varsignature='3046022100...';// case 1varsignature=newBuffer('...');// case 2varsignature={r:'b1fc...',s:'9c42...'};// case 3// Verify signatureconsole.log(key.verify(msgHash,signature));
varEdDSA=require('elliptic').eddsa;// Create and initialize EdDSA context// (better do it once and reuse it)varec=newEdDSA('ed25519');// Create key pair from secretvarkey=ec.keyFromSecret('693e3c...');// hex string, array or Buffer// Sign the message's hash (input must be an array, or a hex-string)varmsgHash=[0,1,2,3,4,5,6,7,8,9,10];varsignature=key.sign(msgHash).toHex();// Verify signatureconsole.log(key.verify(msgHash,signature));// CHECK WITH NO PRIVATE KEY// Import public keyvarpub='0a1af638...';varkey=ec.keyFromPublic(pub,'hex');// Verify signaturevarsignature='70bed1...';console.log(key.verify(msgHash,signature));
varEC=require('elliptic').ec;varec=newEC('curve25519');// Generate keysvarkey1=ec.genKeyPair();varkey2=ec.genKeyPair();varshared1=key1.derive(key2.getPublic());varshared2=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:
varEC=require('elliptic').ec;varec=newEC('curve25519');varA=ec.genKeyPair();varB=ec.genKeyPair();varC=ec.genKeyPair();varAB=A.getPublic().mul(B.getPrivate())varBC=B.getPublic().mul(C.getPrivate())varCA=C.getPublic().mul(A.getPrivate())varABC=AB.mul(C.getPrivate())varBCA=BC.mul(A.getPrivate())varCAB=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 aBN instance.
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: Thatcurve25519
could not be used for ECDSA, useed25519
instead.
ECDSA is using deterministick
value generation as perRFC6979. Most ofthe curve operations are performed on non-affine coordinates (either projectiveor extended), various windowing techniques are used for different cases.
All operations are performed in reduction context usingbn.js, hashing isprovided byhash.js
- eccrypto: isomorphic implementation of ECDSA, ECDH and ECIES for bothbrowserify and node (uses
elliptic
for browser andsecp256k1-node fornode)
This software is licensed under the MIT License.
Copyright Fedor Indutny, 2014.
Permission is hereby granted, free of charge, to any person obtaining acopy of this software and associated documentation files (the"Software"), to deal in the Software without restriction, includingwithout limitation the rights to use, copy, modify, merge, publish,distribute, sublicense, and/or sell copies of the Software, and to permitpersons to whom the Software is furnished to do so, subject to thefollowing conditions:
The above copyright notice and this permission notice shall be includedin all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESSOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OFMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. INNO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OROTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THEUSE OR OTHER DEALINGS IN THE SOFTWARE.
About
Fast Elliptic Curve Cryptography in plain javascript
Resources
Uh oh!
There was an error while loading.Please reload this page.