Bounded integer helpers for compiling common Concrete FHE circuits
Project description
concrete-fhe-toolkit
concrete-fhe-toolkit is an unofficial collection of bounded integer helpers
for Zama Concrete. It turns common notebook
patterns into reusable, validated circuit factories and compiler helpers.
Included operations
- Sign comparison of two encrypted integers
- Ascending or descending bitonic sort
- Minimum and maximum reductions
- Argmin and argmax with explicit first/last tie handling
- Exact floor division through a multivariate table lookup
- Exact
numerator // (left * right)through a multivariate table lookup
All operations use integer inputs. Array sizes and value bounds are fixed when a circuit is compiled.
Installation
pip install concrete-fhe-toolkit
Concrete 2.11 supports Python 3.9 through 3.12 on Linux and macOS.
Quick start
import numpy as np
from concrete_fhe_toolkit import compile_argmin, compile_sort
values = np.array([12, 3, 7, 1, 15, 0, 4, 9], dtype=np.int64)
sort_circuit = compile_sort(size=8, min_value=0, max_value=15)
sorted_values = sort_circuit.encrypt_run_decrypt(values)
argmin_circuit = compile_argmin(size=8, min_value=0, max_value=15)
minimum_index = argmin_circuit.encrypt_run_decrypt(values)
The default tie behavior for compile_argmin and compile_argmax is
tie_break="first", matching NumPy's convention. Use tie_break="last" when
the final matching index is preferred.
Division
Direct x // y does not compile when both x and y are encrypted in
Concrete 2.11. This package uses fhe.multivariate explicitly:
from concrete_fhe_toolkit import compile_floor_divide
circuit = compile_floor_divide(
max_numerator=15,
max_denominator=7,
zero_result=-1,
)
assert circuit.encrypt_run_decrypt(15, 3) == 5
assert circuit.encrypt_run_decrypt(15, 0) == -1
Multivariate table lookups become slower and may stop compiling as the combined input bit width grows. Keep bounds as small as the application permits.
Factories for custom circuits
The make_* functions return traceable functions that can be composed inside a
larger Concrete program:
from concrete import fhe
from concrete_fhe_toolkit import make_minimum
minimum_of_four = make_minimum(size=4, min_value=-8, max_value=7)
compiler = fhe.Compiler(minimum_of_four, {"x": "encrypted"})
When compiling manually, the inputset must include the full declared bounds.
The compile_* helpers do this automatically. Incomplete inputsets can produce
an undersized output type and incorrect results.
Important limits
- Inputs must stay inside the bounds used at compilation.
- Sorting requires a power-of-two array size.
- Min/max and argmin/argmax support any positive fixed size.
- Larger bounds increase table-lookup bit width and cost.
- Concrete supports integer inputs and outputs, not arbitrary floating point.
- FHE execution is probabilistic; choose Concrete error parameters appropriate for the application's risk.
Notebook audit
This package was derived from these Kaggle experiments:
The audit corrected these notebook issues:
- A
compare_swapcell ends inreturn mn, mxdef, so its displayed source is not consistent with the later successful output. - The direct encrypted division cell has no output and does not compile on Concrete 2.11.
- The original argmax encoding selected the last duplicate maximum. The package makes tie handling explicit and defaults to the first index.
- Original lookup tables and value bounds were hard-coded to unsigned 4-bit values. The package validates and parameterizes the bounds.
License and Concrete terms
The package's original code is available under the MIT License. MIT permits commercial and private use, modification, and redistribution while requiring the copyright and license notice to be preserved.
Concrete is a separate dependency with its own BSD-3-Clause-Clear license and patent terms. This package does not change or grant rights under Concrete's license. Review Zama's current licensing terms before commercial use.
This project is not affiliated with or endorsed by Zama.
Author and contributors
Author and maintainer
- Erkan Işık Bacak
Contributors
- Tolga Büyüktanır
- Didem Civelek
- Yusuf Emir Alakuş
Organizational contributor
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file concrete_fhe_toolkit-0.1.1.tar.gz.
File metadata
- Download URL: concrete_fhe_toolkit-0.1.1.tar.gz
- Upload date:
- Size: 13.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1165025c6875d8a544f1928592cb8324d92e7c6f346620c3a58850b4937224b2
|
|
| MD5 |
36012294d3463781e4fc655a6923604d
|
|
| BLAKE2b-256 |
ae2789742d47dc83b741196bbd5d92cb61751938dce4bc0d7b278a8949ef8eff
|
File details
Details for the file concrete_fhe_toolkit-0.1.1-py3-none-any.whl.
File metadata
- Download URL: concrete_fhe_toolkit-0.1.1-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79839dfa5961bc33bed939e578130b9f448d9fce0e49f44cb321446932fefd55
|
|
| MD5 |
655f142da850acd470d20328662e94b4
|
|
| BLAKE2b-256 |
75bef44c5321c284f0323b157d5e3331d217384377f65a14aafbe3ced65de701
|