heir icon indicating copy to clipboard operation
heir copied to clipboard

`convert-to-ciphertext-semantics` changes rotation behavior when the packed slot count differs from the logical tensor size

Open YGTao opened this issue 1 month ago • 2 comments

I found a wrong-result case where tensor_ext.rotate works on the original logical tensor, but changes meaning after ciphertext packing.

It looks like convert-to-ciphertext-semantics changes the tensor's shape while keeping the original rotation. When the packed slot count differs from the logical tensor size, the rotation wraps around the wrong boundary.

Reproducer

Tested with HEIR commit bc7eabdddd0085e1048c87ecaa4e1f3439810dae.

func.func @rotate_3_1(%x: tensor<3xi8> {secret.secret}) -> tensor<3xi8> {
  %shift = arith.constant 1 : index
  %y = tensor_ext.rotate %x, %shift : tensor<3xi8>, index
  return %y : tensor<3xi8>
}

Run:

heir-opt --mlir-to-secret-arithmetic=min-slot-count=4 repro.mlir

The input is packed from tensor<3xi8> into tensor<1x4xi8>, with the fourth slot set to zero. However, the generated computation still contains a rotation by one:

%1 = tensor_ext.rotate %input0, %c1 {heir.kernel_info = {gap_factor = 1 : i64, result_shape = array<i64: 3>}} : tensor<1x4xi8>, index

For input [1, 0, 0]:

Expected:

[1, 0, 0] -> [0, 0, 1]

Actual:

Packed input:  [1, 0, 0, 0]
After rotate:  [0, 0, 0, 1]
Decoded output: [0, 0, 0]

The rotation wraps around four physical slots instead of three logical elements, so the value that should appear at the last logical position is lost.

I checked all eight binary inputs; four produce incorrect results.

The same example works correctly with min-slot-count=3, but fails with 4 or 8 slots.

Also happens with multiple ciphertexts

I reproduced the same issue using an ordinary affine.for program that computes:

y[i] = x[i] + x[(i + 1) % 4]

When compiled with min-slot-count=2, HEIR packs the four-element input into two ciphertexts with two slots each. The generated computation rotates each ciphertext independently instead of rotating across the original four-element logical tensor.

For example:

Input:    [0, 0, 1, 0]
Expected: [0, 1, 1, 0]
Actual:   [0, 0, 1, 1]

Eight of the sixteen binary inputs produce incorrect results. With min-slot-count=4, the same program works correctly.

Possible cause

In ConvertToCiphertextSemantics.cpp, tensor_ext.rotate appears to go through the generic ConvertAnyAddingMaterializedAttr conversion, which changes the operand type without adjusting the rotation to match the new layout.

A logical rotation is not necessarily equivalent to a physical slot rotation after padding or splitting the tensor across multiple ciphertexts. The lowering seems to need a layout-aware rotation or remapping in these cases.

YGTao avatar Oct 28 '26 12:10 YGTao

If anything I think these inputs should produce a compiler error. tensor_ext.rotate is only considered a valid op for an input program if the program declares that it has pre-packed its input tensors (enable-arithmetization=false), but the examples here are using ciphertext-semantic operations on tensor-semantic values.

@AlexanderViand do you agree?

j2kun avatar Oct 28 '26 23:10 j2kun

  1. If anything I think these inputs should produce a compiler error. tensor_ext.rotate is only considered a valid op for an input program if the program declares that it has pre-packed its input tensors (enable-arithmetization=false), but the examples here are using ciphertext-semantic operations on tensor-semantic values. @AlexanderViand do you agree?

The original hand-written tensor_ext.rotate example was outside the supported input contract. Here is a reproducer using ordinary affine.for and tensor operations instead.

func.func @neighbor_sum(%x: tensor<16xi16> {secret.secret}) -> tensor<16xi16> {
  %one = arith.constant 1 : index
  %n = arith.constant 16 : index
  %out = affine.for %i = 0 to 16 iter_args(%a = %x) -> tensor<16xi16> {
    %j = arith.addi %i, %one : index
    %k = arith.remui %j, %n : index
    %u = tensor.extract %x[%i] : tensor<16xi16>
    %v = tensor.extract %x[%k] : tensor<16xi16>
    %w = arith.addi %u, %v : i16
    %r = tensor.insert %w into %a[%i] : tensor<16xi16>
    affine.yield %r : tensor<16xi16>
  }
  return %out : tensor<16xi16>
}

Tested on HEIR 4133db19:

heir-opt --mlir-to-secret-arithmetic='min-slot-count=8 unroll-fhe-kernel-loops=true' neighbor_sum_16.mlir

For x = [1, 2, ..., 16], the expected computation is y[i] = x[i] + x[(i+1) % 16].

          Expected    Actual
y[7]         17         9
y[15]        17        25

With min-slot-count=16, the same program produces the correct results.

The compiler generates the rotation itself in insert-rotate, but convert-to-ciphertext-semantics lowers it to a per-ciphertext rotation on tensor<2x8xi16>. Values crossing the ciphertext boundary are lost.

The same issue reproduces with 2,048 elements at min-slot-count=1024, including real Lattigo encrypted execution. Both bc7eabdd and 4133db19 exhibit the behavior.

The HECO SIMD-vectorizer documentation mentions a single-ciphertext assumption. If this multi-ciphertext configuration is unsupported by the top-level pipeline, it should be rejected rather than silently producing incorrect results. Otherwise, the lowering needs to preserve cross-ciphertext semantics.

YGTao avatar Oct 29 '26 03:10 YGTao