forked from Roasbeef/go-go-gadget-paillier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
80 lines (71 loc) · 2.13 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package paillier_test
import (
"crypto/rand"
"fmt"
"math/big"
"github.com/roasbeef/go-go-gadget-paillier"
)
// This example demonstrates basic usage of this library.
// Features shown:
// * Encrypt/Decrypt
// * Homomorphic cipher text addition
// * Homomorphic addition with constant
// * Homomorphic multiplication with constant
func main() {
// Generate a 128-bit private key.
privKey, err := paillier.GenerateKey(rand.Reader, 128)
if err != nil {
fmt.Println(err)
return
}
// Encrypt the number "15".
m15 := new(big.Int).SetInt64(15)
c15, err := paillier.Encrypt(&privKey.PublicKey, m15.Bytes())
if err != nil {
fmt.Println(err)
return
}
// Decrypt the number "15".
d, err := paillier.Decrypt(privKey, c15)
if err != nil {
fmt.Println(err)
return
}
plainText := new(big.Int).SetBytes(d)
fmt.Println("Decryption Result of 15: ", plainText.String())
// Now for the fun stuff.
// Encrypt the number "20".
m20 := new(big.Int).SetInt64(20)
c20, err := paillier.Encrypt(&privKey.PublicKey, m20.Bytes())
if err != nil {
fmt.Println(err)
return
}
// Add the encrypted integers 15 and 20 together.
plusM16M20 := paillier.AddCipher(&privKey.PublicKey, c15, c20)
decryptedAddition, err := paillier.Decrypt(privKey, plusM16M20)
if err != nil {
fmt.Println(err)
return
}
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, err = paillier.Decrypt(privKey, plusE15and10)
if err != nil {
fmt.Println(err)
return
}
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, err := paillier.Decrypt(privKey, mulE15and10)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Result of 15*10 after decryption: ",
new(big.Int).SetBytes(decryptedMul).String()) // 150
}