fhelium.algorithms
Homomorphic polynomial evaluation and slot-wise linear transforms.
The package provides mathematical objects (Polynomial, LinearTransform), frozen evaluation plans that report consumed depth, products and rotation steps, and functions that evaluate these objects on a ciphertext with an explicit fhelium.eager.Engine. Every function is composed of ordinary Eager operations, so fhelium.compile captures it. Plan objects implement evaluate(arithmetic, ...), which receives an Arithmetic carrying the per-depth target scales; a custom plan following the same protocol can replace a built-in one.
Arithmetic
class View source
Arithmetic(engine: Engine, constant_cache: MutableMapping[object, object] | None = None, retain_ntt: bool = True, basis_conversion: Literal['exact', 'crt', 'normalized_crt'] = 'exact')CKKS operations that evaluation components use to reach per-depth target scales.
Polynomial plans, linear-transform plans and periodic reductions receive an Arithmetic as the first argument of their evaluate method and perform their scalar, constant, product and depth operations through it. target_scales[d] is the target engine.config.scale_at(engine.max_depth), where engine.config.rescale_divisor(d). With these targets:
multiply_scalar,weighted_scalar_sumandadvance_depthmove depth and return actual scale ; the plaintext scale is for input scale (plaintext_scale);multiply_relinearize_rescaleandrelinearize(..., rescale=True)move with actual scale ;add_scalarandmultiply_by_monomialkeep depth, scale, domain and basis;align_depthsadvances the shallower operand.
Every rescale records its actual quotient scale. for_input and scaled_targets derive views with other targets for intermediate values. engine exposes the remaining Eager operations (add, rotate, conjugate and so on).
Keys and encoded constants pass through the object, so the same evaluate call serves both preparation and prepared execution: during fhelium.bootstrap.prepare and fhelium.algorithms.linear.prepare a relinearization inventory selects and records one key per depth and constant_cache records each scalar plaintext with its first stochastic encoding sample; prepared calls read the recorded entries and encode nothing. A component therefore obtains keys and constants only through its arguments and these methods. Scalar preparation requires max(abs(real(c)), abs(imag(c))) * scale < 2**63; addition uses the ciphertext scale and multiplication its selected plaintext scale, and values outside this bound raise ValueError. retain_ntt=False returns coefficient-domain results, and basis_conversion is forwarded to relinearization.
Attributes
| Name | Type | Default/value |
|---|---|---|
engine | Engine | |
constant_cache | MutableMapping[object, object] | None | None |
retain_ntt | bool | True |
basis_conversion | Literal['exact', 'crt', 'normalized_crt'] | 'exact' |
target_scales
property
target_scales: tuple[float, ...]Return the selected per-depth arithmetic scales.
The default selection is computed backward from the terminal default scale using for_input follows an actual input scale forward over the requested depths. scaled_targets instead describes coefficient accumulators multiplied by unscaled basis nodes.
for_input
method
def for_input(ciphertext: Ciphertext, required_depths: int) -> Arithmetic: ...Follow
Polynomial basis nodes use this recurrence through their required depths; coefficient products may select a different output scale.
scaled_targets
method
def scaled_targets(factor: float) -> Arithmetic: ...Use a common scale factor for polynomial coefficient accumulators.
plaintext_scale
method
def plaintext_scale(input_scale: float, depth: int) -> float: ...Choose
multiply_by_monomial
method
def multiply_by_monomial(ciphertext: Ciphertext, exponent: int) -> Ciphertext: ...Return
align_depths
method
def align_depths(lhs: Ciphertext, rhs: Ciphertext) -> tuple[Ciphertext, Ciphertext]: ...Advance the shallower value to the deeper value's Q depth.
advance_depth
method
def advance_depth(ciphertext: Ciphertext) -> Ciphertext: ...Advance one depth and return at the next target scale.
relinearize
method
def relinearize(ciphertext: Ciphertext, key: RelinearizationKey | EvaluationKeySet | Mapping[int, RelinearizationKey], *, output_domain: Literal['coefficient', 'ntt']='coefficient', rescale: bool=False) -> Ciphertext: ...Reduce
A primitive key is consumed directly. During material preparation an inventory selects a key covering the product's Q rows. Prepared execution reads its selected per-depth references. The basis conversion and requested polynomial representation are forwarded to the Eager operation. Without rescaling, Q depth and actual scale are preserved. With rescale=True, the operation advances depth once and returns actual scale
multiply_relinearize_rescale
method
def multiply_relinearize_rescale(lhs: Ciphertext, rhs: Ciphertext, *, relinearization_key: RelinearizationKey | EvaluationKeySet | Mapping[int, RelinearizationKey]) -> Ciphertext: ...Return relinearized
multiply_scalar
method
def multiply_scalar(ciphertext: Ciphertext, scalar: complex) -> Ciphertext: ...Multiply by one encoded scalar, rescale, and return target scale.
weighted_scalar_sum
method
def weighted_scalar_sum(terms: Sequence[tuple[Ciphertext, complex]]) -> Ciphertext: ...Return
add_scalar
method
def add_scalar(ciphertext: Ciphertext, scalar: complex) -> Ciphertext: ...Add a scalar without changing depth, scale, domain, or RNS basis.
LinearTransform
class View source
LinearTransform(diagonals: Mapping[int, ArrayLike], slots: int, name: str = 'diagonal_linear_transform')An immutable cyclic-diagonal linear map over packed CKKS slots.
The map is
Each stored diagonal is a CPU complex128 NumPy vector with shape [slot]; reference accepts and returns the same one-dimensional shape. An evaluator chooses the execution algorithm for these diagonals. Offsets are cyclic modulo -1 and slots - 1 are combined by normalized_diagonals.
Attributes
| Name | Type | Default/value |
|---|---|---|
diagonals | Mapping[int, ArrayLike] | |
slots | int | |
name | str | 'diagonal_linear_transform' |
normalized_diagonals
method
def normalized_diagonals() -> dict[int, np.ndarray]: ...Map every offset to
The stored arrays remain immutable. A new mapping is returned because two input offsets can normalize to the same cyclic key and must then be added elementwise. Returned vectors retain shape [slot].
reference
method
def reference(values: ArrayLike) -> np.ndarray: ...Apply
values must have shape [slot]. The returned CPU complex128 array has shape [slot]. This plaintext oracle does not encode, rescale, consume depths, or model CKKS error.
Polynomial
class View source
Polynomial(basis: PolynomialBasis, coefficients: tuple[complex, ...], domain: tuple[float, float] = (-1.0, 1.0), name: str = 'polynomial', max_error: float | None = None)An immutable polynomial produced independently of its evaluation DAG.
The coefficient convention is ascending degree. For basis="power",
while basis="chebyshev" means
domain=(a, b) records the physical interval used to design the approximation. The evaluator input is nevertheless normalized (a, b) == (-1, 1); the caller owns the affine map.
Attributes
basis: Basis in whichcoefficientsare expressed.coefficients: Ascending coefficients: entryimultiplies either or .domain: Plaintext interval on which the approximation was designed.name: Human-readable diagnostic name.max_error: Optional sampled or certified approximation error.
Attributes
| Name | Type | Default/value |
|---|---|---|
basis | PolynomialBasis | |
coefficients | tuple[complex, ...] | |
domain | tuple[float, float] | (-1.0, 1.0) |
name | str | 'polynomial' |
max_error | float | None | None |
degree
property
degree: intReturn the algebraic degree including trailing zero entries.
evaluate_plaintext
method
def evaluate_plaintext(values: np.ndarray) -> np.ndarray: ...Evaluate
values may have any NumPy-broadcastable shape, which is preserved in the output. They are coordinates in the polynomial's basis domain. For a Chebyshev approximation created on a physical interval other than
chebyshev_fit
function View source
def chebyshev_fit(function: Callable[[np.ndarray], np.ndarray], degree: int, *, domain: tuple[float, float]=(-1.0, 1.0), name: str='polynomial') -> Polynomial: ...Interpolate function at the
The returned coefficients are in the Chebyshev basis of the normalized coordinate domain=(a, b); max_error records the sampled maximum deviation on the interval.
linear
module
fhelium.algorithms.linear
polynomial
module
fhelium.algorithms.polynomial