In standard Paillier encryption
- Property 1 really is:
$m_1=D(c_1)\text{ and }m_2=D(c_2)\implies D(c_1\cdot c_2\bmod n^2) = m_1+m_2\bmod n$.
- Property 2 does not hold (but see final off-topic note).
As a consequence of property 1, for all $k$ in $\mathbb Z$, it holds $D({c_1}^k\bmod n^2)\ =\ m_1\cdot k\bmod n$. Proof for positive $k$ can be by induction, proof for negative $k$ follows from group properties of multiplication in $\mathbb Z_{n^2}^*$ and $\mathbb Z_n^*$.
Note: For integers $u$, $v$ and $w$, by definition of $u\bmod v=w$, that means $u-w$ is a multiple of $v$ and $0\le w<v$. The operator $\bmod$ has priority less than $+$ (thus less than $\cdot$) but more than $=$. Contrast with operator %
in computer languages, which typically has priority more than +
.
Once this party has received some EncryptedNumber instances (e.g. see Serialisation), it can perform basic mathematical operations supported by the Paillier encryption:
- Addition of an EncryptedNumber to a scalar
- Addition of two EncryptedNumber instances
- Multiplication of an EncryptedNumber by a scalar
That's correct if we take addition and multiplication modulo $n$. Given $c_1$ and $c_2$ such that $m_1=D(c_1)$ and $m_2=D(c_2)$, and a scalar $k$ in $\mathbb Z$, any party knowing the public key $(n,g)$ can
Compute $c$ such that $D(c)=m_1+k\bmod n$, by encrypting $k$ and applying property 1. That is $c\gets c_1\cdot g^k\cdot r^n\bmod n^2$ for some $r$ coprime with $n$ (possibly $r=1$ if hiding $k$ and that $c$ derives from $c_1$ are pointless in the application).
Compute $c$ such that $D(c)=m_1+m_2\bmod n$, by application of property 1.
Compute $c$ such that $D(c)=m_1\cdot k\bmod n$, by application of the consequence of property 1.
The doc linked in the question is for a library that adds a wrapper on Paillier encryption. This is the part of the source doing the crypto. The library handles negatives and floating point numbers, as explained there, by
- Restricting it's plaintext integers to range $\left[\,-\left\lfloor n/3\right\rfloor,\left\lfloor n/3\right\rfloor\,\right]$, with negatives mapped to $\left[\,n-\left\lfloor n/3\right\rfloor,n-1\,\right]$ of standard Paillier encryption.
- Separating mantissa (encrypted with sign as above) and exponent (which remains in clear).
Off-topic: Python allows operator +
to perform anything the programmer wants when applied to objects defined by the programmer. Both in the above library and the simpler code linked in comment, +
for two ciphertexts has been coerced overloaded to call an internal function that makes use of property 1, thus performs a multiplication modulo $n^2$. Similarly, *
has been overloaded to call an internal function that makes use of the consequence of property 1, thus performs an exponentiation modulo $n^2$.