-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.go
45 lines (39 loc) · 846 Bytes
/
common.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
package batchgcd
import (
"fmt"
"github.com/ncw/gmp"
)
type Collision struct {
Modulus *gmp.Int
P *gmp.Int
Q *gmp.Int
}
func (x Collision) HavePrivate() bool {
return x.P != nil || x.Q != nil
}
func (x Collision) String() string {
if x.HavePrivate() {
if x.P.Cmp(x.Q) < 0 {
return fmt.Sprintf("COLLISION: N=%x P=%x Q=%x", x.Modulus, x.P, x.Q)
} else {
return fmt.Sprintf("COLLISION: N=%x P=%x Q=%x", x.Modulus, x.Q, x.P)
}
} else {
return fmt.Sprintf("DUPLICATE: %x", x.Modulus)
}
}
func (x Collision) Test() bool {
if !x.HavePrivate() {
return true
}
n := gmp.NewInt(0)
n.Mul(x.P, x.Q)
return n.Cmp(x.Modulus) == 0
}
func (x Collision) Csv() string {
if x.P.Cmp(x.Q) < 0 {
return fmt.Sprintf("%x,%x,%x", x.Modulus, x.P, x.Q)
} else {
return fmt.Sprintf("%x,%x,%x", x.Modulus, x.Q, x.P)
}
}