@@ -551,7 +551,7 @@ static C_KZG_RET compute_commitment_to_aggregated_interpolation_poly(
551
551
if (ret != C_KZG_OK ) goto out ;
552
552
553
553
////////////////////////////////////////////////////////////////////////////////////////////////
554
- // Aggregates cells from the same column
554
+ // Aggregate cells from the same column
555
555
////////////////////////////////////////////////////////////////////////////////////////////////
556
556
557
557
/* Start with zeroed out columns */
@@ -562,23 +562,37 @@ static C_KZG_RET compute_commitment_to_aggregated_interpolation_poly(
562
562
}
563
563
}
564
564
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 ++ ) {
568
577
fr_t original_fr , scaled_fr ;
569
578
570
579
/* 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
+ );
573
584
if (ret != C_KZG_OK ) goto out ;
574
585
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 ]);
577
588
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 */
580
592
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
582
596
);
583
597
}
584
598
}
@@ -614,26 +628,26 @@ static C_KZG_RET compute_commitment_to_aggregated_interpolation_poly(
614
628
/* Offset to the first cell for this column */
615
629
size_t index = i * FIELD_ELEMENTS_PER_CELL ;
616
630
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
+ */
618
635
ret = bit_reversal_permutation (
619
636
& aggregated_column_cells [index ], sizeof (fr_t ), FIELD_ELEMENTS_PER_CELL
620
637
);
621
638
if (ret != C_KZG_OK ) goto out ;
622
639
623
640
/*
624
641
* 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.
627
644
*/
628
645
ret = fr_ifft (
629
646
column_interpolation_poly , & aggregated_column_cells [index ], FIELD_ELEMENTS_PER_CELL , s
630
647
);
631
648
if (ret != C_KZG_OK ) goto out ;
632
649
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 */
637
651
uint64_t pos = reverse_bits_limited (CELLS_PER_EXT_BLOB , i );
638
652
fr_t inv_coset_factor ;
639
653
blst_fr_eucl_inverse (& inv_coset_factor , & s -> roots_of_unity [pos ]);
0 commit comments