From 827f00715c842415ef07c95eb5b493ca97d13882 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 17 Apr 2014 04:10:05 +1000 Subject: [PATCH] Moves test/misc.js tests to appropriate location --- test/ec.js | 46 ++++++++++++++++++++++++++++---------- test/ecdsa.js | 50 +++++++++++++++++++++++++++++++++++++---- test/misc.js | 61 --------------------------------------------------- 3 files changed, 80 insertions(+), 77 deletions(-) delete mode 100644 test/misc.js diff --git a/test/ec.js b/test/ec.js index 6123294..94382fa 100644 --- a/test/ec.js +++ b/test/ec.js @@ -1,22 +1,44 @@ var assert = require('assert') -var sec = require('../src/jsbn/sec') var ecdsa = require('../').ecdsa +var sec = require('../src/jsbn/sec') var ecparams = sec('secp256k1') -describe('EC', function() { - it('handles point multiplication', function() { - var G = ecparams.getG() - var n = ecparams.getN() +var ECPointFp = require('../').ECPointFp +var ECKey = require('../').ECKey - assert.ok(G.multiply(n).isInfinity(), "Gn is infinite") +describe('ec', function() { + describe('ECPointFp', function() { + it('behaviours correctly', function() { + var G = ecparams.getG() + var n = ecparams.getN() - var k = ecdsa.getBigRandom(n) - 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(G.multiply(n).isInfinity(), "Gn is infinite") - assert.ok(P.validate(), "kG validates as a public key") + var k = ecdsa.getBigRandom(n) + 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('decodeFrom', function() { + it('decodes valid ECPoints', function() { + var p1 = ECKey().getPub().toBytes() + assert.equal(p1.length, 65) + + var p1_q = ECPointFp.decodeFrom(ecparams.getCurve(), p1) + assert.ok(p1_q) + assert.ok(p1_q.validate()) + + var p2 = new Buffer('0486f356006a38b847bedec1bf47013776925d939d5a35a97a4d1263e550c7f1ab5aba44ab74d22892097a0e851addf07ba97e33416df5affaceeb35d5607cd23c', 'hex') + + var p2_q = ECPointFp.decodeFrom(ecparams.getCurve(), p2) + assert.ok(p2_q) + assert.ok(p2_q.validate()) + }) }) }) diff --git a/test/ecdsa.js b/test/ecdsa.js index 9e55a05..93dfb51 100644 --- a/test/ecdsa.js +++ b/test/ecdsa.js @@ -1,8 +1,13 @@ var assert = require('assert') -var convert = require('../').convert -var ecdsa = require('../').ecdsa -var ECPubKey = require('../').ECPubKey -var Message = require('../').Message +var convert = require('..').convert +var crypto = require('../').crypto +var ecdsa = require('..').ecdsa +var rng = require('secure-random') + +var BigInteger = require('..').BigInteger +var ECKey = require('..').ECKey +var ECPubKey = require('..').ECPubKey +var Message = require('..').Message describe('ecdsa', function() { describe('recoverPubKey', function() { @@ -15,4 +20,41 @@ describe('ecdsa', function() { assert.equal(pubKey.toHex(true), '02e8fcf4d749b35879bc1f3b14b49e67ab7301da3558c5a9b74a54f1e6339c334c') }) }) + + describe('sign/verify', function() { + it('Signing and Verifying', function () { + var s1 = new ECKey() + var sig_a = s1.sign(BigInteger.ZERO) + + assert.ok(sig_a, 'Sign null') + assert.ok(s1.verify(BigInteger.ZERO, sig_a)) + + var message = new BigInteger(1024, rng).toByteArrayUnsigned() + var hash = crypto.sha256(message) + var sig_b = s1.sign(hash) + assert.ok(sig_b, 'Sign random string') + assert.ok(s1.verify(hash, sig_b)) + + var message2 = convert.hexToBytes( + '12dce2c169986b3346827ffb2305cf393984627f5f9722a1b1368e933c8d' + + 'd296653fbe5d7ac031c4962ad0eb1c4298c3b91d244e1116b4a76a130c13' + + '1e7aec7fa70184a71a2e66797052831511b93c6e8d72ae58a1980eaacb66' + + '8a33f50d7cefb96a5dab897b5efcb99cbafb0d777cb83fc9b2115b69c0fa' + + '3d82507b932b84e4') + + var hash2 = crypto.sha256(message2) + + var sig_c = convert.hexToBytes( + '3044022038d9b8dd5c9fbf330565c1f51d72a59ba869aeb2c2001be959d3' + + '79e861ec71960220a73945f32cf90d03127d2c3410d16cee120fa1a4b4c3' + + 'f273ab082801a95506c4') + + var s2 = convert.hexToBytes( + '045a1594316e433fb91f35ef4874610d22177c3f1a1060f6c1e70a609d51' + + 'b20be5795cd2a5eae0d6b872ba42db95e9afaeea3fbb89e98099575b6828' + + '609a978528') + + assert.ok(ecdsa.verify(hash2, sig_c, s2), 'Verify constant signature') + }) + }) }) diff --git a/test/misc.js b/test/misc.js deleted file mode 100644 index 0b162d8..0000000 --- a/test/misc.js +++ /dev/null @@ -1,61 +0,0 @@ -var assert = require('assert') -var bitcoinjs = require('../') -var crypto = require('../').crypto -var sec = require('../src/jsbn/sec') -var BigInteger = require('../src/jsbn/jsbn.js') -var rng = require('secure-random') -var ecparams = sec('secp256k1') -var ECPointFp = bitcoinjs.ECPointFp -var convert = require('../src/convert') - -it('Keys & Key Management', function () { - var p1 = bitcoinjs.ECKey().getPub().toBytes() - assert.equal(p1.length, 65) - - var p1_q = ECPointFp.decodeFrom(ecparams.getCurve(), p1) - assert.ok(p1_q) - assert.ok(p1_q.validate()) - - var p2 = bitcoinjs.convert.hexToBytes( - '0486f356006a38b847bedec1bf47013776925d939d5a35a97a4d1263e550c7f1a' + - 'b5aba44ab74d22892097a0e851addf07ba97e33416df5affaceeb35d5607cd23c') - - var p2_q = ECPointFp.decodeFrom(ecparams.getCurve(), p2) - assert.ok(p2_q) - assert.ok(p2_q.validate()) -}) - -it('Signing and Verifying', function () { - var s1 = bitcoinjs.ECKey() - var sig_a = s1.sign(BigInteger.ZERO) - assert.ok(sig_a, 'Sign null') - - assert.ok(s1.verify(BigInteger.ZERO, sig_a)) - - var message = new BigInteger(1024, rng).toByteArrayUnsigned() - var hash = crypto.sha256(message) - var sig_b = s1.sign(hash) - assert.ok(sig_b, 'Sign random string') - assert.ok(s1.verify(hash, sig_b)) - - var message2 = bitcoinjs.convert.hexToBytes( - '12dce2c169986b3346827ffb2305cf393984627f5f9722a1b1368e933c8d' + - 'd296653fbe5d7ac031c4962ad0eb1c4298c3b91d244e1116b4a76a130c13' + - '1e7aec7fa70184a71a2e66797052831511b93c6e8d72ae58a1980eaacb66' + - '8a33f50d7cefb96a5dab897b5efcb99cbafb0d777cb83fc9b2115b69c0fa' + - '3d82507b932b84e4') - - var hash2 = crypto.sha256(message2) - - var sig_c = bitcoinjs.convert.hexToBytes( - '3044022038d9b8dd5c9fbf330565c1f51d72a59ba869aeb2c2001be959d3' + - '79e861ec71960220a73945f32cf90d03127d2c3410d16cee120fa1a4b4c3' + - 'f273ab082801a95506c4') - - var s2 = bitcoinjs.convert.hexToBytes( - '045a1594316e433fb91f35ef4874610d22177c3f1a1060f6c1e70a609d51' + - 'b20be5795cd2a5eae0d6b872ba42db95e9afaeea3fbb89e98099575b6828' + - '609a978528') - - assert.ok(bitcoinjs.ecdsa.verify(hash2, sig_c, s2), 'Verify constant signature') -})