zk

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package zk provides GPU-accelerated zero-knowledge proof operations.

Supports NTT, MSM, polynomial arithmetic, Poseidon2 hashing, FRI folding, and commitment schemes. All operations use unified accel backend selection with automatic fallback to CPU implementations.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidInput      = errors.New("zk: invalid input")
	ErrInvalidDegree     = errors.New("zk: invalid polynomial degree")
	ErrInvalidModulus    = errors.New("zk: invalid modulus")
	ErrDimensionMismatch = errors.New("zk: dimension mismatch")
	ErrEmptyBatch        = errors.New("zk: empty batch")
)

Errors

Functions

func BatchCommitPoly

func BatchCommitPoly(params CommitParams, coeffsList [][][]byte, srs [][]byte) ([][]byte, error)

BatchCommitPoly computes multiple polynomial commitments in parallel.

func BatchINTT

func BatchINTT(params NTTParams, polys [][]uint64) ([][]uint64, error)

BatchINTT performs inverse NTT on multiple polynomials in parallel.

func BatchNTT

func BatchNTT(params NTTParams, polys [][]uint64) ([][]uint64, error)

BatchNTT performs forward NTT on multiple polynomials in parallel.

func BatchPoseidon2Hash

func BatchPoseidon2Hash(params Poseidon2Params, lefts, rights []uint64) ([]uint64, error)

BatchPoseidon2Hash computes multiple Poseidon2 hashes in parallel. Used for building Merkle trees efficiently.

func CommitPoly

func CommitPoly(params CommitParams, coeffs [][]byte, srs [][]byte) ([]byte, error)

CommitPoly computes a polynomial commitment (KZG). coeffs: polynomial coefficients srs: structured reference string (powers of tau) Returns commitment point in compressed form.

func FRIFold

func FRIFold(params FRIParams, evals []uint64, alpha uint64) ([]uint64, error)

FRIFold performs FRI folding step. evals: polynomial evaluations at rate-2 LDE alpha: random folding challenge Returns folded polynomial evaluations.

func FRIQueryPhase

func FRIQueryPhase(params FRIParams, evals []uint64, indices []uint32) ([][]uint64, error)

FRIQueryPhase computes query responses for FRI protocol. evals: committed polynomial evaluations indices: query indices Returns query response data.

func FieldAdd

func FieldAdd(params FieldParams, a, b []uint64) ([]uint64, error)

FieldAdd adds field elements.

func FieldExp

func FieldExp(params FieldParams, a []uint64, exp uint64) ([]uint64, error)

FieldExp computes a^exp mod modulus for each element.

func FieldInv

func FieldInv(params FieldParams, a []uint64) ([]uint64, error)

FieldInv computes modular inverse of field elements.

func FieldMul

func FieldMul(params FieldParams, a, b []uint64) ([]uint64, error)

FieldMul multiplies field elements.

func INTT

func INTT(params NTTParams, evals []uint64) ([]uint64, error)

INTT performs inverse Number Theoretic Transform. Transforms polynomial from evaluation to coefficient form.

func MSM

func MSM(curve CurveType, scalars, points [][]byte) ([]byte, error)

MSM performs multi-scalar multiplication. result = sum(scalars[i] * points[i]) for all i

func MSMBatch

func MSMBatch(curve CurveType, scalars, points [][][]byte) ([][]byte, error)

MSMBatch performs multiple MSMs in parallel.

func NTT

func NTT(params NTTParams, coeffs []uint64) ([]uint64, error)

NTT performs forward Number Theoretic Transform. Transforms polynomial from coefficient to evaluation form.

func PolyAdd

func PolyAdd(params FieldParams, a, b []uint64) ([]uint64, error)

PolyAdd adds two polynomials coefficient-wise.

func PolyEval

func PolyEval(params FieldParams, coeffs, points []uint64) ([]uint64, error)

PolyEval evaluates polynomial at given points. coeffs: [degree+1] coefficients, points: evaluation points Returns values[i] = poly(points[i])

func PolyInterpolate

func PolyInterpolate(params FieldParams, xs, ys []uint64) ([]uint64, error)

PolyInterpolate computes polynomial from points using Lagrange interpolation. xs, ys: evaluation points and values Returns polynomial coefficients.

func PolyMul

func PolyMul(params NTTParams, a, b []uint64) ([]uint64, error)

PolyMul multiplies two polynomials using NTT. Result is in coefficient form with degree len(a) + len(b) - 1.

func PolyMulPointwise

func PolyMulPointwise(params FieldParams, a, b []uint64) ([]uint64, error)

PolyMulPointwise multiplies polynomials in NTT domain (pointwise).

func PolySub

func PolySub(params FieldParams, a, b []uint64) ([]uint64, error)

PolySub subtracts two polynomials coefficient-wise.

func Poseidon2

func Poseidon2(params Poseidon2Params, inputs []uint64) (uint64, error)

Poseidon2 computes Poseidon2 hash of inputs. Returns field element as uint64 (for fields <= 64 bits) or []byte.

func Poseidon2Hash

func Poseidon2Hash(params Poseidon2Params, left, right uint64) (uint64, error)

Poseidon2Hash computes Poseidon2 hash for Merkle trees. left and right are field elements to hash together.

Types

type CommitParams

type CommitParams struct {
	Curve  CurveType // Curve for commitment
	Degree uint32    // Maximum polynomial degree
}

CommitParams contains parameters for polynomial commitments.

type CurveType

type CurveType uint8

CurveType identifies the elliptic curve for MSM operations.

const (
	CurveBN254     CurveType = iota // BN254 (alt_bn128)
	CurveBLS12_381                  // BLS12-381
	CurveBLS12_377                  // BLS12-377
	CurvePallas                     // Pallas (Zcash/Halo2)
	CurveVesta                      // Vesta (Zcash/Halo2)
)

type FRIParams

type FRIParams struct {
	Modulus      uint64 // Field modulus
	FoldFactor   uint32 // Folding factor (typically 2 or 4)
	BlowupFactor uint32 // Reed-Solomon blowup factor
}

FRIParams contains parameters for FRI folding.

type FieldParams

type FieldParams struct {
	Modulus uint64 // Prime modulus
}

FieldParams contains parameters for finite field operations.

type NTTParams

type NTTParams struct {
	N       uint32 // Polynomial degree (power of 2)
	Modulus uint64 // Prime modulus
	Root    uint64 // Primitive N-th root of unity mod Modulus
}

NTTParams contains parameters for Number Theoretic Transform.

type Poseidon2Params

type Poseidon2Params struct {
	T          uint32   // State width (t)
	D          uint32   // S-box degree (typically 5 or 7)
	RoundsF    uint32   // Full rounds
	RoundsP    uint32   // Partial rounds
	Modulus    uint64   // Field modulus
	RoundConst []uint64 // Round constants
	MDS        []uint64 // MDS matrix (t x t)
}

Poseidon2Params contains parameters for Poseidon2 hash.

Jump to

Keyboard shortcuts

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