modify cell indices validation

This commit is contained in:
healthykim 2026-04-23 14:26:16 +02:00
parent 22bcf90bf0
commit b950503b89

View file

@ -20,7 +20,6 @@ package kzg4844
import ( import (
"embed" "embed"
"errors" "errors"
"fmt"
"hash" "hash"
"reflect" "reflect"
"sync/atomic" "sync/atomic"
@ -214,6 +213,8 @@ func IsValidVersionedHash(h []byte) bool {
// For this function, it is sufficient to only provide some of the cells. // For this function, it is sufficient to only provide some of the cells.
// //
// The `cellIndices` specify which of the 128 cells of each blob are given. // The `cellIndices` specify which of the 128 cells of each blob are given.
// Indices must be given in ascending order.
//
// Note the list of indices is shared among all blobs, i.e. for a given list of indices // Note the list of indices is shared among all blobs, i.e. for a given list of indices
// [1, 2, 13], the cells slice must contain cells [1, 2, 13] of each blob. // [1, 2, 13], the cells slice must contain cells [1, 2, 13] of each blob.
// Thus, `len(cells)` must be a multiple of `len(cellIndices)`. // Thus, `len(cells)` must be a multiple of `len(cellIndices)`.
@ -232,6 +233,9 @@ func VerifyCells(cells []Cell, commitments []Commitment, proofs []Proof, cellInd
if err := validateCellIndices(cells, cellIndices); err != nil { if err := validateCellIndices(cells, cellIndices); err != nil {
return err return err
} }
if len(cells)/len(cellIndices) != len(commitments) {
return errors.New("invalid number of cells for blob count")
}
if useCKZG.Load() { if useCKZG.Load() {
return ckzgVerifyCells(cells, commitments, proofs, cellIndices) return ckzgVerifyCells(cells, commitments, proofs, cellIndices)
@ -265,23 +269,13 @@ func validateCellIndices(cells []Cell, cellIndices []uint64) error {
switch { switch {
case len(cellIndices) == 0: case len(cellIndices) == 0:
return errors.New("no cellIndices given") return errors.New("no cellIndices given")
case len(cellIndices) < len(cells): case len(cellIndices) > len(cells):
return errors.New("less cellIndices than cells") return errors.New("less cells than cellIndices")
case len(cellIndices) > CellsPerBlob: case len(cellIndices) > CellsPerBlob:
return errors.New("too many cellIndices") return errors.New("too many cellIndices")
case len(cells)%len(cellIndices) != 0: case len(cells)%len(cellIndices) != 0:
return errors.New("len(cells) must be a multiple of len(cellIndices)") return errors.New("len(cells) must be a multiple of len(cellIndices)")
} }
// check no duplicates // The library checks the canonical ordering of indices, so we don't have to do it here.
var bm [CellsPerBlob / 8]uint64
for _, i := range cellIndices {
if i >= CellsPerBlob {
return fmt.Errorf("invalid cell index %d", i)
}
if bm[i>>8]&(1<<(i%8)) != 0 {
return fmt.Errorf("duplicate cell index %d", i)
}
bm[i>>8] |= 1 << (i % 8)
}
return nil return nil
} }