Skip to content

Commit 4fb9ec5

Browse files
authored
Batch convert commitments/proofs to bytes (#626)
1 parent 5b90e0b commit 4fb9ec5

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

src/eip4844/eip4844.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,8 @@ static C_KZG_RET compute_r_powers_for_verify_kzg_proof_batch(
604604
) {
605605
C_KZG_RET ret;
606606
uint8_t *bytes = NULL;
607+
blst_p1_affine *commitments_affine = NULL;
608+
blst_p1_affine *proofs_affine = NULL;
607609
Bytes32 r_bytes;
608610
fr_t r;
609611

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

618+
/* Allocate space for affine commitments and proofs */
619+
ret = c_kzg_malloc((void **)&commitments_affine, n * sizeof(blst_p1_affine));
620+
if (ret != C_KZG_OK) goto out;
621+
ret = c_kzg_malloc((void **)&proofs_affine, n * sizeof(blst_p1_affine));
622+
if (ret != C_KZG_OK) goto out;
623+
624+
/* Batch convert commitments and proofs to affine */
625+
const blst_p1 *commitments_arg[2] = {commitments_g1, NULL};
626+
blst_p1s_to_affine(commitments_affine, commitments_arg, n);
627+
const blst_p1 *proofs_arg[2] = {proofs_g1, NULL};
628+
blst_p1s_to_affine(proofs_affine, proofs_arg, n);
629+
616630
/* Pointer tracking `bytes` for writing on top of it */
617631
uint8_t *offset = bytes;
618632

@@ -633,7 +647,7 @@ static C_KZG_RET compute_r_powers_for_verify_kzg_proof_batch(
633647

634648
for (size_t i = 0; i < n; i++) {
635649
/* Copy commitment */
636-
bytes_from_g1((Bytes48 *)offset, &commitments_g1[i]);
650+
blst_p1_affine_compress(offset, &commitments_affine[i]);
637651
offset += BYTES_PER_COMMITMENT;
638652

639653
/* Copy z */
@@ -645,7 +659,7 @@ static C_KZG_RET compute_r_powers_for_verify_kzg_proof_batch(
645659
offset += BYTES_PER_FIELD_ELEMENT;
646660

647661
/* Copy proof */
648-
bytes_from_g1((Bytes48 *)offset, &proofs_g1[i]);
662+
blst_p1_affine_compress(offset, &proofs_affine[i]);
649663
offset += BYTES_PER_PROOF;
650664
}
651665

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

661675
out:
662676
c_kzg_free(bytes);
677+
c_kzg_free(commitments_affine);
678+
c_kzg_free(proofs_affine);
663679
return ret;
664680
}
665681

src/eip7594/eip7594.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ C_KZG_RET compute_cells_and_kzg_proofs(
6565
fr_t *poly_lagrange = NULL;
6666
fr_t *data_fr = NULL;
6767
g1_t *proofs_g1 = NULL;
68+
blst_p1_affine *proofs_affine = NULL;
6869

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

134-
/* Convert all of the proofs to byte-form */
135+
/* Allocate space for affine proofs */
136+
ret = c_kzg_malloc((void **)&proofs_affine, CELLS_PER_EXT_BLOB * sizeof(blst_p1_affine));
137+
if (ret != C_KZG_OK) goto out;
138+
139+
/* Batch convert proofs to affine */
140+
const blst_p1 *proofs_arg[2] = {proofs_g1, NULL};
141+
blst_p1s_to_affine(proofs_affine, proofs_arg, CELLS_PER_EXT_BLOB);
142+
143+
/* Compress all of the proofs to byte-form */
135144
for (size_t i = 0; i < CELLS_PER_EXT_BLOB; i++) {
136-
bytes_from_g1(&proofs[i], &proofs_g1[i]);
145+
blst_p1_affine_compress(proofs[i].bytes, &proofs_affine[i]);
137146
}
138147
}
139148

@@ -142,6 +151,7 @@ C_KZG_RET compute_cells_and_kzg_proofs(
142151
c_kzg_free(poly_lagrange);
143152
c_kzg_free(data_fr);
144153
c_kzg_free(proofs_g1);
154+
c_kzg_free(proofs_affine);
145155
return ret;
146156
}
147157

@@ -174,6 +184,7 @@ C_KZG_RET recover_cells_and_kzg_proofs(
174184
C_KZG_RET ret;
175185
fr_t *recovered_cells_fr = NULL;
176186
g1_t *recovered_proofs_g1 = NULL;
187+
blst_p1_affine *recovered_proofs_affine = NULL;
177188

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

271-
/* Convert all of the proofs to byte-form */
282+
/* Allocate space for affine proofs */
283+
ret = c_kzg_malloc(
284+
(void **)&recovered_proofs_affine, CELLS_PER_EXT_BLOB * sizeof(blst_p1_affine)
285+
);
286+
if (ret != C_KZG_OK) goto out;
287+
288+
/* Batch convert proofs to affine */
289+
const blst_p1 *recovered_proofs_arg[2] = {recovered_proofs_g1, NULL};
290+
blst_p1s_to_affine(recovered_proofs_affine, recovered_proofs_arg, CELLS_PER_EXT_BLOB);
291+
292+
/* Compress all of the proofs to byte-form */
272293
for (size_t i = 0; i < CELLS_PER_EXT_BLOB; i++) {
273-
bytes_from_g1(&recovered_proofs[i], &recovered_proofs_g1[i]);
294+
blst_p1_affine_compress(recovered_proofs[i].bytes, &recovered_proofs_affine[i]);
274295
}
275296
}
276297

277298
out:
278299
c_kzg_free(recovered_cells_fr);
279300
c_kzg_free(recovered_proofs_g1);
301+
c_kzg_free(recovered_proofs_affine);
280302
return ret;
281303
}
282304

0 commit comments

Comments
 (0)