|
| 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/ethereum/go-ethereum/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.SetupG1)*(len(params.SetupG1[0])-2)/2) |
| 51 | + for i, g1 := range params.SetupG1 { |
| 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 | + proof, claim, err := ckzg4844.ComputeKZGProof((ckzg4844.Blob)(blob), (ckzg4844.Bytes32)(point)) |
| 78 | + if err != nil { |
| 79 | + return Proof{}, Claim{}, err |
| 80 | + } |
| 81 | + return (Proof)(proof), (Claim)(claim), nil |
| 82 | +} |
| 83 | + |
| 84 | +// ckzgVerifyProof verifies the KZG proof that the polynomial represented by the blob |
| 85 | +// evaluated at the given point is the claimed value. |
| 86 | +func ckzgVerifyProof(commitment Commitment, point Point, claim Claim, proof Proof) error { |
| 87 | + valid, err := ckzg4844.VerifyKZGProof((ckzg4844.Bytes48)(commitment), (ckzg4844.Bytes32)(point), (ckzg4844.Bytes32)(claim), (ckzg4844.Bytes48)(proof)) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + if !valid { |
| 92 | + return errors.New("invalid proof") |
| 93 | + } |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +// ckzgComputeBlobProof returns the KZG proof that is used to verify the blob against |
| 98 | +// the commitment. |
| 99 | +// |
| 100 | +// This method does not verify that the commitment is correct with respect to blob. |
| 101 | +func ckzgComputeBlobProof(blob Blob, commitment Commitment) (Proof, error) { |
| 102 | + proof, err := ckzg4844.ComputeBlobKZGProof((ckzg4844.Blob)(blob), (ckzg4844.Bytes48)(commitment)) |
| 103 | + if err != nil { |
| 104 | + return Proof{}, err |
| 105 | + } |
| 106 | + return (Proof)(proof), nil |
| 107 | +} |
| 108 | + |
| 109 | +// ckzgVerifyBlobProof verifies that the blob data corresponds to the provided commitment. |
| 110 | +func ckzgVerifyBlobProof(blob Blob, commitment Commitment, proof Proof) error { |
| 111 | + valid, err := ckzg4844.VerifyBlobKZGProof((ckzg4844.Blob)(blob), (ckzg4844.Bytes48)(commitment), (ckzg4844.Bytes48)(proof)) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + if !valid { |
| 116 | + return errors.New("invalid proof") |
| 117 | + } |
| 118 | + return nil |
| 119 | +} |
0 commit comments