From c72ea8e1dcafa0254bac02b2f813fc8042686f05 Mon Sep 17 00:00:00 2001 From: Justin Traglia <95511699+jtraglia@users.noreply.github.com> Date: Thu, 12 Jan 2023 12:10:23 -0600 Subject: [PATCH] Convert Blob type to struct (#61) * Convert Blob type to struct * Rename blob.data to blob.bytes --- bindings/csharp/ckzg.c | 2 +- bindings/csharp/ckzg.h | 2 +- bindings/java/c_kzg_4844_jni.c | 6 +++--- bindings/node.js/kzg.cxx | 6 +++--- bindings/python/ckzg.c | 2 +- src/c_kzg_4844.c | 18 +++++++++--------- src/c_kzg_4844.h | 10 +++++----- 7 files changed, 23 insertions(+), 23 deletions(-) diff --git a/bindings/csharp/ckzg.c b/bindings/csharp/ckzg.c index 1eafbe2..a204674 100644 --- a/bindings/csharp/ckzg.c +++ b/bindings/csharp/ckzg.c @@ -24,7 +24,7 @@ void free_trusted_setup_wrap(KZGSettings *s) { free(s); } -C_KZG_RET blob_to_kzg_commitment_wrap(uint8_t out[48], const Blob blob, const KZGSettings *s) { +C_KZG_RET blob_to_kzg_commitment_wrap(uint8_t out[48], const Blob *blob, const KZGSettings *s) { KZGCommitment c; C_KZG_RET ret; ret = blob_to_kzg_commitment(&c, blob, s); diff --git a/bindings/csharp/ckzg.h b/bindings/csharp/ckzg.h index 80e0c65..cecc700 100644 --- a/bindings/csharp/ckzg.h +++ b/bindings/csharp/ckzg.h @@ -13,7 +13,7 @@ DLLEXPORT KZGSettings* load_trusted_setup_wrap(const char* file); DLLEXPORT void free_trusted_setup_wrap(KZGSettings *s); -DLLEXPORT C_KZG_RET blob_to_kzg_commitment_wrap(uint8_t out[48], const Blob blob, const KZGSettings *s); +DLLEXPORT C_KZG_RET blob_to_kzg_commitment_wrap(uint8_t out[48], const Blob *blob, const KZGSettings *s); DLLEXPORT int verify_aggregate_kzg_proof_wrap(const Blob blobs[], const uint8_t commitments[], size_t n, const uint8_t proof[48], const KZGSettings *s); diff --git a/bindings/java/c_kzg_4844_jni.c b/bindings/java/c_kzg_4844_jni.c index 4890066..39ddfc8 100644 --- a/bindings/java/c_kzg_4844_jni.c +++ b/bindings/java/c_kzg_4844_jni.c @@ -145,7 +145,7 @@ JNIEXPORT jbyteArray JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_computeAggregate KZGProof p; - C_KZG_RET ret = compute_aggregate_kzg_proof(&p, (uint8_t const(*)[BYTES_PER_BLOB])blobs_native, (size_t)count, settings); + C_KZG_RET ret = compute_aggregate_kzg_proof(&p, (const Blob *)blobs_native, (size_t)count, settings); (*env)->ReleaseByteArrayElements(env, blobs, blobs_native, JNI_ABORT); @@ -222,7 +222,7 @@ JNIEXPORT jboolean JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_verifyAggregateKzg jbyte *blobs_native = (*env)->GetByteArrayElements(env, blobs, NULL); bool out; - ret = verify_aggregate_kzg_proof(&out, (uint8_t const(*)[BYTES_PER_BLOB])blobs_native, c, count_native, &f, settings); + ret = verify_aggregate_kzg_proof(&out, (const Blob *)blobs_native, c, count_native, &f, settings); (*env)->ReleaseByteArrayElements(env, blobs, blobs_native, JNI_ABORT); free(c); @@ -256,7 +256,7 @@ JNIEXPORT jbyteArray JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_blobToKzgCommitm KZGCommitment c; C_KZG_RET ret; - ret = blob_to_kzg_commitment(&c, (uint8_t *)blob_native, settings); + ret = blob_to_kzg_commitment(&c, (const Blob *)blob_native, settings); (*env)->ReleaseByteArrayElements(env, blob, blob_native, JNI_ABORT); diff --git a/bindings/node.js/kzg.cxx b/bindings/node.js/kzg.cxx index cf79dfe..84f0578 100644 --- a/bindings/node.js/kzg.cxx +++ b/bindings/node.js/kzg.cxx @@ -134,7 +134,7 @@ Napi::Value BlobToKzgCommitment(const Napi::CallbackInfo& info) { return throw_invalid_arguments_count(expected_argument_count, argument_count, env); } - auto blob = extract_byte_array_from_param(info, 0, "blob"); + Blob *blob = (Blob *)extract_byte_array_from_param(info, 0, "blob"); if (env.IsExceptionPending()) { return env.Null(); } @@ -174,7 +174,7 @@ Napi::Value ComputeAggregateKzgProof(const Napi::CallbackInfo& info) { for (uint32_t blob_index = 0; blob_index < blobs_count; blob_index++) { Napi::Value blob = blobs_param[blob_index]; auto blob_bytes = blob.As().Data(); - memcpy(blobs[blob_index], blob_bytes, BYTES_PER_BLOB); + memcpy(blobs[blob_index].bytes, blob_bytes, BYTES_PER_BLOB); } KZGProof proof; @@ -225,7 +225,7 @@ Napi::Value VerifyAggregateKzgProof(const Napi::CallbackInfo& info) { Napi::Value blob = blobs_param[blob_index]; auto blob_bytes = blob.As().Data(); - memcpy(blobs[blob_index], blob_bytes, BYTES_PER_BLOB); + memcpy(blobs[blob_index].bytes, blob_bytes, BYTES_PER_BLOB); // Extract a G1 point for each commitment Napi::Value commitment = commitments_param[blob_index]; diff --git a/bindings/python/ckzg.c b/bindings/python/ckzg.c index 70e7f21..ca65d71 100644 --- a/bindings/python/ckzg.c +++ b/bindings/python/ckzg.c @@ -42,7 +42,7 @@ static PyObject* blob_to_kzg_commitment_wrap(PyObject *self, PyObject *args) { if (PyBytes_Size(b) != 32 * FIELD_ELEMENTS_PER_BLOB) return PyErr_Format(PyExc_ValueError, "expected 32 * FIELD_ELEMENTS_PER_BLOB bytes"); - uint8_t* blob = (uint8_t*)PyBytes_AsString(b); + Blob *blob = (Blob*)PyBytes_AsString(b); KZGCommitment *k = (KZGCommitment*)malloc(sizeof(KZGCommitment)); diff --git a/src/c_kzg_4844.c b/src/c_kzg_4844.c index 83f2fe0..b67a7af 100644 --- a/src/c_kzg_4844.c +++ b/src/c_kzg_4844.c @@ -996,16 +996,16 @@ static C_KZG_RET poly_to_kzg_commitment(KZGCommitment *out, const Polynomial *p, return g1_lincomb(out, s->g1_values, (const fr_t *)(&p->evals), FIELD_ELEMENTS_PER_BLOB); } -static C_KZG_RET poly_from_blob(Polynomial *p, const Blob blob) { +static C_KZG_RET poly_from_blob(Polynomial *p, const Blob *blob) { C_KZG_RET ret; for (size_t i = 0; i < FIELD_ELEMENTS_PER_BLOB; i++) { - ret = bytes_to_bls_field(&p->evals[i], &blob[i * BYTES_PER_FIELD_ELEMENT]); + ret = bytes_to_bls_field(&p->evals[i], &blob->bytes[i * BYTES_PER_FIELD_ELEMENT]); if (ret != C_KZG_OK) return ret; } return C_KZG_OK; } -C_KZG_RET blob_to_kzg_commitment(KZGCommitment *out, const Blob blob, const KZGSettings *s) { +C_KZG_RET blob_to_kzg_commitment(KZGCommitment *out, const Blob *blob, const KZGSettings *s) { Polynomial p; C_KZG_RET ret = poly_from_blob(&p, blob); if (ret != C_KZG_OK) return ret; @@ -1249,7 +1249,7 @@ static C_KZG_RET compute_challenges(BLSFieldElement *out, BLSFieldElement r_powe static C_KZG_RET compute_aggregated_poly_and_commitment(Polynomial *poly_out, KZGCommitment *comm_out, BLSFieldElement *chal_out, const Polynomial *polys, - const KZGCommitment kzg_commitments[], + const KZGCommitment *kzg_commitments, size_t n) { BLSFieldElement* r_powers = calloc(n, sizeof(BLSFieldElement)); if (0 < n && r_powers == NULL) return C_KZG_MALLOC; @@ -1268,7 +1268,7 @@ out: } C_KZG_RET compute_aggregate_kzg_proof(KZGProof *out, - const Blob blobs[], + const Blob *blobs, size_t n, const KZGSettings *s) { C_KZG_RET ret; @@ -1288,7 +1288,7 @@ C_KZG_RET compute_aggregate_kzg_proof(KZGProof *out, } for (size_t i = 0; i < n; i++) { - ret = poly_from_blob(&polys[i], blobs[i]); + ret = poly_from_blob(&polys[i], &blobs[i]); if (ret != C_KZG_OK) goto out; ret = poly_to_kzg_commitment(&commitments[i], &polys[i], s); if (ret != C_KZG_OK) goto out; @@ -1309,8 +1309,8 @@ out: } C_KZG_RET verify_aggregate_kzg_proof(bool *out, - const Blob blobs[], - const KZGCommitment expected_kzg_commitments[], + const Blob *blobs, + const KZGCommitment *expected_kzg_commitments, size_t n, const KZGProof *kzg_aggregated_proof, const KZGSettings *s) { @@ -1318,7 +1318,7 @@ C_KZG_RET verify_aggregate_kzg_proof(bool *out, Polynomial* polys = calloc(n, sizeof(Polynomial)); if (polys == NULL) return C_KZG_MALLOC; for (size_t i = 0; i < n; i++) { - ret = poly_from_blob(&polys[i], blobs[i]); + ret = poly_from_blob(&polys[i], &blobs[i]); if (ret != C_KZG_OK) goto out; } diff --git a/src/c_kzg_4844.h b/src/c_kzg_4844.h index baeb9b4..ca324e5 100644 --- a/src/c_kzg_4844.h +++ b/src/c_kzg_4844.h @@ -47,7 +47,7 @@ typedef blst_fr fr_t; /**< Internal Fr field element type */ typedef g1_t KZGCommitment; typedef g1_t KZGProof; typedef fr_t BLSFieldElement; -typedef uint8_t Blob[BYTES_PER_BLOB]; +typedef struct { uint8_t bytes[BYTES_PER_BLOB]; } Blob; /** * The common return type for all routines in which something can go wrong. @@ -100,19 +100,19 @@ void free_trusted_setup( KZGSettings *s); C_KZG_RET compute_aggregate_kzg_proof(KZGProof *out, - const Blob blobs[], + const Blob *blobs, size_t n, const KZGSettings *s); C_KZG_RET verify_aggregate_kzg_proof(bool *out, - const Blob blobs[], - const KZGCommitment expected_kzg_commitments[], + const Blob *blobs, + const KZGCommitment *expected_kzg_commitments, size_t n, const KZGProof *kzg_aggregated_proof, const KZGSettings *s); C_KZG_RET blob_to_kzg_commitment(KZGCommitment *out, - const Blob blob, + const Blob *blob, const KZGSettings *s); C_KZG_RET verify_kzg_proof(bool *out,