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

feat: expose secp hashing to lua environment #314

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
6 changes: 5 additions & 1 deletion scripts/gen_bytecode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ function gen_bytecode()
from_balance, from_seq = get_account(from)
payload = sig_payload(to, value, sequence)
check_sig(from, sig, payload)
if sequence < from_seq then

hash = make_hash("Hello World!")
print(hash)

if sequence < from_seq then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably, this is meant to test that the newly-exposed function works; can this be verified using a real test instead of adding IO to the bytecode generation?

On that note, when testing the exposure of a hash-function (or any data-transformation) specifically, it's probably a good idea to verify that it works against known values (i.e., I'd suggest asserting that the hash from "Hello World!" is what you expect).

error("sequence number too low")
end

Expand Down
25 changes: 25 additions & 0 deletions src/parsec/agent/runners/lua/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace cbdc::parsec::agent::runner {
luaL_openlibs(m_state.get());

lua_register(m_state.get(), "check_sig", &lua_runner::check_sig);
lua_register(m_state.get(), "make_hash", &lua_runner::make_hash);

static constexpr auto function_name = "contract";

Expand Down Expand Up @@ -299,4 +300,28 @@ namespace cbdc::parsec::agent::runner {

return 0;
}

auto lua_runner::make_hash(lua_State* L) -> int {
int n = lua_gettop(L);
if(n != 1) {
lua_pushliteral(L, "hash takes 1 arg");
lua_error(L);
}
if(lua_isstring(L, 1) != 1) {
lua_pushliteral(L, "invalid preimage");
lua_error(L);
}
size_t sz{};
const auto* str = lua_tolstring(L, 1, &sz);
assert(str != nullptr);
auto sha = CSHA256();
auto unsigned_str = std::vector<unsigned char>(sz);
std::memcpy(unsigned_str.data(), str, sz);
sha.Write(unsigned_str.data(), sz);
cbdc::hash_t computed_hash{};
sha.Finalize(computed_hash.data());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cbdc::hash_data() is effectively this snippet refactored into a method; I might suggest using it here directly.

lua_pushlstring(L, reinterpret_cast<char*>(computed_hash.data()), sz);
return 1; // function returns 1 stack element
}

}
1 change: 1 addition & 0 deletions src/parsec/agent/runners/lua/impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace cbdc::parsec::agent::runner {
handle_try_lock(const broker::interface::try_lock_return_type& res);

static auto check_sig(lua_State* L) -> int;
static auto make_hash(lua_State* L) -> int;
};
}

Expand Down
Loading