Kernel

‘kernel’ Dialect

The Kernel dialect contains operations for high-level math kernels.

Kernel types

PreparedLinearTransformType

A linear transform prepared for evaluation by a backend

Syntax:

!kernel.prepared_linear_transform<
  int64_t,   # level
  int64_t,   # slots
  int64_t   # log_bsgs_ratio
>

The result of kernel.prepare_linear_transform: the transform’s diagonals encoded into plaintexts, ready to be applied to a ciphertext with kernel.apply_linear_transform.

Note this op assumes the implementation kernel is semantically equivalent to Halevi-Shoup, aligning with most backend implementations.

The type carries the static parameters:

  • level: the modulus level the diagonals are encoded at, which must equal the level of the ciphertext the transform is applied to. Backends are assumed to have a level-specific scaling factor.
  • slots: the ciphertext slot count the transform is encoded for.
  • log_bsgs_ratio: the log2 baby-step/giant-step ratio used to split the transform’s rotations.

The type implements PlaintextTypeInterface, so preprocessing dataflow analyses treat it like any other encoded plaintext.

Parameters:

ParameterC++ typeDescription
levelint64_t
slotsint64_t
log_bsgs_ratioint64_t

Kernel ops

kernel.apply_linear_transform (heir::kernel::ApplyLinearTransformOp)

Applies a prepared linear transform to a ciphertext.

Syntax:

operation ::= `kernel.apply_linear_transform` $input `,` $prepared prop-dict attr-dict `:` type($input) `,` type($prepared) `->` type($output)

Evaluates a transform prepared by kernel.prepare_linear_transform on a ciphertext. The prepared type’s parameters are verified against the ciphertext: applying at a level other than the one the diagonals were encoded at would silently compute with a wrongly-scaled transform, so a mismatch is a compile-time error here rather than a runtime surprise.

diagonal_indices and diagonal_width retain planning metadata needed by target lowerings after preparation and application are split across functions.

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, ElementwiseByOperandOpInterface, IncreasesMulDepthOpInterface, NoMemoryEffect (MemoryEffectOpInterface), ReducesLevelOpInterface

Effects: MemoryEffects::Effect{}

Attributes:

AttributeMLIR TypeDescription
diagonal_indices::mlir::DenseI64ArrayAttri64 dense array attribute
diagonal_width::mlir::IntegerAttr64-bit signless integer attribute

Operands:

OperandDescription
inputany non-token type
preparedA linear transform prepared for evaluation by a backend

Results:

ResultDescription
outputany non-token type

kernel.eval_chebyshev (heir::kernel::EvalChebyshevOp)

Evaluates a Chebyshev polynomial.

Syntax:

operation ::= `kernel.eval_chebyshev` $input prop-dict attr-dict `:` type($input) `->` type($output)

The kernel.eval_chebyshev operation evaluates a Chebyshev polynomial on a given input. The domain is required to be [-1, 1].

Traits: AlwaysSpeculatableImplTrait, Elementwise, Scalarizable, Tensorizable, Vectorizable

Interfaces: ConditionallySpeculatable, IncreasesMulDepthOpInterface, NoMemoryEffect (MemoryEffectOpInterface), ReducesLevelOpInterface, RequiresLinearKeyBasisOpInterface

Effects: MemoryEffects::Effect{}

Attributes:

AttributeMLIR TypeDescription
coefficients::mlir::ArrayAttrarray attribute

Operands:

OperandDescription
inputany non-token type

Results:

ResultDescription
outputany non-token type

kernel.linear_transform (heir::kernel::LinearTransformOp)

Performs a linear transform (matrix-vector multiplication) on a ciphertext.

Syntax:

operation ::= `kernel.linear_transform` $input `,` $diagonals prop-dict attr-dict `:` type($input) `,` type($diagonals) `->` type($output)

Performs a linear transform on a ciphertext using the diagonal method. Row k of the diagonals operand is the generalized diagonal named by diagonal_indices[k]. If source_row_indices is present, that diagonal is instead read from row source_row_indices[k].

When applied elementwise to a tensor of ciphertexts, the op is mapped over input only; diagonals is replicated to each application. After that mapping, input may be a single secret-typed value.

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, ElementwiseByOperandOpInterface, IncreasesMulDepthOpInterface, NoMemoryEffect (MemoryEffectOpInterface), ReducesLevelOpInterface, RequiresLinearKeyBasisOpInterface

Effects: MemoryEffects::Effect{}

Attributes:

AttributeMLIR TypeDescription
diagonal_indices::mlir::DenseI64ArrayAttri64 dense array attribute
source_row_indices::mlir::DenseI64ArrayAttri64 dense array attribute
bsgs_ratio::mlir::FloatAttr64-bit float attribute

Operands:

OperandDescription
inputranked tensor of integer, floating-point, or secret-typed values, or a single secret-typed value
diagonals2D tensor of floating-point or integer values

Results:

ResultDescription
outputany non-token type

kernel.prepare_linear_transform (heir::kernel::PrepareLinearTransformOp)

Prepares (encodes) a linear transform for later application.

Syntax:

operation ::= `kernel.prepare_linear_transform` $diagonals prop-dict attr-dict `:` type($diagonals) `->` type($prepared)

Encodes the generalized diagonals of a matrix into a backend-specific prepared transform, at the level, slot count, and BSGS split recorded in the result type. The prepared transform is applied to a ciphertext with kernel.apply_linear_transform.

Preparation is pure cleartext work (row selection, float conversion, plaintext encoding), so the op implements PlaintextEncodeOpInterface and split-preprocessing hoists it out of the hot path: an inference then only pays for the application.

As with kernel.linear_transform, row k of the diagonals operand is the generalized diagonal named by diagonal_indices[k]. If source_row_indices is present, that diagonal is instead read from row source_row_indices[k].

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface), PlaintextEncodeOpInterface

Effects: MemoryEffects::Effect{}

Attributes:

AttributeMLIR TypeDescription
diagonal_indices::mlir::DenseI64ArrayAttri64 dense array attribute
source_row_indices::mlir::DenseI64ArrayAttri64 dense array attribute

Operands:

OperandDescription
diagonals2D tensor of floating-point or integer values

Results:

ResultDescription
preparedA linear transform prepared for evaluation by a backend