github.com/theQRL/go-zond@v0.1.1/crypto/kzg4844/kzg4844.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 implements the KZG crypto for EIP-4844.
    18  package kzg4844
    19  
    20  import (
    21  	"embed"
    22  	"errors"
    23  	"sync/atomic"
    24  )
    25  
    26  //go:embed trusted_setup.json
    27  var content embed.FS
    28  
    29  // Blob represents a 4844 data blob.
    30  type Blob [131072]byte
    31  
    32  // Commitment is a serialized commitment to a polynomial.
    33  type Commitment [48]byte
    34  
    35  // Proof is a serialized commitment to the quotient polynomial.
    36  type Proof [48]byte
    37  
    38  // Point is a BLS field element.
    39  type Point [32]byte
    40  
    41  // Claim is a claimed evaluation value in a specific point.
    42  type Claim [32]byte
    43  
    44  // useCKZG controls whether the cryptography should use the Go or C backend.
    45  var useCKZG atomic.Bool
    46  
    47  // UseCKZG can be called to switch the default Go implementation of KZG to the C
    48  // library if fo some reason the user wishes to do so (e.g. consensus bug in one
    49  // or the other).
    50  func UseCKZG(use bool) error {
    51  	if use && !ckzgAvailable {
    52  		return errors.New("CKZG unavailable on your platform")
    53  	}
    54  	useCKZG.Store(use)
    55  
    56  	// Initializing the library can take 2-4 seconds - and can potentially crash
    57  	// on CKZG and non-ADX CPUs - so might as well do it now and don't wait until
    58  	// a crypto operation is actually needed live.
    59  	if use {
    60  		ckzgIniter.Do(ckzgInit)
    61  	} else {
    62  		gokzgIniter.Do(gokzgInit)
    63  	}
    64  	return nil
    65  }
    66  
    67  // BlobToCommitment creates a small commitment out of a data blob.
    68  func BlobToCommitment(blob Blob) (Commitment, error) {
    69  	if useCKZG.Load() {
    70  		return ckzgBlobToCommitment(blob)
    71  	}
    72  	return gokzgBlobToCommitment(blob)
    73  }
    74  
    75  // ComputeProof computes the KZG proof at the given point for the polynomial
    76  // represented by the blob.
    77  func ComputeProof(blob Blob, point Point) (Proof, Claim, error) {
    78  	if useCKZG.Load() {
    79  		return ckzgComputeProof(blob, point)
    80  	}
    81  	return gokzgComputeProof(blob, point)
    82  }
    83  
    84  // VerifyProof verifies the KZG proof that the polynomial represented by the blob
    85  // evaluated at the given point is the claimed value.
    86  func VerifyProof(commitment Commitment, point Point, claim Claim, proof Proof) error {
    87  	if useCKZG.Load() {
    88  		return ckzgVerifyProof(commitment, point, claim, proof)
    89  	}
    90  	return gokzgVerifyProof(commitment, point, claim, proof)
    91  }
    92  
    93  // ComputeBlobProof returns the KZG proof that is used to verify the blob against
    94  // the commitment.
    95  //
    96  // This method does not verify that the commitment is correct with respect to blob.
    97  func ComputeBlobProof(blob Blob, commitment Commitment) (Proof, error) {
    98  	if useCKZG.Load() {
    99  		return ckzgComputeBlobProof(blob, commitment)
   100  	}
   101  	return gokzgComputeBlobProof(blob, commitment)
   102  }
   103  
   104  // VerifyBlobProof verifies that the blob data corresponds to the provided commitment.
   105  func VerifyBlobProof(blob Blob, commitment Commitment, proof Proof) error {
   106  	if useCKZG.Load() {
   107  		return ckzgVerifyBlobProof(blob, commitment, proof)
   108  	}
   109  	return gokzgVerifyBlobProof(blob, commitment, proof)
   110  }