Skip to content

Commit 2b38295

Browse files
authored
Merge pull request ethereum#500 from asn-d6/verify_doc_columns
Document `compute_commitment_to_aggregated_interpolation_poly()`
2 parents 7126419 + 9f6d28f commit 2b38295

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

src/eip7594/eip7594.c

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ static C_KZG_RET compute_commitment_to_aggregated_interpolation_poly(
551551
if (ret != C_KZG_OK) goto out;
552552

553553
////////////////////////////////////////////////////////////////////////////////////////////////
554-
// Aggregates cells from the same column
554+
// Aggregate cells from the same column
555555
////////////////////////////////////////////////////////////////////////////////////////////////
556556

557557
/* Start with zeroed out columns */
@@ -562,23 +562,37 @@ static C_KZG_RET compute_commitment_to_aggregated_interpolation_poly(
562562
}
563563
}
564564

565-
/* Scale each cell with the corresponding powers of the random challenge */
566-
for (uint64_t i = 0; i < num_cells; i++) {
567-
for (size_t j = 0; j < FIELD_ELEMENTS_PER_CELL; j++) {
565+
/*
566+
* Vertically collapse cells of the 2D matrix into a single array: `aggregated_column_cells`.
567+
*
568+
* For each provided cell, go over its field elements, and scale them by the appropriate power
569+
* of r. Then aggregate all field elements on the same vertical slice into a single array.
570+
*/
571+
for (uint64_t cell_index = 0; cell_index < num_cells; cell_index++) {
572+
/* Determine which column this cell belongs to */
573+
uint64_t column_index = cell_indices[cell_index];
574+
575+
/* Iterate over every field element of this cell: scale it and aggregate it */
576+
for (size_t fr_index = 0; fr_index < FIELD_ELEMENTS_PER_CELL; fr_index++) {
568577
fr_t original_fr, scaled_fr;
569578

570579
/* Get the field element at this offset */
571-
size_t offset = j * BYTES_PER_FIELD_ELEMENT;
572-
ret = bytes_to_bls_field(&original_fr, (const Bytes32 *)&cells[i].bytes[offset]);
580+
size_t offset = fr_index * BYTES_PER_FIELD_ELEMENT;
581+
ret = bytes_to_bls_field(
582+
&original_fr, (const Bytes32 *)&cells[cell_index].bytes[offset]
583+
);
573584
if (ret != C_KZG_OK) goto out;
574585

575-
/* Scale the field element by the power for that cell */
576-
blst_fr_mul(&scaled_fr, &original_fr, &r_powers[i]);
586+
/* Scale the field element by the appropriate power of r */
587+
blst_fr_mul(&scaled_fr, &original_fr, &r_powers[cell_index]);
577588

578-
/* Aggregate the scaled field element into the column */
579-
size_t index = cell_indices[i] * FIELD_ELEMENTS_PER_CELL + j;
589+
/* Figure out the right index for this field element within the extended array */
590+
size_t array_index = column_index * FIELD_ELEMENTS_PER_CELL + fr_index;
591+
/* Aggregate the scaled field element into the array */
580592
blst_fr_add(
581-
&aggregated_column_cells[index], &aggregated_column_cells[index], &scaled_fr
593+
&aggregated_column_cells[array_index],
594+
&aggregated_column_cells[array_index],
595+
&scaled_fr
582596
);
583597
}
584598
}
@@ -614,26 +628,26 @@ static C_KZG_RET compute_commitment_to_aggregated_interpolation_poly(
614628
/* Offset to the first cell for this column */
615629
size_t index = i * FIELD_ELEMENTS_PER_CELL;
616630

617-
/* We don't need to copy this because it's not used again */
631+
/*
632+
* Reach into the big array and permute the right column.
633+
* No need to copy the data, we are not gonna use them again.
634+
*/
618635
ret = bit_reversal_permutation(
619636
&aggregated_column_cells[index], sizeof(fr_t), FIELD_ELEMENTS_PER_CELL
620637
);
621638
if (ret != C_KZG_OK) goto out;
622639

623640
/*
624641
* Get interpolation polynomial for this column. To do so we first do an IDFT over the roots
625-
* of unity and then we scale by the coset factor. We can't do an IDFT directly over the
626-
* coset because it's not a subgroup.
642+
* of unity and then we scale the coefficients by the coset factor. We can't do an IDFT
643+
* directly over the coset because it's not a subgroup.
627644
*/
628645
ret = fr_ifft(
629646
column_interpolation_poly, &aggregated_column_cells[index], FIELD_ELEMENTS_PER_CELL, s
630647
);
631648
if (ret != C_KZG_OK) goto out;
632649

633-
/*
634-
* To unscale, divide by the coset. It's faster to multiply with the inverse. We can skip
635-
* the first iteration because its dividing by one.
636-
*/
650+
/* Now divide by the coset shift factor */
637651
uint64_t pos = reverse_bits_limited(CELLS_PER_EXT_BLOB, i);
638652
fr_t inv_coset_factor;
639653
blst_fr_eucl_inverse(&inv_coset_factor, &s->roots_of_unity[pos]);

0 commit comments

Comments
 (0)