Title: | Homomorphic Computations in R |
---|---|
Description: | Homomorphic computations in R for privacy-preserving applications. Currently only the Paillier Scheme is implemented. |
Authors: | Balasubramanian Narasimhan [aut, cre] |
Maintainer: | Balasubramanian Narasimhan <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.2-5 |
Built: | 2024-11-19 04:56:15 UTC |
Source: | https://github.com/bnaras/homomorpher |
homomorpheR
is a start at a rudimentary package for
homomorphic computations in R. The goal is to collect homomorphic
encryption schemes in this package for privacy-preserving
distributed computations; for example, applications of the sort
immplemented in package distcomp
.
At the moment, only one scheme is implemented, the Paillier scheme. The current implementation makes no pretense at efficiency and also uses direct translations of other implementations, particularly the one in Javascript.
For a quick overview of the features, read the
homomorpheR
vignette by running
vignette("homomorpheR")
.
https://en.wikipedia.org/wiki/Homomorphic_encryption
https://mhe.github.io/jspaillier/
keys <- PaillierKeyPair$new(1024) # Generate new key pair encryptAndDecrypt <- function(x) keys$getPrivateKey()$decrypt(keys$pubkey$encrypt(x)) a <- gmp::as.bigz(1273849) identical(a + 10L, encryptAndDecrypt(a+10L)) x <- lapply(1:100, function(x) random.bigz(nBits = 512)) edx <- lapply(x, encryptAndDecrypt) identical(x, edx)
keys <- PaillierKeyPair$new(1024) # Generate new key pair encryptAndDecrypt <- function(x) keys$getPrivateKey()$decrypt(keys$pubkey$encrypt(x)) a <- gmp::as.bigz(1273849) identical(a + 10L, encryptAndDecrypt(a+10L)) x <- lapply(1:100, function(x) random.bigz(nBits = 512)) edx <- lapply(x, encryptAndDecrypt) identical(x, edx)
Construct a Paillier public and private key pair given a fixed number of bits
Construct a Paillier public and private key pair given a fixed number of bits
An R6Class
generator object
PaillierKeyPair$getPrivateKey()
Return the private key
pubkey
the public key
new()
Create a new public private key pair with specified number of modulus bits
PaillierKeyPair$new(modulusBits)
modulusBits
the number of bits to use
a PaillierKeyPair
object
getPrivateKey()
Return the private key
PaillierKeyPair$getPrivateKey()
the private key
clone()
The objects of this class are cloneable with this method.
PaillierKeyPair$clone(deep = FALSE)
deep
Whether to make a deep clone.
PaillierPublicKey
and PaillierPrivateKey
keys <- PaillierKeyPair$new(1024) keys$pubkey keys$getPrivateKey()
keys <- PaillierKeyPair$new(1024) keys$pubkey keys$getPrivateKey()
Construct a Paillier private key with the given secret and a public key
Construct a Paillier private key with the given secret and a public key
An R6Class
generator object
pubkey
the public key
new()
Create a new private key with given secret lambda
and the public key
PaillierPrivateKey$new(lambda, pubkey)
lambda
the secret
pubkey
the public key
getLambda()
Return the secret lambda
PaillierPrivateKey$getLambda()
lambda
decrypt()
Decrypt a message
PaillierPrivateKey$decrypt(c)
c
the message
the decrypted message
clone()
The objects of this class are cloneable with this method.
PaillierPrivateKey$clone(deep = FALSE)
deep
Whether to make a deep clone.
PaillierPublicKey
which goes hand-in-hand with this object
Construct a Paillier public key with the given modulus.
Construct a Paillier public key with the given modulus.
An R6Class
generator object
bits
the number of bits in the modulus
n
the modulus
nSquared
the square of the modulus
nPlusOne
one more than the modulus
new()
Create a new public key and precompute some internal values for efficiency
PaillierPublicKey$new(bits, n)
bits
number of bits to use
n
the modulus to use
a new PaillierPublicKey
object
encrypt()
Encrypt a message
PaillierPublicKey$encrypt(m)
m
the message
the encrypted message
add()
Add two encrypted messages
PaillierPublicKey$add(a, b)
a
a message
b
another message
the sum of a
and b
sub()
Subtract one encrypted message from another
PaillierPublicKey$sub(a, b)
a
a message
b
another message
the difference a - b
add_real()
Return the sum a + b
of an encrypted real message a
,
a list consisting of a encrypted
integer part (named int
) and an
encrypted fractional part (named frac
),
and a real number a
using
den
as denominator in the rational
approximation.
PaillierPublicKey$add_real(den, a, b)
den
the denominator to use for rational approximations
a
the real message, a list consisting of the integer and fractional parts named int
and frac
respectively
b
a simple real number
sub_real()
Return the difference a - b
of an encrypted real message a
,
a list consisting of a encrypted
integer part (named int
) and an
encrypted fractional part (named frac
),
and a real number b
using
den
as denominator in the rational
approximation.
PaillierPublicKey$sub_real(den, a, b)
den
the denominator to use for rational approximations
a
the real message, a list consisting of the integer and fractional parts named int
and frac
respectively
b
a simple real number
mult()
Return the product of two encrypted
messages a
and b
PaillierPublicKey$mult(a, b)
a
a message
b
another message
the product of a
and b
clone()
The objects of this class are cloneable with this method.
PaillierPublicKey$clone(deep = FALSE)
deep
Whether to make a deep clone.
PaillierPrivateKey
which goes hand-in-hand with this object
sodium
package.Return a random big number using the cryptographically secure random number generator
from in the sodium
package.
random.bigz(nBits)
random.bigz(nBits)
nBits |
the number of bits, which must be a multiple of 8, is not checked for efficiency. |