Skip to content

Commit

Permalink
chore: remove tiny_sha3
Browse files Browse the repository at this point in the history
  • Loading branch information
mpernambuco committed Jan 11, 2025
1 parent 090f3d8 commit 432e809
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 400 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ EMU_TO_INC= $(addprefix src/,jsonrpc-machine-c-api.h machine-c-api.h machine-c-v
UARCH_TO_SHARE= uarch-ram.bin

TESTS_TO_BIN= tests/build/misc/test-merkle-tree-hash tests/build/misc/test-machine-c-api
TESTS_LUA_TO_LUA_PATH=tests/lua/cartesi
TESTS_LUA_TO_LUA_PATH=tests/lua/cartesi tests/lua/third_party
TESTS_LUA_TO_TEST_LUA_PATH=$(wildcard tests/lua/*.lua)
TESTS_SCRIPTS_TO_TEST_SCRIPTS_PATH=$(wildcard tests/scripts/*.sh)
TESTS_DATA_TO_TESTS_DATA_PATH= tests/build/machine tests/build/uarch tests/build/uarch-riscv-arch-test tests/build/images
Expand Down
9 changes: 0 additions & 9 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ WARNS=-Wall -Wextra -Wpedantic
# Place our include directories before the system's
INCS+= \
-I../third-party/llvm-flang-uint128 \
-I../third-party/tiny_sha3 \
-I../third-party/SHA256 \
-I../third-party/nlohmann-json \
-I../third-party/downloads \
Expand Down Expand Up @@ -210,9 +209,6 @@ ifneq ($(git_commit),)
DEFS+=-DGIT_COMMIT='"$(git_commit)"'
endif

# The SHA3 is third party library we always want to compile with O3
SHA3_CFLAGS=-O3

# The SHA256 is third party library we always want to compile with O3
SHA256_CFLAGS=-O3

Expand Down Expand Up @@ -370,7 +366,6 @@ LIBCARTESI_OBJS:= \
uarch-machine.o \
uarch-step.o \
uarch-reset-state.o \
sha3.o \
machine-merkle-tree.o \
pristine-merkle-tree.o \
uarch-interpret.o \
Expand All @@ -394,7 +389,6 @@ LUACARTESI_OBJS:= \
$(CARTESI_CLUA_OBJS)

LIBCARTESI_MERKLE_TREE_OBJS:= \
sha3.o \
machine-merkle-tree.o \
back-merkle-tree.o \
pristine-merkle-tree.o \
Expand Down Expand Up @@ -557,9 +551,6 @@ jsonrpc-discover.cpp: jsonrpc-discover.json
@$(CC) $(CFLAGS) $< -MM -MT $@ -MF $@.d > /dev/null 2>&1
@touch $@

sha3.o: ../third-party/tiny_sha3/sha3.c
$(CC) $(CFLAGS) $(SHA3_CFLAGS) -c -o $@ $<

sha256.o: ../third-party/SHA256/sha256.c
$(CC) $(CFLAGS) $(SHA256_CFLAGS) -c -o $@ $<

Expand Down
44 changes: 0 additions & 44 deletions src/clua-cartesi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "base64.h"
#include "clua-i-virtual-machine.h"
#include "clua.h"
#include "keccak-256-hasher.h"
#include "machine-c-api.h"
#include "machine-c-version.h"
#include "riscv-constants.h"
Expand All @@ -52,46 +51,6 @@ static const auto gperf_meta = clua_make_luaL_Reg_array({
});
#endif

/// \brief This is the cartesi.keccak() function implementation.
/// \param L Lua state.
static int cartesi_mod_keccak(lua_State *L) {
using namespace cartesi;
keccak_256_hasher h;
keccak_256_hasher::hash_type hash;
if (lua_gettop(L) > 2) {
luaL_argerror(L, 3, "too many arguments");
}
if (lua_gettop(L) < 1) {
luaL_argerror(L, 1, "too few arguments");
}
if (lua_isinteger(L, 1) != 0) {
if (lua_gettop(L) > 1) {
luaL_argerror(L, 2, "too many arguments");
}
uint64_t word = luaL_checkinteger(L, 1);
h.begin();
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
h.add_data(reinterpret_cast<const unsigned char *>(&word), sizeof(word));
h.end(hash);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
lua_pushlstring(L, reinterpret_cast<const char *>(hash.data()), hash.size());
return 1;
}
h.begin();
size_t len1 = 0;
const char *hash1 = luaL_checklstring(L, 1, &len1);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
h.add_data(reinterpret_cast<const unsigned char *>(hash1), len1);
size_t len2 = 0;
const char *hash2 = luaL_optlstring(L, 2, "", &len2);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
h.add_data(reinterpret_cast<const unsigned char *>(hash2), len2);
h.end(hash);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
lua_pushlstring(L, reinterpret_cast<const char *>(hash.data()), hash.size());
return 1;
}

/// \brief This is the cartesi.keccak() function implementation.
/// \param L Lua state.
static int cartesi_mod_hash(lua_State *L) {
Expand Down Expand Up @@ -187,9 +146,6 @@ static int cartesi_mod_new(lua_State *L) try {

/// \brief Contents of the cartesi module table.
static const auto cartesi_mod = clua_make_luaL_Reg_array({
// keccak is only used in cmio-test.lua.
// If we find a pure Lua implementation, we can remove keccak altogether.
{"keccak", cartesi_mod_keccak},
{"hash", cartesi_mod_hash},
{"tobase64", cartesi_mod_tobase64},
{"frombase64", cartesi_mod_frombase64},
Expand Down
76 changes: 0 additions & 76 deletions src/keccak-256-hasher.h

This file was deleted.

13 changes: 9 additions & 4 deletions tests/lua/cmio-test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@
local cartesi = require("cartesi")
local test_util = require("cartesi.tests.util")
local test_data = require("cartesi.tests.data")
local sha3 = require("third_party.plc.sha3")
local jsonrpc

local function keccak(a, b)
return sha3.keccak(a .. (b or ""))
end

local function adjust_images_path(path)
return string.gsub(path or ".", "/*$", "") .. "/"
end
Expand Down Expand Up @@ -160,7 +165,7 @@ local function check_output(machine, expected)
end
assert(expected == output)

return cartesi.keccak(output)
return keccak(output)
end

local function check_report(machine, expected)
Expand Down Expand Up @@ -195,14 +200,14 @@ local function check_outputs_root_hash(root_hash, output_hashes)
end
local c2 = output_hashes[child + 1]
if c2 then
parent_output_hashes[parent] = cartesi.keccak(c1, c2)
parent_output_hashes[parent] = keccak(c1, c2)
else
parent_output_hashes[parent] = cartesi.keccak(c1, z)
parent_output_hashes[parent] = keccak(c1, z)
end
parent = parent + 1
child = child + 2
end
z = cartesi.keccak(z, z)
z = keccak(z, z)
output_hashes = parent_output_hashes
end
assert(root_hash == output_hashes[1], "output root hash mismatch")
Expand Down
22 changes: 22 additions & 0 deletions tests/lua/third_party/plc/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2018 Phil Leblanc
Copyright (c) 2017 Pierre Chapuis (files salsa20.lua, test_salsa20, box.lua, test_box.lua)

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Loading

0 comments on commit 432e809

Please sign in to comment.