paillier

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2021 License: Apache-2.0 Imports: 5 Imported by: 1

README

TNO MPC Lab - Paillier

The TNO MPC lab consists of generic software components, procedures, and functionalities developed and maintained on a regular basis to facilitate and aid in the development of MPC solutions. The lab is a cross-project initiative allowing us to integrate and reuse previously developed MPC functionalities to boost the development of new protocols and solutions.

The package paillier is part of the TNO Go Toolbox.

Limitations in (end-)use: the content of this repository may solely be used for applications that comply with international export control laws.

Paillier cryptosystem

This library is an implementation of the Paillier homomorphic encryption scheme in Go.

Usage

To generate a Paillier private key, use GenerateKey:

	sk, err := paillier.GenerateKey(2048)
	if err != nil {
		t.Fatalf("Error generating key: %v", err)
	}

You can extract the public key from the returned structure, and send it safely to others.

	pk := sk.PublicKey

To encrypt data, use the Encrypt method on a public or private key. You can also compute with encrypted data by using the Add and Mul methods.

	m1 := big.NewInt(10)
	m2 := big.NewInt(15)
	sum := new(big.Int).Add(m1, m2)

	ct1 := pk.Encrypt(m1)

	ct2 := pk.Encrypt(m2)

	ctSum := pk.Add(ct1, ct2)

	decryptedSum := sk.Decrypt(ctSum)

	// decryptedSum.Cmp(sum) == 0

The Paillier cryptosystem allows you to randomize ciphertexts. This is necessary, since otherwise, certain meaningful values (e.g. 0) would be easy to recognize if they result from a computation, even by persons without the private key. You can use the Randomize method to re-randomize any ciphertext.

Since random data is expensive in terms of time, the package comes with a facility to pre-compute randomness. If you need to encrypt large data sets, but not very often, you can speed up encryption by keeping a buffer of randomness that is replenished after you are done with encrypting the data set. See documentation on the NewPrecomputeBuffer function for information on how to use it.

Both the precompute buffer and the public key implement the Encrypter interface, which you can use in your software to transparently work with either plain keys or keys with a buffer attached.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Encrypter

type Encrypter interface {
	// Randomize (re-)randomizes an encrypted value
	Randomize(ciphertext *big.Int) *big.Int
	// Encrypt encrypts a message (big.Int)
	Encrypt(plaintext *big.Int) *big.Int
	// PartiallyEncrypt does the first part of the encryption, so that the
	// (computationally expensive) randomization can be done at a later time.
	PartiallyEncrypt(plaintext *big.Int) *big.Int
	// Add adds two encrypted values and returns the encrypted sum.
	Add(ciphertextA, ciphertextB *big.Int) *big.Int
	// AddTo adds b to the Encrypted value a (and also returns it).
	AddTo(ciphertextA, ciphertextB *big.Int) *big.Int
	// Mul multiplies the plaintext value b with the encrypted value a and returns the result.
	Mul(ciphertextA, plaintextB *big.Int) *big.Int
}

Encrypter is an interface for additively homomorphic encryption schemes.

type PrecomputeBuffer

type PrecomputeBuffer struct {
	PublicKey // public key for which we generate random exponents
	// contains filtered or unexported fields
}

PrecomputeBuffer embeds PublicKey and thus implements the Encrypter interface,

func NewPrecomputeBuffer

func NewPrecomputeBuffer(pk PublicKey, bufferSize int, numProc int, waitForCompletion bool) (*PrecomputeBuffer, error)

NewPrecomputeBuffer creates a new PrecomputeBuffer for the specified public key and immediately starts filling this buffer using numProc processors (goroutines).

func (*PrecomputeBuffer) Close

func (pcbuf *PrecomputeBuffer) Close()

Close stops the goroutines associated with the PrecomputeBuffer

func (*PrecomputeBuffer) Encrypt

func (pcbuf *PrecomputeBuffer) Encrypt(m *big.Int) *big.Int

Encrypt encrypts a message (big.Int) and uses pre-computed random exponents to speed-up the computation.

func (*PrecomputeBuffer) Get

func (pcbuf *PrecomputeBuffer) Get() *big.Int

Get returns a new random exponent

func (*PrecomputeBuffer) Randomize

func (pcbuf *PrecomputeBuffer) Randomize(a *big.Int) *big.Int

Randomize randomizes an encrypted value and uses pre-computed random exponents to speed-up the computation.

type PrecomputeBufferMock

type PrecomputeBufferMock struct {
	PublicKey // public key for which we generate random exponents
	// contains filtered or unexported fields
}

func NewPrecomputeBufferMock

func NewPrecomputeBufferMock(pk PublicKey) (*PrecomputeBufferMock, error)

NewPrecomputeBufferMock creates a new PrecomputeBufferMock for the specified public key and reuses the same random value to mock a filled ProcomputeBuffer

func (*PrecomputeBufferMock) Close

func (pcbuf *PrecomputeBufferMock) Close()

Close stops the goroutines associated with the PrecomputeBuffer

func (*PrecomputeBufferMock) Encrypt

func (pcbuf *PrecomputeBufferMock) Encrypt(m *big.Int) *big.Int

Encrypt encrypts a message (big.Int) and uses pre-computed random exponents to speed-up the computation.

func (*PrecomputeBufferMock) Get

func (pcbuf *PrecomputeBufferMock) Get() *big.Int

Get returns a new random exponent

func (*PrecomputeBufferMock) Randomize

func (pcbuf *PrecomputeBufferMock) Randomize(a *big.Int) *big.Int

Randomize randomizes an encrypted value and uses pre-computed random exponents to speed-up the computation.

type PrivateKey

type PrivateKey struct {
	PublicKey
	Lambda *big.Int
	X      *big.Int // Cached value for faster decryption
}

PrivateKey is a Paillier private key

func GenerateKey

func GenerateKey(bits int) (priv *PrivateKey, err error)

GenerateKey generates a random Paillier private key

func (*PrivateKey) Decrypt

func (k *PrivateKey) Decrypt(c *big.Int) *big.Int

Decrypt attempts to decrypt the provided cyphertext and returns the result.

type PublicKey

type PublicKey struct {
	N      *big.Int
	N2     *big.Int // cached value of N^2
	Nplus1 *big.Int // cached value of N + 1
	// contains filtered or unexported fields
}

PublicKey is a Paillier public key and implements the Encrypter interface

func (*PublicKey) Add

func (k *PublicKey) Add(a, b *big.Int) *big.Int

Add adds two encrypted values and returns the encrypted sum.

func (*PublicKey) AddTo

func (k *PublicKey) AddTo(a, b *big.Int) *big.Int

AddTo adds b to the Encrypted value a (and also returns it).

func (*PublicKey) Encrypt

func (k *PublicKey) Encrypt(m *big.Int) *big.Int

Encrypt encrypts a message (big.Int).

func (*PublicKey) Mul

func (k *PublicKey) Mul(a, b *big.Int) *big.Int

Mul multiplies the plaintext value b with the encrypted value a and returns the result.

func (*PublicKey) PartiallyEncrypt

func (k *PublicKey) PartiallyEncrypt(m *big.Int) *big.Int

PartiallyEncrypt does the first part of the encryption, so that the (computationally expensive) randomization can be done at a later time.

func (*PublicKey) Randomize

func (k *PublicKey) Randomize(a *big.Int) *big.Int

Randomize randomizes an encrypted value.

Jump to

Keyboard shortcuts

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