Skip to content

Commit 7d68d2e

Browse files
committed
Reduce the size of scripts so that they take at most 6 clusters (3072kb)
1 parent 49450bb commit 7d68d2e

File tree

5 files changed

+176
-507
lines changed

5 files changed

+176
-507
lines changed

demo/extra/exbench.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ if not LIB then print("Please run bench.lua from the same working directory inst
22

33
local function benchmark_md5sum()
44
local it, s = 2, pcall(require, "md5sum")
5-
local bm = BenchmarkStart("Checksum MD5", it)
5+
local bm = BS("Checksum MD5", it)
66
local file = io.open(arg[0], "rb")
77

88
if not s or not file then
@@ -16,12 +16,12 @@ local function benchmark_md5sum()
1616
file:seek("set", 0)
1717
end
1818
file:close()
19-
BenchmarkEnd(bm)
19+
BE(bm)
2020
end
2121

2222
local function benchmark_sha256()
2323
local it, s = 1, pcall(require, "s256sum")
24-
local bm = BenchmarkStart("Checksum SHA256", it)
24+
local bm = BS("Checksum SHA256", it)
2525
local file = io.open(arg[0], "rb")
2626

2727
if not s or not file then
@@ -35,7 +35,7 @@ local function benchmark_sha256()
3535
file:seek("set", 0)
3636
end
3737
file:close()
38-
BenchmarkEnd(bm)
38+
BE(bm)
3939
end
4040

4141
benchmark_md5sum()

demo/extra/md5sum.lua

Lines changed: 51 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,102 @@
11
#!/usr/bin/env lua
22

3-
--- Read a files hash from it's current point to the end
4-
--- @param file file The file to read to the end
5-
--- @return string The md5 checksum result of the file
3+
--- Read file hash
4+
--- @param file file The file to read (to end)
5+
--- @return string MD5 checksum
66
function md5_file(file)
7-
local function Md5Transform(chunk, A, B, C, D) -- Core MD5 transformation
8-
local F = function(x, y, z)
9-
return (x & y) | (~x & z)
7+
local function Md5Transform(chunk, A, B, C, D)
8+
local F = function(x, y, z) return (x & y) | (~x & z) end
9+
local G = function(x, y, z) return (x & z) | (y & ~z) end
10+
local H = function(x, y, z) return x ~ y ~ z end
11+
local I = function(x, y, z) return y ~ (x | ~z) end
12+
local function LS(x, n) return ((x << n) | (x >> (32 - n))) & 0xFFFFFFFF end
13+
local wo, sh = {}, {{ 7, 12, 17, 22 }, { 5, 9, 14, 20 }, { 4, 11, 16, 23 }, { 6, 10, 15, 21 }}
14+
for i = 0, 15 do
15+
local o = i * 4 + 1
16+
wo[i + 1] =
17+
string.byte(chunk, o) |
18+
(string.byte(chunk, o + 1) << 8) |
19+
(string.byte(chunk, o + 2) << 16) |
20+
(string.byte(chunk, o + 3) << 24)
1021
end
11-
12-
local G = function(x, y, z)
13-
return (x & z) | (y & ~z)
14-
end
15-
16-
local H = function(x, y, z)
17-
return x ~ y ~ z
18-
end
19-
20-
local I = function(x, y, z)
21-
return y ~ (x | ~z)
22-
end
23-
24-
local function LShift32(x, n)
25-
return ((x << n) | (x >> (32 - n))) & 0xFFFFFFFF
26-
end
27-
28-
local words, shifts = {}, {
29-
{ 7, 12, 17, 22 },
30-
{ 5, 9, 14, 20 },
31-
{ 4, 11, 16, 23 },
32-
{ 6, 10, 15, 21 },
33-
}
34-
35-
for i = 0, 15 do -- Break the chunk into 16 little-endian 32-bit words
36-
local offset = i * 4 + 1
37-
words[i + 1] = string.byte(chunk, offset) |
38-
(string.byte(chunk, offset + 1) << 8) |
39-
(string.byte(chunk, offset + 2) << 16) |
40-
(string.byte(chunk, offset + 3) << 24)
41-
end
42-
4322
local a, b, c, d = A, B, C, D
44-
45-
for i = 1, 64 do -- Main loop: 64 rounds of transformations
23+
for i = 1, 64 do
4624
local f, g
47-
local round = math.floor((i - 1) / 16) + 1
48-
local shift = shifts[round][(i - 1) % 4 + 1]
49-
50-
if round == 1 then
25+
local r = math.floor((i - 1) / 16) + 1
26+
local s = sh[r][(i - 1) % 4 + 1]
27+
if r == 1 then
5128
f = F(b, c, d)
5229
g = (i - 1) % 16
53-
elseif round == 2 then
30+
elseif r == 2 then
5431
f = G(b, c, d)
5532
g = (5 * (i - 1) + 1) % 16
56-
elseif round == 3 then
33+
elseif r == 3 then
5734
f = H(b, c, d)
5835
g = (3 * (i - 1) + 5) % 16
59-
elseif round == 4 then
36+
elseif r == 4 then
6037
f = I(b, c, d)
6138
g = (7 * (i - 1)) % 16
6239
end
63-
64-
local temp = d
40+
local t = d
6541
d = c
6642
c = b
67-
b = (b + LShift32((a + f + words[g + 1] +
68-
T[i]) & 0xFFFFFFFF, shift)) & 0xFFFFFFFF
69-
a = temp
43+
b = (b + LS((a + f + wo[g + 1] + T[i]) & 0xFFFFFFFF, s)) & 0xFFFFFFFF
44+
a = t
7045
end
71-
72-
-- Add chunk's hash to the result so far
7346
A = (A + a) & 0xFFFFFFFF
7447
B = (B + b) & 0xFFFFFFFF
7548
C = (C + c) & 0xFFFFFFFF
7649
D = (D + d) & 0xFFFFFFFF
77-
7850
return A, B, C, D
7951
end
80-
8152
local function Md5Pad(messageLength)
82-
local msg_len = messageLength * 8 -- Message length in bits
83-
local padding = "\128" -- Initial padding
84-
local pad_len = (56 - (messageLength % 64)) -- 448 mod 512
85-
86-
if pad_len <= 0 then
87-
pad_len = pad_len + 64
88-
end
89-
90-
padding = padding .. string.rep("\0", pad_len - 1)
91-
92-
-- Append the original message length as a 64-bit little-endian integer
93-
for i = 0, 7 do
94-
padding = padding .. string.char((msg_len >> (8 * i)) & 0xFF)
95-
end
96-
97-
return padding
53+
local ml = messageLength * 8
54+
local p = "\128"
55+
local pl = (56 - (messageLength % 64))
56+
if pl <= 0 then pl = pl + 64 end
57+
p = p .. string.rep("\0", pl - 1)
58+
for i = 0, 7 do p = p .. string.char((ml >> (8 * i)) & 0xFF) end
59+
return p
9860
end
99-
100-
local A, B, C, D, len = 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0
61+
local A, B, C, D, l = 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0
10162

10263
while true do
103-
local chunk = file:read(64) -- Read in chunks of 64 bytes (512 bits)
104-
105-
if not chunk then break end
106-
len = len + #chunk
107-
108-
-- If it's the final chunk, apply padding
109-
if #chunk < 64 then chunk = chunk .. Md5Pad(len) end
110-
A, B, C, D = Md5Transform(chunk, A, B, C, D)
111-
112-
-- Stop processing after the padded block
113-
if #chunk > 64 then break end
64+
local c = file:read(64)
65+
if not c then break end
66+
l = l + #c
67+
if #c < 64 then c = c .. Md5Pad(l) end
68+
A, B, C, D = Md5Transform(c, A, B, C, D)
69+
if #c > 64 then break end
11470
end
115-
116-
-- Format output as a hexadecimal string in little-endian order
11771
local function to_hex(x)
11872
return string.format("%02x%02x%02x%02x",
11973
x & 0xFF, (x >> 8) & 0xFF, (x >> 16) & 0xFF, (x >> 24) & 0xFF)
12074
end
121-
12275
return to_hex(A) .. to_hex(B) .. to_hex(C) .. to_hex(D)
12376
end
12477

125-
local function PredefineConstants()
126-
T = {} -- Predefined MD5 constants of sine-based shifts (T values)
127-
for i = 1, 64 do
128-
T[i] = math.floor(2 ^ 32 * math.abs(math.sin(i)))
129-
end
78+
local function init()
79+
T = {}
80+
for i = 1, 64 do T[i] = math.floor(2 ^ 32 * math.abs(math.sin(i))) end
13081
end
13182

132-
if LIB then PredefineConstants() return end
133-
83+
if LIB then init() return end
13484
if #arg < 1 then
135-
-- Show accurate Lua binary and script location
13685
print((arg[-1] or "?") .. " " .. (arg[0] or "?") .. " [FILE]...")
13786
os.exit(1)
13887
else
139-
PredefineConstants()
140-
88+
init()
14189
for i = 1, #arg do
142-
local file, err = io.open(arg[i], "rb")
143-
144-
if file then
145-
local sum = md5_file(file)
146-
file:close()
147-
90+
local f, e = io.open(arg[i], "rb")
91+
if f then
92+
local sum = md5_file(f) f:close()
14893
if sum then
14994
print(sum .. " " .. arg[i])
15095
else
151-
print(arg[i] .. ": " .. "Unknown error")
152-
os.exit(-1)
96+
print(arg[i] .. ": " .. "Unknown error") os.exit(-1)
15397
end
15498
else
155-
print(arg[i] .. ": " .. err)
156-
os.exit(1)
99+
print(arg[i] .. ": " .. e) os.exit(1)
157100
end
158101
end
159102
end

0 commit comments

Comments
 (0)