-
Notifications
You must be signed in to change notification settings - Fork 4
/
factorize.go
55 lines (51 loc) · 877 Bytes
/
factorize.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
// PQ factorization derived from org.telegram.mtproto.secure.pq.PQLopatin of github.com/ex3ndr/telegram-mt
package factorize
import (
"math/rand"
)
func Lopatin(what uint64, r *rand.Rand) (uint64, uint64) {
var it int = 0
for i := uint(0); i < 3; i++ {
q := uint64((r.Intn(128) & 15) + 17)
x := uint64(r.Intn(1000000000) + 1)
y := x
lim := 1 << (i + 18)
for j := 1; j < lim; j++ {
it++
a, b, c := x, x, q
for b != 0 {
if (b & 1) != 0 {
c += a
if c >= what {
c -= what
}
}
a += a
if a >= what {
a -= what
}
b = b >> 1
}
x = c
var z uint64
if x < y {
z = y - x
} else {
z = x - y
}
g := gcd(z, what)
if g != 1 {
p := what / g
if p <= g {
return p, g
} else {
return g, p
}
}
if (j & (j - 1)) == 0 {
y = x
}
}
}
return 1, what
}