Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: Desilo/liberate-fhe
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.9.0
Choose a base ref
...
head repository: Desilo/liberate-fhe
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref

Commits on Nov 23, 2023

  1. Merge pull request #2 from Desilo/1-v090

    update Liberate.FHE v0.9.0
    juwhan-k authored Nov 23, 2023
    Copy the full SHA
    e51152f View commit details
  2. modify setup.py

    hanyul-ryu committed Nov 23, 2023
    Copy the full SHA
    67f66df View commit details
  3. Update README.md

    Fixed citation.
    juwhan-k authored Nov 23, 2023
    Copy the full SHA
    cab596c View commit details

Commits on Nov 24, 2023

  1. Update README.md

    typo
    hanyul-ryu authored Nov 24, 2023
    Copy the full SHA
    1acbf6c View commit details
  2. Update pyproject.toml

    typo
    hanyul-ryu authored Nov 24, 2023
    Copy the full SHA
    2b09384 View commit details
  3. Update README.md

    typo
    hanyul-ryu authored Nov 24, 2023
    Copy the full SHA
    ab051c7 View commit details

Commits on Nov 25, 2023

  1. README.md typo fix

    We use logN=15 for silver.
    Yongwoo-Lee-ccl authored Nov 25, 2023
    Copy the full SHA
    9517514 View commit details
  2. Merge pull request #3 from Yongwoo-Lee-ccl/patch-1

    README.md typo fix
    juwhan-k authored Nov 25, 2023
    Copy the full SHA
    65d35ea View commit details

Commits on Nov 29, 2023

  1. typos

    hanyul-ryu committed Nov 29, 2023
    Copy the full SHA
    54d0a4d View commit details

Commits on Dec 12, 2023

  1. Update rns_partition.py

    Dead code removal request
    hanyul-ryu authored Dec 12, 2023
    Copy the full SHA
    ce31d09 View commit details
  2. Update csprng.py

    Rationalized length calculation.
    hanyul-ryu authored Dec 12, 2023
    Copy the full SHA
    8df4f39 View commit details
  3. Merge pull request #7 from Desilo/6-dead-code-removal-request

    6 dead code removal request
    desilo-yongwoo authored Dec 12, 2023
    Copy the full SHA
    eca7773 View commit details

Commits on Dec 22, 2023

  1. Copy the full SHA
    bfa88ad View commit details
  2. Copy the full SHA
    7883614 View commit details

Commits on Jan 3, 2024

  1. Stability improvement and decrypt rounding.

    Juwhan Kim authored and Juwhan Kim committed Jan 3, 2024
    Copy the full SHA
    408fccd View commit details
  2. Merge pull request #10 from Desilo/9-improve-stability

    Stability improvement and decrypt rounding.
    juwhan-k authored Jan 3, 2024
    Copy the full SHA
    0d71574 View commit details
  3. Copy the full SHA
    9cb001c View commit details
  4. Merge pull request #12 from Desilo/11-fix-cov

    Removed rescale_every from cov
    hanyul-ryu authored Jan 3, 2024
    Copy the full SHA
    82567d6 View commit details

Commits on Jan 5, 2024

  1. Copy the full SHA
    4daf9c6 View commit details
  2. Merge pull request #14 from Desilo/13-remove-length-parameter-from-th…

    …e-csprng-api
    
    Removed length from the csprng API.
    hanyul-ryu authored Jan 5, 2024
    Copy the full SHA
    25aff4d View commit details

Commits on Jan 11, 2024

  1. Merge branch 'main' into dev

    juwhan-k committed Jan 11, 2024
    Copy the full SHA
    9feb1b9 View commit details
  2. Merge pull request #15 from Desilo/dev

    Dev synchronized with main
    juwhan-k authored Jan 11, 2024
    Copy the full SHA
    c75c8d8 View commit details

Commits on Jan 24, 2024

  1. Improved create_switcher

    hanyul-ryu committed Jan 24, 2024
    Copy the full SHA
    bce0adb View commit details
  2. Merge pull request #16 from Desilo/dev

    Improved create_switcher
    hanyul-ryu authored Jan 24, 2024
    Copy the full SHA
    a61e2b2 View commit details

Commits on Aug 6, 2024

  1. Update pyproject.toml

    fix torch version
    hanyul-ryu authored Aug 6, 2024
    Copy the full SHA
    c176105 View commit details
  2. Create clean.py

    add clean.py
    hanyul-ryu authored Aug 6, 2024
    Copy the full SHA
    268ae01 View commit details
  3. Update pyproject.toml

    hanyul-ryu authored Aug 6, 2024
    Copy the full SHA
    1692a8c View commit details

Commits on Aug 18, 2024

  1. update poetry.lock

    pignuante committed Aug 18, 2024
    Copy the full SHA
    b3858e9 View commit details
  2. Copy the full SHA
    488ee91 View commit details
  3. update poetry.lock

    pignuante committed Aug 18, 2024
    Copy the full SHA
    51aed6c View commit details
  4. update poetry.lock

    pignuante committed Aug 18, 2024
    Copy the full SHA
    fcfb711 View commit details

Commits on Jan 2, 2025

  1. Copy the full SHA
    831eadd View commit details
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -13,8 +13,7 @@ Additionally, several design decisions were made to maximize the usability of th
- Make the number of dependencies minimal.
- Make the software easily hackable.
- Set the usage of multiple GPUs as the default.
- Make the resulting library easily integrated with the pre-existing software, especially Artificial Intelligence (
AI) related ones.
- Make the resulting library easily integrated with the pre-existing software, especially Artificial Intelligence (AI) related ones.

# Key Features

@@ -30,7 +29,7 @@ from liberate import fhe
from liberate.fhe import presets

# Generate CKKS engine with preset parameters
grade = "silver" # logN=14
grade = "silver" # logN=15
params = presets.params[grade]

engine = fhe.ckks_engine(**params, verbose=True)
@@ -66,7 +65,8 @@ the [official documentation](https://docs.desilo.ai/liberate-fhe/getting-started
### Clone this repository

```shell
git clone https://github.com/Desilo/liberate.git
git clone https://github.com/Desilo/liberate-fhe.git
cd liberate-fhe
```

### Install dependencies
@@ -108,7 +108,7 @@ instructions, examples, and documentation.
title={{Liberate.FHE: A New FHE Library for Bridging the Gap between Theory and Practice with a Focus on Performance and Accuracy}},
author={DESILO},
year={2023},
note={\url{https://github.com/Desilo/liberate}},
note={\url{https://github.com/Desilo/liberate-fhe}},
}
```

29 changes: 29 additions & 0 deletions clean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pathlib
import shutil


def clean_project():
"""
Remove common build directories and *.so files in a Python project.
"""
# Directories to remove
dirs_to_remove = [
"__pycache__", ".pytest_cache",
"build", "*.egg-info", # "dist",
]
# File patterns to remove
files_to_remove = ["*.so"]

for path in pathlib.Path(".").rglob("*"):
# Remove specified directories
if path.is_dir() and any(path.match(d) for d in dirs_to_remove):
shutil.rmtree(path, ignore_errors=True)
print(f"Removed directory: {path}")
# Remove specified files
elif path.is_file() and any(path.match(f) for f in files_to_remove):
path.unlink()
print(f"Removed file: {path}")


if __name__ == "__main__":
clean_project()
968 changes: 513 additions & 455 deletions poetry.lock

Large diffs are not rendered by default.

16 changes: 11 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -13,13 +13,16 @@ maintainers = [
readme = [
"README.md"
]

homepage = "https://desilo.ai/"
documentation = "https://docs.desilo.ai/liberate-fhe/"
repository = "https://github.com/desilo/liberate"
repository = "https://github.com/desilo/liberate-fhe/"

license = "BSD-3-Clause-Clear"

keywords = [
"DESILO", "python", "cryptohraphy", "privacy", "encryption", "cuda",
"homoomrphic encryption", "homomorphic encryption library", "fhe", "gpu accelerated", "liberate"
"DESILO", "python", "cryptoghraphy", "privacy", "encryption", "cuda",
"homomorphic encryption", "homomorphic encryption library", "fhe", "gpu accelerated", "liberate"
]
classifiers = [
"Environment :: GPU :: NVIDIA CUDA",
@@ -45,13 +48,13 @@ include = [
]

[tool.poetry.dependencies]
python = ">=3.10,<3.12"
python = ">=3.10,<3.13"
numpy = "^1.23.5"
mpmath = "^1.3.0"
scipy = "^1.10.1"
matplotlib = "^3.7.1"
joblib = "^1.2.0"
torch = "^2.0.0"
torch = "==2.2.1"
tqdm = "^4.66.1"

[tool.poetry.group.dev.dependencies]
@@ -73,3 +76,6 @@ line-length = 79
[tool.isort]
profile = "black"
line_length = 79

[tool.poetry.scripts]
clean = "clean:clean_project"
25 changes: 13 additions & 12 deletions setup.py
Original file line number Diff line number Diff line change
@@ -5,29 +5,29 @@
CUDAExtension(
name="randint_cuda",
sources=[
"liberate/csprng/randint.cpp",
"liberate/csprng/randint_cuda_kernel.cu",
"src/liberate/csprng/randint.cpp",
"src/liberate/csprng/randint_cuda_kernel.cu",
],
),
CUDAExtension(
name="randround_cuda",
sources=[
"liberate/csprng/randround.cpp",
"liberate/csprng/randround_cuda_kernel.cu",
"src/liberate/csprng/randround.cpp",
"src/liberate/csprng/randround_cuda_kernel.cu",
],
),
CUDAExtension(
name="discrete_gaussian_cuda",
sources=[
"liberate/csprng/discrete_gaussian.cpp",
"liberate/csprng/discrete_gaussian_cuda_kernel.cu",
"src/liberate/csprng/discrete_gaussian.cpp",
"src/liberate/csprng/discrete_gaussian_cuda_kernel.cu",
],
),
CUDAExtension(
name="chacha20_cuda",
sources=[
"liberate/csprng/chacha20.cpp",
"liberate/csprng/chacha20_cuda_kernel.cu",
"src/liberate/csprng/chacha20.cpp",
"src/liberate/csprng/chacha20_cuda_kernel.cu",
],
),
]
@@ -36,8 +36,8 @@
CUDAExtension(
name="ntt_cuda",
sources=[
"liberate/ntt/ntt.cpp",
"liberate/ntt/ntt_cuda_kernel.cu",
"src/liberate/ntt/ntt.cpp",
"src/liberate/ntt/ntt_cuda_kernel.cu",
],
)
]
@@ -50,7 +50,7 @@
script_args=["build_ext"],
options={
"build": {
"build_lib": "liberate/csprng",
"build_lib": "src/liberate/csprng",
}
},
)
@@ -62,7 +62,8 @@
cmdclass={"build_ext": BuildExtension},
options={
"build": {
"build_lib": "liberate/ntt",
"build_lib": "src/liberate/ntt",
}
},
)

32 changes: 10 additions & 22 deletions src/liberate/csprng/csprng.py
Original file line number Diff line number Diff line change
@@ -222,23 +222,18 @@ def generate_nonce(self, seed):
# nonce is 64bits.
return self.generate_initial_bytes(8, seed=None)

def randbytes(self, shares=None, repeats=0, length=None, reshape=False):
def randbytes(self, shares=None, repeats=0, reshape=False):
# Generates (shares_i + repeats) X length random bytes.
if shares is None:
shares = self.shares

if length is None:
L = self.L
else:
L = length

# Set the target states.
target_states = []
for devi in range(self.num_devices):
start_channel = self.shares[devi] - shares[devi]
end_channel = self.shares[devi] + repeats
device_states = self.channeled_states[devi][
start_channel:end_channel, :L, :
start_channel:end_channel, :, :
]
target_states.append(device_states.view(-1, 16))

@@ -247,22 +242,17 @@ def randbytes(self, shares=None, repeats=0, length=None, reshape=False):

# If not reshape, flatten.
if reshape:
random_bytes = [rb.view(-1, L, 16) for rb in random_bytes]
random_bytes = [rb.view(-1, self.L, 16) for rb in random_bytes]

return random_bytes

def randint(self, amax=3, shift=0, repeats=0, length=None):
def randint(self, amax=3, shift=0, repeats=0):
# The default values are for generating the same uniform ternary
# arrays in all GPUs.

if not isinstance(amax, (list, tuple)):
amax = [[amax] for share in self.shares]

if length is None:
L = self.L
else:
L = length

# Calculate shares.
# If repeats are greater than 0, those channels are
# subtracted from shares.
@@ -278,7 +268,7 @@ def randint(self, amax=3, shift=0, repeats=0, length=None):
start_channel = self.shares[devi] - shares[devi]
end_channel = self.shares[devi] + repeats
device_states = self.channeled_states[devi][
start_channel:end_channel, :L, :
start_channel:end_channel, :, :
]
target_states.append(device_states)

@@ -289,24 +279,19 @@ def randint(self, amax=3, shift=0, repeats=0, length=None):

return rand_int

def discrete_gaussian(self, non_repeats=0, repeats=1, length=None):
def discrete_gaussian(self, non_repeats=0, repeats=1):
if not isinstance(non_repeats, (list, tuple)):
shares = [non_repeats] * self.num_devices
else:
shares = non_repeats

if length is None:
L = self.L
else:
L = length

# Set the target states.
target_states = []
for devi in range(self.num_devices):
start_channel = self.shares[devi] - shares[devi]
end_channel = self.shares[devi] + repeats
device_states = self.channeled_states[devi][
start_channel:end_channel, :L, :
start_channel:end_channel, :, :
]
target_states.append(device_states.view(-1, 16))

@@ -327,6 +312,9 @@ def randround(self, coef):
"""Randomly round coef. Coef must be a double tensor.
coef must reside in the fist GPU in the GPUs list"""

# The following slicing is OK, since we're using only the first
# contiguous stream of states.
# It will not make the target state strided.
L = self.num_coefs // 16
rand_bytes = chacha20_cuda.chacha20((self.states[0][:L],), self.inc)[
0
348 changes: 228 additions & 120 deletions src/liberate/fhe/ckks_engine.py

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/liberate/fhe/context/ckks_context.py
Original file line number Diff line number Diff line change
@@ -157,7 +157,7 @@ def __init__(
num_scales=None,
num_special_primes=2,
sigma=3.2,
uniform_tenary_secret=True,
uniform_ternary_secret=True,
cache_folder=CACHE_FOLDER,
security_bits=128,
quantum="post_quantum",
@@ -203,11 +203,11 @@ def __init__(
self.distribution = distribution
# Sampling strategy.
self.sigma = sigma
self.uniform_tenary_secret = uniform_tenary_secret
if self.uniform_tenary_secret:
self.secret_key_sampling_method = "uniform tenary"
self.uniform_ternary_secret = uniform_ternary_secret
if self.uniform_ternary_secret:
self.secret_key_sampling_method = "uniform ternary"
else:
self.secret_key_sampling_method = "sparse tenary"
self.secret_key_sampling_method = "sparse ternary"

# dtypes.
self.torch_dtype = {30: torch.int32, 62: torch.int64}[
14 changes: 7 additions & 7 deletions src/liberate/fhe/context/security_parameters.py
Original file line number Diff line number Diff line change
@@ -15,8 +15,8 @@
# We separate them in respective dictionaries.
#
# Also, there are 3 different methods of sampling the messages according to respective distributions.
# Those are uniform, error, and (-1, 1) (tenary).
# We differentiate the message distribution by dictionary keys: 'uniform', 'error', and 'tenary'.
# Those are uniform, error, and (-1, 1) (ternary).
# We differentiate the message distribution by dictionary keys: 'uniform', 'error', and 'ternary'.

# This is the pre-quantum security requirements.
logq_preq = {}
@@ -60,7 +60,7 @@
613,
478,
]
logq_preq["tenary"] = [
logq_preq["ternary"] = [
27,
19,
14,
@@ -123,7 +123,7 @@
573,
445,
]
logq_postq["tenary"] = [
logq_postq["ternary"] = [
25,
17,
13,
@@ -158,7 +158,7 @@ def partitq(q):

# Gather up.
logq = {}
distributions = ["uniform", "error", "tenary"]
distributions = ["uniform", "error", "ternary"]
logq["pre_quantum"] = {
distributions[disti]: partitq(logq_preq[dist])
for disti, dist in enumerate(distributions)
@@ -176,7 +176,7 @@ def minimum_cyclotomic_order(
"pre_quantum",
"post_quantum",
], "Wrong quantum security model!!!"
assert distribution in ["uniform", "error", "tenary"]
assert distribution in ["uniform", "error", "ternary"]
assert security_bits in [128, 192, 256]

x = logq[quantum][distribution][security_bits]
@@ -192,7 +192,7 @@ def maximum_qbits(
"pre_quantum",
"post_quantum",
], "Wrong quantum security model!!!"
assert distribution in ["uniform", "error", "tenary"]
assert distribution in ["uniform", "error", "ternary"]
assert security_bits in [128, 192, 256]

x = cyclotomic_n
1 change: 0 additions & 1 deletion src/liberate/fhe/presets/params.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@
"logN": 14,
"num_special_primes": 1,
"devices": [0],
"bias_guard": False,
"scale_bits": 40,
"num_scales": None,
},
2 changes: 1 addition & 1 deletion src/liberate/fhe/tests/test_generate_engine.py
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ def ckks_engine(
"num_special_primes": 2,
"buffer_bit_length": 62,
"sigma": 3.2,
"uniform_tenary_secret": True,
"uniform_ternary_secret": True,
"cache_folder": "cache/",
"quantum": "post_quantum",
"distribution": "uniform",
6 changes: 0 additions & 6 deletions src/liberate/ntt/rns_partition.py
Original file line number Diff line number Diff line change
@@ -56,12 +56,6 @@ def __init__(
self.num_scales = self.num_ordinary_primes - 1

self.base_prime_idx = self.num_ordinary_primes - 1
self.special_prime_idx = list(
range(
self.num_ordinary_primes + 1,
self.num_ordinary_primes + 1 + self.num_special_primes,
)
)

self.compute_destination_arrays()
self.compute_rescaler_locations()