Skip to content

Commit

Permalink
Add Michelson hooks (#104)
Browse files Browse the repository at this point in the history
* fix Makefile PHONY targets

* add libcryptopp submodule and build recipe

* make CI build libcryptopp instead of using package

Packaged version does not provide needed symbols

* add blake2b hash function with 256 bit digest size

* add sha2 hash function with 512 bit digest size

* Update libcryptopp submodule to ignore untracked files

Co-authored-by: Everett Hildenbrandt <[email protected]>

* extract common utility functions

* move problematic hash functions to separate file

* remove inline attribute from allocString to fix compile error

Co-authored-by: Everett Hildenbrandt <[email protected]>
  • Loading branch information
sskeirik and ehildenb authored Aug 3, 2020
1 parent 6c91921 commit f03a4f7
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 27 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@
path = deps/secp256k1
url = https://github.com/bitcoin-core/secp256k1
ignore = untracked
[submodule "deps/cryptopp"]
path = deps/cryptopp
url = https://github.com/weidai11/cryptopp
ignore = untracked
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ RUN apt-get update \
cmake \
git \
libboost-test-dev \
libcrypto++-dev \
libgflags-dev \
libgmp-dev \
libjemalloc-dev \
Expand Down
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pipeline {
when { changeRequest() }
steps {
sh '''
make -j16 CXX=clang++-8 libff
make -j16 CXX=clang++-8 libcryptopp libff
make -j16 CXX=clang++-8
'''
}
Expand Down
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ endif
INCLUDES := -I $(K_RELEASE)/include/kllvm -I $(PREFIX)/include -I dummy-version -I plugin -I plugin-c -I deps/cpp-httplib
CPPFLAGS += --std=c++14 $(INCLUDES)

.PHONY: build libff
build: client-c/json.o client-c/main.o plugin-c/blake2.o plugin-c/blockchain.o plugin-c/crypto.o plugin-c/world.o
.PHONY: build libcryptopp libff libsecp256k1
build: client-c/json.o client-c/main.o plugin-c/blake2.o plugin-c/blockchain.o plugin-c/crypto.o plugin-c/plugin_util.o plugin-c/world.o

plugin-c/blockchain.o: plugin/proto/msg.pb.h

Expand All @@ -30,6 +30,14 @@ clean:
rm -rf */*.o */*/*.o plugin/proto/*.pb.* build deps/libff/build
cd deps/secp256k1 && $(MAKE) clean

# libcryptopp

libcryptopp: $(PREFIX)/lib/libcryptopp.a
$(PREFIX)/lib/libcryptopp.a:
cd deps/cryptopp \
&& $(MAKE) \
&& $(MAKE) install PREFIX=$(PREFIX)

# libff

libff: $(PREFIX)/lib/libff.a
Expand Down
1 change: 1 addition & 0 deletions deps/cryptopp
Submodule cryptopp added at 9dcc26
34 changes: 11 additions & 23 deletions plugin-c/crypto.cpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,30 @@
#include <cstdint>
#include <cryptopp/keccak.h>
#include <cryptopp/ripemd.h>
#include <cryptopp/sha.h>
#include <cryptopp/sha3.h>
#include <secp256k1_recovery.h>
#include <gmp.h>
#include <libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp>
#include <libff/common/profiling.hpp>
#include "runtime/alloc.h"
#include "runtime/header.h"
#include "blake2.h"
#include "plugin_util.h"

using namespace CryptoPP;
using namespace libff;

extern "C" {
static inline string* allocString(size_t len) {
struct string *result = (struct string *)koreAllocToken(len + sizeof(string));
set_len(result, len);
return result;
}

static string *hexEncode(unsigned char *digest, size_t len) {
uint64_t hexLen = len * 2;
char byte[3];
struct string *result = allocString(hexLen);
for (size_t i = 0, j = 0; i < len; i++, j += 2) {
sprintf(byte, "%02x", digest[i]);
result->data[j] = byte[0];
result->data[j+1] = byte[1];
}
return result;
struct string *hook_KRYPTO_sha512raw(struct string *str) {
SHA512 h;
unsigned char digest[64];
h.CalculateDigest(digest, (unsigned char *)str->data, len(str));
return raw(digest, sizeof(digest));
}

static string *raw(unsigned char *digest, size_t len) {
struct string *result = allocString(len);
memcpy(result->data, digest, len);
return result;
struct string *hook_KRYPTO_sha512(struct string *str) {
SHA512 h;
unsigned char digest[64];
h.CalculateDigest(digest, (unsigned char *)str->data, len(str));
return hexEncode(digest, sizeof(digest));
}

struct string *hook_KRYPTO_sha3raw(struct string *str) {
Expand Down
22 changes: 22 additions & 0 deletions plugin-c/hash_ext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <cryptopp/blake2.h>
#include "plugin_util.h"

using namespace CryptoPP;

extern "C" {

struct string *hook_KRYPTO_blake2b256raw(struct string *str) {
BLAKE2b h(false,32);
unsigned char digest[32];
h.CalculateDigest(digest, (unsigned char *)str->data, len(str));
return raw(digest, sizeof(digest));
}

struct string *hook_KRYPTO_blake2b256(struct string *str) {
BLAKE2b h(false,32);
unsigned char digest[32];
h.CalculateDigest(digest, (unsigned char *)str->data, len(str));
return hexEncode(digest, sizeof(digest));
}

}
27 changes: 27 additions & 0 deletions plugin-c/plugin_util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "plugin_util.h"

extern "C" {
string* allocString(size_t len) {
struct string *result = (struct string *)koreAllocToken(len + sizeof(string));
set_len(result, len);
return result;
}

string *hexEncode(unsigned char *digest, size_t len) {
uint64_t hexLen = len * 2;
char byte[3];
struct string *result = allocString(hexLen);
for (size_t i = 0, j = 0; i < len; i++, j += 2) {
sprintf(byte, "%02x", digest[i]);
result->data[j] = byte[0];
result->data[j+1] = byte[1];
}
return result;
}

string *raw(unsigned char *digest, size_t len) {
struct string *result = allocString(len);
memcpy(result->data, digest, len);
return result;
}
}
16 changes: 16 additions & 0 deletions plugin-c/plugin_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef PLUGIN_UTIL_H
#define PLUGIN_UTIL_H

#include <cstdint>
#include <cstring>
#include <gmp.h>
#include "runtime/alloc.h"
#include "runtime/header.h"

extern "C" {
string* allocString(size_t len);
string *hexEncode(unsigned char *digest, size_t len);
string *raw(unsigned char *digest, size_t len);
}

#endif

0 comments on commit f03a4f7

Please sign in to comment.