-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgroup.go
67 lines (53 loc) · 1.58 KB
/
group.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 algebra
import (
"math/big"
)
type Group struct {
Field *Field
G *FieldElement
}
type GroupElement struct {
Value *FieldElement
}
// new group over a specified field with generator g
func NewGroup(f *Field, g *FieldElement) *Group {
return &Group{f, g}
}
// multiply two group elements
func (g *Group) Mul(a, b *GroupElement) *GroupElement {
newElement := g.Field.Mul(a.Value, b.Value)
return &GroupElement{newElement}
}
// return multiplicative inverse of the element
func (g *Group) MulInv(a *GroupElement) *GroupElement {
newElement := g.Field.MulInv(a.Value)
return &GroupElement{newElement}
}
// exponentiate a group element by a scalar
func (g *Group) Exp(a *GroupElement, b *FieldElement) *GroupElement {
newElement := g.Field.Exp(a.Value, b.Int)
return &GroupElement{newElement}
}
// new element g**alpha mod P = 2q+1
func (g *Group) NewElement(a *big.Int) *GroupElement {
newElement := g.Field.Exp(g.G, a)
return &GroupElement{newElement}
}
// new random element in the group (also returns discrete log)
func (g *Group) RandomElement() (*GroupElement, *big.Int) {
// should make it not repeat this calculation
a := randomInt(g.Field.P)
return g.NewElement(a), a
}
func (g *Group) Identity() *GroupElement {
return g.NewElement(big.NewInt(0)) // g^0 = 1
}
func (elem *GroupElement) Cmp(b *GroupElement) int {
return elem.Value.Cmp(b.Value)
}
func (elem *GroupElement) Copy() *GroupElement {
return &GroupElement{&FieldElement{big.NewInt(0).SetBytes(elem.Value.Int.Bytes())}}
}
func (f *Field) Pminus1() *big.Int {
return new(big.Int).Sub(f.P, big.NewInt(1))
}