Skip to content

Arithmetic

Bounded scalar comparison and division circuits from the package root.

concrete_fhe_toolkit.arithmetic

Bounded scalar arithmetic operations for Concrete FHE.

compare

compare(x: Any, y: Any) -> Any

Return 1 when x > y, 0 when equal, and -1 when x < y.

Parameters:

Name Type Description Default
x Any

The first encrypted operand.

required
y Any

The second encrypted operand.

required

Returns:

Name Type Description
Any Any

1 if x > y, 0 if x == y, and -1 if x < y.

Example
from concrete_fhe_toolkit import compare

print(compare(10, 5))  # 1
print(compare(5, 5))   # 0
Source code in src/concrete_fhe_toolkit/arithmetic.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def compare(x: Any, y: Any) -> Any:
    """Return 1 when x > y, 0 when equal, and -1 when x < y.

    Args:
        x: The first encrypted operand.
        y: The second encrypted operand.

    Returns:
        Any: 1 if x > y, 0 if x == y, and -1 if x < y.

    Example:
        ```python
        from concrete_fhe_toolkit import compare

        print(compare(10, 5))  # 1
        print(compare(5, 5))   # 0
        ```
    """
    difference = x - y
    return 1 * (difference > 0) - 1 * (difference < 0)

compile_compare

compile_compare(min_value: int = -15, max_value: int = 15, *, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile a sign-comparison circuit with inclusive input bounds.

Parameters:

Name Type Description Default
min_value int

The lower bound for the encrypted inputs, used to dimension the FHE circuit.

-15
max_value int

The upper bound for the encrypted inputs, used to dimension the FHE circuit.

15
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit for sign-comparison.

Example
from concrete_fhe_toolkit import compile_compare

circuit = compile_compare(min_value=-10, max_value=10)
print(circuit.encrypt_run_decrypt(5, 2))  # 1
Source code in src/concrete_fhe_toolkit/arithmetic.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def compile_compare(
    min_value: int = -15,
    max_value: int = 15,
    *,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile a sign-comparison circuit with inclusive input bounds.

    Args:
        min_value (int): The lower bound for the encrypted inputs, used to dimension the FHE circuit.
        max_value (int): The upper bound for the encrypted inputs, used to dimension the FHE circuit.
        configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

    Returns:
        fhe.Circuit: The compiled FHE circuit for sign-comparison.

    Example:
        ```python
        from concrete_fhe_toolkit import compile_compare

        circuit = compile_compare(min_value=-10, max_value=10)
        print(circuit.encrypt_run_decrypt(5, 2))  # 1
        ```
    """
    minimum, maximum = validate_bounds(min_value, max_value)
    inputset = [
        (minimum, minimum),
        (minimum, maximum),
        (maximum, minimum),
        (maximum, maximum),
    ]
    return compile_function(
        compare,
        {"x": "encrypted", "y": "encrypted"},
        inputset,
        configuration,
    )

sign

sign(x: Any) -> Any

Return the sign of a number (1 if positive, -1 if negative, 0 if zero).

Parameters:

Name Type Description Default
x Any

The encrypted operand.

required

Returns:

Name Type Description
Any Any

1 if positive, -1 if negative, 0 if zero.

Example
from concrete_fhe_toolkit import sign

print(sign(-42))  # -1
Source code in src/concrete_fhe_toolkit/arithmetic.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def sign(x: Any) -> Any:
    """Return the sign of a number (1 if positive, -1 if negative, 0 if zero).

    Args:
        x (Any): The encrypted operand.

    Returns:
        Any: 1 if positive, -1 if negative, 0 if zero.

    Example:
        ```python
        from concrete_fhe_toolkit import sign

        print(sign(-42))  # -1
        ```
    """
    return compare(x,0)

compile_sign

compile_sign(min_value: int = -15, max_value: int = 15, *, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile a circuit returning the sign (-1, 0, or 1) of one encrypted input.

Parameters:

Name Type Description Default
min_value int

The lower bound for the encrypted inputs, used to dimension the FHE circuit.

-15
max_value int

The upper bound for the encrypted inputs, used to dimension the FHE circuit.

15
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit returning the sign of the input.

Example
from concrete_fhe_toolkit import compile_sign

circuit = compile_sign(min_value=-5, max_value=5)
print(circuit.encrypt_run_decrypt(-3))  # -1
Source code in src/concrete_fhe_toolkit/arithmetic.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
def compile_sign(
        min_value: int = -15,
        max_value: int = 15,
        *,
        configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile a circuit returning the sign (-1, 0, or 1) of one encrypted input.

    Args:
        min_value (int): The lower bound for the encrypted inputs, used to dimension the FHE circuit.
        max_value (int): The upper bound for the encrypted inputs, used to dimension the FHE circuit.
        configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

    Returns:
        fhe.Circuit: The compiled FHE circuit returning the sign of the input.

    Example:
        ```python
        from concrete_fhe_toolkit import compile_sign

        circuit = compile_sign(min_value=-5, max_value=5)
        print(circuit.encrypt_run_decrypt(-3))  # -1
        ```
    """
    minimum, maximum = validate_bounds(min_value, max_value)
    inputset = [minimum, maximum]
    if minimum <= 0 <= maximum:
        inputset.insert(1, 0)
    return compile_function(
        sign,
        {"x": "encrypted"},
        inputset,
        configuration,
    )

make_floor_divide

make_floor_divide(*, zero_result: int = 0) -> BinaryFunction

Create exact encrypted floor division using a multivariate table lookup.

Parameters:

Name Type Description Default
zero_result int

Result to return when the operation is undefined (e.g., division by zero).

0

Returns:

Name Type Description
BinaryFunction BinaryFunction

A function that performs encrypted floor division.

Example
from concrete_fhe_toolkit import make_floor_divide

divide = make_floor_divide(zero_result=0)
# Use `divide(x, y)` inside a larger FHE program compilation
Source code in src/concrete_fhe_toolkit/arithmetic.py
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
def make_floor_divide(*, zero_result: int = 0) -> BinaryFunction:
    """Create exact encrypted floor division using a multivariate table lookup.

    Args:
        zero_result (int): Result to return when the operation is undefined (e.g., division by zero).

    Returns:
        BinaryFunction: A function that performs encrypted floor division.

    Example:
        ```python
        from concrete_fhe_toolkit import make_floor_divide

        divide = make_floor_divide(zero_result=0)
        # Use `divide(x, y)` inside a larger FHE program compilation
        ```
    """
    zero_result = validate_integer("zero_result", zero_result)

    def clear_floor_divide(numerator: Any, denominator: Any) -> Any:
        """Perform floor division with zero-handling fallback.

        Args:
            numerator: The encrypted dividend/numerator.
            denominator: The encrypted divisor/denominator.

        Returns:
            The quotient or the zero fallback result.
        """
        safe_denominator = np.where(denominator == 0, 1, denominator)
        quotient = np.floor_divide(numerator, safe_denominator)
        return np.asarray(
            np.where(denominator == 0, zero_result, quotient),
            dtype=np.int64,
        )

    operation = fhe.multivariate(clear_floor_divide)

    def floor_divide(numerator: Any, denominator: Any) -> Any:
        """Perform encrypted floor division.

        Args:
            numerator: The encrypted dividend/numerator.
            denominator: The encrypted divisor/denominator.

        Returns:
            Encrypted quotient.
        """
        return operation(numerator, denominator)

    return floor_divide

make_floor_divide_by_product

make_floor_divide_by_product(*, zero_result: int = 0) -> TernaryFunction

Create numerator // (left * right) using a multivariate table lookup.

Parameters:

Name Type Description Default
zero_result int

Result to return when the operation is undefined (e.g., division by zero).

0

Returns:

Name Type Description
TernaryFunction TernaryFunction

A function that computes numerator // (left * right).

Example
from concrete_fhe_toolkit import make_floor_divide_by_product

div_prod = make_floor_divide_by_product(zero_result=0)
# Use `div_prod(num, a, b)` inside a larger FHE program compilation
Source code in src/concrete_fhe_toolkit/arithmetic.py
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
def make_floor_divide_by_product(*, zero_result: int = 0) -> TernaryFunction:
    """Create numerator // (left * right) using a multivariate table lookup.

    Args:
        zero_result (int): Result to return when the operation is undefined (e.g., division by zero).

    Returns:
        TernaryFunction: A function that computes numerator // (left * right).

    Example:
        ```python
        from concrete_fhe_toolkit import make_floor_divide_by_product

        div_prod = make_floor_divide_by_product(zero_result=0)
        # Use `div_prod(num, a, b)` inside a larger FHE program compilation
        ```
    """
    floor_divide = make_floor_divide(zero_result=zero_result)

    def floor_divide_by_product(
        numerator: Any,
        left: Any,
        right: Any,
    ) -> Any:
        """Perform encrypted division by a product of two numbers.

        Args:
            numerator: The encrypted dividend/numerator.
            left: The left encrypted operand.
            right: The right encrypted operand.

        Returns:
            Encrypted quotient.
        """
        return floor_divide(numerator, left * right)

    return floor_divide_by_product

compile_floor_divide

compile_floor_divide(max_numerator: int, max_denominator: int, *, zero_result: int = 0, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile floor division for nonnegative bounded encrypted inputs.

Parameters:

Name Type Description Default
max_numerator int

The upper bound for the encrypted numerator.

required
max_denominator int

The upper bound for the encrypted denominator.

required
zero_result int

Result to return when the operation is undefined (e.g., division by zero).

0
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit for floor division.

Example
from concrete_fhe_toolkit import compile_floor_divide

circuit = compile_floor_divide(max_numerator=10, max_denominator=5)
print(circuit.encrypt_run_decrypt(9, 2))  # 4
Source code in src/concrete_fhe_toolkit/arithmetic.py
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
def compile_floor_divide(
    max_numerator: int,
    max_denominator: int,
    *,
    zero_result: int = 0,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile floor division for nonnegative bounded encrypted inputs.

    Args:
        max_numerator (int): The upper bound for the encrypted numerator.
        max_denominator (int): The upper bound for the encrypted denominator.
        zero_result (int): Result to return when the operation is undefined (e.g., division by zero).
        configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

    Returns:
        fhe.Circuit: The compiled FHE circuit for floor division.

    Example:
        ```python
        from concrete_fhe_toolkit import compile_floor_divide

        circuit = compile_floor_divide(max_numerator=10, max_denominator=5)
        print(circuit.encrypt_run_decrypt(9, 2))  # 4
        ```
    """
    maximum_numerator = validate_integer(
        "max_numerator",
        max_numerator,
        minimum=0,
    )
    maximum_denominator = validate_integer(
        "max_denominator",
        max_denominator,
        minimum=1,
    )
    function = make_floor_divide(zero_result=zero_result)
    inputset = [
        (0, 0),
        (0, 1),
        (0, maximum_denominator),
        (maximum_numerator, 0),
        (maximum_numerator, 1),
        (maximum_numerator, maximum_denominator),
    ]
    return compile_function(
        function,
        {"numerator": "encrypted", "denominator": "encrypted"},
        inputset,
        configuration,
    )

compile_floor_divide_by_product

compile_floor_divide_by_product(max_numerator: int, max_left: int, max_right: int, *, zero_result: int = 0, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile numerator // (left * right) for nonnegative bounded inputs.

Parameters:

Name Type Description Default
max_numerator int

The upper bound for the encrypted numerator.

required
max_left int

The upper bound for the left encrypted operand.

required
max_right int

The upper bound for the right encrypted operand.

required
zero_result int

Result to return when the operation is undefined (e.g., division by zero).

0
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit for floor division by product.

Example
from concrete_fhe_toolkit import compile_floor_divide_by_product

circuit = compile_floor_divide_by_product(max_numerator=20, max_left=5, max_right=5)
print(circuit.encrypt_run_decrypt(20, 2, 3))  # 3
Source code in src/concrete_fhe_toolkit/arithmetic.py
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
def compile_floor_divide_by_product(
    max_numerator: int,
    max_left: int,
    max_right: int,
    *,
    zero_result: int = 0,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile numerator // (left * right) for nonnegative bounded inputs.

    Args:
        max_numerator (int): The upper bound for the encrypted numerator.
        max_left (int): The upper bound for the left encrypted operand.
        max_right (int): The upper bound for the right encrypted operand.
        zero_result (int): Result to return when the operation is undefined (e.g., division by zero).
        configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

    Returns:
        fhe.Circuit: The compiled FHE circuit for floor division by product.

    Example:
        ```python
        from concrete_fhe_toolkit import compile_floor_divide_by_product

        circuit = compile_floor_divide_by_product(max_numerator=20, max_left=5, max_right=5)
        print(circuit.encrypt_run_decrypt(20, 2, 3))  # 3
        ```
    """
    maximum_numerator = validate_integer(
        "max_numerator",
        max_numerator,
        minimum=0,
    )
    maximum_left = validate_integer("max_left", max_left, minimum=1)
    maximum_right = validate_integer("max_right", max_right, minimum=1)
    function = make_floor_divide_by_product(zero_result=zero_result)
    inputset = [
        (0, 0, 0),
        (0, maximum_left, maximum_right),
        (maximum_numerator, 0, maximum_right),
        (maximum_numerator, maximum_left, 0),
        (maximum_numerator, 1, 1),
        (maximum_numerator, maximum_left, maximum_right),
    ]
    return compile_function(
        function,
        {
            "numerator": "encrypted",
            "left": "encrypted",
            "right": "encrypted",
        },
        inputset,
        configuration,
    )