From d1853b2e5280f099d8c2f619d5518e68d3155ea9 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 18 May 2014 10:24:10 -0500 Subject: [PATCH] util: simplify 64bit writes using bn.js. Signed-off-by: Fedor Indutny --- lib/bcoin/utils.js | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index 92841158..319b00f8 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -180,19 +180,9 @@ utils.writeU64 = function writeU64(dst, num, off) { if (!off) off = 0; - var num = new bn(num); - var left = +num.shrn(32).toString(10); - var right = +num.maskn(32).toString(10); - - dst[off] = right & 0xff; - dst[off + 1] = (right >>> 8) & 0xff; - dst[off + 2] = (right >>> 16) & 0xff; - dst[off + 3] = (right >>> 24) & 0xff; - - dst[off + 4] = left & 0xff; - dst[off + 5] = (left >>> 8) & 0xff; - dst[off + 6] = (left >>> 16) & 0xff; - dst[off + 7] = (left >>> 24) & 0xff; + new bn(num).toArray().reverse().forEach(function(ch, i) { + dst[off + i] = ch; + }); return 8; }; @@ -219,19 +209,9 @@ utils.writeU64BE = function writeU64BE(dst, num, off) { if (!off) off = 0; - var num = new bn(num); - var left = +num.shrn(32).toString(10); - var right = +num.maskn(32).toString(10); - - dst[off] = (left >>> 24) & 0xff; - dst[off + 1] = (left >>> 16) & 0xff; - dst[off + 2] = (left >>> 8) & 0xff; - dst[off + 3] = left & 0xff; - - dst[off + 4] = (right >>> 24) & 0xff; - dst[off + 5] = (right >>> 16) & 0xff; - dst[off + 6] = (right >>> 8) & 0xff; - dst[off + 7] = right & 0xff; + new bn(num).toArray().forEach(function(ch, i) { + dst[off + i] = ch; + }); return 8; };