From 834fa14c6841106ed6f8c55794000d8d438697a6 Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Fri, 16 Aug 2024 11:18:55 -0500 Subject: [PATCH 1/4] Refactor is_cell_used array & usage --- src/eip7594/eip7594.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/eip7594/eip7594.c b/src/eip7594/eip7594.c index 2823826f..c47fa3cf 100644 --- a/src/eip7594/eip7594.c +++ b/src/eip7594/eip7594.c @@ -484,7 +484,7 @@ static C_KZG_RET compute_commitment_to_aggregated_interpolation_poly( // Array allocations //////////////////////////////////////////////////////////////////////////////////////////////// - ret = new_bool_array(&is_cell_used, FIELD_ELEMENTS_PER_EXT_BLOB); + ret = new_bool_array(&is_cell_used, CELLS_PER_EXT_BLOB); if (ret != C_KZG_OK) goto out; ret = new_fr_array(&aggregated_column_cells, FIELD_ELEMENTS_PER_EXT_BLOB); if (ret != C_KZG_OK) goto out; @@ -493,6 +493,20 @@ static C_KZG_RET compute_commitment_to_aggregated_interpolation_poly( ret = new_fr_array(&aggregated_interpolation_poly, FIELD_ELEMENTS_PER_CELL); if (ret != C_KZG_OK) goto out; + //////////////////////////////////////////////////////////////////////////////////////////////// + // Determine which cells are used + //////////////////////////////////////////////////////////////////////////////////////////////// + + /* Start with false values */ + for (size_t i = 0; i < CELLS_PER_EXT_BLOB; i++) { + is_cell_used[i] = false; + } + + /* Mark each cell index as used */ + for (uint64_t i = 0; i < num_cells; i++) { + is_cell_used[cell_indices[i]] = true; + } + //////////////////////////////////////////////////////////////////////////////////////////////// // Aggregates cells from the same column //////////////////////////////////////////////////////////////////////////////////////////////// @@ -502,7 +516,6 @@ static C_KZG_RET compute_commitment_to_aggregated_interpolation_poly( for (size_t j = 0; j < FIELD_ELEMENTS_PER_CELL; j++) { size_t index = i * FIELD_ELEMENTS_PER_CELL + j; aggregated_column_cells[index] = FR_ZERO; - is_cell_used[index] = false; } } @@ -524,9 +537,6 @@ static C_KZG_RET compute_commitment_to_aggregated_interpolation_poly( blst_fr_add( &aggregated_column_cells[index], &aggregated_column_cells[index], &scaled_fr ); - - /* Mark the cell as being used */ - is_cell_used[index] = true; } } @@ -541,12 +551,12 @@ static C_KZG_RET compute_commitment_to_aggregated_interpolation_poly( /* Interpolate each column */ for (size_t i = 0; i < CELLS_PER_EXT_BLOB; i++) { + /* We only care about initialized cells */ + if (!is_cell_used[i]) continue; + /* Offset to the first cell for this column */ size_t index = i * FIELD_ELEMENTS_PER_CELL; - /* We only care about initialized cells */ - if (!is_cell_used[index]) continue; - /* We don't need to copy this because it's not used again */ ret = bit_reversal_permutation( &aggregated_column_cells[index], sizeof(fr_t), FIELD_ELEMENTS_PER_CELL From 049e9a96eb6a24578c38a597ba486bb4077ba89c Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Fri, 16 Aug 2024 11:24:34 -0500 Subject: [PATCH 2/4] Move new section down a bit --- src/eip7594/eip7594.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/eip7594/eip7594.c b/src/eip7594/eip7594.c index c47fa3cf..79de23ac 100644 --- a/src/eip7594/eip7594.c +++ b/src/eip7594/eip7594.c @@ -493,20 +493,6 @@ static C_KZG_RET compute_commitment_to_aggregated_interpolation_poly( ret = new_fr_array(&aggregated_interpolation_poly, FIELD_ELEMENTS_PER_CELL); if (ret != C_KZG_OK) goto out; - //////////////////////////////////////////////////////////////////////////////////////////////// - // Determine which cells are used - //////////////////////////////////////////////////////////////////////////////////////////////// - - /* Start with false values */ - for (size_t i = 0; i < CELLS_PER_EXT_BLOB; i++) { - is_cell_used[i] = false; - } - - /* Mark each cell index as used */ - for (uint64_t i = 0; i < num_cells; i++) { - is_cell_used[cell_indices[i]] = true; - } - //////////////////////////////////////////////////////////////////////////////////////////////// // Aggregates cells from the same column //////////////////////////////////////////////////////////////////////////////////////////////// @@ -540,6 +526,20 @@ static C_KZG_RET compute_commitment_to_aggregated_interpolation_poly( } } + //////////////////////////////////////////////////////////////////////////////////////////////// + // Determine which cells are used + //////////////////////////////////////////////////////////////////////////////////////////////// + + /* Start with false values */ + for (size_t i = 0; i < CELLS_PER_EXT_BLOB; i++) { + is_cell_used[i] = false; + } + + /* Mark each cell index as used */ + for (uint64_t i = 0; i < num_cells; i++) { + is_cell_used[cell_indices[i]] = true; + } + //////////////////////////////////////////////////////////////////////////////////////////////// // Compute interpolation polynomials using the aggregated cells //////////////////////////////////////////////////////////////////////////////////////////////// From 111dff24ea516080cdb2edfd677ce46a878bf10d Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Fri, 16 Aug 2024 11:28:17 -0500 Subject: [PATCH 3/4] Improve comment --- src/eip7594/eip7594.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/eip7594/eip7594.c b/src/eip7594/eip7594.c index 79de23ac..8aaffed2 100644 --- a/src/eip7594/eip7594.c +++ b/src/eip7594/eip7594.c @@ -551,7 +551,7 @@ static C_KZG_RET compute_commitment_to_aggregated_interpolation_poly( /* Interpolate each column */ for (size_t i = 0; i < CELLS_PER_EXT_BLOB; i++) { - /* We only care about initialized cells */ + /* We can skip columns without any cells */ if (!is_cell_used[i]) continue; /* Offset to the first cell for this column */ From c9cb7b7f00e9316d0f095e95b97da9709ec0a829 Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Fri, 16 Aug 2024 11:57:29 -0500 Subject: [PATCH 4/4] Fix one more unrelated nit --- src/eip7594/eip7594.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/eip7594/eip7594.c b/src/eip7594/eip7594.c index 8aaffed2..c6c39a52 100644 --- a/src/eip7594/eip7594.c +++ b/src/eip7594/eip7594.c @@ -670,7 +670,7 @@ C_KZG_RET verify_cell_kzg_proof_batch( } //////////////////////////////////////////////////////////////////////////////////////////////// - // Deduplicate Commitments + // Deduplicate commitments //////////////////////////////////////////////////////////////////////////////////////////////// ret = c_kzg_calloc((void **)&unique_commitments, num_cells, sizeof(Bytes48));