From 9510af10e3e9e789ffa4378d977f37e10b3efa26 Mon Sep 17 00:00:00 2001
From: Daniel Cousens <github@dcousens.com>
Date: Thu, 16 Oct 2014 01:25:39 +1100
Subject: [PATCH] EC*Key: add .curve static property for public API

---
 src/eckey.js     | 13 ++++++++-----
 src/ecpubkey.js  |  9 ++++++---
 test/eckey.js    | 16 ++++++++++++++++
 test/ecpubkey.js | 15 +++++++++++++++
 4 files changed, 45 insertions(+), 8 deletions(-)

diff --git a/src/eckey.js b/src/eckey.js
index 23cd43d..501384d 100644
--- a/src/eckey.js
+++ b/src/eckey.js
@@ -9,18 +9,21 @@ var BigInteger = require('bigi')
 var ECPubKey = require('./ecpubkey')
 
 var ecurve = require('ecurve')
-var curve = ecurve.getCurveByName('secp256k1')
+var secp256k1 = ecurve.getCurveByName('secp256k1')
 
 function ECKey(d, compressed) {
   assert(d.signum() > 0, 'Private key must be greater than 0')
-  assert(d.compareTo(curve.n) < 0, 'Private key must be less than the curve order')
+  assert(d.compareTo(ECKey.curve.n) < 0, 'Private key must be less than the curve order')
 
-  var Q = curve.G.multiply(d)
+  var Q = ECKey.curve.G.multiply(d)
 
   this.d = d
   this.pub = new ECPubKey(Q, compressed)
 }
 
+// Constants
+ECKey.curve = secp256k1
+
 // Static constructors
 ECKey.fromWIF = function(string) {
   var payload = base58check.decode(string)
@@ -51,7 +54,7 @@ ECKey.makeRandom = function(compressed, rng) {
   assert.equal(buffer.length, 32, 'Expected 256-bit Buffer from RNG')
 
   var d = BigInteger.fromBuffer(buffer)
-  d = d.mod(curve.n)
+  d = d.mod(ECKey.curve.n)
 
   return new ECKey(d, compressed)
 }
@@ -75,7 +78,7 @@ ECKey.prototype.toWIF = function(network) {
 
 // Operations
 ECKey.prototype.sign = function(hash) {
-  return ecdsa.sign(curve, hash, this.d)
+  return ecdsa.sign(ECKey.curve, hash, this.d)
 }
 
 module.exports = ECKey
diff --git a/src/ecpubkey.js b/src/ecpubkey.js
index 840fa50..e2b25bc 100644
--- a/src/ecpubkey.js
+++ b/src/ecpubkey.js
@@ -6,7 +6,7 @@ var networks = require('./networks')
 var Address = require('./address')
 
 var ecurve = require('ecurve')
-var curve = ecurve.getCurveByName('secp256k1')
+var secp256k1 = ecurve.getCurveByName('secp256k1')
 
 function ECPubKey(Q, compressed) {
   if (compressed === undefined) compressed = true
@@ -18,9 +18,12 @@ function ECPubKey(Q, compressed) {
   this.Q = Q
 }
 
+// Constants
+ECPubKey.curve = secp256k1
+
 // Static constructors
 ECPubKey.fromBuffer = function(buffer) {
-  var Q = ecurve.Point.decodeFrom(curve, buffer)
+  var Q = ecurve.Point.decodeFrom(ECPubKey.curve, buffer)
   return new ECPubKey(Q, Q.compressed)
 }
 
@@ -36,7 +39,7 @@ ECPubKey.prototype.getAddress = function(network) {
 }
 
 ECPubKey.prototype.verify = function(hash, signature) {
-  return ecdsa.verify(curve, hash, signature, this.Q)
+  return ecdsa.verify(ECPubKey.curve, hash, signature, this.Q)
 }
 
 // Export functions
diff --git a/test/eckey.js b/test/eckey.js
index 849dd7e..271e5f7 100644
--- a/test/eckey.js
+++ b/test/eckey.js
@@ -1,5 +1,6 @@
 var assert = require('assert')
 var crypto = require('crypto')
+var ecurve = require('ecurve')
 var networks = require('../src/networks')
 var sinon = require('sinon')
 
@@ -42,6 +43,21 @@ describe('ECKey', function() {
     })
   })
 
+  it('uses the secp256k1 curve by default', function() {
+    var secp256k1 = ecurve.getCurveByName('secp256k1')
+
+    for (var property in secp256k1) {
+      // FIXME: circular structures in ecurve
+      if (property === 'G') continue
+      if (property === 'infinity') continue
+
+      var actual = ECKey.curve[property]
+      var expected = secp256k1[property]
+
+      assert.deepEqual(actual, expected)
+    }
+  })
+
   describe('fromWIF', function() {
     fixtures.valid.forEach(function(f) {
       f.WIFs.forEach(function(wif) {
diff --git a/test/ecpubkey.js b/test/ecpubkey.js
index 35302f5..bf4ac5a 100644
--- a/test/ecpubkey.js
+++ b/test/ecpubkey.js
@@ -35,6 +35,21 @@ describe('ECPubKey', function() {
     })
   })
 
+  it('uses the secp256k1 curve by default', function() {
+    var secp256k1 = ecurve.getCurveByName('secp256k1')
+
+    for (var property in secp256k1) {
+      // FIXME: circular structures in ecurve
+      if (property === 'G') continue
+      if (property === 'infinity') continue
+
+      var actual = ECPubKey.curve[property]
+      var expected = secp256k1[property]
+
+      assert.deepEqual(actual, expected)
+    }
+  })
+
   describe('fromHex/toHex', function() {
     it('supports compressed points', function() {
       var pubKey = ECPubKey.fromHex(fixtures.compressed.hex)