Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(hash): improve code readability, try to avoid multiple hash initialization and expose also shake256 in zencode hash statement #1003

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 19 additions & 27 deletions src/lua/zencode_hash.lua
Original file line number Diff line number Diff line change
@@ -17,37 +17,29 @@
--If not, see http://www.gnu.org/licenses/agpl.txt
--
--Last modified by Denis Roio
--on Friday, 26th November 2021
--on Monday, 13th January 2025
--]]


-- hashing single strings
When("create hash of ''",function(s)
local src = have(s)
if luatype(src) == 'table' then
src = zencode_serialize(src) -- serialize tables using zenroom's algo
end
ACK.hash = HASH.new(CONF.hash):process(src)
new_codec('hash', { zentype = 'e' })
end
)
-- hashing
local valid_hashes <const> = {
sha256 = true,
sha512 = true,
shake256 = true,
keccak256 = true
}
local function _hash(s, n)
local src = have(s)
n = n or CONF.hash
-- serialize tables using zenroom's algo
src = zencode_serialize(src)
if not valid_hashes[n] then error("Hash algorithm not known: ".. n) end
ACK.hash = HASH[n](src)
new_codec('hash', { zentype = 'e' })
end

When("create hash of '' using ''",function(s, h)
local src = have(s)
if luatype(src) == 'table' then
src = zencode_serialize(src)
end
if strcasecmp(h, 'sha256') then
ACK.hash = sha256(src)
elseif strcasecmp(h, 'sha512') then
ACK.hash = sha512(src)
elseif strcasecmp(h, 'keccak256') then
ACK.hash = HASH.keccak256(src)
end
zencode_assert(ACK.hash, 'Invalid hash: ' .. h)
new_codec('hash', { zentype = 'e' })
end
)
When("create hash of ''", _hash)
When("create hash of '' using ''", _hash)

When("create hash to point '' of ''",function(curve, object)
local F = _G[curve]
58 changes: 25 additions & 33 deletions src/lua/zenroom_hash.lua
Original file line number Diff line number Diff line change
@@ -16,8 +16,8 @@
--GNU Affero General Public License v3.0
--If not, see http://www.gnu.org/licenses/agpl.txt
--
--Last modified by Denis Roio
--on Tuesday, 20th July 2021
--Last modified by Matteo Cristino
--on Monday, 13th January 2025
--]]

local hash = require'hash'
@@ -27,58 +27,50 @@ local SHA256 = nil
local SHA512 = nil
local SHAKE256 = nil
local KECCAK256 = nil
local function init(bits)
local h
if bits == 256 or bits == 32 then
if SHA256==nil then SHA256 = hash.new('sha256') end
h = SHA256
elseif bits == 512 or bits == 64 then
if SHA512==nil then SHA512 = hash.new('sha512') end
h = SHA512
else
error("HASH bits not supported: "..bits)
end
return h
local hash_init_table <const> = {
sha32 = function() SHA256 = SHA256 or hash.new('sha256'); return SHA256 end,
sha256 = function() SHA256 = SHA256 or hash.new('sha256'); return SHA256 end,
sha64 = function() SHA512 = SHA512 or hash.new('sha512'); return SHA512 end,
sha512 = function() SHA512 = SHA512 or hash.new('sha512'); return SHA512 end,
shake256 = function() SHAKE256 = SHAKE256 or hash.new('shake256'); return SHAKE256 end,
keccak256 = function() KECCAK256 = KECCAK256 or hash.new('keccak256'); return KECCAK256 end
}
local function init(name)
local h = hash_init_table[name]
if not h then error("HASH type not supported: "..name, 2) end
return h();
end

function sha256(data) return init(256):process(data) end
function sha512(data) return init(512):process(data) end
hash.shake256 = function(data, len)
if SHAKE256==nil then SHAKE256 = hash.new('shake256') end
if not len then len = 32 end
return SHAKE256:process(data, len)
end
function sha256(data) return init("sha256"):process(data) end
function sha512(data) return init("sha512"):process(data) end

hash.keccak256 = function(data)
if KECCAK256==nil then KECCAK256 = hash.new('keccak256') end
return KECCAK256:process(data)
end
hash.sha256 = sha256
hash.sha512 = sha512
hash.shake256 = function(data, len) return init("shake256"):process(data, len or 32) end
hash.keccak256 = function(data) return init("keccak256"):process(data) end

function KDF(data, bits)
local b = bits or 256
return init(b):kdf2(data)
end
function KDF(data, bits) return init("sha"..tostring(bits or 256)):kdf2(data) end

function hash.dsha256(msg)
local _SHA256 = HASH.new('sha256')
local _SHA256 = init('sha256')
return _SHA256:process(_SHA256:process(msg))
end

function hash.hash160(msg)
local _SHA256 = HASH.new('sha256')
local _SHA256 = init('sha256')
local _RMD160 = HASH.new('ripemd160')
return _RMD160:process(_SHA256:process(msg))

end

--used in BBS+ signature
hash.hkdf_extract = function(salt, ikm)
return HASH.hmac(hash.new('sha256'), salt, ikm)
return HASH.hmac(init('sha256'), salt, ikm)
end

--used in BBS+ signature
hash.hkdf_expand = function(prk, info, l)
local h = hash.new('sha256')
local h = init('sha256')
local hash_len = 32
assert(#prk >= hash_len)
assert(l <= 255 * hash_len)
2 changes: 1 addition & 1 deletion test/zencode/rules.bats
Original file line number Diff line number Diff line change
@@ -243,7 +243,7 @@ and I create the hash of 'source'
Then print 'hash'
EOF
run $ZENROOM_EXECUTABLE -z set_fail.zen
assert_line '[!] Hash algorithm not known: sha123'
assert_line --partial 'Hash algorithm not known: sha123'
}

# --- Rule invalid --- #
Loading