-
Notifications
You must be signed in to change notification settings - Fork 21
/
g2.go
391 lines (316 loc) · 9.5 KB
/
g2.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
package sm9curve
import (
"io"
"sync"
"errors"
"math/big"
)
var g2GeneratorTable *[32 * 2]twistPointTable
var g2GeneratorTableOnce sync.Once
// Gen2 is the generator of G2.
var Gen2 = &G2{twistGen}
// G2 is an abstract cyclic group. The zero value is suitable for use as the
// output of an operation, but cannot be used as an input.
type G2 struct {
p *twistPoint
}
// RandomG2 returns x and g₂ˣ where x is a random, non-zero number read from r.
func RandomG2(r io.Reader) (*big.Int, *G2, error) {
k, err := randomK(r)
if err != nil {
return nil, nil, err
}
g2, err := new(G2).ScalarBaseMult(NormalizeScalar(k.Bytes()))
return k, g2, err
}
func (e *G2) String() string {
return "sm9.G2" + e.p.String()
}
// ScalarBaseMult sets e to g*k where g is the generator of the group and then
// returns out.
func (e *G2) ScalarBaseMult(scalar []byte) (*G2, error) {
if len(scalar) != 32 {
return nil, errors.New("invalid scalar length")
}
if e.p == nil {
e.p = &twistPoint{}
}
// e.p.Mul(twistGen, k)
tables := e.generatorTable()
// This is also a scalar multiplication with a four-bit window like in
// ScalarMult, but in this case the doublings are precomputed. The value
// [windowValue]G added at iteration k would normally get doubled
// (totIterations-k)×4 times, but with a larger precomputation we can
// instead add [2^((totIterations-k)×4)][windowValue]G and avoid the
// doublings between iterations.
t := NewTwistPoint()
e.p.SetInfinity()
tableIndex := len(tables) - 1
for _, byte := range scalar {
windowValue := byte >> 4
tables[tableIndex].Select(t, windowValue)
e.p.Add(e.p, t)
tableIndex--
windowValue = byte & 0b1111
tables[tableIndex].Select(t, windowValue)
e.p.Add(e.p, t)
tableIndex--
}
return e, nil
}
// ScalarMult sets e to a*k and then returns e.
func (e *G2) ScalarMult(a *G2, scalar []byte) (*G2, error) {
if e.p == nil {
e.p = &twistPoint{}
}
// e.p.Mul(a.p, k)
// Compute a twistPointTable for the base point a.
var table = twistPointTable{
NewTwistPoint(), NewTwistPoint(), NewTwistPoint(),
NewTwistPoint(), NewTwistPoint(), NewTwistPoint(),
NewTwistPoint(), NewTwistPoint(), NewTwistPoint(),
NewTwistPoint(), NewTwistPoint(), NewTwistPoint(),
NewTwistPoint(), NewTwistPoint(), NewTwistPoint(),
}
table[0].Set(a.p)
for i := 1; i < 15; i += 2 {
table[i].Double(table[i/2])
table[i+1].Add(table[i], a.p)
}
// Instead of doing the classic double-and-add chain, we do it with a
// four-bit window: we double four times, and then add [0-15]P.
t := &G2{NewTwistPoint()}
e.p.SetInfinity()
for i, byte := range scalar {
// No need to double on the first iteration, as p is the identity at
// this point, and [N]∞ = ∞.
if i != 0 {
e.p.Double(e.p)
e.p.Double(e.p)
e.p.Double(e.p)
e.p.Double(e.p)
}
windowValue := byte >> 4
table.Select(t.p, windowValue)
e.Add(e, t)
e.p.Double(e.p)
e.p.Double(e.p)
e.p.Double(e.p)
e.p.Double(e.p)
windowValue = byte & 0b1111
table.Select(t.p, windowValue)
e.Add(e, t)
}
return e, nil
}
// Add sets e to a+b and then returns e.
func (e *G2) Add(a, b *G2) *G2 {
if e.p == nil {
e.p = &twistPoint{}
}
e.p.Add(a.p, b.p)
return e
}
// Neg sets e to -a and then returns e.
func (e *G2) Neg(a *G2) *G2 {
if e.p == nil {
e.p = &twistPoint{}
}
e.p.Neg(a.p)
return e
}
// Set sets e to a and then returns e.
func (e *G2) Set(a *G2) *G2 {
if e.p == nil {
e.p = &twistPoint{}
}
e.p.Set(a.p)
return e
}
// Equal compare e and other
func (e *G2) Equal(other *G2) bool {
if e.p == nil && other.p == nil {
return true
}
return e.p.Equal(other.p)
}
// IsOnCurve returns true if e is on the twist curve.
func (e *G2) IsOnCurve() bool {
return e.p.IsOnCurve()
}
func (g *G2) generatorTable() *[32 * 2]twistPointTable {
g2GeneratorTableOnce.Do(func() {
g2GeneratorTable = new([32 * 2]twistPointTable)
base := NewTwistGenerator()
for i := 0; i < 32*2; i++ {
g2GeneratorTable[i][0] = &twistPoint{}
g2GeneratorTable[i][0].Set(base)
for j := 1; j < 15; j += 2 {
g2GeneratorTable[i][j] = &twistPoint{}
g2GeneratorTable[i][j].Double(g2GeneratorTable[i][j/2])
g2GeneratorTable[i][j+1] = &twistPoint{}
g2GeneratorTable[i][j+1].Add(g2GeneratorTable[i][j], base)
}
base.Double(base)
base.Double(base)
base.Double(base)
base.Double(base)
}
})
return g2GeneratorTable
}
// Marshal converts e into a byte slice.
func (e *G2) Marshal() []byte {
// Each value is a 256-bit number.
const numBytes = 256 / 8
ret := make([]byte, numBytes*4)
e.fillBytes(ret)
return ret
}
func (e *G2) fillBytes(buffer []byte) {
// Each value is a 256-bit number.
const numBytes = 256 / 8
if e.p == nil {
e.p = &twistPoint{}
}
e.p.MakeAffine()
if e.p.IsInfinity() {
return
}
temp := &gfP{}
montDecode(temp, &e.p.x.x)
temp.Marshal(buffer)
montDecode(temp, &e.p.x.y)
temp.Marshal(buffer[numBytes:])
montDecode(temp, &e.p.y.x)
temp.Marshal(buffer[2*numBytes:])
montDecode(temp, &e.p.y.y)
temp.Marshal(buffer[3*numBytes:])
}
// Unmarshal sets e to the result of converting the output of Marshal back into
// a group element and then returns e.
func (e *G2) Unmarshal(m []byte) ([]byte, error) {
// Each value is a 256-bit number.
const numBytes = 256 / 8
if len(m) < 4*numBytes {
return nil, errors.New("sm9.G2: not enough data")
}
// Unmarshal the points and check their caps
if e.p == nil {
e.p = &twistPoint{}
}
var err error
if err = e.p.x.x.Unmarshal(m); err != nil {
return nil, err
}
if err = e.p.x.y.Unmarshal(m[numBytes:]); err != nil {
return nil, err
}
if err = e.p.y.x.Unmarshal(m[2*numBytes:]); err != nil {
return nil, err
}
if err = e.p.y.y.Unmarshal(m[3*numBytes:]); err != nil {
return nil, err
}
// Encode into Montgomery form and ensure it's on the curve
montEncode(&e.p.x.x, &e.p.x.x)
montEncode(&e.p.x.y, &e.p.x.y)
montEncode(&e.p.y.x, &e.p.y.x)
montEncode(&e.p.y.y, &e.p.y.y)
if e.p.x.IsZero() && e.p.y.IsZero() {
// This is the point at infinity.
e.p.y.SetOne()
e.p.z.SetZero()
e.p.t.SetZero()
} else {
e.p.z.SetOne()
e.p.t.SetOne()
if !e.p.IsOnCurve() {
return nil, errors.New("sm9.G2: malformed point")
}
}
return m[4*numBytes:], nil
}
// MarshalUncompressed converts e into a byte slice with uncompressed point prefix
func (e *G2) MarshalUncompressed() []byte {
// Each value is a 256-bit number.
const numBytes = 256 / 8
ret := make([]byte, numBytes*4+1)
ret[0] = 4
e.fillBytes(ret[1:])
return ret
}
func (e *G2) UnmarshalUncompressed(data []byte) ([]byte, error) {
const numBytes = 256 / 8
if len(data) < 4*numBytes+1 {
return nil, errors.New("sm9.G2: uncompress not enough data")
}
if data[0] != 4 {
return nil, errors.New("sm9.G2: invalid point uncompress byte")
}
return e.Unmarshal(data[1:])
}
// MarshalCompressed converts e into a byte slice with uncompressed point prefix
func (e *G2) MarshalCompressed() []byte {
// Each value is a 256-bit number.
const numBytes = 256 / 8
ret := make([]byte, numBytes*2+1)
if e.p == nil {
e.p = &twistPoint{}
}
e.p.MakeAffine()
temp := &gfP{}
montDecode(temp, &e.p.y.y)
temp.Marshal(ret[1:])
ret[0] = (ret[numBytes] & 1) | 2
montDecode(temp, &e.p.x.x)
temp.Marshal(ret[1:])
montDecode(temp, &e.p.x.y)
temp.Marshal(ret[numBytes+1:])
return ret
}
// UnmarshalCompressed sets e to the result of converting the output of Marshal back into
// a group element and then returns e.
func (e *G2) UnmarshalCompressed(data []byte) ([]byte, error) {
// Each value is a 256-bit number.
const numBytes = 256 / 8
if len(data) < 1+2*numBytes {
return nil, errors.New("sm9.G2: not enough data")
}
if data[0] != 2 && data[0] != 3 { // compressed form
return nil, errors.New("sm9.G2: invalid point compress byte")
}
var err error
// Unmarshal the points and check their caps
if e.p == nil {
e.p = &twistPoint{}
}
if err = e.p.x.x.Unmarshal(data[1:]); err != nil {
return nil, err
}
if err = e.p.x.y.Unmarshal(data[1+numBytes:]); err != nil {
return nil, err
}
montEncode(&e.p.x.x, &e.p.x.x)
montEncode(&e.p.x.y, &e.p.x.y)
x3 := e.p.polynomial(&e.p.x)
e.p.y.Sqrt(x3)
x3y := &gfP{}
montDecode(x3y, &e.p.y.y)
if byte(x3y[0]&1) != data[0]&1 {
e.p.y.Neg(&e.p.y)
}
if e.p.x.IsZero() && e.p.y.IsZero() {
// This is the point at infinity.
e.p.y.SetOne()
e.p.z.SetZero()
e.p.t.SetZero()
} else {
e.p.z.SetOne()
e.p.t.SetOne()
if !e.p.IsOnCurve() {
return nil, errors.New("sm9.G2: malformed point")
}
}
return data[1+2*numBytes:], nil
}