Skip to content

Combinatorics

concrete_fhe_toolkit.math.combinatorics

Bounded combinatorial functions evaluated through encrypted indices.

make_factorial

make_factorial(max_n: int) -> UnaryFunction

Create n! for an encrypted n in [0, max_n].

Parameters:

Name Type Description Default
max_n int

The maximum value for the encrypted integer n.

required

Returns:

Name Type Description
UnaryFunction UnaryFunction

A unary function that computes the factorial.

Example
from concrete_fhe_toolkit.math.combinatorics import make_factorial

factorial_fn = make_factorial(max_n=5)
# Use `factorial_fn(n)` inside an FHE program compilation
Source code in src/concrete_fhe_toolkit/math/combinatorics.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def make_factorial(max_n: int) -> UnaryFunction:
    """Create n! for an encrypted n in [0, max_n].

    Args:
        max_n (int): The maximum value for the encrypted integer n.

    Returns:
        UnaryFunction: A unary function that computes the factorial.

    Example:
        ```python
        from concrete_fhe_toolkit.math.combinatorics import make_factorial

        factorial_fn = make_factorial(max_n=5)
        # Use `factorial_fn(n)` inside an FHE program compilation
        ```
    """
    values = _factorials(max_n)
    return make_unary_lookup(values, 0)

compile_factorial

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

Compile n! for an encrypted n in [0, max_n].

Parameters:

Name Type Description Default
max_n int

The maximum value for the encrypted integer n.

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 for factorial computation.

Example
from concrete_fhe_toolkit.math.combinatorics import compile_factorial

circuit = compile_factorial(max_n=5)
print(circuit.encrypt_run_decrypt(4))  # 24
Source code in src/concrete_fhe_toolkit/math/combinatorics.py
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def compile_factorial(
    max_n: int,
    *,
    allow_large_lookup: bool = False,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile n! for an encrypted n in [0, max_n].

    Args:
        max_n (int): The maximum value for the encrypted integer n.
        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 for factorial computation.

    Example:
        ```python
        from concrete_fhe_toolkit.math.combinatorics import compile_factorial

        circuit = compile_factorial(max_n=5)
        print(circuit.encrypt_run_decrypt(4))  # 24
        ```
    """
    check_lookup_domain('compile_factorial', (0, max_n), allow_large_lookup=allow_large_lookup)
    maximum = _validate_maximum("max_n", max_n)
    values = _factorials(maximum)
    return compile_unary_lookup(
        "compile_factorial",
        values,
        0,
        maximum,
        allow_large_lookup=allow_large_lookup,
        configuration=configuration,
    )

make_fibonacci

make_fibonacci(max_n: int) -> UnaryFunction

Create the nth Fibonacci number for encrypted n in [0, max_n].

Parameters:

Name Type Description Default
max_n int

The maximum value for the encrypted integer n.

required

Returns:

Name Type Description
UnaryFunction UnaryFunction

A unary function that computes the nth Fibonacci number.

Example
from concrete_fhe_toolkit.math.combinatorics import make_fibonacci

fib_fn = make_fibonacci(max_n=10)
# Use `fib_fn(n)` inside an FHE program compilation
Source code in src/concrete_fhe_toolkit/math/combinatorics.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
def make_fibonacci(max_n: int) -> UnaryFunction:
    """Create the nth Fibonacci number for encrypted n in [0, max_n].

    Args:
        max_n (int): The maximum value for the encrypted integer n.

    Returns:
        UnaryFunction: A unary function that computes the nth Fibonacci number.

    Example:
        ```python
        from concrete_fhe_toolkit.math.combinatorics import make_fibonacci

        fib_fn = make_fibonacci(max_n=10)
        # Use `fib_fn(n)` inside an FHE program compilation
        ```
    """
    values = _fibonacci_values(max_n)
    return make_unary_lookup(values, 0)

compile_fibonacci

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

Compile the nth Fibonacci number for encrypted n in [0, max_n].

Parameters:

Name Type Description Default
max_n int

The maximum value for the encrypted integer n.

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 for Fibonacci computation.

Example
from concrete_fhe_toolkit.math.combinatorics import compile_fibonacci

circuit = compile_fibonacci(max_n=10)
print(circuit.encrypt_run_decrypt(6))  # 8
Source code in src/concrete_fhe_toolkit/math/combinatorics.py
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
def compile_fibonacci(
    max_n: int,
    *,
    allow_large_lookup: bool = False,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile the nth Fibonacci number for encrypted n in [0, max_n].

    Args:
        max_n (int): The maximum value for the encrypted integer n.
        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 for Fibonacci computation.

    Example:
        ```python
        from concrete_fhe_toolkit.math.combinatorics import compile_fibonacci

        circuit = compile_fibonacci(max_n=10)
        print(circuit.encrypt_run_decrypt(6))  # 8
        ```
    """
    check_lookup_domain('compile_fibonacci', (0, max_n), allow_large_lookup=allow_large_lookup)
    maximum = _validate_maximum("max_n", max_n)
    values = _fibonacci_values(maximum)
    return compile_unary_lookup(
        "compile_fibonacci",
        values,
        0,
        maximum,
        allow_large_lookup=allow_large_lookup,
        configuration=configuration,
    )

make_power

make_power(base: int, max_exponent: int) -> UnaryFunction

Create public-base exponentiation for an encrypted exponent.

Parameters:

Name Type Description Default
base int

The base to raise to the power of the exponent.

required
max_exponent int

The maximum value for the encrypted exponent.

required

Returns:

Name Type Description
UnaryFunction UnaryFunction

A unary function that computes base**exponent.

Example
from concrete_fhe_toolkit.math.combinatorics import make_power

pow_fn = make_power(base=2, max_exponent=10)
# Use `pow_fn(exponent)` inside an FHE program compilation
Source code in src/concrete_fhe_toolkit/math/combinatorics.py
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
def make_power(base: int, max_exponent: int) -> UnaryFunction:
    """Create public-base exponentiation for an encrypted exponent.

    Args:
        base (int): The base to raise to the power of the exponent.
        max_exponent (int): The maximum value for the encrypted exponent.

    Returns:
        UnaryFunction: A unary function that computes base**exponent.

    Example:
        ```python
        from concrete_fhe_toolkit.math.combinatorics import make_power

        pow_fn = make_power(base=2, max_exponent=10)
        # Use `pow_fn(exponent)` inside an FHE program compilation
        ```
    """
    normalized_base = validate_integer("base", base)
    maximum = _validate_maximum("max_exponent", max_exponent)
    values = [normalized_base**exponent for exponent in range(maximum + 1)]
    return make_unary_lookup(values, 0)

compile_power

compile_power(base: int, max_exponent: int, *, allow_large_lookup: bool = False, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile public-base exponentiation for an encrypted exponent.

Parameters:

Name Type Description Default
base int

The base to raise to the power of the exponent.

required
max_exponent int

The maximum value for the encrypted exponent.

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 for exponentiation.

Example
from concrete_fhe_toolkit.math.combinatorics import compile_power

circuit = compile_power(base=2, max_exponent=10)
print(circuit.encrypt_run_decrypt(3))  # 8
Source code in src/concrete_fhe_toolkit/math/combinatorics.py
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
def compile_power(
    base: int,
    max_exponent: int,
    *,
    allow_large_lookup: bool = False,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile public-base exponentiation for an encrypted exponent.

    Args:
        base (int): The base to raise to the power of the exponent.
        max_exponent (int): The maximum value for the encrypted exponent.
        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 for exponentiation.

    Example:
        ```python
        from concrete_fhe_toolkit.math.combinatorics import compile_power

        circuit = compile_power(base=2, max_exponent=10)
        print(circuit.encrypt_run_decrypt(3))  # 8
        ```
    """
    check_lookup_domain('compile_power', (0, max_exponent), allow_large_lookup=allow_large_lookup)
    normalized_base = validate_integer("base", base)
    maximum = _validate_maximum("max_exponent", max_exponent)
    values = [normalized_base**exponent for exponent in range(maximum + 1)]
    return compile_unary_lookup(
        "compile_power",
        values,
        0,
        maximum,
        allow_large_lookup=allow_large_lookup,
        configuration=configuration,
    )

make_comb

make_comb(max_n: int, *, invalid_result: int = 0) -> BinaryFunction

Create math.comb(n, r), returning invalid_result when r > n.

Parameters:

Name Type Description Default
max_n int

The maximum value for the encrypted integers n and r.

required
invalid_result int

The result to return when r > n. Defaults to 0.

0

Returns:

Name Type Description
BinaryFunction BinaryFunction

A binary function that computes math.comb(n, r).

Example
from concrete_fhe_toolkit.math.combinatorics import make_comb

comb_fn = make_comb(max_n=5, invalid_result=0)
# Use `comb_fn(n, r)` inside an FHE program compilation
Source code in src/concrete_fhe_toolkit/math/combinatorics.py
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
def make_comb(max_n: int, *, invalid_result: int = 0) -> BinaryFunction:
    """Create math.comb(n, r), returning invalid_result when r > n.

    Args:
        max_n (int): The maximum value for the encrypted integers n and r.
        invalid_result (int): The result to return when r > n. Defaults to 0.

    Returns:
        BinaryFunction: A binary function that computes math.comb(n, r).

    Example:
        ```python
        from concrete_fhe_toolkit.math.combinatorics import make_comb

        comb_fn = make_comb(max_n=5, invalid_result=0)
        # Use `comb_fn(n, r)` inside an FHE program compilation
        ```
    """
    return _make_n_r_lookup(_comb_value, max_n, invalid_result)

compile_comb

compile_comb(max_n: int, *, invalid_result: int = 0, allow_large_lookup: bool = False, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile math.comb(n, r) for encrypted n and r in [0, max_n].

Parameters:

Name Type Description Default
max_n int

The maximum value for the encrypted integers n and r.

required
invalid_result int

The result to return when r > n. Defaults to 0.

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 for combinations.

Example
from concrete_fhe_toolkit.math.combinatorics import compile_comb

circuit = compile_comb(max_n=5)
print(circuit.encrypt_run_decrypt(5, 2))  # 10
Source code in src/concrete_fhe_toolkit/math/combinatorics.py
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
def compile_comb(
    max_n: int,
    *,
    invalid_result: int = 0,
    allow_large_lookup: bool = False,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile math.comb(n, r) for encrypted n and r in [0, max_n].

    Args:
        max_n (int): The maximum value for the encrypted integers n and r.
        invalid_result (int): The result to return when r > n. Defaults to 0.
        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 for combinations.

    Example:
        ```python
        from concrete_fhe_toolkit.math.combinatorics import compile_comb

        circuit = compile_comb(max_n=5)
        print(circuit.encrypt_run_decrypt(5, 2))  # 10
        ```
    """
    check_lookup_domain(
        "compile_comb",
        (0, max_n),
        (0, max_n),
        allow_large_lookup=allow_large_lookup,
    )
    maximum = _validate_maximum("max_n", max_n)
    invalid = validate_integer("invalid_result", invalid_result)
    values = binary_values(
        lambda n, r: _comb_value(n, r, invalid),
        0,
        maximum,
        0,
        maximum,
    )
    return compile_binary_lookup(
        "compile_comb",
        values,
        0,
        maximum,
        0,
        maximum,
        allow_large_lookup=allow_large_lookup,
        configuration=configuration,
    )

make_perm

make_perm(max_n: int, *, invalid_result: int = 0) -> BinaryFunction

Create math.perm(n, r), returning invalid_result when r > n.

Parameters:

Name Type Description Default
max_n int

The maximum value for the encrypted integers n and r.

required
invalid_result int

The result to return when r > n. Defaults to 0.

0

Returns:

Name Type Description
BinaryFunction BinaryFunction

A binary function that computes math.perm(n, r).

Example
from concrete_fhe_toolkit.math.combinatorics import make_perm

perm_fn = make_perm(max_n=5, invalid_result=0)
# Use `perm_fn(n, r)` inside an FHE program compilation
Source code in src/concrete_fhe_toolkit/math/combinatorics.py
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
def make_perm(max_n: int, *, invalid_result: int = 0) -> BinaryFunction:
    """Create math.perm(n, r), returning invalid_result when r > n.

    Args:
        max_n (int): The maximum value for the encrypted integers n and r.
        invalid_result (int): The result to return when r > n. Defaults to 0.

    Returns:
        BinaryFunction: A binary function that computes math.perm(n, r).

    Example:
        ```python
        from concrete_fhe_toolkit.math.combinatorics import make_perm

        perm_fn = make_perm(max_n=5, invalid_result=0)
        # Use `perm_fn(n, r)` inside an FHE program compilation
        ```
    """
    return _make_n_r_lookup(_perm_value, max_n, invalid_result)

compile_perm

compile_perm(max_n: int, *, invalid_result: int = 0, allow_large_lookup: bool = False, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile math.perm(n, r) for encrypted n and r in [0, max_n].

Parameters:

Name Type Description Default
max_n int

The maximum value for the encrypted integers n and r.

required
invalid_result int

The result to return when r > n. Defaults to 0.

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 for permutations.

Example
from concrete_fhe_toolkit.math.combinatorics import compile_perm

circuit = compile_perm(max_n=5)
print(circuit.encrypt_run_decrypt(5, 2))  # 20
Source code in src/concrete_fhe_toolkit/math/combinatorics.py
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
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
def compile_perm(
    max_n: int,
    *,
    invalid_result: int = 0,
    allow_large_lookup: bool = False,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile math.perm(n, r) for encrypted n and r in [0, max_n].

    Args:
        max_n (int): The maximum value for the encrypted integers n and r.
        invalid_result (int): The result to return when r > n. Defaults to 0.
        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 for permutations.

    Example:
        ```python
        from concrete_fhe_toolkit.math.combinatorics import compile_perm

        circuit = compile_perm(max_n=5)
        print(circuit.encrypt_run_decrypt(5, 2))  # 20
        ```
    """
    check_lookup_domain(
        "compile_perm",
        (0, max_n),
        (0, max_n),
        allow_large_lookup=allow_large_lookup,
    )
    maximum = _validate_maximum("max_n", max_n)
    invalid = validate_integer("invalid_result", invalid_result)
    values = binary_values(
        lambda n, r: _perm_value(n, r, invalid),
        0,
        maximum,
        0,
        maximum,
    )
    return compile_binary_lookup(
        "compile_perm",
        values,
        0,
        maximum,
        0,
        maximum,
        allow_large_lookup=allow_large_lookup,
        configuration=configuration,
    )

make_powmod

make_powmod(base: int, modulus: int, max_exponent: int) -> UnaryFunction

Create pow(base, exponent, modulus) with a public base and modulus.

The exponent is encrypted and bounded by max_exponent; the result stays below the modulus, so the output bit width is small even for large exponents.

Parameters:

Name Type Description Default
base int

The base to raise to the power of the exponent.

required
modulus int

The modulus for the operation.

required
max_exponent int

The maximum value for the encrypted exponent.

required

Returns:

Name Type Description
UnaryFunction UnaryFunction

A unary function that computes pow(base, exponent, modulus).

Example
from concrete_fhe_toolkit.math.combinatorics import make_powmod

powmod_fn = make_powmod(base=2, modulus=5, max_exponent=10)
# Use `powmod_fn(exponent)` inside an FHE program compilation
Source code in src/concrete_fhe_toolkit/math/combinatorics.py
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
def make_powmod(base: int, modulus: int, max_exponent: int) -> UnaryFunction:
    """Create pow(base, exponent, modulus) with a public base and modulus.

    The exponent is encrypted and bounded by ``max_exponent``; the result
    stays below the modulus, so the output bit width is small even for
    large exponents.

    Args:
        base (int): The base to raise to the power of the exponent.
        modulus (int): The modulus for the operation.
        max_exponent (int): The maximum value for the encrypted exponent.

    Returns:
        UnaryFunction: A unary function that computes pow(base, exponent, modulus).

    Example:
        ```python
        from concrete_fhe_toolkit.math.combinatorics import make_powmod

        powmod_fn = make_powmod(base=2, modulus=5, max_exponent=10)
        # Use `powmod_fn(exponent)` inside an FHE program compilation
        ```
    """
    normalized_base = validate_integer("base", base)
    normalized_modulus = validate_integer("modulus", modulus, minimum=1)
    maximum = _validate_maximum("max_exponent", max_exponent)
    values = [
        pow(normalized_base, exponent, normalized_modulus)
        for exponent in range(maximum + 1)
    ]
    return make_unary_lookup(values, 0)

compile_powmod

compile_powmod(base: int, modulus: int, max_exponent: int, *, allow_large_lookup: bool = False, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile pow(base, exponent, modulus) with a public base and modulus.

Parameters:

Name Type Description Default
base int

The base to raise to the power of the exponent.

required
modulus int

The modulus for the operation.

required
max_exponent int

The maximum value for the encrypted exponent.

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 for modular exponentiation.

Example
from concrete_fhe_toolkit.math.combinatorics import compile_powmod

circuit = compile_powmod(base=2, modulus=5, max_exponent=10)
print(circuit.encrypt_run_decrypt(3))  # 3
Source code in src/concrete_fhe_toolkit/math/combinatorics.py
440
441
442
443
444
445
446
447
448
449
450
451
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
478
479
480
481
482
483
def compile_powmod(
    base: int,
    modulus: int,
    max_exponent: int,
    *,
    allow_large_lookup: bool = False,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile pow(base, exponent, modulus) with a public base and modulus.

    Args:
        base (int): The base to raise to the power of the exponent.
        modulus (int): The modulus for the operation.
        max_exponent (int): The maximum value for the encrypted exponent.
        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 for modular exponentiation.

    Example:
        ```python
        from concrete_fhe_toolkit.math.combinatorics import compile_powmod

        circuit = compile_powmod(base=2, modulus=5, max_exponent=10)
        print(circuit.encrypt_run_decrypt(3))  # 3
        ```
    """
    check_lookup_domain('compile_powmod', (0, max_exponent), allow_large_lookup=allow_large_lookup)
    normalized_base = validate_integer("base", base)
    normalized_modulus = validate_integer("modulus", modulus, minimum=1)
    maximum = _validate_maximum("max_exponent", max_exponent)
    values = [
        pow(normalized_base, exponent, normalized_modulus)
        for exponent in range(maximum + 1)
    ]
    return compile_unary_lookup(
        "compile_powmod",
        values,
        0,
        maximum,
        allow_large_lookup=allow_large_lookup,
        configuration=configuration,
    )