|
| 1 | +package kzg |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/protolambda/go-kzg/bls" |
| 8 | + |
| 9 | + "github.com/ethereum/go-ethereum/crypto" |
| 10 | + "github.com/ethereum/go-ethereum/params" |
| 11 | +) |
| 12 | + |
| 13 | +// VerifyKZGProof implements verify_kzg_proof from the EIP-4844 consensus spec: |
| 14 | +// https://github.com/ethereum/consensus-specs/blob/dev/specs/eip4844/polynomial-commitments.md#verify_kzg_proof |
| 15 | +func VerifyKZGProof(polynomialKZG [48]byte, z *bls.Fr, y *bls.Fr, kzgProof [48]byte) (bool, error) { |
| 16 | + polynomialKZGG1, err := bls.FromCompressedG1(polynomialKZG[:]) |
| 17 | + if err != nil { |
| 18 | + return false, fmt.Errorf("failed to decode polynomialKZG: %v", err) |
| 19 | + } |
| 20 | + kzgProofG1, err := bls.FromCompressedG1(kzgProof[:]) |
| 21 | + if err != nil { |
| 22 | + return false, fmt.Errorf("failed to decode kzgProof: %v", err) |
| 23 | + } |
| 24 | + return VerifyKZGProofFromPoints(polynomialKZGG1, z, y, kzgProofG1), nil |
| 25 | +} |
| 26 | + |
| 27 | +func VerifyKZGProofFromPoints(polynomialKZG *bls.G1Point, z *bls.Fr, y *bls.Fr, kzgProof *bls.G1Point) bool { |
| 28 | + var zG2 bls.G2Point |
| 29 | + bls.MulG2(&zG2, &bls.GenG2, z) |
| 30 | + var yG1 bls.G1Point |
| 31 | + bls.MulG1(&yG1, &bls.GenG1, y) |
| 32 | + |
| 33 | + var xMinusZ bls.G2Point |
| 34 | + bls.SubG2(&xMinusZ, &kzgSetupG2[1], &zG2) |
| 35 | + var pMinusY bls.G1Point |
| 36 | + bls.SubG1(&pMinusY, polynomialKZG, &yG1) |
| 37 | + |
| 38 | + return bls.PairingsVerify(&pMinusY, &bls.GenG2, kzgProof, &xMinusZ) |
| 39 | +} |
| 40 | + |
| 41 | +// KZGToVersionedHash implements kzg_to_versioned_hash from EIP-4844 |
| 42 | +func KZGToVersionedHash(kzg [48]byte) [32]byte { |
| 43 | + h := crypto.Keccak256Hash(kzg[:]) |
| 44 | + h[0] = params.BlobCommitmentVersionKZG |
| 45 | + return h |
| 46 | +} |
| 47 | + |
| 48 | +// PointEvaluationPrecompile implements point_evaluation_precompile from EIP-4844 |
| 49 | +func PointEvaluationPrecompile(input []byte) ([]byte, error) { |
| 50 | + if len(input) != 192 { |
| 51 | + return nil, errors.New("invalid input length") |
| 52 | + } |
| 53 | + |
| 54 | + // versioned hash: first 32 bytes |
| 55 | + var versionedHash [32]byte |
| 56 | + copy(versionedHash[:], input[:32]) |
| 57 | + |
| 58 | + var x, y [32]byte |
| 59 | + // Evaluation point: next 32 bytes |
| 60 | + copy(x[:], input[32:64]) |
| 61 | + // Expected output: next 32 bytes |
| 62 | + copy(y[:], input[64:96]) |
| 63 | + |
| 64 | + // successfully converting x and y to bls.Fr confirms they are < MODULUS per the spec |
| 65 | + var xFr, yFr bls.Fr |
| 66 | + ok := bls.FrFrom32(&xFr, x) |
| 67 | + if !ok { |
| 68 | + return nil, errors.New("invalid evaluation point") |
| 69 | + } |
| 70 | + ok = bls.FrFrom32(&yFr, y) |
| 71 | + if !ok { |
| 72 | + return nil, errors.New("invalid expected output") |
| 73 | + } |
| 74 | + |
| 75 | + // input kzg point: next 48 bytes |
| 76 | + var dataKZG [48]byte |
| 77 | + copy(dataKZG[:], input[96:144]) |
| 78 | + if KZGToVersionedHash(dataKZG) != versionedHash { |
| 79 | + return nil, errors.New("mismatched versioned hash") |
| 80 | + } |
| 81 | + |
| 82 | + // Quotient kzg: next 48 bytes |
| 83 | + var quotientKZG [48]byte |
| 84 | + copy(quotientKZG[:], input[144:192]) |
| 85 | + |
| 86 | + ok, err := VerifyKZGProof(dataKZG, &xFr, &yFr, quotientKZG) |
| 87 | + if err != nil { |
| 88 | + return nil, fmt.Errorf("verify_kzg_proof error: %v", err) |
| 89 | + } |
| 90 | + if !ok { |
| 91 | + return nil, errors.New("failed to verify kzg proof") |
| 92 | + } |
| 93 | + return []byte{}, nil |
| 94 | +} |
0 commit comments