use ecurve instead of custom ec
This commit is contained in:
parent
de8b6a9931
commit
4ce9015f3b
14 changed files with 82 additions and 735 deletions
test
84
test/ec.js
84
test/ec.js
|
@ -1,84 +0,0 @@
|
|||
var assert = require('assert')
|
||||
|
||||
var sec = require('../src/sec')
|
||||
var ecparams = sec('secp256k1')
|
||||
|
||||
var BigInteger = require('bigi')
|
||||
var ECPointFp = require('../src/ec').ECPointFp
|
||||
|
||||
var fixtures = require('./fixtures/ec.json')
|
||||
|
||||
describe('ec', function() {
|
||||
describe('ECPointFp', function() {
|
||||
it('behaves correctly', function() {
|
||||
var G = ecparams.getG()
|
||||
var n = ecparams.getN()
|
||||
|
||||
assert.ok(G.multiply(n).isInfinity(), "Gn is infinite")
|
||||
|
||||
var k = BigInteger.ONE
|
||||
var P = G.multiply(k)
|
||||
assert.ok(!P.isInfinity(), "kG is not infinite")
|
||||
assert.ok(P.isOnCurve(), "kG on curve")
|
||||
assert.ok(P.multiply(n).isInfinity(), "kGn is infinite")
|
||||
|
||||
assert.ok(P.validate(), "kG validates as a public key")
|
||||
})
|
||||
|
||||
describe('getEncoded', function() {
|
||||
it('encodes a point correctly', function() {
|
||||
fixtures.valid.ECPointFp.forEach(function(f) {
|
||||
var curve = ecparams.getCurve()
|
||||
var Q = new ECPointFp(
|
||||
curve,
|
||||
curve.fromBigInteger(new BigInteger(f.x)),
|
||||
curve.fromBigInteger(new BigInteger(f.y))
|
||||
)
|
||||
|
||||
var encoded = Q.getEncoded(f.compressed)
|
||||
assert.equal(encoded.toString('hex'), f.hex)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('decodeFrom', function() {
|
||||
it('decodes the correct point', function() {
|
||||
fixtures.valid.ECPointFp.forEach(function(f) {
|
||||
var curve = ecparams.getCurve()
|
||||
var buffer = new Buffer(f.hex, 'hex')
|
||||
|
||||
var decoded = ECPointFp.decodeFrom(curve, buffer)
|
||||
assert.equal(decoded.Q.getX().toBigInteger().toString(), f.x)
|
||||
assert.equal(decoded.Q.getY().toBigInteger().toString(), f.y)
|
||||
assert.equal(decoded.compressed, f.compressed)
|
||||
})
|
||||
})
|
||||
|
||||
fixtures.invalid.ECPointFp.forEach(function(f) {
|
||||
it('throws on ' + f.description, function() {
|
||||
var curve = ecparams.getCurve()
|
||||
var buffer = new Buffer(f.hex, 'hex')
|
||||
|
||||
assert.throws(function() {
|
||||
ECPointFp.decodeFrom(curve, buffer)
|
||||
}, /Invalid sequence length|Invalid sequence tag/)
|
||||
})
|
||||
})
|
||||
|
||||
it('supports secp256r1', function() {
|
||||
var f = fixtures.valid.ECPointFp[1]
|
||||
var ecparams2 = sec('secp256r1')
|
||||
var curve = ecparams2.getCurve()
|
||||
|
||||
var d = BigInteger.ONE
|
||||
var Q = ecparams2.getG().multiply(d)
|
||||
|
||||
var buffer = Q.getEncoded(true)
|
||||
var decoded = ECPointFp.decodeFrom(curve, buffer)
|
||||
|
||||
assert(Q.equals(decoded.Q))
|
||||
assert(decoded.compressed, true)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
|
@ -4,11 +4,11 @@ var ecdsa = require('../src/ecdsa')
|
|||
var message = require('../src/message')
|
||||
var networks = require('../src/networks')
|
||||
|
||||
var sec = require('../src/sec')
|
||||
var ecparams = sec("secp256k1")
|
||||
|
||||
var BigInteger = require('bigi')
|
||||
|
||||
var ecurve = require('ecurve')
|
||||
var curve = ecurve.getCurveByName('secp256k1')
|
||||
|
||||
var fixtures = require('./fixtures/ecdsa.json')
|
||||
|
||||
describe('ecdsa', function() {
|
||||
|
@ -18,7 +18,7 @@ describe('ecdsa', function() {
|
|||
var d = BigInteger.fromHex(f.d)
|
||||
var h1 = crypto.sha256(f.message)
|
||||
|
||||
var k = ecdsa.deterministicGenerateK(ecparams, h1, d)
|
||||
var k = ecdsa.deterministicGenerateK(curve, h1, d)
|
||||
assert.equal(k.toHex(), f.k)
|
||||
})
|
||||
})
|
||||
|
@ -29,12 +29,12 @@ describe('ecdsa', function() {
|
|||
var d = BigInteger.ONE
|
||||
var signature = new Buffer('INcvXVVEFyIfHLbDX+xoxlKFn3Wzj9g0UbhObXdMq+YMKC252o5RHFr0/cKdQe1WsBLUBi4morhgZ77obDJVuV0=', 'base64')
|
||||
|
||||
var Q = ecparams.getG().multiply(d)
|
||||
var Q = curve.params.G.multiply(d)
|
||||
var hash = message.magicHash('1111', networks.bitcoin)
|
||||
var e = BigInteger.fromBuffer(hash)
|
||||
var parsed = ecdsa.parseSigCompact(signature)
|
||||
|
||||
var Qprime = ecdsa.recoverPubKey(ecparams, e, parsed.signature, parsed.i)
|
||||
var Qprime = ecdsa.recoverPubKey(curve, e, parsed.signature, parsed.i)
|
||||
assert(Q.equals(Qprime))
|
||||
})
|
||||
})
|
||||
|
@ -44,7 +44,7 @@ describe('ecdsa', function() {
|
|||
it('produces a deterministic signature for \"' + f.message + '\"', function() {
|
||||
var d = BigInteger.fromHex(f.d)
|
||||
var hash = crypto.sha256(f.message)
|
||||
var signature = ecdsa.sign(ecparams, hash, d)
|
||||
var signature = ecdsa.sign(curve, hash, d)
|
||||
|
||||
assert.equal(signature.r.toString(), f.signature.r)
|
||||
assert.equal(signature.s.toString(), f.signature.s)
|
||||
|
@ -53,10 +53,10 @@ describe('ecdsa', function() {
|
|||
|
||||
it('should sign with low S value', function() {
|
||||
var hash = crypto.sha256('Vires in numeris')
|
||||
var sig = ecdsa.sign(ecparams, hash, BigInteger.ONE)
|
||||
var sig = ecdsa.sign(curve, hash, BigInteger.ONE)
|
||||
|
||||
// See BIP62 for more information
|
||||
var N_OVER_TWO = ecparams.getN().shiftRight(1)
|
||||
var N_OVER_TWO = curve.params.n.shiftRight(1)
|
||||
assert(sig.s.compareTo(N_OVER_TWO) <= 0)
|
||||
})
|
||||
})
|
||||
|
@ -65,7 +65,7 @@ describe('ecdsa', function() {
|
|||
fixtures.valid.forEach(function(f) {
|
||||
it('verifies a valid signature for \"' + f.message + '\"', function() {
|
||||
var d = BigInteger.fromHex(f.d)
|
||||
var Q = ecparams.getG().multiply(d)
|
||||
var Q = curve.params.G.multiply(d)
|
||||
|
||||
var signature = {
|
||||
r: new BigInteger(f.signature.r),
|
||||
|
@ -73,7 +73,7 @@ describe('ecdsa', function() {
|
|||
}
|
||||
var e = BigInteger.fromBuffer(crypto.sha256(f.message))
|
||||
|
||||
assert(ecdsa.verifyRaw(ecparams, e, signature, Q))
|
||||
assert(ecdsa.verifyRaw(curve, e, signature, Q))
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -85,9 +85,9 @@ describe('ecdsa', function() {
|
|||
r: new BigInteger(f.signature.r),
|
||||
s: new BigInteger(f.signature.s)
|
||||
}
|
||||
var Q = ecparams.getG().multiply(d)
|
||||
var Q = curve.params.G.multiply(d)
|
||||
|
||||
assert.equal(ecdsa.verifyRaw(ecparams, e, signature, Q), false)
|
||||
assert.equal(ecdsa.verifyRaw(curve, e, signature, Q), false)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -1,25 +1,24 @@
|
|||
var assert = require('assert')
|
||||
var crypto = require('../src/crypto')
|
||||
var networks = require('../src/networks')
|
||||
var sec = require('../src/sec')
|
||||
var ecparams = sec('secp256k1')
|
||||
|
||||
var BigInteger = require('bigi')
|
||||
var ECPointFp = require('../src/ec').ECPointFp
|
||||
var ECPubKey = require('../src/ecpubkey')
|
||||
|
||||
var ecurve = require('ecurve')
|
||||
var curve = ecurve.getCurveByName('secp256k1')
|
||||
var ECPoint = ecurve.Point
|
||||
|
||||
var fixtures = require('./fixtures/ecpubkey.json')
|
||||
|
||||
describe('ECPubKey', function() {
|
||||
var Q
|
||||
|
||||
beforeEach(function() {
|
||||
var curve = ecparams.getCurve()
|
||||
|
||||
Q = new ECPointFp(
|
||||
Q = ECPoint.fromAffine(
|
||||
curve,
|
||||
curve.fromBigInteger(new BigInteger(fixtures.Q.x)),
|
||||
curve.fromBigInteger(new BigInteger(fixtures.Q.y))
|
||||
new BigInteger(fixtures.Q.x),
|
||||
new BigInteger(fixtures.Q.y)
|
||||
)
|
||||
})
|
||||
|
||||
|
|
60
test/fixtures/ec.json
vendored
60
test/fixtures/ec.json
vendored
|
@ -1,60 +0,0 @@
|
|||
{
|
||||
"valid": {
|
||||
"ECPointFp": [
|
||||
{
|
||||
"x": "55066263022277343669578718895168534326250603453777594175500187360389116729240",
|
||||
"y": "32670510020758816978083085130507043184471273380659243275938904335757337482424",
|
||||
"compressed": false,
|
||||
"hex": "0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8"
|
||||
},
|
||||
{
|
||||
"x": "55066263022277343669578718895168534326250603453777594175500187360389116729240",
|
||||
"y": "32670510020758816978083085130507043184471273380659243275938904335757337482424",
|
||||
"compressed": true,
|
||||
"hex": "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
|
||||
},
|
||||
{
|
||||
"x": "83225686012142088543596389522774768397204444195709443235253141114409346958144",
|
||||
"y": "23739058578904784236915560265041168694780215705543362357495033621678991351768",
|
||||
"compressed": true,
|
||||
"hex": "02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340"
|
||||
},
|
||||
{
|
||||
"x": "30095590000961171681152428142595206241714764354580127609094760797518133922356",
|
||||
"y": "93521207164355458151597931319591130635754976513751247168472016818884561919702",
|
||||
"compressed": true,
|
||||
"hex": "024289801366bcee6172b771cf5a7f13aaecd237a0b9a1ff9d769cabc2e6b70a34"
|
||||
},
|
||||
{
|
||||
"x": "55066263022277343669578718895168534326250603453777594175500187360389116729240",
|
||||
"y": "83121579216557378445487899878180864668798711284981320763518679672151497189239",
|
||||
"compressed": true,
|
||||
"hex": "0379be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
|
||||
}
|
||||
]
|
||||
},
|
||||
"invalid": {
|
||||
"ECPointFp": [
|
||||
{
|
||||
"description": "Invalid sequence tag",
|
||||
"hex": "0179be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
|
||||
},
|
||||
{
|
||||
"description": "Sequence too short",
|
||||
"hex": "0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10"
|
||||
},
|
||||
{
|
||||
"description": "Sequence too short (compressed)",
|
||||
"hex": "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f8"
|
||||
},
|
||||
{
|
||||
"description": "Sequence too long",
|
||||
"hex": "0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b80000"
|
||||
},
|
||||
{
|
||||
"description": "Sequence too long (compressed)",
|
||||
"hex": "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f817980000"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
|
@ -1,17 +1,18 @@
|
|||
var assert = require('assert')
|
||||
var networks = require('../src/networks')
|
||||
var sec = require('../src/sec')
|
||||
var ecparams = sec("secp256k1")
|
||||
|
||||
var BigInteger = require('bigi')
|
||||
var HDNode = require('../src/hdnode')
|
||||
|
||||
var ecurve = require('ecurve')
|
||||
var curve = ecurve.getCurveByName('secp256k1')
|
||||
|
||||
var fixtures = require('./fixtures/hdnode.json')
|
||||
|
||||
describe('HDNode', function() {
|
||||
describe('Constructor', function() {
|
||||
var d = BigInteger.ONE
|
||||
var Q = ecparams.getG().multiply(d)
|
||||
var Q = curve.params.G.multiply(d)
|
||||
var chainCode = new Buffer(32)
|
||||
chainCode.fill(1)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue