Skip to content

Commit

Permalink
Unroll the _encipher() loop. Saves ~80ms on my machine (at cost 12).
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam B committed Jan 20, 2017
1 parent 9350b93 commit 0b7c11a
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions src/bcrypt/impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ function _encipher(lr, off, P, S) { // This is our bottleneck: 1714/1905 ticks /
r = lr[off + 1];

l ^= P[0];

/*
for (var i=0, k=BLOWFISH_NUM_ROUNDS-2; i<=k;)
// Feistel substitution on left word
n = S[l >>> 24],
Expand All @@ -288,6 +290,98 @@ function _encipher(lr, off, P, S) { // This is our bottleneck: 1714/1905 ticks /
n ^= S[0x200 | ((r >> 8) & 0xff)],
n += S[0x300 | (r & 0xff)],
l ^= n ^ P[++i];
*/

//The following is an unrolled version of the above loop.
//Iteration 0
n = S[l >>> 24];
n += S[0x100 | ((l >> 16) & 0xff)];
n ^= S[0x200 | ((l >> 8) & 0xff)];
n += S[0x300 | (l & 0xff)];
r ^= n ^ P[1];
n = S[r >>> 24];
n += S[0x100 | ((r >> 16) & 0xff)];
n ^= S[0x200 | ((r >> 8) & 0xff)];
n += S[0x300 | (r & 0xff)];
l ^= n ^ P[2];
//Iteration 1
n = S[l >>> 24];
n += S[0x100 | ((l >> 16) & 0xff)];
n ^= S[0x200 | ((l >> 8) & 0xff)];
n += S[0x300 | (l & 0xff)];
r ^= n ^ P[3];
n = S[r >>> 24];
n += S[0x100 | ((r >> 16) & 0xff)];
n ^= S[0x200 | ((r >> 8) & 0xff)];
n += S[0x300 | (r & 0xff)];
l ^= n ^ P[4];
//Iteration 2
n = S[l >>> 24];
n += S[0x100 | ((l >> 16) & 0xff)];
n ^= S[0x200 | ((l >> 8) & 0xff)];
n += S[0x300 | (l & 0xff)];
r ^= n ^ P[5];
n = S[r >>> 24];
n += S[0x100 | ((r >> 16) & 0xff)];
n ^= S[0x200 | ((r >> 8) & 0xff)];
n += S[0x300 | (r & 0xff)];
l ^= n ^ P[6];
//Iteration 3
n = S[l >>> 24];
n += S[0x100 | ((l >> 16) & 0xff)];
n ^= S[0x200 | ((l >> 8) & 0xff)];
n += S[0x300 | (l & 0xff)];
r ^= n ^ P[7];
n = S[r >>> 24];
n += S[0x100 | ((r >> 16) & 0xff)];
n ^= S[0x200 | ((r >> 8) & 0xff)];
n += S[0x300 | (r & 0xff)];
l ^= n ^ P[8];
//Iteration 4
n = S[l >>> 24];
n += S[0x100 | ((l >> 16) & 0xff)];
n ^= S[0x200 | ((l >> 8) & 0xff)];
n += S[0x300 | (l & 0xff)];
r ^= n ^ P[9];
n = S[r >>> 24];
n += S[0x100 | ((r >> 16) & 0xff)];
n ^= S[0x200 | ((r >> 8) & 0xff)];
n += S[0x300 | (r & 0xff)];
l ^= n ^ P[10];
//Iteration 5
n = S[l >>> 24];
n += S[0x100 | ((l >> 16) & 0xff)];
n ^= S[0x200 | ((l >> 8) & 0xff)];
n += S[0x300 | (l & 0xff)];
r ^= n ^ P[11];
n = S[r >>> 24];
n += S[0x100 | ((r >> 16) & 0xff)];
n ^= S[0x200 | ((r >> 8) & 0xff)];
n += S[0x300 | (r & 0xff)];
l ^= n ^ P[12];
//Iteration 6
n = S[l >>> 24];
n += S[0x100 | ((l >> 16) & 0xff)];
n ^= S[0x200 | ((l >> 8) & 0xff)];
n += S[0x300 | (l & 0xff)];
r ^= n ^ P[13];
n = S[r >>> 24];
n += S[0x100 | ((r >> 16) & 0xff)];
n ^= S[0x200 | ((r >> 8) & 0xff)];
n += S[0x300 | (r & 0xff)];
l ^= n ^ P[14];
//Iteration 7
n = S[l >>> 24];
n += S[0x100 | ((l >> 16) & 0xff)];
n ^= S[0x200 | ((l >> 8) & 0xff)];
n += S[0x300 | (l & 0xff)];
r ^= n ^ P[15];
n = S[r >>> 24];
n += S[0x100 | ((r >> 16) & 0xff)];
n ^= S[0x200 | ((r >> 8) & 0xff)];
n += S[0x300 | (r & 0xff)];
l ^= n ^ P[16];

lr[off] = r ^ P[BLOWFISH_NUM_ROUNDS + 1];
lr[off + 1] = l;
return lr;
Expand Down

0 comments on commit 0b7c11a

Please sign in to comment.