\href{https://saucelabs.com/u/gh-indutny-elliptic}{\texttt{ }}

Fast elliptic-\/curve cryptography in a plain javascript implementation.

NOTE\+: Please take a look at \href{http://safecurves.cr.yp.to/}{\texttt{ http\+://safecurves.\+cr.\+yp.\+to/}} before choosing a curve for your cryptography operations.\hypertarget{md_sfcode_node_modules_elliptic_README_autotoc_md8336}{}\doxysection{Incentive}\label{md_sfcode_node_modules_elliptic_README_autotoc_md8336}
ECC is much slower than regular RSA cryptography, the JS implementations are even more slower.\hypertarget{md_sfcode_node_modules_elliptic_README_autotoc_md8337}{}\doxysection{Benchmarks}\label{md_sfcode_node_modules_elliptic_README_autotoc_md8337}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ node benchmarks/index.js}
\DoxyCodeLine{Benchmarking: sign}
\DoxyCodeLine{elliptic\#sign x 262 ops/sec ±0.51\% (177 runs sampled)}
\DoxyCodeLine{eccjs\#sign x 55.91 ops/sec ±0.90\% (144 runs sampled)}
\DoxyCodeLine{-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/}
\DoxyCodeLine{Fastest is elliptic\#sign}
\DoxyCodeLine{========================}
\DoxyCodeLine{Benchmarking: verify}
\DoxyCodeLine{elliptic\#verify x 113 ops/sec ±0.50\% (166 runs sampled)}
\DoxyCodeLine{eccjs\#verify x 48.56 ops/sec ±0.36\% (125 runs sampled)}
\DoxyCodeLine{-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/}
\DoxyCodeLine{Fastest is elliptic\#verify}
\DoxyCodeLine{========================}
\DoxyCodeLine{Benchmarking: gen}
\DoxyCodeLine{elliptic\#gen x 294 ops/sec ±0.43\% (176 runs sampled)}
\DoxyCodeLine{eccjs\#gen x 62.25 ops/sec ±0.63\% (129 runs sampled)}
\DoxyCodeLine{-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/}
\DoxyCodeLine{Fastest is elliptic\#gen}
\DoxyCodeLine{========================}
\DoxyCodeLine{Benchmarking: ecdh}
\DoxyCodeLine{elliptic\#ecdh x 136 ops/sec ±0.85\% (156 runs sampled)}
\DoxyCodeLine{-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/}
\DoxyCodeLine{Fastest is elliptic\#ecdh}
\DoxyCodeLine{======================== }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_elliptic_README_autotoc_md8338}{}\doxysection{API}\label{md_sfcode_node_modules_elliptic_README_autotoc_md8338}
\hypertarget{md_sfcode_node_modules_elliptic_README_autotoc_md8339}{}\doxysubsection{ECDSA}\label{md_sfcode_node_modules_elliptic_README_autotoc_md8339}

\begin{DoxyCode}{0}
\DoxyCodeLine{var EC = require('elliptic').ec;}
\DoxyCodeLine{}
\DoxyCodeLine{// Create and initialize EC context}
\DoxyCodeLine{// (better do it once and reuse it)}
\DoxyCodeLine{var ec = new EC('secp256k1');}
\DoxyCodeLine{}
\DoxyCodeLine{// Generate keys}
\DoxyCodeLine{var key = ec.genKeyPair();}
\DoxyCodeLine{}
\DoxyCodeLine{// Sign the message's hash (input must be an array, or a hex-\/string)}
\DoxyCodeLine{var msgHash = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];}
\DoxyCodeLine{var signature = key.sign(msgHash);}
\DoxyCodeLine{}
\DoxyCodeLine{// Export DER encoded signature in Array}
\DoxyCodeLine{var derSign = signature.toDER();}
\DoxyCodeLine{}
\DoxyCodeLine{// Verify signature}
\DoxyCodeLine{console.log(key.verify(msgHash, derSign));}
\DoxyCodeLine{}
\DoxyCodeLine{// CHECK WITH NO PRIVATE KEY}
\DoxyCodeLine{}
\DoxyCodeLine{var pubPoint = key.getPublic();}
\DoxyCodeLine{var x = pubPoint.getX();}
\DoxyCodeLine{var y = pubPoint.getY();}
\DoxyCodeLine{}
\DoxyCodeLine{// Public Key MUST be either:}
\DoxyCodeLine{// 1) '04' + hex string of x + hex string of y; or}
\DoxyCodeLine{// 2) object with two hex string properties (x and y); or}
\DoxyCodeLine{// 3) object with two buffer properties (x and y)}
\DoxyCodeLine{var pub = pubPoint.encode('hex');                                 // case 1}
\DoxyCodeLine{var pub = \{ x: x.toString('hex'), y: y.toString('hex') \};         // case 2}
\DoxyCodeLine{var pub = \{ x: x.toBuffer(), y: y.toBuffer() \};                   // case 3}
\DoxyCodeLine{var pub = \{ x: x.toArrayLike(Buffer), y: y.toArrayLike(Buffer) \}; // case 3}
\DoxyCodeLine{}
\DoxyCodeLine{// Import public key}
\DoxyCodeLine{var key = ec.keyFromPublic(pub, 'hex');}
\DoxyCodeLine{}
\DoxyCodeLine{// Signature MUST be either:}
\DoxyCodeLine{// 1) DER-\/encoded signature as hex-\/string; or}
\DoxyCodeLine{// 2) DER-\/encoded signature as buffer; or}
\DoxyCodeLine{// 3) object with two hex-\/string properties (r and s); or}
\DoxyCodeLine{// 4) object with two buffer properties (r and s)}
\DoxyCodeLine{}
\DoxyCodeLine{var signature = '3046022100...'; // case 1}
\DoxyCodeLine{var signature = new Buffer('...'); // case 2}
\DoxyCodeLine{var signature = \{ r: 'b1fc...', s: '9c42...' \}; // case 3}
\DoxyCodeLine{}
\DoxyCodeLine{// Verify signature}
\DoxyCodeLine{console.log(key.verify(msgHash, signature)); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_elliptic_README_autotoc_md8340}{}\doxysubsection{Ed\+DSA}\label{md_sfcode_node_modules_elliptic_README_autotoc_md8340}

\begin{DoxyCode}{0}
\DoxyCodeLine{var EdDSA = require('elliptic').eddsa;}
\DoxyCodeLine{}
\DoxyCodeLine{// Create and initialize EdDSA context}
\DoxyCodeLine{// (better do it once and reuse it)}
\DoxyCodeLine{var ec = new EdDSA('ed25519');}
\DoxyCodeLine{}
\DoxyCodeLine{// Create key pair from secret}
\DoxyCodeLine{var key = ec.keyFromSecret('693e3c...'); // hex string, array or Buffer}
\DoxyCodeLine{}
\DoxyCodeLine{// Sign the message's hash (input must be an array, or a hex-\/string)}
\DoxyCodeLine{var msgHash = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];}
\DoxyCodeLine{var signature = key.sign(msgHash).toHex();}
\DoxyCodeLine{}
\DoxyCodeLine{// Verify signature}
\DoxyCodeLine{console.log(key.verify(msgHash, signature));}
\DoxyCodeLine{}
\DoxyCodeLine{// CHECK WITH NO PRIVATE KEY}
\DoxyCodeLine{}
\DoxyCodeLine{// Import public key}
\DoxyCodeLine{var pub = '0a1af638...';}
\DoxyCodeLine{var key = ec.keyFromPublic(pub, 'hex');}
\DoxyCodeLine{}
\DoxyCodeLine{// Verify signature}
\DoxyCodeLine{var signature = '70bed1...';}
\DoxyCodeLine{console.log(key.verify(msgHash, signature)); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_elliptic_README_autotoc_md8341}{}\doxysubsection{ECDH}\label{md_sfcode_node_modules_elliptic_README_autotoc_md8341}

\begin{DoxyCode}{0}
\DoxyCodeLine{var EC = require('elliptic').ec;}
\DoxyCodeLine{var ec = new EC('curve25519');}
\DoxyCodeLine{}
\DoxyCodeLine{// Generate keys}
\DoxyCodeLine{var key1 = ec.genKeyPair();}
\DoxyCodeLine{var key2 = ec.genKeyPair();}
\DoxyCodeLine{}
\DoxyCodeLine{var shared1 = key1.derive(key2.getPublic());}
\DoxyCodeLine{var shared2 = key2.derive(key1.getPublic());}
\DoxyCodeLine{}
\DoxyCodeLine{console.log('Both shared secrets are BN instances');}
\DoxyCodeLine{console.log(shared1.toString(16));}
\DoxyCodeLine{console.log(shared2.toString(16)); }

\end{DoxyCode}


three and more members\+: 
\begin{DoxyCode}{0}
\DoxyCodeLine{var EC = require('elliptic').ec;}
\DoxyCodeLine{var ec = new EC('curve25519');}
\DoxyCodeLine{}
\DoxyCodeLine{var A = ec.genKeyPair();}
\DoxyCodeLine{var B = ec.genKeyPair();}
\DoxyCodeLine{var C = ec.genKeyPair();}
\DoxyCodeLine{}
\DoxyCodeLine{var AB = A.getPublic().mul(B.getPrivate())}
\DoxyCodeLine{var BC = B.getPublic().mul(C.getPrivate())}
\DoxyCodeLine{var CA = C.getPublic().mul(A.getPrivate())}
\DoxyCodeLine{}
\DoxyCodeLine{var ABC = AB.mul(C.getPrivate())}
\DoxyCodeLine{var BCA = BC.mul(A.getPrivate())}
\DoxyCodeLine{var CAB = CA.mul(B.getPrivate())}
\DoxyCodeLine{}
\DoxyCodeLine{console.log(ABC.getX().toString(16))}
\DoxyCodeLine{console.log(BCA.getX().toString(16))}
\DoxyCodeLine{console.log(CAB.getX().toString(16)) }

\end{DoxyCode}


NOTE\+: {\ttfamily .derive()} returns a \href{https://github.com/indutny/bn.js}{\texttt{ BN}} instance.\hypertarget{md_sfcode_node_modules_elliptic_README_autotoc_md8342}{}\doxysection{Supported curves}\label{md_sfcode_node_modules_elliptic_README_autotoc_md8342}
Elliptic.\+js support following curve types\+:


\begin{DoxyItemize}
\item Short Weierstrass
\item Montgomery
\item Edwards
\item Twisted Edwards
\end{DoxyItemize}

Following curve \textquotesingle{}presets\textquotesingle{} are embedded into the library\+:


\begin{DoxyItemize}
\item {\ttfamily secp256k1}
\item {\ttfamily p192}
\item {\ttfamily p224}
\item {\ttfamily p256}
\item {\ttfamily p384}
\item {\ttfamily p521}
\item {\ttfamily curve25519}
\item {\ttfamily ed25519}
\end{DoxyItemize}

NOTE\+: That {\ttfamily curve25519} could not be used for ECDSA, use {\ttfamily ed25519} instead.\hypertarget{md_sfcode_node_modules_elliptic_README_autotoc_md8343}{}\doxysubsection{Implementation details}\label{md_sfcode_node_modules_elliptic_README_autotoc_md8343}
ECDSA is using deterministic {\ttfamily k} value generation as per \href{http://tools.ietf.org/html/rfc6979}{\texttt{ 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 \href{https://github.com/indutny/bn.js}{\texttt{ bn.\+js}}, hashing is provided by \href{https://github.com/indutny/hash.js}{\texttt{ hash.\+js}}\hypertarget{md_sfcode_node_modules_elliptic_README_autotoc_md8344}{}\doxysubsection{Related projects}\label{md_sfcode_node_modules_elliptic_README_autotoc_md8344}

\begin{DoxyItemize}
\item \href{https://github.com/bitchan/eccrypto}{\texttt{ eccrypto}}\+: isomorphic implementation of ECDSA, ECDH and ECIES for both browserify and node (uses {\ttfamily elliptic} for browser and \href{https://github.com/wanderer/secp256k1-node}{\texttt{ secp256k1-\/node}} for node)
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_elliptic_README_autotoc_md8345}{}\doxysubsubsection{LICENSE}\label{md_sfcode_node_modules_elliptic_README_autotoc_md8345}
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 \char`\"{}\+Software\char`\"{}), 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 \char`\"{}\+AS IS\char`\"{}, 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. 