-
Notifications
You must be signed in to change notification settings - Fork 0
/
paillier.go
274 lines (243 loc) · 8.1 KB
/
paillier.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package crypto
import (
"crypto/rand"
"errors"
"io"
"math/big"
)
var one = big.NewInt(1)
var zero = big.NewInt(0)
// GenerateKey generates an Paillier keypair of the given bit size using the
// random source random (for example, crypto/rand.Reader).
func GenerateKey(random io.Reader, bits int) (*PrivateKey, error) {
// First, begin generation of p in the background.
var p *big.Int
var errChan = make(chan error, 1)
go func() {
var err error
p, err = rand.Prime(random, bits/2)
errChan <- err
}()
// Now, find a prime q in the foreground.
q, err := rand.Prime(random, bits/2)
if err != nil {
return nil, err
}
// Wait for generation of p to complete successfully.
if err := <-errChan; err != nil {
return nil, err
}
n := new(big.Int).Mul(p, q)
pp := new(big.Int).Mul(p, p)
qq := new(big.Int).Mul(q, q)
return &PrivateKey{
PublicKey: PublicKey{
N: n,
NSquared: new(big.Int).Mul(n, n),
G: new(big.Int).Add(n, one), // g = n + 1
},
p: p,
pp: pp,
pminusone: new(big.Int).Sub(p, one),
q: q,
qq: qq,
qminusone: new(big.Int).Sub(q, one),
pinvq: new(big.Int).ModInverse(p, q),
hp: h(p, pp, n),
hq: h(q, qq, n),
n: n,
}, nil
}
// PrivateKey represents a Paillier key.
type PrivateKey struct {
PublicKey
p *big.Int
pp *big.Int
pminusone *big.Int
q *big.Int
qq *big.Int
qminusone *big.Int
pinvq *big.Int
hp *big.Int
hq *big.Int
n *big.Int
}
// PublicKey represents the public part of a Paillier key.
type PublicKey struct {
N *big.Int // modulus
G *big.Int // n+1, since p and q are same length
NSquared *big.Int
}
func GetPulicKey(nstr string) (*PublicKey){
n,ok := new(big.Int).SetString(nstr,0)
if ok {
return &PublicKey{
N: n,
NSquared: new(big.Int).Mul(n, n),
G: new(big.Int).Add(n, one), // g = n + 1
}
}else{
return nil
}
}
func h(p *big.Int, pp *big.Int, n *big.Int) *big.Int {
gp := new(big.Int).Mod(new(big.Int).Sub(one, n), pp)
lp := l(gp, p)
hp := new(big.Int).ModInverse(lp, p)
return hp
}
func l(u *big.Int, n *big.Int) *big.Int {
return new(big.Int).Div(new(big.Int).Sub(u, one), n)
}
// Encrypt encrypts a plain text represented as a byte array. The passed plain
// text MUST NOT be larger than the modulus of the passed public key.
func Encrypt(pubKey *PublicKey, plainText []byte) ([]byte, error) {
c, _, err := EncryptAndNonce(pubKey, plainText)
return c, err
}
// EncryptAndNonce encrypts a plain text represented as a byte array, and in
// addition, returns the nonce used during encryption. The passed plain text
// MUST NOT be larger than the modulus of the passed public key.
func EncryptAndNonce(pubKey *PublicKey, plainText []byte) ([]byte, *big.Int, error) {
r, err := rand.Int(rand.Reader, pubKey.N)
if err != nil {
return nil, nil, err
}
c, err := EncryptWithNonce(pubKey, r, plainText)
if err != nil {
return nil, nil, err
}
return c.Bytes(), r, nil
}
// EncryptWithNonce encrypts a plain text represented as a byte array using the
// provided nonce to perform encryption. The passed plain text MUST NOT be
// larger than the modulus of the passed public key.
func EncryptWithNonce(pubKey *PublicKey, r *big.Int, plainText []byte) (*big.Int, error) {
m := new(big.Int).SetBytes(plainText)
if pubKey.N.Cmp(m) < 1 { // N < m
return nil, errors.New("paillier: message too long for Paillier public key size")
}
// c = g^m * r^n mod n^2 = ((m*n+1) mod n^2) * r^n mod n^2
n := pubKey.N
c := new(big.Int).Mod(
new(big.Int).Mul(
new(big.Int).Mod(new(big.Int).Add(one, new(big.Int).Mul(m, n)), pubKey.NSquared),
new(big.Int).Exp(r, n, pubKey.NSquared),
),
pubKey.NSquared,
)
return c, nil
}
func EncryptNumberWithNonce(pubKey *PublicKey, r *big.Int, m *big.Int) (*big.Int, error) {
if pubKey.N.Cmp(new(big.Int).Abs(m)) < 1 { // N < m
return nil, errors.New("paillier: message too long for Paillier public key size")
}
// c = g^m * r^n mod n^2 = ((m*n+1) mod n^2) * r^n mod n^2
n := pubKey.N
nsquare := pubKey.NSquared
a := new(big.Int)
c := new(big.Int)
_,a = new(big.Int).QuoRem(new(big.Int).Add(one, new(big.Int).Mul(m, n)), nsquare,a)
_,c = new(big.Int).QuoRem(
new(big.Int).Mul(
a,
new(big.Int).Exp(r, n, nsquare),
),
nsquare,a)
//rn := new(big.Int).Exp(r,n,nsquare)
//gm := new(big.Int).Mod(
// new(big.Int).Add(new(big.Int).Mul(m,n),one),nsquare)
//c := new(big.Int).Mod(new(big.Int).Mul(gm,rn),nsquare)
return c, nil
}
// Decrypt decrypts the passed cipher text.
func Decrypt(privKey *PrivateKey, cipherText []byte) ([]byte, error) {
c := new(big.Int).SetBytes(cipherText)
if privKey.NSquared.Cmp(c) < 1 { // c < n^2
return nil, errors.New("paillier: message too long for Paillier public key size")
}
cp := new(big.Int).Exp(c, privKey.pminusone, privKey.pp)
lp := l(cp, privKey.p)
mp := new(big.Int).Mod(new(big.Int).Mul(lp, privKey.hp), privKey.p)
cq := new(big.Int).Exp(c, privKey.qminusone, privKey.qq)
lq := l(cq, privKey.q)
mqq := new(big.Int).Mul(lq, privKey.hq)
mq := new(big.Int).Mod(mqq, privKey.q)
m := crt(mp, mq, privKey)
return m.Bytes(), nil
}
// Decrypt decrypts the passed cipher text.
func DecryptNumber(privKey *PrivateKey, c *big.Int) (*big.Int, error) {
if privKey.NSquared.Cmp(c) < 1 { // c < n^2
return nil, errors.New("paillier: message too long for Paillier public key size")
}
cp := new(big.Int).Exp(c, privKey.pminusone, privKey.pp)
lp := l(cp, privKey.p)
mp := new(big.Int).Mod(new(big.Int).Mul(lp, privKey.hp), privKey.p)
cq := new(big.Int).Exp(c, privKey.qminusone, privKey.qq)
lq := l(cq, privKey.q)
mqq := new(big.Int).Mul(lq, privKey.hq)
mq := new(big.Int).Mod(mqq, privKey.q)
m := crt(mp, mq, privKey)
n := privKey.N
if m.Cmp(new(big.Int).Exp(new(big.Int).SetInt64(2),new(big.Int).SetInt64(64),nil)) > 0 {
m = new(big.Int).Sub(m, n)
}
return m, nil
}
func crt(mp *big.Int, mq *big.Int, privKey *PrivateKey) *big.Int {
//u := new(big.Int)
//_,u = new(big.Int).QuoRem(new(big.Int).Mul(new(big.Int).Sub(mq, mp), privKey.pinvq), privKey.q,u)
//fmt.Println("u is ",u)
//m := new(big.Int)
//_,m = new(big.Int).QuoRem(new(big.Int).Add(mp, new(big.Int).Mul(u, privKey.p)),privKey.n,m)
u := new(big.Int).Mod(new(big.Int).Mul(new(big.Int).Sub(mq, mp), privKey.pinvq), privKey.q)
m := new(big.Int).Mod(new(big.Int).Add(mp, new(big.Int).Mul(u, privKey.p)),privKey.n)
return m
}
// AddCipher homomorphically adds together two cipher texts.
// To do this we multiply the two cipher texts, upon decryption, the resulting
// plain text will be the sum of the corresponding plain texts.
func AddCipher(pubKey *PublicKey, cipher1, cipher2 []byte) []byte {
x := new(big.Int).SetBytes(cipher1)
y := new(big.Int).SetBytes(cipher2)
// x * y mod n^2
return new(big.Int).Mod(
new(big.Int).Mul(x, y),
pubKey.NSquared,
).Bytes()
}
func AddCipherNumber(pubKey *PublicKey, x, y *big.Int) *big.Int {
// x * y mod n^2
return new(big.Int).Mod(
new(big.Int).Mul(x, y),
pubKey.NSquared,
)
}
// Add homomorphically adds a passed constant to the encrypted integer
// (our cipher text). We do this by multiplying the constant with our
// ciphertext. Upon decryption, the resulting plain text will be the sum of
// the plaintext integer and the constant.
func Add(pubKey *PublicKey, cipher, constant []byte) []byte {
c := new(big.Int).SetBytes(cipher)
x := new(big.Int).SetBytes(constant)
// c * g ^ x mod n^2
return new(big.Int).Mod(
new(big.Int).Mul(c, new(big.Int).Exp(pubKey.G, x, pubKey.NSquared)),
pubKey.NSquared,
).Bytes()
}
// Mul homomorphically multiplies an encrypted integer (cipher text) by a
// constant. We do this by raising our cipher text to the power of the passed
// constant. Upon decryption, the resulting plain text will be the product of
// the plaintext integer and the constant.
func Mul(pubKey *PublicKey, cipher []byte, constant []byte) []byte {
c := new(big.Int).SetBytes(cipher)
x := new(big.Int).SetBytes(constant)
// c ^ x mod n^2
return new(big.Int).Exp(c, x, pubKey.NSquared).Bytes()
}
func MulNumber(pubKey *PublicKey, c *big.Int, x *big.Int) *big.Int {
// c ^ x mod n^2
return new(big.Int).Exp(c, x, pubKey.NSquared)
}