Skip to content
Open
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
20 changes: 18 additions & 2 deletions src/eip4844/eip4844.c
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,8 @@ static C_KZG_RET compute_r_powers_for_verify_kzg_proof_batch(
) {
C_KZG_RET ret;
uint8_t *bytes = NULL;
blst_p1_affine *commitments_affine = NULL;
blst_p1_affine *proofs_affine = NULL;
Bytes32 r_bytes;
fr_t r;

Expand All @@ -613,6 +615,18 @@ static C_KZG_RET compute_r_powers_for_verify_kzg_proof_batch(
ret = c_kzg_malloc((void **)&bytes, input_size);
if (ret != C_KZG_OK) goto out;

/* Allocate space for affine commitments and proofs */
ret = c_kzg_malloc((void **)&commitments_affine, n * sizeof(blst_p1_affine));
if (ret != C_KZG_OK) goto out;
ret = c_kzg_malloc((void **)&proofs_affine, n * sizeof(blst_p1_affine));
if (ret != C_KZG_OK) goto out;

/* Batch convert commitments and proofs to affine */
const blst_p1 *commitments_arg[2] = {commitments_g1, NULL};
blst_p1s_to_affine(commitments_affine, commitments_arg, n);
const blst_p1 *proofs_arg[2] = {proofs_g1, NULL};
blst_p1s_to_affine(proofs_affine, proofs_arg, n);

/* Pointer tracking `bytes` for writing on top of it */
uint8_t *offset = bytes;

Expand All @@ -633,7 +647,7 @@ static C_KZG_RET compute_r_powers_for_verify_kzg_proof_batch(

for (size_t i = 0; i < n; i++) {
/* Copy commitment */
bytes_from_g1((Bytes48 *)offset, &commitments_g1[i]);
blst_p1_affine_compress(offset, &commitments_affine[i]);
offset += BYTES_PER_COMMITMENT;

/* Copy z */
Expand All @@ -645,7 +659,7 @@ static C_KZG_RET compute_r_powers_for_verify_kzg_proof_batch(
offset += BYTES_PER_FIELD_ELEMENT;

/* Copy proof */
bytes_from_g1((Bytes48 *)offset, &proofs_g1[i]);
blst_p1_affine_compress(offset, &proofs_affine[i]);
offset += BYTES_PER_PROOF;
}

Expand All @@ -660,6 +674,8 @@ static C_KZG_RET compute_r_powers_for_verify_kzg_proof_batch(

out:
c_kzg_free(bytes);
c_kzg_free(commitments_affine);
c_kzg_free(proofs_affine);
return ret;
}

Expand Down
30 changes: 26 additions & 4 deletions src/eip7594/eip7594.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ C_KZG_RET compute_cells_and_kzg_proofs(
fr_t *poly_lagrange = NULL;
fr_t *data_fr = NULL;
g1_t *proofs_g1 = NULL;
blst_p1_affine *proofs_affine = NULL;

/* If both of these are null, something is wrong */
if (cells == NULL && proofs == NULL) {
Expand Down Expand Up @@ -131,9 +132,17 @@ C_KZG_RET compute_cells_and_kzg_proofs(
ret = bit_reversal_permutation(proofs_g1, sizeof(g1_t), CELLS_PER_EXT_BLOB);
if (ret != C_KZG_OK) goto out;

/* Convert all of the proofs to byte-form */
/* Allocate space for affine proofs */
ret = c_kzg_malloc((void **)&proofs_affine, CELLS_PER_EXT_BLOB * sizeof(blst_p1_affine));
if (ret != C_KZG_OK) goto out;

/* Batch convert proofs to affine */
const blst_p1 *proofs_arg[2] = {proofs_g1, NULL};
blst_p1s_to_affine(proofs_affine, proofs_arg, CELLS_PER_EXT_BLOB);

/* Compress all of the proofs to byte-form */
for (size_t i = 0; i < CELLS_PER_EXT_BLOB; i++) {
bytes_from_g1(&proofs[i], &proofs_g1[i]);
blst_p1_affine_compress(proofs[i].bytes, &proofs_affine[i]);
}
}

Expand All @@ -142,6 +151,7 @@ C_KZG_RET compute_cells_and_kzg_proofs(
c_kzg_free(poly_lagrange);
c_kzg_free(data_fr);
c_kzg_free(proofs_g1);
c_kzg_free(proofs_affine);
return ret;
}

Expand Down Expand Up @@ -174,6 +184,7 @@ C_KZG_RET recover_cells_and_kzg_proofs(
C_KZG_RET ret;
fr_t *recovered_cells_fr = NULL;
g1_t *recovered_proofs_g1 = NULL;
blst_p1_affine *recovered_proofs_affine = NULL;

/* Ensure only one blob's worth of cells was provided */
if (num_cells > CELLS_PER_EXT_BLOB) {
Expand Down Expand Up @@ -268,15 +279,26 @@ C_KZG_RET recover_cells_and_kzg_proofs(
ret = bit_reversal_permutation(recovered_proofs_g1, sizeof(g1_t), CELLS_PER_EXT_BLOB);
if (ret != C_KZG_OK) goto out;

/* Convert all of the proofs to byte-form */
/* Allocate space for affine proofs */
ret = c_kzg_malloc(
(void **)&recovered_proofs_affine, CELLS_PER_EXT_BLOB * sizeof(blst_p1_affine)
);
if (ret != C_KZG_OK) goto out;

/* Batch convert proofs to affine */
const blst_p1 *recovered_proofs_arg[2] = {recovered_proofs_g1, NULL};
blst_p1s_to_affine(recovered_proofs_affine, recovered_proofs_arg, CELLS_PER_EXT_BLOB);

/* Compress all of the proofs to byte-form */
for (size_t i = 0; i < CELLS_PER_EXT_BLOB; i++) {
bytes_from_g1(&recovered_proofs[i], &recovered_proofs_g1[i]);
blst_p1_affine_compress(recovered_proofs[i].bytes, &recovered_proofs_affine[i]);
}
}

out:
c_kzg_free(recovered_cells_fr);
c_kzg_free(recovered_proofs_g1);
c_kzg_free(recovered_proofs_affine);
return ret;
}

Expand Down
Loading