cometbls

package
v0.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2026 License: UNKNOWN not legal advice Imports: 0 Imported by: 0

Documentation

Overview

Package cometbls provides native bindings for verifying CometBLS Groth16 proofs over BN254 from Gno contracts.

The host-side verifier is a line-for-line Go port of the Rust cometbls-groth16-verifier crate (unionlabs/union: lib/cometbls-groth16-verifier/). Pre-negated verifying-key constants live in constants.go (generated from verifying_key.bin; see the union repo's cmd/gen).

Gno contracts call VerifyZKP declared in cometbls.gno, which encodes the LightHeader using the bespoke fixed-width layout (see EncodedLightHeaderSize) and forwards to X_verifyZKP.

Index

Constants

View Source
const (
	FqSize = 32
	G1Size = 2 * FqSize
	G2Size = 2 * G1Size

	// ExpectedProofSize is the byte length of a serialized proof:
	//   A(G1) | B(G2) | C(G1) | ProofCommitment(G1) | ProofCommitmentPoK(G1)
	ExpectedProofSize = G1Size + G2Size + G1Size + G1Size + G1Size

	// EncodedLightHeaderSize is the byte length of the bespoke LightHeader
	// encoding accepted by VerifyZKP:
	//   8 height (BE i64) | 8 seconds (BE i64) | 4 nanos (BE i32)
	//   | 32 validators_hash | 32 next_validators_hash | 32 app_hash
	EncodedLightHeaderSize = 8 + 8 + 4 + 32 + 32 + 32
)

Field and point sizes (match the Rust crate).

View Source
const NbPublicInputs = 2

NbPublicInputs is the number of public inputs to the circuit, excluding the constant term and the Pedersen commitment contribution.

Variables

View Source
var (
	AlphaG1               bn254.G1Affine
	BetaNegG2             bn254.G2Affine
	GammaNegG2            bn254.G2Affine
	DeltaNegG2            bn254.G2Affine
	PedersenG             bn254.G2Affine
	PedersenGRootSigmaNeg bn254.G2Affine
	GammaAbcG1            [3]bn254.G1Affine
)

Pre-negated Groth16 verifying-key constants extracted from verifying_key.bin. BetaNegG2, GammaNegG2, DeltaNegG2 and PedersenGRootSigmaNeg are stored pre-negated so pairing checks can be evaluated as a single product equal to 1 (matching the Rust cometbls-groth16-verifier).

View Source
var (
	ErrInvalidPublicInput = errors.New("invalid public input")
	ErrInvalidPoint       = errors.New("invalid point")
	ErrInvalidProof       = errors.New("invalid proof")
	ErrInvalidPok         = errors.New("invalid pok")
	ErrInvalidCommitment  = errors.New("invalid commitment")
	ErrInvalidRawProof    = errors.New("invalid raw proof")
	ErrInvalidHeight      = errors.New("invalid height")
	ErrInvalidTimestamp   = errors.New("invalid timestamp")
	ErrInvalidHeaderLen   = errors.New("invalid header encoding length")
	ErrInvalidChainIDLen  = errors.New("chain id must be at most 31 bytes")
)

Sentinel errors mirroring the Rust `Error` enum. Tests compare against these by identity using errors.Is.

Functions

func EncodeLightHeader

func EncodeLightHeader(h LightHeader) []byte

EncodeLightHeader produces the bespoke fixed-width encoding accepted by VerifyZKP. A matching encoder is provided gno-side in cometbls.gno.

func PublicInputs

func PublicInputs(chainID string, trustedValidatorsHash [32]byte, header LightHeader, zkp *ZKP) ([2]fr.Element, error)

PublicInputs computes the two scalar public inputs consumed by the circuit: the SHA-256-derived inputs hash (top byte zeroed to fit in F_r) and the commitment hash.

func VerifyZKP

func VerifyZKP(chainID string, trustedValidatorsHash []byte, headerEncoded []byte, zkpBytes []byte) error

VerifyZKP verifies a CometBLS Groth16 proof. The header is accepted in the bespoke fixed-width encoding produced by EncodeLightHeader (see EncodedLightHeaderSize for the layout).

Returns nil on success or one of the sentinel errors on failure.

func X_verifyZKP

func X_verifyZKP(chainID string, trustedValidatorsHash []byte, headerEncoded []byte, zkp []byte) string

X_verifyZKP is the host-side native binding for the `verifyZKP` function declared in cometbls.gno. It returns an empty string on successful proof verification and a non-empty error message otherwise — gno cannot currently return a Go `error` across the native ABI, so the gno wrapper converts the string back into an error value.

`headerEncoded` must be exactly EncodedLightHeaderSize bytes (116); see EncodeLightHeader in cometbls.gno for the canonical encoder.

Types

type LightHeader

type LightHeader struct {
	Height             int64
	TimeSeconds        int64
	TimeNanos          int32
	ValidatorsHash     [32]byte
	NextValidatorsHash [32]byte
	AppHash            [32]byte
}

LightHeader matches cometbls-light-client-types::LightHeader.

func DecodeLightHeader

func DecodeLightHeader(buf []byte) (LightHeader, error)

DecodeLightHeader parses the bespoke encoding, validating bounds.

type Proof

type Proof struct {
	A bn254.G1Affine
	B bn254.G2Affine
	C bn254.G1Affine
}

Proof is the three canonical Groth16 points.

type ZKP

type ZKP struct {
	Proof              Proof
	ProofCommitment    bn254.G1Affine
	ProofCommitmentPoK bn254.G1Affine
}

ZKP bundles a Groth16 proof with the Pedersen commitment and its proof-of-knowledge, matching the layout expected by the CometBLS circuit.

func ParseZKP

func ParseZKP(raw []byte) (*ZKP, error)

ParseZKP decodes a raw proof byte slice in big-endian layout.

Layout (bytes):

0     .. G1                    = A
G1    .. G1+G2                 = B
G1+G2 .. 2*G1+G2               = C
2*G1+G2 .. 3*G1+G2             = ProofCommitment
3*G1+G2 .. 4*G1+G2             = ProofCommitmentPoK

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL