Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion lib/zama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use std::os::raw::c_char;

use tfhe::prelude::*;
use tfhe::{ConfigBuilder, FheUint8, FheUint32, generate_keys, set_server_key};
use bincode; // Make sure to add bincode to Cargo.toml
use bincode;

// use crate::genome_processing::{encode_genotype, get_genotype_encoding_map};

#[unsafe(no_mangle)]
pub extern "C" fn zama_run() -> *mut c_char {
Expand Down Expand Up @@ -71,7 +73,41 @@ pub extern "C" fn zama_get_client_key() -> *mut c_char {
// Key generation
let (client_key, server_keys) = generate_keys(config);

CString::new(serde_json::to_string(&client_key).unwrap())
.unwrap()
.into_raw()
}


#[unsafe(no_mangle)]
pub extern "C" fn zama_get_server_key() -> *mut c_char {
// Basic configuration to use homomorphic integers
let config = ConfigBuilder::default().build();

// Key generation
let (client_key, server_keys) = generate_keys(config);

CString::new(serde_json::to_string(&server_keys).unwrap())
.unwrap()
.into_raw()
}

// TODO: use a single function in getting the keys
#[unsafe(no_mangle)]
pub extern "C" fn zama_get_keys() -> *mut c_char {
let config = ConfigBuilder::default().build();

let (client_key, server_keys) = generate_keys(config);

// Serialize both keys into a JSON object
let keys = serde_json::json!({
"client_key": client_key,
"server_keys": server_keys,
});

// Serialize the entire JSON object to a string
let json_str = serde_json::to_string(&keys).unwrap();

// Convert to a CString and return a raw pointer
CString::new(json_str).unwrap().into_raw()
}
Binary file added lib/zama/src/libzama.a
Binary file not shown.
10 changes: 10 additions & 0 deletions monadic_ios/includes/lib_headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>

char* zama_run();
char* zama_get_client_key();
char* zama_get_server_key();
char* zama_get_keys();

int32_t zama_process_genome_data(
const char* filename,
size_t num_lines,
const char* client_key_json,
const char* server_key_json
);


#endif
Binary file modified monadic_ios/libs/libzama.a
Binary file not shown.
Binary file added monadic_ios/libs/libzama_poc.a
Binary file not shown.
22 changes: 16 additions & 6 deletions monadic_ios/monadic_ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
CA445A852CFE803400BF9EFC /* libzama.a in Frameworks */ = {isa = PBXBuildFile; fileRef = CA445A842CFE802B00BF9EFC /* libzama.a */; };
CA42209D2D06772700AD2E5A /* libzama.a in Frameworks */ = {isa = PBXBuildFile; fileRef = CA42209C2D06771E00AD2E5A /* libzama.a */; };
CA445A8B2CFE855A00BF9EFC /* KeychainSwift in Frameworks */ = {isa = PBXBuildFile; productRef = CA445A8A2CFE855A00BF9EFC /* KeychainSwift */; };
CAB71F242D06C2E8004A4B4E /* libzama_poc.a in Frameworks */ = {isa = PBXBuildFile; fileRef = CAB71F232D06C2DE004A4B4E /* libzama_poc.a */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -29,11 +30,12 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
CA445A842CFE802B00BF9EFC /* libzama.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libzama.a; path = libs/libzama.a; sourceTree = "<group>"; };
CA42209C2D06771E00AD2E5A /* libzama.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libzama.a; path = libs/libzama.a; sourceTree = "<group>"; };
CA98B90A2CF5E864006ABA9E /* monadic_ios.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = monadic_ios.app; sourceTree = BUILT_PRODUCTS_DIR; };
CA98B91A2CF5E866006ABA9E /* monadic_iosTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = monadic_iosTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
CA98B9242CF5E866006ABA9E /* monadic_iosUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = monadic_iosUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
CA98BA822CF876B8006ABA9E /* Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Bridging-Header.h"; sourceTree = "<group>"; };
CAB71F232D06C2DE004A4B4E /* libzama_poc.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libzama_poc.a; path = libs/libzama_poc.a; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFileSystemSynchronizedRootGroup section */
Expand All @@ -59,8 +61,9 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
CAB71F242D06C2E8004A4B4E /* libzama_poc.a in Frameworks */,
CA42209D2D06772700AD2E5A /* libzama.a in Frameworks */,
CA445A8B2CFE855A00BF9EFC /* KeychainSwift in Frameworks */,
CA445A852CFE803400BF9EFC /* libzama.a in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -106,7 +109,8 @@
CA98BA7D2CF87545006ABA9E /* Frameworks */ = {
isa = PBXGroup;
children = (
CA445A842CFE802B00BF9EFC /* libzama.a */,
CAB71F232D06C2DE004A4B4E /* libzama_poc.a */,
CA42209C2D06771E00AD2E5A /* libzama.a */,
);
name = Frameworks;
sourceTree = "<group>";
Expand Down Expand Up @@ -431,7 +435,10 @@
"$(inherited)",
"@executable_path/Frameworks",
);
LIBRARY_SEARCH_PATHS = "$(PROJECT_DIR)/libs";
LIBRARY_SEARCH_PATHS = (
"$(PROJECT_DIR)/libs",
"$(PROJECT_DIR)",
);
"LIBRARY_SEARCH_PATHS[arch=*]" = "$(PROJECT_DIR)/libs";
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = "com.aisha.monadic-ios";
Expand Down Expand Up @@ -463,7 +470,10 @@
"$(inherited)",
"@executable_path/Frameworks",
);
LIBRARY_SEARCH_PATHS = "$(PROJECT_DIR)/libs";
LIBRARY_SEARCH_PATHS = (
"$(PROJECT_DIR)/libs",
"$(PROJECT_DIR)",
);
"LIBRARY_SEARCH_PATHS[arch=*]" = "$(PROJECT_DIR)/libs";
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = "com.aisha.monadic-ios";
Expand Down
Binary file not shown.
57 changes: 51 additions & 6 deletions monadic_ios/monadic_ios/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ struct ContentView: View {
@State private var statusText: String = ""
@State private var clientKeyText: String = ""

// Define the number of lines
@State private var numLines = 20

@State private var presentImporter = false


var body: some View {
VStack {

Text(statusText)
.padding()

Button("Run Zama Code") {
if let result = zama_run() {
let resultString = String(cString: result)
Expand All @@ -24,8 +31,27 @@ struct ContentView: View {
}
storeClientKey()
}



Button("Upload Genome Data") {
presentImporter = true
}.fileImporter(isPresented: $presentImporter, allowedContentTypes: [.text]) { result in
uploadAndProcessGenomeData(result: result)
}
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(8)

Button("Retrieve Server Key") {
guard let serverKey = retrieveServerKey() else {
clientKeyText = "No server key found in keychain"
return
}
let serverKeyLength = strlen(serverKey)
print("Length of server key: \(serverKeyLength)")
}


Button("Retrieve Client Key") {
guard let clientKey = retrieveClientKey() else {
clientKeyText = "No client key found in keychain"
Expand All @@ -35,16 +61,35 @@ struct ContentView: View {
print("Length of client key: \(clientKeyLength)")

// clientKeyText = clientKey

clientKeyText = "Length of client key: \(clientKeyLength)"

}
}
}

func uploadAndProcessGenomeData(result: Result<URL, Error>) {
guard let url = try? result.get() else {
print("File upload failed:", result)
return
}

guard url.startAccessingSecurityScopedResource() else {
print("Failed to access file resource.")
return
}
defer { url.stopAccessingSecurityScopedResource() }

guard let filename = url.path.removingPercentEncoding else {
print("Filename is invalid.")
return
}

processGenomeData(filename: filename, numLines: numLines)
}
}



#Preview {
ContentView()
}
49 changes: 49 additions & 0 deletions monadic_ios/monadic_ios/utils/zama.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,66 @@ import KeychainSwift

let keychain = KeychainSwift()

struct ClientKey {
var key: [UInt8]
}

struct ServerKey {
var key: [UInt8]
}

func storeClientKey() {
guard let clientKey = zama_get_client_key() else {
print("Failed to retrieve client key.")
return
}
let clientKeyString = String(cString: clientKey)

guard let serverKey = zama_get_server_key() else {
print("Failed to retrieve server key.")
return
}
let serverKeyString = String(cString: serverKey)


keychain.set(clientKeyString, forKey: "zamaClientKey")
keychain.set(serverKeyString, forKey: "zamaServerKey")

print("Successfully stored client key.")
}

func retrieveClientKey() -> String? {
return keychain.get("zamaClientKey")
}
func retrieveServerKey() -> String? {
return keychain.get("zamaServerKey")
}


func processGenomeData(filename: String, numLines: Int) {
// Retrieve keys from Keychain
guard let clientKey = retrieveClientKey(), let serverKey = retrieveServerKey() else {
print("Failed to retrieve keys from Keychain.")
return
}

// Convert keys to C strings
let clientKeyCString = (clientKey as NSString).utf8String
let serverKeyCString = (serverKey as NSString).utf8String


// Call zama_process_genome_data
let result = zama_process_genome_data(
filename,
numLines,
clientKeyCString,
serverKeyCString
)

if result == 0 {
print("Iteration completed successfully.")
} else {
print("Iteration failed.")
}
}

11 changes: 10 additions & 1 deletion zama-poc/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion zama-poc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ bincode = "1.3.3"
chrono = "0.4.38"
env_logger = "0.11.3"
log = "0.4.22"
reqwest = { version = "0.11", features = ["json"] }
reqwest = { version = "0.11", features = ["blocking", "json"] }
rusqlite = { version = "0.26", features = ["bundled"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tfhe = { version = "0.7.1", features = [ "boolean", "shortint", "integer", "aarch64-unix" ] }
tokio = { version = "1", features = ["full"] }
tokio-stream = "0.1"

[lib]
crate-type = ["staticlib"]


[[bin]]
name = "main"
path = "src/main.rs"
Expand All @@ -30,3 +34,4 @@ path = "src/server.rs"
[[bin]]
name = "client"
path = "src/client.rs"

1 change: 1 addition & 0 deletions zama-poc/src/ffi/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod processing;
Loading