From 64df821b384554bf312625d8a7e49ff7b5e08992 Mon Sep 17 00:00:00 2001 From: Stefan Thomas Date: Sat, 21 Sep 2013 05:41:19 -0700 Subject: [PATCH] JSBN and coverage tests cleanup. Fixes compiler warning about SJCL. Fixes coverage source highlighting issue. Move JSBN outside of src/js/jsbn/. --- .gitignore | 1 + Makefile | 8 ++-- src/js/{ripple => jsbn}/jsbn.js | 81 ++++++++++++++------------------ src/js/ripple/amount.js | 4 +- src/js/ripple/base.js | 12 ++--- src/js/ripple/seed.js | 5 +- src/js/ripple/serializedtypes.js | 5 +- src/js/ripple/uint.js | 8 ++-- src/js/ripple/uint128.js | 4 +- src/js/ripple/uint160.js | 4 +- src/js/ripple/uint256.js | 4 +- src/js/ripple/utils.js | 9 ++-- test/amount-test.js | 6 +-- test/serializedtypes-test.js | 3 +- test/sjcl-extramath-test.js | 27 ++++++++--- test/testutils.js | 4 +- 16 files changed, 88 insertions(+), 97 deletions(-) rename src/js/{ripple => jsbn}/jsbn.js (98%) diff --git a/.gitignore b/.gitignore index 36140020..6539f40e 100644 --- a/.gitignore +++ b/.gitignore @@ -40,4 +40,5 @@ test/config.js # Ignore coverage files /lib-cov +/src-cov /coverage.html \ No newline at end of file diff --git a/Makefile b/Makefile index 5079efa0..e81d143f 100644 --- a/Makefile +++ b/Makefile @@ -2,9 +2,11 @@ test: mocha --reporter spec test/*-test.js coverage: - rm -rf lib-cov - jscoverage src/js/ripple lib-cov + rm -rf src-cov + mkdir src-cov + mkdir src-cov/js + jscoverage --no-highlight src/js/ripple src-cov/js/ripple RIPPLE_LIB_COV=1 mocha --reporter html-cov test/*-test.js > coverage.html - rm -rf lib-cov + rm -rf src-cov .PHONY: test diff --git a/src/js/ripple/jsbn.js b/src/js/jsbn/jsbn.js similarity index 98% rename from src/js/ripple/jsbn.js rename to src/js/jsbn/jsbn.js index 772ae15f..98b80fc3 100644 --- a/src/js/ripple/jsbn.js +++ b/src/js/jsbn/jsbn.js @@ -1,15 +1,8 @@ -// Derived from Tom Wu's jsbn code. -// -// Changes made for clean up and to package as a node.js module. - -// Copyright (c) 2005-2009 Tom Wu +// Copyright (c) 2005 Tom Wu // All Rights Reserved. // See "LICENSE" for details. // Basic JavaScript BN library - subset useful for RSA encryption. -// Extended JavaScript BN functions, required for RSA private ops. -// Version 1.1: new BigInteger("0", 10) returns "proper" zero -// Version 1.2: square() API, isProbablePrime fix // Bits per digit var dbits; @@ -19,15 +12,15 @@ var canary = 0xdeadbeefcafe; var j_lm = ((canary&0xffffff)==0xefcafe); // (public) Constructor -var BigInteger = function BigInteger(a,b,c) { +function BigInteger(a,b,c) { if(a != null) if("number" == typeof a) this.fromNumber(a,b,c); else if(b == null && "string" != typeof a) this.fromString(a,256); else this.fromString(a,b); -}; +} // return new, unset BigInteger -var nbi = function nbi() { return new BigInteger(null); }; +function nbi() { return new BigInteger(null); } // am: Compute w_j += (x*this_i), propagate carries, // c is initial carry, returns final carry. @@ -74,7 +67,6 @@ function am3(i,x,w,j,c,n) { } return c; } - if(j_lm && 'undefined' !== typeof navigator && (navigator.appName == "Microsoft Internet Explorer")) { BigInteger.prototype.am = am2; dbits = 30; @@ -126,7 +118,7 @@ function bnpFromInt(x) { this.t = 1; this.s = (x<0)?-1:0; if(x > 0) this[0] = x; - else if(x < -1) this[0] = x+DV; + else if(x < -1) this[0] = x+this.DV; else this.t = 0; } @@ -1122,6 +1114,7 @@ function bnpMillerRabin(t) { return true; } + // protected BigInteger.prototype.chunkSize = bnpChunkSize; BigInteger.prototype.toRadix = bnpToRadix; @@ -1137,7 +1130,31 @@ BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo; BigInteger.prototype.modInt = bnpModInt; BigInteger.prototype.millerRabin = bnpMillerRabin; +BigInteger.prototype.copyTo = bnpCopyTo; +BigInteger.prototype.fromInt = bnpFromInt; +BigInteger.prototype.fromString = bnpFromString; +BigInteger.prototype.clamp = bnpClamp; +BigInteger.prototype.dlShiftTo = bnpDLShiftTo; +BigInteger.prototype.drShiftTo = bnpDRShiftTo; +BigInteger.prototype.lShiftTo = bnpLShiftTo; +BigInteger.prototype.rShiftTo = bnpRShiftTo; +BigInteger.prototype.subTo = bnpSubTo; +BigInteger.prototype.multiplyTo = bnpMultiplyTo; +BigInteger.prototype.squareTo = bnpSquareTo; +BigInteger.prototype.divRemTo = bnpDivRemTo; +BigInteger.prototype.invDigit = bnpInvDigit; +BigInteger.prototype.isEven = bnpIsEven; +BigInteger.prototype.exp = bnpExp; + // public +BigInteger.prototype.toString = bnToString; +BigInteger.prototype.negate = bnNegate; +BigInteger.prototype.abs = bnAbs; +BigInteger.prototype.compareTo = bnCompareTo; +BigInteger.prototype.bitLength = bnBitLength; +BigInteger.prototype.mod = bnMod; +BigInteger.prototype.modPowInt = bnModPowInt; + BigInteger.prototype.clone = bnClone; BigInteger.prototype.intValue = bnIntValue; BigInteger.prototype.byteValue = bnByteValue; @@ -1175,6 +1192,10 @@ BigInteger.prototype.isProbablePrime = bnIsProbablePrime; // JSBN-specific extension BigInteger.prototype.square = bnSquare; +// "constants" +BigInteger.ZERO = nbv(0); +BigInteger.ONE = nbv(1); + // BigInteger interfaces not implemented in jsbn: // BigInteger(int signum, byte[] magnitude) @@ -1183,37 +1204,7 @@ BigInteger.prototype.square = bnSquare; // int hashCode() // long longValue() // static BigInteger valueOf(long val) -// protected -BigInteger.prototype.copyTo = bnpCopyTo; -BigInteger.prototype.fromInt = bnpFromInt; -BigInteger.prototype.fromString = bnpFromString; -BigInteger.prototype.clamp = bnpClamp; -BigInteger.prototype.dlShiftTo = bnpDLShiftTo; -BigInteger.prototype.drShiftTo = bnpDRShiftTo; -BigInteger.prototype.lShiftTo = bnpLShiftTo; -BigInteger.prototype.rShiftTo = bnpRShiftTo; -BigInteger.prototype.subTo = bnpSubTo; -BigInteger.prototype.multiplyTo = bnpMultiplyTo; -BigInteger.prototype.squareTo = bnpSquareTo; -BigInteger.prototype.divRemTo = bnpDivRemTo; -BigInteger.prototype.invDigit = bnpInvDigit; -BigInteger.prototype.isEven = bnpIsEven; -BigInteger.prototype.exp = bnpExp; -// public -BigInteger.prototype.toString = bnToString; -BigInteger.prototype.negate = bnNegate; -BigInteger.prototype.abs = bnAbs; -BigInteger.prototype.compareTo = bnCompareTo; -BigInteger.prototype.bitLength = bnBitLength; -BigInteger.prototype.mod = bnMod; -BigInteger.prototype.modPowInt = bnModPowInt; +BigInteger.valueOf = nbi; -// "constants" -BigInteger.ZERO = nbv(0); -BigInteger.ONE = nbv(1); - -exports.nbi = nbi; -exports.BigInteger = BigInteger; - -// vim:sw=2:sts=2:ts=8:et +exports.BigInteger = BigInteger; diff --git a/src/js/ripple/amount.js b/src/js/ripple/amount.js index 802ad870..b941f33b 100644 --- a/src/js/ripple/amount.js +++ b/src/js/ripple/amount.js @@ -5,9 +5,7 @@ var utils = require('./utils'); var sjcl = utils.sjcl; var bn = sjcl.bn; -var jsbn = require('./jsbn'); - -var BigInteger = jsbn.BigInteger; +var BigInteger = utils.jsbn.BigInteger; var UInt160 = require('./uint160').UInt160; var Seed = require('./seed').Seed; diff --git a/src/js/ripple/base.js b/src/js/ripple/base.js index 39fd2991..97619d5f 100644 --- a/src/js/ripple/base.js +++ b/src/js/ripple/base.js @@ -1,10 +1,8 @@ var sjcl = require('./utils').sjcl; var utils = require('./utils'); -var jsbn = require('./jsbn'); var extend = require('extend'); -var BigInteger = jsbn.BigInteger; -var nbi = jsbn.nbi; +var BigInteger = utils.jsbn.BigInteger; var Base = {}; @@ -38,8 +36,8 @@ function sha256hash(bytes) { Base.encode = function (input, alpha) { var alphabet = alphabets[alpha || 'ripple']; var bi_base = new BigInteger(String(alphabet.length)); - var bi_q = nbi(); - var bi_r = nbi(); + var bi_q = new BigInteger(); + var bi_r = new BigInteger(); var bi_value = new BigInteger(input); var buffer = []; @@ -65,7 +63,7 @@ Base.decode = function (input, alpha) { var alphabet = alphabets[alpha || 'ripple']; var bi_base = new BigInteger(String(alphabet.length)); - var bi_value = nbi(); + var bi_value = new BigInteger(); var i; for (i = 0; i != input.length && input[i] === alphabet[0]; i += 1) @@ -78,7 +76,7 @@ Base.decode = function (input, alpha) { return void(0); } - var r = nbi(); + var r = new BigInteger(); r.fromInt(v); bi_value = bi_value.multiply(bi_base).add(r); } diff --git a/src/js/ripple/seed.js b/src/js/ripple/seed.js index b6abc5ab..22a3e92e 100644 --- a/src/js/ripple/seed.js +++ b/src/js/ripple/seed.js @@ -2,12 +2,11 @@ // Seed support // -var sjcl = require('./utils').sjcl; var utils = require('./utils'); -var jsbn = require('./jsbn'); +var sjcl = utils.sjcl; var extend = require('extend'); -var BigInteger = jsbn.BigInteger; +var BigInteger = utils.jsbn.BigInteger; var Base = require('./base').Base; var UInt = require('./uint').UInt; diff --git a/src/js/ripple/serializedtypes.js b/src/js/ripple/serializedtypes.js index a83048d7..9616bb84 100644 --- a/src/js/ripple/serializedtypes.js +++ b/src/js/ripple/serializedtypes.js @@ -8,7 +8,7 @@ var extend = require('extend'); var utils = require('./utils'); -var sjcl = require('./utils').sjcl; +var sjcl = utils.sjcl; var amount = require('./amount'); var UInt128 = require('./uint128').UInt128; @@ -21,8 +21,7 @@ var Currency = amount.Currency; var hex = sjcl.codec.hex; var bytes = sjcl.codec.bytes; -var jsbn = require('./jsbn'); -var BigInteger = jsbn.BigInteger; +var BigInteger = utils.jsbn.BigInteger; var SerializedType = function (methods) { extend(this, methods); diff --git a/src/js/ripple/uint.js b/src/js/ripple/uint.js index ae3b2d48..03bb4ae4 100644 --- a/src/js/ripple/uint.js +++ b/src/js/ripple/uint.js @@ -1,10 +1,8 @@ -var sjcl = require('./utils').sjcl; var utils = require('./utils'); +var sjcl = utils.sjcl; var config = require('./config'); -var jsbn = require('./jsbn'); -var BigInteger = jsbn.BigInteger; -var nbi = jsbn.nbi; +var BigInteger = utils.jsbn.BigInteger; var Base = require('./base').Base; @@ -116,7 +114,7 @@ UInt.prototype.parse_generic = function (j) { case this.constructor.STR_ZERO: case this.constructor.ACCOUNT_ZERO: case this.constructor.HEX_ZERO: - this._value = nbi(); + this._value = BigInteger.valueOf(); break; case "1": diff --git a/src/js/ripple/uint128.js b/src/js/ripple/uint128.js index 286ee888..a82b9166 100644 --- a/src/js/ripple/uint128.js +++ b/src/js/ripple/uint128.js @@ -1,11 +1,9 @@ var sjcl = require('./utils').sjcl; var utils = require('./utils'); var config = require('./config'); -var jsbn = require('./jsbn'); var extend = require('extend'); -var BigInteger = jsbn.BigInteger; -var nbi = jsbn.nbi; +var BigInteger = utils.jsbn.BigInteger; var UInt = require('./uint').UInt, Base = require('./base').Base; diff --git a/src/js/ripple/uint160.js b/src/js/ripple/uint160.js index e08aa517..22fe0c4a 100644 --- a/src/js/ripple/uint160.js +++ b/src/js/ripple/uint160.js @@ -1,11 +1,9 @@ var sjcl = require('./utils').sjcl; var utils = require('./utils'); var config = require('./config'); -var jsbn = require('./jsbn'); var extend = require('extend'); -var BigInteger = jsbn.BigInteger; -var nbi = jsbn.nbi; +var BigInteger = utils.jsbn.BigInteger; var UInt = require('./uint').UInt; var Base = require('./base').Base; diff --git a/src/js/ripple/uint256.js b/src/js/ripple/uint256.js index 6f385410..f10c1b86 100644 --- a/src/js/ripple/uint256.js +++ b/src/js/ripple/uint256.js @@ -1,11 +1,9 @@ var sjcl = require('./utils').sjcl; var utils = require('./utils'); var config = require('./config'); -var jsbn = require('./jsbn'); var extend = require('extend'); -var BigInteger = jsbn.BigInteger; -var nbi = jsbn.nbi; +var BigInteger = utils.jsbn.BigInteger; var UInt = require('./uint').UInt, Base = require('./base').Base; diff --git a/src/js/ripple/utils.js b/src/js/ripple/utils.js index 8d45a7ec..1b4411e0 100644 --- a/src/js/ripple/utils.js +++ b/src/js/ripple/utils.js @@ -141,10 +141,9 @@ exports.assert = assert; exports.arrayUnique = arrayUnique; exports.toTimestamp = toTimestamp; -try { - exports.sjcl = require('../build/sjcl'); -} catch(e) { - exports.sjcl = require('../../../build/sjcl'); -} +// Going up three levels is needed to escape the src-cov folder used for the +// test coverage stuff. +exports.sjcl = require('../../../build/sjcl'); +exports.jsbn = require('../../../src/js/jsbn/jsbn'); // vim:sw=2:sts=2:ts=8:et diff --git a/test/amount-test.js b/test/amount-test.js index c584a157..e0564087 100644 --- a/test/amount-test.js +++ b/test/amount-test.js @@ -1,7 +1,6 @@ var assert = require('assert'); var utils = require('./testutils'); -var jsbn = utils.load_module('jsbn'); -var BigInteger = jsbn.BigInteger; +var BigInteger = require('../src/js/jsbn/jsbn').BigInteger; var Amount = utils.load_module('amount').Amount; var UInt160 = utils.load_module('uint160').UInt160; var config = utils.get_config(); @@ -34,7 +33,7 @@ describe('Amount', function() { }); describe('UInt160', function() { it('Parse 0', function () { - assert.deepEqual(jsbn.nbi(), UInt160.from_generic('0')._value); + assert.deepEqual(new BigInteger(), UInt160.from_generic('0')._value); }); it('Parse 0 export', function () { assert.strictEqual(UInt160.ACCOUNT_ZERO, UInt160.from_generic('0').to_json()); @@ -49,6 +48,7 @@ describe('Amount', function() { assert.strictEqual(UInt160.ACCOUNT_ONE, UInt160.from_json('rrrrrrrrrrrrrrrrrrrrBZbvji').to_json()); }); it('Parse mtgox export', function () { + console.log(UInt160.from_json('mtgox').to_json()); assert.strictEqual(config.accounts['mtgox'].account, UInt160.from_json('mtgox').to_json()); }); it('is_valid rrrrrrrrrrrrrrrrrrrrrhoLvTp', function () { diff --git a/test/serializedtypes-test.js b/test/serializedtypes-test.js index 5ac5f31a..472f4872 100644 --- a/test/serializedtypes-test.js +++ b/test/serializedtypes-test.js @@ -2,8 +2,7 @@ var utils = require('./testutils'); var assert = require('assert'); var SerializedObject = utils.load_module('serializedobject').SerializedObject; var types = utils.load_module('serializedtypes'); -var jsbn = utils.load_module('jsbn'); -var BigInteger = jsbn.BigInteger; +var BigInteger = require('../src/js/jsbn/jsbn').BigInteger; var config = require('./testutils').get_config(); diff --git a/test/sjcl-extramath-test.js b/test/sjcl-extramath-test.js index 5ebba269..aa05c03f 100644 --- a/test/sjcl-extramath-test.js +++ b/test/sjcl-extramath-test.js @@ -21,18 +21,31 @@ describe('SJCL Extramath', function() { }); }); describe('testBit', function() { - it('0x03 test bit 0 => 1', function () { + it('0x03', function () { var val = new sjcl.bn("03"); assert.strictEqual(val.testBit(0), 1); - }); - it('0x03 test bit 1 => 1', function () { - var val = new sjcl.bn("03"); assert.strictEqual(val.testBit(1), 1); - }); - it('0x03 test bit 2 => 0', function () { - var val = new sjcl.bn("03"); assert.strictEqual(val.testBit(2), 0); }); + it('0x1000000', function () { + var val = new sjcl.bn("1000000"); + assert.strictEqual(val.testBit(25), 0); + assert.strictEqual(val.testBit(24), 1); + assert.strictEqual(val.testBit(23), 0); + assert.strictEqual(val.testBit( 1), 0); + assert.strictEqual(val.testBit( 0), 0); + }); + it('0xff7fffffff', function () { + var val = new sjcl.bn("ff7fffffff"); + assert.strictEqual(val.testBit(32), 1); + assert.strictEqual(val.testBit(31), 0); + assert.strictEqual(val.testBit(30), 1); + assert.strictEqual(val.testBit(24), 1); + assert.strictEqual(val.testBit(23), 1); + assert.strictEqual(val.testBit(22), 1); + assert.strictEqual(val.testBit( 1), 1); + assert.strictEqual(val.testBit( 0), 1); + }); }); }); diff --git a/test/testutils.js b/test/testutils.js index 1cab290c..e200eabd 100644 --- a/test/testutils.js +++ b/test/testutils.js @@ -14,11 +14,11 @@ function get_config() { exports.load_config = load_config; function load_config(config) { - return( require('../src/js/ripple/config')).load(config); + return load_module('config').load(config); } exports.load_module = load_module; function load_module(name) { - return require((process.env.RIPPLE_LIB_COV ? '../lib-cov/' : '../src/js/ripple/') + name); + return require((process.env.RIPPLE_LIB_COV ? '../src-cov/js/ripple/' : '../src/js/ripple/') + name); }