Convert Figma logo to code with AI

zama-ai logoconcrete

Concrete: TFHE Compiler that converts python programs into FHE equivalent

1,202
139
1,202
17

Top Related Projects

3,572

Microsoft SEAL is an easy-to-use and powerful homomorphic encryption library.

9,470

Perform data science on data that remains in someone else's server

An FHE compiler for C++

Quick Overview

Concrete is a Python library for working with homomorphic encryption, a cryptographic technique that allows computations to be performed on encrypted data without decrypting it. It provides a high-level API for working with various homomorphic encryption schemes, including CKKS, BGV, and BFV.

Pros

  • Abstraction: Concrete provides a unified API for working with different homomorphic encryption schemes, making it easier to switch between them.
  • Performance: The library is designed for efficiency, with optimized implementations of the underlying cryptographic operations.
  • Flexibility: Concrete supports a wide range of homomorphic encryption use cases, from simple arithmetic operations to more complex machine learning tasks.
  • Active Development: The project is actively maintained and regularly updated with new features and improvements.

Cons

  • Complexity: Homomorphic encryption is a complex and advanced cryptographic technique, and Concrete's API may have a steep learning curve for some users.
  • Limited Documentation: While the project has good documentation, it may not be as comprehensive or user-friendly as some users would like.
  • Performance Tradeoffs: Depending on the specific use case, the performance of homomorphic encryption operations may not be as efficient as other cryptographic techniques.
  • Limited Language Support: Concrete is currently only available as a Python library, which may limit its adoption in other programming environments.

Code Examples

Here are a few examples of how to use the Concrete library:

  1. Performing Simple Arithmetic Operations:
from concrete.common.encoding import Encoding
from concrete.ckks.ckks import CKKSContext, CKKSSecretKey, CKKSPublicKey

# Create a CKKS context and keys
context = CKKSContext()
secret_key = CKKSSecretKey(context)
public_key = CKKSPublicKey(context, secret_key)

# Encode some data
x = Encoding([1.0, 2.0, 3.0], context)
y = Encoding([4.0, 5.0, 6.0], context)

# Perform homomorphic addition and multiplication
z = x + y
w = x * y

# Decrypt the results
print(z.decode())  # Output: [5.0, 7.0, 9.0]
print(w.decode())  # Output: [4.0, 10.0, 18.0]
  1. Applying a Polynomial Function:
from concrete.ckks.ckks import CKKSContext, CKKSSecretKey, CKKSPublicKey
from concrete.common.encoding import Encoding

# Create a CKKS context and keys
context = CKKSContext()
secret_key = CKKSSecretKey(context)
public_key = CKKSPublicKey(context, secret_key)

# Encode some data
x = Encoding([1.0, 2.0, 3.0], context)

# Define a polynomial function
coefficients = [1.0, 2.0, 3.0]
polynomial = sum(c * x ** i for i, c in enumerate(coefficients))

# Evaluate the polynomial homomorphically
result = polynomial.evaluate()

# Decrypt the result
print(result.decode())  # Output: [6.0, 24.0, 54.0]
  1. Performing Homomorphic Machine Learning:
from concrete.ckks.ckks import CKKSContext, CKKSSecretKey, CKKSPublicKey
from concrete.common.encoding import Encoding
import numpy as np

# Create a CKKS context and keys
context = CKKSContext()
secret_key = CKKSSecretKey(context)
public_key = CKKSPublicKey(context, secret_key)

# Encode some data
X = Encoding(np.random.rand(10, 5), context)
y = Encoding(np.random.rand(10), context)

# Train a linear regression model homomorphically
w = Encoding(np.random.rand(5), context)
b = Encoding

Competitor Comparisons

3,572

Microsoft SEAL is an easy-to-use and powerful homomorphic encryption library.

Pros of SEAL

  • SEAL is a mature and well-documented library, with a large and active community.
  • SEAL provides a wide range of features, including support for various homomorphic encryption schemes.
  • SEAL has been extensively tested and is widely used in industry and academia.

Cons of SEAL

  • SEAL has a steeper learning curve compared to Concrete, especially for beginners in homomorphic encryption.
  • SEAL may be more resource-intensive than Concrete, as it supports a broader range of features.
  • SEAL's licensing may be less permissive than Concrete's, which could be a consideration for some users.

Code Comparison

SEAL:

auto context = SEALContext::Create(parms);
Encryptor encryptor(context, public_key);
Evaluator evaluator(context);
Decryptor decryptor(context, secret_key);

Plaintext plain_a, plain_b;
Ciphertext cipher_a, cipher_b, cipher_result;

encryptor.Encrypt(plain_a, cipher_a);
encryptor.Encrypt(plain_b, cipher_b);
evaluator.Add(cipher_a, cipher_b, cipher_result);

Concrete:

from concrete.seal import *

context = SEALContext(...)
encryptor = Encryptor(context, public_key)
evaluator = Evaluator(context)
decryptor = Decryptor(context, secret_key)

plain_a = Plaintext(...)
plain_b = Plaintext(...)
cipher_a = encryptor.encrypt(plain_a)
cipher_b = encryptor.encrypt(plain_b)
cipher_result = evaluator.add(cipher_a, cipher_b)
9,470

Perform data science on data that remains in someone else's server

Pros of PySyft

  • PySyft provides a comprehensive set of tools for secure and private machine learning, including support for federated learning, differential privacy, and encrypted computation.
  • PySyft has a large and active community, with regular updates and a wealth of documentation and tutorials.
  • PySyft integrates with popular machine learning frameworks like PyTorch and TensorFlow, making it easy to incorporate into existing projects.

Cons of PySyft

  • PySyft has a steeper learning curve compared to Concrete, as it covers a broader range of privacy-preserving techniques.
  • PySyft may be overkill for some use cases that only require basic privacy-preserving features.
  • PySyft's focus on secure and private machine learning may not be as relevant for projects that don't involve sensitive data.

Code Comparison

PySyft (PyTorch-based):

import torch
import syft as sy

hook = sy.TorchHook(torch)
bob = sy.VirtualWorker(hook, id="bob")
alice = sy.VirtualWorker(hook, id="alice")

x = torch.tensor([1, 2, 3, 4, 5]).send(bob)
y = torch.tensor([1, 1, 1, 1, 1]).send(alice)
z = x + y
print(z.get())

Concrete:

from concrete.common.types import FieldType
from concrete.compiler.compiler import compile_circuit

# Define the circuit
circuit = [
    ("add", [FieldType.PRIVATE, FieldType.PRIVATE], FieldType.PRIVATE),
    ("mul", [FieldType.PRIVATE, FieldType.PRIVATE], FieldType.PRIVATE),
]

# Compile the circuit
compiled_circuit = compile_circuit(circuit)

An FHE compiler for C++

Pros of google/fully-homomorphic-encryption

  • Comprehensive documentation and resources for understanding and implementing fully homomorphic encryption (FHE)
  • Actively maintained and updated by the Google team, ensuring the latest advancements in FHE are available
  • Supports multiple FHE schemes, providing flexibility in choosing the appropriate algorithm for a given use case

Cons of google/fully-homomorphic-encryption

  • Complexity of FHE algorithms can make it challenging for newcomers to understand and implement
  • Performance overhead of FHE operations may limit its practical applications in certain scenarios
  • Limited support for specific programming languages or environments, depending on the FHE scheme used

Code Comparison

Concrete:

use concrete::*;

fn main() {
    let mut rng = rand::thread_rng();
    let (sk, pk) = LweParams::default().generate_keys(&mut rng);
    let message = LweSample::encode(42, &pk);
    let encrypted = pk.encrypt(&message, &mut rng);
    let decrypted = sk.decrypt(&encrypted);
    println!("Decrypted message: {}", decrypted.decode());
}

Fully Homomorphic Encryption:

from google.cloud.tink import tink
from google.cloud.tink.cc.pybind.tink import FHEContext

def main():
    # Initialize the FHE context
    fhe_context = FHEContext()

    # Generate a new FHE key pair
    key_template = tink.KeyTemplates.get('FHE_CKKS_RAW')
    key_set = tink.KeysetHandle.generate_new(key_template)
    private_key = key_set.get_primary_private_key()

    # Encrypt a plaintext
    plaintext = 42
    ciphertext = fhe_context.encrypt(private_key, plaintext)

    # Perform homomorphic operations on the ciphertext
    result = fhe_context.add(ciphertext, ciphertext)

    # Decrypt the result
    decrypted = fhe_context.decrypt(private_key, result)
    print(f'Decrypted result: {decrypted}')

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Zama Concrete


📒 Documentation | 💛 Community support | 📚 FHE resources by Zama

SLSA 3

About

What is Concrete

Concrete is an open-source FHE Compiler that simplifies the use of fully homomorphic encryption (FHE). Concrete framework contains a TFHE Compiler based on LLVM, making writing FHE programs an easy task for developers.

Fully Homomorphic Encryption (FHE) enables performing computations on encrypted data directly without the need to decrypt it first. FHE allows developers to build services that ensure privacy for all users. FHE is also an excellent solution against data breaches as everything is performed on encrypted data. Even if the server is compromised, no sensitive data will be leaked.

Concrete is a versatile library that can be used for a variety of purposes. For instance, Concrete ML is built on top of Concrete to simplify Machine-Learning oriented use cases.

Table of Contents

Getting Started

Installation

Depending on your OS, Concrete may be installed with Docker or with pip:

OS / HWAvailable on DockerAvailable on PyPI
LinuxYesYes
WindowsYesNo
Windows Subsystem for LinuxYesYes
macOS 11+ (Intel)YesYes
macOS 11+ (Apple Silicon: M1, M2, etc.)Coming soonYes

Pip

The preferred way to install Concrete is through PyPI:

pip install -U pip wheel setuptools
pip install concrete-python

Note: Not all versions are available on PyPI. If you need a version that is not on PyPI (including nightly releases), you can install it from our package index by adding --index-url https://pypi.zama.ai/cpu.

Note: Wheels with GPU support are not on PyPI. You can install it from our package index by adding --index-url https://pypi.zama.ai/gpu, more information on GPU wheels here.

Docker

You can get the concrete-python docker image by pulling the latest docker image:

docker pull zamafhe/concrete-python:v2.0.0

Find more detailed installation instructions in this part of the documentation

↑ Back to top

A simple example

To compute on encrypted data, you first need to define the function you want to compute, then compile it into a Concrete Circuit, which you can use to perform homomorphic evaluation. Here is the full example:

from concrete import fhe

def add(x, y):
    return x + y

compiler = fhe.Compiler(add, {"x": "encrypted", "y": "encrypted"})

inputset = [(2, 3), (0, 0), (1, 6), (7, 7), (7, 1), (3, 2), (6, 1), (1, 7), (4, 5), (5, 4)]

print(f"Compilation...")
circuit = compiler.compile(inputset)

print(f"Key generation...")
circuit.keygen()

print(f"Homomorphic evaluation...")
encrypted_x, encrypted_y = circuit.encrypt(2, 6)
encrypted_result = circuit.run(encrypted_x, encrypted_y)
result = circuit.decrypt(encrypted_result)

assert result == add(2, 6)

This example is explained in more detail in this part of the documentation.

↑ Back to top

[!Note] Zama 5-Question Developer Survey

We want to hear from you! Take 1 minute to share your thoughts and helping us enhance our documentation and libraries. 👉 Click here to participate.

Resources

Concrete deep dive

Tutorials

Explore more useful resources in Concrete tutorials and Awesome Zama repo. If you have built awesome projects using Concrete, please let us know and we will be happy to showcase them here!

Documentation

Full, comprehensive documentation is available at https://docs.zama.ai/concrete.

↑ Back to top

Working with Concrete

Citations

To cite Concrete in academic papers, please use the following entry:

@Misc{Concrete,
  title={{Concrete: TFHE Compiler that converts python programs into FHE equivalent}},
  author={Zama},
  year={2022},
  note={\url{https://github.com/zama-ai/concrete}},
}

Contributing

There are two ways to contribute to Concrete. You can:

  • Open issues to report bugs and typos, or to suggest new ideas
  • Request to become an official contributor by emailing hello@zama.ai.

Becoming an approved contributor involves signing our Contributor License Agreement (CLA). Only approved contributors can send pull requests (PRs), so get in touch before you do!

Additionally, you can contribute to advancing the FHE space with Zama by participating in our Bounty Program and Grant Programs!

License

This software is distributed under the BSD-3-Clause-Clear license. Read this for more details.

FAQ

Is Zama’s technology free to use?

Zama’s libraries are free to use under the BSD 3-Clause Clear license only for development, research, prototyping, and experimentation purposes. However, for any commercial use of Zama's open source code, companies must purchase Zama’s commercial patent license.

Everything we do is open source and we are very transparent on what it means for our users, you can read more about how we monetize our open source products at Zama in this blog post.

What do I need to do if I want to use Zama’s technology for commercial purposes?

To commercially use Zama’s technology you need to be granted Zama’s patent license. Please contact us at hello@zama.ai for more information.

Do you file IP on your technology?

Yes, all Zama’s technologies are patented.

Can you customize a solution for my specific use case?

We are open to collaborating and advancing the FHE space with our partners. If you have specific needs, please email us at hello@zama.ai.

↑ Back to top

Support

Support

🌟 If you find this project helpful or interesting, please consider giving it a star on GitHub! Your support helps to grow the community and motivates further development.

↑ Back to top