he

package module
v0.0.0-...-1b065a1 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2020 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

hewrap

Wrapper for homomorphic encryption implementations. Currently supports Paillier and BGN.

Installation

Installation is somewhat tedious since this library requires the GMP and PBC libraries to be installed on the machine. To do so, please follow the instructions below. It should only take a few minutes :)

To install GMP in Debian / Ubuntu:

$ sudo apt-get install libgmp-dev

For more information or to compile from source, visit https://gmplib.org/

To install the PBC library, download the appropriate files for your system from https://crypto.stanford.edu/pbc/download.html. PBC has three dependencies: the gcc compiler, flex (http://flex.sourceforge.net/), and bison (https://www.gnu.org/software/bison/).

To install GMP in Debian / Ubuntu:

$ sudo apt-get install build-essential flex bison

The PBC source can be compiled and installed using the usual GNU Build System:

$ wget -c https://crypto.stanford.edu/pbc/files/pbc-0.5.14.tar.gz -O - | tar -xz
$ cd pbc-0.5.14
$ ./configure
$ make
$ sudo make install

After installing, you may need to rebuild the search path for libraries.

Add:

export LD_LIBRARY_PATH=/usr/local/lib/

to your .profile or equivalent.

Documentation

Currently wraps Paillier and BGN homomorphic encryption schemes.

KeyGen
func KeyGen(secbits int, fixedPointPrecision FixedPointPrecision, scheme Scheme) (*SecretKey, *PublicKey) 

secBits: RSA security parameter e.g. 2048. fixedPointPrecision: fixed-point precision used to encoded float64 as fixed-point values. scheme: choose between Paillier and BGN.

Encrypt
func (pk *PublicKey) Encrypt(a *big.Int) *Ciphertext

Encrypts an integer using the public key.

func (pk *PublicKey) EncryptFloat(a *big.Float) *Ciphertext

Encrypts a float as a fixed-point value using the public key.

Decrypt
func (sk *SecretKey) Decrypt(pk *PublicKey, ct *Ciphertext) *big.Int

Decrypts an integer using the secret key.

func (sk *SecretKey) DecryptFloat(pk *PublicKey, ct *Ciphertext) *big.Float

Decrypts a fixed-point values using the secret key.

Homomorphic Addition/Subtraction
(pk *PublicKey) Add(a *Ciphertext, b *Ciphertext) *Ciphertext

Returns a ciphertext representing the addition of the values encrypted by a and b.

(pk *PublicKey) Sub(a *Ciphertext, b *Ciphertext) *Ciphertext

Returns a ciphertext representing the subtraction of the values encrypted by a and b.

Homomorphic Multiplication by Constant
func (pk *PublicKey) ConstMul(a *Ciphertext, constant *big.Int) *Ciphertext

Returns a ciphertext encrypting the value encrypted by a multiplied by constant.

func (pk *PublicKey) FloatConstMul(a *Ciphertext, constant *big.Float) *Ciphertext

Returns a ciphertext encrypting the value encrypted by a multiplied by the fixed-point encoding of constant.

Homomorphic Multiplication (only BGN)
func (pk *PublicKey) Mul(a *Ciphertext, b *Ciphertext) *Ciphertext {

Returns the encrypted product of the values encrypted by a and b. Only supported when using the BGN encryption scheme.

Disclaimer ⚠️

None of the cryptography used in this project was verified by experts. The code is intended to be used for research purposes only. DO NOT USE THIS CODE IN PRODUCTION.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AHEKeyGen

func AHEKeyGen(secbits int, fixedPointScale int64) (*AHESecretKey, *AHEPublicKey)

AHEKeyGen generates a (secret, public) key pair secbits is the RSA security parameter

func KeyGen

func KeyGen(secbits int, fixedPointPrecision FixedPointPrecision, scheme Scheme) (*SecretKey, *PublicKey)

KeyGen generates a public and secret key for a homomorphic encryption scheme (either Paillier or SHE)

func SHEKeyGen

func SHEKeyGen(secbits int, messageSpace *big.Int, fixedPointScale int64) (*SHESecretKey, *SHEPublicKey)

SHEKeyGen generates a (secret, public) key pair secbits is the RSA security parameter

Types

type AHECiphertext

type AHECiphertext struct {
	Ciphertext      *paillier.Ciphertext
	FixedPointScale *big.Int // fixed point scaling factor associated with the ciphertext (grows with # multiplications)
}

AHECiphertext consists of an ciphertext C in an additively homomorphic encryption (AHE) sheme, encrypting a value that is scaled by FixedPointScale

func (*AHECiphertext) Bytes

func (ct *AHECiphertext) Bytes() []byte

Bytes returns the byte encoding of the ciphertext struct

type AHEPublicKey

type AHEPublicKey struct {
	Key             *paillier.PublicKey
	FixedPointScale int64
	N               *big.Int // modulus
}

AHEPublicKey of the AHE scheme Modulus is the message space modulus FixedPointScale is the scaling factor for fixed-point representation in the message space

func (*AHEPublicKey) Add

Add uses the homomorphic properties of the encryption sheme to add two encrypted values together and returns the ciphertext encrypting the sum

func (*AHEPublicKey) CiphertextSpace

func (pk *AHEPublicKey) CiphertextSpace() *big.Int

CiphertextSpace reutns the size of the ciphertexts

func (*AHEPublicKey) ConstMul

func (pk *AHEPublicKey) ConstMul(a *AHECiphertext, constant *big.Int) *AHECiphertext

ConstMul multiplies an encrypted value by a plaintext constant (int) note that the constant is first converted to an element of the message space

func (*AHEPublicKey) DecodeFixedPoint

func (pk *AHEPublicKey) DecodeFixedPoint(a *big.Int, scale *big.Int) *big.Float

DecodeFixedPoint converts a fixed-pont encoded value into a floa64

func (*AHEPublicKey) EncodeFixedPoint

func (pk *AHEPublicKey) EncodeFixedPoint(a *big.Float) *big.Int

EncodeFixedPoint converts a floating-point value a to a fixed-point encoded integer value where the scaling factor is represented in the public key

func (*AHEPublicKey) Encrypt

func (pk *AHEPublicKey) Encrypt(a *big.Int) *AHECiphertext

Encrypt encrypts the int value with fixedpoint scaling factor of 1

func (*AHEPublicKey) EncryptDeterministic

func (pk *AHEPublicKey) EncryptDeterministic(a *big.Int) *AHECiphertext

EncryptDeterministic encrypts the int value without randomizing

func (*AHEPublicKey) EncryptFloat

func (pk *AHEPublicKey) EncryptFloat(a *big.Float) *AHECiphertext

EncryptFloat first converts the input into the encryption scheme's message space (using fixed-point encoding) and then returns a fresh encryption of the encoded value

func (*AHEPublicKey) EncryptFloatDeterministic

func (pk *AHEPublicKey) EncryptFloatDeterministic(a *big.Float) *AHECiphertext

EncryptFloatDeterministic encrypts a float without randomness

func (*AHEPublicKey) EncryptOne

func (pk *AHEPublicKey) EncryptOne() *AHECiphertext

EncryptOne returns a ciphertext encrypting one

func (*AHEPublicKey) EncryptZero

func (pk *AHEPublicKey) EncryptZero() *AHECiphertext

EncryptZero returns a ciphertext encrypting zero

func (*AHEPublicKey) FloatConstMul

func (pk *AHEPublicKey) FloatConstMul(a *AHECiphertext, constant *big.Float) *AHECiphertext

FloatConstMul multiplies an encrypted value by a plaintext constant (float) note that the constant is first converted to an element of the message space

func (*AHEPublicKey) MessageSpace

func (pk *AHEPublicKey) MessageSpace() *big.Int

MessageSpace returns the largest value that can be encrypted

func (*AHEPublicKey) Modulus

func (pk *AHEPublicKey) Modulus() *big.Int

Modulus returns the modulus of the message space

func (*AHEPublicKey) NewCiphertextFromBytes

func (pk *AHEPublicKey) NewCiphertextFromBytes(data []byte) (*AHECiphertext, error)

NewCiphertextFromBytes initializes a ciphertext from a byte encoding. Requires the public key to ensure field elements are correct (see PBC library)

func (*AHEPublicKey) NullCiphertext

func (pk *AHEPublicKey) NullCiphertext() *AHECiphertext

NullCiphertext returns an encryption of zero without randomness

func (*AHEPublicKey) Sub

Sub uses the homomorphic properties of the encryption sheme to evaluate and return and encryption of the the difference a - b

type AHESecretKey

type AHESecretKey struct {
	Key *paillier.SecretKey
}

AHESecretKey of the AHE scheme

func (*AHESecretKey) Decrypt

func (sk *AHESecretKey) Decrypt(pk *AHEPublicKey, ct *AHECiphertext) *big.Int

Decrypt uses the secret key to decrypt a ciphertext

func (*AHESecretKey) DecryptFloat

func (sk *AHESecretKey) DecryptFloat(pk *AHEPublicKey, ct *AHECiphertext) *big.Float

DecryptFloat uses the secret key to decrypt a ciphertext and recover the fixed-point encoded value which is returned as a float64 type

type Ciphertext

type Ciphertext struct {
	Paillier *AHECiphertext
	Bgn      *SHECiphertext
	IsShe    bool
}

Ciphertext for either Paillier or SHE scheme

func (*Ciphertext) Bytes

func (ct *Ciphertext) Bytes() ([]byte, error)

Bytes returns the byte encoding of the ciphertext struct

func (*Ciphertext) String

func (ct *Ciphertext) String() string

type FixedPointPrecision

type FixedPointPrecision int64

FixedPointPrecision specifies fixed-point encoding scale used to encode floating point values over the integers

const (
	// LowPrecision = 1 << 10 scaling factor
	LowPrecision FixedPointPrecision = 1 << 10

	// MediumPrecision = 1 << 20 scaling factor
	MediumPrecision FixedPointPrecision = 1 << 20

	// HighPrecision = 1 << 30 scaling factor
	HighPrecision FixedPointPrecision = 1 << 30

	// VeryHighPrecision = 1 << 40 scaling factor
	VeryHighPrecision FixedPointPrecision = 1 << 40
)

type PublicKey

type PublicKey struct {
	Paillier *AHEPublicKey
	Bgn      *SHEPublicKey
	IsShe    bool
}

PublicKey for either Paillier or SHE scheme

func (*PublicKey) Add

func (pk *PublicKey) Add(a *Ciphertext, b *Ciphertext) *Ciphertext

Add uses the homomorphic properties of the encryption sheme to add two encrypted values together and returns the ciphertext encrypting the sum

func (*PublicKey) CiphertextSpace

func (pk *PublicKey) CiphertextSpace() *big.Int

CiphertextSpace returns the size of a ciphertext

func (*PublicKey) ConstMul

func (pk *PublicKey) ConstMul(a *Ciphertext, constant *big.Int) *Ciphertext

ConstMul multiplies an encrypted value by a plaintext constant (int) note that the constant is first converted to an element of the message space

func (*PublicKey) DecodeFixedPoint

func (pk *PublicKey) DecodeFixedPoint(a *big.Int, scale *big.Int) *big.Float

DecodeFixedPoint converts a fixed-pont encoded value into a floa64

func (*PublicKey) EncodeFixedPoint

func (pk *PublicKey) EncodeFixedPoint(a *big.Float) *big.Int

EncodeFixedPoint converts a floating-point value a to a fixed-point encoded integer value where the scaling factor is represented in the public key

func (*PublicKey) Encrypt

func (pk *PublicKey) Encrypt(a *big.Int) *Ciphertext

Encrypt encrypts the int value with fixedpoint scaling factor of 1

func (*PublicKey) EncryptDeterministic

func (pk *PublicKey) EncryptDeterministic(a *big.Int) *Ciphertext

EncryptDeterministic encrypts the int value without randomness

func (*PublicKey) EncryptFloat

func (pk *PublicKey) EncryptFloat(a *big.Float) *Ciphertext

EncryptFloat first converts the input into the encryption scheme's message space (using fixed-point encoding) and then returns a fresh encryption of the encoded value

func (*PublicKey) EncryptFloatDeterministic

func (pk *PublicKey) EncryptFloatDeterministic(a *big.Float) *Ciphertext

EncryptFloatDeterministic encrypts a flaot without randomness

func (*PublicKey) EncryptOne

func (pk *PublicKey) EncryptOne() *Ciphertext

EncryptOne returns a ciphertext encrypting one

func (*PublicKey) EncryptZero

func (pk *PublicKey) EncryptZero() *Ciphertext

EncryptZero returns a ciphertext encrypting zero

func (*PublicKey) FloatConstMul

func (pk *PublicKey) FloatConstMul(a *Ciphertext, constant *big.Float) *Ciphertext

FloatConstMul multiplies an encrypted value by a plaintext constant (float) note that the constant is first converted to an element of the message space

func (*PublicKey) MessageSpace

func (pk *PublicKey) MessageSpace() *big.Int

MessageSpace returns the largest value that can be encrypted

func (*PublicKey) Modulus

func (pk *PublicKey) Modulus() *big.Int

Modulus returns the modulus of the message space (may be larger than message space; e.g. BGN)

func (*PublicKey) Mul

func (pk *PublicKey) Mul(a *Ciphertext, b *Ciphertext) *Ciphertext

Mul multiplies an encrypted value by a another encrypted value b

func (*PublicKey) NewCiphertextFromBytes

func (pk *PublicKey) NewCiphertextFromBytes(data []byte) (*Ciphertext, error)

NewCiphertextFromBytes initializes a ciphertext from a byte encoding. Requires the public key to ensure field elements are correct (see PBC library)

func (*PublicKey) NullCiphertext

func (pk *PublicKey) NullCiphertext() *Ciphertext

NullCiphertext returns a ciphertext encrypting zero without randomness

func (*PublicKey) Sub

func (pk *PublicKey) Sub(a *Ciphertext, b *Ciphertext) *Ciphertext

Sub uses the homomorphic properties of the encryption sheme to evaluate and return and encryption of the the difference a - b

type SHECiphertext

type SHECiphertext struct {
	Ciphertext *bgn.Ciphertext
	Poly       *bgn.PolyCiphertext
	IsPoly     bool // if the ciphertext is a polynomial
}

SHECiphertext consists of an ciphertext C in a somewhat homomorphic encryption (SHE) sheme, encrypting a value that is scaled by FixedPointScale

func (*SHECiphertext) Bytes

func (ct *SHECiphertext) Bytes() []byte

Bytes returns the byte encoding of the ciphertext struct

type SHEPublicKey

type SHEPublicKey struct {
	Key      *bgn.PublicKey
	MsgSpace *big.Int
}

SHEPublicKey of the SHE scheme

func (*SHEPublicKey) Add

Add uses the homomorphic properties of the encryption sheme to add two encrypted values together and returns the ciphertext encrypting the sum

func (*SHEPublicKey) CiphertextSpace

func (pk *SHEPublicKey) CiphertextSpace() *big.Int

CiphertextSpace reutns the size of the ciphertexts

func (*SHEPublicKey) ConstMul

func (pk *SHEPublicKey) ConstMul(a *SHECiphertext, constant *big.Int) *SHECiphertext

ConstMul multiplies an encrypted value by a plaintext constant (int) note that the constant is first converted to an element of the message space

func (*SHEPublicKey) Encrypt

func (pk *SHEPublicKey) Encrypt(a *big.Int) *SHECiphertext

Encrypt first converts the input into the encryption scheme's message space (using fixed-point encoding) and then returns a fresh encryption of the encoded value

func (*SHEPublicKey) EncryptDeterministic

func (pk *SHEPublicKey) EncryptDeterministic(a *big.Int) *SHECiphertext

EncryptDeterministic encrypts the int value without randomizing

func (*SHEPublicKey) EncryptFloat

func (pk *SHEPublicKey) EncryptFloat(a *big.Float) *SHECiphertext

EncryptFloat encryptes a floating point using a fixed-point representation

func (*SHEPublicKey) EncryptFloatDeterministic

func (pk *SHEPublicKey) EncryptFloatDeterministic(a *big.Float) *SHECiphertext

EncryptFloatDeterministic encrypts a float without randomness

func (*SHEPublicKey) EncryptOne

func (pk *SHEPublicKey) EncryptOne() *SHECiphertext

EncryptOne returns a ciphertext encrypting one

func (*SHEPublicKey) EncryptZero

func (pk *SHEPublicKey) EncryptZero() *SHECiphertext

EncryptZero returns a ciphertext encrypting zero

func (*SHEPublicKey) FloatConstMul

func (pk *SHEPublicKey) FloatConstMul(a *SHECiphertext, constant *big.Float) *SHECiphertext

FloatConstMul multiplies an encrypted value by a plaintext constant (float) note that the constant is first converted to an element of the message space

func (*SHEPublicKey) MessageSpace

func (pk *SHEPublicKey) MessageSpace() *big.Int

MessageSpace returns the largest value that can be encrypted

func (*SHEPublicKey) Modulus

func (pk *SHEPublicKey) Modulus() *big.Int

Modulus returns the modulus of the message space (may be larger than message space; e.g. BGN)

func (*SHEPublicKey) Mul

Mul multiplies two encryped values homomorphically and returns the ciphertext encrypting the product

func (*SHEPublicKey) NewCiphertextFromBytes

func (pk *SHEPublicKey) NewCiphertextFromBytes(data []byte) (*SHECiphertext, error)

NewCiphertextFromBytes initializes a ciphertext from a byte encoding. Requires the public key to ensure field elements are correct (see PBC library)

func (*SHEPublicKey) NullCiphertext

func (pk *SHEPublicKey) NullCiphertext() *SHECiphertext

NullCiphertext returns an encryption of zero without randomness

func (*SHEPublicKey) Sub

Sub uses the homomorphic properties of the encryption sheme to evaluate and return and encryption of the the difference a - b

type SHESecretKey

type SHESecretKey struct {
	Key *bgn.SecretKey
}

SHESecretKey of the AHE scheme

func (*SHESecretKey) Decrypt

func (sk *SHESecretKey) Decrypt(pk *SHEPublicKey, ct *SHECiphertext) *big.Int

Decrypt uses the secret key to decrypt a ciphertext

func (*SHESecretKey) DecryptFloat

func (sk *SHESecretKey) DecryptFloat(pk *SHEPublicKey, ct *SHECiphertext) *big.Float

DecryptFloat uses the secret key to decrypt a ciphertext and recover the fixed-point encoded value which is returned as a float64 type

type Scheme

type Scheme int

Scheme specifies the encryption scheme to use

const (
	// Paillier is an additively homomorphic encryption scheme
	Paillier Scheme = iota

	// BGN is a somewhat homomorphic encryption scheme supporting addition and one multiplication
	BGN
)

type SecretKey

type SecretKey struct {
	Paillier *AHESecretKey
	Bgn      *SHESecretKey
	IsShe    bool
}

SecretKey for either Paillier or SHE scheme

func (*SecretKey) Decrypt

func (sk *SecretKey) Decrypt(pk *PublicKey, ct *Ciphertext) *big.Int

Decrypt uses the secret key to decrypt a ciphertext

func (*SecretKey) DecryptFloat

func (sk *SecretKey) DecryptFloat(pk *PublicKey, ct *Ciphertext) *big.Float

DecryptFloat uses the secret key to decrypt a ciphertext and recover the fixed-point encoded value which is returned as a float64 type

Jump to

Keyboard shortcuts

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