mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 14:46:42 +00:00
crypto/kzg4844: verify cell proofs
This commit is contained in:
parent
a67f95dca0
commit
24c18d58af
5 changed files with 68 additions and 36 deletions
|
|
@ -202,6 +202,9 @@ func validateBlobSidecarOsaka(hashes []common.Hash, sidecar *types.BlobTxSidecar
|
||||||
if len(sidecar.Blobs) != len(hashes) {
|
if len(sidecar.Blobs) != len(hashes) {
|
||||||
return fmt.Errorf("invalid number of %d blobs compared to %d blob hashes", len(sidecar.Blobs), len(hashes))
|
return fmt.Errorf("invalid number of %d blobs compared to %d blob hashes", len(sidecar.Blobs), len(hashes))
|
||||||
}
|
}
|
||||||
|
if len(sidecar.Commitments) != len(hashes) {
|
||||||
|
return fmt.Errorf("invalid number of %d commitments compared to %d blob hashes", len(sidecar.Commitments), len(hashes))
|
||||||
|
}
|
||||||
if len(sidecar.Proofs) != len(hashes)*kzg4844.CellProofsPerBlob {
|
if len(sidecar.Proofs) != len(hashes)*kzg4844.CellProofsPerBlob {
|
||||||
return fmt.Errorf("invalid number of %d blob proofs expected %d", len(sidecar.Proofs), len(hashes)*kzg4844.CellProofsPerBlob)
|
return fmt.Errorf("invalid number of %d blob proofs expected %d", len(sidecar.Proofs), len(hashes)*kzg4844.CellProofsPerBlob)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -155,6 +155,9 @@ func VerifyBlobProof(blob *Blob, commitment Commitment, proof Proof) error {
|
||||||
return gokzgVerifyBlobProof(blob, commitment, proof)
|
return gokzgVerifyBlobProof(blob, commitment, proof)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// VerifyCellProofs verifies a batch of proofs corresponding to the blobs and commitments.
|
||||||
|
// Expects length of blobs and commitments to be equal.
|
||||||
|
// Expects length of proofs be 128 * length of blobs.
|
||||||
func VerifyCellProofs(blobs []*Blob, commitments []Commitment, proofs []Proof) error {
|
func VerifyCellProofs(blobs []*Blob, commitments []Commitment, proofs []Proof) error {
|
||||||
if useCKZG.Load() {
|
if useCKZG.Load() {
|
||||||
return ckzgVerifyCellProofBatch(blobs, commitments, proofs)
|
return ckzgVerifyCellProofBatch(blobs, commitments, proofs)
|
||||||
|
|
|
||||||
|
|
@ -151,23 +151,37 @@ func ckzgComputeCellProofs(blob *Blob) ([]Proof, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ckzgVerifyCellProofs 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, proofs []Proof) error {
|
func ckzgVerifyCellProofBatch(blobs []*Blob, commitments []Commitment, cellProofs []Proof) error {
|
||||||
ckzgIniter.Do(ckzgInit)
|
ckzgIniter.Do(ckzgInit)
|
||||||
|
var (
|
||||||
cells, err := ckzg.ComputeCells(blob)
|
proofs = make([]ckzg4844.Bytes48, len(cellProofs))
|
||||||
if err != nil {
|
commits = make([]ckzg4844.Bytes48, 0, len(cellProofs))
|
||||||
return err
|
cellIndices = make([]uint64, 0, len(cellProofs))
|
||||||
|
cells = make([]ckzg4844.Cell, 0, len(cellProofs))
|
||||||
|
)
|
||||||
|
// Copy over the cell proofs
|
||||||
|
for i, proof := range cellProofs {
|
||||||
|
proofs[i] = (ckzg4844.Bytes48)(proof)
|
||||||
}
|
}
|
||||||
cellIndices := make([]uint64, 0, len(cells))
|
// Blow up the commitments to be the same length as the proofs
|
||||||
for range len(cells) {
|
for _, commitment := range commitments {
|
||||||
cellIndices = append(cellIndices, 0)
|
for range gokzg4844.CellsPerExtBlob {
|
||||||
|
commits = append(commits, (ckzg4844.Bytes48)(commitment))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
kzgProofs := make([]ckzg4844.Bytes48, len(proofs))
|
// Compute the cells and cell indices
|
||||||
for _, proof := range proofs {
|
for _, blob := range blobs {
|
||||||
kzgProofs = append(kzgProofs, (ckzg4844.Bytes48)(proof))
|
cellsI, err := ckzg4844.ComputeCells((*ckzg4844.Blob)(blob))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
cells = append(cells, cellsI[:]...)
|
||||||
|
for idx := range len(cellsI) {
|
||||||
|
cellIndices = append(cellIndices, uint64(idx))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
valid, err := ckzg4844.VerifyCellKZGProofBatch(([]ckzg4844.Bytes48)(commitment), cells, cellIndices, kzgProofs)
|
valid, err := ckzg4844.VerifyCellKZGProofBatch(commits, cellIndices, cells, proofs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -116,30 +116,35 @@ func gokzgComputeCellProofs(blob *Blob) ([]Proof, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// gokzgVerifyCellProofs verifies that the blob data corresponds to the provided commitment.
|
// gokzgVerifyCellProofs verifies that the blob data corresponds to the provided commitment.
|
||||||
func gokzgVerifyCellProofBatch(blobs []*Blob, commitments []Commitment, proofs []Proof) error {
|
func gokzgVerifyCellProofBatch(blobs []*Blob, commitments []Commitment, cellProofs []Proof) error {
|
||||||
gokzgIniter.Do(gokzgInit)
|
gokzgIniter.Do(gokzgInit)
|
||||||
|
|
||||||
var cellIndices []uint64
|
var (
|
||||||
var cells []*gokzg4844.Cell
|
proofs = make([]gokzg4844.KZGProof, len(cellProofs))
|
||||||
for i, blob := range blobs {
|
commits = make([]gokzg4844.KZGCommitment, 0, len(cellProofs))
|
||||||
|
cellIndices = make([]uint64, 0, len(cellProofs))
|
||||||
|
cells = make([]*gokzg4844.Cell, 0, len(cellProofs))
|
||||||
|
)
|
||||||
|
// Copy over the cell proofs
|
||||||
|
for i, proof := range cellProofs {
|
||||||
|
proofs[i] = gokzg4844.KZGProof(proof)
|
||||||
|
}
|
||||||
|
// Blow up the commitments to be the same length as the proofs
|
||||||
|
for _, commitment := range commitments {
|
||||||
|
for range gokzg4844.CellsPerExtBlob {
|
||||||
|
commits = append(commits, gokzg4844.KZGCommitment(commitment))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Compute the cell and cell indices
|
||||||
|
for _, blob := range blobs {
|
||||||
cellsI, err := context.ComputeCells((*gokzg4844.Blob)(blob), 2)
|
cellsI, err := context.ComputeCells((*gokzg4844.Blob)(blob), 2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cells = append(cells, cellsI[:]...)
|
cells = append(cells, cellsI[:]...)
|
||||||
for range len(cellsI) {
|
for idx := range len(cellsI) {
|
||||||
cellIndices = append(cellIndices, uint64(i))
|
cellIndices = append(cellIndices, uint64(idx))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return context.VerifyCellKZGProofBatch(commits, cellIndices, cells[:], proofs)
|
||||||
comms := make([]gokzg4844.KZGCommitment, len(commitments))
|
|
||||||
for i, commitment := range commitments {
|
|
||||||
comms[i] = gokzg4844.KZGCommitment(commitment)
|
|
||||||
}
|
|
||||||
pr := make([]gokzg4844.KZGProof, len(proofs))
|
|
||||||
for i, proof := range proofs {
|
|
||||||
pr[i] = gokzg4844.KZGProof(proof)
|
|
||||||
}
|
|
||||||
|
|
||||||
return context.VerifyCellKZGProofBatch(comms, cellIndices, cells[:], pr)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -203,22 +203,29 @@ func testKZGCells(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)
|
||||||
|
|
||||||
blob := randBlob()
|
blob1 := randBlob()
|
||||||
|
blob2 := randBlob()
|
||||||
|
|
||||||
commitment, err := BlobToCommitment(blob)
|
commitment1, err := BlobToCommitment(blob1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to create KZG commitment from blob: %v", err)
|
t.Fatalf("failed to create KZG commitment from blob: %v", err)
|
||||||
}
|
}
|
||||||
proof, err := ComputeCellProofs(blob)
|
commitment2, err := BlobToCommitment(blob2)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to create KZG commitment from blob: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
proofs1, err := ComputeCellProofs(blob1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to create KZG proof at point: %v", err)
|
t.Fatalf("failed to create KZG proof at point: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var commitments []Commitment
|
proofs2, err := ComputeCellProofs(blob2)
|
||||||
for range len(proof) {
|
if err != nil {
|
||||||
commitments = append(commitments, commitment)
|
t.Fatalf("failed to create KZG proof at point: %v", err)
|
||||||
}
|
}
|
||||||
if err := VerifyCellProofs([]*Blob{blob}, commitments, proof); err != nil {
|
proofs := append(proofs1, proofs2...)
|
||||||
|
if err := VerifyCellProofs([]*Blob{blob1, blob2}, []Commitment{commitment1, commitment2}, proofs); err != nil {
|
||||||
t.Fatalf("failed to verify KZG proof at point: %v", err)
|
t.Fatalf("failed to verify KZG proof at point: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue