fhelium.ir.dialects.rns
Logical residue-number-system operations used below CKKS lowering.
A residue number system (RNS) stores a polynomial row modulo each active ciphertext prime in Q. The QP basis appends special P primes used by hybrid key switching. A hybrid digit is one selected group of Q rows lifted together during that procedure. Montgomery representation stores residue
Each operation defines one complete transformation of an RNS bundle. The operations contain no CPU, CUDA, Triton, kernel, radix, or fusion selection. Their parameter and table operands carry numerical Tensors supplied by Eager or bound to a Program before execution.
AddStandardOp
class View source
AddStandardOp(lhs: SSAValue | Operation, rhs: SSAValue | Operation, parameters: SSAValue | Operation, result_type: Attribute, *, attributes: Mapping[str, Attribute] | None=None)Bases: IRDLOperation
Add two standard-residue polynomial bundles row by row.
An RNS (residue number system) row stores one polynomial modulo an active prime
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.add_standard' | |
lhs | operand_def(RnsBundleType) | |
rhs | operand_def(RnsBundleType) | |
parameters | operand_def() | |
result | result_def(RnsBundleType) | |
traits | traits_def(Pure()) |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe result reads in this operation's value coordinates.
AddPlaintextOp
class View source
AddPlaintextOp()Bases: _CiphertextPlaintextOp
Add a prepared plaintext polynomial to ciphertext component zero.
For ciphertext
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.add_plaintext' | |
polynomial_domain | attr_def(StringAttr, default_value=StringAttr('coefficient')) |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe result reads in this operation's value coordinates.
SumStandardBatchOp
class View source
SumStandardBatchOp(value: SSAValue | Operation, parameters: SSAValue | Operation, result_type: Attribute, *, dim: int | IntegerAttr)Bases: IRDLOperation
Reduce one leading batch axis of a standard-residue bundle.
The operand is dim selects the reduced axis of length
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.sum_standard_batch' | |
value | operand_def(RnsBundleType) | |
parameters | operand_def() | |
result | result_def(RnsBundleType) | |
dim | attr_def(IntegerAttr) | |
traits | traits_def(Pure()) |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe result reads in this operation's value coordinates.
AddMontgomeryLazyOp
class View source
AddMontgomeryLazyOp(lhs: SSAValue | Operation, rhs: SSAValue | Operation, parameters: SSAValue | Operation, result_type: Attribute)Bases: IRDLOperation
Accumulate two Montgomery bundles without full standard reduction.
For each active prime
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.add_montgomery_lazy' | |
lhs | operand_def(RnsBundleType) | |
rhs | operand_def(RnsBundleType) | |
parameters | operand_def() | |
result | result_def(RnsBundleType) | |
traits | traits_def(Pure()) |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe result reads in this operation's value coordinates.
CoefficientAutomorphismOp
class View source
CoefficientAutomorphismOp(value: SSAValue | Operation, parameters: SSAValue | Operation, result_type: Attribute, *, galois_element: int | IntegerAttr)Bases: IRDLOperation
Apply the negacyclic ring automorphism
For odd galois_element
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.coefficient_automorphism' | |
value | operand_def(RnsBundleType) | |
parameters | operand_def() | |
result | result_def(RnsBundleType) | |
galois_element | attr_def(IntegerAttr) | |
traits | traits_def(Pure()) |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe result reads in this operation's value coordinates.
verify_
method
def verify_() -> None: ...ExtractComponentOp
class View source
ExtractComponentOp(value: SSAValue | Operation, result_type: Attribute, *, component: int | IntegerAttr)Bases: IRDLOperation
Select one ciphertext-component polynomial without arithmetic.
For an RNS bundle component=j returns
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.extract_component' | |
value | operand_def(RnsBundleType) | |
result | result_def(RnsBundleType) | |
component | attr_def(IntegerAttr) | |
traits | traits_def(Pure()) |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe result reads in this operation's value coordinates.
verify_
method
def verify_() -> None: ...NegateStandardOp
class View source
NegateStandardOp(value: SSAValue | Operation, parameters: SSAValue | Operation, result_type: Attribute)Bases: IRDLOperation
Negate a standard-residue polynomial bundle row by row.
For every active prime
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.negate_standard' | |
value | operand_def(RnsBundleType) | |
parameters | operand_def() | |
result | result_def(RnsBundleType) | |
traits | traits_def(Pure()) |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe result reads in this operation's value coordinates.
HybridModUpDigitOp
class View source
HybridModUpDigitOp(source: SSAValue | Operation, parameters: Sequence[SSAValue | Operation], result_type: Attribute, *, attributes: Mapping[str, Attribute] | None=None, digit_index: int=0)Bases: IRDLOperation
Extend one hybrid-RNS digit from active Q rows to active QP rows.
A hybrid digit is a selected group of Q-prime residues used in key switching. For each coefficient, the selected rows are reconstructed as one integer class
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.hybrid_modup_digit' | |
source | operand_def(RnsBundleType) | |
parameters | var_operand_def() | |
result | result_def(RnsBundleType) | |
digit_index | attr_def(IntegerAttr) | |
traits | traits_def(Pure()) |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe result reads in this operation's value coordinates.
verify_
method
def verify_() -> None: ...KeySwitchDigitProductOp
class View source
KeySwitchDigitProductOp(digit: SSAValue | Operation, key: SSAValue | Operation, parameters: SSAValue | Operation, result_type: Attribute, *, key_digit_index: int, key_row_start: int=0)Bases: IRDLOperation
Multiply one lifted digit by its evaluation-key pair.
For NTT/Montgomery digit
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.key_switch_digit_product' | |
digit | operand_def(RnsBundleType) | |
key | operand_def() | |
parameters | operand_def() | |
result | result_def(RnsBundleType) | |
key_digit_index | attr_def(IntegerAttr) | |
key_role | opt_attr_def(StringAttr) | |
traits | traits_def(Pure()) |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe result reads in this operation's value coordinates.
verify_
method
def verify_() -> None: ...ModDownQpToQOp
class View source
ModDownQpToQOp(value: SSAValue | Operation, parameters: Sequence[SSAValue | Operation], result_type: Attribute, *, attributes: Mapping[str, Attribute] | None=None)Bases: IRDLOperation
Remove the special-prime product P from a key-switch accumulator.
Let
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.moddown_qp_to_q' | |
value | operand_def(RnsBundleType) | |
parameters | var_operand_def() | |
result | result_def(RnsBundleType) | |
traits | traits_def(Pure()) |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe result reads in this operation's value coordinates.
ModDownNttQpToQOp
class View source
ModDownNttQpToQOp(value: SSAValue | Operation, parameters: Sequence[SSAValue | Operation], result_type: Attribute, *, attributes: Mapping[str, Attribute] | None=None)Bases: IRDLOperation
Remove P from two QP accumulators and retain NTT/Montgomery Q rows.
The input has shape [2, *batch, |Q|+|P|, N]; the result has shape [2, *batch, |Q|, N]. The NTT-plan operand supplies the transforms for the same RNS context as the parameters and key-switch plan.
Let
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.moddown_ntt_qp_to_q' | |
value | operand_def(RnsBundleType) | |
parameters | var_operand_def() | |
result | result_def(RnsBundleType) | |
traits | traits_def(Pure()) |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe result reads in this operation's value coordinates.
MontgomeryToStandardOp
class View source
MontgomeryToStandardOp()Bases: _ResidueConversionOp
Remove the Montgomery factor from coefficient residues.
For every active prime
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.montgomery_to_standard' |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe result reads in this operation's value coordinates.
MontgomeryMultiplyOp
class View source
MontgomeryMultiplyOp(lhs: SSAValue | Operation, rhs: SSAValue | Operation, parameters: SSAValue | Operation, result_type: Attribute)Bases: IRDLOperation
Multiply NTT/Montgomery polynomial bundles pointwise.
For active prime
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.montgomery_multiply' | |
lhs | operand_def(RnsBundleType) | |
rhs | operand_def(RnsBundleType) | |
parameters | operand_def() | |
result | result_def(RnsBundleType) | |
traits | traits_def(Pure()) |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe result reads in this operation's value coordinates.
MontgomeryWeightedSumOp
class View source
MontgomeryWeightedSumOp(parameters: SSAValue | Operation, ciphertexts: tuple[SSAValue | Operation, ...], plaintexts: tuple[SSAValue | Operation, ...], result_type: Attribute)Bases: IRDLOperation
Sum ciphertext/plaintext products in NTT/Montgomery representation.
For
Every term uses the same component and batch shape, depth, prime rows, ciphertext scale, and plaintext scale. term_count separates the two equal operand ranges. The result keeps the ciphertext shape, depth, and NTT/Montgomery representation; its CKKS scale is the product of the common ciphertext and plaintext scales. The operation chooses no grouping, platform, kernel, or execution schedule.
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.montgomery_weighted_sum' | |
parameters | operand_def() | |
terms | var_operand_def(RnsBundleType) | |
result | result_def(RnsBundleType) | |
term_count | attr_def(IntegerAttr) | |
traits | traits_def(Pure()) |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe each term's NTT positions and its output component mapping.
verify_
method
def verify_() -> None: ...MontgomeryWeightedSumsOp
class View source
MontgomeryWeightedSumsOp(parameters: SSAValue | Operation, ciphertexts: tuple[SSAValue | Operation, ...], plaintexts: tuple[SSAValue | Operation, ...], result_type: Attribute, *, group_count: int)Bases: IRDLOperation
Apply a dense plaintext matrix to shared ciphertext terms.
For
Operands after parameters contain the term_count and group_count describe the supplied matrix; the operation does not choose its groups, platform, kernel, or execution schedule.
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.montgomery_weighted_sums' | |
parameters | operand_def() | |
terms | var_operand_def(RnsBundleType) | |
result | result_def(RnsBundleType) | |
term_count | attr_def(IntegerAttr) | |
group_count | attr_def(IntegerAttr) | |
traits | traits_def(Pure()) |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe each term's NTT positions and its output component mapping.
verify_
method
def verify_() -> None: ...MultiplyPlaintextOp
class View source
MultiplyPlaintextOp()Bases: _CiphertextPlaintextOp
Multiply every ciphertext component by a prepared plaintext.
The ciphertext and plaintext are NTT-domain Montgomery bundles on matching prime rows. For each component
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.multiply_plaintext' |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe result reads in this operation's value coordinates.
OPERATION_SPECS
constant View source
OPERATION_SPECS: tuple[OperationSpec, ...] = tuple((registered_operation_spec(operation_type, 'rns', validator=flat_with_attributes(*_RNS_ATTRIBUTES[operation_type]) if operation_type in _RNS_ATTRIBUTES else flat_without_attributes) for operation_type in _RNS_OPERATION_TYPES))PackTwoComponentsOp
class View source
PackTwoComponentsOp(component0: SSAValue | Operation, component1: SSAValue | Operation, result_type: Attribute)Bases: IRDLOperation
Stack two polynomial bundles as
No residue arithmetic is performed. Both inputs retain their active prime rows and representation; a new leading component axis forms a two-component ciphertext-shaped bundle.
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.pack_two_components' | |
component0 | operand_def(RnsBundleType) | |
component1 | operand_def(RnsBundleType) | |
result | result_def(RnsBundleType) | |
traits | traits_def(Pure()) |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe result reads in this operation's value coordinates.
PackThreeComponentsOp
class View source
PackThreeComponentsOp(component0: SSAValue | Operation, component1: SSAValue | Operation, component2: SSAValue | Operation, result_type: Attribute)Bases: IRDLOperation
Stack three polynomial bundles as
No residue arithmetic is performed. Matching prime rows and representation are preserved while a new leading component axis forms a three-component ciphertext-shaped bundle.
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.pack_three_components' | |
component0 | operand_def(RnsBundleType) | |
component1 | operand_def(RnsBundleType) | |
component2 | operand_def(RnsBundleType) | |
result | result_def(RnsBundleType) | |
traits | traits_def(Pure()) |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe result reads in this operation's value coordinates.
SubtractStandardOp
class View source
SubtractStandardOp(lhs: SSAValue | Operation, rhs: SSAValue | Operation, parameters: SSAValue | Operation, result_type: Attribute)Bases: IRDLOperation
Subtract standard-residue polynomial bundles row by row.
For each active prime
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.subtract_standard' | |
lhs | operand_def(RnsBundleType) | |
rhs | operand_def(RnsBundleType) | |
parameters | operand_def() | |
result | result_def(RnsBundleType) | |
traits | traits_def(Pure()) |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe result reads in this operation's value coordinates.
FHEliumRns
data View source
FHEliumRns = Dialect('fhelium_rns', [*_RNS_OPERATION_TYPES], [RnsBundleType, RnsParametersType])RescaleDropLeadingPrimesOp
class View source
RescaleDropLeadingPrimesOp(value: SSAValue | Operation, parameters: Sequence[SSAValue | Operation], result_type: Attribute, *, drop_count: int=1, rounding: str | StringAttr='nearest', polynomial_domain: Literal['coefficient', 'ntt']='coefficient', attributes: Mapping[str, Attribute] | None=None)Bases: IRDLOperation
Divide-round by the product of a leading prime group and remove its rows.
For the first drop_count primes, let
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.rescale_drop_leading_primes' | |
value | operand_def(RnsBundleType) | |
parameters | var_operand_def() | |
result | result_def(RnsBundleType) | |
drop_count | attr_def(IntegerAttr, default_value=IntegerAttr(1, 64)) | |
rounding | opt_attr_def(StringAttr) | |
input_domain | attr_def(StringAttr, default_value=StringAttr('coefficient')) | |
output_domain | attr_def(StringAttr, default_value=StringAttr('coefficient')) | |
traits | traits_def(Pure()) |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe the rounded quotient in its represented polynomial domain.
verify_
method
def verify_() -> None: ...Accept only the represented CKKS quotient-rounding rules.
RestrictDepthOp
class View source
RestrictDepthOp(value: SSAValue | Operation, result_type: Attribute, *, target_depth: int | IntegerAttr)Bases: IRDLOperation
Select the suffix of prime rows belonging to a later depth.
If depth
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.restrict_depth' | |
value | operand_def(RnsBundleType) | |
result | result_def(RnsBundleType) | |
target_depth | attr_def(IntegerAttr) | |
traits | traits_def(Pure()) |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe result reads in this operation's value coordinates.
ReinterpretScaleOp
class View source
ReinterpretScaleOp(value: SSAValue | Operation, result_type: Attribute, *, scale: float | FloatAttr)Bases: IRDLOperation
Replace scale metadata without changing an RNS payload.
The result refers to the same residue classes and polynomial representation but records the supplied actual scale
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.reinterpret_scale' | |
value | operand_def(RnsBundleType) | |
result | result_def(RnsBundleType) | |
scale | attr_def(FloatAttr) | |
traits | traits_def(Pure()) |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe result reads in this operation's value coordinates.
RnsBundleType
class View source
RnsBundleType()Bases: OpenStateType
RNS components with partial arithmetic, layout, and placement state.
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.bundle' |
RnsParametersType
class View source
RnsParametersType()Bases: OpenStateType
CKKS-parameter-specific RNS moduli and Montgomery parameters.
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.parameters' |
StandardToMontgomeryOp
class View source
StandardToMontgomeryOp()Bases: _ResidueConversionOp
Convert coefficient residues to Montgomery representation.
For every active prime
Attributes
| Name | Type | Default/value |
|---|---|---|
name | 'fhelium_rns.standard_to_montgomery' |
dependencies
method
def dependencies() -> OperationDependencies: ...Describe result reads in this operation's value coordinates.