From 1c76bdf9c3405145e8ffb6a38daf2ab288484947 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 16 May 2014 23:17:46 +1000 Subject: [PATCH] ec: add better test vectors for ECPointFp encode/decode These test vectors were generated internally. --- test/ec.js | 54 +++++++++++++++++++++++++++++++--------- test/fixtures/ec.js | 60 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 12 deletions(-) create mode 100644 test/fixtures/ec.js diff --git a/test/ec.js b/test/ec.js index af3e65d..963bf0e 100644 --- a/test/ec.js +++ b/test/ec.js @@ -6,6 +6,8 @@ var ecparams = sec('secp256k1') var BigInteger = require('bigi') var ECPointFp = require('../src/ec').ECPointFp +var fixtures = require('./fixtures/ec.js') + describe('ec', function() { describe('ECPointFp', function() { it('behaves correctly', function() { @@ -22,21 +24,49 @@ describe('ec', function() { assert.ok(P.validate(), "kG validates as a public key") }) - }) - describe('decodeFrom', function() { - it('decodes compressed ECPoints', function() { - var s = new Buffer('02789ece95adf35fb3de994b8b16c90166736d70913a18378fff79503e8c5db7fb', 'hex') - var Q = ECPointFp.decodeFrom(ecparams.getCurve(), s) - assert.ok(Q) - assert.ok(Q.validate()) + 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 = new Buffer(Q.getEncoded(f.compressed)) + assert.equal(encoded.toString('hex'), f.hex) + }) + }) }) - it('decodes uncompressed ECPoints', function() { - var s = new Buffer('0486f356006a38b847bedec1bf47013776925d939d5a35a97a4d1263e550c7f1ab5aba44ab74d22892097a0e851addf07ba97e33416df5affaceeb35d5607cd23c', 'hex') - var Q = ECPointFp.decodeFrom(ecparams.getCurve(), s) - assert.ok(Q) - assert.ok(Q.validate()) + 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.getX().toBigInteger().toString(), f.x) + assert.equal(decoded.getY().toBigInteger().toString(), f.y) + + // TODO +// assert.equal(decoded.compressed, f.compressed) + }) + }) + + // FIXME + // 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) + // }) + // }) + // }) }) }) }) diff --git a/test/fixtures/ec.js b/test/fixtures/ec.js new file mode 100644 index 0000000..b7b973f --- /dev/null +++ b/test/fixtures/ec.js @@ -0,0 +1,60 @@ +module.exports = { + "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" + } + ] + } +}