forked from Roasbeef/go-go-gadget-paillier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paillier_test.go
134 lines (114 loc) · 3.86 KB
/
paillier_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package paillier_test
import (
"crypto/rand"
"github.com/roasbeef/go-go-gadget-paillier"
"math/big"
"testing"
)
func TestCorrectness(t *testing.T) {
// Generate a 128-bit private key.
privKey, err := paillier.GenerateKey(rand.Reader, 128)
if err != nil {
t.Fatalf("Unable to generate private key: ", err)
}
// Encrypt the integer 15.
m := new(big.Int).SetInt64(15)
c, err := paillier.Encrypt(&privKey.PublicKey, m.Bytes())
if err != nil {
t.Fatalf("Unable to encrypt plain text: ", err)
}
// Now decrypt the cipher text. Should come back out to 15.
d, err := paillier.Decrypt(privKey, c)
if err != nil {
t.Fatalf("Unable to decrypt cipher text: ", err)
}
originalInt := new(big.Int).SetBytes(d)
if originalInt.Cmp(m) != 0 { // originalInt != 15
t.Fatalf("Scheme is not correct. Got %v back should've got %v",
originalInt.String(), m.String())
}
}
func TestHomomorphicCipherTextAddition(t *testing.T) {
// Generate a 128-bit private key.
privKey, err := paillier.GenerateKey(rand.Reader, 128)
if err != nil {
t.Fatalf("Unable to generate private key: ", err)
}
// Encrypt the integer 15.
m15 := new(big.Int).SetInt64(15)
c15, err := paillier.Encrypt(&privKey.PublicKey, m15.Bytes())
if err != nil {
t.Fatalf("Unable to encrypt plain text: ", err)
}
// Encrypt the integer 20.
m20 := new(big.Int).SetInt64(20)
c20, err := paillier.Encrypt(&privKey.PublicKey, m20.Bytes())
if err != nil {
t.Fatalf("Unable to encrypt plain text: ", err)
}
// Now homomorphically add the encrypted integers.
addedCiphers := paillier.AddCipher(&privKey.PublicKey, c15, c20)
// When decrypted, the result should be 15+20 = 35
plaintext, err := paillier.Decrypt(privKey, addedCiphers)
if err != nil {
t.Fatalf("Unable to decrypted cipher text: ", err)
}
decryptedInt := new(big.Int).SetBytes(plaintext)
if decryptedInt.Cmp(new(big.Int).SetInt64(35)) != 0 {
t.Fatalf("Incorrect. Plaintext decrypted to %v should be %v",
decryptedInt.String(), 35)
}
}
func TestHomomorphicConstantAddition(t *testing.T) {
// Generate a 128-bit private key.
privKey, err := paillier.GenerateKey(rand.Reader, 128)
if err != nil {
t.Fatalf("Unable to generate private key: ", err)
}
// Encrypt the integer 15.
m15 := new(big.Int).SetInt64(15)
c15, err := paillier.Encrypt(&privKey.PublicKey, m15.Bytes())
if err != nil {
t.Fatalf("Unable to encrypt plain text: ", err)
}
// Attempt to add the plaintext constant "10" to our encrypted integer
// "15".
ten := new(big.Int).SetInt64(10)
encryptedAdd := paillier.Add(&privKey.PublicKey, c15, ten.Bytes())
plainText, err := paillier.Decrypt(privKey, encryptedAdd)
if err != nil {
t.Fatalf("Unable to decrypt cipher text: ", err)
}
decryptedInt := new(big.Int).SetBytes(plainText)
// When decrypted, the result should be 15+10 = 25
if decryptedInt.Cmp(new(big.Int).SetInt64(25)) != 0 {
t.Fatalf("Incorrect. Plaintext decrypted to %v should be %v",
decryptedInt.String(), 25)
}
}
func TestHomomorphicConstantMultiplication(t *testing.T) {
// Generate a 128-bit private key.
privKey, err := paillier.GenerateKey(rand.Reader, 128)
if err != nil {
t.Fatalf("Unable to generate private key: ", err)
}
// Encrypt the integer 15.
m15 := new(big.Int).SetInt64(15)
c15, err := paillier.Encrypt(&privKey.PublicKey, m15.Bytes())
if err != nil {
t.Fatalf("Unable to encrypt plain text: ", err)
}
// Attempt to multiply our encrypted integer
ten := new(big.Int).SetInt64(10)
encryptedAdd := paillier.Mul(&privKey.PublicKey, c15, ten.Bytes())
plainText, err := paillier.Decrypt(privKey, encryptedAdd)
if err != nil {
t.Fatalf("Unable to decrypt cipher text: ", err)
}
decryptedInt := new(big.Int).SetBytes(plainText)
// When decrypted, the result should be 15*10 = 150
if decryptedInt.Cmp(new(big.Int).SetInt64(150)) != 0 {
t.Fatalf("Incorrect. Plaintext decrypted to %v should be %v",
decryptedInt.String(), 150)
}
}