ecurve: upgrade to 0.9.0
This commit is contained in:
parent
cfe5436394
commit
f42993297c
6 changed files with 19 additions and 19 deletions
|
@ -71,7 +71,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bigi": "1.1.0",
|
"bigi": "1.1.0",
|
||||||
"crypto-js": "3.1.2-3",
|
"crypto-js": "3.1.2-3",
|
||||||
"ecurve": "0.7.0",
|
"ecurve": "0.9.0",
|
||||||
"secure-random": "0.2.1"
|
"secure-random": "0.2.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
14
src/ecdsa.js
14
src/ecdsa.js
|
@ -23,7 +23,7 @@ function deterministicGenerateK(curve, hash, d) {
|
||||||
v = crypto.HmacSHA256(v, k)
|
v = crypto.HmacSHA256(v, k)
|
||||||
v = crypto.HmacSHA256(v, k)
|
v = crypto.HmacSHA256(v, k)
|
||||||
|
|
||||||
var n = curve.params.n
|
var n = curve.n
|
||||||
var kB = BigInteger.fromBuffer(v).mod(n)
|
var kB = BigInteger.fromBuffer(v).mod(n)
|
||||||
assert(kB.compareTo(BigInteger.ONE) > 0, 'Invalid k value')
|
assert(kB.compareTo(BigInteger.ONE) > 0, 'Invalid k value')
|
||||||
assert(kB.compareTo(n) < 0, 'Invalid k value')
|
assert(kB.compareTo(n) < 0, 'Invalid k value')
|
||||||
|
@ -34,8 +34,8 @@ function deterministicGenerateK(curve, hash, d) {
|
||||||
function sign(curve, hash, d) {
|
function sign(curve, hash, d) {
|
||||||
var k = deterministicGenerateK(curve, hash, d)
|
var k = deterministicGenerateK(curve, hash, d)
|
||||||
|
|
||||||
var n = curve.params.n
|
var n = curve.n
|
||||||
var G = curve.params.G
|
var G = curve.G
|
||||||
var Q = G.multiply(k)
|
var Q = G.multiply(k)
|
||||||
var e = BigInteger.fromBuffer(hash)
|
var e = BigInteger.fromBuffer(hash)
|
||||||
|
|
||||||
|
@ -62,8 +62,8 @@ function verify(curve, hash, signature, Q) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function verifyRaw(curve, e, signature, Q) {
|
function verifyRaw(curve, e, signature, Q) {
|
||||||
var n = curve.params.n
|
var n = curve.n
|
||||||
var G = curve.params.G
|
var G = curve.G
|
||||||
|
|
||||||
var r = signature.r
|
var r = signature.r
|
||||||
var s = signature.s
|
var s = signature.s
|
||||||
|
@ -104,8 +104,8 @@ function recoverPubKey(curve, e, signature, i) {
|
||||||
// first or second candidate key.
|
// first or second candidate key.
|
||||||
var isSecondKey = i >> 1
|
var isSecondKey = i >> 1
|
||||||
|
|
||||||
var n = curve.params.n
|
var n = curve.n
|
||||||
var G = curve.params.G
|
var G = curve.G
|
||||||
var p = curve.p
|
var p = curve.p
|
||||||
var a = curve.a
|
var a = curve.a
|
||||||
var b = curve.b
|
var b = curve.b
|
||||||
|
|
|
@ -12,9 +12,9 @@ var curve = ecurve.getCurveByName('secp256k1')
|
||||||
|
|
||||||
function ECKey(d, compressed) {
|
function ECKey(d, compressed) {
|
||||||
assert(d.signum() > 0, 'Private key must be greater than 0')
|
assert(d.signum() > 0, 'Private key must be greater than 0')
|
||||||
assert(d.compareTo(curve.params.n) < 0, 'Private key must be less than the curve order')
|
assert(d.compareTo(curve.n) < 0, 'Private key must be less than the curve order')
|
||||||
|
|
||||||
var Q = curve.params.G.multiply(d)
|
var Q = curve.G.multiply(d)
|
||||||
|
|
||||||
this.d = d
|
this.d = d
|
||||||
this.pub = new ECPubKey(Q, compressed)
|
this.pub = new ECPubKey(Q, compressed)
|
||||||
|
@ -47,7 +47,7 @@ ECKey.makeRandom = function(compressed, rng) {
|
||||||
|
|
||||||
var buffer = new Buffer(rng(32))
|
var buffer = new Buffer(rng(32))
|
||||||
var d = BigInteger.fromBuffer(buffer)
|
var d = BigInteger.fromBuffer(buffer)
|
||||||
d = d.mod(curve.params.n)
|
d = d.mod(curve.n)
|
||||||
|
|
||||||
return new ECKey(d, compressed)
|
return new ECKey(d, compressed)
|
||||||
}
|
}
|
||||||
|
|
|
@ -222,7 +222,7 @@ HDNode.prototype.derive = function(index) {
|
||||||
var pIL = BigInteger.fromBuffer(IL)
|
var pIL = BigInteger.fromBuffer(IL)
|
||||||
|
|
||||||
// In case parse256(IL) >= n, proceed with the next value for i
|
// In case parse256(IL) >= n, proceed with the next value for i
|
||||||
if (pIL.compareTo(curve.params.n) >= 0) {
|
if (pIL.compareTo(curve.n) >= 0) {
|
||||||
return this.derive(index + 1)
|
return this.derive(index + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -230,7 +230,7 @@ HDNode.prototype.derive = function(index) {
|
||||||
var hd
|
var hd
|
||||||
if (this.privKey) {
|
if (this.privKey) {
|
||||||
// ki = parse256(IL) + kpar (mod n)
|
// ki = parse256(IL) + kpar (mod n)
|
||||||
var ki = pIL.add(this.privKey.d).mod(curve.params.n)
|
var ki = pIL.add(this.privKey.d).mod(curve.n)
|
||||||
|
|
||||||
// In case ki == 0, proceed with the next value for i
|
// In case ki == 0, proceed with the next value for i
|
||||||
if (ki.signum() === 0) {
|
if (ki.signum() === 0) {
|
||||||
|
@ -243,7 +243,7 @@ HDNode.prototype.derive = function(index) {
|
||||||
} else {
|
} else {
|
||||||
// Ki = point(parse256(IL)) + Kpar
|
// Ki = point(parse256(IL)) + Kpar
|
||||||
// = G*IL + Kpar
|
// = G*IL + Kpar
|
||||||
var Ki = curve.params.G.multiply(pIL).add(this.pubKey.Q)
|
var Ki = curve.G.multiply(pIL).add(this.pubKey.Q)
|
||||||
|
|
||||||
// In case Ki is the point at infinity, proceed with the next value for i
|
// In case Ki is the point at infinity, proceed with the next value for i
|
||||||
if (curve.isInfinity(Ki)) {
|
if (curve.isInfinity(Ki)) {
|
||||||
|
|
|
@ -29,7 +29,7 @@ describe('ecdsa', function() {
|
||||||
fixtures.valid.forEach(function(f) {
|
fixtures.valid.forEach(function(f) {
|
||||||
it('recovers the pubKey for ' + f.d, function() {
|
it('recovers the pubKey for ' + f.d, function() {
|
||||||
var d = BigInteger.fromHex(f.d)
|
var d = BigInteger.fromHex(f.d)
|
||||||
var Q = curve.params.G.multiply(d)
|
var Q = curve.G.multiply(d)
|
||||||
var signature = {
|
var signature = {
|
||||||
r: new BigInteger(f.signature.r),
|
r: new BigInteger(f.signature.r),
|
||||||
s: new BigInteger(f.signature.s)
|
s: new BigInteger(f.signature.s)
|
||||||
|
@ -94,7 +94,7 @@ describe('ecdsa', function() {
|
||||||
var sig = ecdsa.sign(curve, hash, BigInteger.ONE)
|
var sig = ecdsa.sign(curve, hash, BigInteger.ONE)
|
||||||
|
|
||||||
// See BIP62 for more information
|
// See BIP62 for more information
|
||||||
var N_OVER_TWO = curve.params.n.shiftRight(1)
|
var N_OVER_TWO = curve.n.shiftRight(1)
|
||||||
assert(sig.s.compareTo(N_OVER_TWO) <= 0)
|
assert(sig.s.compareTo(N_OVER_TWO) <= 0)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -108,7 +108,7 @@ describe('ecdsa', function() {
|
||||||
new BigInteger(f.signature.r),
|
new BigInteger(f.signature.r),
|
||||||
new BigInteger(f.signature.s)
|
new BigInteger(f.signature.s)
|
||||||
)
|
)
|
||||||
var Q = curve.params.G.multiply(d)
|
var Q = curve.G.multiply(d)
|
||||||
|
|
||||||
assert(ecdsa.verifyRaw(curve, e, signature, Q))
|
assert(ecdsa.verifyRaw(curve, e, signature, Q))
|
||||||
})
|
})
|
||||||
|
@ -122,7 +122,7 @@ describe('ecdsa', function() {
|
||||||
new BigInteger(f.signature.r),
|
new BigInteger(f.signature.r),
|
||||||
new BigInteger(f.signature.s)
|
new BigInteger(f.signature.s)
|
||||||
)
|
)
|
||||||
var Q = curve.params.G.multiply(d)
|
var Q = curve.G.multiply(d)
|
||||||
|
|
||||||
assert.equal(ecdsa.verifyRaw(curve, e, signature, Q), false)
|
assert.equal(ecdsa.verifyRaw(curve, e, signature, Q), false)
|
||||||
})
|
})
|
||||||
|
|
|
@ -12,7 +12,7 @@ var fixtures = require('./fixtures/hdnode.json')
|
||||||
describe('HDNode', function() {
|
describe('HDNode', function() {
|
||||||
describe('Constructor', function() {
|
describe('Constructor', function() {
|
||||||
var d = BigInteger.ONE
|
var d = BigInteger.ONE
|
||||||
var Q = curve.params.G.multiply(d)
|
var Q = curve.G.multiply(d)
|
||||||
var chainCode = new Buffer(32)
|
var chainCode = new Buffer(32)
|
||||||
chainCode.fill(1)
|
chainCode.fill(1)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue