-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrypto.go
66 lines (55 loc) · 1.44 KB
/
crypto.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
package paillier
import (
"crypto/rand"
"math/big"
)
// PublicKey is the key that may be shared
type PublicKey struct {
Length int
N *big.Int
NSq *big.Int
G *big.Int
}
// PrivateKey must be kept secret
type PrivateKey struct {
Length int
PublicKey *PublicKey
L *big.Int
U *big.Int
Threshold *big.Int
}
// NewKeyPair generates a new public and private key. The key length must be large enough to encrypt the message. The
// threshold should be greater than the maximum integer that will be encrypted.
func NewKeyPair(keyLength, threshold int) (*PublicKey, *PrivateKey, error) {
p1, err := rand.Prime(rand.Reader, keyLength)
if err != nil {
return nil, nil, err
}
p2, err := rand.Prime(rand.Reader, keyLength)
if err != nil {
return nil, nil, err
}
n := new(big.Int).Mul(p1, p2)
publicKey := &PublicKey{
Length: keyLength,
N: n,
NSq: new(big.Int).Mul(n, n),
G: new(big.Int).Add(n, one),
}
// (prime1 - 1) * (prime2 - 1)
l := new(big.Int).Mul(p1.Sub(p1, one), p2.Sub(p2, one))
privateKey := &PrivateKey{
Length: keyLength,
PublicKey: publicKey,
L: l,
U: new(big.Int).ModInverse(l, n),
Threshold: big.NewInt(int64(threshold)),
}
return publicKey, privateKey, nil
}
func requirePublicKeysEqual(x, y *PublicKey) {
if x.Length == y.Length && x.N.Cmp(y.N) == 0 && x.NSq.Cmp(y.NSq) == 0 && x.G.Cmp(y.G) == 0 {
return
}
panic("public keys not equal")
}