Skip to content

Commit

Permalink
bn: make .strip() an internal method (see indutny#105)
Browse files Browse the repository at this point in the history
(cherry picked from commit e88e7b7)
  • Loading branch information
axic authored and ivanshukhov committed Jul 31, 2020
1 parent f9f8487 commit 0a3148d
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions lib/bn.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
this.negative = 1;
}

this.strip();
this._strip();

if (endian !== 'le') return;

Expand Down Expand Up @@ -180,7 +180,7 @@
}
}
}
return this.strip();
return this._strip();
};

function parseHex (str, start, end) {
Expand Down Expand Up @@ -234,11 +234,12 @@
this.words[j] |= (w << off) & 0x3ffffff;
this.words[j + 1] |= w >>> (26 - off) & 0x3fffff;
}
this.strip();
this._strip();
};

function parseBase (str, start, end, mul) {
var r = 0;
var b = 0;
var len = Math.min(str.length, end);
for (var i = start; i < len; i++) {
var c = str.charCodeAt(i) - 48;
Expand All @@ -247,16 +248,18 @@

// 'a'
if (c >= 49) {
r += c - 49 + 0xa;
b = c - 49 + 0xa;

// 'A'
} else if (c >= 17) {
r += c - 17 + 0xa;
b = c - 17 + 0xa;

// '0' - '9'
} else {
r += c;
b = c;
}
assert(b < mul, 'Invalid character');
r += b;
}
return r;
}
Expand Down Expand Up @@ -330,7 +333,7 @@
};

// Remove leading `0` from `this`
BN.prototype.strip = function strip () {
BN.prototype._strip = function strip () {
while (this.length > 1 && this.words[this.length - 1] === 0) {
this.length--;
}
Expand Down Expand Up @@ -509,7 +512,7 @@
};

BN.prototype.toJSON = function toJSON () {
return this.toString(16);
return this.toString(16, 2);
};

BN.prototype.toBuffer = function toBuffer (endian, length) {
Expand All @@ -533,7 +536,7 @@
assert(byteLength <= reqLength, 'byte array longer than desired length');
assert(reqLength > 0, 'Requested array length <= 0');

this.strip();
this._strip();
var littleEndian = endian === 'le';
var res = allocate(ArrayType, reqLength);

Expand Down Expand Up @@ -708,7 +711,7 @@
this.words[i] = this.words[i] | num.words[i];
}

return this.strip();
return this._strip();
};

BN.prototype.ior = function ior (num) {
Expand Down Expand Up @@ -743,7 +746,7 @@

this.length = b.length;

return this.strip();
return this._strip();
};

BN.prototype.iand = function iand (num) {
Expand Down Expand Up @@ -787,7 +790,7 @@

this.length = a.length;

return this.strip();
return this._strip();
};

BN.prototype.ixor = function ixor (num) {
Expand Down Expand Up @@ -831,7 +834,7 @@
}

// And remove leading zeroes
return this.strip();
return this._strip();
};

BN.prototype.notn = function notn (width) {
Expand All @@ -853,7 +856,7 @@
this.words[off] = this.words[off] & ~(1 << wbit);
}

return this.strip();
return this._strip();
};

// Add `num` to `this` in-place
Expand Down Expand Up @@ -994,7 +997,7 @@
this.negative = 1;
}

return this.strip();
return this._strip();
};

// Subtract `num` from `this`
Expand Down Expand Up @@ -1040,7 +1043,7 @@
out.length--;
}

return out.strip();
return out._strip();
}

// TODO(indutny): it may be reasonable to omit it for users who don't need
Expand Down Expand Up @@ -1662,7 +1665,7 @@
out.length--;
}

return out.strip();
return out._strip();
}

function jumboMulTo (self, num, out) {
Expand Down Expand Up @@ -1879,7 +1882,7 @@

out.negative = x.negative ^ y.negative;
out.length = x.length + y.length;
return out.strip();
return out._strip();
};

// Multiply `this` by `num`
Expand Down Expand Up @@ -1997,7 +2000,7 @@
this.length += s;
}

return this.strip();
return this._strip();
};

BN.prototype.ishln = function ishln (bits) {
Expand Down Expand Up @@ -2065,7 +2068,7 @@
this.length = 1;
}

return this.strip();
return this._strip();
};

BN.prototype.ishrn = function ishrn (bits, hint, extended) {
Expand Down Expand Up @@ -2132,7 +2135,7 @@
this.words[this.length - 1] &= mask;
}

return this.strip();
return this._strip();
};

// Return only lowers bits of number
Expand Down Expand Up @@ -2207,7 +2210,7 @@
}
}

return this.strip();
return this._strip();
};

BN.prototype.addn = function addn (num) {
Expand Down Expand Up @@ -2249,7 +2252,7 @@
this.words[i + shift] = w & 0x3ffffff;
}

if (carry === 0) return this.strip();
if (carry === 0) return this._strip();

// Subtraction overflow
assert(carry === -1);
Expand All @@ -2261,7 +2264,7 @@
}
this.negative = 1;

return this.strip();
return this._strip();
};

BN.prototype._wordDiv = function _wordDiv (num, mode) {
Expand Down Expand Up @@ -2323,9 +2326,9 @@
}
}
if (q) {
q.strip();
q._strip();
}
a.strip();
a._strip();

// Denormalize
if (mode !== 'div' && shift !== 0) {
Expand Down Expand Up @@ -2494,7 +2497,7 @@
carry = w % num;
}

return this.strip();
return this._strip();
};

BN.prototype.divn = function divn (num) {
Expand Down Expand Up @@ -2746,7 +2749,7 @@
if (this.negative !== 0 && !negative) return -1;
if (this.negative === 0 && negative) return 1;

this.strip();
this._strip();

var res;
if (this.length > 1) {
Expand Down

0 comments on commit 0a3148d

Please sign in to comment.