Skip to content

Commit e6e5b1e

Browse files
committed
inline Sigma functions
1 parent 52659f7 commit e6e5b1e

File tree

2 files changed

+47
-35
lines changed

2 files changed

+47
-35
lines changed

sha256.js

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,36 +53,28 @@ Sha256.prototype.init = function () {
5353
return this
5454
}
5555

56-
function S (X, n) {
57-
return (X >>> n) | (X << (32 - n))
58-
}
59-
60-
function R (X, n) {
61-
return (X >>> n)
62-
}
63-
6456
function Ch (x, y, z) {
65-
return ((x & y) ^ ((~x) & z))
57+
return z ^ (x & (y ^ z))
6658
}
6759

6860
function Maj (x, y, z) {
69-
return ((x & y) ^ (x & z) ^ (y & z))
61+
return (x & y) | (z & (x | y))
7062
}
7163

72-
function Sigma0256 (x) {
73-
return (S(x, 2) ^ S(x, 13) ^ S(x, 22))
64+
function Sigma0 (x) {
65+
return (x >>> 2 | x << 30) ^ (x >>> 13 | x << 19) ^ (x >>> 22 | x << 10)
7466
}
7567

76-
function Sigma1256 (x) {
77-
return (S(x, 6) ^ S(x, 11) ^ S(x, 25))
68+
function Sigma1 (x) {
69+
return (x >>> 6 | x << 26) ^ (x >>> 11 | x << 21) ^ (x >>> 25 | x << 7)
7870
}
7971

80-
function Gamma0256 (x) {
81-
return (S(x, 7) ^ S(x, 18) ^ R(x, 3))
72+
function Gamma0 (x) {
73+
return (x >>> 7 | x << 25) ^ (x >>> 18 | x << 14) ^ (x >>> 3)
8274
}
8375

84-
function Gamma1256 (x) {
85-
return (S(x, 17) ^ S(x, 19) ^ R(x, 10))
76+
function Gamma1 (x) {
77+
return (x >>> 17 | x << 15) ^ (x >>> 19 | x << 13) ^ (x >>> 10)
8678
}
8779

8880
Sha256.prototype._update = function (M) {
@@ -99,12 +91,12 @@ Sha256.prototype._update = function (M) {
9991

10092
var j = 0
10193

102-
function calcW () { return Gamma1256(W[j - 2]) + W[j - 7] + Gamma0256(W[j - 15]) + W[j - 16] }
94+
function calcW () { return Gamma1(W[j - 2]) + W[j - 7] + Gamma0(W[j - 15]) + W[j - 16] }
10395
function loop (w) {
10496
W[j] = w
10597

106-
var T1 = h + Sigma1256(e) + Ch(e, f, g) + K[j] + w
107-
var T2 = Sigma0256(a) + Maj(a, b, c)
98+
var T1 = h + Sigma1(e) + Ch(e, f, g) + K[j] + w
99+
var T2 = Sigma0(a) + Maj(a, b, c)
108100

109101
h = g
110102
g = f

sha512.js

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,36 @@ Sha512.prototype.init = function () {
7777
return this
7878
}
7979

80-
function S (X, Xl, n) {
81-
return (X >>> n) | (Xl << (32 - n))
82-
}
83-
8480
function Ch (x, y, z) {
85-
return ((x & y) ^ ((~x) & z))
81+
return z ^ (x & (y ^ z))
8682
}
8783

8884
function Maj (x, y, z) {
89-
return ((x & y) ^ (x & z) ^ (y & z))
85+
return (x & y) | (z & (x | y))
86+
}
87+
88+
function Sigma0 (x, xl) {
89+
return (x >>> 28 | xl << 4) ^ (xl >>> 2 | x << 30) ^ (xl >>> 7 | x << 25)
90+
}
91+
92+
function Sigma1 (x, xl) {
93+
return (x >>> 14 | xl << 18) ^ (x >>> 18 | xl << 14) ^ (xl >>> 9 | x << 23)
94+
}
95+
96+
function Gamma0 (x, xl) {
97+
return (x >>> 1 | xl << 31) ^ (x >>> 8 | xl << 24) ^ (x >>> 7)
98+
}
99+
100+
function Gamma0l (x, xl) {
101+
return (x >>> 1 | xl << 31) ^ (x >>> 8 | xl << 24) ^ (x >>> 7 | xl << 25)
102+
}
103+
104+
function Gamma1 (x, xl) {
105+
return (x >>> 19 | xl << 13) ^ (xl >>> 29 | x << 3) ^ (x >>> 6)
106+
}
107+
108+
function Gamma1l (x, xl) {
109+
return (x >>> 19 | xl << 13) ^ (xl >>> 29 | x << 3) ^ (x >>> 6 | xl << 26)
90110
}
91111

92112
Sha512.prototype._update = function (M) {
@@ -116,13 +136,13 @@ Sha512.prototype._update = function (M) {
116136
function calcW () {
117137
var x = W[j - 15 * 2]
118138
var xl = W[j - 15 * 2 + 1]
119-
var gamma0 = S(x, xl, 1) ^ S(x, xl, 8) ^ (x >>> 7)
120-
var gamma0l = S(xl, x, 1) ^ S(xl, x, 8) ^ S(xl, x, 7)
139+
var gamma0 = Gamma0(x, xl)
140+
var gamma0l = Gamma0l(xl, x)
121141

122142
x = W[j - 2 * 2]
123143
xl = W[j - 2 * 2 + 1]
124-
var gamma1 = S(x, xl, 19) ^ S(xl, x, 29) ^ (x >>> 6)
125-
var gamma1l = S(xl, x, 19) ^ S(x, xl, 29) ^ S(xl, x, 6)
144+
var gamma1 = Gamma1(x, xl)
145+
var gamma1l = Gamma1l(xl, x)
126146

127147
// W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16]
128148
var Wi7 = W[j - 7 * 2]
@@ -146,10 +166,10 @@ Sha512.prototype._update = function (M) {
146166
var maj = Maj(a, b, c)
147167
var majl = Maj(al, bl, cl)
148168

149-
var sigma0h = S(a, al, 28) ^ S(al, a, 2) ^ S(al, a, 7)
150-
var sigma0l = S(al, a, 28) ^ S(a, al, 2) ^ S(a, al, 7)
151-
var sigma1h = S(e, el, 14) ^ S(e, el, 18) ^ S(el, e, 9)
152-
var sigma1l = S(el, e, 14) ^ S(el, e, 18) ^ S(e, el, 9)
169+
var sigma0h = Sigma0(a, al)
170+
var sigma0l = Sigma0(al, a)
171+
var sigma1h = Sigma1(e, el)
172+
var sigma1l = Sigma1(el, e)
153173

154174
// t1 = h + sigma1 + ch + K[i] + W[i]
155175
var Ki = K[j]

0 commit comments

Comments
 (0)