Skip to content

Commit

Permalink
Swap to using bit32.byteswap for blake2s
Browse files Browse the repository at this point in the history
  • Loading branch information
Dekkonot committed Dec 5, 2023
1 parent 2421181 commit 576ac32
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
4 changes: 4 additions & 0 deletions modules/blake2s/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Swap to use built-in `bit32.byteswap` over custom implementation

## Version 1.0.0

- Initial release
32 changes: 8 additions & 24 deletions modules/blake2s/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,6 @@ local SIGMA = {
{ 11, 3, 9, 5, 8, 7, 2, 6, 16, 12, 10, 15, 4, 13, 14, 1 },
}

--[=[
Swaps the byte order of `n`. This is in effect changing the endianness
of an integer.
@param n The number to swap the byte order of.
@return The endianness-swapped number.
]=]
local function byteswap(n: number): number
return bit32.bor(
bit32.lshift(n, 24),
bit32.band(bit32.lshift(n, 8), 0xFF0000),
bit32.band(bit32.rshift(n, 8), 0xFF00),
bit32.rshift(n, 24)
)
end

--[=[
The BLAKE2s compression function.
Expand Down Expand Up @@ -222,14 +206,14 @@ local function blake2s(message: string, outSize: number, key: string?): string
-- TODO efficiency
local digest = string.format(
"%08x%08x%08x%08x%08x%08x%08x%08x",
byteswap(h[1]),
byteswap(h[2]),
byteswap(h[3]),
byteswap(h[4]),
byteswap(h[5]),
byteswap(h[6]),
byteswap(h[7]),
byteswap(h[8])
bit32.byteswap(h[1]),
bit32.byteswap(h[2]),
bit32.byteswap(h[3]),
bit32.byteswap(h[4]),
bit32.byteswap(h[5]),
bit32.byteswap(h[6]),
bit32.byteswap(h[7]),
bit32.byteswap(h[8])
)

return string.sub(digest, 1, outSize * 2)
Expand Down

0 comments on commit 576ac32

Please sign in to comment.