Smart Contract Documentation
The Paillier smart contract implements the Paillier homomorphic cryptosystem on the Gateway Shield testnet. It provides a comprehensive set of operations for performing computations on encrypted data while maintaining privacy.
Contract Address
Data Structures
Ciphertext
A wrapper for encrypted values stored as byte arrays. The byte array represents the encrypted value in the Paillier cryptosystem.
PublicKey
Contains the public parameters needed for encryption and homomorphic operations.
PrivateKey
Contains the private parameters needed for decryption. These should never be shared or stored on-chain.
Core Functions
Addition Operations
add
Performs homomorphic addition of two encrypted values.
Parameters:
a
: First encrypted valueb
: Second encrypted valuepublicKey
: Public key parameters
Returns:
BigNumber
: Encrypted sum E(m₁ + m₂)
Example:
add_const
Adds a plaintext constant to an encrypted value.
Parameters:
a
: Encrypted valueb
: Plaintext constant to addpublicKey
: Public key parameters
Returns:
BigNumber
: Encrypted result E(m + k)
Example:
Subtraction Operations
sub
Performs homomorphic subtraction of two encrypted values.
Parameters:
a
: Encrypted minuendb
: Encrypted subtrahendpublicKey
: Public key parameters
Returns:
BigNumber
: Encrypted difference E(m₁ - m₂)
sub_const
Subtracts a plaintext constant from an encrypted value.
Parameters:
a
: Encrypted valueb
: Plaintext constant to subtractpublicKey
: Public key parameters
Returns:
BigNumber
: Encrypted result E(m - k)
Multiplication and Division
mul_const
Multiplies an encrypted value by a plaintext constant.
Parameters:
a
: Encrypted valueb
: Plaintext multiplierpublicKey
: Public key parameters
Returns:
BigNumber
: Encrypted product E(m * k)
div_const
Divides an encrypted value by a plaintext constant.
Parameters:
a
: Encrypted valueb
: Plaintext divisorpublicKey
: Public key parameters
Returns:
BigNumber
: Encrypted quotient E(m / k)
Cryptographic Operations
encryptZero
Creates an encryption of zero using a random value.
Parameters:
rnd
: Random value for probabilistic encryptionpublicKey
: Public key parameters
Returns:
BigNumber
: Encryption of zero E(0)
encrypt
Encrypts a plaintext value using the Paillier cryptosystem.
Parameters:
value
: Plaintext value to encryptrnd
: Random value for probabilistic encryptionpublicKey
: Public key parameters
Returns:
BigNumber
: Encrypted value E(m)
decrypt
Decrypts an encrypted value using the private key.
Parameters:
encValue
: Encrypted value to decryptpublicKey
: Public key parametersprivateKey
: Private key parameterssigma
: Precomputed sigma value for efficient decryption
Returns:
BigNumber
: Decrypted plaintext value
Security Note: The sigma
parameter is a precomputed value to avoid expensive big integer division on-chain. The contract verifies that the provided sigma is correct before completing the decryption.
Security Considerations
Key Generation
- Keys should be generated off-chain using secure random number generation
- Public and private keys should have sufficient bit length (recommended: 2048 bits)
- Private keys should never be exposed or stored on-chain
Parameter Validation
Gas Optimization
-
Batch Operations
-
Parameter Size
- Use minimum required bit lengths for parameters
- Consider gas costs when choosing parameter sizes
-
Operation Ordering
- Perform as many operations off-chain as possible
- Batch similar operations together
Integration Example
Error Handling
Common errors and their solutions:
-
Invalid Sigma Error
Solution: Ensure sigma is correctly precomputed off-chain
-
Gas Limit Exceeded
Solution: Batch operations or reduce parameter sizes
-
Invalid Parameter Size
Solution: Use appropriate key sizes (≥2048 bits)