github.com/klaytn/klaytn@v1.12.1/crypto/kzg4844/kzg4844_ckzg_cgo.go (about) 1 // Modifications Copyright 2023 The klaytn Authors 2 // Copyright 2023 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from crypto/kzg4844/kzg4844_ckzg_cgo.go (2023/11/14). 19 // Modified and improved for the klaytn development. 20 21 //go:build ckzg && !nacl && !js && cgo && !gofuzz 22 23 package kzg4844 24 25 import ( 26 "encoding/json" 27 "errors" 28 "sync" 29 30 gokzg4844 "github.com/crate-crypto/go-kzg-4844" 31 ckzg4844 "github.com/ethereum/c-kzg-4844/bindings/go" 32 "github.com/klaytn/klaytn/common/hexutil" 33 ) 34 35 // ckzgAvailable signals whether the library was compiled into Geth. 36 const ckzgAvailable = true 37 38 // ckzgIniter ensures that we initialize the KZG library once before using it. 39 var ckzgIniter sync.Once 40 41 // ckzgInit initializes the KZG library with the provided trusted setup. 42 func ckzgInit() { 43 config, err := content.ReadFile("trusted_setup.json") 44 if err != nil { 45 panic(err) 46 } 47 params := new(gokzg4844.JSONTrustedSetup) 48 if err = json.Unmarshal(config, params); err != nil { 49 panic(err) 50 } 51 if err = gokzg4844.CheckTrustedSetupIsWellFormed(params); err != nil { 52 panic(err) 53 } 54 g1s := make([]byte, len(params.SetupG1Lagrange)*(len(params.SetupG1Lagrange[0])-2)/2) 55 for i, g1 := range params.SetupG1Lagrange { 56 copy(g1s[i*(len(g1)-2)/2:], hexutil.MustDecode(g1)) 57 } 58 g2s := make([]byte, len(params.SetupG2)*(len(params.SetupG2[0])-2)/2) 59 for i, g2 := range params.SetupG2 { 60 copy(g2s[i*(len(g2)-2)/2:], hexutil.MustDecode(g2)) 61 } 62 if err = ckzg4844.LoadTrustedSetup(g1s, g2s); err != nil { 63 panic(err) 64 } 65 } 66 67 // ckzgBlobToCommitment creates a small commitment out of a data blob. 68 func ckzgBlobToCommitment(blob Blob) (Commitment, error) { 69 ckzgIniter.Do(ckzgInit) 70 71 commitment, err := ckzg4844.BlobToKZGCommitment((ckzg4844.Blob)(blob)) 72 if err != nil { 73 return Commitment{}, err 74 } 75 return (Commitment)(commitment), nil 76 } 77 78 // ckzgComputeProof computes the KZG proof at the given point for the polynomial 79 // represented by the blob. 80 func ckzgComputeProof(blob Blob, point Point) (Proof, Claim, error) { 81 ckzgIniter.Do(ckzgInit) 82 83 proof, claim, err := ckzg4844.ComputeKZGProof((ckzg4844.Blob)(blob), (ckzg4844.Bytes32)(point)) 84 if err != nil { 85 return Proof{}, Claim{}, err 86 } 87 return (Proof)(proof), (Claim)(claim), nil 88 } 89 90 // ckzgVerifyProof verifies the KZG proof that the polynomial represented by the blob 91 // evaluated at the given point is the claimed value. 92 func ckzgVerifyProof(commitment Commitment, point Point, claim Claim, proof Proof) error { 93 ckzgIniter.Do(ckzgInit) 94 95 valid, err := ckzg4844.VerifyKZGProof((ckzg4844.Bytes48)(commitment), (ckzg4844.Bytes32)(point), (ckzg4844.Bytes32)(claim), (ckzg4844.Bytes48)(proof)) 96 if err != nil { 97 return err 98 } 99 if !valid { 100 return errors.New("invalid proof") 101 } 102 return nil 103 } 104 105 // ckzgComputeBlobProof returns the KZG proof that is used to verify the blob against 106 // the commitment. 107 // 108 // This method does not verify that the commitment is correct with respect to blob. 109 func ckzgComputeBlobProof(blob Blob, commitment Commitment) (Proof, error) { 110 ckzgIniter.Do(ckzgInit) 111 112 proof, err := ckzg4844.ComputeBlobKZGProof((ckzg4844.Blob)(blob), (ckzg4844.Bytes48)(commitment)) 113 if err != nil { 114 return Proof{}, err 115 } 116 return (Proof)(proof), nil 117 } 118 119 // ckzgVerifyBlobProof verifies that the blob data corresponds to the provided commitment. 120 func ckzgVerifyBlobProof(blob Blob, commitment Commitment, proof Proof) error { 121 ckzgIniter.Do(ckzgInit) 122 123 valid, err := ckzg4844.VerifyBlobKZGProof((ckzg4844.Blob)(blob), (ckzg4844.Bytes48)(commitment), (ckzg4844.Bytes48)(proof)) 124 if err != nil { 125 return err 126 } 127 if !valid { 128 return errors.New("invalid proof") 129 } 130 return nil 131 }