From f05ce704b1a9e220a3d8daab506cf564655e809b Mon Sep 17 00:00:00 2001 From: rickwebiii Date: Tue, 11 Jul 2023 12:16:42 -0700 Subject: [PATCH] transparent-ciphertexts feature (#280) --- .vscode/launch.json | 311 +++++++++++++++++++++++++++++++----- seal_fhe/Cargo.toml | 1 + seal_fhe/build.rs | 13 +- sunscreen/Cargo.toml | 1 + sunscreen/tests/features.rs | 61 +++++++ 5 files changed, 343 insertions(+), 44 deletions(-) create mode 100644 sunscreen/tests/features.rs diff --git a/.vscode/launch.json b/.vscode/launch.json index 3c244b361..4d7519516 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -57,6 +57,25 @@ "args": [], "cwd": "${workspaceFolder}" }, + { + "type": "lldb", + "request": "launch", + "name": "Debug integration test 'features'", + "cargo": { + "args": [ + "test", + "--no-run", + "--test=features", + "--package=sunscreen" + ], + "filter": { + "name": "features", + "kind": "test" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, { "type": "lldb", "request": "launch", @@ -76,6 +95,25 @@ "args": [], "cwd": "${workspaceFolder}" }, + { + "type": "lldb", + "request": "launch", + "name": "Debug integration test 'unsigned'", + "cargo": { + "args": [ + "test", + "--no-run", + "--test=unsigned", + "--package=sunscreen" + ], + "filter": { + "name": "unsigned", + "kind": "test" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, { "type": "lldb", "request": "launch", @@ -171,6 +209,25 @@ "args": [], "cwd": "${workspaceFolder}" }, + { + "type": "lldb", + "request": "launch", + "name": "Debug integration test 'serialization'", + "cargo": { + "args": [ + "test", + "--no-run", + "--test=serialization", + "--package=sunscreen" + ], + "filter": { + "name": "serialization", + "kind": "test" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, { "type": "lldb", "request": "launch", @@ -304,6 +361,25 @@ "args": [], "cwd": "${workspaceFolder}" }, + { + "type": "lldb", + "request": "launch", + "name": "Debug benchmark 'smart_fhe'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bench=smart_fhe", + "--package=sunscreen" + ], + "filter": { + "name": "smart_fhe", + "kind": "bench" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, { "type": "lldb", "request": "launch", @@ -456,25 +532,6 @@ "args": [], "cwd": "${workspaceFolder}" }, - { - "type": "lldb", - "request": "launch", - "name": "Debug integration test 'serialization'", - "cargo": { - "args": [ - "test", - "--no-run", - "--test=serialization", - "--package=sunscreen_runtime" - ], - "filter": { - "name": "serialization", - "kind": "test" - } - }, - "args": [], - "cwd": "${workspaceFolder}" - }, { "type": "lldb", "request": "launch", @@ -742,15 +799,15 @@ { "type": "lldb", "request": "launch", - "name": "Debug executable 'simple_multiply'", + "name": "Debug executable 'bigint'", "cargo": { "args": [ "build", - "--bin=simple_multiply", - "--package=simple_multiply" + "--bin=bigint", + "--package=bigint" ], "filter": { - "name": "simple_multiply", + "name": "bigint", "kind": "bin" } }, @@ -760,16 +817,16 @@ { "type": "lldb", "request": "launch", - "name": "Debug unit tests in executable 'simple_multiply'", + "name": "Debug unit tests in executable 'bigint'", "cargo": { "args": [ "test", "--no-run", - "--bin=simple_multiply", - "--package=simple_multiply" + "--bin=bigint", + "--package=bigint" ], "filter": { - "name": "simple_multiply", + "name": "bigint", "kind": "bin" } }, @@ -779,15 +836,15 @@ { "type": "lldb", "request": "launch", - "name": "Debug executable 'calculator_rational'", + "name": "Debug executable 'calculator_fractional'", "cargo": { "args": [ "build", - "--bin=calculator_rational", - "--package=calculator_rational" + "--bin=calculator_fractional", + "--package=calculator_fractional" ], "filter": { - "name": "calculator_rational", + "name": "calculator_fractional", "kind": "bin" } }, @@ -797,16 +854,16 @@ { "type": "lldb", "request": "launch", - "name": "Debug unit tests in executable 'calculator_rational'", + "name": "Debug unit tests in executable 'calculator_fractional'", "cargo": { "args": [ "test", "--no-run", - "--bin=calculator_rational", - "--package=calculator_rational" + "--bin=calculator_fractional", + "--package=calculator_fractional" ], "filter": { - "name": "calculator_rational", + "name": "calculator_fractional", "kind": "bin" } }, @@ -816,15 +873,15 @@ { "type": "lldb", "request": "launch", - "name": "Debug executable 'calculator_fractional'", + "name": "Debug executable 'calculator_rational'", "cargo": { "args": [ "build", - "--bin=calculator_fractional", - "--package=calculator_fractional" + "--bin=calculator_rational", + "--package=calculator_rational" ], "filter": { - "name": "calculator_fractional", + "name": "calculator_rational", "kind": "bin" } }, @@ -834,16 +891,16 @@ { "type": "lldb", "request": "launch", - "name": "Debug unit tests in executable 'calculator_fractional'", + "name": "Debug unit tests in executable 'calculator_rational'", "cargo": { "args": [ "test", "--no-run", - "--bin=calculator_fractional", - "--package=calculator_fractional" + "--bin=calculator_rational", + "--package=calculator_rational" ], "filter": { - "name": "calculator_fractional", + "name": "calculator_rational", "kind": "bin" } }, @@ -961,6 +1018,43 @@ "args": [], "cwd": "${workspaceFolder}" }, + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'ordering_zkp'", + "cargo": { + "args": [ + "build", + "--bin=ordering_zkp", + "--package=ordering_zkp" + ], + "filter": { + "name": "ordering_zkp", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'ordering_zkp'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=ordering_zkp", + "--package=ordering_zkp" + ], + "filter": { + "name": "ordering_zkp", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, { "type": "lldb", "request": "launch", @@ -998,6 +1092,80 @@ "args": [], "cwd": "${workspaceFolder}" }, + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'simple_multiply'", + "cargo": { + "args": [ + "build", + "--bin=simple_multiply", + "--package=simple_multiply" + ], + "filter": { + "name": "simple_multiply", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'simple_multiply'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=simple_multiply", + "--package=simple_multiply" + ], + "filter": { + "name": "simple_multiply", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'sudoku_zkp'", + "cargo": { + "args": [ + "build", + "--bin=sudoku_zkp", + "--package=sudoku_zkp" + ], + "filter": { + "name": "sudoku_zkp", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'sudoku_zkp'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=sudoku_zkp", + "--package=sudoku_zkp" + ], + "filter": { + "name": "sudoku_zkp", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, { "type": "lldb", "request": "launch", @@ -1017,6 +1185,25 @@ "args": [], "cwd": "${workspaceFolder}" }, + { + "type": "lldb", + "request": "launch", + "name": "Debug benchmark 'linear_relation'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bench=linear_relation", + "--package=logproof" + ], + "filter": { + "name": "linear_relation", + "kind": "bench" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, { "type": "lldb", "request": "launch", @@ -1035,6 +1222,44 @@ }, "args": [], "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug benchmark 'gpu'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bench=gpu", + "--package=sunscreen_math" + ], + "filter": { + "name": "gpu", + "kind": "bench" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug benchmark 'cpu'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bench=cpu", + "--package=sunscreen_math" + ], + "filter": { + "name": "cpu", + "kind": "bench" + } + }, + "args": [], + "cwd": "${workspaceFolder}" } ] } \ No newline at end of file diff --git a/seal_fhe/Cargo.toml b/seal_fhe/Cargo.toml index d82132753..20aa2c76c 100644 --- a/seal_fhe/Cargo.toml +++ b/seal_fhe/Cargo.toml @@ -35,3 +35,4 @@ serde_json="1.0.74" [features] hexl = [] +transparent-ciphertexts = [] diff --git a/seal_fhe/build.rs b/seal_fhe/build.rs index 846caed06..905379317 100644 --- a/seal_fhe/build.rs +++ b/seal_fhe/build.rs @@ -34,6 +34,13 @@ fn compile_native(profile: &str, out_path: &Path) { "OFF" }; + let forbid_transparent_ciphertexts = + if std::env::var("CARGO_FEATURE_TRANSPARENT_CIPHERTEXTS").is_ok() { + "OFF" + } else { + "ON" + }; + let mut builder = Config::new("SEAL"); builder @@ -52,7 +59,11 @@ fn compile_native(profile: &str, out_path: &Path) { .define("SEAL_USE_INTRIN", "ON") .define("SEAL_USE_MSGSL", "OFF") .define("SEAL_USE_ZLIB", "ON") - .define("SEAL_USE_ZSTD", "ON"); + .define("SEAL_USE_ZSTD", "ON") + .define( + "SEAL_THROW_ON_TRANSPARENT_CIPHERTEXT", + forbid_transparent_ciphertexts, + ); setup_macos_cross_compile(&mut builder); diff --git a/sunscreen/Cargo.toml b/sunscreen/Cargo.toml index b933941c2..0b1477afc 100644 --- a/sunscreen/Cargo.toml +++ b/sunscreen/Cargo.toml @@ -56,6 +56,7 @@ serde_json = "1.0.74" [features] bulletproofs = ["sunscreen_zkp_backend/bulletproofs"] hexl = ["seal_fhe/hexl"] +transparent-ciphertexts = ["seal_fhe/transparent-ciphertexts"] [[bench]] name = "fractional_range_proof" diff --git a/sunscreen/tests/features.rs b/sunscreen/tests/features.rs new file mode 100644 index 000000000..7031348ac --- /dev/null +++ b/sunscreen/tests/features.rs @@ -0,0 +1,61 @@ +use sunscreen::{ + types::{bfv::Signed, Cipher}, + Compiler, +}; +use sunscreen_compiler_macros::fhe_program; +use sunscreen_runtime::{FheProgramInput, Runtime}; + +#[cfg(not(feature = "transparent-ciphertexts"))] +mod transparent_ciphertexts { + use super::*; + + #[test] + #[should_panic] + fn panics_on_transparent_ciphertext() { + #[fhe_program(scheme = "bfv")] + fn add(a: Cipher, b: Signed) -> Cipher { + a * b + } + + let app = Compiler::new().fhe_program(add).compile().unwrap(); + + let runtime = Runtime::new_fhe(app.params()).unwrap(); + + let (public, _) = runtime.generate_keys().unwrap(); + + let a = runtime.encrypt(Signed::from(42), &public).unwrap(); + + let args: Vec = vec![a.into(), Signed::from(0).into()]; + + runtime + .run(app.get_fhe_program(add).unwrap(), args, &public) + .unwrap(); + } +} + +#[cfg(feature = "transparent-ciphertexts")] +mod transparent_ciphertexts { + use super::*; + + #[test] + fn no_panic_on_transparent_ciphertext() { + #[fhe_program(scheme = "bfv")] + fn add(a: Cipher, b: Signed) -> Cipher { + a * b + } + + let app = Compiler::new().fhe_program(add).compile().unwrap(); + + let runtime = Runtime::new_fhe(app.params()).unwrap(); + + let (public, _) = runtime.generate_keys().unwrap(); + + let a = runtime.encrypt(Signed::from(42), &public).unwrap(); + + let args: Vec = vec![a.into(), Signed::from(0).into()]; + + runtime + .run(app.get_fhe_program(add).unwrap(), args, &public) + .unwrap(); + } +}