You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
7.0 KiB

4 years ago
  1. # Elliptic [![Build Status](https://secure.travis-ci.org/indutny/elliptic.png)](http://travis-ci.org/indutny/elliptic) [![Coverage Status](https://coveralls.io/repos/indutny/elliptic/badge.svg?branch=master&service=github)](https://coveralls.io/github/indutny/elliptic?branch=master) [![Code Climate](https://codeclimate.com/github/indutny/elliptic/badges/gpa.svg)](https://codeclimate.com/github/indutny/elliptic)
  2. [![Saucelabs Test Status](https://saucelabs.com/browser-matrix/gh-indutny-elliptic.svg)](https://saucelabs.com/u/gh-indutny-elliptic)
  3. Fast elliptic-curve cryptography in a plain javascript implementation.
  4. NOTE: Please take a look at http://safecurves.cr.yp.to/ before choosing a curve
  5. for your cryptography operations.
  6. ## Incentive
  7. ECC is much slower than regular RSA cryptography, the JS implementations are
  8. even more slower.
  9. ## Benchmarks
  10. ```bash
  11. $ node benchmarks/index.js
  12. Benchmarking: sign
  13. elliptic#sign x 262 ops/sec ±0.51% (177 runs sampled)
  14. eccjs#sign x 55.91 ops/sec ±0.90% (144 runs sampled)
  15. ------------------------
  16. Fastest is elliptic#sign
  17. ========================
  18. Benchmarking: verify
  19. elliptic#verify x 113 ops/sec ±0.50% (166 runs sampled)
  20. eccjs#verify x 48.56 ops/sec ±0.36% (125 runs sampled)
  21. ------------------------
  22. Fastest is elliptic#verify
  23. ========================
  24. Benchmarking: gen
  25. elliptic#gen x 294 ops/sec ±0.43% (176 runs sampled)
  26. eccjs#gen x 62.25 ops/sec ±0.63% (129 runs sampled)
  27. ------------------------
  28. Fastest is elliptic#gen
  29. ========================
  30. Benchmarking: ecdh
  31. elliptic#ecdh x 136 ops/sec ±0.85% (156 runs sampled)
  32. ------------------------
  33. Fastest is elliptic#ecdh
  34. ========================
  35. ```
  36. ## API
  37. ### ECDSA
  38. ```javascript
  39. var EC = require('elliptic').ec;
  40. // Create and initialize EC context
  41. // (better do it once and reuse it)
  42. var ec = new EC('secp256k1');
  43. // Generate keys
  44. var key = ec.genKeyPair();
  45. // Sign the message's hash (input must be an array, or a hex-string)
  46. var msgHash = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
  47. var signature = key.sign(msgHash);
  48. // Export DER encoded signature in Array
  49. var derSign = signature.toDER();
  50. // Verify signature
  51. console.log(key.verify(msgHash, derSign));
  52. // CHECK WITH NO PRIVATE KEY
  53. var pubPoint = key.getPublic();
  54. var x = pubPoint.getX();
  55. var y = pubPoint.getY();
  56. // Public Key MUST be either:
  57. // 1) '04' + hex string of x + hex string of y; or
  58. // 2) object with two hex string properties (x and y); or
  59. // 3) object with two buffer properties (x and y)
  60. var pub = pubPoint.encode('hex'); // case 1
  61. var pub = { x: x.toString('hex'), y: y.toString('hex') }; // case 2
  62. var pub = { x: x.toBuffer(), y: y.toBuffer() }; // case 3
  63. var pub = { x: x.toArrayLike(Buffer), y: y.toArrayLike(Buffer) }; // case 3
  64. // Import public key
  65. var key = ec.keyFromPublic(pub, 'hex');
  66. // Signature MUST be either:
  67. // 1) DER-encoded signature as hex-string; or
  68. // 2) DER-encoded signature as buffer; or
  69. // 3) object with two hex-string properties (r and s); or
  70. // 4) object with two buffer properties (r and s)
  71. var signature = '3046022100...'; // case 1
  72. var signature = new Buffer('...'); // case 2
  73. var signature = { r: 'b1fc...', s: '9c42...' }; // case 3
  74. // Verify signature
  75. console.log(key.verify(msgHash, signature));
  76. ```
  77. ### EdDSA
  78. ```javascript
  79. var EdDSA = require('elliptic').eddsa;
  80. // Create and initialize EdDSA context
  81. // (better do it once and reuse it)
  82. var ec = new EdDSA('ed25519');
  83. // Create key pair from secret
  84. var key = ec.keyFromSecret('693e3c...'); // hex string, array or Buffer
  85. // Sign the message's hash (input must be an array, or a hex-string)
  86. var msgHash = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
  87. var signature = key.sign(msgHash).toHex();
  88. // Verify signature
  89. console.log(key.verify(msgHash, signature));
  90. // CHECK WITH NO PRIVATE KEY
  91. // Import public key
  92. var pub = '0a1af638...';
  93. var key = ec.keyFromPublic(pub, 'hex');
  94. // Verify signature
  95. var signature = '70bed1...';
  96. console.log(key.verify(msgHash, signature));
  97. ```
  98. ### ECDH
  99. ```javascript
  100. var EC = require('elliptic').ec;
  101. var ec = new EC('curve25519');
  102. // Generate keys
  103. var key1 = ec.genKeyPair();
  104. var key2 = ec.genKeyPair();
  105. var shared1 = key1.derive(key2.getPublic());
  106. var shared2 = key2.derive(key1.getPublic());
  107. console.log('Both shared secrets are BN instances');
  108. console.log(shared1.toString(16));
  109. console.log(shared2.toString(16));
  110. ```
  111. three and more members:
  112. ```javascript
  113. var EC = require('elliptic').ec;
  114. var ec = new EC('curve25519');
  115. var A = ec.genKeyPair();
  116. var B = ec.genKeyPair();
  117. var C = ec.genKeyPair();
  118. var AB = A.getPublic().mul(B.getPrivate())
  119. var BC = B.getPublic().mul(C.getPrivate())
  120. var CA = C.getPublic().mul(A.getPrivate())
  121. var ABC = AB.mul(C.getPrivate())
  122. var BCA = BC.mul(A.getPrivate())
  123. var CAB = CA.mul(B.getPrivate())
  124. console.log(ABC.getX().toString(16))
  125. console.log(BCA.getX().toString(16))
  126. console.log(CAB.getX().toString(16))
  127. ```
  128. NOTE: `.derive()` returns a [BN][1] instance.
  129. ## Supported curves
  130. Elliptic.js support following curve types:
  131. * Short Weierstrass
  132. * Montgomery
  133. * Edwards
  134. * Twisted Edwards
  135. Following curve 'presets' are embedded into the library:
  136. * `secp256k1`
  137. * `p192`
  138. * `p224`
  139. * `p256`
  140. * `p384`
  141. * `p521`
  142. * `curve25519`
  143. * `ed25519`
  144. NOTE: That `curve25519` could not be used for ECDSA, use `ed25519` instead.
  145. ### Implementation details
  146. ECDSA is using deterministic `k` value generation as per [RFC6979][0]. Most of
  147. the curve operations are performed on non-affine coordinates (either projective
  148. or extended), various windowing techniques are used for different cases.
  149. All operations are performed in reduction context using [bn.js][1], hashing is
  150. provided by [hash.js][2]
  151. ### Related projects
  152. * [eccrypto][3]: isomorphic implementation of ECDSA, ECDH and ECIES for both
  153. browserify and node (uses `elliptic` for browser and [secp256k1-node][4] for
  154. node)
  155. #### LICENSE
  156. This software is licensed under the MIT License.
  157. Copyright Fedor Indutny, 2014.
  158. Permission is hereby granted, free of charge, to any person obtaining a
  159. copy of this software and associated documentation files (the
  160. "Software"), to deal in the Software without restriction, including
  161. without limitation the rights to use, copy, modify, merge, publish,
  162. distribute, sublicense, and/or sell copies of the Software, and to permit
  163. persons to whom the Software is furnished to do so, subject to the
  164. following conditions:
  165. The above copyright notice and this permission notice shall be included
  166. in all copies or substantial portions of the Software.
  167. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  168. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  169. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  170. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  171. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  172. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  173. USE OR OTHER DEALINGS IN THE SOFTWARE.
  174. [0]: http://tools.ietf.org/html/rfc6979
  175. [1]: https://github.com/indutny/bn.js
  176. [2]: https://github.com/indutny/hash.js
  177. [3]: https://github.com/bitchan/eccrypto
  178. [4]: https://github.com/wanderer/secp256k1-node