Crypto: Update ECDSA with to use PrivateKey and PublicKey

This commit is contained in:
Braydon Fuller 2014-11-25 15:10:22 -05:00
parent f6e610dd28
commit 2a2dc8196d
3 changed files with 9 additions and 24 deletions

View File

@ -25,7 +25,7 @@ ECDSA.prototype.set = function(obj) {
}; };
ECDSA.prototype.privkey2pubkey = function(){ ECDSA.prototype.privkey2pubkey = function(){
this.pubkey = PublicKey().fromPrivateKey(this.privkey); this.pubkey = PublicKey.fromPrivateKey(this.privkey);
}; };
ECDSA.prototype.calci = function() { ECDSA.prototype.calci = function() {
@ -35,6 +35,7 @@ ECDSA.prototype.calci = function() {
try { try {
Qprime = this.sig2pubkey(); Qprime = this.sig2pubkey();
} catch (e) { } catch (e) {
console.log(e);
continue; continue;
} }
if (Qprime.point.eq(this.pubkey.point)) { if (Qprime.point.eq(this.pubkey.point)) {
@ -52,9 +53,9 @@ ECDSA.prototype.fromString = function(str) {
if (obj.hashbuf) if (obj.hashbuf)
this.hashbuf = new Buffer(obj.hashbuf, 'hex'); this.hashbuf = new Buffer(obj.hashbuf, 'hex');
if (obj.pubkey) if (obj.pubkey)
this.pubkey = PublicKey().fromString(obj.pubkey); this.pubkey = PublicKey.fromString(obj.pubkey);
if (obj.privkey) if (obj.privkey)
this.privkey = PrivateKey().fromString(obj.privkey); this.privkey = PrivateKey.fromString(obj.privkey);
if (obj.sig) if (obj.sig)
this.sig = Signature().fromString(obj.sig); this.sig = Signature().fromString(obj.sig);
if (obj.k) if (obj.k)
@ -114,9 +115,7 @@ ECDSA.prototype.sig2pubkey = function() {
//var Q = R.multiplyTwo(s, G, eNeg).mul(rInv); //var Q = R.multiplyTwo(s, G, eNeg).mul(rInv);
var Q = R.mul(s).add(G.mul(eNeg)).mul(rInv); var Q = R.mul(s).add(G.mul(eNeg)).mul(rInv);
var pubkey = new PublicKey({point: Q}); var pubkey = PublicKey.fromPoint(Q, this.sig.compressed);
pubkey.compressed = this.sig.compressed;
pubkey.validate();
return pubkey; return pubkey;
}; };
@ -125,12 +124,6 @@ ECDSA.prototype.sigError = function() {
if (!Buffer.isBuffer(this.hashbuf) || this.hashbuf.length !== 32) if (!Buffer.isBuffer(this.hashbuf) || this.hashbuf.length !== 32)
return 'hashbuf must be a 32 byte buffer'; return 'hashbuf must be a 32 byte buffer';
try {
this.pubkey.validate();
} catch (e) {
return 'Invalid pubkey: ' + e;
}
var r = this.sig.r; var r = this.sig.r;
var s = this.sig.s; var s = this.sig.s;
if (!(r.gt(0) && r.lt(Point.getN())) if (!(r.gt(0) && r.lt(Point.getN()))

View File

@ -186,11 +186,11 @@ PublicKey.fromBuffer = function(buf) {
* @param {Point} point - A Point instance * @param {Point} point - A Point instance
* @returns {PublicKey} A new valid instance of PublicKey * @returns {PublicKey} A new valid instance of PublicKey
*/ */
PublicKey.fromPoint = function(point){ PublicKey.fromPoint = function(point, compressed){
if (!(point instanceof Point)) { if (!(point instanceof Point)) {
throw new TypeError('First argument must be an instance of Point.'); throw new TypeError('First argument must be an instance of Point.');
} }
return new PublicKey(point); return new PublicKey(point, compressed);
}; };
/** /**

View File

@ -8,7 +8,6 @@ var PrivateKey = bitcore.PrivateKey;
var PublicKey = bitcore.PublicKey; var PublicKey = bitcore.PublicKey;
var Signature = bitcore.Signature; var Signature = bitcore.Signature;
var BN = bitcore.crypto.BN; var BN = bitcore.crypto.BN;
var Point = bitcore.crypto.Point;
describe('ECDSA', function() { describe('ECDSA', function() {
@ -20,7 +19,7 @@ describe('ECDSA', function() {
var ecdsa = new ECDSA(); var ecdsa = new ECDSA();
ecdsa.hashbuf = Hash.sha256(new Buffer('test data')); ecdsa.hashbuf = Hash.sha256(new Buffer('test data'));
ecdsa.privkey = new PrivateKey(BN().fromBuffer(new Buffer('fee0a1f7afebf9d2a5a80c0c98a31c709681cce195cbcd06342b517970c0be1e', 'hex'))); ecdsa.privkey = new PrivateKey(BN().fromBuffer(new Buffer('fee0a1f7afebf9d2a5a80c0c98a31c709681cce195cbcd06342b517970c0be1e', 'hex')));
ecdsa.pubkey = new PublicKey(Point(BN().fromBuffer(new Buffer('ac242d242d23be966085a2b2b893d989f824e06c9ad0395a8a52f055ba39abb2', 'hex')))); ecdsa.privkey2pubkey();
describe('#set', function() { describe('#set', function() {
@ -105,17 +104,10 @@ describe('ECDSA', function() {
ecdsa.sigError().should.equal('hashbuf must be a 32 byte buffer'); ecdsa.sigError().should.equal('hashbuf must be a 32 byte buffer');
}); });
it('should return an error if the pubkey is invalid', function() {
var ecdsa = new ECDSA();
ecdsa.hashbuf = Hash.sha256(new Buffer('test'));
ecdsa.sigError().indexOf("Invalid pubkey").should.equal(0);
});
it('should return an error if r, s are invalid', function() { it('should return an error if r, s are invalid', function() {
var ecdsa = new ECDSA(); var ecdsa = new ECDSA();
ecdsa.hashbuf = Hash.sha256(new Buffer('test')); ecdsa.hashbuf = Hash.sha256(new Buffer('test'));
var pk = new PublicKey(); var pk = PublicKey.fromDER(new Buffer('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex'));
pk.fromDER(new Buffer('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex'));
ecdsa.pubkey = pk; ecdsa.pubkey = pk;
ecdsa.sig = new Signature(); ecdsa.sig = new Signature();
ecdsa.sig.r = BN(0); ecdsa.sig.r = BN(0);