fhe

package
v1.22.78 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2026 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package fhe provides EVM precompiles for Fully Homomorphic Encryption operations. These precompiles expose FHE operations to smart contracts running on the C-Chain.

Architecture:

  • Precompiles at 0x0080-0x00FF for FHE operations
  • Ciphertexts stored by 32-byte handle
  • Operations consume gas proportional to computational cost
  • Threshold decryption via Warp messaging to T-Chain

Index

Constants

View Source
const (
	// Arithmetic operations
	FHEAddAddress = 0x0080
	FHESubAddress = 0x0081
	FHEMulAddress = 0x0082
	FHEDivAddress = 0x0083
	FHERemAddress = 0x0084
	FHENegAddress = 0x0085

	// Comparison operations
	FHELeAddress = 0x0086
	FHELtAddress = 0x0087
	FHEGeAddress = 0x0088
	FHEGtAddress = 0x0089
	FHEEqAddress = 0x008A
	FHENeAddress = 0x008B

	// Min/Max operations
	FHEMinAddress = 0x008C
	FHEMaxAddress = 0x008D

	// Bitwise operations
	FHEAndAddress  = 0x0090
	FHEOrAddress   = 0x0091
	FHEXorAddress  = 0x0092
	FHENotAddress  = 0x0093
	FHEShlAddress  = 0x0094
	FHEShrAddress  = 0x0095
	FHERotlAddress = 0x0096
	FHERotrAddress = 0x0097

	// Encryption/Decryption
	TrivialEncryptAddress   = 0x00A0
	VerifyCiphertextAddress = 0x00A1
	CastAddress             = 0x00A2
	ReencryptAddress        = 0x00A3
	OptReencryptAddress     = 0x00A4
	DecryptAddress          = 0x00A5

	// Select (conditional)
	FHESelectAddress = 0x00B0
)

Precompile addresses for FHE operations

Variables

View Source
var (
	ErrInvalidInput          = errors.New("fhe: invalid input")
	ErrInvalidHandle         = errors.New("fhe: invalid ciphertext handle")
	ErrHandleNotFound        = errors.New("fhe: ciphertext handle not found")
	ErrTypeMismatch          = errors.New("fhe: operand type mismatch")
	ErrInsufficientLevels    = errors.New("fhe: insufficient multiplicative levels")
	ErrUnauthorized          = errors.New("fhe: unauthorized access to ciphertext")
	ErrDecryptionFailed      = errors.New("fhe: threshold decryption failed")
	ErrOperationNotSupported = errors.New("fhe: operation not supported")
	ErrInvalidCiphertext     = errors.New("fhe: invalid ciphertext data")
)

Errors

View Source
var GasCosts = map[uint64]uint64{
	FHEAddAddress: 50_000,
	FHESubAddress: 50_000,
	FHEMulAddress: 150_000,
	FHEDivAddress: 500_000,
	FHERemAddress: 500_000,
	FHENegAddress: 30_000,

	FHELeAddress: 200_000,
	FHELtAddress: 200_000,
	FHEGeAddress: 200_000,
	FHEGtAddress: 200_000,
	FHEEqAddress: 200_000,
	FHENeAddress: 200_000,

	FHEMinAddress: 250_000,
	FHEMaxAddress: 250_000,

	FHEAndAddress:  50_000,
	FHEOrAddress:   50_000,
	FHEXorAddress:  50_000,
	FHENotAddress:  30_000,
	FHEShlAddress:  100_000,
	FHEShrAddress:  100_000,
	FHERotlAddress: 100_000,
	FHERotrAddress: 100_000,

	TrivialEncryptAddress:   20_000,
	VerifyCiphertextAddress: 100_000,
	CastAddress:             50_000,
	ReencryptAddress:        200_000,
	OptReencryptAddress:     150_000,
	DecryptAddress:          1_000_000,

	FHESelectAddress: 300_000,
}

Gas costs for FHE operations (in gas units) These are approximate costs based on computational complexity

Functions

func Cast

func Cast(ctx *PrecompileContext, input []byte) ([]byte, error)

Cast converts a ciphertext to a different type NOTE: Cast is not directly supported by luxfi/fhe IntegerEvaluator In production, this would use BitwiseEvaluator.CastTo

func Decrypt

func Decrypt(ctx *PrecompileContext, input []byte) ([]byte, error)

Decrypt requests threshold decryption of a ciphertext Returns a placeholder - actual decryption happens asynchronously via T-Chain

func EncodeBool

func EncodeBool(b bool) []byte

EncodeBool writes a boolean result as 32-byte output

func EncodeHandle

func EncodeHandle(h Handle) []byte

EncodeHandle writes a handle to a 32-byte output slice

func EncodeUint256

func EncodeUint256(data []byte) []byte

EncodeUint256 encodes bytes as 32-byte output

func FHEAdd

func FHEAdd(ctx *PrecompileContext, input []byte) ([]byte, error)

FHEAdd performs homomorphic addition: result = a + b

func FHEAnd

func FHEAnd(ctx *PrecompileContext, input []byte) ([]byte, error)

FHEAnd performs bitwise AND: result = a & b

func FHEDiv

func FHEDiv(ctx *PrecompileContext, input []byte) ([]byte, error)

FHEDiv performs homomorphic division: result = a / b

func FHEEq

func FHEEq(ctx *PrecompileContext, input []byte) ([]byte, error)

FHEEq performs equality comparison: result = (a == b)

func FHEGe

func FHEGe(ctx *PrecompileContext, input []byte) ([]byte, error)

FHEGe performs greater-than-or-equal comparison: result = (a >= b)

func FHEGt

func FHEGt(ctx *PrecompileContext, input []byte) ([]byte, error)

FHEGt performs greater-than comparison: result = (a > b)

func FHELe

func FHELe(ctx *PrecompileContext, input []byte) ([]byte, error)

FHELe performs less-than-or-equal comparison: result = (a <= b)

func FHELt

func FHELt(ctx *PrecompileContext, input []byte) ([]byte, error)

FHELt performs less-than comparison: result = (a < b)

func FHEMax

func FHEMax(ctx *PrecompileContext, input []byte) ([]byte, error)

FHEMax returns the maximum of two encrypted values

func FHEMin

func FHEMin(ctx *PrecompileContext, input []byte) ([]byte, error)

FHEMin returns the minimum of two encrypted values

func FHEMul

func FHEMul(ctx *PrecompileContext, input []byte) ([]byte, error)

FHEMul performs homomorphic multiplication: result = a * b

func FHENe

func FHENe(ctx *PrecompileContext, input []byte) ([]byte, error)

FHENe performs not-equal comparison: result = (a != b)

func FHENeg

func FHENeg(ctx *PrecompileContext, input []byte) ([]byte, error)

FHENeg performs homomorphic negation: result = -a

func FHENot

func FHENot(ctx *PrecompileContext, input []byte) ([]byte, error)

FHENot performs bitwise NOT: result = ~a

func FHEOr

func FHEOr(ctx *PrecompileContext, input []byte) ([]byte, error)

FHEOr performs bitwise OR: result = a | b

func FHERem

func FHERem(ctx *PrecompileContext, input []byte) ([]byte, error)

FHERem performs homomorphic remainder: result = a % b

func FHERotl

func FHERotl(ctx *PrecompileContext, input []byte) ([]byte, error)

FHERotl performs left rotation: result = rotl(a, bits) NOTE: Not directly supported by luxfi/fhe library

func FHERotr

func FHERotr(ctx *PrecompileContext, input []byte) ([]byte, error)

FHERotr performs right rotation: result = rotr(a, bits) NOTE: Not directly supported by luxfi/fhe library

func FHESelect

func FHESelect(ctx *PrecompileContext, input []byte) ([]byte, error)

FHESelect performs conditional selection: result = condition ? ifTrue : ifFalse

func FHEShl

func FHEShl(ctx *PrecompileContext, input []byte) ([]byte, error)

FHEShl performs left shift: result = a << bits

func FHEShr

func FHEShr(ctx *PrecompileContext, input []byte) ([]byte, error)

FHEShr performs right shift: result = a >> bits

func FHESub

func FHESub(ctx *PrecompileContext, input []byte) ([]byte, error)

FHESub performs homomorphic subtraction: result = a - b

func FHEXor

func FHEXor(ctx *PrecompileContext, input []byte) ([]byte, error)

FHEXor performs bitwise XOR: result = a ^ b

func OptReencrypt

func OptReencrypt(ctx *PrecompileContext, input []byte) ([]byte, error)

OptReencrypt performs optimistic reencryption (faster, less secure)

func ParseUint64

func ParseUint64(input []byte, offset int) (uint64, error)

ParseUint64 extracts a uint64 from 32-byte slot (big-endian)

func Reencrypt

func Reencrypt(ctx *PrecompileContext, input []byte) ([]byte, error)

Reencrypt creates a new ciphertext encrypted for a different public key

func TrivialEncrypt

func TrivialEncrypt(ctx *PrecompileContext, input []byte) ([]byte, error)

TrivialEncrypt creates a ciphertext from a plaintext value

func VerifyCiphertext

func VerifyCiphertext(ctx *PrecompileContext, input []byte) ([]byte, error)

VerifyCiphertext verifies a ciphertext from external input

Types

type CiphertextMeta

type CiphertextMeta struct {
	Type       FheUintType
	Handle     Handle
	Owner      [20]byte // Ethereum address that created this ciphertext
	Serialized []byte   // Serialized ciphertext data
}

CiphertextMeta stores metadata about a ciphertext

type CiphertextStore

type CiphertextStore struct {
	// contains filtered or unexported fields
}

CiphertextStore manages encrypted values for FHE precompiles. Ciphertexts are stored by their handle (hash of serialized form).

Thread-safe for concurrent access from multiple EVM executions.

func NewCiphertextStore

func NewCiphertextStore() (*CiphertextStore, error)

NewCiphertextStore creates a new ciphertext store with FHE parameters.

func (*CiphertextStore) Decryptor

func (s *CiphertextStore) Decryptor() *fhe.IntegerDecryptor

Decryptor returns the FHE decryptor (for testing only)

func (*CiphertextStore) Delete

func (s *CiphertextStore) Delete(handle Handle)

Delete removes a ciphertext from the store

func (*CiphertextStore) Encryptor

func (s *CiphertextStore) Encryptor() *fhe.IntegerEncryptor

Encryptor returns the FHE encryptor

func (*CiphertextStore) Evaluator

func (s *CiphertextStore) Evaluator() *fhe.IntegerEvaluator

Evaluator returns the FHE evaluator

func (*CiphertextStore) Exists

func (s *CiphertextStore) Exists(handle Handle) bool

Exists checks if a handle exists in the store

func (*CiphertextStore) GetMeta

func (s *CiphertextStore) GetMeta(handle Handle) (*CiphertextMeta, error)

GetMeta retrieves metadata for a handle

func (*CiphertextStore) Load

func (s *CiphertextStore) Load(handle Handle) (*fhe.RadixCiphertext, error)

Load retrieves a ciphertext by handle

func (*CiphertextStore) Params

func (s *CiphertextStore) Params() *fhe.IntegerParams

Params returns the FHE parameters

func (*CiphertextStore) Store

func (s *CiphertextStore) Store(ct *fhe.RadixCiphertext, owner [20]byte) (Handle, error)

Store saves a ciphertext and returns its handle

type FheUintType

type FheUintType uint8

FheUintType represents encrypted integer types matching the Solidity interface

const (
	FheBool    FheUintType = 0
	FheUint4   FheUintType = 1
	FheUint8   FheUintType = 2
	FheUint16  FheUintType = 3
	FheUint32  FheUintType = 4
	FheUint64  FheUintType = 5
	FheUint128 FheUintType = 6
	FheUint160 FheUintType = 7 // Ethereum address
	FheUint256 FheUintType = 8
)

func ParseType

func ParseType(input []byte, offset int) (FheUintType, error)

ParseType extracts the FHE type from input (last byte of a 32-byte slot)

func (FheUintType) NumBits

func (t FheUintType) NumBits() int

NumBits returns the bit width for the type

func (FheUintType) String

func (t FheUintType) String() string

String returns the type name

func (FheUintType) ToLibType

func (t FheUintType) ToLibType() fhe.FheUintType

ToLibType converts precompile type to the fhe library type

type Handle

type Handle [32]byte

Handle is a 32-byte identifier for a stored ciphertext

func ComputeHandle

func ComputeHandle(data []byte) Handle

ComputeHandle generates a deterministic handle from ciphertext data

func ParseHandle

func ParseHandle(input []byte, offset int) (Handle, error)

ParseHandle extracts a 32-byte handle from input bytes at given offset

func (Handle) Empty

func (h Handle) Empty() bool

Empty returns true if handle is all zeros

type PrecompileContext

type PrecompileContext struct {
	Store  *CiphertextStore
	Caller [20]byte // msg.sender
}

PrecompileContext provides context for FHE precompile execution

Jump to

Keyboard shortcuts

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