github.com/theQRL/go-zond@v0.1.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-kzg-4844"
    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  }