Skip to content

Basic operations

concrete_fhe_toolkit.math.basic

Basic bounded scalar operations for encrypted integers.

add

add(left: Any, right: Any) -> Any

Compute the add of the inputs.

Parameters:

Name Type Description Default
left Any

The left encrypted operand.

required
right Any

The right encrypted operand.

required

Returns:

Name Type Description
Any Any

The result of the operation.

Source code in src/concrete_fhe_toolkit/math/basic.py
25
26
27
28
29
30
31
32
33
34
35
def add(left: Any, right: Any) -> Any:
    """Compute the add of the inputs.

        Args:
            left (Any): The left encrypted operand.
            right (Any): The right encrypted operand.

        Returns:
            Any: The result of the operation.
        """
    return left + right

subtract

subtract(left: Any, right: Any) -> Any

Compute the subtract of the inputs.

Parameters:

Name Type Description Default
left Any

The left encrypted operand.

required
right Any

The right encrypted operand.

required

Returns:

Name Type Description
Any Any

The result of the operation.

Source code in src/concrete_fhe_toolkit/math/basic.py
38
39
40
41
42
43
44
45
46
47
48
def subtract(left: Any, right: Any) -> Any:
    """Compute the subtract of the inputs.

        Args:
            left (Any): The left encrypted operand.
            right (Any): The right encrypted operand.

        Returns:
            Any: The result of the operation.
        """
    return left - right

multiply

multiply(left: Any, right: Any) -> Any

Compute the multiply of the inputs.

Parameters:

Name Type Description Default
left Any

The left encrypted operand.

required
right Any

The right encrypted operand.

required

Returns:

Name Type Description
Any Any

The result of the operation.

Source code in src/concrete_fhe_toolkit/math/basic.py
51
52
53
54
55
56
57
58
59
60
61
def multiply(left: Any, right: Any) -> Any:
    """Compute the multiply of the inputs.

        Args:
            left (Any): The left encrypted operand.
            right (Any): The right encrypted operand.

        Returns:
            Any: The result of the operation.
        """
    return left * right

negate

negate(value: Any) -> Any

Compute the negate of the inputs.

Parameters:

Name Type Description Default
value Any

The encrypted operand.

required

Returns:

Name Type Description
Any Any

The result of the operation.

Source code in src/concrete_fhe_toolkit/math/basic.py
64
65
66
67
68
69
70
71
72
73
def negate(value: Any) -> Any:
    """Compute the negate of the inputs.

        Args:
            value (Any): The encrypted operand.

        Returns:
            Any: The result of the operation.
        """
    return -value

square

square(value: Any) -> Any

Compute the square of the inputs.

Parameters:

Name Type Description Default
value Any

The encrypted operand.

required

Returns:

Name Type Description
Any Any

The result of the operation.

Source code in src/concrete_fhe_toolkit/math/basic.py
76
77
78
79
80
81
82
83
84
85
def square(value: Any) -> Any:
    """Compute the square of the inputs.

        Args:
            value (Any): The encrypted operand.

        Returns:
            Any: The result of the operation.
        """
    return value * value

cube

cube(value: Any) -> Any

Compute the cube of the inputs.

Parameters:

Name Type Description Default
value Any

The encrypted operand.

required

Returns:

Name Type Description
Any Any

The result of the operation.

Source code in src/concrete_fhe_toolkit/math/basic.py
88
89
90
91
92
93
94
95
96
97
def cube(value: Any) -> Any:
    """Compute the cube of the inputs.

        Args:
            value (Any): The encrypted operand.

        Returns:
            Any: The result of the operation.
        """
    return value * value * value

equal

equal(left: Any, right: Any) -> Any

Compute the equal of the inputs.

Parameters:

Name Type Description Default
left Any

The left encrypted operand.

required
right Any

The right encrypted operand.

required

Returns:

Name Type Description
Any Any

The result of the operation.

Source code in src/concrete_fhe_toolkit/math/basic.py
100
101
102
103
104
105
106
107
108
109
110
def equal(left: Any, right: Any) -> Any:
    """Compute the equal of the inputs.

        Args:
            left (Any): The left encrypted operand.
            right (Any): The right encrypted operand.

        Returns:
            Any: The result of the operation.
        """
    return (left == right) * 1

not_equal

not_equal(left: Any, right: Any) -> Any

Compute the not equal of the inputs.

Parameters:

Name Type Description Default
left Any

The left encrypted operand.

required
right Any

The right encrypted operand.

required

Returns:

Name Type Description
Any Any

The result of the operation.

Source code in src/concrete_fhe_toolkit/math/basic.py
112
113
114
115
116
117
118
119
120
121
122
def not_equal(left: Any, right: Any) -> Any:
    """Compute the not equal of the inputs.

        Args:
            left (Any): The left encrypted operand.
            right (Any): The right encrypted operand.

        Returns:
            Any: The result of the operation.
        """
    return (left != right) * 1

less

less(left: Any, right: Any) -> Any

Compute the less of the inputs.

Parameters:

Name Type Description Default
left Any

The left encrypted operand.

required
right Any

The right encrypted operand.

required

Returns:

Name Type Description
Any Any

The result of the operation.

Source code in src/concrete_fhe_toolkit/math/basic.py
124
125
126
127
128
129
130
131
132
133
134
def less(left: Any, right: Any) -> Any:
    """Compute the less of the inputs.

        Args:
            left (Any): The left encrypted operand.
            right (Any): The right encrypted operand.

        Returns:
            Any: The result of the operation.
        """
    return (left < right) * 1

less_equal

less_equal(left: Any, right: Any) -> Any

Compute the less equal of the inputs.

Parameters:

Name Type Description Default
left Any

The left encrypted operand.

required
right Any

The right encrypted operand.

required

Returns:

Name Type Description
Any Any

The result of the operation.

Source code in src/concrete_fhe_toolkit/math/basic.py
136
137
138
139
140
141
142
143
144
145
146
def less_equal(left: Any, right: Any) -> Any:
    """Compute the less equal of the inputs.

        Args:
            left (Any): The left encrypted operand.
            right (Any): The right encrypted operand.

        Returns:
            Any: The result of the operation.
        """
    return (left <= right) * 1

greater

greater(left: Any, right: Any) -> Any

Compute the greater of the inputs.

Parameters:

Name Type Description Default
left Any

The left encrypted operand.

required
right Any

The right encrypted operand.

required

Returns:

Name Type Description
Any Any

The result of the operation.

Source code in src/concrete_fhe_toolkit/math/basic.py
148
149
150
151
152
153
154
155
156
157
158
def greater(left: Any, right: Any) -> Any:
    """Compute the greater of the inputs.

        Args:
            left (Any): The left encrypted operand.
            right (Any): The right encrypted operand.

        Returns:
            Any: The result of the operation.
        """
    return (left > right) * 1

greater_equal

greater_equal(left: Any, right: Any) -> Any

Compute the greater equal of the inputs.

Parameters:

Name Type Description Default
left Any

The left encrypted operand.

required
right Any

The right encrypted operand.

required

Returns:

Name Type Description
Any Any

The result of the operation.

Source code in src/concrete_fhe_toolkit/math/basic.py
160
161
162
163
164
165
166
167
168
169
170
def greater_equal(left: Any, right: Any) -> Any:
    """Compute the greater equal of the inputs.

        Args:
            left (Any): The left encrypted operand.
            right (Any): The right encrypted operand.

        Returns:
            Any: The result of the operation.
        """
    return (left >= right) * 1

is_zero

is_zero(value: Any) -> Any

Compute the is zero of the inputs.

Parameters:

Name Type Description Default
value Any

The encrypted operand.

required

Returns:

Name Type Description
Any Any

The result of the operation.

Source code in src/concrete_fhe_toolkit/math/basic.py
172
173
174
175
176
177
178
179
180
181
def is_zero(value: Any) -> Any:
    """Compute the is zero of the inputs.

        Args:
            value (Any): The encrypted operand.

        Returns:
            Any: The result of the operation.
        """
    return (value == 0) * 1

maximum

maximum(left: Any, right: Any) -> Any

Compute the maximum of the inputs.

Parameters:

Name Type Description Default
left Any

The left encrypted operand.

required
right Any

The right encrypted operand.

required

Returns:

Name Type Description
Any Any

The result of the operation.

Source code in src/concrete_fhe_toolkit/math/basic.py
184
185
186
187
188
189
190
191
192
193
194
def maximum(left: Any, right: Any) -> Any:
    """Compute the maximum of the inputs.

        Args:
            left (Any): The left encrypted operand.
            right (Any): The right encrypted operand.

        Returns:
            Any: The result of the operation.
        """
    return ((left+right)+abs(left-right))//2

minimum

minimum(left: Any, right: Any) -> Any

Compute the minimum of the inputs.

Parameters:

Name Type Description Default
left Any

The left encrypted operand.

required
right Any

The right encrypted operand.

required

Returns:

Name Type Description
Any Any

The result of the operation.

Source code in src/concrete_fhe_toolkit/math/basic.py
197
198
199
200
201
202
203
204
205
206
207
def minimum(left: Any, right: Any) -> Any:
    """Compute the minimum of the inputs.

        Args:
            left (Any): The left encrypted operand.
            right (Any): The right encrypted operand.

        Returns:
            Any: The result of the operation.
        """
    return ((left+right)-abs(left-right))//2

make_scalar_multiply

make_scalar_multiply(multiplier: int) -> UnaryFunction

Create a scalar multiply function.

Parameters:

Name Type Description Default
multiplier int

The cleartext multiplier.

required

Returns:

Name Type Description
UnaryFunction UnaryFunction

The generated function.

Source code in src/concrete_fhe_toolkit/math/basic.py
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
def make_scalar_multiply(multiplier: int) -> UnaryFunction:
    """Create a scalar multiply function.

        Args:
            multiplier (int): The cleartext multiplier.

        Returns:
            UnaryFunction: The generated function.
        """
    normalized = validate_integer("multiplier", multiplier)

    def scalar_multiply(value: Any) -> Any:
        """Compute the scalar_multiply operation."""
        return value * normalized

    return scalar_multiply

make_is_close

make_is_close(absolute_tolerance: int) -> BinaryFunction

Create a is close function.

Parameters:

Name Type Description Default
absolute_tolerance int

The absolute_tolerance.

required

Returns:

Name Type Description
BinaryFunction BinaryFunction

The generated function.

Source code in src/concrete_fhe_toolkit/math/basic.py
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
def make_is_close(absolute_tolerance: int) -> BinaryFunction:
    """Create a is close function.

        Args:
            absolute_tolerance (int): The absolute_tolerance.

        Returns:
            BinaryFunction: The generated function.
        """
    tolerance = validate_integer(
        "absolute_tolerance",
        absolute_tolerance,
        minimum=0,
    )

    def is_close(left: Any, right: Any) -> Any:
        """Compute the is_close operation."""
        difference = left - right
        return (difference >= -tolerance) * (difference <= tolerance)

    return is_close

compile_add

compile_add(min_left: int = 0, max_left: int = 15, min_right: int = 0, max_right: int = 15, *, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile an FHE circuit for add.

Parameters:

Name Type Description Default
min_left int

The lower bound for the left encrypted operand.

0
max_left int

The upper bound for the left encrypted operand.

15
min_right int

The lower bound for the right encrypted operand.

0
max_right int

The upper bound for the right encrypted operand.

15
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit.

Source code in src/concrete_fhe_toolkit/math/basic.py
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
def compile_add(
    min_left: int = 0,
    max_left: int = 15,
    min_right: int = 0,
    max_right: int = 15,
    *,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for add.

        Args:
            min_left (int): The lower bound for the left encrypted operand.
            max_left (int): The upper bound for the left encrypted operand.
            min_right (int): The lower bound for the right encrypted operand.
            max_right (int): The upper bound for the right encrypted operand.
            configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

        Returns:
            fhe.Circuit: The compiled FHE circuit.
        """
    return _compile_binary_native(
        add,
        min_left,
        max_left,
        min_right,
        max_right,
        configuration,
    )

compile_subtract

compile_subtract(min_left: int = 0, max_left: int = 15, min_right: int = 0, max_right: int = 15, *, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile an FHE circuit for subtract.

Parameters:

Name Type Description Default
min_left int

The lower bound for the left encrypted operand.

0
max_left int

The upper bound for the left encrypted operand.

15
min_right int

The lower bound for the right encrypted operand.

0
max_right int

The upper bound for the right encrypted operand.

15
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit.

Source code in src/concrete_fhe_toolkit/math/basic.py
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
def compile_subtract(
    min_left: int = 0,
    max_left: int = 15,
    min_right: int = 0,
    max_right: int = 15,
    *,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for subtract.

        Args:
            min_left (int): The lower bound for the left encrypted operand.
            max_left (int): The upper bound for the left encrypted operand.
            min_right (int): The lower bound for the right encrypted operand.
            max_right (int): The upper bound for the right encrypted operand.
            configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

        Returns:
            fhe.Circuit: The compiled FHE circuit.
        """
    return _compile_binary_native(
        subtract,
        min_left,
        max_left,
        min_right,
        max_right,
        configuration,
    )

compile_multiply

compile_multiply(min_left: int = 0, max_left: int = 15, min_right: int = 0, max_right: int = 15, *, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile an FHE circuit for multiply.

Parameters:

Name Type Description Default
min_left int

The lower bound for the left encrypted operand.

0
max_left int

The upper bound for the left encrypted operand.

15
min_right int

The lower bound for the right encrypted operand.

0
max_right int

The upper bound for the right encrypted operand.

15
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit.

Source code in src/concrete_fhe_toolkit/math/basic.py
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
def compile_multiply(
    min_left: int = 0,
    max_left: int = 15,
    min_right: int = 0,
    max_right: int = 15,
    *,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for multiply.

        Args:
            min_left (int): The lower bound for the left encrypted operand.
            max_left (int): The upper bound for the left encrypted operand.
            min_right (int): The lower bound for the right encrypted operand.
            max_right (int): The upper bound for the right encrypted operand.
            configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

        Returns:
            fhe.Circuit: The compiled FHE circuit.
        """
    return _compile_binary_native(
        multiply,
        min_left,
        max_left,
        min_right,
        max_right,
        configuration,
    )

compile_negate

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

Compile an FHE circuit for negate.

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.

Source code in src/concrete_fhe_toolkit/math/basic.py
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
def compile_negate(
    min_value: int = -15,
    max_value: int = 15,
    *,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for negate.

        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.
        """
    minimum, maximum = validate_bounds(min_value, max_value)
    return compile_function(
        negate,
        {"value": "encrypted"},
        [minimum, maximum],
        configuration,
    )

compile_square

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

Compile an FHE circuit for square.

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.

Source code in src/concrete_fhe_toolkit/math/basic.py
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
def compile_square(
    min_value: int = -15,
    max_value: int = 15,
    *,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for square.

        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.
        """
    minimum, maximum = validate_bounds(min_value, max_value)
    inputset = [minimum, maximum]
    if minimum <= 0 <= maximum:
        inputset.append(0)
    return compile_function(
        square,
        {"value": "encrypted"},
        inputset,
        configuration,
    )

compile_cube

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

Compile an FHE circuit for cube.

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.

Source code in src/concrete_fhe_toolkit/math/basic.py
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
def compile_cube(
    min_value: int = -15,
    max_value: int = 15,
    *,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for cube.

        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.
        """
    minimum, maximum = validate_bounds(min_value, max_value)
    inputset = [minimum, maximum]
    if minimum <= 0 <= maximum:
        inputset.append(0)
    return compile_function(
        cube,
        {"value": "encrypted"},
        inputset,
        configuration,
    )    

compile_scalar_multiply

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

Compile an FHE circuit for scalar multiply.

Parameters:

Name Type Description Default
min_value int

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

required
max_value int

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

required
multiplier int

The cleartext multiplier.

required
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit.

Source code in src/concrete_fhe_toolkit/math/basic.py
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
def compile_scalar_multiply(
    min_value: int,
    max_value: int,
    multiplier: int,
    *,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for scalar multiply.

        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.
            multiplier (int): The cleartext multiplier.
            configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

        Returns:
            fhe.Circuit: The compiled FHE circuit.
        """
    minimum, maximum = validate_bounds(min_value, max_value)
    function = make_scalar_multiply(multiplier)
    return compile_function(
        function,
        {"value": "encrypted"},
        [minimum, maximum],
        configuration,
    )

compile_equal

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

Compile an FHE circuit for equal.

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.

Source code in src/concrete_fhe_toolkit/math/basic.py
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
def compile_equal(
    min_value: int = -15,
    max_value: int = 15,
    *,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for equal.

        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.
        """
    return _compile_predicate(equal, min_value, max_value, configuration)

compile_not_equal

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

Compile an FHE circuit for not equal.

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.

Source code in src/concrete_fhe_toolkit/math/basic.py
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
def compile_not_equal(
    min_value: int = -15,
    max_value: int = 15,
    *,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for not equal.

        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.
        """
    return _compile_predicate(not_equal, min_value, max_value, configuration)

compile_less

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

Compile an FHE circuit for less.

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.

Source code in src/concrete_fhe_toolkit/math/basic.py
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
def compile_less(
    min_value: int = -15,
    max_value: int = 15,
    *,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for less.

        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.
        """
    return _compile_predicate(less, min_value, max_value, configuration)

compile_less_equal

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

Compile an FHE circuit for less equal.

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.

Source code in src/concrete_fhe_toolkit/math/basic.py
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
def compile_less_equal(
    min_value: int = -15,
    max_value: int = 15,
    *,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for less equal.

        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.
        """
    return _compile_predicate(less_equal, min_value, max_value, configuration)

compile_greater

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

Compile an FHE circuit for greater.

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.

Source code in src/concrete_fhe_toolkit/math/basic.py
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
def compile_greater(
    min_value: int = -15,
    max_value: int = 15,
    *,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for greater.

        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.
        """
    return _compile_predicate(greater, min_value, max_value, configuration)

compile_greater_equal

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

Compile an FHE circuit for greater equal.

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.

Source code in src/concrete_fhe_toolkit/math/basic.py
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
def compile_greater_equal(
    min_value: int = -15,
    max_value: int = 15,
    *,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for greater equal.

        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.
        """
    return _compile_predicate(greater_equal, min_value, max_value, configuration)

compile_is_zero

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

Compile an FHE circuit for is zero.

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.

Source code in src/concrete_fhe_toolkit/math/basic.py
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
def compile_is_zero(
    min_value: int = -15,
    max_value: int = 15,
    *,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for is zero.

        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.
        """
    minimum, maximum = validate_bounds(min_value, max_value)
    return compile_function(
        is_zero,
        {"value": "encrypted"},
        [minimum, 0, maximum],
        configuration,
    )

compile_maximum

compile_maximum(min_left: int = 0, max_left: int = 15, min_right: int = 0, max_right: int = 15, *, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile an FHE circuit for maximum.

Parameters:

Name Type Description Default
min_left int

The lower bound for the left encrypted operand.

0
max_left int

The upper bound for the left encrypted operand.

15
min_right int

The lower bound for the right encrypted operand.

0
max_right int

The upper bound for the right encrypted operand.

15
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit.

Source code in src/concrete_fhe_toolkit/math/basic.py
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
def compile_maximum(
    min_left: int = 0,
    max_left: int = 15,
    min_right: int = 0,
    max_right: int = 15,
    *,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for maximum.

        Args:
            min_left (int): The lower bound for the left encrypted operand.
            max_left (int): The upper bound for the left encrypted operand.
            min_right (int): The lower bound for the right encrypted operand.
            max_right (int): The upper bound for the right encrypted operand.
            configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

        Returns:
            fhe.Circuit: The compiled FHE circuit.
        """
    return _compile_binary_native(
        maximum,
        min_left,
        max_left,
        min_right,
        max_right,
        configuration,
    )

compile_minimum

compile_minimum(min_left: int = 0, max_left: int = 15, min_right: int = 0, max_right: int = 15, *, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile an FHE circuit for minimum.

Parameters:

Name Type Description Default
min_left int

The lower bound for the left encrypted operand.

0
max_left int

The upper bound for the left encrypted operand.

15
min_right int

The lower bound for the right encrypted operand.

0
max_right int

The upper bound for the right encrypted operand.

15
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit.

Source code in src/concrete_fhe_toolkit/math/basic.py
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
def compile_minimum(
    min_left: int = 0,
    max_left: int = 15,
    min_right: int = 0,
    max_right: int = 15,
    *,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for minimum.

        Args:
            min_left (int): The lower bound for the left encrypted operand.
            max_left (int): The upper bound for the left encrypted operand.
            min_right (int): The lower bound for the right encrypted operand.
            max_right (int): The upper bound for the right encrypted operand.
            configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

        Returns:
            fhe.Circuit: The compiled FHE circuit.
        """
    return _compile_binary_native(
        minimum,
        min_left,
        max_left,
        min_right,
        max_right,
        configuration,
    )

compile_is_close

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

Compile an FHE circuit for is close.

Parameters:

Name Type Description Default
min_value int

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

required
max_value int

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

required
absolute_tolerance int

The absolute_tolerance.

required
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit.

Source code in src/concrete_fhe_toolkit/math/basic.py
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
def compile_is_close(
    min_value: int,
    max_value: int,
    *,
    absolute_tolerance: int,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for is close.

        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.
            absolute_tolerance (int): The absolute_tolerance.
            configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

        Returns:
            fhe.Circuit: The compiled FHE circuit.
        """
    function = make_is_close(absolute_tolerance)
    return _compile_predicate(function, min_value, max_value, configuration)

make_absolute

make_absolute(min_value: int, max_value: int) -> UnaryFunction

Create a absolute function.

Parameters:

Name Type Description Default
min_value int

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

required
max_value int

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

required

Returns:

Name Type Description
UnaryFunction UnaryFunction

The generated function.

Source code in src/concrete_fhe_toolkit/math/basic.py
763
764
765
766
767
768
769
770
771
772
773
774
def make_absolute(min_value: int, max_value: int) -> UnaryFunction:
    """Create a absolute function.

        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.

        Returns:
            UnaryFunction: The generated function.
        """
    values = unary_values(abs, min_value, max_value)
    return make_unary_lookup(values, min_value)

compile_absolute

compile_absolute(min_value: int, max_value: int, *, allow_large_lookup: bool = False, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile an FHE circuit for absolute.

Parameters:

Name Type Description Default
min_value int

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

required
max_value int

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

required
allow_large_lookup bool

Whether to allow large table lookups (may be slow).

False
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit.

Source code in src/concrete_fhe_toolkit/math/basic.py
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
def compile_absolute(
    min_value: int,
    max_value: int,
    *,
    allow_large_lookup: bool = False,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for absolute.

        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.
            allow_large_lookup (bool): Whether to allow large table lookups (may be slow).
            configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

        Returns:
            fhe.Circuit: The compiled FHE circuit.
        """
    check_lookup_domain(
        "compile_absolute",
        (min_value, max_value),
        allow_large_lookup=allow_large_lookup,
    )
    values = unary_values(abs, min_value, max_value)
    return compile_unary_lookup(
        "compile_absolute",
        values,
        min_value,
        max_value,
        allow_large_lookup=allow_large_lookup,
        configuration=configuration,
    )

make_clamp

make_clamp(min_input: int, max_input: int, min_value: int, max_value: int) -> UnaryFunction

Create a clamp function.

Parameters:

Name Type Description Default
min_input int

The min_input.

required
max_input int

The max_input.

required
min_value int

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

required
max_value int

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

required

Returns:

Name Type Description
UnaryFunction UnaryFunction

The generated function.

Source code in src/concrete_fhe_toolkit/math/basic.py
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
def make_clamp(
    min_input: int,
    max_input: int,
    min_value: int,
    max_value: int,
) -> UnaryFunction:
    """Create a clamp function.

        Args:
            min_input (int): The min_input.
            max_input (int): The max_input.
            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.

        Returns:
            UnaryFunction: The generated function.
        """
    input_minimum, input_maximum = validate_bounds(min_input, max_input)
    clamp_minimum, clamp_maximum = validate_bounds(min_value, max_value)
    values = unary_values(
        lambda value: max(clamp_minimum, min(value, clamp_maximum)),
        input_minimum,
        input_maximum,
    )
    return make_unary_lookup(values, input_minimum)

compile_clamp

compile_clamp(min_input: int, max_input: int, min_value: int, max_value: int, *, allow_large_lookup: bool = False, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile an FHE circuit for clamp.

Parameters:

Name Type Description Default
min_input int

The min_input.

required
max_input int

The max_input.

required
min_value int

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

required
max_value int

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

required
allow_large_lookup bool

Whether to allow large table lookups (may be slow).

False
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit.

Source code in src/concrete_fhe_toolkit/math/basic.py
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
def compile_clamp(
    min_input: int,
    max_input: int,
    min_value: int,
    max_value: int,
    *,
    allow_large_lookup: bool = False,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for clamp.

        Args:
            min_input (int): The min_input.
            max_input (int): The max_input.
            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.
            allow_large_lookup (bool): Whether to allow large table lookups (may be slow).
            configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

        Returns:
            fhe.Circuit: The compiled FHE circuit.
        """
    check_lookup_domain(
        "compile_clamp",
        (min_input, max_input),
        allow_large_lookup=allow_large_lookup,
    )
    input_minimum, input_maximum = validate_bounds(min_input, max_input)
    clamp_minimum, clamp_maximum = validate_bounds(min_value, max_value)
    values = unary_values(
        lambda value: max(clamp_minimum, min(value, clamp_maximum)),
        input_minimum,
        input_maximum,
    )
    return compile_unary_lookup(
        "compile_clamp",
        values,
        input_minimum,
        input_maximum,
        allow_large_lookup=allow_large_lookup,
        configuration=configuration,
    )

make_modulo

make_modulo(min_numerator: int, max_numerator: int, min_denominator: int, max_denominator: int, *, zero_result: int = 0) -> BinaryFunction

Create a modulo function.

Parameters:

Name Type Description Default
min_numerator int

The lower bound for the encrypted numerator.

required
max_numerator int

The upper bound for the encrypted numerator.

required
min_denominator int

The lower bound for the encrypted denominator.

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

Returns:

Name Type Description
BinaryFunction BinaryFunction

The generated function.

Source code in src/concrete_fhe_toolkit/math/basic.py
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
def make_modulo(
    min_numerator: int,
    max_numerator: int,
    min_denominator: int,
    max_denominator: int,
    *,
    zero_result: int = 0,
) -> BinaryFunction:
    """Create a modulo function.

        Args:
            min_numerator (int): The lower bound for the encrypted numerator.
            max_numerator (int): The upper bound for the encrypted numerator.
            min_denominator (int): The lower bound for the encrypted denominator.
            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).

        Returns:
            BinaryFunction: The generated function.
        """
    zero = validate_integer("zero_result", zero_result)
    values = binary_values(
        lambda numerator, denominator: (
            zero if denominator == 0 else numerator % denominator
        ),
        min_numerator,
        max_numerator,
        min_denominator,
        max_denominator,
    )
    denominator_minimum, denominator_maximum = validate_bounds(
        min_denominator,
        max_denominator,
    )
    return make_binary_lookup(
        values,
        min_numerator,
        denominator_minimum,
        denominator_maximum - denominator_minimum + 1,
    )

compile_modulo

compile_modulo(min_numerator: int, max_numerator: int, min_denominator: int, max_denominator: int, *, zero_result: int = 0, allow_large_lookup: bool = False, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile an FHE circuit for modulo.

Parameters:

Name Type Description Default
min_numerator int

The lower bound for the encrypted numerator.

required
max_numerator int

The upper bound for the encrypted numerator.

required
min_denominator int

The lower bound for the encrypted denominator.

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
allow_large_lookup bool

Whether to allow large table lookups (may be slow).

False
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit.

Source code in src/concrete_fhe_toolkit/math/basic.py
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
def compile_modulo(
    min_numerator: int,
    max_numerator: int,
    min_denominator: int,
    max_denominator: int,
    *,
    zero_result: int = 0,
    allow_large_lookup: bool = False,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for modulo.

        Args:
            min_numerator (int): The lower bound for the encrypted numerator.
            max_numerator (int): The upper bound for the encrypted numerator.
            min_denominator (int): The lower bound for the encrypted denominator.
            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).
            allow_large_lookup (bool): Whether to allow large table lookups (may be slow).
            configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

        Returns:
            fhe.Circuit: The compiled FHE circuit.
        """
    check_lookup_domain(
        "compile_modulo",
        (min_numerator, max_numerator),
        (min_denominator, max_denominator),
        allow_large_lookup=allow_large_lookup,
    )
    zero = validate_integer("zero_result", zero_result)
    values = binary_values(
        lambda numerator, denominator: (
            zero if denominator == 0 else numerator % denominator
        ),
        min_numerator,
        max_numerator,
        min_denominator,
        max_denominator,
    )
    return compile_binary_lookup(
        "compile_modulo",
        values,
        min_numerator,
        max_numerator,
        min_denominator,
        max_denominator,
        allow_large_lookup=allow_large_lookup,
        configuration=configuration,
    )

make_divmod

make_divmod(min_numerator: int, max_numerator: int, min_denominator: int, max_denominator: int, *, zero_quotient: int = 0, zero_remainder: int = 0) -> BinaryFunction

Create a divmod function.

Parameters:

Name Type Description Default
min_numerator int

The lower bound for the encrypted numerator.

required
max_numerator int

The upper bound for the encrypted numerator.

required
min_denominator int

The lower bound for the encrypted denominator.

required
max_denominator int

The upper bound for the encrypted denominator.

required
zero_quotient int

Result to return for quotient when division by zero.

0
zero_remainder int

Result to return for remainder when division by zero.

0

Returns:

Name Type Description
BinaryFunction BinaryFunction

The generated function.

Source code in src/concrete_fhe_toolkit/math/basic.py
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
def make_divmod(
    min_numerator: int,
    max_numerator: int,
    min_denominator: int,
    max_denominator: int,
    *,
    zero_quotient: int = 0,
    zero_remainder: int = 0,
) -> BinaryFunction:
    """Create a divmod function.

        Args:
            min_numerator (int): The lower bound for the encrypted numerator.
            max_numerator (int): The upper bound for the encrypted numerator.
            min_denominator (int): The lower bound for the encrypted denominator.
            max_denominator (int): The upper bound for the encrypted denominator.
            zero_quotient (int): Result to return for quotient when division by zero.
            zero_remainder (int): Result to return for remainder when division by zero.

        Returns:
            BinaryFunction: The generated function.
        """
    zero_q = validate_integer("zero_quotient", zero_quotient)
    zero_r = validate_integer("zero_remainder", zero_remainder)
    denominator_minimum, denominator_maximum = validate_bounds(
        min_denominator,
        max_denominator,
    )
    quotient_values = binary_values(
        lambda numerator, denominator: (
            zero_q if denominator == 0 else numerator // denominator
        ),
        min_numerator,
        max_numerator,
        denominator_minimum,
        denominator_maximum,
    )
    remainder_values = binary_values(
        lambda numerator, denominator: (
            zero_r if denominator == 0 else numerator % denominator
        ),
        min_numerator,
        max_numerator,
        denominator_minimum,
        denominator_maximum,
    )
    width = denominator_maximum - denominator_minimum + 1
    quotient = make_binary_lookup(
        quotient_values,
        min_numerator,
        denominator_minimum,
        width,
    )
    remainder = make_binary_lookup(
        remainder_values,
        min_numerator,
        denominator_minimum,
        width,
    )

    def quotient_and_remainder(numerator: Any, denominator: Any) -> Any:
        """Compute the quotient and remainder operations."""
        return quotient(numerator, denominator), remainder(numerator, denominator)

    return quotient_and_remainder

compile_divmod

compile_divmod(min_numerator: int, max_numerator: int, min_denominator: int, max_denominator: int, *, zero_quotient: int = 0, zero_remainder: int = 0, allow_large_lookup: bool = False, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile an FHE circuit for divmod.

Parameters:

Name Type Description Default
min_numerator int

The lower bound for the encrypted numerator.

required
max_numerator int

The upper bound for the encrypted numerator.

required
min_denominator int

The lower bound for the encrypted denominator.

required
max_denominator int

The upper bound for the encrypted denominator.

required
zero_quotient int

Result to return for quotient when division by zero.

0
zero_remainder int

Result to return for remainder when division by zero.

0
allow_large_lookup bool

Whether to allow large table lookups (may be slow).

False
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit.

Source code in src/concrete_fhe_toolkit/math/basic.py
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
def compile_divmod(
    min_numerator: int,
    max_numerator: int,
    min_denominator: int,
    max_denominator: int,
    *,
    zero_quotient: int = 0,
    zero_remainder: int = 0,
    allow_large_lookup: bool = False,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for divmod.

        Args:
            min_numerator (int): The lower bound for the encrypted numerator.
            max_numerator (int): The upper bound for the encrypted numerator.
            min_denominator (int): The lower bound for the encrypted denominator.
            max_denominator (int): The upper bound for the encrypted denominator.
            zero_quotient (int): Result to return for quotient when division by zero.
            zero_remainder (int): Result to return for remainder when division by zero.
            allow_large_lookup (bool): Whether to allow large table lookups (may be slow).
            configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

        Returns:
            fhe.Circuit: The compiled FHE circuit.
        """
    check_lookup_domain(
        "compile_divmod",
        (min_numerator, max_numerator),
        (min_denominator, max_denominator),
        allow_large_lookup=allow_large_lookup,
    )
    function = make_divmod(
        min_numerator,
        max_numerator,
        min_denominator,
        max_denominator,
        zero_quotient=zero_quotient,
        zero_remainder=zero_remainder,
    )
    numerator_minimum, numerator_maximum = validate_bounds(
        min_numerator,
        max_numerator,
    )
    denominator_minimum, denominator_maximum = validate_bounds(
        min_denominator,
        max_denominator,
    )
    quotient_values = binary_values(
        lambda numerator, denominator: (
            zero_quotient if denominator == 0 else numerator // denominator
        ),
        numerator_minimum,
        numerator_maximum,
        denominator_minimum,
        denominator_maximum,
    )
    remainder_values = binary_values(
        lambda numerator, denominator: (
            zero_remainder if denominator == 0 else numerator % denominator
        ),
        numerator_minimum,
        numerator_maximum,
        denominator_minimum,
        denominator_maximum,
    )
    check_lookup_cost(
        "compile_divmod quotient",
        quotient_values,
        allow_large_lookup=allow_large_lookup,
    )
    check_lookup_cost(
        "compile_divmod remainder",
        remainder_values,
        allow_large_lookup=allow_large_lookup,
    )
    inputset = _binary_inputset(
        numerator_minimum,
        numerator_maximum,
        denominator_minimum,
        denominator_maximum,
    )

    if denominator_minimum <= 0 <= denominator_maximum:
        inputset.append((numerator_minimum, 0))
        inputset.append((numerator_maximum, 0))

    return compile_function(
        function,
        {"numerator": "encrypted", "denominator": "encrypted"},
        inputset,
        configuration,
    )

select

select(control: Any, when_true: Any, when_false: Any) -> Any

Compute the select of the inputs.

Parameters:

Name Type Description Default
control Any

The control.

required
when_true Any

The when_true.

required
when_false Any

The when_false.

required

Returns:

Name Type Description
Any Any

The result of the operation.

Source code in src/concrete_fhe_toolkit/math/basic.py
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
def select(control: Any, when_true: Any, when_false: Any) -> Any:
    """Compute the select of the inputs.

        Args:
            control (Any): The control.
            when_true (Any): The when_true.
            when_false (Any): The when_false.

        Returns:
            Any: The result of the operation.
        """
    return control * (when_true - when_false) + when_false

compile_select

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

Compile an FHE circuit for select.

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.

Source code in src/concrete_fhe_toolkit/math/basic.py
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
def compile_select(
    min_value: int = -15,
    max_value: int = 15,
    *,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for select.

        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.
        """
    minimum, maximum = validate_bounds(min_value, max_value)
    inputset = [
        (0, minimum, minimum),
        (0, maximum, maximum),
        (0, minimum, maximum),
        (1, minimum, minimum),
        (1, maximum, maximum),
        (1, maximum, minimum),
    ]
    return compile_function(
        select,
        {
            "control": "encrypted",
            "when_true": "encrypted",
            "when_false": "encrypted",
        },
        inputset,
        configuration,
    )

make_abs_diff

make_abs_diff(min_value: int = 0, max_value: int = 15) -> BinaryFunction

Create a abs diff function.

Parameters:

Name Type Description Default
min_value int

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

0
max_value int

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

15

Returns:

Name Type Description
BinaryFunction BinaryFunction

The generated function.

Source code in src/concrete_fhe_toolkit/math/basic.py
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
def make_abs_diff(min_value: int = 0, max_value: int = 15) -> BinaryFunction:
    """Create a abs diff function.

        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.

        Returns:
            BinaryFunction: The generated function.
        """
    minimum, maximum = validate_bounds(min_value, max_value)
    span = maximum - minimum

    if span == 0:
        def constant_abs_diff(left: Any, right: Any) -> Any:
            """Compute the constant_abs_diff operation."""
            return left - right

        return constant_abs_diff

    values = unary_values(abs, -span, span)
    lookup = make_unary_lookup(values, -span)

    def abs_diff(left: Any, right: Any) -> Any:
        """Compute the abs_diff operation."""
        return lookup(left - right)

    return abs_diff

compile_abs_diff

compile_abs_diff(min_value: int = 0, max_value: int = 15, *, allow_large_lookup: bool = False, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile an FHE circuit for abs diff.

Parameters:

Name Type Description Default
min_value int

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

0
max_value int

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

15
allow_large_lookup bool

Whether to allow large table lookups (may be slow).

False
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit.

Source code in src/concrete_fhe_toolkit/math/basic.py
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
def compile_abs_diff(
    min_value: int = 0,
    max_value: int = 15,
    *,
    allow_large_lookup: bool = False,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for abs diff.

        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.
            allow_large_lookup (bool): Whether to allow large table lookups (may be slow).
            configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

        Returns:
            fhe.Circuit: The compiled FHE circuit.
        """
    low, high = validate_bounds(min_value, max_value)
    check_lookup_domain(
        "compile_abs_diff",
        (-(high - low), high - low),
        allow_large_lookup=allow_large_lookup,
    )
    minimum, maximum = validate_bounds(min_value, max_value)
    span = maximum - minimum
    if span:
        check_lookup_cost(
            "compile_abs_diff",
            unary_values(abs, -span, span),
            allow_large_lookup=allow_large_lookup,
        )
    function = make_abs_diff(minimum, maximum)
    inputset = [
        (minimum, minimum),
        (minimum, maximum),
        (maximum, minimum),
        (maximum, maximum),
    ]
    return compile_function(
        function,
        {"left": "encrypted", "right": "encrypted"},
        inputset,
        configuration,
    )

make_copysign

make_copysign(min_value: int = -15, max_value: int = 15) -> BinaryFunction

Create a copysign function.

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

Returns:

Name Type Description
BinaryFunction BinaryFunction

The generated function.

Source code in src/concrete_fhe_toolkit/math/basic.py
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
def make_copysign(min_value: int = -15, max_value: int = 15) -> BinaryFunction:
    """Create a copysign function.

        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.

        Returns:
            BinaryFunction: The generated function.
        """
    minimum, maximum = validate_bounds(min_value, max_value)
    absolute = make_absolute(minimum, maximum)

    def copysign(x: Any, y: Any) -> Any:
        """Compute the copysign operation."""
        sign_factor = (y >= 0) * 2 - 1
        return absolute(x) * sign_factor

    return copysign

compile_copysign

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

Compile an FHE circuit for copysign.

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
allow_large_lookup bool

Whether to allow large table lookups (may be slow).

False
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit.

Source code in src/concrete_fhe_toolkit/math/basic.py
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
def compile_copysign(
    min_value: int = -15,
    max_value: int = 15,
    *,
    allow_large_lookup: bool = False,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for copysign.

        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.
            allow_large_lookup (bool): Whether to allow large table lookups (may be slow).
            configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

        Returns:
            fhe.Circuit: The compiled FHE circuit.
        """
    check_lookup_domain(
        "compile_copysign",
        (min_value, max_value),
        allow_large_lookup=allow_large_lookup,
    )
    minimum, maximum = validate_bounds(min_value, max_value)
    check_lookup_cost(
        "compile_copysign",
        unary_values(abs, minimum, maximum),
        allow_large_lookup=allow_large_lookup,
    )
    function = make_copysign(minimum, maximum)
    inputset = [
        (minimum, minimum),
        (minimum, maximum),
        (maximum, minimum),
        (maximum, maximum),
    ]
    return compile_function(
        function,
        {"x": "encrypted", "y": "encrypted"},
        inputset,
        configuration,
    )

make_saturating_add

make_saturating_add(min_value: int = -15, max_value: int = 15) -> BinaryFunction

Create a saturating add function.

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

Returns:

Name Type Description
BinaryFunction BinaryFunction

The generated function.

Source code in src/concrete_fhe_toolkit/math/basic.py
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
def make_saturating_add(min_value: int = -15, max_value: int = 15) -> BinaryFunction:
    """Create a saturating add function.

        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.

        Returns:
            BinaryFunction: The generated function.
        """
    return _make_saturating("add", min_value, max_value)

make_saturating_subtract

make_saturating_subtract(min_value: int = -15, max_value: int = 15) -> BinaryFunction

Create a saturating subtract function.

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

Returns:

Name Type Description
BinaryFunction BinaryFunction

The generated function.

Source code in src/concrete_fhe_toolkit/math/basic.py
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
def make_saturating_subtract(min_value: int = -15, max_value: int = 15) -> BinaryFunction:
    """Create a saturating subtract function.

        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.

        Returns:
            BinaryFunction: The generated function.
        """
    return _make_saturating("subtract", min_value, max_value)

make_saturating_multiply

make_saturating_multiply(min_value: int = -15, max_value: int = 15) -> BinaryFunction

Create a saturating multiply function.

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

Returns:

Name Type Description
BinaryFunction BinaryFunction

The generated function.

Source code in src/concrete_fhe_toolkit/math/basic.py
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
def make_saturating_multiply(min_value: int = -15, max_value: int = 15) -> BinaryFunction:
    """Create a saturating multiply function.

        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.

        Returns:
            BinaryFunction: The generated function.
        """
    return _make_saturating("multiply", min_value, max_value)

compile_saturating_add

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

Compile an FHE circuit for saturating add.

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
allow_large_lookup bool

Whether to allow large table lookups (may be slow).

False
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit.

Source code in src/concrete_fhe_toolkit/math/basic.py
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
def compile_saturating_add(
    min_value: int = -15,
    max_value: int = 15,
    *,
    allow_large_lookup: bool = False,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for saturating add.

        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.
            allow_large_lookup (bool): Whether to allow large table lookups (may be slow).
            configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

        Returns:
            fhe.Circuit: The compiled FHE circuit.
        """
    return _compile_saturating("add", min_value, max_value, allow_large_lookup, configuration)

compile_saturating_subtract

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

Compile an FHE circuit for saturating subtract.

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
allow_large_lookup bool

Whether to allow large table lookups (may be slow).

False
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit.

Source code in src/concrete_fhe_toolkit/math/basic.py
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
def compile_saturating_subtract(
    min_value: int = -15,
    max_value: int = 15,
    *,
    allow_large_lookup: bool = False,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for saturating subtract.

        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.
            allow_large_lookup (bool): Whether to allow large table lookups (may be slow).
            configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

        Returns:
            fhe.Circuit: The compiled FHE circuit.
        """
    return _compile_saturating(
        "subtract", min_value, max_value, allow_large_lookup, configuration
    )

compile_saturating_multiply

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

Compile an FHE circuit for saturating multiply.

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
allow_large_lookup bool

Whether to allow large table lookups (may be slow).

False
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit.

Source code in src/concrete_fhe_toolkit/math/basic.py
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
def compile_saturating_multiply(
    min_value: int = -15,
    max_value: int = 15,
    *,
    allow_large_lookup: bool = False,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for saturating multiply.

        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.
            allow_large_lookup (bool): Whether to allow large table lookups (may be slow).
            configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

        Returns:
            fhe.Circuit: The compiled FHE circuit.
        """
    return _compile_saturating(
        "multiply", min_value, max_value, allow_large_lookup, configuration
    )

make_fdim

make_fdim(min_value: int = 0, max_value: int = 15) -> BinaryFunction

Create a fdim function.

Parameters:

Name Type Description Default
min_value int

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

0
max_value int

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

15

Returns:

Name Type Description
BinaryFunction BinaryFunction

The generated function.

Source code in src/concrete_fhe_toolkit/math/basic.py
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
def make_fdim(min_value: int = 0, max_value: int = 15) -> BinaryFunction:
    """Create a fdim function.

        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.

        Returns:
            BinaryFunction: The generated function.
        """
    minimum, maximum = validate_bounds(min_value, max_value)
    span = maximum - minimum

    if span == 0:
        def constant_fdim(left: Any, right: Any) -> Any:
            """Compute the constant_fdim operation."""
            return left - right

        return constant_fdim

    values = unary_values(lambda diff: max(diff, 0), -span, span)
    lookup = make_unary_lookup(values, -span)

    def fdim(left: Any, right: Any) -> Any:
        """Compute the fdim operation."""
        return lookup(left - right)

    return fdim

compile_fdim

compile_fdim(min_value: int = 0, max_value: int = 15, *, allow_large_lookup: bool = False, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile an FHE circuit for fdim.

Parameters:

Name Type Description Default
min_value int

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

0
max_value int

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

15
allow_large_lookup bool

Whether to allow large table lookups (may be slow).

False
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit.

Source code in src/concrete_fhe_toolkit/math/basic.py
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
def compile_fdim(
    min_value: int = 0,
    max_value: int = 15,
    *,
    allow_large_lookup: bool = False,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for fdim.

        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.
            allow_large_lookup (bool): Whether to allow large table lookups (may be slow).
            configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

        Returns:
            fhe.Circuit: The compiled FHE circuit.
        """
    low, high = validate_bounds(min_value, max_value)
    check_lookup_domain(
        "compile_fdim",
        (0, 2 * (high - low)),
        allow_large_lookup=allow_large_lookup,
    )
    minimum, maximum = validate_bounds(min_value, max_value)
    span = maximum - minimum
    if span:
        check_lookup_cost(
            "compile_fdim",
            unary_values(lambda diff: max(diff, 0), -span, span),
            allow_large_lookup=allow_large_lookup,
        )
    function = make_fdim(minimum, maximum)
    inputset = [
        (minimum, minimum),
        (minimum, maximum),
        (maximum, minimum),
        (maximum, maximum),
    ]
    return compile_function(
        function,
        {"left": "encrypted", "right": "encrypted"},
        inputset,
        configuration,
    )

fma

fma(left: Any, right: Any, addend: Any) -> Any

Compute the fma of the inputs.

Parameters:

Name Type Description Default
left Any

The left encrypted operand.

required
right Any

The right encrypted operand.

required
addend Any

The addend.

required

Returns:

Name Type Description
Any Any

The result of the operation.

Source code in src/concrete_fhe_toolkit/math/basic.py
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
def fma(left: Any, right: Any, addend: Any) -> Any:
    """Compute the fma of the inputs.

        Args:
            left (Any): The left encrypted operand.
            right (Any): The right encrypted operand.
            addend (Any): The addend.

        Returns:
            Any: The result of the operation.
        """
    return left * right + addend

compile_fma

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

Compile an FHE circuit for fma.

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.

Source code in src/concrete_fhe_toolkit/math/basic.py
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
def compile_fma(
    min_value: int = -15,
    max_value: int = 15,
    *,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for fma.

        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.
        """
    minimum, maximum = validate_bounds(min_value, max_value)
    inputset = [
        (minimum, minimum, minimum),
        (minimum, maximum, minimum),
        (maximum, minimum, maximum),
        (maximum, maximum, maximum),
        (minimum, maximum, maximum),
        (maximum, maximum, minimum),
    ]
    return compile_function(
        fma,
        {"left": "encrypted", "right": "encrypted", "addend": "encrypted"},
        inputset,
        configuration,
    )

make_remainder

make_remainder(min_numerator: int, max_numerator: int, min_denominator: int, max_denominator: int, *, zero_result: int = 0) -> BinaryFunction

Create a remainder function.

Parameters:

Name Type Description Default
min_numerator int

The lower bound for the encrypted numerator.

required
max_numerator int

The upper bound for the encrypted numerator.

required
min_denominator int

The lower bound for the encrypted denominator.

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

Returns:

Name Type Description
BinaryFunction BinaryFunction

The generated function.

Source code in src/concrete_fhe_toolkit/math/basic.py
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
def make_remainder(
    min_numerator: int,
    max_numerator: int,
    min_denominator: int,
    max_denominator: int,
    *,
    zero_result: int = 0,
) -> BinaryFunction:
    """Create a remainder function.

        Args:
            min_numerator (int): The lower bound for the encrypted numerator.
            max_numerator (int): The upper bound for the encrypted numerator.
            min_denominator (int): The lower bound for the encrypted denominator.
            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).

        Returns:
            BinaryFunction: The generated function.
        """
    from fractions import Fraction

    zero = validate_integer("zero_result", zero_result)
    denominator_minimum, denominator_maximum = validate_bounds(
        min_denominator,
        max_denominator,
    )
    values = binary_values(
        lambda numerator, denominator: (
            zero
            if denominator == 0
            else numerator - round(Fraction(numerator, denominator)) * denominator
        ),
        min_numerator,
        max_numerator,
        denominator_minimum,
        denominator_maximum,
    )
    return make_binary_lookup(
        values,
        min_numerator,
        denominator_minimum,
        denominator_maximum - denominator_minimum + 1,
    )

compile_remainder

compile_remainder(min_numerator: int, max_numerator: int, min_denominator: int, max_denominator: int, *, zero_result: int = 0, allow_large_lookup: bool = False, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile an FHE circuit for remainder.

Parameters:

Name Type Description Default
min_numerator int

The lower bound for the encrypted numerator.

required
max_numerator int

The upper bound for the encrypted numerator.

required
min_denominator int

The lower bound for the encrypted denominator.

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
allow_large_lookup bool

Whether to allow large table lookups (may be slow).

False
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit.

Source code in src/concrete_fhe_toolkit/math/basic.py
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
def compile_remainder(
    min_numerator: int,
    max_numerator: int,
    min_denominator: int,
    max_denominator: int,
    *,
    zero_result: int = 0,
    allow_large_lookup: bool = False,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for remainder.

        Args:
            min_numerator (int): The lower bound for the encrypted numerator.
            max_numerator (int): The upper bound for the encrypted numerator.
            min_denominator (int): The lower bound for the encrypted denominator.
            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).
            allow_large_lookup (bool): Whether to allow large table lookups (may be slow).
            configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

        Returns:
            fhe.Circuit: The compiled FHE circuit.
        """
    check_lookup_domain(
        "compile_remainder",
        (min_numerator, max_numerator),
        (min_denominator, max_denominator),
        allow_large_lookup=allow_large_lookup,
    )
    from fractions import Fraction

    zero = validate_integer("zero_result", zero_result)
    values = binary_values(
        lambda numerator, denominator: (
            zero
            if denominator == 0
            else numerator - round(Fraction(numerator, denominator)) * denominator
        ),
        min_numerator,
        max_numerator,
        min_denominator,
        max_denominator,
    )
    return compile_binary_lookup(
        "compile_remainder",
        values,
        min_numerator,
        max_numerator,
        min_denominator,
        max_denominator,
        allow_large_lookup=allow_large_lookup,
        configuration=configuration,
    )

make_ldexp

make_ldexp(exponent: int) -> UnaryFunction

Create a ldexp function.

Parameters:

Name Type Description Default
exponent int

The exponent.

required

Returns:

Name Type Description
UnaryFunction UnaryFunction

The generated function.

Source code in src/concrete_fhe_toolkit/math/basic.py
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
def make_ldexp(exponent: int) -> UnaryFunction:
    """Create a ldexp function.

        Args:
            exponent (int): The exponent.

        Returns:
            UnaryFunction: The generated function.
        """
    normalized = validate_integer("exponent", exponent)
    if normalized >= 0:
        factor = 1 << normalized

        def scale_up(value: Any) -> Any:
            """Compute the scale_up operation."""
            return value * factor

        return scale_up

    divisor = 1 << (-normalized)

    def scale_down(value: Any) -> Any:
        """Compute the scale_down operation."""
        return value // divisor

    return scale_down

compile_ldexp

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

Compile an FHE circuit for ldexp.

Parameters:

Name Type Description Default
min_value int

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

required
max_value int

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

required
exponent int

The exponent.

required
configuration Optional[Configuration]

Optional FHE compiler configuration object.

None

Returns:

Type Description
Circuit

fhe.Circuit: The compiled FHE circuit.

Source code in src/concrete_fhe_toolkit/math/basic.py
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
def compile_ldexp(
    min_value: int,
    max_value: int,
    exponent: int,
    *,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile an FHE circuit for ldexp.

        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.
            exponent (int): The exponent.
            configuration (Optional[fhe.Configuration]): Optional FHE compiler configuration object.

        Returns:
            fhe.Circuit: The compiled FHE circuit.
        """
    minimum, maximum = validate_bounds(min_value, max_value)
    function = make_ldexp(exponent)
    return compile_function(
        function,
        {"value": "encrypted"},
        [minimum, maximum],
        configuration,
    )

fsum

fsum(values: Any) -> Any

Compute the fsum of the inputs.

Parameters:

Name Type Description Default
values Any

The values.

required

Returns:

Name Type Description
Any Any

The result of the operation.

Source code in src/concrete_fhe_toolkit/math/basic.py
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
def fsum(values: Any) -> Any:
    """Compute the fsum of the inputs.

        Args:
            values (Any): The values.

        Returns:
            Any: The result of the operation.
        """
    items = list(values)
    if not items:
        return 0
    while len(items) > 1:
        next_items = []
        for index in range(0, len(items) - 1, 2):
            next_items.append(items[index] + items[index + 1])
        if len(items) % 2 == 1:
            next_items.append(items[-1])
        items = next_items
    return items[0]

prod

prod(values: Any, start: int = 1) -> Any

Compute the prod of the inputs.

Parameters:

Name Type Description Default
values Any

The values.

required
start int

The start.

1

Returns:

Name Type Description
Any Any

The result of the operation.

Source code in src/concrete_fhe_toolkit/math/basic.py
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
def prod(values: Any, start: int = 1) -> Any:
    """Compute the prod of the inputs.

        Args:
            values (Any): The values.
            start (int): The start.

        Returns:
            Any: The result of the operation.
        """
    normalized_start = validate_integer("start", start)
    items = list(values)
    if not items:
        return normalized_start
    while len(items) > 1:
        next_items = []
        for index in range(0, len(items) - 1, 2):
            next_items.append(items[index] * items[index + 1])
        if len(items) % 2 == 1:
            next_items.append(items[-1])
        items = next_items
    return items[0] * normalized_start

sumprod

sumprod(p: Any, q: Any) -> Any

Compute the sumprod of the inputs.

Parameters:

Name Type Description Default
p Any

The p.

required
q Any

The q.

required

Returns:

Name Type Description
Any Any

The result of the operation.

Source code in src/concrete_fhe_toolkit/math/basic.py
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
def sumprod(p: Any, q: Any) -> Any:
    """Compute the sumprod of the inputs.

        Args:
            p (Any): The p.
            q (Any): The q.

        Returns:
            Any: The result of the operation.
        """
    left = list(p)
    right = list(q)
    if len(left) != len(right):
        raise ValueError("p and q must have the same length")
    return fsum(item_p * item_q for item_p, item_q in zip(left, right))