This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
170 lines (132 loc) · 2.97 KB
/
generator.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
package gobbs
import (
"crypto/rand"
"fmt"
"math/big"
)
// Generator defines a Blum-Blum-Shub generator
type Generator struct {
p *big.Int
q *big.Int
n *big.Int
seed *big.Int
x0 *big.Int
x1 *big.Int
bits int
}
// New returns a Generator with default config
func New() (*Generator, error) {
return NewWithConfig(DefaultConfig)
}
// NewWithConfig returns a Generator with given config
func NewWithConfig(config *Config) (*Generator, error) {
g := &Generator{
p: big.NewInt(0),
q: big.NewInt(0),
n: big.NewInt(0),
seed: big.NewInt(0),
x0: big.NewInt(0),
x1: big.NewInt(0),
bits: config.Bits,
}
// Handle config
if config.PrimeP != nil && config.PrimeQ != nil {
// @todo validate primes first
g.p.Set(config.PrimeP)
g.q.Set(config.PrimeQ)
g.n.Mul(g.p, g.q)
}
if config.Seed != nil {
g.seed.Set(config.Seed)
}
// Check values
if g.n.Cmp(bigZero) == 0 {
_, _, _, err := g.CalcBlumUnits()
if err != nil {
return g, err
}
}
if g.seed.Cmp(bigZero) == 0 {
_, err := g.CalcRandomSeed()
if err != nil {
return g, err
}
}
// x0 = (seed ^ 2) mod n
g.x0.Exp(g.seed, bigTwo, g.n)
return g, nil
}
func (g *Generator) Read(p []byte) (n int, err error) {
if l := len(p); l > 0 {
for n < l {
p[n] = g.readByte()
n++
}
}
return
}
// CalcBlumUnits calculates, sets to *g and returns two Blum primes (p) and (q), as well as their product, the Blum integer (n).
func (g *Generator) CalcBlumUnits() (*big.Int, *big.Int, *big.Int, error) {
var (
err error
tmp = big.NewInt(0)
)
g.p = big.NewInt(0)
g.q = big.NewInt(0)
g.n = big.NewInt(0)
for tmp.Mod(g.p, bigFour).Cmp(bigThree) != 0 {
g.p, err = rand.Prime(rand.Reader, g.bits)
if err != nil {
return g.p, g.q, g.n, err
}
}
for tmp.Mod(g.n, bigFour).Cmp(bigOne) != 0 || g.p.Cmp(g.q) == 0 {
g.q, err = rand.Prime(rand.Reader, g.bits)
if err != nil {
return g.p, g.q, g.n, err
}
g.n.Mul(g.p, g.q)
}
return g.p, g.q, g.n, nil
}
// CalcRandomSeed finds, sets to *g and returns a random large integer (x), that is relatively prime to (n).
func (g *Generator) CalcRandomSeed() (*big.Int, error) {
if g.n.Cmp(bigZero) == 0 {
return nil, fmt.Errorf("no valid blum integer (n) set")
}
var (
err error
r = big.NewInt(0)
maxInt = big.NewInt(int64(g.bits))
)
g.seed = big.NewInt(0)
// maxInt = 2^n - 1 (n=bits)
maxInt.Exp(bigTwo, maxInt, nil)
maxInt.Sub(maxInt, bigOne)
if maxInt.Cmp(bigZero) == 0 {
return g.seed, fmt.Errorf("error calculating maximum integer size")
}
for r.Cmp(bigOne) != 0 {
g.seed, err = rand.Int(rand.Reader, maxInt)
if err != nil {
return g.seed, err
}
r.GCD(nil, nil, g.n, g.seed)
}
return g.seed, err
}
func (g *Generator) readByte() byte {
var (
bitCounter uint
val uint
)
for bitCounter = 0; bitCounter < 8; bitCounter++ {
// x1 = (x0 ^ 2) mod n
g.x1.Exp(g.x0, bigTwo, g.n)
if g.x1.Bit(0) == 1 {
val |= 1 << bitCounter
}
g.x0.Set(g.x1)
}
return byte(val)
}