#'
#' Save and load keys and ciphertexts
#'
#' Use these functions rather than the base R functions for saving and loading
#' of ciphertexts and keys. Note that this is due to a limitation in Rcpp,
#' so the \code{save}, \code{save.image} and \code{saveRDS} base R functions are
#' overriden to warn if they are used incorrectly on ciper text objects.
#'
#' These functions provide a way to save ciphertexts and keys generated by this
#' package to a file in such a way that they can later be restored to another
#' R session (possibly on a different machine).
#'
#' At present no compression is performed so for long-term storage or transmission
#' over a network it would be advisable to gzip the files after creation.
#'
#' @aliases save save.image saveRDS
#'
#' @param object the ciphertext or keys to be saved
#'
#' @param file the filename to save (load) the object to (from)
#'
#' @examples
#' p <- pars("FandV")
#' keys <- keygen(p)
#' ct1 <- enc(keys$pk, 2)
#' \dontrun{saveFHE(ct1, "~/myCipherText.fhe")}
#'
#' # Start a new session or transfer to another machine
#' \dontrun{ct1 <- loadFHE("~/myCipherText.fhe")}
saveFHE <- function(object, file) {
if(is.null(attr(object, "FHEt")) || is.null(attr(object, "FHEs"))) stop("This function is only for saving ciphertext and key objects produced by this package.")
file <- path.expand(file)
UseMethod("saveFHE", object)
}
#' @rdname saveFHE
loadFHE <- function(file) {
header <- readLines(file, n=2)
if(header[1] != "=> FHE pkg obj <=") {
stop("File does not contain a ciphertext or key object")
}
file <- path.expand(file)
eval(parse(text=paste("loadFHE.", header[2], "(file)", sep="")))
}
##### Override built-in save functions #####
save <- function(...) {
# Check directly specified arguments
args <- list(...)
for(arg in args) {
if(!is.null(attr(arg, "FHEs")) && !is.null(attr(arg, "FHEt"))) {
stop("Due to a limitation in Rcpp the save method cannot be used with ciphertexts. Please use saveFHE and loadFHE instead (see ?saveFHE).")
}
}
# Check variables named via list
for(var in args$list) {
var2 <- get(var, envir=.GlobalEnv)
if(!is.null(attr(var2, "FHEs")) && !is.null(attr(var2, "FHEt"))) {
stop("Due to a limitation in Rcpp the save method cannot be used with ciphertexts. Please use saveFHE and loadFHE instead (see ?saveFHE).")
}
}
base::save(...)
}
save.image <- function(...) {
vars <- ls(all.names=TRUE, envir=.GlobalEnv)
nonCT <- c()
for(var in vars) {
var2 <- get(var, envir=.GlobalEnv)
if(!is.null(attr(var2, "FHEs")) && !is.null(attr(var2, "FHEt"))) {
warning(var, " not being saved in image - all encryption variables omitted.")
} else {
nonCT <- c(nonCT, var)
}
}
args <- list(...)
if(is.null(args$file)) {
base::save(list=nonCT, file=".RData", envir=.GlobalEnv, ...)
} else {
base::save(list=nonCT, envir=.GlobalEnv, ...)
}
}
saveRDS <- function(object, ...) {
if(!is.null(attr(object, "FHEs")) && !is.null(attr(object, "FHEt"))) {
stop("Due to a limitation in Rcpp the saveRDS method cannot be used with ciphertexts. Please use saveFHE and loadFHE instead (see ?saveFHE).")
}
base::saveRDS(object, ...)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.