JSBN and coverage tests cleanup.

Fixes compiler warning about SJCL.
Fixes coverage source highlighting issue.
Move JSBN outside of src/js/jsbn/.
This commit is contained in:
Stefan Thomas
2013-09-21 05:41:19 -07:00
parent 0986b37267
commit 64df821b38
16 changed files with 88 additions and 97 deletions

1
.gitignore vendored
View File

@@ -40,4 +40,5 @@ test/config.js
# Ignore coverage files
/lib-cov
/src-cov
/coverage.html

View File

@@ -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

View File

@@ -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;

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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);

View File

@@ -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":

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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

View File

@@ -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 () {

View File

@@ -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();

View File

@@ -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);
});
});
});

View File

@@ -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);
}