github.com/theQRL/go-zond@v0.1.1/crypto/kzg4844/kzg4844_ckzg_cgo.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  //go:build ckzg && !nacl && !js && cgo && !gofuzz
    18  
    19  package kzg4844
    20  
    21  import (
    22  	"encoding/json"
    23  	"errors"
    24  	"sync"
    25  
    26  	gokzg4844 "github.com/crate-crypto/go-kzg-4844"
    27  	ckzg4844 "github.com/ethereum/c-kzg-4844/bindings/go"
    28  	"github.com/theQRL/go-zond/common/hexutil"
    29  )
    30  
    31  // ckzgAvailable signals whether the library was compiled into Geth.
    32  const ckzgAvailable = true
    33  
    34  // ckzgIniter ensures that we initialize the KZG library once before using it.
    35  var ckzgIniter sync.Once
    36  
    37  // ckzgInit initializes the KZG library with the provided trusted setup.
    38  func ckzgInit() {
    39  	config, err := content.ReadFile("trusted_setup.json")
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  	params := new(gokzg4844.JSONTrustedSetup)
    44  	if err = json.Unmarshal(config, params); err != nil {
    45  		panic(err)
    46  	}
    47  	if err = gokzg4844.CheckTrustedSetupIsWellFormed(params); err != nil {
    48  		panic(err)
    49  	}
    50  	g1s := make([]byte, len(params.SetupG1Lagrange)*(len(params.SetupG1Lagrange[0])-2)/2)
    51  	for i, g1 := range params.SetupG1Lagrange {
    52  		copy(g1s[i*(len(g1)-2)/2:], hexutil.MustDecode(g1))
    53  	}
    54  	g2s := make([]byte, len(params.SetupG2)*(len(params.SetupG2[0])-2)/2)
    55  	for i, g2 := range params.SetupG2 {
    56  		copy(g2s[i*(len(g2)-2)/2:], hexutil.MustDecode(g2))
    57  	}
    58  	if err = ckzg4844.LoadTrustedSetup(g1s, g2s); err != nil {
    59  		panic(err)
    60  	}
    61  }
    62  
    63  // ckzgBlobToCommitment creates a small commitment out of a data blob.
    64  func ckzgBlobToCommitment(blob Blob) (Commitment, error) {
    65  	ckzgIniter.Do(ckzgInit)
    66  
    67  	commitment, err := ckzg4844.BlobToKZGCommitment((ckzg4844.Blob)(blob))
    68  	if err != nil {
    69  		return Commitment{}, err
    70  	}
    71  	return (Commitment)(commitment), nil
    72  }
    73  
    74  // ckzgComputeProof computes the KZG proof at the given point for the polynomial
    75  // represented by the blob.
    76  func ckzgComputeProof(blob Blob, point Point) (Proof, Claim, error) {
    77  	ckzgIniter.Do(ckzgInit)
    78  
    79  	proof, claim, err := ckzg4844.ComputeKZGProof((ckzg4844.Blob)(blob), (ckzg4844.Bytes32)(point))
    80  	if err != nil {
    81  		return Proof{}, Claim{}, err
    82  	}
    83  	return (Proof)(proof), (Claim)(claim), nil
    84  }
    85  
    86  // ckzgVerifyProof verifies the KZG proof that the polynomial represented by the blob
    87  // evaluated at the given point is the claimed value.
    88  func ckzgVerifyProof(commitment Commitment, point Point, claim Claim, proof Proof) error {
    89  	ckzgIniter.Do(ckzgInit)
    90  
    91  	valid, err := ckzg4844.VerifyKZGProof((ckzg4844.Bytes48)(commitment), (ckzg4844.Bytes32)(point), (ckzg4844.Bytes32)(claim), (ckzg4844.Bytes48)(proof))
    92  	if err != nil {
    93  		return err
    94  	}
    95  	if !valid {
    96  		return errors.New("invalid proof")
    97  	}
    98  	return nil
    99  }
   100  
   101  // ckzgComputeBlobProof returns the KZG proof that is used to verify the blob against
   102  // the commitment.
   103  //
   104  // This method does not verify that the commitment is correct with respect to blob.
   105  func ckzgComputeBlobProof(blob Blob, commitment Commitment) (Proof, error) {
   106  	ckzgIniter.Do(ckzgInit)
   107  
   108  	proof, err := ckzg4844.ComputeBlobKZGProof((ckzg4844.Blob)(blob), (ckzg4844.Bytes48)(commitment))
   109  	if err != nil {
   110  		return Proof{}, err
   111  	}
   112  	return (Proof)(proof), nil
   113  }
   114  
   115  // ckzgVerifyBlobProof verifies that the blob data corresponds to the provided commitment.
   116  func ckzgVerifyBlobProof(blob Blob, commitment Commitment, proof Proof) error {
   117  	ckzgIniter.Do(ckzgInit)
   118  
   119  	valid, err := ckzg4844.VerifyBlobKZGProof((ckzg4844.Blob)(blob), (ckzg4844.Bytes48)(commitment), (ckzg4844.Bytes48)(proof))
   120  	if err != nil {
   121  		return err
   122  	}
   123  	if !valid {
   124  		return errors.New("invalid proof")
   125  	}
   126  	return nil
   127  }