[Pkg-javascript-commits] [node-ripemd160] 50/71: add little optimizations
Bastien Roucariès
rouca at moszumanska.debian.org
Thu May 4 10:25:41 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-ripemd160.
commit 88ea8101a98ef5e7211ad89cad89e115de22b251
Author: Kirill Fomichev <fanatid at ya.ru>
Date: Mon Feb 29 12:05:12 2016 +0300
add little optimizations
---
lib/ripemd160.js | 153 ++++++++++++++++++++-----------------------------------
1 file changed, 55 insertions(+), 98 deletions(-)
diff --git a/lib/ripemd160.js b/lib/ripemd160.js
index a96ac5b..0646d6b 100644
--- a/lib/ripemd160.js
+++ b/lib/ripemd160.js
@@ -48,37 +48,35 @@ var sr = [
8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
]
-var hl = [0x00000000, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E]
-var hr = [0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0x00000000]
+var hl = [0x00000000, 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xa953fd4e]
+var hr = [0x50a28be6, 0x5c4dd124, 0x6d703ef3, 0x7a6d76e9, 0x00000000]
-function bytesToWords (bytes) {
- var words = []
- for (var i = 0, b = 0; i < bytes.length; i++, b += 8) {
- words[b >>> 5] |= bytes[i] << (24 - b % 32)
- }
- return words
+function f1 (x, y, z) {
+ return x ^ y ^ z
}
-function wordsToBytes (words) {
- var bytes = []
- for (var b = 0; b < words.length * 32; b += 8) {
- bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF)
- }
- return bytes
+function f2 (x, y, z) {
+ return (x & y) | ((~x) & z)
}
-function processBlock (H, M, offset) {
- // swap endian
- for (var i = 0; i < 16; i++) {
- var offset_i = offset + i
- var M_offset_i = M[offset_i]
-
- // Swap
- M[offset_i] = (
- (((M_offset_i << 8) | (M_offset_i >>> 24)) & 0x00ff00ff) |
- (((M_offset_i << 24) | (M_offset_i >>> 8)) & 0xff00ff00)
- )
- }
+function f3 (x, y, z) {
+ return (x | (~y)) ^ z
+}
+
+function f4 (x, y, z) {
+ return (x & z) | (y & (~z))
+}
+
+function f5 (x, y, z) {
+ return x ^ (y | (~z))
+}
+
+function rotl (x, n) {
+ return (x << n) | (x >>> (32 - n))
+}
+
+function processBlock (H, words, offset) {
+ for (var j = 0; j < 16; ++j) words[offset + j] = words[offset + j] | 0
// Working variables
var al, bl, cl, dl, el
@@ -92,44 +90,37 @@ function processBlock (H, M, offset) {
// computation
var t
- for (i = 0; i < 80; i += 1) {
- t = (al + M[offset + zl[i]]) | 0
+ for (var i = 0; i < 80; i += 1) {
if (i < 16) {
- t += f1(bl, cl, dl) + hl[0]
+ t = (al + words[offset + zl[i]] + f1(bl, cl, dl) + hl[0]) | 0
} else if (i < 32) {
- t += f2(bl, cl, dl) + hl[1]
+ t = (al + words[offset + zl[i]] + f2(bl, cl, dl) + hl[1]) | 0
} else if (i < 48) {
- t += f3(bl, cl, dl) + hl[2]
+ t = (al + words[offset + zl[i]] + f3(bl, cl, dl) + hl[2]) | 0
} else if (i < 64) {
- t += f4(bl, cl, dl) + hl[3]
+ t = (al + words[offset + zl[i]] + f4(bl, cl, dl) + hl[3]) | 0
} else {// if (i<80) {
- t += f5(bl, cl, dl) + hl[4]
+ t = (al + words[offset + zl[i]] + f5(bl, cl, dl) + hl[4]) | 0
}
- t = t | 0
- t = rotl(t, sl[i])
- t = (t + el) | 0
+ t = (rotl(t, sl[i]) + el) | 0
al = el
el = dl
dl = rotl(cl, 10)
cl = bl
bl = t
- t = (ar + M[offset + zr[i]]) | 0
if (i < 16) {
- t += f5(br, cr, dr) + hr[0]
+ t = (ar + words[offset + zr[i]] + f5(br, cr, dr) + hr[0]) | 0
} else if (i < 32) {
- t += f4(br, cr, dr) + hr[1]
+ t = (ar + words[offset + zr[i]] + f4(br, cr, dr) + hr[1]) | 0
} else if (i < 48) {
- t += f3(br, cr, dr) + hr[2]
+ t = (ar + words[offset + zr[i]] + f3(br, cr, dr) + hr[2]) | 0
} else if (i < 64) {
- t += f2(br, cr, dr) + hr[3]
+ t = (ar + words[offset + zr[i]] + f2(br, cr, dr) + hr[3]) | 0
} else {// if (i<80) {
- t += f1(br, cr, dr) + hr[4]
+ t = (ar + words[offset + zr[i]] + f1(br, cr, dr) + hr[4]) | 0
}
-
- t = t | 0
- t = rotl(t, sr[i])
- t = (t + er) | 0
+ t = (rotl(t, sr[i]) + er) | 0
ar = er
er = dr
dr = rotl(cr, 10)
@@ -146,65 +137,31 @@ function processBlock (H, M, offset) {
H[0] = t
}
-function f1 (x, y, z) {
- return ((x) ^ (y) ^ (z))
-}
-
-function f2 (x, y, z) {
- return (((x) & (y)) | ((~x) & (z)))
-}
-
-function f3 (x, y, z) {
- return (((x) | (~(y))) ^ (z))
-}
-
-function f4 (x, y, z) {
- return (((x) & (z)) | ((y) & (~(z))))
-}
-
-function f5 (x, y, z) {
- return ((x) ^ ((y) | (~(z))))
-}
-
-function rotl (x, n) {
- return (x << n) | (x >>> (32 - n))
-}
-
function ripemd160 (message) {
- var H = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0]
-
if (typeof message === 'string') {
message = new Buffer(message, 'utf8')
}
- var m = bytesToWords(message)
-
- var nBitsLeft = message.length * 8
- var nBitsTotal = message.length * 8
-
- // Add padding
- m[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32)
- m[(((nBitsLeft + 64) >>> 9) << 4) + 14] = (
- (((nBitsTotal << 8) | (nBitsTotal >>> 24)) & 0x00ff00ff) |
- (((nBitsTotal << 24) | (nBitsTotal >>> 8)) & 0xff00ff00)
- )
-
- for (var i = 0; i < m.length; i += 16) {
- processBlock(H, m, i)
- }
-
- // swap endian
- for (i = 0; i < 5; i++) {
- // shortcut
- var H_i = H[i]
-
- // Swap
- H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff) |
- (((H_i << 24) | (H_i >>> 8)) & 0xff00ff00)
+ var words = []
+ for (var i = 0, b = 0; i < message.length; i++, b += 8) {
+ words[b >>> 5] |= message[i] << b % 32
}
- var digestbytes = wordsToBytes(H)
- return new Buffer(digestbytes)
+ // Add padding
+ var nBits = message.length * 8
+ words[nBits >>> 5] |= 0x80 << nBits % 32
+ words[(((nBits + 64) >>> 9) << 4) + 14] = nBits
+
+ var H = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0]
+ for (var j = 0; j < words.length; j += 16) processBlock(H, words, j)
+
+ var digest = new Buffer(20)
+ digest.writeUInt32LE(H[0] >>> 0, 0)
+ digest.writeUInt32LE(H[1] >>> 0, 4)
+ digest.writeUInt32LE(H[2] >>> 0, 8)
+ digest.writeUInt32LE(H[3] >>> 0, 12)
+ digest.writeUInt32LE(H[4] >>> 0, 16)
+ return digest
}
module.exports = ripemd160
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-ripemd160.git
More information about the Pkg-javascript-commits
mailing list