@@ -16,6 +16,7 @@ var Generator = Element{inner: bandersnatch.PointProj{
16
16
Y : bandersnatch .GetEdwardsCurve ().Base .Y ,
17
17
Z : fp .One (),
18
18
}}
19
+
19
20
var Identity = Element {inner : bandersnatch.PointProj {
20
21
X : fp .Zero (),
21
22
Y : fp .One (),
@@ -34,7 +35,7 @@ func (p Element) Bytes() [sizePointCompressed]byte {
34
35
affine_representation .FromProj (& p .inner )
35
36
36
37
// Serialisation takes the x co-ordinate and multiplies it by the sign of y
37
- var x = affine_representation .X
38
+ x : = affine_representation .X
38
39
if ! affine_representation .Y .LexicographicallyLargest () {
39
40
x .Neg (& x )
40
41
}
@@ -44,15 +45,15 @@ func (p Element) Bytes() [sizePointCompressed]byte {
44
45
// Serialises multiple group elements using a batch multi inversion
45
46
func ElementsToBytes (elements []* Element ) [][sizePointCompressed ]byte {
46
47
// Collect all z co-ordinates
47
- var zs []fp.Element
48
+ zs := make ( []fp.Element , len ( elements ))
48
49
for i := 0 ; i < int (len (elements )); i ++ {
49
- zs = append ( zs , elements [i ].inner .Z )
50
+ zs [ i ] = elements [i ].inner .Z
50
51
}
51
52
52
53
// Invert z co-ordinates
53
54
zInvs := fp .BatchInvert (zs )
54
55
55
- var serialised_points [][sizePointCompressed ]byte
56
+ serialised_points := make ( [][sizePointCompressed ]byte , len ( elements ))
56
57
57
58
// Multiply x and y by zInv
58
59
for i := 0 ; i < int (len (elements )); i ++ {
@@ -69,11 +70,10 @@ func ElementsToBytes(elements []*Element) [][sizePointCompressed]byte {
69
70
X .Neg (& X )
70
71
}
71
72
72
- serialised_points = append ( serialised_points , X .Bytes () )
73
+ serialised_points [ i ] = X .Bytes ()
73
74
}
74
75
75
76
return serialised_points
76
-
77
77
}
78
78
79
79
func (p * Element ) setBytes (buf []byte , trusted bool ) error {
@@ -116,49 +116,41 @@ func (p *Element) SetBytesTrusted(buf []byte) error {
116
116
117
117
// computes X/Y
118
118
func (p Element ) mapToBaseField () fp.Element {
119
-
120
119
var res fp.Element
121
120
res .Div (& p .inner .X , & p .inner .Y )
122
121
return res
123
122
}
124
123
125
- func (p Element ) MapToScalarField () fr.Element {
124
+ func (p Element ) MapToScalarField (res * fr.Element ) {
126
125
basefield := p .mapToBaseField ()
127
126
baseFieldBytes := basefield .BytesLE ()
128
127
129
- var res fr.Element
130
128
res .SetBytesLE (baseFieldBytes [:])
131
-
132
- return res
133
129
}
134
130
135
131
// Maps each point to a field element in the scalar field
136
- func MultiMapToScalarField (elements []* Element ) []fr.Element {
132
+ func MultiMapToScalarField (result []* fr.Element , elements []* Element ) {
133
+ if len (result ) != len (elements ) {
134
+ panic ("MultiMapToScalarField expects the result slice to be the same length of elements" )
135
+ }
136
+
137
137
// Collect all y co-ordinates
138
- var ys []fp.Element
138
+ ys := make ( []fp.Element , len ( elements ))
139
139
for i := 0 ; i < int (len (elements )); i ++ {
140
- ys = append ( ys , elements [i ].inner .Y )
140
+ ys [ i ] = elements [i ].inner .Y
141
141
}
142
142
143
143
// Invert y co-ordinates
144
144
yInvs := fp .BatchInvert (ys )
145
145
146
- var scalars []fr.Element
147
-
148
146
// Multiply x by yInv
149
147
for i := 0 ; i < int (len (elements )); i ++ {
150
148
var mappedElement fp.Element
151
149
152
150
mappedElement .Mul (& elements [i ].inner .X , & yInvs [i ])
153
151
byts := mappedElement .BytesLE ()
154
-
155
- var res fr.Element
156
- res .SetBytesLE (byts [:])
157
- scalars = append (scalars , res )
152
+ result [i ].SetBytesLE (byts [:])
158
153
}
159
-
160
- return scalars
161
-
162
154
}
163
155
164
156
// TODO: change this to not use pointers
@@ -191,7 +183,7 @@ func (p *Element) Equal(other *Element) bool {
191
183
func subgroup_check (x fp.Element ) error {
192
184
var res , one , ax_sq fp.Element
193
185
one .SetOne ()
194
- var A = bandersnatch .GetEdwardsCurve ().A
186
+ A : = bandersnatch .GetEdwardsCurve ().A
195
187
196
188
// 1 - ax^2
197
189
ax_sq .Square (& x )
@@ -209,24 +201,27 @@ func (p *Element) Identity() *Element {
209
201
* p = Identity
210
202
return p
211
203
}
204
+
212
205
func (p * Element ) Double (p1 * Element ) * Element {
213
206
p .inner .Double (& p1 .inner )
214
207
return p
215
208
}
209
+
216
210
func (p * Element ) Add (p1 , p2 * Element ) * Element {
217
211
p .inner .Add (& p1 .inner , & p2 .inner )
218
212
return p
219
213
}
214
+
220
215
func (p * Element ) AddMixed (p1 * Element , p2 bandersnatch.PointAffine ) * Element {
221
216
p .inner .MixedAdd (& p1 .inner , & p2 )
222
217
return p
223
218
}
219
+
224
220
func (p * Element ) Sub (p1 , p2 * Element ) * Element {
225
221
var neg_p2 Element
226
222
neg_p2 .Neg (p2 )
227
223
228
224
return p .Add (p1 , & neg_p2 )
229
-
230
225
}
231
226
232
227
func (p * Element ) IsOnCurve () bool {
@@ -244,6 +239,7 @@ func (p *Element) Normalise() {
244
239
p .inner .Y .Set (& point_aff .Y )
245
240
p .inner .Z .SetOne ()
246
241
}
242
+
247
243
func (p * Element ) Set (p1 * Element ) * Element {
248
244
p .inner .X .Set (& p1 .inner .X )
249
245
p .inner .Y .Set (& p1 .inner .Y )
@@ -255,6 +251,7 @@ func (p *Element) Neg(p1 *Element) *Element {
255
251
p .inner .Neg (& p1 .inner )
256
252
return p
257
253
}
254
+
258
255
func (p * Element ) ScalarMul (p1 * Element , scalar_mont * fr.Element ) * Element {
259
256
p .inner .ScalarMul (& p1 .inner , scalar_mont )
260
257
return p
@@ -269,7 +266,6 @@ func (p *Element) ScalarMul(p1 *Element, scalar_mont *fr.Element) *Element {
269
266
//
270
267
// we could increase storage by 2x and save CPU time by serialising the projective point
271
268
func UnsafeReadUncompressedPoint (r io.Reader ) * Element {
272
-
273
269
affine_point := bandersnatch .ReadUncompressedPoint (r )
274
270
var proj_repr bandersnatch.PointProj
275
271
proj_repr .FromAffine (& affine_point )
@@ -281,7 +277,6 @@ func UnsafeReadUncompressedPoint(r io.Reader) *Element {
281
277
282
278
// Writes an uncompressed affine point to an io.Writer
283
279
func (element * Element ) UnsafeWriteUncompressedPoint (w io.Writer ) (int , error ) {
284
-
285
280
// Convert underlying point to affine representation
286
281
var p bandersnatch.PointAffine
287
282
p .FromProj (& element .inner )
0 commit comments