Documentation
¶
Overview ¶
Package circuits provides the types and interfaces for defining, parsing and executing circuits.
Index ¶
- Variables
- func ApplyNodeMapping(nm map[string]sessions.NodeID, nids ...sessions.NodeID) (mappedNodeIds []sessions.NodeID, err error)
- func ArgumentOfType[T any](sig Signature, argName string) (arg T, err error)
- func GetProtocolSignature(t protocols.Type, in OperandLabel, params map[string]string) (pd protocols.Signature)
- func IDFromProtocolDescriptor(pd protocols.Descriptor) sessions.CircuitID
- type Circuit
- type Descriptor
- type Event
- type EventType
- type FutureOperand
- type Input
- type Metadata
- type Name
- type Operand
- type OperandLabel
- func (opl OperandLabel) CiphertextID() sessions.CiphertextID
- func (opl OperandLabel) CircuitID() sessions.CircuitID
- func (opl OperandLabel) ForCircuit(cid sessions.CircuitID) OperandLabel
- func (opl OperandLabel) ForMapping(nodeMapping map[string]sessions.NodeID) OperandLabel
- func (opl OperandLabel) HasNode(id sessions.NodeID) bool
- func (opl OperandLabel) NodeID() sessions.NodeID
- func (opl OperandLabel) SetNode(id sessions.NodeID) OperandLabel
- type Output
- type PublicKeyProvider
- type Runtime
- type Signature
- type TestRuntime
- func (tr *TestRuntime) CircuitDescriptor() Descriptor
- func (tr *TestRuntime) DEC(in Operand, rec sessions.NodeID, params map[string]string) error
- func (tr *TestRuntime) EvalLocal(needRlk bool, galKeys []uint64, f func(he.Evaluator) error) error
- func (tr *TestRuntime) Input(opl OperandLabel) *FutureOperand
- func (tr *TestRuntime) InputSum(opl OperandLabel, nodeIDs ...sessions.NodeID) *FutureOperand
- func (tr *TestRuntime) Load(_ OperandLabel) *Operand
- func (tr *TestRuntime) Logf(msg string, v ...any)
- func (tr *TestRuntime) NewOperand(opl OperandLabel) *Operand
- func (tr *TestRuntime) PCKS(in Operand, rec sessions.NodeID, params map[string]string) error
- func (tr *TestRuntime) Parameters() sessions.FHEParameters
Constants ¶
This section is empty.
Variables ¶
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") err := 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) }) if err != nil { return err } return ec.DEC(*opRes, "rec", map[string]string{ "smudging": "40.0", }) }, "bgv-add-n-dec": func(ec Runtime) error { n, err := ArgumentOfType[int](ec.CircuitDescriptor().Signature, "n") if err != nil { return err } params := ec.Parameters().(bgv.Parameters) in := make([]*FutureOperand, n) for i := 0; i < n; i++ { in[i] = ec.Input(OperandLabel(fmt.Sprintf("//p%d/in", i+1))) } opRes := ec.NewOperand("//eval/sum") err = ec.EvalLocal(false, nil, func(eval he.Evaluator) error { opRes.Ciphertext = bgv.NewCiphertext(params, 1, params.MaxLevel()) for i := 0; i < n; i++ { if err := eval.Add(in[i].Get().Ciphertext, opRes.Ciphertext, opRes.Ciphertext); err != nil { return err } } return nil }) if err != nil { return err } 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", }) }, "bgv-add-all-dec": func(ec Runtime) error { fopIn := ec.InputSum("//eval/sum") opRes := ec.NewOperand("//eval/res") opRes.Ciphertext = fopIn.Get().Ciphertext 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") err := 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) }) if err != nil { return err } 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 ApplyNodeMapping ¶ added in v0.3.0
func ArgumentOfType ¶ added in v0.3.0
ArgumentOfType returns the argument of the given type from the signature. The arguments are parsed from their string representation according to the `strconv` package. Numbers are assumed to be in base 10 representation. The function returns an error if the argument is not found or if the type conversion fails.
func GetProtocolSignature ¶
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 ¶
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.
func (Descriptor) Clone ¶ added in v0.3.0
func (d Descriptor) Clone() Descriptor
Clone returns a deep copy of the Descriptor.
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 )
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 Input ¶ added in v0.3.0
type Input struct {
OperandLabel
OperandValue any
}
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)
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 ExpandInputSumLabels ¶ added in v0.3.0
func ExpandInputSumLabels(in OperandLabel, sess *sessions.Session, cid sessions.CircuitID, nids ...sessions.NodeID) (opls []OperandLabel, err error)
func (OperandLabel) CiphertextID ¶ added in v0.3.0
func (opl OperandLabel) CiphertextID() sessions.CiphertextID
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.
func (OperandLabel) SetNode ¶ added in v0.3.0
func (opl OperandLabel) SetNode(id sessions.NodeID) OperandLabel
SetNode sets the node id part of the operand label to the given node id. It returns a new operand label with the node id part replaced by the given node id.
type Output ¶
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 {
CircuitDescriptor() Descriptor
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
// InputSum reads a multiparty input operand with the given label from the context.
// It is a special case of Input, where the input is a sum of multiple inputs from at least T parties,
// where T is the session threshold.
// When specifying the label, the user:
// - must not specify a node id; it will be automatically resolved by the runtime.
// - can omit the session-id part as it wil be automatically resolved by the runtime.
// The node id list must be either empty or contain at least T node ids. An empty list is equivalent to
// all the nodes in the session. Placeholder ids are allowed.
InputSum(OperandLabel, ...sessions.NodeID) *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
// Logf logs a message with the given format and arguments.
Logf(format string, args ...interface{})
}
Runtime defines the interface that is available to circuits to access their execution context.
type Signature ¶
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.
type TestRuntime ¶ added in v0.3.0
type TestRuntime struct {
sessions.TestSession
// contains filtered or unexported fields
}
TestRuntime is an implementation of the Runtime interface for testing purposes. It can be initialized from the FHE parameters and circuit inputs, then passed to a Circuit to execute it. It encrypts the requested inputs on-the-fly, under a test secret-key. See the Runtime interface.
func NewTestRuntime ¶ added in v0.3.0
func NewTestRuntime(tsess *sessions.TestSession, cd Descriptor, inputProvider func(OperandLabel) *rlwe.Plaintext, outputReceiver func(Output)) *TestRuntime
NewTestRuntime creates a new TestRuntime instance with the given FHE parameters and input/output functions.
func (*TestRuntime) CircuitDescriptor ¶ added in v0.3.0
func (tr *TestRuntime) CircuitDescriptor() Descriptor
func (*TestRuntime) Input ¶ added in v0.3.0
func (tr *TestRuntime) Input(opl OperandLabel) *FutureOperand
func (*TestRuntime) InputSum ¶ added in v0.3.0
func (tr *TestRuntime) InputSum(opl OperandLabel, nodeIDs ...sessions.NodeID) *FutureOperand
func (*TestRuntime) Load ¶ added in v0.3.0
func (tr *TestRuntime) Load(_ OperandLabel) *Operand
func (*TestRuntime) Logf ¶ added in v0.3.0
func (tr *TestRuntime) Logf(msg string, v ...any)
func (*TestRuntime) NewOperand ¶ added in v0.3.0
func (tr *TestRuntime) NewOperand(opl OperandLabel) *Operand
func (*TestRuntime) Parameters ¶ added in v0.3.0
func (tr *TestRuntime) Parameters() sessions.FHEParameters