Skip to content

Ensure cell indices are sorted in recover_cells_and_kzg_proofs#594

Merged
asn-d6 merged 6 commits intoethereum:mainfrom
jtraglia:kzg-recovery-shuffled
Sep 3, 2025
Merged

Ensure cell indices are sorted in recover_cells_and_kzg_proofs#594
asn-d6 merged 6 commits intoethereum:mainfrom
jtraglia:kzg-recovery-shuffled

Conversation

@jtraglia
Copy link
Member

@jtraglia jtraglia commented Aug 22, 2025

This PR will reject inputs to the recovery function if the cell indices are not in ascending order. With this check, we remove the fr_is_null duplicate checks below. I've kept the memcpy change because I prefer how the new version reads.

See also the specifications update related to this:

Thanks to @asanso for pointing this out.

This would work with partial inputs but not when all cells were provided.
@jtraglia jtraglia changed the title Fix recover_cells_and_kzg_proofs to support shuffled indices Ensure cell indices are sorted in recover_cells_and_kzg_proofs Aug 26, 2025
Comment on lines 228 to 230
for (size_t i = 0; i < CELLS_PER_EXT_BLOB; i++) {
uint64_t index = cell_indices[i];
recovered_cells[index] = cells[i];
Copy link
Contributor

@asn-d6 asn-d6 Aug 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see why this is a bit nicer than the blind memcpy, but IMO it's also a bit confusing.

It's confusing because at this point we are past all the initial checks and we also have num_cells == CELLS_PER_EXT_BLOB, and hence we MUST be certain that cell_indices = [0, 1, ..., CELLS_PER_EXT_BLOB-1].

The problem is that this new code introduces index and sorta implies it's a free ranging index, whereas in reality it should be very very constrainted.

For me, the following would be more descriptive:

Suggested change
for (size_t i = 0; i < CELLS_PER_EXT_BLOB; i++) {
uint64_t index = cell_indices[i];
recovered_cells[index] = cells[i];
for (size_t i = 0; i < CELLS_PER_EXT_BLOB; i++) {
/* At this point we know that all cells are in the right order */
assert(cell_indices[i] == i);
recovered_cells[i] = cells[i];

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried this after our discussion. It does not use an assert but it also does not introduce a new index: 65ebfdd

Copy link
Contributor

@asn-d6 asn-d6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks!

@asn-d6 asn-d6 merged commit b542e6a into ethereum:main Sep 3, 2025
42 of 43 checks passed
@jtraglia jtraglia deleted the kzg-recovery-shuffled branch September 3, 2025 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants