Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions src/eip7594/eip7594.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,17 @@ C_KZG_RET recover_cells_and_kzg_proofs(
goto out;
}

/* Check that cell indices are valid */
for (size_t i = 0; i < num_cells; i++) {
/* Check that cell indices are valid */
if (cell_indices[i] >= CELLS_PER_EXT_BLOB) {
ret = C_KZG_BADARGS;
goto out;
}
/* Check that indices are in strictly ascending order */
if (i > 0 && cell_indices[i] <= cell_indices[i - 1]) {
ret = C_KZG_BADARGS;
goto out;
}
}

/* Do allocations */
Expand All @@ -210,19 +215,8 @@ C_KZG_RET recover_cells_and_kzg_proofs(
for (size_t i = 0; i < num_cells; i++) {
size_t index = cell_indices[i] * FIELD_ELEMENTS_PER_CELL;
for (size_t j = 0; j < FIELD_ELEMENTS_PER_CELL; j++) {
fr_t *ptr = &recovered_cells_fr[index + j];

/*
* Check if the field has already been set. If it has, there was a duplicate cell index
* and we can return an error. The compiler will optimize this and the overhead is
* practically zero.
*/
if (!fr_is_null(ptr)) {
ret = C_KZG_BADARGS;
goto out;
}

/* Convert the untrusted input bytes to a field element */
fr_t *ptr = &recovered_cells_fr[index + j];
size_t offset = j * BYTES_PER_FIELD_ELEMENT;
ret = bytes_to_bls_field(ptr, (const Bytes32 *)&cells[i].bytes[offset]);
if (ret != C_KZG_OK) goto out;
Expand All @@ -231,7 +225,10 @@ C_KZG_RET recover_cells_and_kzg_proofs(

if (num_cells == CELLS_PER_EXT_BLOB) {
/* Nothing to recover, copy the cells */
memcpy(recovered_cells, cells, CELLS_PER_EXT_BLOB * sizeof(Cell));
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

}
} else {
/* Perform cell recovery */
ret = recover_cells(recovered_cells_fr, cell_indices, num_cells, recovered_cells_fr, s);
Expand Down
Loading
Loading