|
|
@ -6,10 +6,10 @@ var Privkey = require('./privkey'); |
|
|
|
var Pubkey = require('./pubkey'); |
|
|
|
var Random = require('./random'); |
|
|
|
|
|
|
|
var ECDSA = function ECDSA(hash, key, sig, k) { |
|
|
|
var ECDSA = function ECDSA(hashbuf, key, sig, k) { |
|
|
|
if (!(this instanceof ECDSA)) |
|
|
|
return new ECDSA(hash, key, sig, k); |
|
|
|
this.hash = hash; |
|
|
|
return new ECDSA(hashbuf, key, sig, k); |
|
|
|
this.hashbuf = hashbuf; |
|
|
|
this.key = key; |
|
|
|
this.sig = sig; |
|
|
|
this.k = k; |
|
|
@ -36,8 +36,8 @@ ECDSA.prototype.calci = function() { |
|
|
|
|
|
|
|
ECDSA.prototype.fromString = function(str) { |
|
|
|
var obj = JSON.parse(str); |
|
|
|
if (obj.hash) |
|
|
|
this.hash = new Buffer(obj.hash, 'hex'); |
|
|
|
if (obj.hashbuf) |
|
|
|
this.hashbuf = new Buffer(obj.hashbuf, 'hex'); |
|
|
|
if (obj.key) |
|
|
|
this.key = Key().fromString(obj.key); |
|
|
|
if (obj.sig) |
|
|
@ -62,7 +62,7 @@ ECDSA.prototype.sig2pubkey = function() { |
|
|
|
if (!(i === 0 || i === 1 || i === 2 || i === 3)) |
|
|
|
throw new Error('signature: i must be equal to 0, 1, 2, or 3'); |
|
|
|
|
|
|
|
var e = BN().fromBuffer(this.hash); |
|
|
|
var e = BN().fromBuffer(this.hashbuf); |
|
|
|
var r = this.sig.r; |
|
|
|
var s = this.sig.s; |
|
|
|
|
|
|
@ -103,7 +103,7 @@ ECDSA.prototype.sig2pubkey = function() { |
|
|
|
}; |
|
|
|
|
|
|
|
ECDSA.prototype.sigError = function() { |
|
|
|
if (!Buffer.isBuffer(this.hash) || this.hash.length !== 32) |
|
|
|
if (!Buffer.isBuffer(this.hashbuf) || this.hashbuf.length !== 32) |
|
|
|
return 'Invalid hash'; |
|
|
|
|
|
|
|
try { |
|
|
@ -118,7 +118,7 @@ ECDSA.prototype.sigError = function() { |
|
|
|
|| !(s.gt(0) && s.lt(Point.getN()))) |
|
|
|
return 'r and s not in range'; |
|
|
|
|
|
|
|
var e = BN().fromBuffer(this.hash); |
|
|
|
var e = BN().fromBuffer(this.hashbuf); |
|
|
|
var n = Point.getN(); |
|
|
|
var sinv = s.invm(n); |
|
|
|
var u1 = sinv.mul(e).mod(n); |
|
|
@ -135,17 +135,17 @@ ECDSA.prototype.sigError = function() { |
|
|
|
}; |
|
|
|
|
|
|
|
ECDSA.prototype.sign = function() { |
|
|
|
var hash = this.hash; |
|
|
|
var hashbuf = this.hashbuf; |
|
|
|
var privkey = this.key.privkey; |
|
|
|
var k = this.k; |
|
|
|
var d = privkey.bn; |
|
|
|
|
|
|
|
if (!hash || !privkey || !k || !d) |
|
|
|
if (!hashbuf || !privkey || !k || !d) |
|
|
|
throw new Error('ecdsa: invalid parameters'); |
|
|
|
|
|
|
|
var N = Point.getN(); |
|
|
|
var G = Point.getG(); |
|
|
|
var e = BN().fromBuffer(hash); |
|
|
|
var e = BN().fromBuffer(hashbuf); |
|
|
|
|
|
|
|
do { |
|
|
|
var Q = G.mul(k); |
|
|
@ -164,8 +164,8 @@ ECDSA.prototype.signRandomK = function() { |
|
|
|
|
|
|
|
ECDSA.prototype.toString = function() { |
|
|
|
var obj = {}; |
|
|
|
if (this.hash) |
|
|
|
obj.hash = this.hash.toString('hex'); |
|
|
|
if (this.hashbuf) |
|
|
|
obj.hashbuf = this.hashbuf.toString('hex'); |
|
|
|
if (this.key) |
|
|
|
obj.key = this.key.toString(); |
|
|
|
if (this.sig) |
|
|
|