[Pkg-javascript-commits] [node-hash.js] 19/29: utils: speed up 64bit ops by 20%

Bastien Roucariès rouca at moszumanska.debian.org
Thu Apr 20 19:30:40 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-hash.js.

commit d5e7f9135f98af883a3df373c09358920d405e5c
Author: Fedor Indutny <fedor at indutny.com>
Date:   Tue Jan 6 02:01:44 2015 +0300

    utils: speed up 64bit ops by 20%
---
 lib/hash/sha.js   | 11 +++++----
 lib/hash/utils.js | 71 +++++++++++++++++++++++++++++++++++++------------------
 2 files changed, 54 insertions(+), 28 deletions(-)

diff --git a/lib/hash/sha.js b/lib/hash/sha.js
index affe767..ffff976 100644
--- a/lib/hash/sha.js
+++ b/lib/hash/sha.js
@@ -201,7 +201,7 @@ SHA512.prototype._update = function _update(msg, start) {
   for (var i = 0; i < 16; i++)
     W[i] = new Num64(msg[start + 2 * i], msg[start + 2 * i + 1]);
   for (; i < W.length; i++)
-    W[i] = g1_512(W[i - 2]).sum3(W[i - 7], g0_512(W[i - 15]), W[i - 16]);
+    W[i] = g1_512(W[i - 2]).isum3(W[i - 7], g0_512(W[i - 15]), W[i - 16]);
 
   var a = this.h[0];
   var b = this.h[1];
@@ -214,8 +214,8 @@ SHA512.prototype._update = function _update(msg, start) {
 
   assert(this.k.length === W.length);
   for (var i = 0; i < W.length; i++) {
-    var T1 = h.sum4(s1_512(e), ch64(e, f, g), this.k[i], W[i]);
-    var T2 = s0_512(a).sum(maj64(a, b, c));
+    var T1 = h.clone().isum4(s1_512(e), ch64(e, f, g), this.k[i], W[i]);
+    var T2 = s0_512(a).isum(maj64(a, b, c));
     h = g;
     g = f;
     f = e;
@@ -223,7 +223,7 @@ SHA512.prototype._update = function _update(msg, start) {
     d = c;
     c = b;
     b = a;
-    a = T1.sum(T2);
+    a = T1.isum(T2);
   }
 
   this.h[0].isum(a);
@@ -381,7 +381,8 @@ function maj64(x, y, z) {
 }
 
 function s0_512(x, y, z) {
-  return x.rotr(28).xor2(x.rotr(34), x.rotr(39));
+  var r = x.rotr(34);
+  return x.rotr(28).xor2(r, r.rotr(5));
 }
 
 function s1_512(x, y, z) {
diff --git a/lib/hash/utils.js b/lib/hash/utils.js
index 33ef3b9..32f2d2f 100644
--- a/lib/hash/utils.js
+++ b/lib/hash/utils.js
@@ -187,7 +187,7 @@ function rotl32(w, b) {
 utils.rotl32 = rotl32;
 
 function sum32(a, b) {
-  var r = (a + b) & 0xffffffff;
+  var r = (a + b) | 0;
   if (r < 0)
     r += 0x100000000;
   return r;
@@ -195,7 +195,7 @@ function sum32(a, b) {
 utils.sum32 = sum32;
 
 function sum32_3(a, b, c) {
-  var r = (a + b + c) & 0xffffffff;
+  var r = (a + b + c) | 0;
   if (r < 0)
     r += 0x100000000;
   return r;
@@ -203,7 +203,7 @@ function sum32_3(a, b, c) {
 utils.sum32_3 = sum32_3;
 
 function sum32_4(a, b, c, d) {
-  var r = (a + b + c + d) & 0xffffffff;
+  var r = (a + b + c + d) | 0;
   if (r < 0)
     r += 0x100000000;
   return r;
@@ -211,7 +211,7 @@ function sum32_4(a, b, c, d) {
 utils.sum32_4 = sum32_4;
 
 function sum32_5(a, b, c, d, e) {
-  var r = (a + b + c + d + e) & 0xffffffff;
+  var r = (a + b + c + d + e) | 0;
   if (r < 0)
     r += 0x100000000;
   return r;
@@ -227,14 +227,9 @@ utils.assert = assert;
 utils.inherits = inherits;
 
 function Num64(hi, lo) {
-  if (hi < 0)
-    this.hi = hi + 0x100000000;
-  else
-    this.hi = hi;
-  if (lo < 0)
-    this.lo = lo + 0x100000000;
-  else
-    this.lo = lo;
+  this.hi = hi;
+  this.lo = lo;
+  this.normalize();
 }
 utils.Num64 = Num64;
 
@@ -246,6 +241,13 @@ Num64.prototype.clone = function clone() {
   return new Num64(this.hi, this.lo);
 };
 
+Num64.prototype.normalize = function normalize() {
+  if (this.hi < 0)
+    this.hi += 0x100000000;
+  if (this.lo < 0)
+    this.lo += 0x100000000;
+};
+
 Num64.prototype.xor2 = function xor2(a, b) {
   return new Num64(
     a.hi ^ b.hi ^ this.hi,
@@ -253,24 +255,47 @@ Num64.prototype.xor2 = function xor2(a, b) {
   );
 };
 
-Num64.prototype.sum3 = function sum3(a, b, c) {
-  return this.clone().isum(a).isum(b).isum(c);
+Num64.prototype.isum3 = function isum3(a, b, c) {
+  var lo = this.lo + a.lo + b.lo + c.lo;
+  var hi = this.hi + a.hi + b.hi + c.hi + ((lo / 0x100000000) | 0);
+
+  this.lo = lo | 0;
+  this.hi = hi | 0;
+  this.normalize();
+
+  return this;
 };
 
-Num64.prototype.sum4 = function sum4(a, b, c, d) {
-  return this.clone().isum(a).isum(b).isum(c).isum(d);
+Num64.prototype.isum4 = function isum4(a, b, c, d) {
+  var lo = this.lo + a.lo + b.lo + c.lo + d.lo;
+  var hi = this.hi + a.hi + b.hi + c.hi + d.hi + ((lo / 0x100000000) | 0);
+
+  this.lo = lo | 0;
+  this.hi = hi | 0;
+  this.normalize();
+
+  return this;
 };
 
-Num64.prototype.rotr = function rotr(num) {
-  if (num >= 32)
-    return new Num64(this.lo, this.hi).rotr(num - 32);
+Num64.prototype.rotr = function rotr(n) {
+  var hi;
+  var lo;
+  var num = n;
+  if (num >= 32) {
+    hi = this.lo | 0;
+    lo = this.hi | 0;
+    num -= 32;
+  } else {
+    hi = this.hi | 0;
+    lo = this.lo | 0;
+  }
 
   // num < 32
   var mask = (1 << num) - 1;
   var rnum = 32 - num;
   return new Num64(
-    ((this.lo & mask) << rnum) | (this.hi >>> num),
-    ((this.hi & mask) << rnum) | (this.lo >>> num)
+    ((lo & mask) << rnum) | (hi >>> num),
+    ((hi & mask) << rnum) | (lo >>> num)
   );
 };
 
@@ -288,10 +313,10 @@ Num64.prototype.shr = function shr(num) {
 
 Num64.prototype.isum = function isum(num) {
   var lo = this.lo + num.lo;
-  var hi = (((lo / 0x100000000) | 0) + this.hi + num.hi) & 0xffffffff;
+  var hi = (((lo / 0x100000000) | 0) + this.hi + num.hi) | 0;
 
   this.hi = hi;
-  this.lo = lo & 0xffffffff;
+  this.lo = lo | 0;
   if (this.hi < 0)
     this.hi += 0x100000000;
   if (this.lo < 0)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-hash.js.git



More information about the Pkg-javascript-commits mailing list