Lately I’ve been studying Fully Homomorphic Encryption, which is the miraculous ability to perform arbitrary computations on encrypted data without learning any information about the underlying message. It’s the most comprehensive private computing solution that can exist (and it does exist!).

The first FHE scheme by Craig Gentry was based on ideal lattices and was considered very complex (I never took the time to learn how it worked). Some later schemes (GSW = Gentry-Sahai-Waters) are based on matrix multiplication, and are conceptually much simpler. Even more recent FHE schemes build on GSW or use it as a core subroutine.

All of these schemes inject random noise into the ciphertext, and each homomorphic operation increases noise. Once the noise gets too big, you can no longer decrypt the message, and so every now and then you must apply a process called “bootstrapping” that reduces noise. It also tends to be the performance bottleneck of any FHE scheme, and this bottleneck is why FHE is not considered practical yet.

To help reduce noise growth, many FHE schemes like GSW use a technical construction dubbed the *gadget decomposition*. Despite the terribly vague name, it’s a crucial limitation on noise growth. When it shows up in a paper, it’s usually remarked as “well known in the literature,” and the details you’d need to implement it are omitted. It’s one of *those *topics.

So I’ll provide some details. The code from this post is on GitHub.

## Binary digit decomposition

To create an FHE scheme, you need to apply two homomorphic operations to ciphertexts: addition and multiplication. Most FHE schemes admit one of the two operations trivially. If the ciphertexts are numbers as in RSA, you multiply them as numbers and that multiplies the underlying messages, but addition is not known to be possible. If ciphertexts are vectors as in the “Learning With Errors” scheme (LWE)—the basis of many FHE schemes—you add them as vectors and that adds the underlying messages. (Here the “Error” in LWE is synonymous with “random noise”, I will use the term “noise”) In LWE and most FHE schemes, a ciphertext hides the underlying message by adding random noise, and addition of two ciphertexts adds the corresponding noise. After too many unmitigated additions, the noise will grow so large it obstructs the message. So you stop computing, or you apply a bootstrapping operation to reduce the noise.

Most FHE schemes also allow you to multiply a ciphertext by an unencrypted constant , but then the noise scales by a factor of , which is undesirable if is large. So you either need to limit the coefficients of your linear combinations by some upper bound, or use a version of the gadget decomposition.

The simplest version of the gadget decomposition works like this. Instead of encrypting a message , you would encrypt for some choice of , and then to multiply you write the binary digits of and you compute . If the noise in each encryption is , and summing ciphertexts sums noise, then this trick reduces the noise growth from to , at the cost of tracking ciphertexts. (Calling the noise is a bit of an abuse—in reality the error is sampled from a random distribution—but hopefully you see my point).

Some folks call the mapping , and for the sake of this article let’s call the operation of writing a number in terms of its binary digits (note, the first digit is the least-significant bit, i.e., it’s a little-endian representation). Then PowersOf2 and Bin expand an integer product into a dot product, while shifting powers of 2 from one side to the other.

This inspired the following “proof by meme” that I can’t resist including.

Working out an example, if the message is and , then and (again, little-endian), and the dot product is

## A generalized gadget construction

One can generalize the binary digit decomposition to different bases, or to vectors of messages instead of a single message, or to include a subset of the digits for varying approximations. I’ve been puzzling over an FHE scheme that does all three. In my search for clarity I came across a nice paper of Genise, Micciancio, and Polyakov called “Building an Efficient Lattice Gadget Toolkit: Subgaussian Sampling and More“, in which they state a nice general definition.

**Definition: **For any finite additive group , an –*gadget* of size and quality is a vector such that any group element can be written as an integer combination where has norm at most .

The main groups considered in my case are , where is usually or , i.e., unsigned int sizes on computers for which we get free modulus operations. In this case, a -gadget is a matrix , and the representation of satisfies .

Here and are fixed, and are traded off to make the chosen gadget scheme more efficient (smaller ) or better at reducing noise (smaller ). An example of how this could work is shown in the next section by generalizing the binary digit decomposition to an arbitrary base . This allows you to use fewer digits to represent the number , but each digit may be as large as and so the quality is .

One commonly-used construction is to convert an -gadget to an -gadget using the Kronecker product. Let be an -gadget of quality . Then the following matrix is an -gadget of size and quality :

Blank spaces represent zeros, for clarity.

An example with . The -gadget is . This has size and quality . Then for an -gadget, we construct

Now given a vector we write it as follows, where each little-endian representation is concatenated into a single vector.

And finally,

To use the definition more rigorously, if we had to write the matrix above as a gadget “vector”, it would be in column order from left to right, . Since the vector can be at worst all 1’s, its norm is at most , as claimed above.

## A signed representation in base B

As we’ve seen, the gadget decomposition trades reducing noise for a larger ciphertext size. With integers modulo , this can be fine-tuned a bit more by using a larger base. Instead of PowersOf2 we could define PowersOfB, where , such that divides . For example, with , we would only need to track 4 ciphertexts. And the gadget decomposition of the number we’re multiplying by would be the little-endian digits of its base- representation. The cost here is that the maximum entry of the decomposed representation is 255.

We can fine tune this a little bit more by using a *signed* base- representation. To my knowledge this is not the same thing as what computer programmers normally refer to as a signed integer, nor does it have anything to do with the *two’s complement* representation of negative numbers. Rather, instead of the normal base- digits for a number , the signed representation chooses .

Computing the digits is slightly more involved, and it works by shifting large coefficients by , and “absorbing” the impact of that shift into the next more significant digit. E.g., if and (all 1s up to the 10th digit), then the unsigned little-endian base- representation of is . The corresponding *signed* base- representation subtracts from the first digit, and adds 1 to the second digit, resulting in . This works in general because of the following “add zero” identity, where and are two successive unsigned digits in the unsigned base- representation of a number.

Then if , you’d repeat and carry the 1 to the next higher coefficient.

The result of all this is that the maximum absolute value of a coefficient of the signed representation is halved from the unsigned representation, which reduces the noise growth at the cost of a slightly more complex representation (from an implementation standpoint). Another side effect is that the largest representable number is less than . If you try to apply this algorithm to such a large number, the largest digit would need to be shifted, but there is no successor to carry to. Rather, if there are digits in the unsigned base- representation, the maximum number representable in the signed version has all digits set to . In our example with and 32 bits, the largest digit is 127. The formula for the max representable integer is .

```
max_digit = base // 2 - 1
max_representable = (max_digit
* (base ** (num_bits // base_log) - 1) // (base - 1)
)
```

A simple python implementation computes the signed representation, with code copied below, in which is the `base`

, and is `base_log`

.

```
def signed_decomposition(
x: int, base_log: int, total_num_bits=32) -> List[int]:
result = []
base = 1 << base_log
digit_mask = (1 << base_log) - 1
base_over_2_threshold = 1 << (base_log - 1)
carry = 0
for i in range(total_num_bits // base_log):
unsigned_digit = (x >> (i * base_log)) & digit_mask
if carry:
unsigned_digit += carry
carry = 0
signed_digit = unsigned_digit
if signed_digit >= base_over_2_threshold:
signed_digit -= base
carry = 1
result.append(signed_digit)
return result
```

In a future article I’d like to demonstrate the gadget decomposition in action in a practical setting called *key switching*, which allows one to convert an LWE ciphertext encrypted with key into an LWE ciphertext encrypted with a different key . This operation increases noise, and so the gadget decomposition is used to reduce noise growth. Key switching is used in FHE because some operations (like bootstrapping) have the side effect of switching the encryption key.

Until then!