paillier

package module
v0.0.0-...-14f1f86 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2018 License: MIT Imports: 4 Imported by: 0

README

go-go-gadget-paillier

A Go implementation of the partially homomorphic Paillier Cryptosystem.

Inspector Paillier

Explanation

Homomorphic cryptosystems are a family of cryptosystems that allow computations to be performed on generated ciphertexts. The general idea is that: within one of these systems, one is able to perform certain computations on a chiper text, which when decrypted reflects those operations on the corresponding plaintext.

Cryptosystems from this family that support arbitary computations are known as Fully Homomorphic Cryptosystems (FHE). This powerful property would allow for the creation extremely useful systems whose functionality operates on inputs which are entirely encrypted, subsequently producing encrypted output. With the ability to perform arbitary computation on encrypted data, one could outsource sensitive private data to third-parties who are then able to perform useful operations without ever decrypting the data. Applications of this technology are extremley wide reaching, and could be applied to services such as: Search Engines, Cloud Computing Providers, E-Mail spam detection, etc.

However, most FHE systems are too inefficient for practical use, and are an on-going research area in the field of Cryptography.

Instead, this package contains an implementation of a Partially Homomorphic Cryptosystem. Partially homomorphic encryption instead only supports a subset of operations on ciphertexts. Examples of such systems are those that support addition or multiplication on ciphertexts.

The Paillier Cryptosystem is an additive cryptosystem. This means that given two ciphertexts, one can perform operations equivalent to adding the respective plaintexts. Additionally, Paillier Cryptosystem supports further computations:

  • Encrypted integers can be added together
  • Encrypted integers can be multiplied by an unencrypted integer
  • Encrypted integers and unencrypted integers can be added together

Example Usage


// Generate a 128-bit private key.
privKey, _ := paillier.GenerateKey(rand.Reader, 128)

// Encrypt the number "15".
m15 := new(big.Int).SetInt64(15)
c15, _ := paillier.Encrypt(&privKey.PublicKey, m15.Bytes())

// Decrypt the number "15".
d, _ := paillier.Decrypt(privKey, c15)
plainText := new(big.Int).SetBytes(d)
fmt.Println("Decryption Result of 15: ", plainText.String()) // 15

// Now for the fun stuff.
        
// Encrypt the number "20".
m20 := new(big.Int).SetInt64(20)
c20, _ := paillier.Encrypt(&privKey.PublicKey, m20.Bytes())

// Add the encrypted integers 15 and 20 together.
plusM16M20 := paillier.AddCipher(&privKey.PublicKey, c15, c20)
decryptedAddition, _ := paillier.Decrypt(privKey, plusM16M20)
fmt.Println("Result of 15+20 after decryption: ",
        new(big.Int).SetBytes(decryptedAddition).String()) // 35!

// Add the encrypted integer 15 to plaintext constant 10.
plusE15and10 := paillier.Add(&privKey.PublicKey, c15, new(big.Int).SetInt64(10).Bytes())
decryptedAddition, _ = paillier.Decrypt(privKey, plusE15and10)
fmt.Println("Result of 15+10 after decryption: ",
        new(big.Int).SetBytes(decryptedAddition).String()) // 25!

// Multiply the encrypted integer 15 by the plaintext constant 10.
mulE15and10 := paillier.Mul(&privKey.PublicKey, c15, new(big.Int).SetInt64(10).Bytes())
decryptedMul, _ := paillier.Decrypt(privKey, mulE15and10)
fmt.Println("Result of 15*10 after decryption: ",
        new(big.Int).SetBytes(decryptedMul).String()) // 150!

Installation

$ go get github.com/roasbeef/go-go-gadget-paillier

Warning

This library was created primarily for education purposes, with future application for a course project. You should NOT USE THIS CODE IN PRODUCTION SYSTEMS.

Benchmarks

$ go test -timeout 5h -bench=. -benchtime=1m

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMessageTooLong = errors.New("paillier: message too long for Paillier public key size")

ErrMessageTooLong is returned when attempting to encrypt a message which is too large for the size of the public key.

Functions

func Add

func Add(pubKey *PublicKey, cipher, constant []byte) []byte

Add homomorphically adds a passed constant to the encrypted integer (our cipher text). We do this by multiplying the constant with our ciphertext. Upon decryption, the resulting plain text will be the sum of the plaintext integer and the constant.

func AddCipher

func AddCipher(pubKey *PublicKey, cipher1, cipher2 []byte) []byte

AddCipher homomorphically adds together two cipher texts. To do this we multiply the two cipher texts, upon decryption, the resulting plain text will be the sum of the corresponding plain texts.

func Decrypt

func Decrypt(privKey *PrivateKey, cipherText []byte) ([]byte, error)

Decrypt decrypts the passed cipher text.

func Encrypt

func Encrypt(pubKey *PublicKey, plainText []byte) ([]byte, error)

Encrypt encrypts a plain text represented as a byte array. The passed plain text MUST NOT be larger than the modulus of the passed public key.

func EncryptAndNonce

func EncryptAndNonce(pubKey *PublicKey, plainText []byte) ([]byte, *big.Int, error)

EncryptAndNonce encrypts a plain text represented as a byte array, and in addition, returns the nonce used during encryption. The passed plain text MUST NOT be larger than the modulus of the passed public key.

func EncryptWithNonce

func EncryptWithNonce(pubKey *PublicKey, r *big.Int, plainText []byte) (*big.Int, error)

EncryptWithNonce encrypts a plain text represented as a byte array using the provided nonce to perform encryption. The passed plain text MUST NOT be larger than the modulus of the passed public key.

func Mul

func Mul(pubKey *PublicKey, cipher []byte, constant []byte) []byte

Mul homomorphically multiplies an encrypted integer (cipher text) by a constant. We do this by raising our cipher text to the power of the passed constant. Upon decryption, the resulting plain text will be the product of the plaintext integer and the constant.

Types

type PrivateKey

type PrivateKey struct {
	PublicKey
	// contains filtered or unexported fields
}

PrivateKey represents a Paillier key.

func GenerateKey

func GenerateKey(random io.Reader, bits int) (*PrivateKey, error)

GenerateKey generates an Paillier keypair of the given bit size using the random source random (for example, crypto/rand.Reader).

type PublicKey

type PublicKey struct {
	N        *big.Int // modulus
	G        *big.Int // n+1, since p and q are same length
	NSquared *big.Int
}

PublicKey represents the public part of a Paillier key.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL