fix: fix errors in cell level kzg functions

This commit is contained in:
healthykim 2025-09-04 19:27:54 +09:00
parent 0ba527f46c
commit da8a204517
5 changed files with 107 additions and 55 deletions

View file

@ -206,7 +206,7 @@ func IsValidVersionedHash(h []byte) bool {
} }
// VerifyCellProof verifies a batch of proofs corresponding to the cells and commitments. // VerifyCellProof verifies a batch of proofs corresponding to the cells and commitments.
// Expects length of proofs, cells and cellIndices, flattened // Expects equal length of proofs and cells, flattened
func VerifyCells(cells []Cell, commitments []Commitment, proofs []Proof, cellIndices []uint64) error { func VerifyCells(cells []Cell, commitments []Commitment, proofs []Proof, cellIndices []uint64) error {
if useCKZG.Load() { if useCKZG.Load() {
return ckzgVerifyCells(cells, commitments, proofs, cellIndices) return ckzgVerifyCells(cells, commitments, proofs, cellIndices)
@ -221,10 +221,10 @@ func ComputeCells(blobs []Blob) ([]Cell, error) {
return gokzgComputeCells(blobs) return gokzgComputeCells(blobs)
} }
func RecoverBlob(cells []Cell, cellIndices []uint64) (Blob, error) { func RecoverBlobs(cells []Cell, cellIndices []uint64) ([]Blob, error) {
if useCKZG.Load() { if useCKZG.Load() {
return ckzgRecoverBlob(cells, cellIndices) return ckzgRecoverBlobs(cells, cellIndices)
} }
return gokzgRecoverBlob(cells, cellIndices) return gokzgRecoverBlobs(cells, cellIndices)
} }

View file

@ -150,7 +150,7 @@ func ckzgComputeCellProofs(blob *Blob) ([]Proof, error) {
return p, nil return p, nil
} }
// ckzgVerifyCellProofBatch verifies that the blob data corresponds to the provided commitment. // ckzgVerifyCellProofs verifies that the blob data corresponds to the provided commitment.
func ckzgVerifyCellProofBatch(blobs []Blob, commitments []Commitment, cellProofs []Proof) error { func ckzgVerifyCellProofBatch(blobs []Blob, commitments []Commitment, cellProofs []Proof) error {
ckzgIniter.Do(ckzgInit) ckzgIniter.Do(ckzgInit)
var ( var (
@ -196,6 +196,7 @@ func ckzgVerifyCells(cells []Cell, commitments []Commitment, cellProofs []Proof,
var ( var (
proofs = make([]ckzg4844.Bytes48, len(cellProofs)) proofs = make([]ckzg4844.Bytes48, len(cellProofs))
commits = make([]ckzg4844.Bytes48, 0, len(cellProofs)) commits = make([]ckzg4844.Bytes48, 0, len(cellProofs))
indices = make([]uint64, 0, len(cellProofs))
kzgcells = make([]ckzg4844.Cell, 0, len(cellProofs)) kzgcells = make([]ckzg4844.Cell, 0, len(cellProofs))
) )
// Copy over the cell proofs and cells // Copy over the cell proofs and cells
@ -213,6 +214,11 @@ func ckzgVerifyCells(cells []Cell, commitments []Commitment, cellProofs []Proof,
commits = append(commits, (ckzg4844.Bytes48)(commitment)) commits = append(commits, (ckzg4844.Bytes48)(commitment))
} }
} }
blobCounts := len(cellProofs) / len(cellIndices)
for j := 0; j < blobCounts; j++ {
indices = append(indices, cellIndices...)
}
valid, err := ckzg4844.VerifyCellKZGProofBatch(commits, cellIndices, kzgcells, proofs) valid, err := ckzg4844.VerifyCellKZGProofBatch(commits, cellIndices, kzgcells, proofs)
if err != nil { if err != nil {
return err return err
@ -230,7 +236,7 @@ func ckzgComputeCells(blobs []Blob) ([]Cell, error) {
for i := range blobs { for i := range blobs {
cellsI, err := ckzg4844.ComputeCells((*ckzg4844.Blob)(&blobs[i])) cellsI, err := ckzg4844.ComputeCells((*ckzg4844.Blob)(&blobs[i]))
if err != nil { if err != nil {
return nil, err return []Cell{}, err
} }
for _, c := range cellsI { for _, c := range cellsI {
cells = append(cells, Cell(c)) cells = append(cells, Cell(c))
@ -239,25 +245,37 @@ func ckzgComputeCells(blobs []Blob) ([]Cell, error) {
return cells, nil return cells, nil
} }
func ckzgRecoverBlob(cells []Cell, cellIndices []uint64) (Blob, error) { func ckzgRecoverBlobs(cells []Cell, cellIndices []uint64) ([]Blob, error) {
ckzgIniter.Do(ckzgInit) ckzgIniter.Do(ckzgInit)
var kzgcells = make([]ckzg4844.Cell, 0, len(cells)) if len(cells)%len(cellIndices) != 0 {
return []Blob{}, errors.New("cells with wrong length")
}
for _, cell := range cells { blobCount := len(cells) / len(cellIndices)
var blobs = make([]Blob, 0, blobCount)
offset := 0
for range blobCount {
var kzgcells = make([]ckzg4844.Cell, 0, len(cellIndices))
for _, cell := range cells[offset : offset+len(cellIndices)] {
kzgcells = append(kzgcells, ckzg4844.Cell(cell)) kzgcells = append(kzgcells, ckzg4844.Cell(cell))
} }
extCells, _, err := ckzg4844.RecoverCellsAndKZGProofs(cellIndices, kzgcells) extCells, _, err := ckzg4844.RecoverCellsAndKZGProofs(cellIndices, kzgcells)
if err != nil { if err != nil {
return Blob{}, err return []Blob{}, err
} }
var result Blob var blob Blob
offset := 0 for i, cell := range extCells[:64] {
for _, cell := range extCells[:64] { copy(blob[i*len(cell):], cell[:])
copy(result[offset:], cell[:])
offset += len(cell)
} }
return result, nil blobs = append(blobs, blob)
offset = offset + len(cellIndices)
}
return blobs, nil
} }

View file

@ -82,6 +82,6 @@ func ckzgComputeCells(blobs []Blob) ([]Cell, error) {
panic("unsupported platform") panic("unsupported platform")
} }
func ckzgRecoverBlob(cells []Cell, cellIndices []uint64) (Blob, error) { func ckzgRecoverBlobs(cells []Cell, cellIndices []uint64) ([]Blob, error) {
panic("unsupported platform") panic("unsupported platform")
} }

View file

@ -156,6 +156,7 @@ func gokzgVerifyCells(cells []Cell, commitments []Commitment, cellProofs []Proof
var ( var (
proofs = make([]gokzg4844.KZGProof, len(cellProofs)) proofs = make([]gokzg4844.KZGProof, len(cellProofs))
commits = make([]gokzg4844.KZGCommitment, 0, len(cellProofs)) commits = make([]gokzg4844.KZGCommitment, 0, len(cellProofs))
indices = make([]uint64, 0, len(cellProofs))
kzgcells = make([]*gokzg4844.Cell, 0, len(cellProofs)) kzgcells = make([]*gokzg4844.Cell, 0, len(cellProofs))
) )
// Copy over the cell proofs and cells // Copy over the cell proofs and cells
@ -174,7 +175,12 @@ func gokzgVerifyCells(cells []Cell, commitments []Commitment, cellProofs []Proof
commits = append(commits, gokzg4844.KZGCommitment(commitment)) commits = append(commits, gokzg4844.KZGCommitment(commitment))
} }
} }
return context.VerifyCellKZGProofBatch(commits, cellIndices, kzgcells, proofs) blobCounts := len(cellProofs) / len(cellIndices)
for j := 0; j < blobCounts; j++ {
indices = append(indices, cellIndices...)
}
return context.VerifyCellKZGProofBatch(commits, indices, kzgcells, proofs)
} }
// gokzgComputeCells computes cells from blobs. // gokzgComputeCells computes cells from blobs.
@ -186,7 +192,7 @@ func gokzgComputeCells(blobs []Blob) ([]Cell, error) {
for i := range blobs { for i := range blobs {
cellsI, err := context.ComputeCells((*gokzg4844.Blob)(&blobs[i]), 2) cellsI, err := context.ComputeCells((*gokzg4844.Blob)(&blobs[i]), 2)
if err != nil { if err != nil {
return nil, err return []Cell{}, err
} }
for _, c := range cellsI { for _, c := range cellsI {
if c != nil { if c != nil {
@ -197,26 +203,38 @@ func gokzgComputeCells(blobs []Blob) ([]Cell, error) {
return cells, nil return cells, nil
} }
func gokzgRecoverBlob(cells []Cell, cellIndices []uint64) (Blob, error) { func gokzgRecoverBlobs(cells []Cell, cellIndices []uint64) ([]Blob, error) {
gokzgIniter.Do(gokzgInit) gokzgIniter.Do(gokzgInit)
var kzgcells = make([]*gokzg4844.Cell, 0, len(cells)) if len(cells)%len(cellIndices) != 0 {
return []Blob{}, errors.New("cells with wrong length")
}
for _, cell := range cells { blobCount := len(cells) / len(cellIndices)
var blobs = make([]Blob, 0, blobCount)
offset := 0
for range blobCount {
var kzgcells = make([]*gokzg4844.Cell, 0, len(cellIndices))
for _, cell := range cells[offset : offset+len(cellIndices)] {
gc := gokzg4844.Cell(cell) gc := gokzg4844.Cell(cell)
kzgcells = append(kzgcells, &gc) kzgcells = append(kzgcells, &gc)
} }
extCells, _, err := context.RecoverCellsAndComputeKZGProofs(cellIndices, kzgcells, 2) // todo extCells, _, err := context.RecoverCellsAndComputeKZGProofs(cellIndices, kzgcells, 2) // todo
if err != nil { if err != nil {
return Blob{}, err return []Blob{}, err
} }
var result Blob var blob Blob
offset := 0 for i, cell := range extCells[:64] {
for _, cell := range extCells[:64] { copy(blob[i*len(cell):], cell[:])
copy(result[offset:], cell[:])
offset += len(cell)
} }
return result, nil blobs = append(blobs, blob)
offset = offset + len(cellIndices)
}
return blobs, nil
} }

View file

@ -269,18 +269,18 @@ func testVerifyPartialCells(t *testing.T, ckzg bool) {
if err != nil { if err != nil {
t.Fatalf("failed to compute cells: %v", err) t.Fatalf("failed to compute cells: %v", err)
} }
commits = append(commits, commitments[bi])
// sample 0, 31, 63, 95 cells // sample 0, 31, 63, 95 cells
step := len(cells) / 4 step := len(cells) / 4
sampleIdx := []int{0, step - 1, 2*step - 1, 3*step - 1} indices = []uint64{0, uint64(step - 1), uint64(2*step - 1), uint64(3*step - 1)}
for _, idx := range sampleIdx { for _, idx := range indices {
partialCells = append(partialCells, cells[idx]) partialCells = append(partialCells, cells[idx])
partialProofs = append(partialProofs, proofs[idx]) partialProofs = append(partialProofs, proofs[idx])
commits = append(commits, commitments[bi])
indices = append(indices, uint64(idx))
} }
} }
// t.Fatalf("length: %d %d %d %d", len(partialCells), len(commits), len(partialProofs), len(indices))
if err := VerifyCells(partialCells, commits, partialProofs, indices); err != nil { if err := VerifyCells(partialCells, commits, partialProofs, indices); err != nil {
t.Fatalf("failed to verify partial cell proofs: %v", err) t.Fatalf("failed to verify partial cell proofs: %v", err)
@ -297,37 +297,53 @@ func testRecoverBlob(t *testing.T, ckzg bool) {
defer func(old bool) { useCKZG.Store(old) }(useCKZG.Load()) defer func(old bool) { useCKZG.Store(old) }(useCKZG.Load())
useCKZG.Store(ckzg) useCKZG.Store(ckzg)
var blob = randBlob() blobs := []Blob{}
cells, err := ComputeCells([]Blob{*blob}) blobs = append(blobs, *randBlob())
blobs = append(blobs, *randBlob())
blobs = append(blobs, *randBlob())
cells, err := ComputeCells(blobs)
if err != nil { if err != nil {
t.Fatalf("failed to compute cells: %v", err) t.Fatalf("failed to compute cells: %v", err)
} }
proof, err := ComputeCellProofs(blob) proofs := make([]Proof, 0)
commitments := make([]Commitment, len(blobs))
for i, blob := range blobs {
proof, err := ComputeCellProofs(&blob)
if err != nil { if err != nil {
t.Fatalf("failed to compute proof: %v", err) t.Fatalf("failed to compute proof: %v", err)
} }
commitment, err := BlobToCommitment(blob) proofs = append(proofs, proof...)
commitment, err := BlobToCommitment(&blob)
if err != nil { if err != nil {
t.Fatalf("failed to compute commitment: %v", err) t.Fatalf("failed to compute commitment: %v", err)
} }
commitments[i] = commitment
}
var ( var (
partialCells []Cell partialCells []Cell
indices []uint64 indices []uint64
) )
for ci, cell := range cells { for ci := 64; ci < 128; ci++ {
partialCells = append(partialCells, cell)
indices = append(indices, uint64(ci)) indices = append(indices, uint64(ci))
} }
recoverBlob, err := RecoverBlob(partialCells, indices) for i := 0; i < len(cells); i += 128 {
start := i + 64
end := i + 128
partialCells = append(partialCells, cells[start:end]...)
}
recoverBlobs, err := RecoverBlobs(partialCells, indices)
if err != nil { if err != nil {
t.Fatalf("failed to recover blob: %v", err) t.Fatalf("failed to recover blob: %v", err)
} }
if err := VerifyCellProofs([]Blob{recoverBlob}, []Commitment{commitment}, proof); err != nil { if err := VerifyCellProofs(recoverBlobs, commitments, proofs); err != nil {
t.Fatalf("failed to verify recovered blob: %v", err) t.Fatalf("failed to verify recovered blob: %v", err)
} }
} }