arnaucube
arnaucube/cryptofunPublic Self-hosted repository
cryptofun/paillier/README.md
paillier/README.mdRaw
 1## Paillier cryptosystem & Homomorphic Addition
 2- https://en.wikipedia.org/wiki/Paillier_cryptosystem
 3- https://en.wikipedia.org/wiki/Homomorphic_encryption
 4
 5- [x] GenerateKeyPair
 6- [x] Encrypt
 7- [x] Decrypt
 8- [x] Homomorphic Addition
 9
10#### Usage
11- Encrypt, Decrypt
12```go
13// key generation
14key, err := GenerateKeyPair()
15if err!=nil {
16	fmt.Println(err)
17}
18
19mBytes := []byte("Hi")
20m := new(big.Int).SetBytes(mBytes)
21
22// encryption
23c := Encrypt(m, key.PubK)
24
25// decryption
26d := Decrypt(c, key.PubK, key.PrivK)
27if m == d {
28	fmt.Println("ciphertext decrypted correctly")
29}
30```
31
32- Homomorphic Addition
33```go
34// key generation [Alice]
35key, err := GenerateKeyPair()
36if err!=nil {
37	fmt.Println(err)
38}
39
40// define values [Alice]
41n1 := big.NewInt(int64(110))
42n2 := big.NewInt(int64(150))
43
44// encrypt values [Alice]
45c1 := Encrypt(n1, key.PubK)
46c2 := Encrypt(n2, key.PubK)
47
48// compute homomorphic addition [Bob]
49c3c4 := HomomorphicAddition(c1, c2, key.PubK)
50
51// decrypt the result [Alice]
52d := Decrypt(c3c4, key.PubK, key.PrivK)
53if !bytes.Equal(new(big.Int).Add(n1, n2).Bytes(), d.Bytes()) {
54	fmt.Println("decrypted result not equal to expected result")
55}
56```