-
Notifications
You must be signed in to change notification settings - Fork 151
/
conv.go
388 lines (320 loc) · 9.72 KB
/
conv.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
package element
const Conv = `
// rSquare where r is the Montgommery constant
// see section 2.3.2 of Tolga Acar's thesis
// https://www.microsoft.com/en-us/research/wp-content/uploads/1998/06/97Acar.pdf
var rSquare = {{.ElementName}}{
{{- range $i := .RSquare}}
{{$i}},{{end}}
}
// toMont converts z to Montgomery form
// sets and returns z = z * r²
func (z *{{.ElementName}}) toMont() *{{.ElementName}} {
return z.Mul(z, &rSquare)
}
// String returns the decimal representation of z as generated by
// z.Text(10).
func (z *{{.ElementName}}) String() string {
return z.Text(10)
}
// toBigInt returns z as a big.Int in Montgomery form
func (z *{{.ElementName}}) toBigInt(res *big.Int) *big.Int {
var b [Bytes]byte
{{- range $i := reverse .NbWordsIndexesFull}}
{{- $j := mul $i 8}}
{{- $k := sub $.NbWords 1}}
{{- $k := sub $k $i}}
{{- $jj := add $j 8}}
binary.BigEndian.PutUint64(b[{{$j}}:{{$jj}}], z[{{$k}}])
{{- end}}
return res.SetBytes(b[:])
}
{{- $noNeg := and (eq $.NbWords 1) (ltu64 (index $.Q 0) 1000000)}}
// Text returns the string representation of z in the given base.
// Base must be between 2 and 36, inclusive. The result uses the
// lower-case letters 'a' to 'z' for digit values 10 to 35.
// No prefix (such as "0x") is added to the string. If z is a nil
// pointer it returns "<nil>".
{{- if not $noNeg}}
// If base == 10 and -z fits in a uint16 prefix "-" is added to the string.
{{- end}}
func (z *{{.ElementName}}) Text(base int) string {
if base < 2 || base > 36 {
panic("invalid base")
}
if z == nil {
return "<nil>"
}
const maxUint16 = 65535
{{- if eq $.NbWords 1}}
{{- if not $noNeg}}
if base == 10 {
var zzNeg {{.ElementName}}
zzNeg.Neg(z)
zzNeg.fromMont()
if zzNeg[0] <= maxUint16 && zzNeg[0] != 0 {
return "-" + strconv.FormatUint(zzNeg[0], base)
}
}
{{- end}}
zz := z.Bits()
return strconv.FormatUint(zz[0], base)
{{- else }}
if base == 10 {
var zzNeg {{.ElementName}}
zzNeg.Neg(z)
zzNeg.fromMont()
if zzNeg.FitsOnOneWord() && zzNeg[0] <= maxUint16 && zzNeg[0] != 0 {
return "-" + strconv.FormatUint(zzNeg[0], base)
}
}
zz := *z
zz.fromMont()
if zz.FitsOnOneWord() {
return strconv.FormatUint(zz[0], base)
}
vv := pool.BigInt.Get()
r := zz.toBigInt(vv).Text(base)
pool.BigInt.Put(vv)
return r
{{- end}}
}
// BigInt sets and return z as a *big.Int
func (z *{{.ElementName}}) BigInt(res *big.Int) *big.Int {
_z := *z
_z.fromMont()
return _z.toBigInt(res)
}
// ToBigIntRegular returns z as a big.Int in regular form
//
// Deprecated: use BigInt(*big.Int) instead
func (z {{.ElementName}}) ToBigIntRegular(res *big.Int) *big.Int {
z.fromMont()
return z.toBigInt(res)
}
// Bits provides access to z by returning its value as a little-endian [{{.NbWords}}]uint64 array.
// Bits is intended to support implementation of missing low-level {{.ElementName}}
// functionality outside this package; it should be avoided otherwise.
func (z *{{.ElementName}}) Bits() [{{.NbWords}}]uint64 {
_z := *z
fromMont(&_z)
return _z
}
// Bytes returns the value of z as a big-endian byte array
func (z *{{.ElementName}}) Bytes() (res [Bytes]byte) {
BigEndian.PutElement(&res, *z)
return
}
// Marshal returns the value of z as a big-endian byte slice
func (z *{{.ElementName}}) Marshal() []byte {
b := z.Bytes()
return b[:]
}
// SetBytes interprets e as the bytes of a big-endian unsigned integer,
// sets z to that value, and returns z.
func (z *{{.ElementName}}) SetBytes(e []byte) *{{.ElementName}} {
if len(e) == Bytes {
// fast path
v, err := BigEndian.Element((*[Bytes]byte)(e))
if err == nil {
*z = v
return z
}
}
// slow path.
// get a big int from our pool
vv := pool.BigInt.Get()
vv.SetBytes(e)
// set big int
z.SetBigInt(vv)
// put temporary object back in pool
pool.BigInt.Put(vv)
return z
}
// SetBytesCanonical interprets e as the bytes of a big-endian {{.NbBytes}}-byte integer.
// If e is not a {{.NbBytes}}-byte slice or encodes a value higher than q,
// SetBytesCanonical returns an error.
func (z *{{.ElementName}}) SetBytesCanonical(e []byte) error {
if len(e) != Bytes {
return errors.New("invalid {{.PackageName}}.{{.ElementName}} encoding")
}
v, err := BigEndian.Element((*[Bytes]byte)(e))
if err != nil {
return err
}
*z = v
return nil
}
// SetBigInt sets z to v and returns z
func (z *{{.ElementName}}) SetBigInt(v *big.Int) *{{.ElementName}} {
z.SetZero()
var zero big.Int
// fast path
c := v.Cmp(&_modulus)
if c == 0 {
// v == 0
return z
} else if c != 1 && v.Cmp(&zero) != -1 {
// 0 < v < q
return z.setBigInt(v)
}
// get temporary big int from the pool
vv := pool.BigInt.Get()
// copy input + modular reduction
vv.Mod(v, &_modulus)
// set big int byte value
z.setBigInt(vv)
// release object into pool
pool.BigInt.Put(vv)
return z
}
// setBigInt assumes 0 ⩽ v < q
func (z *{{.ElementName}}) setBigInt(v *big.Int) *{{.ElementName}} {
vBits := v.Bits()
if bits.UintSize == 64 {
for i := 0; i < len(vBits); i++ {
z[i] = uint64(vBits[i])
}
} else {
for i := 0; i < len(vBits); i++ {
if i%2 == 0 {
z[i/2] = uint64(vBits[i])
} else {
z[i/2] |= uint64(vBits[i]) << 32
}
}
}
return z.toMont()
}
// SetString creates a big.Int with number and calls SetBigInt on z
//
// The number prefix determines the actual base: A prefix of
// ''0b'' or ''0B'' selects base 2, ''0'', ''0o'' or ''0O'' selects base 8,
// and ''0x'' or ''0X'' selects base 16. Otherwise, the selected base is 10
// and no prefix is accepted.
//
// For base 16, lower and upper case letters are considered the same:
// The letters 'a' to 'f' and 'A' to 'F' represent digit values 10 to 15.
//
// An underscore character ''_'' may appear between a base
// prefix and an adjacent digit, and between successive digits; such
// underscores do not change the value of the number.
// Incorrect placement of underscores is reported as a panic if there
// are no other errors.
//
// If the number is invalid this method leaves z unchanged and returns nil, error.
func (z *{{.ElementName}}) SetString(number string) (*{{.ElementName}}, error) {
// get temporary big int from the pool
vv := pool.BigInt.Get()
if _, ok := vv.SetString(number, 0); !ok {
return nil, errors.New("{{.ElementName}}.SetString failed -> can't parse number into a big.Int " + number)
}
z.SetBigInt(vv)
// release object into pool
pool.BigInt.Put(vv)
return z, nil
}
// MarshalJSON returns json encoding of z (z.Text(10))
// If z == nil, returns null
func (z *{{.ElementName}}) MarshalJSON() ([]byte, error) {
if z == nil {
return []byte("null"), nil
}
const maxSafeBound = 15 // we encode it as number if it's small
s := z.Text(10)
if len(s) <= maxSafeBound {
return []byte(s), nil
}
var sbb strings.Builder
sbb.WriteByte('"')
sbb.WriteString(s)
sbb.WriteByte('"')
return []byte(sbb.String()), nil
}
// UnmarshalJSON accepts numbers and strings as input
// See {{.ElementName}}.SetString for valid prefixes (0x, 0b, ...)
func (z *{{.ElementName}}) UnmarshalJSON(data []byte) error {
s := string(data)
if len(s) > Bits*3 {
return errors.New("value too large (max = {{.ElementName}}.Bits * 3)")
}
// we accept numbers and strings, remove leading and trailing quotes if any
if len(s) > 0 && s[0] == '"' {
s = s[1:]
}
if len(s) > 0 && s[len(s)-1] == '"' {
s = s[:len(s)-1]
}
// get temporary big int from the pool
vv := pool.BigInt.Get()
if _, ok := vv.SetString(s, 0); !ok {
return errors.New("can't parse into a big.Int: " + s)
}
z.SetBigInt(vv)
// release object into pool
pool.BigInt.Put(vv)
return nil
}
// A ByteOrder specifies how to convert byte slices into a {{.ElementName}}
type ByteOrder interface {
Element(*[Bytes]byte) ({{.ElementName}}, error)
PutElement(*[Bytes]byte, {{.ElementName}})
String() string
}
// BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder.
var BigEndian bigEndian
type bigEndian struct{}
// Element interpret b is a big-endian {{.NbBytes}}-byte slice.
// If b encodes a value higher than q, Element returns error.
func (bigEndian) Element(b *[Bytes]byte) ({{.ElementName}}, error) {
var z {{.ElementName}}
{{- range $i := reverse .NbWordsIndexesFull}}
{{- $j := mul $i 8}}
{{- $k := sub $.NbWords 1}}
{{- $k := sub $k $i}}
{{- $jj := add $j 8}}
z[{{$k}}] = binary.BigEndian.Uint64((*b)[{{$j}}:{{$jj}}])
{{- end}}
if !z.smallerThanModulus() {
return {{.ElementName}}{}, errors.New("invalid {{.PackageName}}.{{.ElementName}} encoding")
}
z.toMont()
return z, nil
}
func (bigEndian) PutElement(b *[Bytes]byte, e {{.ElementName}}) {
e.fromMont()
{{- range $i := reverse .NbWordsIndexesFull}}
{{- $j := mul $i 8}}
{{- $k := sub $.NbWords 1}}
{{- $k := sub $k $i}}
{{- $jj := add $j 8}}
binary.BigEndian.PutUint64((*b)[{{$j}}:{{$jj}}], e[{{$k}}])
{{- end}}
}
func (bigEndian) String() string { return "BigEndian" }
// LittleEndian is the little-endian implementation of ByteOrder and AppendByteOrder.
var LittleEndian littleEndian
type littleEndian struct{}
func (littleEndian) Element(b *[Bytes]byte) ({{.ElementName}}, error) {
var z {{.ElementName}}
{{- range $i := .NbWordsIndexesFull}}
{{- $j := mul $i 8}}
{{- $jj := add $j 8}}
z[{{$i}}] = binary.LittleEndian.Uint64((*b)[{{$j}}:{{$jj}}])
{{- end}}
if !z.smallerThanModulus() {
return {{.ElementName}}{}, errors.New("invalid {{.PackageName}}.{{.ElementName}} encoding")
}
z.toMont()
return z, nil
}
func (littleEndian) PutElement(b *[Bytes]byte, e {{.ElementName}}) {
e.fromMont()
{{- range $i := .NbWordsIndexesFull}}
{{- $j := mul $i 8}}
{{- $jj := add $j 8}}
binary.LittleEndian.PutUint64((*b)[{{$j}}:{{$jj}}], e[{{$i}}])
{{- end}}
}
func (littleEndian) String() string { return "LittleEndian" }
`