plugeth/crypto/kzg4844/kzg4844_gokzg.go
Péter Szilágyi 2169fa343a
crypto/kzg4844: pull in the C and Go libs for KZG cryptography (#27155)
* cryto/kzg4844: pull in the C and Go libs for KZG cryptography

* go.mod: pull in the KZG libraries

* crypto/kzg4844: add basic becnhmarks for ballpark numbers

* cmd, crypto: integrate both CKZG and GoKZG all the time, add flag

* cmd/utils, crypto/kzg4844: run library init on startup

* crypto/kzg4844: make linter happy

* crypto/kzg4844: push missing file

* crypto/kzg4844: fully disable CKZG but leave in the sources

* build, crypto/kzg4844, internal: link CKZG by default and with portable mode

* crypto/kzg4844: drop verifying the trusted setup in gokzg

* internal/build: yolo until it works?

* cmd/utils: make flag description friendlier

Co-authored-by: Martin Holst Swende <martin@swende.se>

* crypto/ckzg: no need for double availability check

* build: tiny flag cleanup nitpick

---------

Co-authored-by: Martin Holst Swende <martin@swende.se>
2023-05-10 14:54:14 +03:00

99 lines
3.4 KiB
Go

// Copyright 2023 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package kzg4844
import (
"encoding/json"
"sync"
gokzg4844 "github.com/crate-crypto/go-kzg-4844"
)
// context is the crypto primitive pre-seeded with the trusted setup parameters.
var context *gokzg4844.Context
// gokzgIniter ensures that we initialize the KZG library once before using it.
var gokzgIniter sync.Once
// gokzgInit initializes the KZG library with the provided trusted setup.
func gokzgInit() {
config, err := content.ReadFile("trusted_setup.json")
if err != nil {
panic(err)
}
params := new(gokzg4844.JSONTrustedSetup)
if err = json.Unmarshal(config, params); err != nil {
panic(err)
}
context, err = gokzg4844.NewContext4096(params)
if err != nil {
panic(err)
}
}
// gokzgBlobToCommitment creates a small commitment out of a data blob.
func gokzgBlobToCommitment(blob Blob) (Commitment, error) {
gokzgIniter.Do(gokzgInit)
commitment, err := context.BlobToKZGCommitment((gokzg4844.Blob)(blob), 0)
if err != nil {
return Commitment{}, err
}
return (Commitment)(commitment), nil
}
// gokzgComputeProof computes the KZG proof at the given point for the polynomial
// represented by the blob.
func gokzgComputeProof(blob Blob, point Point) (Proof, Claim, error) {
gokzgIniter.Do(gokzgInit)
proof, claim, err := context.ComputeKZGProof((gokzg4844.Blob)(blob), (gokzg4844.Scalar)(point), 0)
if err != nil {
return Proof{}, Claim{}, err
}
return (Proof)(proof), (Claim)(claim), nil
}
// gokzgVerifyProof verifies the KZG proof that the polynomial represented by the blob
// evaluated at the given point is the claimed value.
func gokzgVerifyProof(commitment Commitment, point Point, claim Claim, proof Proof) error {
gokzgIniter.Do(gokzgInit)
return context.VerifyKZGProof((gokzg4844.KZGCommitment)(commitment), (gokzg4844.Scalar)(point), (gokzg4844.Scalar)(claim), (gokzg4844.KZGProof)(proof))
}
// gokzgComputeBlobProof returns the KZG proof that is used to verify the blob against
// the commitment.
//
// This method does not verify that the commitment is correct with respect to blob.
func gokzgComputeBlobProof(blob Blob, commitment Commitment) (Proof, error) {
gokzgIniter.Do(gokzgInit)
proof, err := context.ComputeBlobKZGProof((gokzg4844.Blob)(blob), (gokzg4844.KZGCommitment)(commitment), 0)
if err != nil {
return Proof{}, err
}
return (Proof)(proof), nil
}
// gokzgVerifyBlobProof verifies that the blob data corresponds to the provided commitment.
func gokzgVerifyBlobProof(blob Blob, commitment Commitment, proof Proof) error {
gokzgIniter.Do(gokzgInit)
return context.VerifyBlobKZGProof((gokzg4844.Blob)(blob), (gokzg4844.KZGCommitment)(commitment), (gokzg4844.KZGProof)(proof))
}