Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

Commit

Permalink
[TASK] Use an external sjcl (vs ripple-lib)
Browse files Browse the repository at this point in the history
  • Loading branch information
vhpoet committed Sep 16, 2015
1 parent 0340024 commit 4f31050
Show file tree
Hide file tree
Showing 11 changed files with 780 additions and 4 deletions.
8 changes: 8 additions & 0 deletions deps/sjcl-custom/index.js
@@ -0,0 +1,8 @@
'use strict';
require('./sjcl-ecc-pointextras.js');
require('./sjcl-secp256k1.js');
require('./sjcl-extramath.js');
require('./sjcl-validecc.js');
require('./sjcl-ecdsa-canonical.js');
require('./sjcl-ecdsa-der.js');
require('./sjcl-ecdsa-recoverablepublickey.js');
86 changes: 86 additions & 0 deletions deps/sjcl-custom/sjcl-ecc-pointextras.js
@@ -0,0 +1,86 @@
/* 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:
* Elliptic Curve Public Key Validation Primitive
* http://www.secg.org/download/aid-780/sec1-v2.pdf
*
* @returns {Boolean} true if point is valid
*/
sjcl.ecc.point.prototype.isValidPoint = function() {

var self = this;

var field_modulus = self.curve.field.modulus;

if (self.isIdentity) {
return false;
}

// Check that coordinatres are in bounds
// Return false if x < 1 or x > (field_modulus - 1)
if (((new sjcl.bn(1).greaterEquals(self.x)) &&
!self.x.equals(1)) ||
(self.x.greaterEquals(field_modulus.sub(1))) &&
!self.x.equals(1)) {

return false;
}

// Return false if y < 1 or y > (field_modulus - 1)
if (((new sjcl.bn(1).greaterEquals(self.y)) &&
!self.y.equals(1)) ||
(self.y.greaterEquals(field_modulus.sub(1))) &&
!self.y.equals(1)) {

return false;
}

if (!self.isOnCurve()) {
return false;
}

// TODO check to make sure point is a scalar multiple of base_point

return true;

};

/**
* Check that the point is on the curve
*
* @returns {Boolean} true if point is on the curve
*/
sjcl.ecc.point.prototype.isOnCurve = function() {

var self = this;

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

return left_hand_side.equals(right_hand_side);

};


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

sjcl.ecc.pointJac.prototype.toString = function() {
return '(' +
this.x.toString() + ', ' +
this.y.toString() + ', ' +
this.z.toString() +
')';
};
20 changes: 20 additions & 0 deletions deps/sjcl-custom/sjcl-ecdsa-canonical.js
@@ -0,0 +1,20 @@
'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));

// For a canonical signature we want the lower of two possible values for s
// 0 < s <= n/2
if (!R.copy().halveM().greaterEquals(s)) {
s = R.sub(s);
}

return w.concat(r.toBits(l), s.toBits(l));
};

45 changes: 45 additions & 0 deletions deps/sjcl-custom/sjcl-ecdsa-der.js
@@ -0,0 +1,45 @@
'use strict';
var sjcl = require('sjcl');

sjcl.ecc.ecdsa.secretKey.prototype.signDER = function(hash, paranoia) {
return this.encodeDER(this.sign(hash, paranoia));
};

sjcl.ecc.ecdsa.secretKey.prototype.encodeDER = function(rs) {
var w = sjcl.bitArray,
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));

// Drop empty leading bytes
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);
}

var buffer = [].concat(
0x30,
4 + rb.length + sb.length,
0x02,
rb.length,
rb,
0x02,
sb.length,
sb
);

return sjcl.codec.bytes.toBits(buffer);
};

0 comments on commit 4f31050

Please sign in to comment.