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

Add c-kzg lib #208

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
path = deps/cryptopp
url = https://github.com/weidai11/cryptopp
ignore = untracked
[submodule "deps/c-kzg-4844"]
path = deps/c-kzg-4844
url = https://github.com/ethereum/c-kzg-4844.git
27 changes: 24 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,27 @@ $(PREFIX)/libff/lib/libff.a:
&& $(MAKE) install


# ----------
# c-kzg-4844
# ----------

C_KZG_4844 := $(CURDIR)/deps/c-kzg-4844
C_KZG_4844_CFLAGS := -fPIC -O2 -I$(C_KZG_4844)/src -I$(C_KZG_4844)/inc

$(C_KZG_4844)/lib/libblst.a:
$(MAKE) -C $(C_KZG_4844)/src build_blst

$(C_KZG_4844)/lib/libckzg.o: $(C_KZG_4844)/src/ckzg.c $(C_KZG_4844)/lib/libblst.a
$(CC) $(C_KZG_4844_CFLAGS) $< -c -o $@

$(PREFIX)/c-kzg-4844/lib/libckzg.a: $(C_KZG_4844)/lib/libckzg.o $(C_KZG_4844)/lib/libblst.a
mkdir -p $(dir $@)
ar r $@ $^

.PHONY: c-kzg-4844
c-kzg-4844: $(PREFIX)/c-kzg-4844/lib/libckzg.a


# ------
# blake2
# ------
Expand All @@ -92,7 +113,7 @@ $(PREFIX)/blake2/lib/blake2.a: plugin-c/blake2-compress.o plugin-c/blake2-avx2.o
# plugin-c
# --------

INCLUDES := -I $(K_INCLUDE)/kllvm -I $(K_INCLUDE) -I $(PREFIX)/libcryptopp/include -I $(PREFIX)/libff/include -I dummy-version -I plugin -I plugin-c -I deps/cpp-httplib
INCLUDES := -I $(K_INCLUDE)/kllvm -I $(K_INCLUDE) -I $(PREFIX)/libcryptopp/include -I $(PREFIX)/libff/include -I dummy-version -I plugin -I plugin-c -I deps/cpp-httplib -I$(C_KZG_4844)/inc -I$(C_KZG_4844)/src

ifneq ($(APPLE_SILICON),)
GMP_PREFIX ?= $(shell brew --prefix gmp)
Expand All @@ -106,7 +127,7 @@ endif

CPPFLAGS += --std=c++17 -fPIC -O3 $(INCLUDES)

plugin-c/%.o: plugin-c/%.cpp $(PREFIX)/libcryptopp/lib/libcryptopp.a $(PREFIX)/libff/lib/libff.a
plugin-c/%.o: plugin-c/%.cpp $(PREFIX)/libcryptopp/lib/libcryptopp.a $(PREFIX)/libff/lib/libff.a $(PREFIX)/c-kzg-4844/lib/libckzg.a
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) -o $@ $<

$(PREFIX)/plugin/lib/plugin.a: plugin-c/crypto.o plugin-c/hash_ext.o plugin-c/json.o plugin-c/k.o plugin-c/plugin_util.o
Expand All @@ -121,7 +142,7 @@ plugin: $(PREFIX)/plugin/lib/plugin.a
# krypto
# ------

$(PREFIX)/krypto/lib/krypto.a: $(PREFIX)/libff/lib/libff.a $(PREFIX)/libcryptopp/lib/libcryptopp.a $(PREFIX)/blake2/lib/blake2.a $(PREFIX)/plugin/lib/plugin.a
$(PREFIX)/krypto/lib/krypto.a: $(PREFIX)/libff/lib/libff.a $(PREFIX)/libcryptopp/lib/libcryptopp.a $(PREFIX)/blake2/lib/blake2.a $(PREFIX)/plugin/lib/plugin.a $(PREFIX)/c-kzg-4844/lib/libckzg.a
$(eval TMP := $(shell mktemp -d))
for lib in $^; do \
(cd $(TMP); ar x $$lib;) \
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Building
These instructions are for Ubuntu, assuming that Clang12 is installed.

- Update submodules: `git submodule update --init --recursive`
- Build dependencies: `make CXX=clang++-12 -j3 libff libcryptopp libsecp256k1`
- Build: `make CXX=clang++-12 build -j3`
- Build dependencies: `make CC=clang-14 CXX=clang++-14 -j4 libff libcryptopp libsecp256k1 c-kzg-eip4844`
- Build: `make CC=clang-14 CXX=clang++-14 build -j4`

Testing
-------
Expand Down
1 change: 1 addition & 0 deletions deps/c-kzg-4844
Submodule c-kzg-4844 added at 8a18db
92 changes: 92 additions & 0 deletions plugin-c/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "blake2.h"
#include "plugin_util.h"

#include "c-kzg-4844/src/eip4844/eip4844.h"

using namespace CryptoPP;
using namespace libff;

Expand Down Expand Up @@ -362,4 +364,94 @@ bool hook_KRYPTO_bn128ate(list *g1, list *g2) {
}
return alt_bn128_final_exponentiation(accum) == alt_bn128_GT::one();
}

uint8_t const VERSIONED_HASH_VERSION_KZG = 0x01;

struct string *kzg_to_versioned_hash(struct string *commitment) {
if(len(commitment) != 48) {
throw std::runtime_error("kzg commitment: invalid length");
}
struct string *commitment_sha256 = hook_KRYPTO_sha256raw(commitment),
*result = allocString(64);
memcpy(result->data, &VERSIONED_HASH_VERSION_KZG,1);
memcpy(result->data + 2, commitment_sha256 -> data + 2, 31);
free commitment_sha256 -> data;
free commitment_sha256;
commitment_sha256 = NULL;
return result;
}

static void setup(KGZSettings *s) {
FILE *fp;
C_KZG_RET ret;

/* Open the mainnet trusted setup file */
fp = fopen("deps/k-czg-4844/trusted_setup.txt", "r");
if(fp == NULL){
throw std::runtime_error("unable to open setup file");
}

/* Load the trusted setup file */
ret = load_trusted_setup_file(s, fp, 0);
if(ret != C_KZG_OK) {
throw std::runtime_error("unable to load trusted setup file");
}

fclose(fp);
}

//int const FIELD_ELEMENTS_PER_BLOB = 4096;
//BLS_MODULUS = 52435875175126190479447740508185965837690552500527637822603658699938581184513;
static const uint8_t FIELD_ELEMENTS_PER_BLOB[32] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00
};

static const uint8_t BLS_MODULUS[32] = {
0x73, 0xED, 0xDC, 0xFF, 0x41, 0xB3, 0xA6, 0xD2,
0xC3, 0x6B, 0xA9, 0xE0, 0xD3, 0xEB, 0x3F, 0xF1,
0x87, 0x7D, 0x5F, 0xA2, 0x02, 0x7F, 0x32, 0x64,
0xDC, 0xB8, 0x18, 0x03, 0xA8, 0x39, 0xF6, 0x03
};

struct string *point_evaluation_precompile(struct string *input) {
//# The data is encoded as follows: versioned_hash | z | y | commitment | proof | with z and y being padded 32 byte big endian values
if (len(input) != 192) {
throw std::runtime_error("input: invalid length");
}
struct string *versioned_hash = allocString(64),
*y = allocString(64),
*z = allocString(64),
*commitment = allocString(96),
*proof = allocString(96);
memcpy(versioned_hash->data, input->data, 32);
memcpy(z->data, input->data + 32, 32);
memcpy(y->data, input->data + 64, 32);
memcpy(commitment->data, input->data + 96, 48);
memcpy(proof->data, input->data + 144, 48);

struct string *computed_versioned_hash = kzg_to_versioned_hash(&commitment);
if (memcmp(versioned_hash.data, computed_versioned_hash->data, 32) != 0) {
throw std::runtime_error("input: versioned hash mismatch");
}

bool ok;
KCZGSettings *settings = malloc(sizeof(KZGSettings));
setup(settings);
verify_kzg_proof(&ok, commitment, z, y, proof, settings);
if(!ok){
throw std::runtime_error("error verifying kzg proof");
}

struct string *result = allocString(64);

// Copy FIELD_ELEMENTS_PER_BLOB and BLS_MODULUS directly
memcpy(result->data, FIELD_ELEMENTS_PER_BLOB, 32);
memcpy(result->data + 32, BLS_MODULUS, 32);

return result;
}

}
7 changes: 7 additions & 0 deletions plugin/krypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,12 @@ The BN128 elliptic curve is defined over 2-dimensional points over the fields of
syntax Bool ::= isValidPoint(G1Point) [function, hook(KRYPTO.bn128valid)]
| isValidPoint(G2Point) [function, symbol(isValidG2Point), hook(KRYPTO.bn128g2valid)]
// ---------------------------------------------------------------------------------------------------
```

Point Evaluation Precompile
---------------------------
```k
syntax Bytes ::= pointEvaluation ( Bytes ) [function, hook(KRYPTO.pointEvaluation)]
// -----------------------------------------------------------------------------------
endmodule
```
Loading