Skip to content

Binary division

concrete_fhe_toolkit.math.binary_division

Binary restoring division helpers for Concrete FHE circuits.

unsigned_divide_bits

unsigned_divide_bits(numerator_bits: Iterable[Any], denominator_bits: Iterable[Any], *, quotient_width: int | None = None, zero_result: int = 0) -> tuple[Any, ...]

Return little-endian quotient bits for unsigned floor division.

Division by zero returns the clear fallback encoded by zero_result.

Parameters:

Name Type Description Default
numerator_bits Iterable[...]

An iterable of encrypted or cleartext bits representing the numerator in little-endian format.

required
denominator_bits Iterable[...]

An iterable of encrypted or cleartext bits representing the denominator in little-endian format.

required

Returns:

Type Description
tuple[Any, ...]

tuple[...]: The resulting value.

Example
from concrete_fhe_toolkit.math.binary_division import unsigned_divide_bits

num_bits = [1, 0, 1]  # 5
den_bits = [0, 1]     # 2
# Use `unsigned_divide_bits(num_bits, den_bits)` inside an FHE program compilation
Source code in src/concrete_fhe_toolkit/math/binary_division.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
160
161
162
163
164
165
166
167
168
169
def unsigned_divide_bits(
    numerator_bits: Iterable[Any],
    denominator_bits: Iterable[Any],
    *,
    quotient_width: int | None = None,
    zero_result: int = 0,
) -> tuple[Any, ...]:
    """Return little-endian quotient bits for unsigned floor division.

    Division by zero returns the clear fallback encoded by `zero_result`.

    Args:
        numerator_bits (Iterable[...]): An iterable of encrypted or cleartext bits representing the numerator in little-endian format.
        denominator_bits (Iterable[...]): An iterable of encrypted or cleartext bits representing the denominator in little-endian format.

    Returns:
        tuple[...]: The resulting value.

    Example:
        ```python
        from concrete_fhe_toolkit.math.binary_division import unsigned_divide_bits

        num_bits = [1, 0, 1]  # 5
        den_bits = [0, 1]     # 2
        # Use `unsigned_divide_bits(num_bits, den_bits)` inside an FHE program compilation
        ```
    """
    numerator = _as_nonempty_bits("numerator_bits", numerator_bits)
    denominator = _as_nonempty_bits("denominator_bits", denominator_bits)
    width = (
        len(numerator)
        if quotient_width is None
        else _validate_width("quotient_width", quotient_width)
    )
    if len(numerator) > width:
        raise ValueError("quotient_width must be at least len(numerator_bits)")
    zero = _validate_zero_result(zero_result, width)

    quotient, _ = _restoring_divide_bits(numerator, denominator, width)
    denominator_nonzero = bit_or_many(denominator)
    fallback = unsigned_to_bits(zero, width)
    return tuple(
        bit_select(denominator_nonzero, quotient[index], fallback[index])
        for index in range(width)
    )

fixed_point_divide_bits

fixed_point_divide_bits(numerator_bits: Iterable[Any], denominator_bits: Iterable[Any], *, fractional_bits: int, quotient_width: int | None = None, zero_result: int = 0) -> tuple[Any, ...]

Return quotient bits for floor((numerator << fractional_bits) / denominator).

Parameters:

Name Type Description Default
numerator_bits Iterable[...]

An iterable of encrypted or cleartext bits representing the numerator in little-endian format.

required
denominator_bits Iterable[...]

An iterable of encrypted or cleartext bits representing the denominator in little-endian format.

required

Returns:

Type Description
tuple[Any, ...]

tuple[...]: The resulting value.

Example
from concrete_fhe_toolkit.math.binary_division import fixed_point_divide_bits

num_bits = [1, 0, 1]  # 5
den_bits = [0, 1]     # 2
# Use `fixed_point_divide_bits(num_bits, den_bits, fractional_bits=1)` inside an FHE program compilation
Source code in src/concrete_fhe_toolkit/math/binary_division.py
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
def fixed_point_divide_bits(
    numerator_bits: Iterable[Any],
    denominator_bits: Iterable[Any],
    *,
    fractional_bits: int,
    quotient_width: int | None = None,
    zero_result: int = 0,
) -> tuple[Any, ...]:
    """Return quotient bits for floor((numerator << fractional_bits) / denominator).

    Args:
        numerator_bits (Iterable[...]): An iterable of encrypted or cleartext bits representing the numerator in little-endian format.
        denominator_bits (Iterable[...]): An iterable of encrypted or cleartext bits representing the denominator in little-endian format.

    Returns:
        tuple[...]: The resulting value.

    Example:
        ```python
        from concrete_fhe_toolkit.math.binary_division import fixed_point_divide_bits

        num_bits = [1, 0, 1]  # 5
        den_bits = [0, 1]     # 2
        # Use `fixed_point_divide_bits(num_bits, den_bits, fractional_bits=1)` inside an FHE program compilation
        ```
    """
    numerator = _as_nonempty_bits("numerator_bits", numerator_bits)
    fraction = _validate_fractional_bits(fractional_bits)
    shifted_numerator = (0,) * fraction + numerator
    width = (
        len(shifted_numerator)
        if quotient_width is None
        else _validate_width("quotient_width", quotient_width)
    )
    if len(shifted_numerator) > width:
        raise ValueError(
            "quotient_width must be at least len(numerator_bits) + fractional_bits"
        )
    return unsigned_divide_bits(
        shifted_numerator,
        denominator_bits,
        quotient_width=width,
        zero_result=zero_result,
    )

make_unsigned_floor_divide

make_unsigned_floor_divide(numerator_width: int, denominator_width: int, *, quotient_width: int | None = None, zero_result: int = 0) -> Any

Create unsigned encrypted floor division from scalar integer inputs.

Parameters:

Name Type Description Default
numerator_width int

The bit width of the numerator.

required
denominator_width int

The bit width of the denominator.

required

Returns:

Name Type Description
Any Any

The resulting value.

Example
from concrete_fhe_toolkit.math.binary_division import make_unsigned_floor_divide

div_fn = make_unsigned_floor_divide(numerator_width=4, denominator_width=4)
# Use `div_fn(num, den)` inside an FHE program compilation
Source code in src/concrete_fhe_toolkit/math/binary_division.py
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
def make_unsigned_floor_divide(
    numerator_width: int,
    denominator_width: int,
    *,
    quotient_width: int | None = None,
    zero_result: int = 0,
) -> Any:
    """Create unsigned encrypted floor division from scalar integer inputs.

    Args:
        numerator_width (int): The bit width of the numerator.
        denominator_width (int): The bit width of the denominator.

    Returns:
        Any: The resulting value.

    Example:
        ```python
        from concrete_fhe_toolkit.math.binary_division import make_unsigned_floor_divide

        div_fn = make_unsigned_floor_divide(numerator_width=4, denominator_width=4)
        # Use `div_fn(num, den)` inside an FHE program compilation
        ```
    """
    n_width = _validate_width("numerator_width", numerator_width)
    d_width = _validate_width("denominator_width", denominator_width)
    q_width = (
        n_width
        if quotient_width is None
        else _validate_width("quotient_width", quotient_width)
    )
    if q_width < n_width:
        raise ValueError("quotient_width must be at least numerator_width")
    zero = _validate_zero_result(zero_result, q_width)

    def divide(numerator: Any, denominator: Any) -> Any:
        """Execute the unsigned floor division circuit on inputs.

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

        Returns:
            The unsigned integer result of the floor division.
        """
        numerator_bits = integer_to_bits(numerator, n_width)
        denominator_bits = integer_to_bits(denominator, d_width)
        quotient_bits = unsigned_divide_bits(
            numerator_bits,
            denominator_bits,
            quotient_width=q_width,
            zero_result=zero,
        )
        return bits_to_unsigned(quotient_bits)

    return divide

make_fixed_point_divide

make_fixed_point_divide(numerator_width: int, denominator_width: int, *, fractional_bits: int, quotient_width: int | None = None, zero_result: int = 0) -> Any

Create fixed-point unsigned division from scalar integer inputs.

Parameters:

Name Type Description Default
numerator_width int

The bit width of the numerator.

required
denominator_width int

The bit width of the denominator.

required

Returns:

Name Type Description
Any Any

The resulting value.

Example
from concrete_fhe_toolkit.math.binary_division import make_fixed_point_divide

div_fn = make_fixed_point_divide(4, 4, fractional_bits=2)
# Use `div_fn(num, den)` inside an FHE program compilation
Source code in src/concrete_fhe_toolkit/math/binary_division.py
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
def make_fixed_point_divide(
    numerator_width: int,
    denominator_width: int,
    *,
    fractional_bits: int,
    quotient_width: int | None = None,
    zero_result: int = 0,
) -> Any:
    """Create fixed-point unsigned division from scalar integer inputs.

    Args:
        numerator_width (int): The bit width of the numerator.
        denominator_width (int): The bit width of the denominator.

    Returns:
        Any: The resulting value.

    Example:
        ```python
        from concrete_fhe_toolkit.math.binary_division import make_fixed_point_divide

        div_fn = make_fixed_point_divide(4, 4, fractional_bits=2)
        # Use `div_fn(num, den)` inside an FHE program compilation
        ```
    """
    n_width = _validate_width("numerator_width", numerator_width)
    d_width = _validate_width("denominator_width", denominator_width)
    fraction = _validate_fractional_bits(fractional_bits)
    default_width = n_width + fraction
    q_width = (
        default_width
        if quotient_width is None
        else _validate_width("quotient_width", quotient_width)
    )
    if q_width < default_width:
        raise ValueError(
            "quotient_width must be at least numerator_width + fractional_bits"
        )
    zero = _validate_zero_result(zero_result, q_width)

    def divide(numerator: Any, denominator: Any) -> Any:
        """Execute the fixed-point division circuit on inputs.

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

        Returns:
            The unsigned integer fixed-point division result.
        """
        numerator_bits = integer_to_bits(numerator, n_width)
        denominator_bits = integer_to_bits(denominator, d_width)
        quotient_bits = fixed_point_divide_bits(
            numerator_bits,
            denominator_bits,
            fractional_bits=fraction,
            quotient_width=q_width,
            zero_result=zero,
        )
        return bits_to_unsigned(quotient_bits)

    return divide

compile_unsigned_floor_divide

compile_unsigned_floor_divide(numerator_width: int, denominator_width: int, *, quotient_width: int | None = None, zero_result: int = 0, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile unsigned floor division using a bit-level restoring circuit.

Parameters:

Name Type Description Default
numerator_width int

The bit width of the numerator.

required
denominator_width int

The bit width of the denominator.

required

Returns:

Name Type Description
Any Circuit

The resulting value.

Example
from concrete_fhe_toolkit.math.binary_division import compile_unsigned_floor_divide

circuit = compile_unsigned_floor_divide(numerator_width=4, denominator_width=4)
print(circuit.encrypt_run_decrypt(10, 3))  # 3
Source code in src/concrete_fhe_toolkit/math/binary_division.py
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
def compile_unsigned_floor_divide(
    numerator_width: int,
    denominator_width: int,
    *,
    quotient_width: int | None = None,
    zero_result: int = 0,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile unsigned floor division using a bit-level restoring circuit.

    Args:
        numerator_width (int): The bit width of the numerator.
        denominator_width (int): The bit width of the denominator.

    Returns:
        Any: The resulting value.

    Example:
        ```python
        from concrete_fhe_toolkit.math.binary_division import compile_unsigned_floor_divide

        circuit = compile_unsigned_floor_divide(numerator_width=4, denominator_width=4)
        print(circuit.encrypt_run_decrypt(10, 3))  # 3
        ```
    """
    n_width = _validate_width("numerator_width", numerator_width)
    d_width = _validate_width("denominator_width", denominator_width)
    function = make_unsigned_floor_divide(
        n_width,
        d_width,
        quotient_width=quotient_width,
        zero_result=zero_result,
    )
    return compile_function(
        function,
        {"numerator": "encrypted", "denominator": "encrypted"},
        _division_inputset(n_width, d_width),
        configuration,
    )

compile_fixed_point_divide

compile_fixed_point_divide(numerator_width: int, denominator_width: int, *, fractional_bits: int, quotient_width: int | None = None, zero_result: int = 0, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile fixed-point unsigned division using a bit-level circuit.

Parameters:

Name Type Description Default
numerator_width int

The bit width of the numerator.

required
denominator_width int

The bit width of the denominator.

required

Returns:

Name Type Description
Any Circuit

The resulting value.

Example
from concrete_fhe_toolkit.math.binary_division import compile_fixed_point_divide

circuit = compile_fixed_point_divide(4, 4, fractional_bits=2)
print(circuit.encrypt_run_decrypt(5, 2))  # floor((5 << 2) / 2) = 10
Source code in src/concrete_fhe_toolkit/math/binary_division.py
417
418
419
420
421
422
423
424
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
451
452
453
454
455
456
457
def compile_fixed_point_divide(
    numerator_width: int,
    denominator_width: int,
    *,
    fractional_bits: int,
    quotient_width: int | None = None,
    zero_result: int = 0,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile fixed-point unsigned division using a bit-level circuit.

    Args:
        numerator_width (int): The bit width of the numerator.
        denominator_width (int): The bit width of the denominator.

    Returns:
        Any: The resulting value.

    Example:
        ```python
        from concrete_fhe_toolkit.math.binary_division import compile_fixed_point_divide

        circuit = compile_fixed_point_divide(4, 4, fractional_bits=2)
        print(circuit.encrypt_run_decrypt(5, 2))  # floor((5 << 2) / 2) = 10
        ```
    """
    n_width = _validate_width("numerator_width", numerator_width)
    d_width = _validate_width("denominator_width", denominator_width)
    function = make_fixed_point_divide(
        n_width,
        d_width,
        fractional_bits=fractional_bits,
        quotient_width=quotient_width,
        zero_result=zero_result,
    )
    return compile_function(
        function,
        {"numerator": "encrypted", "denominator": "encrypted"},
        _division_inputset(n_width, d_width),
        configuration,
    )

unsigned_mod_bits

unsigned_mod_bits(numerator_bits: Iterable[Any], denominator_bits: Iterable[Any], *, zero_result: int = 0) -> tuple[Any, ...]

Return little-endian remainder bits for unsigned modulo.

The remainder always fits in the denominator's bit width. Division by zero returns the clear fallback encoded by zero_result.

Parameters:

Name Type Description Default
numerator_bits Iterable[...]

An iterable of encrypted or cleartext bits representing the numerator in little-endian format.

required
denominator_bits Iterable[...]

An iterable of encrypted or cleartext bits representing the denominator in little-endian format.

required

Returns:

Type Description
tuple[Any, ...]

tuple[...]: The resulting value.

Example
from concrete_fhe_toolkit.math.binary_division import unsigned_mod_bits

num_bits = [1, 0, 1]  # 5
den_bits = [0, 1]     # 2
# Use `unsigned_mod_bits(num_bits, den_bits)` inside an FHE program compilation
Source code in src/concrete_fhe_toolkit/math/binary_division.py
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
def unsigned_mod_bits(
    numerator_bits: Iterable[Any],
    denominator_bits: Iterable[Any],
    *,
    zero_result: int = 0,
) -> tuple[Any, ...]:
    """Return little-endian remainder bits for unsigned modulo.

    The remainder always fits in the denominator's bit width. Division by
    zero returns the clear fallback encoded by ``zero_result``.

    Args:
        numerator_bits (Iterable[...]): An iterable of encrypted or cleartext bits representing the numerator in little-endian format.
        denominator_bits (Iterable[...]): An iterable of encrypted or cleartext bits representing the denominator in little-endian format.

    Returns:
        tuple[...]: The resulting value.

    Example:
        ```python
        from concrete_fhe_toolkit.math.binary_division import unsigned_mod_bits

        num_bits = [1, 0, 1]  # 5
        den_bits = [0, 1]     # 2
        # Use `unsigned_mod_bits(num_bits, den_bits)` inside an FHE program compilation
        ```
    """
    numerator = _as_nonempty_bits("numerator_bits", numerator_bits)
    denominator = _as_nonempty_bits("denominator_bits", denominator_bits)
    width = len(denominator)
    zero = _validate_zero_result(zero_result, width)

    _, remainder = _restoring_divide_bits(numerator, denominator, len(numerator))
    denominator_nonzero = bit_or_many(denominator)
    fallback = unsigned_to_bits(zero, width)
    return tuple(
        bit_select(denominator_nonzero, remainder[index], fallback[index])
        for index in range(width)
    )

make_unsigned_mod

make_unsigned_mod(numerator_width: int, denominator_width: int, *, zero_result: int = 0) -> Any

Create unsigned encrypted modulo from scalar integer inputs.

Parameters:

Name Type Description Default
numerator_width int

The bit width of the numerator.

required
denominator_width int

The bit width of the denominator.

required

Returns:

Name Type Description
Any Any

The resulting value.

Example
from concrete_fhe_toolkit.math.binary_division import make_unsigned_mod

mod_fn = make_unsigned_mod(numerator_width=4, denominator_width=4)
# Use `mod_fn(num, den)` inside an FHE program compilation
Source code in src/concrete_fhe_toolkit/math/binary_division.py
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
def make_unsigned_mod(
    numerator_width: int,
    denominator_width: int,
    *,
    zero_result: int = 0,
) -> Any:
    """Create unsigned encrypted modulo from scalar integer inputs.

    Args:
        numerator_width (int): The bit width of the numerator.
        denominator_width (int): The bit width of the denominator.

    Returns:
        Any: The resulting value.

    Example:
        ```python
        from concrete_fhe_toolkit.math.binary_division import make_unsigned_mod

        mod_fn = make_unsigned_mod(numerator_width=4, denominator_width=4)
        # Use `mod_fn(num, den)` inside an FHE program compilation
        ```
    """
    n_width = _validate_width("numerator_width", numerator_width)
    d_width = _validate_width("denominator_width", denominator_width)
    zero = _validate_zero_result(zero_result, d_width)

    def modulo(numerator: Any, denominator: Any) -> Any:
        """Execute the unsigned modulo circuit on inputs.

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

        Returns:
            The remainder of the unsigned division.
        """
        numerator_bits = integer_to_bits(numerator, n_width)
        denominator_bits = integer_to_bits(denominator, d_width)
        remainder_bits = unsigned_mod_bits(
            numerator_bits,
            denominator_bits,
            zero_result=zero,
        )
        return bits_to_unsigned(remainder_bits)

    return modulo

compile_unsigned_mod

compile_unsigned_mod(numerator_width: int, denominator_width: int, *, zero_result: int = 0, configuration: Optional[Configuration] = None) -> fhe.Circuit

Compile unsigned modulo using the bit-level restoring circuit.

Parameters:

Name Type Description Default
numerator_width int

The bit width of the numerator.

required
denominator_width int

The bit width of the denominator.

required

Returns:

Name Type Description
Any Circuit

The resulting value.

Example
from concrete_fhe_toolkit.math.binary_division import compile_unsigned_mod

circuit = compile_unsigned_mod(numerator_width=4, denominator_width=4)
print(circuit.encrypt_run_decrypt(10, 3))  # 1
Source code in src/concrete_fhe_toolkit/math/binary_division.py
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
def compile_unsigned_mod(
    numerator_width: int,
    denominator_width: int,
    *,
    zero_result: int = 0,
    configuration: Optional[fhe.Configuration] = None,
) -> fhe.Circuit:
    """Compile unsigned modulo using the bit-level restoring circuit.

    Args:
        numerator_width (int): The bit width of the numerator.
        denominator_width (int): The bit width of the denominator.

    Returns:
        Any: The resulting value.

    Example:
        ```python
        from concrete_fhe_toolkit.math.binary_division import compile_unsigned_mod

        circuit = compile_unsigned_mod(numerator_width=4, denominator_width=4)
        print(circuit.encrypt_run_decrypt(10, 3))  # 1
        ```
    """
    n_width = _validate_width("numerator_width", numerator_width)
    d_width = _validate_width("denominator_width", denominator_width)
    function = make_unsigned_mod(
        n_width,
        d_width,
        zero_result=zero_result,
    )
    return compile_function(
        function,
        {"numerator": "encrypted", "denominator": "encrypted"},
        _division_inputset(n_width, d_width),
        configuration,
    )