Skip to content

Commit

Permalink
refactor: replace crypto++ with tiny sha3 to compute Keccak hash
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Nov 6, 2023
1 parent 031592f commit 88bf574
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 237 deletions.
2 changes: 0 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
**/Dockerfile*

build
third-party/cryptopp-CRYPTOPP_7_0_0
third-party/downloads
third-party/grpc
src/cartesi-machine-client
src/cartesi-machine-server
src/cartesi-machine-hash
Expand Down
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
*.deb

build
third-party/cryptopp-CRYPTOPP_7_0_0
third-party/downloads
third-party/grpc
third-party/mongoose-*
src/remote-cartesi-machine
src/jsonrpc-remote-cartesi-machine
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ RUN apt-get update && \
libboost1.81-dev libssl-dev \
ca-certificates automake libtool patchelf pkg-config lua5.4 liblua5.4-dev \
libgrpc++-dev libprotobuf-dev protobuf-compiler-grpc \
luarocks libcrypto++-dev && \
luarocks && \
update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-15 120 && \
update-alternatives --install /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-15 120 && \
rm -rf /var/lib/apt/lists/*
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ apt-get install build-essential wget git clang-tidy-15 clang-format-15 \
libboost1.81-dev libssl-dev \
ca-certificates automake libtool patchelf pkg-config lua5.4 liblua5.4-dev \
libgrpc++-dev libprotobuf-dev protobuf-compiler-grpc \
luarocks libcrypto++-dev
luarocks
sudo luarocks install --lua-version=5.4 lpeg
sudo luarocks install --lua-version=5.4 dkjson
Expand All @@ -47,7 +47,7 @@ sudo luarocks install --lua-version=5.4 luaposix

##### MacPorts
```
sudo port install clang-15 automake boost libtool wget pkgconfig grpc openssl lua libcryptopp lua-luarocks
sudo port install clang-15 automake boost libtool wget pkgconfig grpc openssl lua lua-luarocks
sudo luarocks install --lua-version=5.4 lpeg
sudo luarocks install --lua-version=5.4 dkjson
Expand All @@ -58,7 +58,7 @@ sudo luarocks install --lua-version=5.4 luaposix

##### Homebrew
```
brew install llvm@15 automake boost wget cryptopp pkg-config grpc openssl [email protected] luarocks
brew install llvm@15 automake boost wget pkg-config grpc openssl [email protected] luarocks
luarocks --lua-dir=$(brew --prefix)/opt/[email protected] install lpeg
luarocks --lua-dir=$(brew --prefix)/opt/[email protected] install dkjson
luarocks --lua-dir=$(brew --prefix)/opt/[email protected] install luasocket
Expand Down
46 changes: 24 additions & 22 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ ifneq (,$(shell which brew))
BREW_PREFIX := $(shell brew --prefix)
BOOST_LIB_DIR_Darwin=-L$(BREW_PREFIX)/lib
BOOST_INC_Darwin=-I$(BREW_PREFIX)/include
CRYPTOPP_LIB_Darwin:=-L$(BREW_PREFIX)/lib -lcryptopp
CRYPTOPP_INC_Darwin:=-I$(BREW_PREFIX)/include
GRPC_INC_Darwin:=$(shell pkg-config --cflags-only-I grpc++)
GRPC_LIB_Darwin:=$(shell pkg-config --libs grpc++)
PROTOBUF_INC_Darwin:=$(shell pkg-config --cflags-only-I protobuf)
Expand All @@ -74,8 +72,6 @@ else ifneq (,$(shell which port)) # Macports installation
PORT_PREFIX := /opt/local
BOOST_LIB_DIR_Darwin=-L$(PORT_PREFIX)/libexec/boost/1.81/lib
BOOST_INC_Darwin=-I$(PORT_PREFIX)/libexec/boost/1.81/include
CRYPTOPP_LIB_Darwin:=-L$(PORT_PREFIX)/lib -lcryptopp
CRYPTOPP_INC_Darwin:=-I$(PORT_PREFIX)/include
GRPC_INC_Darwin:=-I$(PORT_PREFIX)/include
GRPC_LIB_Darwin=-L$(PORT_PREFIX)/lib -lgrpc++ -lgrpc -lgpr -lprotobuf -lpthread -labsl_synchronization
PROTOBUF_INC_Darwin:=-I$(PORT_PREFIX)/include
Expand Down Expand Up @@ -106,8 +102,6 @@ INCS_Linux=
FS_LIB_Linux=-lstdc++fs
PTHREAD_LIB_Linux:=-lpthread
BOOST_INC_Linux:=
CRYPTOPP_LIB_Linux:=-lcryptopp
CRYPTOPP_INC_Linux:=
GRPC_INC_Linux:=$(shell pkg-config --cflags-only-I grpc++)
GRPC_LIB_Linux:=$(shell pkg-config --libs grpc++)
PROTOBUF_INC_Linux:=$(shell pkg-config --cflags-only-I protobuf)
Expand All @@ -129,8 +123,6 @@ CXX=$(CXX_$(UNAME))
SOLDFLAGS:=$(SOLDFLAGS_$(UNAME)) $(GCLDFLAGS)
PTHREAD_LIB:=$(PTHREAD_LIB_$(UNAME))
BOOST_INC:=$(BOOST_INC_$(UNAME))
CRYPTOPP_LIB=$(CRYPTOPP_LIB_$(UNAME))
CRYPTOPP_INC=$(CRYPTOPP_INC_$(UNAME))
GRPC_INC:=$(GRPC_INC_$(UNAME))
GRPC_LIB:=$(GRPC_LIB_$(UNAME))
PROTOBUF_INC:=$(PROTOBUF_INC_$(UNAME))
Expand All @@ -146,15 +138,15 @@ LIBCARTESI_GRPC_LDFLAGS=$(LIBCARTESI_GRPC_LDFLAGS_$(UNAME))
LIBCARTESI_GRPC_TESTS_LDFLAGS=$(LIBCARTESI_GRPC_TESTS_LDFLAGS_$(UNAME))
LIBCARTESI_GRPC_LIB=-L. -lcartesi_grpc-$(EMULATOR_VERSION_MAJOR).$(EMULATOR_VERSION_MINOR)

LIBCARTESI_LIBS:=$(CRYPTOPP_LIB)
LIBCARTESI_GRPC_LIBS:=$(CRYPTOPP_LIB) $(GRPC_LIB) $(PROTOBUF_LIB)
LUACARTESI_LIBS:=$(LIBCARTESI_LIB) $(CRYPTOPP_LIB)
LUACARTESI_GRPC_LIBS:=$(LIBCARTESI_LIB) $(CRYPTOPP_LIB) $(LIBCARTESI_GRPC_LIB)
LUACARTESI_JSONRPC_LIBS:=$(LIBCARTESI_LIB) $(CRYPTOPP_LIB)
REMOTE_CARTESI_MACHINE_LIBS:=$(CRYPTOPP_LIB) $(GRPC_LIB) $(PROTOBUF_LIB)
JSONRPC_REMOTE_CARTESI_MACHINE_LIBS:=$(CRYPTOPP_LIB)
TEST_MACHINE_C_API_LIBS:=$(LIBCARTESI_LIB) $(CRYPTOPP_LIB) $(LIBCARTESI_GRPC_LIB) $(PTHREAD_LIB)
HASH_LIBS:=$(CRYPTOPP_LIB)
LIBCARTESI_LIBS:=
LIBCARTESI_GRPC_LIBS:=$(GRPC_LIB) $(PROTOBUF_LIB)
LUACARTESI_LIBS:=$(LIBCARTESI_LIB)
LUACARTESI_GRPC_LIBS:=$(LIBCARTESI_LIB) $(LIBCARTESI_GRPC_LIB)
LUACARTESI_JSONRPC_LIBS:=$(LIBCARTESI_LIB)
REMOTE_CARTESI_MACHINE_LIBS:=$(GRPC_LIB) $(PROTOBUF_LIB)
JSONRPC_REMOTE_CARTESI_MACHINE_LIBS:=
TEST_MACHINE_C_API_LIBS:=$(LIBCARTESI_LIB) $(LIBCARTESI_GRPC_LIB) $(PTHREAD_LIB)
HASH_LIBS:=

#DEFS+= -DMT_ALL_DIRTY

Expand All @@ -164,9 +156,10 @@ WARNS=-W -Wall -pedantic
INCS= \
-I../lib/machine-emulator-defines \
-I../third-party/llvm-flang-uint128 \
-I../third-party/tiny_sha3 \
-I../third-party/downloads \
-I../third-party/mongoose-7.12 \
$(LUA_INC) $(CRYPTOPP_INC) $(BOOST_INC) $(PROTOBUF_INC) $(GRPC_INC) $(INCS_$(UNAME))
$(LUA_INC) $(BOOST_INC) $(PROTOBUF_INC) $(GRPC_INC) $(INCS_$(UNAME))

ifeq ($(dump),yes)
#DEFS+=-DDUMP_ILLEGAL_INSN_EXCEPTIONS
Expand Down Expand Up @@ -254,11 +247,9 @@ PGO_WORKLOAD=\
dhrystone 500000; \
whetstone 2500

# We ignore xkcp-keccak-256-hasher.cpp because it is missing a header file.
# The file is not being compiled but we want to keep it for reference.
# We ignore test-machine-c-api.cpp cause it takes too long.
LINTER_IGNORE_SOURCES=xkcp-keccak-256-hasher.cpp test-machine-c-api.cpp
LINTER_IGNORE_HEADERS=%.pb.h xkcp-keccak-256-hasher.h
LINTER_IGNORE_SOURCES=test-machine-c-api.cpp
LINTER_IGNORE_HEADERS=%.pb.h
LINTER_SOURCES=$(filter-out $(LINTER_IGNORE_SOURCES),$(strip $(wildcard *.cpp) $(wildcard *.c)))
LINTER_HEADERS=$(filter-out $(LINTER_IGNORE_HEADERS),$(strip $(wildcard *.hpp) $(wildcard *.h)))

Expand Down Expand Up @@ -323,6 +314,7 @@ LIBCARTESI_OBJS:= \
shadow-pmas-factory.o \
shadow-tlb.o \
shadow-tlb-factory.o \
sha3.o \
machine-merkle-tree.o \
pristine-merkle-tree.o \
pma.o \
Expand All @@ -338,6 +330,7 @@ LIBCARTESI_OBJS:= \
uarch-interpret.o

LUACARTESI_OBJS:= \
sha3.o \
clua-cartesi.o \
clua-i-virtual-machine.o \
clua-machine.o \
Expand All @@ -355,6 +348,7 @@ GRPC_GEN_OBJS:= \
cartesi-machine-checkin.grpc.pb.o

LIBCARTESI_GRPC_OBJS:= \
sha3.o \
machine-merkle-tree.o \
pristine-merkle-tree.o \
$(GRPC_GEN_OBJS) \
Expand Down Expand Up @@ -531,11 +525,13 @@ hash: merkle-tree-hash tests/test-merkle-tree-hash
c-api: $(LIBCARTESI) $(LIBCARTESI_GRPC) tests/test-machine-c-api

MERKLE_TREE_HASH_OBJS:= \
sha3.o \
back-merkle-tree.o \
pristine-merkle-tree.o \
merkle-tree-hash.o

TEST_MERKLE_TREE_HASH_OBJS:= \
sha3.o \
back-merkle-tree.o \
pristine-merkle-tree.o \
complete-merkle-tree.o \
Expand All @@ -544,6 +540,7 @@ TEST_MERKLE_TREE_HASH_OBJS:= \

TEST_MACHINE_C_API_OBJS:= \
test-machine-c-api.o \
sha3.o \
back-merkle-tree.o \
pristine-merkle-tree.o

Expand Down Expand Up @@ -574,6 +571,7 @@ REMOTE_CARTESI_MACHINE_OBJS:= \
shadow-pmas-factory.o \
shadow-tlb.o \
shadow-tlb-factory.o \
sha3.o \
machine-merkle-tree.o \
pristine-merkle-tree.o \
pma.o \
Expand Down Expand Up @@ -606,6 +604,7 @@ JSONRPC_REMOTE_CARTESI_MACHINE_OBJS:= \
shadow-pmas-factory.o \
shadow-tlb.o \
shadow-tlb-factory.o \
sha3.o \
machine-merkle-tree.o \
pristine-merkle-tree.o \
pma.o \
Expand Down Expand Up @@ -732,6 +731,9 @@ jsonrpc-discover.cpp: jsonrpc-discover.json
mongoose.o: ../third-party/mongoose-7.12/mongoose.c
$(CC) $(CFLAGS) $(MONGOOSE_DEFS) -c -o $@ $<

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

%.o: %.cpp machine-c-version.h
$(CXX) $(CXXFLAGS) -c -o $@ $<

Expand Down
2 changes: 2 additions & 0 deletions src/complete-merkle-tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
//

#include "complete-merkle-tree.h"
#include <limits>
#include <utility>

/// \file
/// \brief Complete Merkle tree implementation.
Expand Down
64 changes: 0 additions & 64 deletions src/cryptopp-keccak-256-hasher.h

This file was deleted.

51 changes: 48 additions & 3 deletions src/keccak-256-hasher.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,57 @@
#ifndef KECCAK_256_HASHER_H
#define KECCAK_256_HASHER_H

#include "cryptopp-keccak-256-hasher.h"
#include <type_traits>

#include "i-hasher.h"

extern "C" {
#include "sha3.h"
}

namespace cartesi {

/// \brief Class used to compute Keccak 256 hashes
using keccak_256_hasher = cryptopp_keccak_256_hasher;
struct keccak_instance final {
union {
uint8_t b[200];
uint64_t q[25];
} st;
int pt;
};

class keccak_256_hasher final : public i_hasher<keccak_256_hasher, std::integral_constant<int, 32>> {
sha3_ctx_t m_ctx{};

friend i_hasher<keccak_256_hasher, std::integral_constant<int, 32>>;

void do_begin(void) {
sha3_init(&m_ctx, 32, 0x01);
}

void do_add_data(const unsigned char *data, size_t length) {
sha3_update(&m_ctx, data, length);
}

void do_end(hash_type &hash) {
sha3_final(hash.data(), &m_ctx);
}

public:
/// \brief Default constructor
keccak_256_hasher(void) = default;

/// \brief Default destructor
~keccak_256_hasher(void) = default;

/// \brief No copy constructor
keccak_256_hasher(const keccak_256_hasher &) = delete;
/// \brief No move constructor
keccak_256_hasher(keccak_256_hasher &&) = delete;
/// \brief No copy assignment
keccak_256_hasher &operator=(const keccak_256_hasher &) = delete;
/// \brief No move assignment
keccak_256_hasher &operator=(keccak_256_hasher &&) = delete;
};

} // namespace cartesi

Expand Down
4 changes: 2 additions & 2 deletions src/merkle-tree-hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
#include <optional>

#include "back-merkle-tree.h"
#include "cryptopp-keccak-256-hasher.h"
#include "keccak-256-hasher.h"
#include "unique-c-ptr.h"

using namespace cartesi;
using hasher_type = cryptopp_keccak_256_hasher;
using hasher_type = keccak_256_hasher;
using hash_type = hasher_type::hash_type;

/// \brief Checks if string matches prefix and captures remaninder
Expand Down
4 changes: 2 additions & 2 deletions src/test-merkle-tree-hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@

#include "back-merkle-tree.h"
#include "complete-merkle-tree.h"
#include "cryptopp-keccak-256-hasher.h"
#include "full-merkle-tree.h"
#include "keccak-256-hasher.h"
#include "merkle-tree-proof.h"
#include "pristine-merkle-tree.h"
#include "unique-c-ptr.h"

using namespace cartesi;
using hasher_type = cryptopp_keccak_256_hasher;
using hasher_type = keccak_256_hasher;
using hash_type = hasher_type::hash_type;

/// \brief Checks if string matches prefix and captures remaninder
Expand Down
Loading

0 comments on commit 88bf574

Please sign in to comment.