Skip to content

Commit f985426

Browse files
committed
sha512: same branch extraction as #18
1 parent e5486fd commit f985426

File tree

1 file changed

+45
-36
lines changed

1 file changed

+45
-36
lines changed

sha512.js

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -110,43 +110,37 @@ Sha512.prototype._update = function(M) {
110110
var gl = this._gl | 0
111111
var hl = this._hl | 0
112112

113-
for (var i = 0; i < 80; i++) {
114-
var j = i * 2
115-
116-
var Wi, Wil
117-
118-
if (i < 16) {
119-
Wi = W[j] = M.readInt32BE(j * 4)
120-
Wil = W[j + 1] = M.readInt32BE(j * 4 + 4)
121-
122-
} else {
123-
var x = W[j - 15*2]
124-
var xl = W[j - 15*2 + 1]
125-
var gamma0 = S(x, xl, 1) ^ S(x, xl, 8) ^ (x >>> 7)
126-
var gamma0l = S(xl, x, 1) ^ S(xl, x, 8) ^ S(xl, x, 7)
127-
128-
x = W[j - 2*2]
129-
xl = W[j - 2*2 + 1]
130-
var gamma1 = S(x, xl, 19) ^ S(xl, x, 29) ^ (x >>> 6)
131-
var gamma1l = S(xl, x, 19) ^ S(x, xl, 29) ^ S(xl, x, 6)
132-
133-
// W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16]
134-
var Wi7 = W[j - 7*2]
135-
var Wi7l = W[j - 7*2 + 1]
136-
137-
var Wi16 = W[j - 16*2]
138-
var Wi16l = W[j - 16*2 + 1]
139-
140-
Wil = gamma0l + Wi7l
141-
Wi = gamma0 + Wi7 + ((Wil >>> 0) < (gamma0l >>> 0) ? 1 : 0)
142-
Wil = Wil + gamma1l
143-
Wi = Wi + gamma1 + ((Wil >>> 0) < (gamma1l >>> 0) ? 1 : 0)
144-
Wil = Wil + Wi16l
145-
Wi = Wi + Wi16 + ((Wil >>> 0) < (Wi16l >>> 0) ? 1 : 0)
113+
var i = 0, j = 0
114+
var Wi, Wil
115+
function calcW() {
116+
var x = W[j - 15*2]
117+
var xl = W[j - 15*2 + 1]
118+
var gamma0 = S(x, xl, 1) ^ S(x, xl, 8) ^ (x >>> 7)
119+
var gamma0l = S(xl, x, 1) ^ S(xl, x, 8) ^ S(xl, x, 7)
120+
121+
x = W[j - 2*2]
122+
xl = W[j - 2*2 + 1]
123+
var gamma1 = S(x, xl, 19) ^ S(xl, x, 29) ^ (x >>> 6)
124+
var gamma1l = S(xl, x, 19) ^ S(x, xl, 29) ^ S(xl, x, 6)
125+
126+
// W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16]
127+
var Wi7 = W[j - 7*2]
128+
var Wi7l = W[j - 7*2 + 1]
129+
130+
var Wi16 = W[j - 16*2]
131+
var Wi16l = W[j - 16*2 + 1]
132+
133+
Wil = gamma0l + Wi7l
134+
Wi = gamma0 + Wi7 + ((Wil >>> 0) < (gamma0l >>> 0) ? 1 : 0)
135+
Wil = Wil + gamma1l
136+
Wi = Wi + gamma1 + ((Wil >>> 0) < (gamma1l >>> 0) ? 1 : 0)
137+
Wil = Wil + Wi16l
138+
Wi = Wi + Wi16 + ((Wil >>> 0) < (Wi16l >>> 0) ? 1 : 0)
139+
}
146140

147-
W[j] = Wi
148-
W[j + 1] = Wil
149-
}
141+
function loop() {
142+
W[j] = Wi
143+
W[j + 1] = Wil
150144

151145
var maj = Maj(a, b, c)
152146
var majl = Maj(al, bl, cl)
@@ -192,6 +186,21 @@ Sha512.prototype._update = function(M) {
192186
bl = al
193187
al = (t1l + t2l) | 0
194188
a = (t1 + t2 + ((al >>> 0) < (t1l >>> 0) ? 1 : 0)) | 0
189+
190+
i++
191+
j += 2
192+
}
193+
194+
while (i < 16) {
195+
Wi = M.readInt32BE(j * 4)
196+
Wil = M.readInt32BE(j * 4 + 4)
197+
198+
loop()
199+
}
200+
201+
while (i < 80) {
202+
calcW()
203+
loop()
195204
}
196205

197206
this._al = (this._al + al) | 0

0 commit comments

Comments
 (0)