Skip to content

Commit

Permalink
Improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
howmanysmall authored Feb 24, 2022
1 parent ba26c36 commit 058634f
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 639 deletions.
51 changes: 25 additions & 26 deletions src/EllipticCurveCryptography/arith.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
-- Big integer arithmetic for 168-bit (and 336-bit) numbers
-- Numbers are represented as little-endian tables of 24-bit integers
local twoPower = require(script.Parent.twoPower)

local function isEqual(a, b)
return (
a[1] == b[1]
and a[2] == b[2]
and a[3] == b[3]
and a[4] == b[4]
and a[5] == b[5]
and a[6] == b[6]
and a[7] == b[7]
)
return a[1] == b[1]
and a[2] == b[2]
and a[3] == b[3]
and a[4] == b[4]
and a[5] == b[5]
and a[6] == b[6]
and a[7] == b[7]
end

local function compare(a, b)
Expand Down Expand Up @@ -208,8 +207,8 @@ local function addDouble(a, b)
end

local function mult(a, b, half_multiply)
local a1, a2, a3, a4, a5, a6, a7 = unpack(a)
local b1, b2, b3, b4, b5, b6, b7 = unpack(b)
local a1, a2, a3, a4, a5, a6, a7 = a[1], a[2], a[3], a[4], a[5], a[6], a[7]
local b1, b2, b3, b4, b5, b6, b7 = b[1], b[2], b[3], b[4], b[5], b[6], b[7]

local c1 = a1 * b1
local c2 = a1 * b2 + a2 * b1
Expand Down Expand Up @@ -279,7 +278,7 @@ end

local function square(a)
-- returns a 336-bit integer (14 words)
local a1, a2, a3, a4, a5, a6, a7 = unpack(a)
local a1, a2, a3, a4, a5, a6, a7 = a[1], a[2], a[3], a[4], a[5], a[6], a[7]

local c1 = a1 * a1
local c2 = a1 * a2 * 2
Expand Down Expand Up @@ -341,11 +340,11 @@ local function square(a)
end

local function encodeInt(a)
local enc = {}
local enc = table.create(21)

for i = 1, 7 do
local word = a[i]
for j = 1, 3 do
for _ = 1, 3 do
table.insert(enc, word % 256)
word = math.floor(word / 256)
end
Expand All @@ -356,7 +355,7 @@ end

local function decodeInt(enc)
local a = {}
local encCopy = {}
local encCopy = table.create(21)

for i = 1, 21 do
local byte = enc[i]
Expand All @@ -369,20 +368,20 @@ local function decodeInt(enc)
for i = 1, 21, 3 do
local word = 0
for j = 2, 0, -1 do
word = word * 256
word = word + encCopy[i + j]
word *= 256 + encCopy[i + j]
end

table.insert(a, word)
end

return a
end

local function mods(d, w)
local result = d[1] % 2 ^ w
local result = d[1] % twoPower[w]

if result >= 2 ^ (w - 1) then
result = result - 2 ^ w
if result >= twoPower[w - 1] then
result -= twoPower[w]
end

return result
Expand All @@ -391,19 +390,19 @@ end
-- Represents a 168-bit number as the (2^w)-ary Non-Adjacent Form
local function NAF(d, w)
local t, t_len = {}, 0
local d = { unpack(d) }
local newD = { table.unpack(d) }

for i = 1, 168 do
if d[1] % 2 == 1 then
for _ = 1, 168 do
if newD[1] % 2 == 1 then
t_len += 1
t[t_len] = mods(d, w)
d = sub(d, { t[#t], 0, 0, 0, 0, 0, 0 })
t[t_len] = mods(newD, w)
newD = sub(newD, { t[#t], 0, 0, 0, 0, 0, 0 })
else
t_len += 1
t[t_len] = 0
end

d = rShift(d)
newD = rShift(newD)
end

return t
Expand Down
22 changes: 14 additions & 8 deletions src/EllipticCurveCryptography/chacha20.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
-- http://www.computercraft.info/forums2/index.php?/user/12870-anavrins
-- http://pastebin.com/GPzf9JSa
-- Last update: April 17, 2017
local twoPower = require(script.Parent.twoPower)
local util = require(script.Parent.util)

local bxor = bit32.bxor
Expand All @@ -17,7 +18,7 @@ local tau = table.pack(string.byte("expand 16-byte k", 1, -1))
local sigma = table.pack(string.byte("expand 32-byte k", 1, -1))

local function rotl(n, b)
local s = n / (2 ^ (32 - b))
local s = n / twoPower[32 - b]
local f = s % 1
return (s - f) + f * mod
end
Expand All @@ -35,17 +36,19 @@ local function quarterRound(s, a, b, c, d)
end

local function hashBlock(state, rnd)
local s = { unpack(state) }
local s = { table.unpack(state) }
for i = 1, rnd do
local r = i % 2 == 1
s = r and quarterRound(s, 1, 5, 9, 13) or quarterRound(s, 1, 6, 11, 16)
s = r and quarterRound(s, 2, 6, 10, 14) or quarterRound(s, 2, 7, 12, 13)
s = r and quarterRound(s, 3, 7, 11, 15) or quarterRound(s, 3, 8, 9, 14)
s = r and quarterRound(s, 4, 8, 12, 16) or quarterRound(s, 4, 5, 10, 15)
end

for i = 1, 16 do
s[i] = (s[i] + state[i]) % mod
end

return s
end

Expand All @@ -59,7 +62,7 @@ end
local function initState(key, nonce, counter)
local isKey256 = #key == 32
local const = isKey256 and sigma or tau
local state = {}
local state = table.create(16)

state[1] = LE_toInt(const, 0)
state[2] = LE_toInt(const, 4)
Expand All @@ -84,14 +87,15 @@ local function initState(key, nonce, counter)
end

local function serialize(state)
local r, len_r = {}, 0
local r, len_r = table.create(16), 0
for i = 1, 16 do
r[len_r + 1] = band(state[i], 0xFF)
r[len_r + 2] = band(brshift(state[i], 8), 0xFF)
r[len_r + 3] = band(brshift(state[i], 16), 0xFF)
r[len_r + 4] = band(brshift(state[i], 24), 0xFF)
len_r += 4
end

return r
end

Expand All @@ -101,21 +105,22 @@ local function crypt(data, key, nonce, cntr, round)
assert(#key == 16 or #key == 32, "ChaCha20: Invalid key length (" .. #key .. "), must be 16 or 32")
assert(#nonce == 12, "ChaCha20: Invalid nonce length (" .. #nonce .. "), must be 12")

local data = type(data) == "table" and { unpack(data) } or util.stringToByteArray(data)
local newData = type(data) == "table" and { table.unpack(data) } or util.stringToByteArray(data)
cntr = tonumber(cntr) or 1
round = tonumber(round) or 20

local out, out_len = {}, 0
local state = initState(key, nonce, cntr)
local blockAmt = math.floor(#data / 64)
local blockAmt = math.floor(#newData / 64)
for i = 0, blockAmt do
local ks = serialize(hashBlock(state, round))
state[13] = (state[13] + 1) % mod

local block = {}
local block = table.create(64)
for j = 1, 64 do
block[j] = data[(i * 64) + j]
block[j] = newData[(i * 64) + j]
end

for j = 1, #block do
out_len += 1
out[out_len] = bxor(block[j], ks[j])
Expand All @@ -125,6 +130,7 @@ local function crypt(data, key, nonce, cntr, round)
task.wait()
end
end

return setmetatable(out, util.byteTableMT)
end

Expand Down
Loading

0 comments on commit 058634f

Please sign in to comment.