From b0f157063f42d60d5b5b75315c95ef7432c7b31d Mon Sep 17 00:00:00 2001 From: Guy Repta <50716988+gtrepta@users.noreply.github.com> Date: Fri, 8 Nov 2024 22:21:36 -0600 Subject: [PATCH 1/7] Add c-kzg-4844 submodule --- .gitmodules | 3 +++ deps/c-kzg-4844 | 1 + 2 files changed, 4 insertions(+) create mode 160000 deps/c-kzg-4844 diff --git a/.gitmodules b/.gitmodules index c1655550b..43612e3dc 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/deps/c-kzg-4844 b/deps/c-kzg-4844 new file mode 160000 index 000000000..8a18db4a0 --- /dev/null +++ b/deps/c-kzg-4844 @@ -0,0 +1 @@ +Subproject commit 8a18db4a058b635ba1de9a8b59654ca760378871 From f8600621964508b188a1e0122119b028426c56c9 Mon Sep 17 00:00:00 2001 From: Guy Repta <50716988+gtrepta@users.noreply.github.com> Date: Sat, 9 Nov 2024 03:07:18 -0600 Subject: [PATCH 2/7] Build the kzg libraries and include them in krypto --- Makefile | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 91dad44be..0be05f112 100644 --- a/Makefile +++ b/Makefile @@ -72,6 +72,27 @@ $(PREFIX)/libff/lib/libff.a: && $(MAKE) install +# ---------- +# c-kzg-4844 +# ---------- + +C_KZG_4844 := 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: + CC=clang $(MAKE) -C $(C_KZG_4844)/src build_blst + +$(C_KZG_4844)/lib/libckzg.o: $(C_KZG_4844)/src/ckzg.c + clang $(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 # ------ @@ -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) @@ -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 @@ -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;) \ From 6546e866158bd3647523a42f9de7d65df8e463f8 Mon Sep 17 00:00:00 2001 From: Guy Repta <50716988+gtrepta@users.noreply.github.com> Date: Sun, 10 Nov 2024 00:30:39 -0600 Subject: [PATCH 3/7] Fix missing target dependency --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 0be05f112..3bf3c4eeb 100644 --- a/Makefile +++ b/Makefile @@ -76,13 +76,13 @@ $(PREFIX)/libff/lib/libff.a: # c-kzg-4844 # ---------- -C_KZG_4844 := deps/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: CC=clang $(MAKE) -C $(C_KZG_4844)/src build_blst -$(C_KZG_4844)/lib/libckzg.o: $(C_KZG_4844)/src/ckzg.c +$(C_KZG_4844)/lib/libckzg.o: $(C_KZG_4844)/src/ckzg.c $(C_KZG_4844)/lib/libblst.a clang $(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 From d22a3f21b37327379c03673f65a55b7f1ec921dd Mon Sep 17 00:00:00 2001 From: Andrei <16517508+anvacaru@users.noreply.github.com> Date: Wed, 13 Nov 2024 07:16:21 +0200 Subject: [PATCH 4/7] Makefile: parametric clang --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 3bf3c4eeb..d06ebecf3 100644 --- a/Makefile +++ b/Makefile @@ -80,10 +80,10 @@ 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: - CC=clang $(MAKE) -C $(C_KZG_4844)/src build_blst + $(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 - clang $(C_KZG_4844_CFLAGS) $< -c -o $@ + $(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 $@) From 9d9a443ae3041b99bb9bf2c287880dd3b5c06140 Mon Sep 17 00:00:00 2001 From: Andrei <16517508+anvacaru@users.noreply.github.com> Date: Wed, 13 Nov 2024 11:39:08 +0200 Subject: [PATCH 5/7] draft kzg_to_versioned_hash --- README.md | 4 ++-- plugin-c/crypto.cpp | 14 ++++++++++++++ plugin/krypto.md | 7 +++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cc41126e7..7dee179b8 100644 --- a/README.md +++ b/README.md @@ -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 ------- diff --git a/plugin-c/crypto.cpp b/plugin-c/crypto.cpp index 567421ac6..b63b9bd21 100644 --- a/plugin-c/crypto.cpp +++ b/plugin-c/crypto.cpp @@ -362,4 +362,18 @@ 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(struct string *str); + struct string *result = allocString(64); + memcpy(result->data, VERSIONED_HASH_VERSION_KZG,1); + memcpy(result->data + 2, commitment_sha256 -> data + 2, 31); + return result; +} + } diff --git a/plugin/krypto.md b/plugin/krypto.md index 77acd4dd6..3777cbc7c 100644 --- a/plugin/krypto.md +++ b/plugin/krypto.md @@ -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 ``` From c181fd22b018ddfe9e3bcb903ca8029fd77bf7ab Mon Sep 17 00:00:00 2001 From: Andrei <16517508+anvacaru@users.noreply.github.com> Date: Wed, 13 Nov 2024 12:32:42 +0200 Subject: [PATCH 6/7] kzg_to_versioned_hash --- plugin-c/crypto.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugin-c/crypto.cpp b/plugin-c/crypto.cpp index b63b9bd21..868de41ba 100644 --- a/plugin-c/crypto.cpp +++ b/plugin-c/crypto.cpp @@ -369,10 +369,11 @@ 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(struct string *str); + struct string* commitment_sha256 = hook_KRYPTO_sha256raw(commitment); struct string *result = allocString(64); - memcpy(result->data, VERSIONED_HASH_VERSION_KZG,1); + memcpy(result->data, &VERSIONED_HASH_VERSION_KZG,1); memcpy(result->data + 2, commitment_sha256 -> data + 2, 31); + delete commitment_sha256; return result; } From 98a45026271ffe2e6fbb98c82fc35e60135ce4bf Mon Sep 17 00:00:00 2001 From: Andrei <16517508+anvacaru@users.noreply.github.com> Date: Wed, 27 Nov 2024 13:23:05 +0200 Subject: [PATCH 7/7] Draft point_evaluation_precompile --- plugin-c/crypto.cpp | 83 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git a/plugin-c/crypto.cpp b/plugin-c/crypto.cpp index 868de41ba..2158fda23 100644 --- a/plugin-c/crypto.cpp +++ b/plugin-c/crypto.cpp @@ -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; @@ -369,11 +371,86 @@ 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); - struct string *result = allocString(64); + 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); - delete commitment_sha256; + 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; }