2014-03-28 18:24:23 +01:00
|
|
|
var assert = require('assert')
|
2014-05-13 09:55:53 +02:00
|
|
|
var crypto = require('../src/crypto')
|
|
|
|
var ecdsa = require('../src/ecdsa')
|
2014-05-16 07:39:17 +02:00
|
|
|
var message = require('../src/message')
|
|
|
|
var networks = require('../src/networks')
|
2014-05-13 08:35:07 +02:00
|
|
|
|
2014-05-13 09:55:53 +02:00
|
|
|
var sec = require('../src/sec')
|
2014-04-17 06:33:35 +02:00
|
|
|
var ecparams = sec("secp256k1")
|
2014-04-16 20:10:05 +02:00
|
|
|
|
2014-05-03 04:04:54 +02:00
|
|
|
var BigInteger = require('bigi')
|
2014-03-28 18:24:23 +01:00
|
|
|
|
2014-05-18 11:47:39 +02:00
|
|
|
var fixtures = require('./fixtures/ecdsa.json')
|
2014-04-23 23:45:28 +02:00
|
|
|
|
2014-03-28 18:24:23 +01:00
|
|
|
describe('ecdsa', function() {
|
2014-04-22 22:18:38 +02:00
|
|
|
describe('deterministicGenerateK', function() {
|
2014-04-23 23:45:28 +02:00
|
|
|
it('matches the test vectors', function() {
|
2014-05-16 15:11:04 +02:00
|
|
|
fixtures.valid.forEach(function(f) {
|
2014-05-17 06:39:31 +02:00
|
|
|
var D = BigInteger.fromHex(f.D)
|
2014-04-23 23:45:28 +02:00
|
|
|
var h1 = crypto.sha256(f.message)
|
|
|
|
|
2014-05-17 07:06:52 +02:00
|
|
|
var k = ecdsa.deterministicGenerateK(ecparams, h1, D)
|
2014-05-16 15:11:04 +02:00
|
|
|
assert.equal(k.toHex(), f.k)
|
2014-04-23 23:45:28 +02:00
|
|
|
})
|
2014-04-22 22:18:38 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-03-28 18:24:23 +01:00
|
|
|
describe('recoverPubKey', function() {
|
|
|
|
it('succesfully recovers a public key', function() {
|
2014-05-17 06:39:31 +02:00
|
|
|
var D = BigInteger.ONE
|
|
|
|
var signature = new Buffer('INcvXVVEFyIfHLbDX+xoxlKFn3Wzj9g0UbhObXdMq+YMKC252o5RHFr0/cKdQe1WsBLUBi4morhgZ77obDJVuV0=', 'base64')
|
2014-05-16 07:39:17 +02:00
|
|
|
|
2014-05-17 06:39:31 +02:00
|
|
|
var Q = ecparams.getG().multiply(D)
|
2014-05-16 07:39:17 +02:00
|
|
|
var hash = message.magicHash('1111', networks.bitcoin)
|
2014-05-10 14:30:29 +02:00
|
|
|
var e = BigInteger.fromBuffer(hash)
|
2014-05-17 06:39:31 +02:00
|
|
|
var psig = ecdsa.parseSigCompact(signature)
|
2014-05-16 07:39:17 +02:00
|
|
|
|
2014-05-17 07:06:52 +02:00
|
|
|
var Qprime = ecdsa.recoverPubKey(ecparams, e, psig.r, psig.s, psig.i)
|
2014-05-17 06:39:31 +02:00
|
|
|
assert(Q.equals(Qprime))
|
2014-03-28 18:24:23 +01:00
|
|
|
})
|
|
|
|
})
|
2014-04-16 20:10:05 +02:00
|
|
|
|
2014-04-23 23:45:28 +02:00
|
|
|
describe('sign', function() {
|
|
|
|
it('matches the test vectors', function() {
|
2014-05-16 15:11:04 +02:00
|
|
|
fixtures.valid.forEach(function(f) {
|
2014-05-16 05:46:06 +02:00
|
|
|
var D = BigInteger.fromHex(f.D)
|
2014-04-23 23:45:28 +02:00
|
|
|
var hash = crypto.sha256(f.message)
|
2014-05-17 07:06:52 +02:00
|
|
|
var sig = ecdsa.sign(ecparams, hash, D)
|
2014-04-16 20:10:05 +02:00
|
|
|
|
2014-05-16 15:11:04 +02:00
|
|
|
assert.equal(sig.r.toString(), f.signature.r)
|
|
|
|
assert.equal(sig.s.toString(), f.signature.s)
|
2014-04-23 23:45:28 +02:00
|
|
|
})
|
2014-04-16 20:10:05 +02:00
|
|
|
})
|
2014-04-17 06:33:35 +02:00
|
|
|
|
|
|
|
it('should sign with low S value', function() {
|
2014-04-23 23:45:28 +02:00
|
|
|
var hash = crypto.sha256('Vires in numeris')
|
2014-05-17 07:06:52 +02:00
|
|
|
var sig = ecdsa.sign(ecparams, hash, BigInteger.ONE)
|
2014-04-23 23:45:28 +02:00
|
|
|
|
|
|
|
// See BIP62 for more information
|
2014-05-17 04:05:05 +02:00
|
|
|
var N_OVER_TWO = ecparams.getN().shiftRight(1)
|
2014-05-10 14:38:05 +02:00
|
|
|
assert(sig.s.compareTo(N_OVER_TWO) <= 0)
|
2014-04-23 23:45:28 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('verifyRaw', function() {
|
2014-05-24 05:40:20 +02:00
|
|
|
it('verifies valid signatures', function() {
|
2014-05-16 15:11:04 +02:00
|
|
|
fixtures.valid.forEach(function(f) {
|
2014-05-16 05:46:06 +02:00
|
|
|
var D = BigInteger.fromHex(f.D)
|
2014-05-17 06:39:31 +02:00
|
|
|
var Q = ecparams.getG().multiply(D)
|
2014-04-17 06:33:35 +02:00
|
|
|
|
2014-05-16 15:11:04 +02:00
|
|
|
var r = new BigInteger(f.signature.r)
|
|
|
|
var s = new BigInteger(f.signature.s)
|
2014-04-23 23:45:28 +02:00
|
|
|
var e = BigInteger.fromBuffer(crypto.sha256(f.message))
|
2014-04-17 06:33:35 +02:00
|
|
|
|
2014-05-17 07:06:52 +02:00
|
|
|
assert(ecdsa.verifyRaw(ecparams, e, r, s, Q))
|
2014-04-23 23:45:28 +02:00
|
|
|
})
|
2014-04-17 06:33:35 +02:00
|
|
|
})
|
2014-05-24 05:40:20 +02:00
|
|
|
|
|
|
|
fixtures.invalid.verifyRaw.forEach(function(f) {
|
|
|
|
it('fails to verify with ' + f.description, function() {
|
|
|
|
var D = BigInteger.fromHex(f.D)
|
|
|
|
var e = BigInteger.fromHex(f.e)
|
|
|
|
var r = new BigInteger(f.signature.r)
|
|
|
|
var s = new BigInteger(f.signature.s)
|
|
|
|
var Q = ecparams.getG().multiply(D)
|
|
|
|
|
|
|
|
assert.equal(ecdsa.verifyRaw(ecparams, e, r, s, Q), false)
|
|
|
|
})
|
|
|
|
})
|
2014-04-16 20:10:05 +02:00
|
|
|
})
|
2014-05-16 15:11:04 +02:00
|
|
|
|
|
|
|
describe('serializeSig', function() {
|
|
|
|
it('encodes a DER signature', function() {
|
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
var r = new BigInteger(f.signature.r)
|
|
|
|
var s = new BigInteger(f.signature.s)
|
|
|
|
|
|
|
|
var signature = new Buffer(ecdsa.serializeSig(r, s))
|
|
|
|
assert.equal(signature.toString('hex'), f.DER)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('parseSig', function() {
|
|
|
|
it('decodes the correct signature', function() {
|
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
var buffer = new Buffer(f.DER, 'hex')
|
|
|
|
var signature = ecdsa.parseSig(buffer)
|
|
|
|
|
|
|
|
assert.equal(signature.r.toString(), f.signature.r)
|
|
|
|
assert.equal(signature.s.toString(), f.signature.s)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.DER.forEach(function(f) {
|
|
|
|
it('throws on ' + f.description, function() {
|
|
|
|
var buffer = new Buffer(f.hex)
|
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
ecdsa.parseSig(buffer)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('serializeSigCompact', function() {
|
|
|
|
it('encodes a compact signature', function() {
|
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
var r = new BigInteger(f.signature.r)
|
|
|
|
var s = new BigInteger(f.signature.s)
|
|
|
|
var i = f.signature.i
|
|
|
|
var compressed = f.signature.compressed
|
|
|
|
|
|
|
|
var signature = ecdsa.serializeSigCompact(r, s, i, compressed)
|
|
|
|
assert.equal(signature.toString('hex'), f.compact)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('parseSigCompact', function() {
|
|
|
|
it('decodes the correct signature', function() {
|
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
var buffer = new Buffer(f.compact, 'hex')
|
|
|
|
var signature = ecdsa.parseSigCompact(buffer)
|
|
|
|
|
|
|
|
assert.equal(signature.r.toString(), f.signature.r)
|
|
|
|
assert.equal(signature.s.toString(), f.signature.s)
|
2014-05-16 16:28:39 +02:00
|
|
|
assert.equal(signature.i, f.signature.i)
|
|
|
|
assert.equal(signature.compressed, f.signature.compressed)
|
2014-05-16 15:11:04 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.compact.forEach(function(f) {
|
|
|
|
it('throws on ' + f.description, function() {
|
|
|
|
var buffer = new Buffer(f.hex)
|
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
ecdsa.parseSigCompact(buffer)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-03-28 18:24:23 +01:00
|
|
|
})
|