util: bignum - use toArray instead of +toString.

Signed-off-by: Fedor Indutny <fedor@indutny.com>
This commit is contained in:
Christopher Jeffrey 2014-05-18 10:34:38 -05:00 committed by Fedor Indutny
parent d1853b2e52
commit ca2609b7e7

View File

@ -180,9 +180,23 @@ utils.writeU64 = function writeU64(dst, num, off) {
if (!off)
off = 0;
new bn(num).toArray().reverse().forEach(function(ch, i) {
dst[off + i] = ch;
});
var num = new bn(num);
var left = num.shrn(32).toArray();
while (left.length < 4) left.unshift(0);
var right = num.maskn(32).toArray();
while (right.length < 4) right.unshift(0);
dst[off] = right[3];
dst[off + 1] = right[2];
dst[off + 2] = right[1];
dst[off + 3] = right[0];
dst[off + 4] = left[3];
dst[off + 5] = left[2];
dst[off + 6] = left[1];
dst[off + 7] = left[0];
return 8;
};
@ -209,9 +223,23 @@ utils.writeU64BE = function writeU64BE(dst, num, off) {
if (!off)
off = 0;
new bn(num).toArray().forEach(function(ch, i) {
dst[off + i] = ch;
});
var num = new bn(num);
var left = num.shrn(32).toArray();
while (left.length < 4) left.unshift(0);
var right = num.maskn(32).toArray();
while (right.length < 4) right.unshift(0);
dst[off] = left[0];
dst[off + 1] = left[1];
dst[off + 2] = left[2];
dst[off + 3] = left[3];
dst[off + 4] = right[0];
dst[off + 5] = right[1];
dst[off + 6] = right[2];
dst[off + 7] = right[3];
return 8;
};