Skip to content

Commit f03a4f7

Browse files
sskeirikehildenb
andauthored
Add Michelson hooks (runtimeverification#104)
* 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]>
1 parent 6c91921 commit f03a4f7

9 files changed

+92
-27
lines changed

.gitmodules

+4
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@
1010
path = deps/secp256k1
1111
url = https://github.com/bitcoin-core/secp256k1
1212
ignore = untracked
13+
[submodule "deps/cryptopp"]
14+
path = deps/cryptopp
15+
url = https://github.com/weidai11/cryptopp
16+
ignore = untracked

Dockerfile

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ RUN apt-get update \
88
cmake \
99
git \
1010
libboost-test-dev \
11-
libcrypto++-dev \
1211
libgflags-dev \
1312
libgmp-dev \
1413
libjemalloc-dev \

Jenkinsfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pipeline {
1515
when { changeRequest() }
1616
steps {
1717
sh '''
18-
make -j16 CXX=clang++-8 libff
18+
make -j16 CXX=clang++-8 libcryptopp libff
1919
make -j16 CXX=clang++-8
2020
'''
2121
}

Makefile

+10-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ endif
1717
INCLUDES := -I $(K_RELEASE)/include/kllvm -I $(PREFIX)/include -I dummy-version -I plugin -I plugin-c -I deps/cpp-httplib
1818
CPPFLAGS += --std=c++14 $(INCLUDES)
1919

20-
.PHONY: build libff
21-
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
20+
.PHONY: build libcryptopp libff libsecp256k1
21+
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
2222

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

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

33+
# libcryptopp
34+
35+
libcryptopp: $(PREFIX)/lib/libcryptopp.a
36+
$(PREFIX)/lib/libcryptopp.a:
37+
cd deps/cryptopp \
38+
&& $(MAKE) \
39+
&& $(MAKE) install PREFIX=$(PREFIX)
40+
3341
# libff
3442

3543
libff: $(PREFIX)/lib/libff.a

deps/cryptopp

Submodule cryptopp added at 9dcc26c

plugin-c/crypto.cpp

+11-23
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,30 @@
1-
#include <cstdint>
21
#include <cryptopp/keccak.h>
32
#include <cryptopp/ripemd.h>
43
#include <cryptopp/sha.h>
54
#include <cryptopp/sha3.h>
65
#include <secp256k1_recovery.h>
7-
#include <gmp.h>
86
#include <libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp>
97
#include <libff/common/profiling.hpp>
10-
#include "runtime/alloc.h"
11-
#include "runtime/header.h"
128
#include "blake2.h"
9+
#include "plugin_util.h"
1310

1411
using namespace CryptoPP;
1512
using namespace libff;
1613

1714
extern "C" {
18-
static inline string* allocString(size_t len) {
19-
struct string *result = (struct string *)koreAllocToken(len + sizeof(string));
20-
set_len(result, len);
21-
return result;
22-
}
2315

24-
static string *hexEncode(unsigned char *digest, size_t len) {
25-
uint64_t hexLen = len * 2;
26-
char byte[3];
27-
struct string *result = allocString(hexLen);
28-
for (size_t i = 0, j = 0; i < len; i++, j += 2) {
29-
sprintf(byte, "%02x", digest[i]);
30-
result->data[j] = byte[0];
31-
result->data[j+1] = byte[1];
32-
}
33-
return result;
16+
struct string *hook_KRYPTO_sha512raw(struct string *str) {
17+
SHA512 h;
18+
unsigned char digest[64];
19+
h.CalculateDigest(digest, (unsigned char *)str->data, len(str));
20+
return raw(digest, sizeof(digest));
3421
}
3522

36-
static string *raw(unsigned char *digest, size_t len) {
37-
struct string *result = allocString(len);
38-
memcpy(result->data, digest, len);
39-
return result;
23+
struct string *hook_KRYPTO_sha512(struct string *str) {
24+
SHA512 h;
25+
unsigned char digest[64];
26+
h.CalculateDigest(digest, (unsigned char *)str->data, len(str));
27+
return hexEncode(digest, sizeof(digest));
4028
}
4129

4230
struct string *hook_KRYPTO_sha3raw(struct string *str) {

plugin-c/hash_ext.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <cryptopp/blake2.h>
2+
#include "plugin_util.h"
3+
4+
using namespace CryptoPP;
5+
6+
extern "C" {
7+
8+
struct string *hook_KRYPTO_blake2b256raw(struct string *str) {
9+
BLAKE2b h(false,32);
10+
unsigned char digest[32];
11+
h.CalculateDigest(digest, (unsigned char *)str->data, len(str));
12+
return raw(digest, sizeof(digest));
13+
}
14+
15+
struct string *hook_KRYPTO_blake2b256(struct string *str) {
16+
BLAKE2b h(false,32);
17+
unsigned char digest[32];
18+
h.CalculateDigest(digest, (unsigned char *)str->data, len(str));
19+
return hexEncode(digest, sizeof(digest));
20+
}
21+
22+
}

plugin-c/plugin_util.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "plugin_util.h"
2+
3+
extern "C" {
4+
string* allocString(size_t len) {
5+
struct string *result = (struct string *)koreAllocToken(len + sizeof(string));
6+
set_len(result, len);
7+
return result;
8+
}
9+
10+
string *hexEncode(unsigned char *digest, size_t len) {
11+
uint64_t hexLen = len * 2;
12+
char byte[3];
13+
struct string *result = allocString(hexLen);
14+
for (size_t i = 0, j = 0; i < len; i++, j += 2) {
15+
sprintf(byte, "%02x", digest[i]);
16+
result->data[j] = byte[0];
17+
result->data[j+1] = byte[1];
18+
}
19+
return result;
20+
}
21+
22+
string *raw(unsigned char *digest, size_t len) {
23+
struct string *result = allocString(len);
24+
memcpy(result->data, digest, len);
25+
return result;
26+
}
27+
}

plugin-c/plugin_util.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef PLUGIN_UTIL_H
2+
#define PLUGIN_UTIL_H
3+
4+
#include <cstdint>
5+
#include <cstring>
6+
#include <gmp.h>
7+
#include "runtime/alloc.h"
8+
#include "runtime/header.h"
9+
10+
extern "C" {
11+
string* allocString(size_t len);
12+
string *hexEncode(unsigned char *digest, size_t len);
13+
string *raw(unsigned char *digest, size_t len);
14+
}
15+
16+
#endif

0 commit comments

Comments
 (0)