forked from dedis/kyber
/
ext.go
294 lines (260 loc) · 6.72 KB
/
ext.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
package curve25519
import (
"crypto/cipher"
"encoding/hex"
"io"
"math/big"
"github.com/michaljirman/kyber"
"github.com/michaljirman/kyber/group/internal/marshalling"
"github.com/michaljirman/kyber/group/mod"
)
type extPoint struct {
X, Y, Z, T mod.Int
c *ExtendedCurve
}
func (P *extPoint) initXY(x, y *big.Int, c kyber.Group) {
P.c = c.(*ExtendedCurve)
P.X.Init(x, &P.c.P)
P.Y.Init(y, &P.c.P)
P.Z.Init64(1, &P.c.P)
P.T.Mul(&P.X, &P.Y)
}
func (P *extPoint) getXY() (x, y *mod.Int) {
P.normalize()
return &P.X, &P.Y
}
func (P *extPoint) String() string {
P.normalize()
//return P.c.pointString(&P.X,&P.Y)
buf, _ := P.MarshalBinary()
return hex.EncodeToString(buf)
}
func (P *extPoint) MarshalSize() int {
return P.c.PointLen()
}
func (P *extPoint) MarshalBinary() ([]byte, error) {
P.normalize()
return P.c.encodePoint(&P.X, &P.Y), nil
}
func (P *extPoint) UnmarshalBinary(b []byte) error {
if err := P.c.decodePoint(b, &P.X, &P.Y); err != nil {
return err
}
P.Z.Init64(1, &P.c.P)
P.T.Mul(&P.X, &P.Y)
return nil
}
func (P *extPoint) MarshalTo(w io.Writer) (int, error) {
return marshalling.PointMarshalTo(P, w)
}
func (P *extPoint) UnmarshalFrom(r io.Reader) (int, error) {
return marshalling.PointUnmarshalFrom(P, r)
}
// Equality test for two Points on the same curve.
// We can avoid inversions here because:
//
// (X1/Z1,Y1/Z1) == (X2/Z2,Y2/Z2)
// iff
// (X1*Z2,Y1*Z2) == (X2*Z1,Y2*Z1)
//
func (P *extPoint) Equal(CP2 kyber.Point) bool {
P2 := CP2.(*extPoint)
var t1, t2 mod.Int
xeq := t1.Mul(&P.X, &P2.Z).Equal(t2.Mul(&P2.X, &P.Z))
yeq := t1.Mul(&P.Y, &P2.Z).Equal(t2.Mul(&P2.Y, &P.Z))
return xeq && yeq
}
func (P *extPoint) Set(CP2 kyber.Point) kyber.Point {
P2 := CP2.(*extPoint)
P.c = P2.c
P.X.Set(&P2.X)
P.Y.Set(&P2.Y)
P.Z.Set(&P2.Z)
P.T.Set(&P2.T)
return P
}
func (P *extPoint) Clone() kyber.Point {
P2 := extPoint{}
P2.c = P.c
P2.X.Set(&P.X)
P2.Y.Set(&P.Y)
P2.Z.Set(&P.Z)
P2.T.Set(&P.T)
return &P2
}
func (P *extPoint) Null() kyber.Point {
P.Set(&P.c.null)
return P
}
func (P *extPoint) Base() kyber.Point {
P.Set(&P.c.base)
return P
}
func (P *extPoint) EmbedLen() int {
return P.c.embedLen()
}
// Normalize the point's representation to Z=1.
func (P *extPoint) normalize() {
P.Z.Inv(&P.Z)
P.X.Mul(&P.X, &P.Z)
P.Y.Mul(&P.Y, &P.Z)
P.Z.V.SetInt64(1)
P.T.Mul(&P.X, &P.Y)
}
// Check the validity of the T coordinate
func (P *extPoint) checkT() {
var t1, t2 mod.Int
if !t1.Mul(&P.X, &P.Y).Equal(t2.Mul(&P.Z, &P.T)) {
panic("oops")
}
}
func (P *extPoint) Embed(data []byte, rand cipher.Stream) kyber.Point {
P.c.embed(P, data, rand)
return P
}
func (P *extPoint) Pick(rand cipher.Stream) kyber.Point {
P.c.embed(P, nil, rand)
return P
}
// Extract embedded data from a point group element
func (P *extPoint) Data() ([]byte, error) {
P.normalize()
return P.c.data(&P.X, &P.Y)
}
// Add two points using optimized extended coordinate addition formulas.
func (P *extPoint) Add(CP1, CP2 kyber.Point) kyber.Point {
P1 := CP1.(*extPoint)
P2 := CP2.(*extPoint)
X1, Y1, Z1, T1 := &P1.X, &P1.Y, &P1.Z, &P1.T
X2, Y2, Z2, T2 := &P2.X, &P2.Y, &P2.Z, &P2.T
X3, Y3, Z3, T3 := &P.X, &P.Y, &P.Z, &P.T
var A, B, C, D, E, F, G, H mod.Int
A.Mul(X1, X2)
B.Mul(Y1, Y2)
C.Mul(T1, T2).Mul(&C, &P.c.d)
D.Mul(Z1, Z2)
E.Add(X1, Y1).Mul(&E, F.Add(X2, Y2)).Sub(&E, &A).Sub(&E, &B)
F.Sub(&D, &C)
G.Add(&D, &C)
H.Mul(&P.c.a, &A).Sub(&B, &H)
X3.Mul(&E, &F)
Y3.Mul(&G, &H)
T3.Mul(&E, &H)
Z3.Mul(&F, &G)
return P
}
// Subtract points.
func (P *extPoint) Sub(CP1, CP2 kyber.Point) kyber.Point {
P1 := CP1.(*extPoint)
P2 := CP2.(*extPoint)
X1, Y1, Z1, T1 := &P1.X, &P1.Y, &P1.Z, &P1.T
X2, Y2, Z2, T2 := &P2.X, &P2.Y, &P2.Z, &P2.T
X3, Y3, Z3, T3 := &P.X, &P.Y, &P.Z, &P.T
var A, B, C, D, E, F, G, H mod.Int
A.Mul(X1, X2)
B.Mul(Y1, Y2)
C.Mul(T1, T2).Mul(&C, &P.c.d)
D.Mul(Z1, Z2)
E.Add(X1, Y1).Mul(&E, F.Sub(Y2, X2)).Add(&E, &A).Sub(&E, &B)
F.Add(&D, &C)
G.Sub(&D, &C)
H.Mul(&P.c.a, &A).Add(&B, &H)
X3.Mul(&E, &F)
Y3.Mul(&G, &H)
T3.Mul(&E, &H)
Z3.Mul(&F, &G)
return P
}
// Find the negative of point A.
// For Edwards curves, the negative of (x,y) is (-x,y).
func (P *extPoint) Neg(CA kyber.Point) kyber.Point {
A := CA.(*extPoint)
P.c = A.c
P.X.Neg(&A.X)
P.Y.Set(&A.Y)
P.Z.Set(&A.Z)
P.T.Neg(&A.T)
return P
}
// Optimized point doubling for use in scalar multiplication.
// Uses the formulae in section 3.3 of:
// https://www.iacr.org/archive/asiacrypt2008/53500329/53500329.pdf
func (P *extPoint) double() {
X1, Y1, Z1, T1 := &P.X, &P.Y, &P.Z, &P.T
var A, B, C, D, E, F, G, H mod.Int
A.Mul(X1, X1)
B.Mul(Y1, Y1)
C.Mul(Z1, Z1).Add(&C, &C)
D.Mul(&P.c.a, &A)
E.Add(X1, Y1).Mul(&E, &E).Sub(&E, &A).Sub(&E, &B)
G.Add(&D, &B)
F.Sub(&G, &C)
H.Sub(&D, &B)
X1.Mul(&E, &F)
Y1.Mul(&G, &H)
T1.Mul(&E, &H)
Z1.Mul(&F, &G)
}
// Multiply point p by scalar s using the repeated doubling method.
//
// Currently doesn't implement the optimization of
// switching between projective and extended coordinates during
// scalar multiplication.
//
func (P *extPoint) Mul(s kyber.Scalar, G kyber.Point) kyber.Point {
v := s.(*mod.Int).V
if G == nil {
return P.Base().Mul(s, P)
}
T := P
if G == P { // Must use temporary for in-place multiply
T = &extPoint{}
}
T.Set(&P.c.null) // Initialize to identity element (0,1)
for i := v.BitLen() - 1; i >= 0; i-- {
T.double()
if v.Bit(i) != 0 {
T.Add(T, G)
}
}
if T != P {
P.Set(T)
}
return P
}
// ExtendedCurve implements Twisted Edwards curves
// using projective coordinate representation (X:Y:Z),
// satisfying the identities x = X/Z, y = Y/Z.
// This representation still supports all Twisted Edwards curves
// and avoids expensive modular inversions on the critical paths.
// Uses the projective arithmetic formulas in:
// http://cr.yp.to/newelliptic/newelliptic-20070906.pdf
//
// ExtendedCurve implements Twisted Edwards curves
// using the Extended Coordinate representation specified in:
// Hisil et al, "Twisted Edwards Curves Revisited",
// http://eprint.iacr.org/2008/522
//
// This implementation is designed to work with all Twisted Edwards curves,
// foregoing the further optimizations that are available for the
// special case with curve parameter a=-1.
// We leave the task of hyperoptimization to curve-specific implementations
// such as the ed25519 package.
//
type ExtendedCurve struct {
curve // generic Edwards curve functionality
null extPoint // Constant identity/null point (0,1)
base extPoint // Standard base point
}
// Point creates a new Point on this curve.
func (c *ExtendedCurve) Point() kyber.Point {
P := new(extPoint)
P.c = c
//P.Set(&c.null)
return P
}
// Init initializes the curve with given parameters.
func (c *ExtendedCurve) Init(p *Param, fullGroup bool) *ExtendedCurve {
c.curve.init(c, p, fullGroup, &c.null, &c.base)
return c
}