[Pkg-javascript-commits] [node-hash.js] 20/29: sha: inline Num64 class
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 54edba6e1e1702238cb1c7438d8308f651b35a3e
Author: Fedor Indutny <fedor at indutny.com>
Date: Tue Jan 6 03:02:36 2015 +0300
sha: inline Num64 class
---
lib/hash/sha.js | 398 ++++++++++++++++++++++++++++++++++++++----------------
lib/hash/utils.js | 227 +++++++++++--------------------
2 files changed, 359 insertions(+), 266 deletions(-)
diff --git a/lib/hash/sha.js b/lib/hash/sha.js
index ffff976..a7837aa 100644
--- a/lib/hash/sha.js
+++ b/lib/hash/sha.js
@@ -7,7 +7,17 @@ var rotl32 = utils.rotl32;
var sum32 = utils.sum32;
var sum32_4 = utils.sum32_4;
var sum32_5 = utils.sum32_5;
-var Num64 = utils.Num64;
+var rotr64_hi = utils.rotr64_hi;
+var rotr64_lo = utils.rotr64_lo;
+var shr64_hi = utils.shr64_hi;
+var shr64_lo = utils.shr64_lo;
+var sum64 = utils.sum64;
+var sum64_hi = utils.sum64_hi;
+var sum64_lo = utils.sum64_lo;
+var sum64_4_hi = utils.sum64_4_hi;
+var sum64_4_lo = utils.sum64_4_lo;
+var sum64_5_hi = utils.sum64_5_hi;
+var sum64_5_lo = utils.sum64_5_lo;
var BlockHash = hash.common.BlockHash;
var sha256_K = [
@@ -30,50 +40,47 @@ var sha256_K = [
];
var sha512_K = [
- [ 0x428a2f98, 0xd728ae22], [ 0x71374491, 0x23ef65cd ],
- [ 0xb5c0fbcf, 0xec4d3b2f], [ 0xe9b5dba5, 0x8189dbbc ],
- [ 0x3956c25b, 0xf348b538], [ 0x59f111f1, 0xb605d019 ],
- [ 0x923f82a4, 0xaf194f9b], [ 0xab1c5ed5, 0xda6d8118 ],
- [ 0xd807aa98, 0xa3030242], [ 0x12835b01, 0x45706fbe ],
- [ 0x243185be, 0x4ee4b28c], [ 0x550c7dc3, 0xd5ffb4e2 ],
- [ 0x72be5d74, 0xf27b896f], [ 0x80deb1fe, 0x3b1696b1 ],
- [ 0x9bdc06a7, 0x25c71235], [ 0xc19bf174, 0xcf692694 ],
- [ 0xe49b69c1, 0x9ef14ad2], [ 0xefbe4786, 0x384f25e3 ],
- [ 0x0fc19dc6, 0x8b8cd5b5], [ 0x240ca1cc, 0x77ac9c65 ],
- [ 0x2de92c6f, 0x592b0275], [ 0x4a7484aa, 0x6ea6e483 ],
- [ 0x5cb0a9dc, 0xbd41fbd4], [ 0x76f988da, 0x831153b5 ],
- [ 0x983e5152, 0xee66dfab], [ 0xa831c66d, 0x2db43210 ],
- [ 0xb00327c8, 0x98fb213f], [ 0xbf597fc7, 0xbeef0ee4 ],
- [ 0xc6e00bf3, 0x3da88fc2], [ 0xd5a79147, 0x930aa725 ],
- [ 0x06ca6351, 0xe003826f], [ 0x14292967, 0x0a0e6e70 ],
- [ 0x27b70a85, 0x46d22ffc], [ 0x2e1b2138, 0x5c26c926 ],
- [ 0x4d2c6dfc, 0x5ac42aed], [ 0x53380d13, 0x9d95b3df ],
- [ 0x650a7354, 0x8baf63de], [ 0x766a0abb, 0x3c77b2a8 ],
- [ 0x81c2c92e, 0x47edaee6], [ 0x92722c85, 0x1482353b ],
- [ 0xa2bfe8a1, 0x4cf10364], [ 0xa81a664b, 0xbc423001 ],
- [ 0xc24b8b70, 0xd0f89791], [ 0xc76c51a3, 0x0654be30 ],
- [ 0xd192e819, 0xd6ef5218], [ 0xd6990624, 0x5565a910 ],
- [ 0xf40e3585, 0x5771202a], [ 0x106aa070, 0x32bbd1b8 ],
- [ 0x19a4c116, 0xb8d2d0c8], [ 0x1e376c08, 0x5141ab53 ],
- [ 0x2748774c, 0xdf8eeb99], [ 0x34b0bcb5, 0xe19b48a8 ],
- [ 0x391c0cb3, 0xc5c95a63], [ 0x4ed8aa4a, 0xe3418acb ],
- [ 0x5b9cca4f, 0x7763e373], [ 0x682e6ff3, 0xd6b2b8a3 ],
- [ 0x748f82ee, 0x5defb2fc], [ 0x78a5636f, 0x43172f60 ],
- [ 0x84c87814, 0xa1f0ab72], [ 0x8cc70208, 0x1a6439ec ],
- [ 0x90befffa, 0x23631e28], [ 0xa4506ceb, 0xde82bde9 ],
- [ 0xbef9a3f7, 0xb2c67915], [ 0xc67178f2, 0xe372532b ],
- [ 0xca273ece, 0xea26619c], [ 0xd186b8c7, 0x21c0c207 ],
- [ 0xeada7dd6, 0xcde0eb1e], [ 0xf57d4f7f, 0xee6ed178 ],
- [ 0x06f067aa, 0x72176fba], [ 0x0a637dc5, 0xa2c898a6 ],
- [ 0x113f9804, 0xbef90dae], [ 0x1b710b35, 0x131c471b ],
- [ 0x28db77f5, 0x23047d84], [ 0x32caab7b, 0x40c72493 ],
- [ 0x3c9ebe0a, 0x15c9bebc], [ 0x431d67c4, 0x9c100d4c ],
- [ 0x4cc5d4be, 0xcb3e42b6], [ 0x597f299c, 0xfc657e2a ],
- [ 0x5fcb6fab, 0x3ad6faec], [ 0x6c44198c, 0x4a475817 ]
+ 0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd,
+ 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
+ 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019,
+ 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
+ 0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe,
+ 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
+ 0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1,
+ 0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
+ 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3,
+ 0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
+ 0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483,
+ 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
+ 0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210,
+ 0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
+ 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725,
+ 0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
+ 0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926,
+ 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
+ 0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8,
+ 0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
+ 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001,
+ 0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
+ 0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910,
+ 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
+ 0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53,
+ 0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
+ 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb,
+ 0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
+ 0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60,
+ 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
+ 0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9,
+ 0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
+ 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207,
+ 0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
+ 0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6,
+ 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
+ 0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493,
+ 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
+ 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a,
+ 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817
];
-sha512_K = sha512_K.map(function(pair) {
- return new Num64(pair[0], pair[1]);
-});
var sha1_K = [
0x5A827999, 0x6ED9EBA1,
@@ -175,16 +182,16 @@ function SHA512() {
return new SHA512();
BlockHash.call(this);
- this.h = [ new Num64(0x6a09e667, 0xf3bcc908),
- new Num64(0xbb67ae85, 0x84caa73b),
- new Num64(0x3c6ef372, 0xfe94f82b),
- new Num64(0xa54ff53a, 0x5f1d36f1),
- new Num64(0x510e527f, 0xade682d1),
- new Num64(0x9b05688c, 0x2b3e6c1f),
- new Num64(0x1f83d9ab, 0xfb41bd6b),
- new Num64(0x5be0cd19, 0x137e2179) ];
+ this.h = [ 0x6a09e667, 0xf3bcc908,
+ 0xbb67ae85, 0x84caa73b,
+ 0x3c6ef372, 0xfe94f82b,
+ 0xa54ff53a, 0x5f1d36f1,
+ 0x510e527f, 0xade682d1,
+ 0x9b05688c, 0x2b3e6c1f,
+ 0x1f83d9ab, 0xfb41bd6b,
+ 0x5be0cd19, 0x137e2179 ];
this.k = sha512_K;
- this.W = new Array(80);
+ this.W = new Array(160);
}
utils.inherits(SHA512, BlockHash);
exports.sha512 = SHA512;
@@ -194,53 +201,127 @@ SHA512.outSize = 512;
SHA512.hmacStrength = 192;
SHA512.padLength = 128;
-SHA512.prototype._update = function _update(msg, start) {
+SHA512.prototype._prepareBlock = function _prepareBlock(msg, start) {
var W = this.W;
// 32 x 32bit words
- 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]).isum3(W[i - 7], g0_512(W[i - 15]), W[i - 16]);
+ for (var i = 0; i < 32; i++)
+ W[i] = msg[start + i];
+ for (; i < W.length; i += 2) {
+ var c0_hi = g1_512_hi(W[i - 4], W[i - 3]); // i - 2
+ var c0_lo = g1_512_lo(W[i - 4], W[i - 3]);
+ var c1_hi = W[i - 14]; // i - 7
+ var c1_lo = W[i - 13];
+ var c2_hi = g0_512_hi(W[i - 30], W[i - 29]); // i - 15
+ var c2_lo = g0_512_lo(W[i - 30], W[i - 29]);
+ var c3_hi = W[i - 32]; // i - 16
+ var c3_lo = W[i - 31];
+
+ W[i] = sum64_4_hi(c0_hi, c0_lo,
+ c1_hi, c1_lo,
+ c2_hi, c2_lo,
+ c3_hi, c3_lo);
+ W[i + 1] = sum64_4_lo(c0_hi, c0_lo,
+ c1_hi, c1_lo,
+ c2_hi, c2_lo,
+ c3_hi, c3_lo);
+ }
+};
- var a = this.h[0];
- var b = this.h[1];
- var c = this.h[2];
- var d = this.h[3];
- var e = this.h[4];
- var f = this.h[5];
- var g = this.h[6];
- var h = this.h[7];
+SHA512.prototype._update = function _update(msg, start) {
+ this._prepareBlock(msg, start);
+
+ var W = this.W;
+
+ var ah = this.h[0];
+ var al = this.h[1];
+ var bh = this.h[2];
+ var bl = this.h[3];
+ var ch = this.h[4];
+ var cl = this.h[5];
+ var dh = this.h[6];
+ var dl = this.h[7];
+ var eh = this.h[8];
+ var el = this.h[9];
+ var fh = this.h[10];
+ var fl = this.h[11];
+ var gh = this.h[12];
+ var gl = this.h[13];
+ var hh = this.h[14];
+ var hl = this.h[15];
assert(this.k.length === W.length);
- for (var i = 0; i < W.length; i++) {
- 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;
- e = d.sum(T1);
- d = c;
- c = b;
- b = a;
- a = T1.isum(T2);
+ for (var i = 0; i < W.length; i += 2) {
+ var c0_hi = hh;
+ var c0_lo = hl;
+ var c1_hi = s1_512_hi(eh, el);
+ var c1_lo = s1_512_lo(eh, el);
+ var c2_hi = ch64_hi(eh, el, fh, fl, gh, gl);
+ var c2_lo = ch64_lo(eh, el, fh, fl, gh, gl);
+ var c3_hi = this.k[i];
+ var c3_lo = this.k[i + 1];
+ var c4_hi = W[i];
+ var c4_lo = W[i + 1];
+
+ var T1_hi = sum64_5_hi(c0_hi, c0_lo,
+ c1_hi, c1_lo,
+ c2_hi, c2_lo,
+ c3_hi, c3_lo,
+ c4_hi, c4_lo);
+ var T1_lo = sum64_5_lo(c0_hi, c0_lo,
+ c1_hi, c1_lo,
+ c2_hi, c2_lo,
+ c3_hi, c3_lo,
+ c4_hi, c4_lo);
+
+ var c0_hi = s0_512_hi(ah, al);
+ var c0_lo = s0_512_lo(ah, al);
+ var c1_hi = maj64_hi(ah, al, bh, bl, ch, cl);
+ var c1_lo = maj64_lo(ah, al, bh, bl, ch, cl);
+
+ var T2_hi = sum64_hi(c0_hi, c0_lo, c1_hi, c1_lo);
+ var T2_lo = sum64_lo(c0_hi, c0_lo, c1_hi, c1_lo);
+
+ hh = gh;
+ hl = gl;
+
+ gh = fh;
+ gl = fl;
+
+ fh = eh;
+ fl = el;
+
+ eh = sum64_hi(dh, dl, T1_hi, T1_lo);
+ el = sum64_lo(dl, dl, T1_hi, T1_lo);
+
+ dh = ch;
+ dl = cl;
+
+ ch = bh;
+ cl = bl;
+
+ bh = ah;
+ bl = al;
+
+ ah = sum64_hi(T1_hi, T1_lo, T2_hi, T2_lo);
+ al = sum64_lo(T1_hi, T1_lo, T2_hi, T2_lo);
}
- this.h[0].isum(a);
- this.h[1].isum(b);
- this.h[2].isum(c);
- this.h[3].isum(d);
- this.h[4].isum(e);
- this.h[5].isum(f);
- this.h[6].isum(g);
- this.h[7].isum(h);
+ sum64(this.h, 0, ah, al);
+ sum64(this.h, 2, bh, bl);
+ sum64(this.h, 4, ch, cl);
+ sum64(this.h, 6, dh, dl);
+ sum64(this.h, 8, eh, el);
+ sum64(this.h, 10, fh, fl);
+ sum64(this.h, 12, gh, gl);
+ sum64(this.h, 14, hh, hl);
};
SHA512.prototype._digest = function digest(enc) {
if (enc === 'hex')
- return utils.toHex64(this.h, 'big');
+ return utils.toHex32(this.h, 'big');
else
- return utils.split64(this.h, 'big');
+ return utils.split32(this.h, 'big');
};
function SHA384() {
@@ -248,14 +329,14 @@ function SHA384() {
return new SHA384();
SHA512.call(this);
- this.h = [ new Num64(0xcbbb9d5d, 0xc1059ed8),
- new Num64(0x629a292a, 0x367cd507),
- new Num64(0x9159015a, 0x3070dd17),
- new Num64(0x152fecd8, 0xf70e5939),
- new Num64(0x67332667, 0xffc00b31),
- new Num64(0x8eb44a87, 0x68581511),
- new Num64(0xdb0c2e0d, 0x64f98fa7),
- new Num64(0x47b5481d, 0xbefa4fa4) ];
+ this.h = [ 0xcbbb9d5d, 0xc1059ed8,
+ 0x629a292a, 0x367cd507,
+ 0x9159015a, 0x3070dd17,
+ 0x152fecd8, 0xf70e5939,
+ 0x67332667, 0xffc00b31,
+ 0x8eb44a87, 0x68581511,
+ 0xdb0c2e0d, 0x64f98fa7,
+ 0x47b5481d, 0xbefa4fa4 ];
}
utils.inherits(SHA384, SHA512);
exports.sha384 = SHA384;
@@ -267,9 +348,9 @@ SHA384.padLength = 128;
SHA384.prototype._digest = function digest(enc) {
if (enc === 'hex')
- return utils.toHex64(this.h.slice(0, 6), 'big');
+ return utils.toHex32(this.h.slice(0, 12), 'big');
else
- return utils.split64(this.h.slice(0, 6), 'big');
+ return utils.split32(this.h.slice(0, 12), 'big');
};
function SHA1() {
@@ -366,33 +447,118 @@ function ft_1(s, x, y, z) {
return maj32(x, y, z);
}
-function ch64(x, y, z) {
- return new Num64(
- (x.hi & y.hi) ^ ((~x.hi) & z.hi),
- (x.lo & y.lo) ^ ((~x.lo) & z.lo)
- );
+function ch64_hi(xh, xl, yh, yl, zh, zl) {
+ var r = (xh & yh) ^ ((~xh) & zh);
+ if (r < 0)
+ r += 0x100000000;
+ return r;
+}
+
+function ch64_lo(xh, xl, yh, yl, zh, zl) {
+ var r = (xl & yl) ^ ((~xl) & zl);
+ if (r < 0)
+ r += 0x100000000;
+ return r;
+}
+
+function maj64_hi(xh, xl, yh, yl, zh, zl) {
+ var r = (xh & yh) ^ (xh & zh) ^ (yh & zh);
+ if (r < 0)
+ r += 0x100000000;
+ return r;
+}
+
+function maj64_lo(xh, xl, yh, yl, zh, zl) {
+ var r = (xl & yl) ^ (xl & zl) ^ (yl & zl);
+ if (r < 0)
+ r += 0x100000000;
+ return r;
+}
+
+function s0_512_hi(xh, xl) {
+ var c0_hi = rotr64_hi(xh, xl, 28);
+ var c1_hi = rotr64_hi(xl, xh, 2); // 34
+ var c2_hi = rotr64_hi(xl, xh, 7); // 39
+
+ var r = c0_hi ^ c1_hi ^ c2_hi;
+ if (r < 0)
+ r += 0x100000000;
+ return r;
+}
+
+function s0_512_lo(xh, xl) {
+ var c0_lo = rotr64_lo(xh, xl, 28);
+ var c1_lo = rotr64_lo(xl, xh, 2); // 34
+ var c2_lo = rotr64_lo(xl, xh, 7); // 39
+
+ var r = c0_lo ^ c1_lo ^ c2_lo;
+ if (r < 0)
+ r += 0x100000000;
+ return r;
+}
+
+function s1_512_hi(xh, xl) {
+ var c0_hi = rotr64_hi(xh, xl, 14);
+ var c1_hi = rotr64_hi(xh, xl, 18);
+ var c2_hi = rotr64_hi(xl, xh, 9); // 41
+
+ var r = c0_hi ^ c1_hi ^ c2_hi;
+ if (r < 0)
+ r += 0x100000000;
+ return r;
}
-function maj64(x, y, z) {
- return new Num64(
- (x.hi & y.hi) ^ (x.hi & z.hi) ^ (y.hi & z.hi),
- (x.lo & y.lo) ^ (x.lo & z.lo) ^ (y.lo & z.lo)
- );
+function s1_512_lo(xh, xl) {
+ var c0_lo = rotr64_lo(xh, xl, 14);
+ var c1_lo = rotr64_lo(xh, xl, 18);
+ var c2_lo = rotr64_lo(xl, xh, 9); // 41
+
+ var r = c0_lo ^ c1_lo ^ c2_lo;
+ if (r < 0)
+ r += 0x100000000;
+ return r;
}
-function s0_512(x, y, z) {
- var r = x.rotr(34);
- return x.rotr(28).xor2(r, r.rotr(5));
+function g0_512_hi(xh, xl) {
+ var c0_hi = rotr64_hi(xh, xl, 1);
+ var c1_hi = rotr64_hi(xh, xl, 8);
+ var c2_hi = shr64_hi(xh, xl, 7);
+
+ var r = c0_hi ^ c1_hi ^ c2_hi;
+ if (r < 0)
+ r += 0x100000000;
+ return r;
}
-function s1_512(x, y, z) {
- return x.rotr(14).xor2(x.rotr(18), x.rotr(41));
+function g0_512_lo(xh, xl) {
+ var c0_lo = rotr64_lo(xh, xl, 1);
+ var c1_lo = rotr64_lo(xh, xl, 8);
+ var c2_lo = shr64_lo(xh, xl, 7);
+
+ var r = c0_lo ^ c1_lo ^ c2_lo;
+ if (r < 0)
+ r += 0x100000000;
+ return r;
}
-function g0_512(x, y, z) {
- return x.rotr(1).xor2(x.rotr(8), x.shr(7));
+function g1_512_hi(xh, xl) {
+ var c0_hi = rotr64_hi(xh, xl, 19);
+ var c1_hi = rotr64_hi(xl, xh, 29); // 61
+ var c2_hi = shr64_hi(xh, xl, 6);
+
+ var r = c0_hi ^ c1_hi ^ c2_hi;
+ if (r < 0)
+ r += 0x100000000;
+ return r;
}
-function g1_512(x, y, z) {
- return x.rotr(19).xor2(x.rotr(61), x.shr(6));
+function g1_512_lo(xh, xl) {
+ var c0_lo = rotr64_lo(xh, xl, 19);
+ var c1_lo = rotr64_lo(xl, xh, 29); // 61
+ var c2_lo = shr64_lo(xh, xl, 6);
+
+ var r = c0_lo ^ c1_lo ^ c2_lo;
+ if (r < 0)
+ r += 0x100000000;
+ return r;
}
diff --git a/lib/hash/utils.js b/lib/hash/utils.js
index 32f2d2f..00ed5fb 100644
--- a/lib/hash/utils.js
+++ b/lib/hash/utils.js
@@ -46,9 +46,7 @@ function htonl(w) {
((w >>> 8) & 0xff00) |
((w << 8) & 0xff0000) |
((w & 0xff) << 24);
- if (res < 0)
- res += 0x100000000;
- return res;
+ return res >>> 0;
}
utils.htonl = htonl;
@@ -64,23 +62,6 @@ function toHex32(msg, endian) {
}
utils.toHex32 = toHex32;
-function toHex64(msg, endian) {
- var res = '';
- for (var i = 0; i < msg.length; i++) {
- var hi = msg[i].hi;
- var lo = msg[i].lo;
- if (endian === 'little') {
- hi = htonl(hi);
- lo = htonl(lo);
- res += zero8(lo.toString(16)) + zero8(hi.toString(16));
- } else {
- res += zero8(hi.toString(16)) + zero8(lo.toString(16));
- }
- }
- return res;
-}
-utils.toHex64 = toHex64;
-
function zero2(word) {
if (word.length === 1)
return '0' + word;
@@ -119,9 +100,7 @@ function join32(msg, start, end, endian) {
w = (msg[k] << 24) | (msg[k + 1] << 16) | (msg[k + 2] << 8) | msg[k + 3];
else
w = (msg[k + 3] << 24) | (msg[k + 2] << 16) | (msg[k + 1] << 8) | msg[k];
- if (w < 0)
- w += 0x100000000;
- res[i] = w;
+ res[i] = w >>> 0;
}
return res;
}
@@ -147,35 +126,6 @@ function split32(msg, endian) {
}
utils.split32 = split32;
-function split64(msg, endian) {
- var res = new Array(msg.length * 8);
- for (var i = 0, k = 0; i < msg.length; i++, k += 8) {
- var hi = msg[i].hi;
- var lo = msg[i].lo;
- if (endian === 'big') {
- res[k] = hi >>> 24;
- res[k + 1] = (hi >>> 16) & 0xff;
- res[k + 2] = (hi >>> 8) & 0xff;
- res[k + 3] = hi & 0xff;
- res[k + 4] = lo >>> 24;
- res[k + 5] = (lo >>> 16) & 0xff;
- res[k + 6] = (lo >>> 8) & 0xff;
- res[k + 7] = lo & 0xff;
- } else {
- res[k + 7] = hi >>> 24;
- res[k + 6] = (hi >>> 16) & 0xff;
- res[k + 5] = (hi >>> 8) & 0xff;
- res[k + 4] = hi & 0xff;
- res[k + 3] = lo >>> 24;
- res[k + 2] = (lo >>> 16) & 0xff;
- res[k + 1] = (lo >>> 8) & 0xff;
- res[k] = lo & 0xff;
- }
- }
- return res;
-}
-utils.split64 = split64;
-
function rotr32(w, b) {
return (w >>> b) | (w << (32 - b));
}
@@ -187,34 +137,22 @@ function rotl32(w, b) {
utils.rotl32 = rotl32;
function sum32(a, b) {
- var r = (a + b) | 0;
- if (r < 0)
- r += 0x100000000;
- return r;
+ return (a + b) >>> 0;
}
utils.sum32 = sum32;
function sum32_3(a, b, c) {
- var r = (a + b + c) | 0;
- if (r < 0)
- r += 0x100000000;
- return r;
+ return (a + b + c) >>> 0;
}
utils.sum32_3 = sum32_3;
function sum32_4(a, b, c, d) {
- var r = (a + b + c + d) | 0;
- if (r < 0)
- r += 0x100000000;
- return r;
+ return (a + b + c + d) >>> 0;
}
utils.sum32_4 = sum32_4;
function sum32_5(a, b, c, d, e) {
- var r = (a + b + c + d + e) | 0;
- if (r < 0)
- r += 0x100000000;
- return r;
+ return (a + b + c + d + e) >>> 0;
}
utils.sum32_5 = sum32_5;
@@ -226,105 +164,94 @@ utils.assert = assert;
utils.inherits = inherits;
-function Num64(hi, lo) {
- this.hi = hi;
- this.lo = lo;
- this.normalize();
+function sum64(buf, pos, ah, al) {
+ var bh = buf[pos];
+ var bl = buf[pos + 1];
+
+ var lo = (al + bl) >>> 0;
+ var hi = (lo < al ? 1 : 0) + ah + bh;
+ buf[pos] = hi >>> 0;
+ buf[pos + 1] = lo;
}
-utils.Num64 = Num64;
+exports.sum64 = sum64;
-Num64.prototype.inspect = function inspect() {
- return '0x' + zero8(this.hi.toString(16)) + zero8(this.lo.toString(16));
+function sum64_hi(ah, al, bh, bl) {
+ var lo = (al + bl) >>> 0;
+ var hi = (lo < al ? 1 : 0) + ah + bh;
+ return hi >>> 0;
};
+exports.sum64_hi = sum64_hi;
-Num64.prototype.clone = function clone() {
- return new Num64(this.hi, this.lo);
+function sum64_lo(ah, al, bh, bl) {
+ var lo = al + bl;
+ return lo >>> 0;
};
-
-Num64.prototype.normalize = function normalize() {
- if (this.hi < 0)
- this.hi += 0x100000000;
- if (this.lo < 0)
- this.lo += 0x100000000;
+exports.sum64_lo = sum64_lo;
+
+function sum64_4_hi(ah, al, bh, bl, ch, cl, dh, dl) {
+ var carry = 0;
+ var lo = al;
+ lo = (lo + bl) >>> 0;
+ carry += lo < al ? 1 : 0;
+ lo = (lo + cl) >>> 0;
+ carry += lo < cl ? 1 : 0;
+ lo = (lo + dl) >>> 0;
+ carry += lo < dl ? 1 : 0;
+
+ var hi = ah + bh + ch + dh + carry;
+ return hi >>> 0;
};
+exports.sum64_4_hi = sum64_4_hi;
-Num64.prototype.xor2 = function xor2(a, b) {
- return new Num64(
- a.hi ^ b.hi ^ this.hi,
- a.lo ^ b.lo ^ this.lo
- );
+function sum64_4_lo(ah, al, bh, bl, ch, cl, dh, dl) {
+ var lo = al + bl + cl + dl;
+ return lo >>> 0;
};
-
-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;
+exports.sum64_4_lo = sum64_4_lo;
+
+function sum64_5_hi(ah, al, bh, bl, ch, cl, dh, dl, eh, el) {
+ var carry = 0;
+ var lo = al;
+ lo = (lo + bl) >>> 0;
+ carry += lo < al ? 1 : 0;
+ lo = (lo + cl) >>> 0;
+ carry += lo < cl ? 1 : 0;
+ lo = (lo + dl) >>> 0;
+ carry += lo < dl ? 1 : 0;
+ lo = (lo + el) >>> 0;
+ carry += lo < el ? 1 : 0;
+
+ var hi = ah + bh + ch + dh + eh + carry;
+ return hi >>> 0;
};
+exports.sum64_5_hi = sum64_5_hi;
-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);
+function sum64_5_lo(ah, al, bh, bl, ch, cl, dh, dl, eh, el) {
+ var lo = al + bl + cl + dl + el;
- this.lo = lo | 0;
- this.hi = hi | 0;
- this.normalize();
-
- return this;
+ return lo >>> 0;
};
+exports.sum64_5_lo = sum64_5_lo;
-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(
- ((lo & mask) << rnum) | (hi >>> num),
- ((hi & mask) << rnum) | (lo >>> num)
- );
+function rotr64_hi(ah, al, num) {
+ var r = (al << (32 - num)) | (ah >>> num);
+ return r >>> 0;
};
+exports.rotr64_hi = rotr64_hi;
-Num64.prototype.shr = function shr(num) {
- if (num >= 32)
- return new Num64(0, this.hi).shr(num - 32);
-
- // num < 32
- var mask = (1 << num) - 1;
- return new Num64(
- this.hi >>> num,
- ((this.hi & mask) << (32 - num)) | (this.lo >>> num)
- );
+function rotr64_lo(ah, al, num) {
+ var r = (ah << (32 - num)) | (al >>> num);
+ return r >>> 0;
};
+exports.rotr64_lo = rotr64_lo;
-Num64.prototype.isum = function isum(num) {
- var lo = this.lo + num.lo;
- var hi = (((lo / 0x100000000) | 0) + this.hi + num.hi) | 0;
-
- this.hi = hi;
- this.lo = lo | 0;
- if (this.hi < 0)
- this.hi += 0x100000000;
- if (this.lo < 0)
- this.lo += 0x100000000;
-
- return this;
+function shr64_hi(ah, al, num) {
+ return ah >>> num;
};
+exports.shr64_hi = shr64_hi;
-Num64.prototype.sum = function sum(num) {
- return this.clone().isum(num);
+function shr64_lo(ah, al, num) {
+ var r = (ah << (32 - num)) | (al >>> num);
+ return r >>> 0;
};
+exports.shr64_lo = shr64_lo;
--
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