b9e53a2e by brandonocasey

precompute every time is faster use that

1 parent fd7b5164
...@@ -49,59 +49,6 @@ const ntoh = function(word) { ...@@ -49,59 +49,6 @@ const ntoh = function(word) {
49 (word >>> 24); 49 (word >>> 24);
50 }; 50 };
51 51
52 /**
53 * Expand the S-box tables.
54 *
55 * @private
56 */
57 const precompute = function() {
58 let _tables = [[[], [], [], [], []], [[], [], [], [], []]];
59 let encTable = _tables[0];
60 let decTable = _tables[1];
61 let sbox = encTable[4];
62 let sboxInv = decTable[4];
63 let i;
64 let x;
65 let xInv;
66 let d = [];
67 let th = [];
68 let x2;
69 let x4;
70 let x8;
71 let s;
72 let tEnc;
73 let tDec;
74
75 // Compute double and third tables
76 for (i = 0; i < 256; i++) {
77 th[(d[i] = i << 1 ^ (i >> 7) * 283) ^ i] = i;
78 }
79
80 for (x = xInv = 0; !sbox[x]; x ^= x2 || 1, xInv = th[xInv] || 1) {
81 // Compute sbox
82 s = xInv ^ xInv << 1 ^ xInv << 2 ^ xInv << 3 ^ xInv << 4;
83 s = s >> 8 ^ s & 255 ^ 99;
84 sbox[x] = s;
85 sboxInv[s] = x;
86
87 // Compute MixColumns
88 x8 = d[x4 = d[x2 = d[x]]];
89 tDec = x8 * 0x1010101 ^ x4 * 0x10001 ^ x2 * 0x101 ^ x * 0x1010100;
90 tEnc = d[s] * 0x101 ^ s * 0x1010100;
91
92 for (i = 0; i < 4; i++) {
93 encTable[i][x] = tEnc = tEnc << 24 ^ tEnc >>> 8;
94 decTable[i][s] = tDec = tDec << 24 ^ tDec >>> 8;
95 }
96 }
97
98 // Compactify. Considerable speedup on Firefox.
99 for (i = 0; i < 5; i++) {
100 encTable[i] = encTable[i].slice(0);
101 decTable[i] = decTable[i].slice(0);
102 }
103 return _tables;
104 };
105 52
106 let aesTables; 53 let aesTables;
107 54
...@@ -126,10 +73,7 @@ class AES { ...@@ -126,10 +73,7 @@ class AES {
126 * 73 *
127 * @private 74 * @private
128 */ 75 */
129 if (!aesTables) { 76 this._tables = this._precompute();
130 aesTables = precompute();
131 }
132 this._tables = JSON.parse(JSON.stringify(aesTables));
133 let i; 77 let i;
134 let j; 78 let j;
135 let tmp; 79 let tmp;
...@@ -183,6 +127,61 @@ class AES { ...@@ -183,6 +127,61 @@ class AES {
183 } 127 }
184 } 128 }
185 129
130
131 /**
132 * Expand the S-box tables.
133 *
134 * @private
135 */
136 _precompute() {
137 let _tables = [[[], [], [], [], []], [[], [], [], [], []]];
138 let encTable = _tables[0];
139 let decTable = _tables[1];
140 let sbox = encTable[4];
141 let sboxInv = decTable[4];
142 let i;
143 let x;
144 let xInv;
145 let d = [];
146 let th = [];
147 let x2;
148 let x4;
149 let x8;
150 let s;
151 let tEnc;
152 let tDec;
153
154 // Compute double and third tables
155 for (i = 0; i < 256; i++) {
156 th[(d[i] = i << 1 ^ (i >> 7) * 283) ^ i] = i;
157 }
158
159 for (x = xInv = 0; !sbox[x]; x ^= x2 || 1, xInv = th[xInv] || 1) {
160 // Compute sbox
161 s = xInv ^ xInv << 1 ^ xInv << 2 ^ xInv << 3 ^ xInv << 4;
162 s = s >> 8 ^ s & 255 ^ 99;
163 sbox[x] = s;
164 sboxInv[s] = x;
165
166 // Compute MixColumns
167 x8 = d[x4 = d[x2 = d[x]]];
168 tDec = x8 * 0x1010101 ^ x4 * 0x10001 ^ x2 * 0x101 ^ x * 0x1010100;
169 tEnc = d[s] * 0x101 ^ s * 0x1010100;
170
171 for (i = 0; i < 4; i++) {
172 encTable[i][x] = tEnc = tEnc << 24 ^ tEnc >>> 8;
173 decTable[i][s] = tDec = tDec << 24 ^ tDec >>> 8;
174 }
175 }
176
177 // Compactify. Considerable speedup on Firefox.
178 for (i = 0; i < 5; i++) {
179 encTable[i] = encTable[i].slice(0);
180 decTable[i] = decTable[i].slice(0);
181 }
182 return _tables;
183 }
184
186 /** 185 /**
187 * Decrypt 16 bytes, specified as four 32-bit words. 186 * Decrypt 16 bytes, specified as four 32-bit words.
188 * @param encrypted0 {number} the first word to decrypt 187 * @param encrypted0 {number} the first word to decrypt
......