Write computations with Ciphertext operators
Write a computation once as a function of slot vectors, check its encrypted error against the same function on Tensors, and compile it. The operators and their CKKS steps are defined in Ciphertext operators.
1. Write the function
import torch
from fhelium.eager import Engine
import fhelium as fh
engine = Engine(fh.Preset.slots8192_scale40_depth7)
w = torch.linspace(-0.1, 0.1, engine.num_slots, dtype=torch.float64)
def model(x, y):
return ((x * y + w) * 0.5).roll(1) - x.sum()2
3
4
5
6
7
8
9
The function uses +, -, *, unary -, .roll(shifts), .sum(), torch.roll and torch.sum, which accept Tensors and Ciphertexts alike. Tensor operands are public slot vectors of shape [*batch, slots]; Python ints multiply the residues without consuming depth; a float roll and sum act on the slot axis, passed as dims=-1 or dim=-1.
2. Compare with the Tensor reference
from fhelium.testing import compare
x = torch.rand(engine.num_slots, dtype=torch.float64)
y = torch.rand(engine.num_slots, dtype=torch.float64)
print(compare(model, x, y, engine=engine))2
3
4
5
compare encrypts each Tensor on its device, runs model on the ciphertexts and on the Tensors, and reports the maximum error and precision bits of each output. A compiled callable can replace model.
To find the operation where the error grows, set FHELIUM_SHADOW=1 (or fhelium.knobs.shadow = True). Each operator then also evaluates its Tensor reference from the decryptions of its operands and records the error of its result:
from fhelium import knobs
from fhelium.testing import clear_shadow_records, shadow_records
knobs.shadow = True
clear_shadow_records()
model(engine.encrypt_message(x), engine.encrypt_message(y))
for record in shadow_records():
print(record.operation, record.depth, f"{record.max_error:.2e}")2
3
4
5
6
7
8
Shadow execution decrypts every intermediate result with the Engine's secret key; use it for diagnosis only.
3. Mix operators with Engine methods
Operator results are ordinary Ciphertexts. Passing one to an Engine method applies its pending relinearization and rescale first, so the method receives a two-component coefficient-domain value at the reported depth and scale. Results of Engine methods are written values: operators apply to them, while Engine methods keep their own state requirements, for example engine.add raises ScaleMismatchError for different scales where + aligns them.
4. Provision keys
Operators select keys from engine.keys and, by default, generate missing ones. For a deployment that must not generate keys, read the requirements of a call and generate them during setup:
from fhelium import compile as fc
compiled = fc.compile(model)
keys = engine.create_keys(compiled.required_keys(ct_x, ct_y), device="cuda:0")2
3
4
5. Compile
compiled = fc.compile(model)
result = compiled(ct_x, ct_y)2
Capture records operators as fhelium_logical operations, and the default pipeline places relinearization, rescaling and alignment by the Eager rules. fc.compile(..., placement="all") also lets placement move relinearizations, rescales and rotations written with Engine methods; this can change ciphertext residues. Supplying pipeline= replaces the default pipeline and the placement option. The default pipeline also merges
Verify the outcome
compare reports an error consistent with the depth and scale of the outputs, and the compiled function reports the same depth and scale as the Eager function.