From bcc138560a9068032af587ae4e72d7edaf47939d Mon Sep 17 00:00:00 2001 From: Ramana Kumar Date: Sat, 19 Nov 2022 09:35:55 +0000 Subject: [PATCH 1/2] Update interface for verify_kzg_proof --- bindings/csharp/ckzg.c | 5 +---- bindings/node.js/kzg.cxx | 6 +----- src/c_kzg_4844.c | 16 ++++++++++++++-- src/c_kzg_4844.h | 4 ++-- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/bindings/csharp/ckzg.c b/bindings/csharp/ckzg.c index 897b369..0b91eb2 100644 --- a/bindings/csharp/ckzg.c +++ b/bindings/csharp/ckzg.c @@ -63,15 +63,12 @@ C_KZG_RET compute_aggregate_kzg_proof_wrap(uint8_t out[48], const Blob blobs[], int verify_kzg_proof_wrap(const uint8_t c[48], const uint8_t x[32], const uint8_t y[32], const uint8_t p[48], KZGSettings *s) { KZGCommitment commitment; KZGProof proof; - BLSFieldElement fx, fy; bool out; - bytes_to_bls_field(&fx, x); - bytes_to_bls_field(&fy, y); if (bytes_to_g1(&commitment, c) != C_KZG_OK) return -1; if (bytes_to_g1(&proof, p) != C_KZG_OK) return -1; - if (verify_kzg_proof(&out, &commitment, &fx, &fy, &proof, s) != C_KZG_OK) + if (verify_kzg_proof(&out, &commitment, x, y, &proof, s) != C_KZG_OK) return -2; return out ? 0 : 1; diff --git a/bindings/node.js/kzg.cxx b/bindings/node.js/kzg.cxx index 958b4cc..924c67f 100644 --- a/bindings/node.js/kzg.cxx +++ b/bindings/node.js/kzg.cxx @@ -306,10 +306,6 @@ Napi::Value VerifyKzgProof(const Napi::CallbackInfo& info) { auto kzg_settings = info[4].As>().Data(); - BLSFieldElement fz, fy; - bytes_to_bls_field(&fz, z); - bytes_to_bls_field(&fy, y); - KZGCommitment commitment; auto ret = bytes_to_g1(&commitment, polynomial_kzg); if (ret != C_KZG_OK) { @@ -327,7 +323,7 @@ Napi::Value VerifyKzgProof(const Napi::CallbackInfo& info) { } bool out; - if (verify_kzg_proof(&out, &commitment, &fz, &fy, &proof, kzg_settings) != C_KZG_OK) { + if (verify_kzg_proof(&out, &commitment, z, y, &proof, kzg_settings) != C_KZG_OK) { Napi::TypeError::New(env, "Failed to verify KZG proof").ThrowAsJavaScriptException(); return env.Null(); } diff --git a/src/c_kzg_4844.c b/src/c_kzg_4844.c index f659748..64ef5eb 100644 --- a/src/c_kzg_4844.c +++ b/src/c_kzg_4844.c @@ -934,7 +934,7 @@ void blob_to_kzg_commitment(KZGCommitment *out, const Blob blob, const KZGSettin * @param[in] ks The settings containing the secrets, previously initialised with #new_kzg_settings * @retval C_CZK_OK All is well */ -C_KZG_RET verify_kzg_proof(bool *out, const g1_t *commitment, const fr_t *x, const fr_t *y, +static C_KZG_RET verify_kzg_proof_impl(bool *out, const g1_t *commitment, const fr_t *x, const fr_t *y, const g1_t *proof, const KZGSettings *ks) { g2_t x_g2, s_minus_x; g1_t y_g1, commitment_minus_y; @@ -948,6 +948,18 @@ C_KZG_RET verify_kzg_proof(bool *out, const g1_t *commitment, const fr_t *x, con return C_KZG_OK; } +C_KZG_RET verify_kzg_proof(bool *out, + const KZGCommitment *commitment, + const uint8_t z[BYTES_PER_FIELD_ELEMENT], + const uint8_t y[BYTES_PER_FIELD_ELEMENT], + const KZGProof *kzg_proof, + const KZGSettings *s) { + BLSFieldElement frz, fry; + bytes_to_bls_field(&frz, z); + bytes_to_bls_field(&fry, y); + return verify_kzg_proof_impl(out, commitment, &frz, &fry, kzg_proof, s); +} + static C_KZG_RET evaluate_polynomial_in_evaluation_form(BLSFieldElement *out, const Polynomial p, const BLSFieldElement *x, const KZGSettings *s) { fr_t tmp, *inverses_in, *inverses; uint64_t i; @@ -1190,5 +1202,5 @@ C_KZG_RET verify_aggregate_kzg_proof(bool *out, BLSFieldElement y; TRY(evaluate_polynomial_in_evaluation_form(&y, aggregated_poly, &evaluation_challenge, s)); - return verify_kzg_proof(out, &aggregated_poly_commitment, &evaluation_challenge, &y, kzg_aggregated_proof, s); + return verify_kzg_proof_impl(out, &aggregated_poly_commitment, &evaluation_challenge, &y, kzg_aggregated_proof, s); } diff --git a/src/c_kzg_4844.h b/src/c_kzg_4844.h index aa8960d..6437925 100644 --- a/src/c_kzg_4844.h +++ b/src/c_kzg_4844.h @@ -116,8 +116,8 @@ void blob_to_kzg_commitment(KZGCommitment *out, C_KZG_RET verify_kzg_proof(bool *out, const KZGCommitment *polynomial_kzg, - const BLSFieldElement *z, - const BLSFieldElement *y, + const uint8_t z[BYTES_PER_FIELD_ELEMENT], + const uint8_t y[BYTES_PER_FIELD_ELEMENT], const KZGProof *kzg_proof, const KZGSettings *s); From 471319fd81025dd82bbf3e98c808b386f52e17d8 Mon Sep 17 00:00:00 2001 From: Ramana Kumar Date: Mon, 21 Nov 2022 17:33:59 +0000 Subject: [PATCH 2/2] Rename x to z in csharp bindings for verify_kzg_proof just for consistency --- bindings/csharp/Ckzg.Bindings/Ckzg.cs | 2 +- bindings/csharp/ckzg.c | 4 ++-- bindings/csharp/ckzg.h | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bindings/csharp/Ckzg.Bindings/Ckzg.cs b/bindings/csharp/Ckzg.Bindings/Ckzg.cs index 8bf98f3..b004b84 100644 --- a/bindings/csharp/Ckzg.Bindings/Ckzg.cs +++ b/bindings/csharp/Ckzg.Bindings/Ckzg.cs @@ -33,7 +33,7 @@ public class Ckzg public static unsafe extern int verify_aggregate_kzg_proof(byte[] blobs, byte[] commitments, int blobCount, byte[] proof, IntPtr ts); [DllImport("ckzg", EntryPoint = "verify_kzg_proof_wrap", CallingConvention = CallingConvention.Cdecl)] // returns 0 on success - public static extern int verify_kzg_proof(byte[/*48*/] commitment, byte[/*32*/] x, byte[/*32*/] y, byte[/*48*/] proof, IntPtr ts); + public static extern int verify_kzg_proof(byte[/*48*/] commitment, byte[/*32*/] z, byte[/*32*/] y, byte[/*48*/] proof, IntPtr ts); [DllImport("ckzg", EntryPoint = "load_trusted_setup_wrap")] // free result with free_trusted_setup() public static extern IntPtr load_trusted_setup(string filename); diff --git a/bindings/csharp/ckzg.c b/bindings/csharp/ckzg.c index 0b91eb2..c97ebba 100644 --- a/bindings/csharp/ckzg.c +++ b/bindings/csharp/ckzg.c @@ -60,7 +60,7 @@ C_KZG_RET compute_aggregate_kzg_proof_wrap(uint8_t out[48], const Blob blobs[], return C_KZG_OK; } -int verify_kzg_proof_wrap(const uint8_t c[48], const uint8_t x[32], const uint8_t y[32], const uint8_t p[48], KZGSettings *s) { +int verify_kzg_proof_wrap(const uint8_t c[48], const uint8_t z[32], const uint8_t y[32], const uint8_t p[48], KZGSettings *s) { KZGCommitment commitment; KZGProof proof; bool out; @@ -68,7 +68,7 @@ int verify_kzg_proof_wrap(const uint8_t c[48], const uint8_t x[32], const uint8_ if (bytes_to_g1(&commitment, c) != C_KZG_OK) return -1; if (bytes_to_g1(&proof, p) != C_KZG_OK) return -1; - if (verify_kzg_proof(&out, &commitment, x, y, &proof, s) != C_KZG_OK) + if (verify_kzg_proof(&out, &commitment, z, y, &proof, s) != C_KZG_OK) return -2; return out ? 0 : 1; diff --git a/bindings/csharp/ckzg.h b/bindings/csharp/ckzg.h index a99e0cd..835acd2 100644 --- a/bindings/csharp/ckzg.h +++ b/bindings/csharp/ckzg.h @@ -6,7 +6,7 @@ #ifdef _WIN32 #define DLLEXPORT __declspec(dllexport) #else -#define DLLEXPORT +#define DLLEXPORT #endif DLLEXPORT KZGSettings* load_trusted_setup_wrap(const char* file); @@ -19,4 +19,4 @@ DLLEXPORT int verify_aggregate_kzg_proof_wrap(const Blob blobs[], const uint8_t DLLEXPORT C_KZG_RET compute_aggregate_kzg_proof_wrap(uint8_t out[48], const Blob blobs[], size_t n, const KZGSettings *s); -DLLEXPORT int verify_kzg_proof_wrap(const uint8_t c[48], const uint8_t x[32], const uint8_t y[32], const uint8_t p[48], KZGSettings *s); +DLLEXPORT int verify_kzg_proof_wrap(const uint8_t c[48], const uint8_t z[32], const uint8_t y[32], const uint8_t p[48], KZGSettings *s);