github.com/ethereum/go-ethereum@v1.16.1/crypto/kzg4844/kzg4844_gokzg.go (about) 1 // Copyright 2023 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package kzg4844 18 19 import ( 20 "encoding/json" 21 "sync" 22 23 gokzg4844 "github.com/crate-crypto/go-eth-kzg" 24 ) 25 26 // context is the crypto primitive pre-seeded with the trusted setup parameters. 27 var context *gokzg4844.Context 28 29 // gokzgIniter ensures that we initialize the KZG library once before using it. 30 var gokzgIniter sync.Once 31 32 // gokzgInit initializes the KZG library with the provided trusted setup. 33 func gokzgInit() { 34 config, err := content.ReadFile("trusted_setup.json") 35 if err != nil { 36 panic(err) 37 } 38 params := new(gokzg4844.JSONTrustedSetup) 39 if err = json.Unmarshal(config, params); err != nil { 40 panic(err) 41 } 42 context, err = gokzg4844.NewContext4096(params) 43 if err != nil { 44 panic(err) 45 } 46 } 47 48 // gokzgBlobToCommitment creates a small commitment out of a data blob. 49 func gokzgBlobToCommitment(blob *Blob) (Commitment, error) { 50 gokzgIniter.Do(gokzgInit) 51 52 commitment, err := context.BlobToKZGCommitment((*gokzg4844.Blob)(blob), 0) 53 if err != nil { 54 return Commitment{}, err 55 } 56 return (Commitment)(commitment), nil 57 } 58 59 // gokzgComputeProof computes the KZG proof at the given point for the polynomial 60 // represented by the blob. 61 func gokzgComputeProof(blob *Blob, point Point) (Proof, Claim, error) { 62 gokzgIniter.Do(gokzgInit) 63 64 proof, claim, err := context.ComputeKZGProof((*gokzg4844.Blob)(blob), (gokzg4844.Scalar)(point), 0) 65 if err != nil { 66 return Proof{}, Claim{}, err 67 } 68 return (Proof)(proof), (Claim)(claim), nil 69 } 70 71 // gokzgVerifyProof verifies the KZG proof that the polynomial represented by the blob 72 // evaluated at the given point is the claimed value. 73 func gokzgVerifyProof(commitment Commitment, point Point, claim Claim, proof Proof) error { 74 gokzgIniter.Do(gokzgInit) 75 76 return context.VerifyKZGProof((gokzg4844.KZGCommitment)(commitment), (gokzg4844.Scalar)(point), (gokzg4844.Scalar)(claim), (gokzg4844.KZGProof)(proof)) 77 } 78 79 // gokzgComputeBlobProof returns the KZG proof that is used to verify the blob against 80 // the commitment. 81 // 82 // This method does not verify that the commitment is correct with respect to blob. 83 func gokzgComputeBlobProof(blob *Blob, commitment Commitment) (Proof, error) { 84 gokzgIniter.Do(gokzgInit) 85 86 proof, err := context.ComputeBlobKZGProof((*gokzg4844.Blob)(blob), (gokzg4844.KZGCommitment)(commitment), 0) 87 if err != nil { 88 return Proof{}, err 89 } 90 return (Proof)(proof), nil 91 } 92 93 // gokzgVerifyBlobProof verifies that the blob data corresponds to the provided commitment. 94 func gokzgVerifyBlobProof(blob *Blob, commitment Commitment, proof Proof) error { 95 gokzgIniter.Do(gokzgInit) 96 97 return context.VerifyBlobKZGProof((*gokzg4844.Blob)(blob), (gokzg4844.KZGCommitment)(commitment), (gokzg4844.KZGProof)(proof)) 98 } 99 100 // gokzgComputeCellProofs returns the KZG cell proofs that are used to verify the blob against 101 // the commitment. 102 // 103 // This method does not verify that the commitment is correct with respect to blob. 104 func gokzgComputeCellProofs(blob *Blob) ([]Proof, error) { 105 gokzgIniter.Do(gokzgInit) 106 107 _, proofs, err := context.ComputeCellsAndKZGProofs((*gokzg4844.Blob)(blob), 0) 108 if err != nil { 109 return []Proof{}, err 110 } 111 var p []Proof 112 for _, proof := range proofs { 113 p = append(p, (Proof)(proof)) 114 } 115 return p, nil 116 } 117 118 // gokzgVerifyCellProofs verifies that the blob data corresponds to the provided commitment. 119 func gokzgVerifyCellProofBatch(blobs []Blob, commitments []Commitment, cellProofs []Proof) error { 120 gokzgIniter.Do(gokzgInit) 121 122 var ( 123 proofs = make([]gokzg4844.KZGProof, len(cellProofs)) 124 commits = make([]gokzg4844.KZGCommitment, 0, len(cellProofs)) 125 cellIndices = make([]uint64, 0, len(cellProofs)) 126 cells = make([]*gokzg4844.Cell, 0, len(cellProofs)) 127 ) 128 // Copy over the cell proofs 129 for i, proof := range cellProofs { 130 proofs[i] = gokzg4844.KZGProof(proof) 131 } 132 // Blow up the commitments to be the same length as the proofs 133 for _, commitment := range commitments { 134 for range gokzg4844.CellsPerExtBlob { 135 commits = append(commits, gokzg4844.KZGCommitment(commitment)) 136 } 137 } 138 // Compute the cell and cell indices 139 for i := range blobs { 140 cellsI, err := context.ComputeCells((*gokzg4844.Blob)(&blobs[i]), 2) 141 if err != nil { 142 return err 143 } 144 cells = append(cells, cellsI[:]...) 145 for idx := range len(cellsI) { 146 cellIndices = append(cellIndices, uint64(idx)) 147 } 148 } 149 return context.VerifyCellKZGProofBatch(commits, cellIndices, cells[:], proofs) 150 }