varEC=require('elliptic').ec;// Create and initialize EC context// (better do it once and reuse it)var ec =newEC('secp256k1');// Generate keysvar key =ec.genKeyPair();// Sign the message's hash (input must be an array, or a hex-string)var msgHash = [ 0,1,2,3,4,5,6,7,8,9,10 ];var signature =key.sign(msgHash);// Export DER encoded signature in Arrayvar derSign =signature.toDER();// Verify signatureconsole.log(key.verify(msgHash, derSign));// CHECK WITH NO PRIVATE KEYvar pubPoint =key.getPublic();var x =pubPoint.getX();var y =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)var pub =pubPoint.encode('hex'); // case 1var pub = { x:x.toString('hex'), y:y.toString('hex') }; // case 2var pub = { x:x.toBuffer(), y:y.toBuffer() }; // case 3var pub = { x:x.toArrayLike(Buffer), y:y.toArrayLike(Buffer) }; // case 3// Import public keyvar key =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)var signature ='3046022100...'; // case 1var signature =newBuffer('...'); // case 2var signature = { r:'b1fc...', s:'9c42...' }; // case 3// Verify signatureconsole.log(key.verify(msgHash, signature));
EdDSA
var EdDSA =require('elliptic').eddsa;// Create and initialize EdDSA context// (better do it once and reuse it)var ec =newEdDSA('ed25519');// Create key pair from secretvar key =ec.keyFromSecret('693e3c...'); // hex string, array or Buffer// Sign the message's hash (input must be an array, or a hex-string)var msgHash = [ 0,1,2,3,4,5,6,7,8,9,10 ];var signature =key.sign(msgHash).toHex();// Verify signatureconsole.log(key.verify(msgHash, signature));// CHECK WITH NO PRIVATE KEY// Import public keyvar pub ='0a1af638...';var key =ec.keyFromPublic(pub,'hex');// Verify signaturevar signature ='70bed1...';console.log(key.verify(msgHash, signature));
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.