forked from Roasbeef/go-go-gadget-paillier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paillier.go
154 lines (129 loc) · 3.95 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
package paillier
import (
"crypto/rand"
"errors"
"io"
"math/big"
)
var one = big.NewInt(1)
// ErrMessageTooLong is returned when attempting to encrypt a message which is
// too large for the size of the public key.
var ErrMessageTooLong = errors.New("paillier: message too long for Paillier public key size")
// 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) {
p, err := rand.Prime(random, bits/2)
if err != nil {
return nil, err
}
q, err := rand.Prime(random, bits/2)
if err != nil {
return nil, err
}
// n = p * q
n := new(big.Int).Mul(p, q)
// l = phi(n) = (p-1) * q(-1)
l := new(big.Int).Mul(
new(big.Int).Sub(p, one),
new(big.Int).Sub(q, one),
)
return &PrivateKey{
PublicKey: PublicKey{
N: n,
NSquared: new(big.Int).Mul(n, n),
G: new(big.Int).Add(n, one), // g = n + 1
},
L: l,
U: new(big.Int).ModInverse(l, n),
}, nil
}
// PrivateKey represents a Paillier key.
type PrivateKey struct {
PublicKey
L *big.Int // phi(n), (p-1)*(q-1)
U *big.Int // l^-1 mod n
}
// 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
}
// 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) {
r, err := rand.Int(rand.Reader, pubKey.N)
if err != nil {
return nil, err
}
m := new(big.Int).SetBytes(plainText)
if pubKey.N.Cmp(m) < 1 { // N < m
return nil, ErrMessageTooLong
}
// c = g^m * r^n mod n^2
n := pubKey.N
c := new(big.Int).Mod(
new(big.Int).Mul(
new(big.Int).Exp(pubKey.G, m, pubKey.NSquared),
new(big.Int).Exp(r, n, pubKey.NSquared),
),
pubKey.NSquared,
)
return c.Bytes(), 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, ErrMessageTooLong
}
// c^l mod n^2
a := new(big.Int).Exp(c, privKey.L, privKey.NSquared)
// L(a)
// (a - 1) / n
l := new(big.Int).Div(
new(big.Int).Sub(a, one),
privKey.N,
)
// m = L(c^l mod n^2) * u mod n
m := new(big.Int).Mod(
new(big.Int).Mul(l, privKey.U),
privKey.N,
)
return m.Bytes(), nil
}
// 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()
}
// 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()
}