Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/bench/tests/sha256.lua
2725 views
1
local function prequire(name) local success, result = pcall(require, name); return success and result end
2
local bench = script and require(script.Parent.bench_support) or prequire("bench_support") or require("../bench_support")
3
4
function test()
5
6
local band = bit32.band
7
local bnot = bit32.bnot
8
local bxor = bit32.bxor
9
local bor = bit32.bor
10
11
local rrotate = bit32.rrotate
12
local rshift = bit32.rshift
13
14
local primes =
15
{
16
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
17
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
18
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
19
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
20
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
21
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
22
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
23
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
24
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
25
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
26
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
27
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
28
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
29
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
30
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
31
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
32
}
33
34
local function toHex(str)
35
local result = str:gsub('.', function (char)
36
return string.format("%02x", char:byte())
37
end)
38
39
return result
40
end
41
42
local function toBytes(value, length)
43
local str = ""
44
45
for i = 1, length do
46
local rem = value % 256
47
str = string.char(rem) .. str
48
value = (value - rem) / 256
49
end
50
51
return str
52
end
53
54
local function digestBlock(msg, i, hash, digest)
55
for j = 1, 16 do
56
local offset = i + (j - 1) * 4
57
local a, b, c, d = string.byte(msg, offset, offset + 3)
58
digest[j] = ((a * 256 + b) * 256 + c) * 256 + d
59
end
60
61
for j = 17, 64 do
62
local v = digest[j - 15]
63
local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
64
65
v = digest[j - 2]
66
digest[j] = digest[j - 16] + s0 + digest[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
67
end
68
69
local a, b, c, d, e, f, g, h = table.unpack(hash)
70
71
for i = 1, 64 do
72
local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
73
local maj = bxor(band(a, b), band(a, c), band(b, c))
74
75
local t2 = s0 + maj
76
local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
77
78
local ch = bxor(band(e, f), band(bnot(e), g))
79
local t1 = h + s1 + ch + primes[i] + digest[i]
80
81
h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
82
end
83
84
hash[1] = bor(hash[1] + a, 0)
85
hash[2] = bor(hash[2] + b, 0)
86
hash[3] = bor(hash[3] + c, 0)
87
hash[4] = bor(hash[4] + d, 0)
88
hash[5] = bor(hash[5] + e, 0)
89
hash[6] = bor(hash[6] + f, 0)
90
hash[7] = bor(hash[7] + g, 0)
91
hash[8] = bor(hash[8] + h, 0)
92
end
93
94
local function sha256(msg)
95
do
96
local extra = 64 - ((#msg + 9) % 64)
97
local len = toBytes(8 * #msg, 8)
98
99
msg = msg .. '\128' .. string.rep('\0', extra) .. len
100
assert(#msg % 64 == 0)
101
end
102
103
local hash =
104
{
105
0x6a09e667,
106
0xbb67ae85,
107
0x3c6ef372,
108
0xa54ff53a,
109
0x510e527f,
110
0x9b05688c,
111
0x1f83d9ab,
112
0x5be0cd19,
113
}
114
115
local digest = {}
116
117
for i = 1, #msg, 64 do
118
digestBlock(msg, i, hash, digest)
119
end
120
121
local result = ""
122
123
for i = 1, 8 do
124
local value = hash[i]
125
result = result .. toBytes(value, 4)
126
end
127
128
return toHex(result)
129
end
130
131
local input = string.rep(".", 1e3)
132
133
local ts0 = os.clock()
134
135
for i = 1, 100 do
136
local res = sha256(input)
137
assert(res == "45849646c50337988ccc877d23fcc0de50d1df7490fdc3b9333aed0de8ab492a")
138
end
139
140
local ts1 = os.clock()
141
142
return ts1 - ts0
143
end
144
145
bench.runCode(test, "sha256")
146
147