#![deny(missing_docs)]
#![deny(rustdoc::broken_intra_doc_links)]
mod compiler;
mod error;
mod params;
pub mod fhe;
pub mod types;
pub mod zkp;
use fhe::{FheOperation, Literal};
use petgraph::stable_graph::StableGraph;
use serde::{Deserialize, Serialize};
use sunscreen_runtime::{marker, Fhe, FheZkp, Zkp};
use sunscreen_zkp_backend::CompiledZkpProgram;
use std::cell::RefCell;
use std::collections::HashMap;
use std::marker::PhantomData;
pub use compiler::{Compiler, FheProgramFn, FheProgramFnExt, GenericCompiler};
pub use error::{Error, Result};
pub use params::PlainModulusConstraint;
pub use seal_fhe::Plaintext as SealPlaintext;
pub use sunscreen_compiler_macros::*;
pub use sunscreen_fhe_program::{SchemeType, SecurityLevel};
pub use sunscreen_runtime::{
CallSignature, Ciphertext, CompiledFheProgram, Error as RuntimeError, FheProgramInput,
FheProgramInputTrait, FheProgramMetadata, FheRuntime, FheZkpRuntime, InnerCiphertext,
InnerPlaintext, Params, Plaintext, PrivateKey, ProofBuilder, PublicKey, RequiredKeys, Runtime,
VerificationBuilder, WithContext, ZkpProgramInput, ZkpRuntime,
};
#[cfg(feature = "bulletproofs")]
pub use sunscreen_zkp_backend::bulletproofs;
pub use sunscreen_zkp_backend::{Error as ZkpError, Proof, Result as ZkpResult, ZkpBackend};
pub use zkp::{invoke_gadget, ZkpProgramFn, ZkpProgramFnExt};
#[derive(Clone)]
pub struct Application<T> {
fhe_programs: HashMap<String, CompiledFheProgram>,
zkp_programs: HashMap<String, CompiledZkpProgram>,
_phantom: PhantomData<T>,
}
impl<T> Application<T> {
pub(crate) fn new(
fhe_programs: HashMap<String, CompiledFheProgram>,
zkp_programs: HashMap<String, CompiledZkpProgram>,
) -> Result<Self> {
if fhe_programs.is_empty() && zkp_programs.is_empty() {
return Err(Error::NoPrograms);
}
Ok(Self {
fhe_programs,
zkp_programs,
_phantom: PhantomData,
})
}
}
impl<T> Application<T>
where
T: marker::Fhe,
{
pub fn params(&self) -> &Params {
&self.fhe_programs.values().next().unwrap().metadata.params
}
#[deprecated]
pub fn get_program<N>(&self, name: N) -> Option<&CompiledFheProgram>
where
N: AsRef<str>,
{
self.get_fhe_program(name)
}
pub fn get_fhe_program<N>(&self, name: N) -> Option<&CompiledFheProgram>
where
N: AsRef<str>,
{
self.fhe_programs.get(name.as_ref())
}
#[deprecated]
pub fn get_programs(&self) -> impl Iterator<Item = (&String, &CompiledFheProgram)> {
self.get_fhe_programs()
}
pub fn get_fhe_programs(&self) -> impl Iterator<Item = (&String, &CompiledFheProgram)> {
self.fhe_programs.iter()
}
fn take_fhe_program<N>(&mut self, name: N) -> Option<CompiledFheProgram>
where
N: AsRef<str>,
{
self.fhe_programs.remove(name.as_ref())
}
}
impl<T> Application<T>
where
T: marker::Zkp,
{
pub fn get_zkp_program<N>(&self, name: N) -> Option<&CompiledZkpProgram>
where
N: AsRef<str>,
{
self.zkp_programs.get(name.as_ref())
}
pub fn get_zkp_programs(&self) -> impl Iterator<Item = (&String, &CompiledZkpProgram)> {
self.zkp_programs.iter()
}
fn take_zkp_program<N>(&mut self, name: N) -> Option<CompiledZkpProgram>
where
N: AsRef<str>,
{
self.zkp_programs.remove(name.as_ref())
}
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
pub enum OperandInfo {
Left,
Right,
Unary,
}
pub trait Value {
fn new() -> Self;
fn output(&self) -> Self;
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct FrontendCompilation {
pub graph: StableGraph<FheOperation, OperandInfo>,
}
thread_local! {
pub static INDEX_ARENA: RefCell<bumpalo::Bump> = RefCell::new(bumpalo::Bump::new());
}
pub type FheApplication = Application<Fhe>;
pub type ZkpApplication = Application<Zkp>;
pub type FheZkpApplication = Application<FheZkp>;