-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
67 lines (57 loc) · 1.37 KB
/
params.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
package makwa
import (
"hash"
"math/big"
"crypto/rand"
"crypto/sha256"
)
// PublicParameters are the public parameters associated with Makwa.
type PublicParameters struct {
N *big.Int
Hash func() hash.Hash
}
// ModulusID returns a fingerprint of the modulus.
func (p PublicParameters) ModulusID() []byte {
return kdf(p.Hash, p.N.Bytes(), 8)
}
// PrivateParameters are the private parameters associated with Makwa.
type PrivateParameters struct {
PublicParameters
P, Q *big.Int
}
// GenerateParameters generates a random Makwa modulus of the given size.
func GenerateParameters(bits int) (*PrivateParameters, error) {
pBits := (bits + 1) >> 1
qBits := bits - pBits
p, err := genPrime(pBits)
if err != nil {
return nil, err
}
q, err := genPrime(qBits)
if err != nil {
return nil, err
}
return &PrivateParameters{
PublicParameters: PublicParameters{
N: new(big.Int).Mul(p, q),
Hash: sha256.New,
},
P: p,
Q: q,
}, nil
}
var (
three = big.NewInt(3)
four = big.NewInt(4)
)
func genPrime(bits int) (*big.Int, error) {
for {
n, err := rand.Prime(rand.Reader, bits)
if err != nil {
return nil, err
}
if new(big.Int).Mod(n, four).Cmp(three) == 0 {
return n, nil
}
}
}