elgamal

package module
v0.0.0-...-b380107 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2021 License: MIT Imports: 6 Imported by: 0

README

ElGamal Cryptosystem (Encryption & Signature Schemes)

This package is implemented according to the pseudo-code and mathematical notations of the following algorithms of the ElGamal cryptosystem:

  • Key Generation
  • Encryption Scheme
    • Encryption
    • Decryption
  • Signature Scheme
    • Signature Generation
    • Signature Verification

ElGamal has multiplicative homomorphic encryption property and is an example of Partially Homomorphic Encryption (PHE). Therefore, the multiplication of ciphers results in the product of original numbers.

Moreover, it also supports the following PHE functions:

  • Homomorphic Encryption over two ciphers
  • Homomorphic Encryption over multiple ciphers

Installation

go get -u github.com/Mirzazhar/elgamal

Warning

This package is intendedly designed for education purposes. Of course, it may contain bugs and needs several improvements. Therefore, this package should not be used for production purposes.

Usage & Examples

Acknowledge

I’m extremely grateful to Drogunov Igor for her contribution in choosing large prime p along with a cyclic group generator Zp. Based on this, I was able to complete the implementation of the keys generation algorithm.

LICENSE

MIT License

References

  1. https://en.wikipedia.org/wiki/ElGamal_encryption
  2. https://en.wikipedia.org/wiki/ElGamal_signature_scheme
  3. https://dl.acm.org/doi/pdf/10.1145/3214303
  4. https://pkg.go.dev/golang.org/x/crypto/openpgp/elgamal
  5. https://github.com/ldinc/pqg

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCipherLarge = errors.New("elgamal: cipher is larger than public key size")
View Source
var ErrMessageLarge = errors.New("elgamal: message is larger than public key size")

Functions

func Gen

func Gen(n, probability int) (*big.Int, *big.Int, *big.Int, error)

Note : this section of code is taken from (https://github.com/ldinc/pqg). Author of this code is "Drogunov Igor". Gen emit <p,q,g>. p = 2q + 1, p,q - safe primes g - cyclic group generator Zp performs n Miller-Rabin tests with 1 - 1/(4^n) probability false rate. Gain n - bit width for integer & probability rang for MR. It returns p, q, g and write error message.

func GeneratePQZp

func GeneratePQZp(bitsize, probability int) (p, q, g *big.Int, err error)

Types

type PrivateKey

type PrivateKey struct {
	PublicKey
	X *big.Int
}

PrivateKey represents Elgamal private key.

func GenerateKey

func GenerateKey(bitsize, probability int) (*PrivateKey, error)

GenerateKey generates elgamal private key according to given bit size and probability. Moreover, the given probability value is used in choosing prime number P for performing n Miller-Rabin tests with 1 - 1/(4^n) probability false rate.

func (*PrivateKey) Decrypt

func (priv *PrivateKey) Decrypt(cipher1, cipher2 []byte) ([]byte, error)

Decrypt decrypts the passed cipher text. It returns an error if cipher text value is larger than modulus P of Public key.

func (*PrivateKey) Signature

func (priv *PrivateKey) Signature(message []byte) ([]byte, []byte, error)

Signature generates signature over the given message. It returns signature value consisting of two parts "r" and "s" as byte arrays.

type PublicKey

type PublicKey struct {
	G, P, Y *big.Int
}

PublicKey represents a Elgamal public key.

func (*PublicKey) Encrypt

func (pub *PublicKey) Encrypt(message []byte) ([]byte, []byte, error)

Encrypt encrypts a plain text represented as a byte array. It returns an error if plain text value is larger than modulus P of Public key.

func (*PublicKey) HommorphicEncMultiple

func (pub *PublicKey) HommorphicEncMultiple(ciphertext [][2][]byte) ([]byte, []byte, error)

HommorphicEncMultiple performs homomorphic operation over multiple passed chiphers. Elgamal has multiplicative homomorphic property, so resultant cipher contains the product of multiple numbers.

func (*PublicKey) HomomorphicEncTwo

func (pub *PublicKey) HomomorphicEncTwo(c1, c2, c1dash, c2dash []byte) ([]byte, []byte, error)

HomomorphicEncTwo performs homomorphic operation over two passed chiphers. Elgamal has multiplicative homomorphic property, so resultant cipher contains the product of two numbers.

func (*PublicKey) SigVerify

func (pub *PublicKey) SigVerify(r, s, message []byte) (bool, error)

SigVerify verifies signature over the given message and signature values (r & s). It returns true as a boolean value if signature is verify correctly. Otherwise it returns false along with error message.

Jump to

Keyboard shortcuts

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