Skip to content
jeffmur edited this page Feb 17, 2024 · 9 revisions

At the core of this library is an abstract wrapper around existing FHE libraries via Abstract FHE (Afhe) Layer. The Afhe library exposes a set of basic functionalities around existing libraries. The Dart adapter layer, FHE, acts as a lower level C API interface between Flutter and Afhe. Using Object Oriented Programming (OOP) Dart models itself as a library with basic functionalities of the desired backend library.

Topology

Class Language Description
Seal Dart Entrypoint for end-user API, inherits Afhe
Afhe Dart FHE Adapter from C to Dart
FHE C Expose basic functionalities, passed by reference
SEAL C++ Concrete implementation of AFHE
AFHE C++ Abstract definition of FHE functionalities

Foreign Function Interface (FFI)

Figure 1 demonstrates a simplified example of how Dart, Afhe, performs C calls with the Interface, FHE, and interacts with C objects, SEAL, within the memory stack. The glue between Afhe and FHE uses dart:ffi to facilitate communication between components.

---
title: Figure 1 - Foreign Function Interface Between Dart & C/C++ Libraries
---

sequenceDiagram
participant Afhe
participant FHE
participant SEAL

%% Data Flow
Afhe->>FHE: init_backend <br> backend_t::seal
FHE->>SEAL: Create Object
activate SEAL
SEAL->>FHE: Memory Address<br>0x12345
note over Afhe,FHE: Store address as a Pointer<br>Referenced on subsequent calls
Afhe->>FHE: generate_context()<br>{polyModDegree, ptMod}
FHE->>SEAL: ContextGen()<br>{4096, 1024}
SEAL->>FHE: Validate Parameters
FHE->>Afhe: success: valid
FHE->>Afhe: [afhe_context] error: reason
Afhe->>FHE: FHE.free()
FHE->>SEAL: Destroy Object
deactivate SEAL
Loading

Adapter

Adapter Fully Homomorphic Encryption (Afhe) package implements the C interface, fhe in Dart.

The adapter design of this library interfaces with the abstraction layer, written in C. Using dart:ffi, Dart can execute C functions, reference memory addresses of C objects, and convert primitive data types.

---
title: Figure 2 - Adapter
---

classDiagram
class Plaintext {
    String text
    Backend backend
    Pointer obj

    Plaintext(Backend, String)
    Plaintext(Backend, Pointer)
}

class Ciphertext {
    Backend backend
    Pointer library

    Ciphertext(Backend)
    size(): int
}

class Backend {
    int value
    String name

    Backend(String)
}

class Scheme {
    int value
    String name

    Scheme(String)
}

Afhe --* "1" Backend: Must have
Afhe --* "1" Scheme: Must have
Afhe --o "1..n" Plaintext: Contains
Afhe --o "1..n" Ciphertext: Contains

class Afhe {
    Backend backend
    Scheme scheme
    Pointer library

    Afhe(Backend)
    Afhe(Backend, Scheme)

    genContext(Map): void
    genKeys()
    genRelinKeys()
    encrypt(Plaintext): Ciphertext
    decrypt(Ciphertext): Plaintext
    invariantNoiseBudget(Ciphertext): int
    relinearize(Ciphertext): Ciphertext
    encodeVecInt(List~int~): Plaintext
    decodeVecInt(Plaintext, int): List~int~
    add(Ciphertext, Ciphertext): Ciphertext
    addPlain(Ciphertext, Plaintext): Ciphertext
    subtract(Ciphertext, Ciphertext): Ciphertext
    subtractPlain(Ciphertext, Plaintext): Ciphertext
    multiply(Ciphertext, Ciphertext): Ciphertext
    multiplyPlain(Ciphertext, Plaintext): Ciphertext
}

class Seal~Afhe~{
    Seal(Scheme)

    plain(String): Plaintext
    cipher(): Ciphertext
}

Seal --|> Afhe: Inherits

Loading

Legend:

  • Seal: Instanciates Microsoft SEAL backend library, exposes additional methods to simplify api.

  • Afhe: Models the desired backend FHE library and encryption schemas. Enables callers execute basic FHE functionalities.

  • Plaintext: Represents a plaintext value and contains a Pointer with the memory address of AFHE Plaintext.

  • Ciphertext: Represents an encrypted ciphertext value and contains a Pointer with the memory address of AFHE Ciphertext.

  • Backend: Contains integer and string value to convert to C Enum backend, for example backend_t::seal.

  • Scheme: Contains integer and string value to convert to C Enum scheme, for example scheme_t::bfv.

Bridge

Abstract Fully Homomorphic Encryption (AFHE) defines a set of commands to perform basic functionalities of existing Fully Homomorphic Encryption (FHE) libraries.

The bridge design of this library implements an abstraction layer over existing FHE libraries. Through abstraction, we can interface with various backend libraries via the same function calls. The interface layer, FHE, exposes AFHE concrete classes, ex. Aseal, lower level C function to be consumed by the Implementation Layer. Through the use of pointers, we can create/destroy/reference AFHE objects from Dart.

---
title: Figure 3 - Bridge
---
classDiagram

class backend_t {
    <<Enumeration>>
    SEAL
    OpenFHE
}

class scheme_t {
    <<Enumeration>>
    NONE
    BFV
    BGV
    CKKS
}

class Plaintext{
    to_string(): String
}

class Ciphertext{
    size(): Int
}

class AFHE {
    <<Abstract>>
    scheme_t scheme

    ContextGen(scheme_t): string
    KeyGen(): void
    RelinKeyGen(): void
    encrypt(Plaintext): void
    decrypt(Ciphertext): void
    invariant_noise_budget(Ciphertext): int
    relinearize(Ciphertext): Ciphertext
    encode_int(vector~uint64_t~, Plaintext): void
    decode_int(Plaintext, vector~uint64~): void
    add(Ciphertext, Ciphertext, Ciphertext): void
    subtract(Ciphertext, Ciphertext, Ciphertext): void
    multiply(Ciphertext, Ciphertext, Ciphertext): void
}

class SEAL~AFHE~{
    setPublicKey(seal::PublicKey)
    setSecretKey(seal::SecretKey)
}

class FHE {
    <<interface>>
    backend_t_from_string(String): backend_t
    scheme_t_from_string(String): scheme_t
    init_backend(backend_t): AFHE
    init_plaintext(backend_t): Plaintext
    init_ciphertext(backend_t): Ciphertext
    generate_context(backend_t, AFHE, scheme_t): String
    generate_keys(backend_t, AFHE): void
    generate_relin_keys(backend, AFHE): void
    encrypt(backend_t, AFHE, Plaintext): Ciphertext
    decrypt(backend_t, AFHE, Ciphertext): Plaintext
    invariant_noise_budget(backend_t, AFHE, Ciphertext): int
    relinearize(backend_t, Afhe, ACiphertext): Ciphertext
    encode_int(backend_t, AFHE, uint64_t, int): Plaintext
    decode_int(backend_t, AFHE, Plaintext): unint64
    add(backend_t, AFHE, Ciphertext, Ciphertext): Ciphertext
    add_plain(backend_t, AFHE, Ciphertext, Plaintext): Ciphertext
    subtract(backend_t, AFHE, Ciphertext, Ciphertext): Ciphertext
    subtract_plain(backend_t, AFHE, Ciphertext, Plaintext): Ciphertext
    multiply(backend_t, AFHE, Ciphertext, Plaintext): Ciphertext
    multiply_plain(backend_t, AFHE, Ciphertext, Plaintext): Ciphertext
}

AFHE --* "1" backend_t: Must have
AFHE --* "1" scheme_t: Must have

AFHE --o "1..*" Plaintext: Contains
AFHE --o "1..*" Ciphertext: Contains

SEAL --|> AFHE: Inherits
FHE ..> "1..*" AFHE: Depends on


Loading

Legend:

  • AFHE: An abstract class representing the main functionality of the library. It has an integer attribute scheme_t that determines the encryption scheme(s) are supported. It provides methods for generating keys, encrypting and decrypting data, and performing addition operations.

  • SEAL: A concrete, refined abstraction, class that extends Afhe and represents a specific implementation of the library using the SEAL encryption scheme. It provides its own implementations of the encryption, decryption, and addition methods.

  • FHE: An interface that defines a method get_backend for obtaining an instance of Afhe based on a given backend library type, SEAL, that will execute the appropriate function call.

  • Plaintext & Ciphertext: An abstract class representing the basic functionality of text objects. Used as the main interface for backend required parameters.

  • backend_t: An enumeration class representing different Fully Homomorphic Encryption libraries.

  • scheme_t: An enumeration class representing different encryption schemes, including BFV, BGV, and CKKS.

Clone this wiki locally