-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgroup_test.go
142 lines (116 loc) · 2.96 KB
/
group_test.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
package algebra
import (
"math/big"
"math/rand"
"testing"
"time"
)
func setupGroup(p *big.Int, n int) (*Group, []*GroupElement) {
rand.Seed(time.Now().UnixNano())
field := NewField(p)
g := findRandomGenerator(field)
group := NewGroup(field, g)
elements := make([]*GroupElement, n)
for i := 0; i < n; i++ {
val := big.NewInt(rand.Int63())
sign := rand.Int() % 2
if sign == 0 {
val.Sub(big.NewInt(0), val)
}
elements[i] = group.NewElement(val)
}
return group, elements
}
func TestAddGroup(t *testing.T) {
n := 100
p := big.NewInt(1523) // 1523 is a safe prime
group, elements := setupGroup(p, n)
sumInt := group.Field.MulIdentity()
sum := group.Identity()
for i := 0; i < n; i++ {
sum = group.Mul(sum, elements[i])
sumInt = group.Field.Mul(sumInt, elements[i].Value)
}
if sum.Value.Int.Cmp(big.NewInt(0)) == 0 {
t.Fatalf("Group element should never be zero!")
}
if sum.Value.Int.Cmp(sumInt.Int) != 0 {
t.Fatalf("Sum over group is not correct. expected: %v, got: %v", sumInt, sum.Value.Int)
}
}
func TestInverseGroup(t *testing.T) {
n := 100
p := big.NewInt(1523) // 1523 is a safe prime
group, elements := setupGroup(p, n)
sum := group.Identity()
sumInv := group.Identity()
for i := 0; i < n; i++ {
sum = group.Mul(sum, elements[i])
sumInv = group.Mul(sumInv, group.MulInv(elements[i]))
}
test := group.Mul(sum, sumInv)
if test.Value.Int.Cmp(big.NewInt(0)) == 0 {
t.Fatalf("Group element should never be zero!")
}
if test.Cmp(group.Identity()) != 0 {
t.Fatalf("Multiplicative inverse is incorrect!")
}
}
// Get all prime factors of a given number n
// taken from https://siongui.github.io/2017/05/09/go-find-all-prime-factors-of-integer-number/
func PrimeFactors(n *big.Int) []*big.Int {
pfs := make([]*big.Int, 0)
two := big.NewInt(2)
zero := big.NewInt(0)
// Get the number of 2s that divide n
for new(big.Int).Mod(n, two).Cmp(zero) == 0 {
pfs = append(pfs, two)
n.Div(n, two)
}
// n must be odd at this point. so we can skip one element
// (note i = i + 2)
i := big.NewInt(3)
for {
// while i divides n, append i and divide n
for big.NewInt(0).Mod(n, i).Cmp(zero) == 0 {
pfs = append(pfs, big.NewInt(0).SetBytes(i.Bytes()))
n = n.Div(n, i)
}
test := big.NewInt(0)
if test.Mul(i, i).Cmp(n) <= 0 {
i.Add(i, two)
} else {
break
}
}
// This condition is to handle the case when n is a prime number
// greater than 2
if n.Cmp(two) > 0 {
pfs = append(pfs, n)
}
return pfs
}
func findRandomGenerator(field *Field) *FieldElement {
found := false
one := big.NewInt(1)
factors := PrimeFactors(field.Pminus1())
g := randomInt(field.P)
for {
// test if g is a generator
for i := 0; i < len(factors); i++ {
pow := new(big.Int).Div(field.Pminus1(), factors[i])
if new(big.Int).Exp(g, pow, field.P).Cmp(one) == 0 {
break
}
if i+1 == len(factors) {
found = true
}
}
if found {
break
}
// try a new candidate
g = randomInt(field.P)
}
return field.NewElement(g)
}