Skip to content

Commit

Permalink
Merge pull request #337 from clark800/sjcl_npm
Browse files Browse the repository at this point in the history
Use sjcl npm module, delete sjcl code
  • Loading branch information
geertweening committed May 12, 2015
2 parents d56e70b + de7fc78 commit ab943f3
Show file tree
Hide file tree
Showing 219 changed files with 374 additions and 39,995 deletions.
49 changes: 4 additions & 45 deletions Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,21 @@ var watch = require('gulp-watch');
var plumber = require('gulp-plumber');
var filelog = require('gulp-filelog');
var cleanDest = require('gulp-clean-dest');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var webpack = require('webpack');
var bump = require('gulp-bump');
var react = require('gulp-react');
var flow = require('gulp-flowtype');
var argv = require('yargs').argv;
// var header = require('gulp-header');

var pkg = require('./package.json');

var sjclSrc = [
'src/js/sjcl/core/sjcl.js',
'src/js/sjcl/core/aes.js',
'src/js/sjcl/core/bitArray.js',
'src/js/sjcl/core/codecString.js',
'src/js/sjcl/core/codecHex.js',
'src/js/sjcl/core/codecBase64.js',
'src/js/sjcl/core/codecBytes.js',
'src/js/sjcl/core/sha256.js',
'src/js/sjcl/core/sha512.js',
'src/js/sjcl/core/sha1.js',
'src/js/sjcl/core/ccm.js',
// 'src/js/sjcl/core/cbc.js',
// 'src/js/sjcl/core/ocb2.js',
'src/js/sjcl/core/hmac.js',
'src/js/sjcl/core/pbkdf2.js',
'src/js/sjcl/core/random.js',
'src/js/sjcl/core/convenience.js',
'src/js/sjcl/core/bn.js',
'src/js/sjcl/core/ecc.js',
'src/js/sjcl/core/srp.js',
'src/js/sjcl-custom/sjcl-ecc-pointextras.js',
'src/js/sjcl-custom/sjcl-secp256k1.js',
'src/js/sjcl-custom/sjcl-ripemd160.js',
'src/js/sjcl-custom/sjcl-extramath.js',
'src/js/sjcl-custom/sjcl-montgomery.js',
'src/js/sjcl-custom/sjcl-validecc.js',
'src/js/sjcl-custom/sjcl-ecdsa-canonical.js',
'src/js/sjcl-custom/sjcl-ecdsa-der.js',
'src/js/sjcl-custom/sjcl-ecdsa-recoverablepublickey.js',
'src/js/sjcl-custom/sjcl-jacobi.js'
];

function logPluginError(error) {
gutil.log(error.toString());
}

gulp.task('concat-sjcl', function() {
return gulp.src(sjclSrc)
.pipe(concat('sjcl.js'))
.pipe(gulp.dest('./build/'));
});

gulp.task('build', ['concat-sjcl'], function(callback) {
gulp.task('build', function(callback) {
webpack({
cache: true,
entry: './src/js/ripple/index.js',
Expand All @@ -79,7 +38,7 @@ gulp.task('build-min', ['build'], function() {
.pipe(gulp.dest('./build/'));
});

gulp.task('build-debug', ['concat-sjcl'], function(callback) {
gulp.task('build-debug', function(callback) {
webpack({
cache: true,
entry: './src/js/ripple/index.js',
Expand All @@ -104,7 +63,7 @@ function buildUseError(cons) {
.replace(new RegExp('<CONS>', 'g'), cons);
}

gulp.task('build-core', ['concat-sjcl'], function(callback) {
gulp.task('build-core', function(callback) {
webpack({
entry: [
'./src/js/ripple/remote.js'
Expand Down Expand Up @@ -194,4 +153,4 @@ gulp.task('version-beta', function() {
.pipe(gulp.dest('./'));
});

gulp.task('default', ['concat-sjcl', 'build', 'build-debug', 'build-min']);
gulp.task('default', ['build', 'build-debug', 'build-min']);
4 changes: 4 additions & 0 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"lodash": "^3.1.0",
"lru-cache": "~2.5.0",
"ripple-wallet-generator": "^1.0.3",
"sjcl": "^1.0.2",
"ws": "~0.7.1"
},
"devDependencies": {
Expand All @@ -30,7 +31,6 @@
"gulp": "~3.8.10",
"gulp-bump": "~0.1.13",
"gulp-clean-dest": "^0.1.0",
"gulp-concat": "~2.4.3",
"gulp-filelog": "^0.4.1",
"gulp-flowtype": "^0.4.1",
"gulp-plumber": "^0.6.6",
Expand All @@ -48,7 +48,7 @@
},
"scripts": {
"build": "node_modules/.bin/gulp",
"pretest": "node_modules/.bin/gulp concat-sjcl",
"postinstall": "cd node_modules/sjcl; ./configure --with-all --compress=none; make",
"test": "./node_modules/.bin/istanbul test -x build/sjcl.js -x src/js/jsbn/* ./node_modules/mocha/bin/_mocha -- --reporter ${MOCHA_REPORTER:=spec} --timeout 10000 --slow 500 test/*-test.js",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
"lint": "if ! [ -f eslintrc ]; then curl -o eslintrc 'https://raw.githubusercontent.com/ripple/javascript-style-guide/master/eslintrc'; fi; eslint --reset -c eslintrc src/js/ripple/*.js",
Expand Down
5 changes: 2 additions & 3 deletions src/js/ripple/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ exports.toTimestamp = toTimestamp;
exports.fromTimestamp = fromTimestamp;
exports.getMantissaDecimalString = getMantissaDecimalString;

// Going up three levels is needed to escape the src-cov folder used for the
// test coverage stuff.
exports.sjcl = require('../../../build/sjcl');
exports.sjcl = require('sjcl');
require('../sjcl-custom');

// vim:sw=2:sts=2:ts=8:et
11 changes: 11 additions & 0 deletions src/js/sjcl-custom/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';
require('./sjcl-ecc-pointextras.js');
require('./sjcl-secp256k1.js');
require('./sjcl-ripemd160.js');
require('./sjcl-extramath.js');
require('./sjcl-montgomery.js');
require('./sjcl-validecc.js');
require('./sjcl-ecdsa-canonical.js');
require('./sjcl-ecdsa-der.js');
require('./sjcl-ecdsa-recoverablepublickey.js');
require('./sjcl-jacobi.js');
17 changes: 10 additions & 7 deletions src/js/sjcl-custom/sjcl-ecc-pointextras.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* eslint new-cap: [2, {newIsCapExceptions: ["bn"]}] */
'use strict';
var sjcl = require('sjcl');
/**
* Check that the point is valid based on the method described in
* SEC 1: Elliptic Curve Cryptography, section 3.2.2.1:
* SEC 1: Elliptic Curve Cryptography, section 3.2.2.1:
* Elliptic Curve Public Key Validation Primitive
* http://www.secg.org/download/aid-780/sec1-v2.pdf
*
* @returns {Boolean}
* @returns {Boolean} true if point is valid
*/
sjcl.ecc.point.prototype.isValidPoint = function() {

Expand Down Expand Up @@ -48,34 +51,34 @@ sjcl.ecc.point.prototype.isValidPoint = function() {
/**
* Check that the point is on the curve
*
* @returns {Boolean}
* @returns {Boolean} true if point is on the curve
*/
sjcl.ecc.point.prototype.isOnCurve = function() {

var self = this;

var field_order = self.curve.r;
var component_a = self.curve.a;
var component_b = self.curve.b;
var field_modulus = self.curve.field.modulus;

var left_hand_side = self.y.mul(self.y).mod(field_modulus);
var right_hand_side = self.x.mul(self.x).mul(self.x).add(component_a.mul(self.x)).add(component_b).mod(field_modulus);
var right_hand_side = self.x.mul(self.x).mul(self.x).add(
component_a.mul(self.x)).add(component_b).mod(field_modulus);

return left_hand_side.equals(right_hand_side);

};


sjcl.ecc.point.prototype.toString = function() {
return '(' +
return '(' +
this.x.toString() + ', ' +
this.y.toString() +
')';
};

sjcl.ecc.pointJac.prototype.toString = function() {
return '(' +
return '(' +
this.x.toString() + ', ' +
this.y.toString() + ', ' +
this.z.toString() +
Expand Down
7 changes: 5 additions & 2 deletions src/js/sjcl-custom/sjcl-ecdsa-canonical.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
'use strict';
var sjcl = require('sjcl');

sjcl.ecc.ecdsa.secretKey.prototype.canonicalizeSignature = function(rs) {
var w = sjcl.bitArray,
R = this._curve.r,
l = R.bitLength();

var r = sjcl.bn.fromBits(w.bitSlice(rs,0,l)),
s = sjcl.bn.fromBits(w.bitSlice(rs,l,2*l));
var r = sjcl.bn.fromBits(w.bitSlice(rs, 0, l)),
s = sjcl.bn.fromBits(w.bitSlice(rs, l, 2 * l));

// For a canonical signature we want the lower of two possible values for s
// 0 < s <= n/2
Expand Down
23 changes: 17 additions & 6 deletions src/js/sjcl-custom/sjcl-ecdsa-der.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
'use strict';
var sjcl = require('sjcl');

sjcl.ecc.ecdsa.secretKey.prototype.signDER = function(hash, paranoia) {
return this.encodeDER(this.sign(hash, paranoia));
};
Expand All @@ -7,16 +10,24 @@ sjcl.ecc.ecdsa.secretKey.prototype.encodeDER = function(rs) {
R = this._curve.r,
l = R.bitLength();

var rb = sjcl.codec.bytes.fromBits(w.bitSlice(rs,0,l)),
sb = sjcl.codec.bytes.fromBits(w.bitSlice(rs,l,2*l));
var rb = sjcl.codec.bytes.fromBits(w.bitSlice(rs, 0, l)),
sb = sjcl.codec.bytes.fromBits(w.bitSlice(rs, l, 2 * l));

// Drop empty leading bytes
while (!rb[0] && rb.length) rb.shift();
while (!sb[0] && sb.length) sb.shift();
while (!rb[0] && rb.length) {
rb.shift();
}
while (!sb[0] && sb.length) {
sb.shift();
}

// If high bit is set, prepend an extra zero byte (DER signed integer)
if (rb[0] & 0x80) rb.unshift(0);
if (sb[0] & 0x80) sb.unshift(0);
if (rb[0] & 0x80) {
rb.unshift(0);
}
if (sb[0] & 0x80) {
sb.unshift(0);
}

var buffer = [].concat(
0x30,
Expand Down
Loading

0 comments on commit ab943f3

Please sign in to comment.