update sign function to use elliptic
This commit is contained in:
parent
8fb6ccaf01
commit
ac4d3186bf
|
@ -101,45 +101,40 @@ Key.prototype.regenerateSync = function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
Key.prototype.signSync = function(hash) {
|
Key.prototype.signSync = function(hash) {
|
||||||
|
/*
|
||||||
var getSECCurveByName = require('../../browser/vendor-bundle.js').getSECCurveByName;
|
var getSECCurveByName = require('../../browser/vendor-bundle.js').getSECCurveByName;
|
||||||
var BigInteger = require('../../browser/vendor-bundle.js').BigInteger;
|
var BigInteger = require('../../browser/vendor-bundle.js').BigInteger;
|
||||||
var rng = new SecureRandom();
|
var rng = new SecureRandom();
|
||||||
var ecparams = getSECCurveByName('secp256k1');
|
var ecparams = getSECCurveByName('secp256k1');
|
||||||
|
*/
|
||||||
|
var ec = elliptic.curves.secp256k1;
|
||||||
|
|
||||||
var rng = {};
|
var genk = function() {
|
||||||
rng.nextBytes = function(array) {
|
//TODO: account for when >= n
|
||||||
var buf = SecureRandom.getRandomBuffer(array.length);
|
return new bignum(SecureRandom.getRandomBuffer(8));
|
||||||
var a = bufferToArray(SecureRandom.getRandomBuffer(array.length));
|
|
||||||
for (var i in a) {
|
|
||||||
array[i] = a[i];
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var getBigRandom = function(limit) {
|
|
||||||
return new BigInteger(limit.bitLength(), rng)
|
|
||||||
.mod(limit.subtract(BigInteger.ONE))
|
|
||||||
.add(BigInteger.ONE);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var sign = function(hash, priv) {
|
var sign = function(hash, priv) {
|
||||||
var d = priv;
|
var d = priv;
|
||||||
var n = ecparams.getN();
|
//var n = ecparams.getN();
|
||||||
var e = BigInteger.fromByteArrayUnsigned(hash);
|
var n = ec.n;
|
||||||
|
//var e = BigInteger.fromByteArrayUnsigned(hash);
|
||||||
|
var e = new bignum(hash);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
var k = getBigRandom(n);
|
var k = genk();
|
||||||
var G = ecparams.getG();
|
var G = ec.g;
|
||||||
var Q = G.multiply(k);
|
var Q = G.mul(k);
|
||||||
var r = Q.getX().toBigInteger().mod(n);
|
var r = Q.getX().mod(n);
|
||||||
var s = k.modInverse(n).multiply(e.add(d.multiply(r))).mod(n);
|
var s = k.invm(n).mul(e.add(d.mul(r))).mod(n);
|
||||||
} while (r.compareTo(BigInteger.ZERO) <= 0 || s.compareTo(BigInteger.ZERO) <= 0);
|
} while (r.cmp(new bignum(0)) <= 0 || s.cmp(new bignum(0)) <= 0);
|
||||||
|
|
||||||
return serializeSig(r, s);
|
return serializeSig(r, s);
|
||||||
};
|
};
|
||||||
|
|
||||||
var serializeSig = function(r, s) {
|
var serializeSig = function(r, s) {
|
||||||
var rBa = r.toByteArraySigned();
|
var rBa = r.toArray();
|
||||||
var sBa = s.toByteArraySigned();
|
var sBa = s.toArray();
|
||||||
|
|
||||||
var sequence = [];
|
var sequence = [];
|
||||||
sequence.push(0x02); // INTEGER
|
sequence.push(0x02); // INTEGER
|
||||||
|
@ -163,9 +158,11 @@ Key.prototype.signSync = function(hash) {
|
||||||
if (!Buffer.isBuffer(hash) || hash.length !== 32) {
|
if (!Buffer.isBuffer(hash) || hash.length !== 32) {
|
||||||
throw new Error('Arg should be a 32 bytes hash buffer');
|
throw new Error('Arg should be a 32 bytes hash buffer');
|
||||||
}
|
}
|
||||||
var privhex = this.private.toString('hex');
|
//var privhex = this.private.toString('hex');
|
||||||
var privnum = new BigInteger(privhex, 16);
|
//var privnum = new BigInteger(privhex, 16);
|
||||||
var signature = sign(bufferToArray(hash), privnum);
|
var privnum = new bignum(this.private);
|
||||||
|
//var signature = sign(bufferToArray(hash), privnum);
|
||||||
|
var signature = sign(hash, privnum);
|
||||||
|
|
||||||
return new Buffer(signature);
|
return new Buffer(signature);
|
||||||
};
|
};
|
||||||
|
|
|
@ -31,6 +31,17 @@ if (typeof process == 'undefined' || typeof process.versions == 'undefined') {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#sub', function() {
|
||||||
|
|
||||||
|
it('should subtract a small number', function() {
|
||||||
|
var bn1 = new Bignum(50);
|
||||||
|
var bn2 = new Bignum(25);
|
||||||
|
var bn3 = bn1.sub(bn2);
|
||||||
|
bn3.toString().should.equal('25');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
describe('#gt', function() {
|
describe('#gt', function() {
|
||||||
|
|
||||||
it('should say 1 is greater than 0', function() {
|
it('should say 1 is greater than 0', function() {
|
||||||
|
|
Loading…
Reference in New Issue