-
Notifications
You must be signed in to change notification settings - Fork 152
/
point.go
411 lines (341 loc) · 9.25 KB
/
point.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright 2020 ConsenSys Software Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Code generated by consensys/gnark-crypto DO NOT EDIT
package twistededwards
import (
"crypto/subtle"
"io"
"math/big"
"math/bits"
"github.com/consensys/gnark-crypto/ecc/bls12-377/fr"
)
// PointAffine point on a twisted Edwards curve
type PointAffine struct {
X, Y fr.Element
}
// PointProj point in projective coordinates
type PointProj struct {
X, Y, Z fr.Element
}
const (
//following https://tools.ietf.org/html/rfc8032#section-3.1,
// an fr element x is negative if its binary encoding is
// lexicographically larger than -x.
mCompressedNegative = 0x80
mCompressedPositive = 0x00
mUnmask = 0x7f
// size in byte of a compressed point (point.Y --> fr.Element)
sizePointCompressed = fr.Limbs * 8
)
// Bytes returns the compressed point as a byte array
// Follows https://tools.ietf.org/html/rfc8032#section-3.1,
// as the twisted Edwards implementation is primarily used
// for eddsa.
func (p *PointAffine) Bytes() [sizePointCompressed]byte {
var res [sizePointCompressed]byte
var mask uint
y := p.Y.Bytes()
if p.X.LexicographicallyLargest() {
mask = mCompressedNegative
} else {
mask = mCompressedPositive
}
// p.Y must be in little endian
y[0] |= byte(mask) // msb of y
for i, j := 0, sizePointCompressed-1; i < j; i, j = i+1, j-1 {
y[i], y[j] = y[j], y[i]
}
subtle.ConstantTimeCopy(1, res[:], y[:])
return res
}
// Marshal converts p to a byte slice
func (p *PointAffine) Marshal() []byte {
b := p.Bytes()
return b[:]
}
func computeX(y *fr.Element) (x fr.Element) {
var one, num, den fr.Element
one.SetOne()
num.Square(y)
den.Mul(&num, &edwards.D)
num.Sub(&one, &num)
den.Sub(&edwards.A, &den)
x.Div(&num, &den)
x.Sqrt(&x)
return
}
// SetBytes sets p from buf
// len(buf) >= sizePointCompressed
// buf contains the Y coordinate masked with a parity bit to recompute the X coordinate
// from the curve equation. See Bytes() and https://tools.ietf.org/html/rfc8032#section-3.1
// Returns the number of read bytes and an error if the buffer is too short.
func (p *PointAffine) SetBytes(buf []byte) (int, error) {
if len(buf) < sizePointCompressed {
return 0, io.ErrShortBuffer
}
bufCopy := make([]byte, sizePointCompressed)
subtle.ConstantTimeCopy(1, bufCopy, buf[:sizePointCompressed])
for i, j := 0, sizePointCompressed-1; i < j; i, j = i+1, j-1 {
bufCopy[i], bufCopy[j] = bufCopy[j], bufCopy[i]
}
isLexicographicallyLargest := (mCompressedNegative&bufCopy[0])>>7 == 1
bufCopy[0] &= mUnmask
p.Y.SetBytes(bufCopy)
p.X = computeX(&p.Y)
if isLexicographicallyLargest {
if !p.X.LexicographicallyLargest() {
p.X.Neg(&p.X)
}
} else {
if p.X.LexicographicallyLargest() {
p.X.Neg(&p.X)
}
}
return sizePointCompressed, nil
}
// Unmarshal alias to SetBytes()
func (p *PointAffine) Unmarshal(b []byte) error {
_, err := p.SetBytes(b)
return err
}
// Set sets p to p1 and return it
func (p *PointProj) Set(p1 *PointProj) *PointProj {
p.X.Set(&p1.X)
p.Y.Set(&p1.Y)
p.Z.Set(&p1.Z)
return p
}
// Set sets p to p1 and return it
func (p *PointAffine) Set(p1 *PointAffine) *PointAffine {
p.X.Set(&p1.X)
p.Y.Set(&p1.Y)
return p
}
// Equal returns true if p=p1 false otherwise
func (p *PointAffine) Equal(p1 *PointAffine) bool {
return p.X.Equal(&p1.X) && p.Y.Equal(&p1.Y)
}
// Equal returns true if p=p1 false otherwise
// If one point is on the affine chart Z=0 it returns false
func (p *PointProj) Equal(p1 *PointProj) bool {
if p.Z.IsZero() || p1.Z.IsZero() {
return false
}
var pAffine, p1Affine PointAffine
pAffine.FromProj(p)
p1Affine.FromProj(p1)
return pAffine.Equal(&p1Affine)
}
// NewPointAffine creates a new instance of PointAffine
func NewPointAffine(x, y fr.Element) PointAffine {
return PointAffine{x, y}
}
// IsOnCurve checks if a point is on the twisted Edwards curve
func (p *PointAffine) IsOnCurve() bool {
ecurve := GetEdwardsCurve()
var lhs, rhs, tmp fr.Element
tmp.Mul(&p.Y, &p.Y)
lhs.Mul(&p.X, &p.X)
mulByA(&lhs)
lhs.Add(&lhs, &tmp)
tmp.Mul(&p.X, &p.X).
Mul(&tmp, &p.Y).
Mul(&tmp, &p.Y).
Mul(&tmp, &ecurve.D)
rhs.SetOne().Add(&rhs, &tmp)
return lhs.Equal(&rhs)
}
// Add adds two points (x,y), (u,v) on a twisted Edwards curve with parameters a, d
// modifies p
func (p *PointAffine) Add(p1, p2 *PointAffine) *PointAffine {
ecurve := GetEdwardsCurve()
var xu, yv, xv, yu, dxyuv, one, denx, deny fr.Element
pRes := new(PointAffine)
xv.Mul(&p1.X, &p2.Y)
yu.Mul(&p1.Y, &p2.X)
pRes.X.Add(&xv, &yu)
xu.Mul(&p1.X, &p2.X)
mulByA(&xu)
yv.Mul(&p1.Y, &p2.Y)
pRes.Y.Sub(&yv, &xu)
dxyuv.Mul(&xv, &yu).Mul(&dxyuv, &ecurve.D)
one.SetOne()
denx.Add(&one, &dxyuv)
deny.Sub(&one, &dxyuv)
p.X.Div(&pRes.X, &denx)
p.Y.Div(&pRes.Y, &deny)
return p
}
// Double doubles point (x,y) on a twisted Edwards curve with parameters a, d
// modifies p
func (p *PointAffine) Double(p1 *PointAffine) *PointAffine {
p.Set(p1)
var xx, yy, xy, denum, two fr.Element
xx.Square(&p.X)
yy.Square(&p.Y)
xy.Mul(&p.X, &p.Y)
mulByA(&xx)
denum.Add(&xx, &yy)
p.X.Double(&xy).Div(&p.X, &denum)
two.SetOne().Double(&two)
denum.Neg(&denum).Add(&denum, &two)
p.Y.Sub(&yy, &xx).Div(&p.Y, &denum)
return p
}
// Neg negates point (x,y) on a twisted Edwards curve with parameters a, d
// modifies p
func (p *PointProj) Neg(p1 *PointProj) *PointProj {
p.Set(p1)
p.X.Neg(&p.X)
return p
}
// FromProj sets p in affine from p in projective
func (p *PointAffine) FromProj(p1 *PointProj) *PointAffine {
p.X.Div(&p1.X, &p1.Z)
p.Y.Div(&p1.Y, &p1.Z)
return p
}
// FromAffine sets p in projective from p in affine
func (p *PointProj) FromAffine(p1 *PointAffine) *PointProj {
p.X.Set(&p1.X)
p.Y.Set(&p1.Y)
p.Z.SetOne()
return p
}
// Add adds points in projective coordinates
// cf https://hyperelliptic.org/EFD/g1p/auto-twisted-projective.html#addition-add-2008-bbjlp
func (p *PointProj) Add(p1, p2 *PointProj) *PointProj {
var res PointProj
ecurve := GetEdwardsCurve()
var A, B, C, D, E, F, G, H, I fr.Element
A.Mul(&p1.Z, &p2.Z)
B.Square(&A)
C.Mul(&p1.X, &p2.X)
D.Mul(&p1.Y, &p2.Y)
E.Mul(&ecurve.D, &C).Mul(&E, &D)
F.Sub(&B, &E)
G.Add(&B, &E)
H.Add(&p1.X, &p1.Y)
I.Add(&p2.X, &p2.Y)
res.X.Mul(&H, &I).
Sub(&res.X, &C).
Sub(&res.X, &D).
Mul(&res.X, &A).
Mul(&res.X, &F)
mulByA(&C)
C.Neg(&C)
res.Y.Add(&D, &C).
Mul(&res.Y, &A).
Mul(&res.Y, &G)
res.Z.Mul(&F, &G)
p.Set(&res)
return p
}
// MixedAdd adds a point in projective to a point in affine coordinates
// cf https://hyperelliptic.org/EFD/g1p/auto-twisted-projective.html#addition-madd-2008-bbjlp
func (p *PointProj) MixedAdd(p1 *PointProj, p2 *PointAffine) *PointProj {
var res PointProj
ecurve := GetEdwardsCurve()
var B, C, D, E, F, G, H, I fr.Element
B.Square(&p1.Z)
C.Mul(&p1.X, &p2.X)
D.Mul(&p1.Y, &p2.Y)
E.Mul(&ecurve.D, &C).Mul(&E, &D)
F.Sub(&B, &E)
G.Add(&B, &E)
H.Add(&p1.X, &p1.Y)
I.Add(&p2.X, &p2.Y)
res.X.Mul(&H, &I).
Sub(&res.X, &C).
Sub(&res.X, &D).
Mul(&res.X, &p1.Z).
Mul(&res.X, &F)
mulByA(&C)
res.Y.Sub(&D, &C).
Mul(&res.Y, &p1.Z).
Mul(&res.Y, &G)
res.Z.Mul(&F, &G)
p.Set(&res)
return p
}
// Double adds points in projective coordinates
// cf https://hyperelliptic.org/EFD/g1p/auto-twisted-projective.html#doubling-dbl-2008-bbjlp
func (p *PointProj) Double(p1 *PointProj) *PointProj {
var res PointProj
var B, C, D, E, F, H, J fr.Element
B.Add(&p1.X, &p1.Y).Square(&B)
C.Square(&p1.X)
D.Square(&p1.Y)
E.Set(&C)
mulByA(&E)
F.Add(&E, &D)
H.Square(&p1.Z)
J.Sub(&F, &H).Sub(&J, &H)
res.X.Sub(&B, &C).
Sub(&res.X, &D).
Mul(&res.X, &J)
res.Y.Sub(&E, &D).Mul(&res.Y, &F)
res.Z.Mul(&F, &J)
p.Set(&res)
return p
}
// Neg sets p to -p1 and returns it
func (p *PointAffine) Neg(p1 *PointAffine) *PointAffine {
p.Set(p1)
p.X.Neg(&p.X)
return p
}
// setInfinity sets p to O (0:1:1)
func (p *PointProj) setInfinity() *PointProj {
p.X.SetZero()
p.Y.SetOne()
p.Z.SetOne()
return p
}
// ScalarMul scalar multiplication of a point
// p1 in projective coordinates with a scalar in big.Int
func (p *PointProj) ScalarMul(p1 *PointProj, scalar *big.Int) *PointProj {
var _scalar big.Int
_scalar.Set(scalar)
p.Set(p1)
if _scalar.Sign() == -1 {
_scalar.Neg(&_scalar)
p.Neg(p)
}
var resProj PointProj
resProj.setInfinity()
const wordSize = bits.UintSize
sWords := _scalar.Bits()
for i := len(sWords) - 1; i >= 0; i-- {
ithWord := sWords[i]
for k := 0; k < wordSize; k++ {
resProj.Double(&resProj)
kthBit := (ithWord >> (wordSize - 1 - k)) & 1
if kthBit == 1 {
resProj.Add(&resProj, p)
}
}
}
p.Set(&resProj)
return p
}
// ScalarMul scalar multiplication of a point
// p1 in affine coordinates with a scalar in big.Int
func (p *PointAffine) ScalarMul(p1 *PointAffine, scalar *big.Int) *PointAffine {
var p1Proj, resProj PointProj
p1Proj.FromAffine(p1)
resProj.ScalarMul(&p1Proj, scalar)
p.FromProj(&resProj)
return p
}