SeComLib
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
Public Member Functions | Private Member Functions | Private Attributes | List of all members
SeComLib::Core::Paillier Class Reference

Implementation of the public-key Paillier Cryptosystem. More...

#include <paillier.h>

Inheritance diagram for SeComLib::Core::Paillier:
SeComLib::Core::CryptoProvider< PaillierPublicKey, PaillierPrivateKey, PaillierCiphertext, PaillierRandomizer >

Public Member Functions

 Paillier ()
 Default constructor. More...
 
 Paillier (const PaillierPublicKey &publicKey)
 Creates an instance of the class for homomorphic operations and ecryption. More...
 
 Paillier (const PaillierPublicKey &publicKey, const PaillierPrivateKey &privateKey)
 Creates an instance of the class for homomorphic operations, ecryption and decryption. More...
 
 ~Paillier ()
 Destructor.
 
virtual bool GenerateKeys ()
 
virtual BigInteger DecryptInteger (const Ciphertext &ciphertext) const
 Decrypt number. More...
 
virtual Ciphertext EncryptIntegerNonrandom (const BigInteger &plaintext) const
 Encrypt number without randomization. More...
 
virtual Randomizer GetRandomizer () const
 Compute the random factor required for the encryption operation. More...
 
virtual Ciphertext RandomizeCiphertext (const Ciphertext &ciphertext) const
 Randomize encrypted number with a self-generated random value. More...
 
virtual const BigInteger & GetMessageSpaceUpperBound () const
 Returns the message space upper bound. More...
 
virtual size_t GetMessageSpaceSize () const
 Returns the message space bit size. More...
 
- Public Member Functions inherited from SeComLib::Core::CryptoProvider< PaillierPublicKey, PaillierPrivateKey, PaillierCiphertext, PaillierRandomizer >
 CryptoProvider (const unsigned int keyLength)
 Constructor. More...
 
 CryptoProvider (const PaillierPublicKey &publicKey, const unsigned int keyLength)
 Constructor. More...
 
 CryptoProvider (const PaillierPublicKey &publicKey, const PaillierPrivateKey &privateKey, const unsigned int keyLength)
 Constructor. More...
 
virtual ~CryptoProvider ()
 Destructor.
 
virtual PaillierCiphertext EncryptInteger (const BigInteger &plaintext) const
 Encrypt an integer and apply randomization. More...
 
const BigInteger & GetEncryptionModulus () const
 Returns the modulus required for reducing the encryption after randomization. More...
 
virtual const BigInteger & GetPositiveNegativeBoundary () const
 Returns the biggest positive number that can be encrypted without overflowing. More...
 
const PaillierPublicKeyGetPublicKey () const
 Public key getter. More...
 
const PaillierPrivateKeyGetPrivateKey () const
 Private key getter. More...
 
Ciphertext GetEncryptedZero (const bool randomized=true) const
 Returns [0]. More...
 
Ciphertext GetEncryptedOne (const bool randomized=true) const
 Returns [1]. More...
 

Private Member Functions

BigInteger L (const BigInteger &input, const BigInteger &d) const
 L function evaluator. More...
 
virtual void validateParameters ()
 Do nothing for now.
 
virtual void doPrecomputations ()
 Precompute values for speedups. More...
 
 Paillier (const Paillier &)
 Copy constructor - not implemented.
 
Paillier operator= (const Paillier &)
 Copy assignment operator - not implemented.
 

Private Attributes

BigInteger pMinusOne
 \( p - 1 \)
 
BigInteger qMinusOne
 \( q - 1 \)
 
BigInteger nMinusOne
 Contains \( n - 1 \).
 
BigInteger pSquared
 \( p^2 \)
 
BigInteger qSquared
 \( q^2 \)
 
BigInteger nSquared
 Contains \( n^2 \).
 
BigInteger pTimesPInvModQ
 \( p (p^{-1} \pmod q) \)
 
BigInteger qTimesQInvModP
 \( q (q^{-1} \pmod p) \)
 
BigInteger hp
 \( L_p(g^{p - 1} (\pmod p^2))^{-1} \pmod p \)
 
BigInteger hq
 \( L_q(g^{q - 1} (\pmod q^2))^{-1} \pmod q \)
 

Additional Inherited Members

- Public Types inherited from SeComLib::Core::CryptoProvider< PaillierPublicKey, PaillierPrivateKey, PaillierCiphertext, PaillierRandomizer >
typedef PaillierCiphertext Ciphertext
 Provide public access to the T_Ciphertext type.
 
typedef PaillierRandomizer Randomizer
 Provide public access to the T_Randomizer type.
 
- Protected Types inherited from SeComLib::Core::CryptoProvider< PaillierPublicKey, PaillierPrivateKey, PaillierCiphertext, PaillierRandomizer >
typedef RandomizerCache
< RandomizerContainer
< CryptoProvider
< PaillierPublicKey,
PaillierPrivateKey,
PaillierCiphertext,
PaillierRandomizer >
, RandomizerCacheParameters > > 
RandomizerCacheType
 Data type of the randomizer cache.
 
- Protected Attributes inherited from SeComLib::Core::CryptoProvider< PaillierPublicKey, PaillierPrivateKey, PaillierCiphertext, PaillierRandomizer >
std::unique_ptr
< RandomizerCacheType
randomizerCache
 Lazy loading randomizer cache.
 
PaillierPublicKey publicKey
 Public key container.
 
PaillierPrivateKey privateKey
 Private key container.
 
unsigned int keyLength
 The key length in bits.
 
std::shared_ptr< BigInteger > encryptionModulus
 The encryption modulus.
 
BigInteger positiveNegativeBoundary
 Contains the delimiter between positive and negative values in the message space (usually \( \lfloor messagespace / 2 \rfloor \))
 
bool hasPrivateKey
 Boolean flag that enables decryption if the private key is present.
 
bool precomputeSpeedupValues
 Boolean flag that indicates wether doPrecomputations() should precompute certain values.
 
PaillierCiphertext encryptedZero
 Contains [0] used as initializer for homomorphic addition accumulators. Precompute it for optimization purposes.
 
PaillierCiphertext encryptedOne
 Contains [1].
 

Detailed Description

Implementation of the public-key Paillier Cryptosystem.

Definition at line 103 of file paillier.h.

Constructor & Destructor Documentation

SeComLib::Core::Paillier::Paillier ( )

Default constructor.

Sets the specified key size from the configuration file (defaults to 1024)

Definition at line 73 of file paillier.cpp.

SeComLib::Core::Paillier::Paillier ( const PaillierPublicKey publicKey)

Creates an instance of the class for homomorphic operations and ecryption.

Performs required precomputations.

Parameters
publicKeya PaillierPublicKey structure

Definition at line 81 of file paillier.cpp.

SeComLib::Core::Paillier::Paillier ( const PaillierPublicKey publicKey,
const PaillierPrivateKey privateKey 
)

Creates an instance of the class for homomorphic operations, ecryption and decryption.

Performs required precomputations.

Parameters
publicKeya PaillierPublicKey structure
privateKeya PaillierPrivateKey structure

Definition at line 92 of file paillier.cpp.

Member Function Documentation

BigInteger SeComLib::Core::Paillier::DecryptInteger ( const Ciphertext ciphertext) const
virtual

Decrypt number.

If \( plaintext \geq \lfloor messagespace / 2 \), it is remapped to a negative value.

Parameters
ciphertextthe ciphertext integer
Returns
Deciphered plaintext
Exceptions
std::runtime_erroroperation requires the private key

Apply CRT for decryption: \( h_p = L_p(g^{p - 1} (\pmod p^2))^{-1} \pmod p \) \( h_q = L_q(g^{q - 1} (\pmod q^2))^{-1} \pmod q \) \( m_p = L_p(c^{p - 1} (\pmod p^2)) h_p \pmod p \) \( m_q = L_q(c^{q - 1} (\pmod q^2)) h_q \pmod q \) \( m = (m_p q (q^{-1} \pmod p) + m_q p (p^{-1} \pmod q)) (\pmod n) \)

If \( plaintext \leq \lfloor messagespace / 2 \rfloor \Rightarrow plaintext \geq 0 \) otherwise \( plaintext < 0 \Rightarrow plaintext = plaintext - messagespace \)

Implements SeComLib::Core::CryptoProvider< PaillierPublicKey, PaillierPrivateKey, PaillierCiphertext, PaillierRandomizer >.

Definition at line 176 of file paillier.cpp.

void SeComLib::Core::Paillier::doPrecomputations ( )
privatevirtual

Precompute values for speedups.

Precompute \( h_p = L_p(g^{p - 1} (\pmod p^2))^{-1} \pmod p \) and \( h_q = L_q(g^{q - 1} (\pmod q^2))^{-1} \pmod q \) to speed up decryption via CRT

Speed optimizations for decryption: precompute \( p (p^{-1} \pmod q) \) and \( q (q^{-1} \pmod p) \)

Todo:
Catch a custom exception here

Populate the randomizer cache

Implements SeComLib::Core::CryptoProvider< PaillierPublicKey, PaillierPrivateKey, PaillierCiphertext, PaillierRandomizer >.

Definition at line 304 of file paillier.cpp.

Paillier::Ciphertext SeComLib::Core::Paillier::EncryptIntegerNonrandom ( const BigInteger &  plaintext) const
virtual

Encrypt number without randomization.

Contains the "standard" and "shortcut" versions of the Paillier encryption algorithm without randomization.

Parameters
plaintextthe plaintext integer
Returns
Encrypted ciphertext
  • "Standard" version: \( c = g^m r^n \pmod {n^2} \)
  • "Shortcut" version: \( c = (n*m + 1) r^n \pmod {n^2} \) The computation is performed in two steps:
  • encrypt data
  • randomize ciphertext

If \( plaintext < 0 \), we remap it to the second half of the message space

"Shortcut" version: \( c = (n*m + 1) \pmod {n^2} \)

Implements SeComLib::Core::CryptoProvider< PaillierPublicKey, PaillierPrivateKey, PaillierCiphertext, PaillierRandomizer >.

Definition at line 214 of file paillier.cpp.

bool SeComLib::Core::Paillier::GenerateKeys ( )
virtual

Generate the public and private keys

Todo:
Implement a non-heuristic algorithm for insuring that \( n \) always has the specified length

Generates the Paillier cryptosystem keys.

Produces \( p \) and \( q \), each having half of the key length, and computes \( n = p q \), enforcing \( n \) to have the length specified by the key length.

Returns
Always true, for now

Set the length of primes p and q

If both are equal (highly unlikely) seek another prime

Compute \( n = p q \)

Need to guarantee that n always has the specified length? There should be a faster implementation for this...

"Shortcut" version:

Set \( g = n + 1 \)

\( \phi(n) \), \( \lambda \) and \( \mu \) are required by the "shortcut version" of the algorithm, but are no longer needed if decryption is done via CRT

Implements SeComLib::Core::CryptoProvider< PaillierPublicKey, PaillierPrivateKey, PaillierCiphertext, PaillierRandomizer >.

Definition at line 105 of file paillier.cpp.

size_t SeComLib::Core::Paillier::GetMessageSpaceSize ( ) const
virtual

Returns the message space bit size.

Returns
The message space bit size.

Implements SeComLib::Core::CryptoProvider< PaillierPublicKey, PaillierPrivateKey, PaillierCiphertext, PaillierRandomizer >.

Definition at line 283 of file paillier.cpp.

const BigInteger & SeComLib::Core::Paillier::GetMessageSpaceUpperBound ( ) const
virtual

Returns the message space upper bound.

Returns
\( n \)

Implements SeComLib::Core::CryptoProvider< PaillierPublicKey, PaillierPrivateKey, PaillierCiphertext, PaillierRandomizer >.

Definition at line 276 of file paillier.cpp.

Paillier::Randomizer SeComLib::Core::Paillier::GetRandomizer ( ) const
virtual

Compute the random factor required for the encryption operation.

Generates a random number \( r \in \mathbb{Z}_n^* \) and computes \( r^n \pmod {n^2} \).

Computes \( r^n \pmod {n^2} \).

Returns
the randomizer

Implements SeComLib::Core::CryptoProvider< PaillierPublicKey, PaillierPrivateKey, PaillierCiphertext, PaillierRandomizer >.

Definition at line 259 of file paillier.cpp.

BigInteger SeComLib::Core::Paillier::L ( const BigInteger &  input,
const BigInteger &  d 
) const
private

L function evaluator.

Computes \( L(u) = \frac{u - 1}{d} \)

Parameters
inputthe function variable
dthe divisor
Returns
L(input)

Definition at line 294 of file paillier.cpp.

Paillier::Ciphertext SeComLib::Core::Paillier::RandomizeCiphertext ( const Ciphertext ciphertext) const
virtual

Randomize encrypted number with a self-generated random value.

Computes \( c = c r^n \pmod {n^2} \).

Parameters
ciphertextthe ciphertext integer
Returns
The randomized ciphertext

Implements SeComLib::Core::CryptoProvider< PaillierPublicKey, PaillierPrivateKey, PaillierCiphertext, PaillierRandomizer >.

Definition at line 269 of file paillier.cpp.


The documentation for this class was generated from the following files: