Permalink
Browse files

update sign function to use elliptic

  • Loading branch information...
Ryan X. Charles
Ryan X. Charles committed Jul 5, 2014
1 parent 8fb6cca commit ac4d3186bfbb4df2aee4389d1a51e488df08b52a
Showing with 34 additions and 26 deletions.
  1. +23 −26 lib/browser/Key.js
  2. +11 −0 test/test.Bignum.browser.js
@@ -101,45 +101,40 @@ Key.prototype.regenerateSync = function() {
};

Key.prototype.signSync = function(hash) {
/*
var getSECCurveByName = require('../../browser/vendor-bundle.js').getSECCurveByName;
var BigInteger = require('../../browser/vendor-bundle.js').BigInteger;
var rng = new SecureRandom();
var ecparams = getSECCurveByName('secp256k1');
*/
var ec = elliptic.curves.secp256k1;

var rng = {};
rng.nextBytes = function(array) {
var buf = SecureRandom.getRandomBuffer(array.length);
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 genk = function() {
//TODO: account for when >= n
return new bignum(SecureRandom.getRandomBuffer(8));
};

var sign = function(hash, priv) {
var d = priv;
var n = ecparams.getN();
var e = BigInteger.fromByteArrayUnsigned(hash);
//var n = ecparams.getN();
var n = ec.n;
//var e = BigInteger.fromByteArrayUnsigned(hash);
var e = new bignum(hash);

do {
var k = getBigRandom(n);
var G = ecparams.getG();
var Q = G.multiply(k);
var r = Q.getX().toBigInteger().mod(n);
var s = k.modInverse(n).multiply(e.add(d.multiply(r))).mod(n);
} while (r.compareTo(BigInteger.ZERO) <= 0 || s.compareTo(BigInteger.ZERO) <= 0);
var k = genk();
var G = ec.g;
var Q = G.mul(k);
var r = Q.getX().mod(n);
var s = k.invm(n).mul(e.add(d.mul(r))).mod(n);
} while (r.cmp(new bignum(0)) <= 0 || s.cmp(new bignum(0)) <= 0);

return serializeSig(r, s);
};

var serializeSig = function(r, s) {
var rBa = r.toByteArraySigned();
var sBa = s.toByteArraySigned();
var rBa = r.toArray();
var sBa = s.toArray();

var sequence = [];
sequence.push(0x02); // INTEGER
@@ -163,9 +158,11 @@ Key.prototype.signSync = function(hash) {
if (!Buffer.isBuffer(hash) || hash.length !== 32) {
throw new Error('Arg should be a 32 bytes hash buffer');
}
var privhex = this.private.toString('hex');
var privnum = new BigInteger(privhex, 16);
var signature = sign(bufferToArray(hash), privnum);
//var privhex = this.private.toString('hex');
//var privnum = new BigInteger(privhex, 16);
var privnum = new bignum(this.private);
//var signature = sign(bufferToArray(hash), privnum);
var signature = sign(hash, privnum);

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() {

it('should say 1 is greater than 0', function() {

0 comments on commit ac4d318

Please sign in to comment.