-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointG1.go
197 lines (152 loc) · 3.77 KB
/
pointG1.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package mcl
import (
"runtime"
crypto "github.com/ThotaGopichandThota/gn-crypto1"
"github.com/ThotaGopichandThota/gn-core3/core/check"
"github.com/herumi/bls-go-binary/bls"
)
// PointG1 -
type PointG1 struct {
*bls.G1
}
// NewPointG1 creates a new point on G1 initialized with base point
func NewPointG1() *PointG1 {
point := &PointG1{
G1: &bls.G1{},
}
bpG1Str := baseG1()
err := point.G1.SetString(bpG1Str, 10)
if err != nil {
panic(err.Error())
}
return point
}
// Equal tests if receiver is equal with the Point p given as parameter.
// Both Points need to be derived from the same Group
func (po *PointG1) Equal(p crypto.Point) (bool, error) {
if p == nil {
return false, crypto.ErrNilParam
}
po2, ok := p.(*PointG1)
if !ok {
return false, crypto.ErrInvalidParam
}
return po.G1.IsEqual(po2.G1), nil
}
// Clone returns a clone of the receiver.
func (po *PointG1) Clone() crypto.Point {
po2 := PointG1{
G1: &bls.G1{},
}
strPo := po.G1.GetString(16)
err := po2.G1.SetString(strPo, 16)
if err != nil {
log.Error("PointG1 Clone", "error", err.Error())
}
return &po2
}
// Null returns the neutral identity element.
func (po *PointG1) Null() crypto.Point {
p := &PointG1{
G1: &bls.G1{},
}
p.G1.Clear()
return p
}
// Set sets the receiver equal to another Point p.
func (po *PointG1) Set(p crypto.Point) error {
if check.IfNil(p) {
return crypto.ErrNilParam
}
po1, ok := p.(*PointG1)
if !ok {
return crypto.ErrInvalidParam
}
strPo := po1.G1.GetString(16)
return po.G1.SetString(strPo, 16)
}
// Add returns the result of adding receiver with Point p given as parameter,
// so that their scalars add homomorphically
func (po *PointG1) Add(p crypto.Point) (crypto.Point, error) {
if check.IfNil(p) {
return nil, crypto.ErrNilParam
}
po1, ok := p.(*PointG1)
if !ok {
return nil, crypto.ErrInvalidParam
}
po2 := PointG1{
G1: &bls.G1{},
}
bls.G1Add(po2.G1, po.G1, po1.G1)
runtime.KeepAlive(po1)
return &po2, nil
}
// Sub returns the result of subtracting from receiver the Point p given as parameter,
// so that their scalars subtract homomorphically
func (po *PointG1) Sub(p crypto.Point) (crypto.Point, error) {
if check.IfNil(p) {
return nil, crypto.ErrNilParam
}
po1, ok := p.(*PointG1)
if !ok {
return nil, crypto.ErrInvalidParam
}
po2 := PointG1{
G1: &bls.G1{},
}
bls.G1Sub(po2.G1, po.G1, po1.G1)
runtime.KeepAlive(po1)
return &po2, nil
}
// Neg returns the negation of receiver
func (po *PointG1) Neg() crypto.Point {
po2 := PointG1{
G1: &bls.G1{},
}
bls.G1Neg(po2.G1, po.G1)
return &po2
}
// Mul returns the result of multiplying receiver by the scalarInt s.
func (po *PointG1) Mul(s crypto.Scalar) (crypto.Point, error) {
if check.IfNil(s) {
return nil, crypto.ErrNilParam
}
po2 := PointG1{
G1: &bls.G1{},
}
s1, ok := s.(*Scalar)
if !ok {
return nil, crypto.ErrInvalidParam
}
bls.G1MulCT(po2.G1, po.G1, s1.Scalar)
runtime.KeepAlive(s1)
return &po2, nil
}
// Pick returns a new random or pseudo-random Point.
func (po *PointG1) Pick() (crypto.Point, error) {
scalar := &bls.Fr{}
scalar.SetByCSPRNG()
po2 := PointG1{
G1: &bls.G1{},
}
bls.G1MulCT(po2.G1, po.G1, scalar)
runtime.KeepAlive(scalar)
return &po2, nil
}
// GetUnderlyingObj returns the object the implementation wraps
func (po *PointG1) GetUnderlyingObj() interface{} {
return po.G1
}
// MarshalBinary converts the point into its byte array representation
func (po *PointG1) MarshalBinary() ([]byte, error) {
return po.G1.Serialize(), nil
}
// UnmarshalBinary reconstructs a point from its byte array representation
func (po *PointG1) UnmarshalBinary(point []byte) error {
return po.G1.Deserialize(point)
}
// IsInterfaceNil returns true if there is no value under the interface
func (po *PointG1) IsInterfaceNil() bool {
return po == nil
}