circuits

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package circuits provides the types and interfaces for defining, parsing and executing circuits.

Index

Constants

This section is empty.

Variables

View Source
var TestCircuits map[Name]Circuit = map[Name]Circuit{
	"bgv-add-2-dec": func(ec Runtime) error {

		params := ec.Parameters().(bgv.Parameters)

		in1, in2 := ec.Input("//p1/in"), ec.Input("//p2/in")

		opRes := ec.NewOperand("//eval/sum")
		ec.EvalLocal(false, nil, func(eval he.Evaluator) error {
			opRes.Ciphertext = bgv.NewCiphertext(params, 1, params.MaxLevel())
			return eval.Add(in1.Get().Ciphertext, in2.Get().Ciphertext, opRes.Ciphertext)
		})

		return ec.DEC(*opRes, "rec", map[string]string{
			"smudging": "40.0",
		})
	},

	"bgv-mul-2-dec": func(ec Runtime) error {

		params := ec.Parameters().(bgv.Parameters)

		in1, in2 := ec.Input("//p1/in"), ec.Input("//p2/in")

		opRes := ec.NewOperand("//eval/mul")

		err := ec.EvalLocal(true, nil, func(eval he.Evaluator) error {
			opRes.Ciphertext = bgv.NewCiphertext(params, 1, params.MaxLevel())
			return eval.MulRelin(in1.Get().Ciphertext, in2.Get().Ciphertext, opRes.Ciphertext)
		})
		if err != nil {
			return err
		}

		return ec.DEC(*opRes, "rec", map[string]string{
			"smudging": "40.0",
		})
	},
	"ckks-add-2-dec": func(ec Runtime) error {

		params := ec.Parameters().(ckks.Parameters)

		in1, in2 := ec.Input("//p1/in"), ec.Input("//p2/in")

		opRes := ec.NewOperand("//eval/sum")
		ec.EvalLocal(false, nil, func(eval he.Evaluator) error {
			opRes.Ciphertext = ckks.NewCiphertext(params, 1, params.MaxLevel())
			return eval.Add(in1.Get().Ciphertext, in2.Get().Ciphertext, opRes.Ciphertext)
		})

		return ec.DEC(*opRes, "rec", map[string]string{
			"smudging": "40.0",
		})
	},

	"ckks-mul-2-dec": func(ec Runtime) error {

		params := ec.Parameters().(ckks.Parameters)

		in1, in2 := ec.Input("//p1/in"), ec.Input("//p2/in")

		opRes := ec.NewOperand("//eval/mul")

		err := ec.EvalLocal(true, nil, func(eval he.Evaluator) error {
			opRes.Ciphertext = ckks.NewCiphertext(params, 1, params.MaxLevel())
			if err := eval.MulRelin(in1.Get().Ciphertext, in2.Get().Ciphertext, opRes.Ciphertext); err != nil {
				return err
			}
			return nil
		})
		if err != nil {
			return err
		}

		return ec.DEC(*opRes, "rec", map[string]string{
			"smudging": "10.0",
		})
	},
}

TestCircuits contains a set of test circuits for the helium framework.

Functions

func GetProtocolSignature

func GetProtocolSignature(t protocols.Type, in OperandLabel, params map[string]string) (pd protocols.Signature)

func IDFromProtocolDescriptor added in v0.2.0

func IDFromProtocolDescriptor(pd protocols.Descriptor) sessions.CircuitID

IDFromProtocolDescriptor returns the circuit ID from a protocol descriptor. // TODO cleaner way than op label ?

Types

type Circuit

type Circuit func(Runtime) error

Circuit is a type for representing circuits, which are Go functions interacting with a provided evaluation runtime.

type Descriptor

type Descriptor struct {
	Signature
	sessions.CircuitID
	NodeMapping map[string]sessions.NodeID // TODO: nil node mapping is identity mapping
	Evaluator   sessions.NodeID
}

Descriptor is a struct for specifying a circuit execution. It holds the identification information for the circuit, as well as a concrete mapping from the node ids in the circuit definition.

type Event

type Event struct {
	EventType
	Descriptor
}

Event is a type for circuit-related events.

func (Event) String

func (u Event) String() string

type EventType

type EventType int8

EventType define the type of event (see circuits.Event)

const (
	// Completed corresponds to then event of a circuit being completed.
	// It is emitted zero (failure) or one time per circuit instance.
	Completed EventType = iota
	// Started corresponds to then event of a circuit being started.
	// It is emitted once per circuit instance.
	Started
	// Executing is an event type for execution-related events.
	// This event tpe can be emitted multiple times per circuit instance,
	// for example, at each protocol Event.
	Executing
	// Failed corresponds to then event of a circuit failing to execute to completion.
	Failed
)

func (EventType) String

func (t EventType) String() string

type FutureOperand

type FutureOperand struct {
	Operand // TODO should hid this field to prevent user to access not-yet-set operands
	// contains filtered or unexported fields
}

FutureOperand is a type for operands for which the actual ciphertext is not known yet and will be set later. It enables waiting on operands.

func NewDummyFutureOperand

func NewDummyFutureOperand(opl OperandLabel) *FutureOperand

NewDummyFutureOperand creates a new future operand with the given label, but for which the ciphertext is immediatly set to nil. It is used in the context of circuit parsing.

func NewFutureOperand

func NewFutureOperand(opl OperandLabel) *FutureOperand

NewFutureOperand creates a new future operand with the given label.

func (*FutureOperand) Get

func (fo *FutureOperand) Get() Operand

Get returns the actual operand, waiting for the ciphertext to be set if necessary.

func (*FutureOperand) Set

func (fo *FutureOperand) Set(op Operand)

Set sets the actual ciphertext for the future operand and unlocks the routines waiting in the Get method.

type Metadata

type Metadata struct {
	Descriptor
	InputSet, Ops, OutputSet utils.Set[OperandLabel]
	InputsFor, OutputsFor    map[sessions.NodeID]utils.Set[OperandLabel]
	KeySwitchOps             map[string]protocols.Signature
	NeedRlk                  bool
	GaloisKeys               utils.Set[uint64]
}

Metadata is a type for gathering information about a circuit instance. It is obtained by parsing a circuit defintion (see circuits.Parse)

func Parse

func Parse(c Circuit, cd Descriptor, params sessions.FHEParameters) (*Metadata, error)

Parse parses a circuit and returns its metadata. The parsing is done by symbolic execution of the circuit.

type Name

type Name string

Name is a type for circuit names. A circuit name is a string that uniquely identifies a circuit within the framework. Multiple instances of the same circuit can exist within the system (see circuits.ID).

type Operand

type Operand struct {
	OperandLabel
	*rlwe.Ciphertext
}

Operand is a type for representing circuit operands. Each operand within a circuit must have a unique label.

type OperandLabel

type OperandLabel string

OperandLabel is a type operand labels. Operand labels have the following format:

//<node-id>/<circuit-id>/<ciphertext-id>

where:

  • node-id is the id of the node that owns the operand,
  • circuit-id is the id of the circuit in which this operand is used,
  • ciphertext-id is the id of the ciphertext within the circuit.

The circuit-id part is optional and can be omitted (1) in the context of a circuit definition, where it will be automatically expanded by the framework and (2) in the context of a global ciphertext that is available session-wide.

func (OperandLabel) CircuitID

func (opl OperandLabel) CircuitID() sessions.CircuitID

CircuitID returns the circuit id part of the operand label.

func (OperandLabel) ForCircuit

func (opl OperandLabel) ForCircuit(cid sessions.CircuitID) OperandLabel

ForCircuit returns a new operand label for the given circuit id, with the circuit id part set to cid.

func (OperandLabel) ForMapping

func (opl OperandLabel) ForMapping(nodeMapping map[string]sessions.NodeID) OperandLabel

ForMapping returns a new operand label with the node id part replaced by the corresponding value in the provided mapping.

func (OperandLabel) HasNode

func (opl OperandLabel) HasNode(id sessions.NodeID) bool

HasNode returns true if the operand label has the given host id.

func (OperandLabel) NodeID

func (opl OperandLabel) NodeID() sessions.NodeID

NodeID returns the node id part of the operand label.

type Output

type Output struct {
	sessions.CircuitID
	Operand
}

Output is a type for circuit outputs. It associates the output operand with the ID of the circuit that has produced it.

type PublicKeyProvider added in v0.2.0

type PublicKeyProvider interface {
	GetCollectivePublicKey(context.Context) (*rlwe.PublicKey, error)
	GetGaloisKey(ctx context.Context, galEl uint64) (*rlwe.GaloisKey, error)
	GetRelinearizationKey(context.Context) (*rlwe.RelinearizationKey, error)
}

PublicKeyProvider is an interface for querying public encryption- and evaluation-keys. The setup service is a notable implementation of this interface.

type Runtime

type Runtime interface {
	Parameters() sessions.FHEParameters

	// Input reads an input operand with the given label from the context.
	// When specifying the label, the user:
	//  - can use a placeholder id for the node, the mapping for which is provided in the Signature.
	//  - can omit the session-id part as it wil be automatically resolved by the runtime.
	Input(OperandLabel) *FutureOperand

	// Load reads an existing ciphertext in the session
	// When specifying the label, the user:
	//  - can use a placeholder id for the node, the mapping for which is provided in the Signature.
	//  - can omit the session-id part as it wil be automatically resolved by the runtime.
	Load(OperandLabel) *Operand

	// NewOperand creates a new operand with the given label.
	// When specifying the label, the user:
	//  - can use a placeholder id for the node, the mapping for which is provided in the Signature.
	//  - can omit the session-id part as it wil be automatically resolved by the runtime.
	NewOperand(OperandLabel) *Operand

	// EvalLocal is used to perform local operation on the ciphertext. This is where the FHE computation
	// is performed. The user must specify the required evaluation keys needed by the function. The provided
	// function must not call any other Runtime function (ie., it must be  strictly local circuit).
	EvalLocal(needRlk bool, galKeys []uint64, f func(he.Evaluator) error) error

	// DEC performes the decryption of in, with private output to rec.
	// The decrypted operand is considered an output for the this circuit and the
	// given reciever.
	// It expect the users to provide the decryption parameters, including the level
	// at which the operation is performed and the smudging parameter.
	DEC(in Operand, rec sessions.NodeID, params map[string]string) error

	// PCKS performes the re-encryption of in to the public key of rec.
	// The the re-encrypted operand is considered an output for the this circuit and the
	// given reciever.
	// It expect the users to provide the key-switch parameters, including the level
	// at which the operation is performed and the smudging parameter.
	PCKS(in Operand, rec sessions.NodeID, params map[string]string) error
}

Runtime defines the interface that is available to circuits to access their execution context.

type Signature

type Signature struct {
	Name
	Args map[string]string
}

Signature is a type for circuit signatures. A circuit signature is akin to a function signature in a programming language: it associates the name of the circuit with a set of arguments.

Jump to

Keyboard shortcuts

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