-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmath.go
More file actions
392 lines (339 loc) · 10.1 KB
/
math.go
File metadata and controls
392 lines (339 loc) · 10.1 KB
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
package bulletproofs
import (
"bytes"
"errors"
"fmt"
"github.com/sirupsen/logrus"
"io"
"math/big"
)
// Point is a group element of the secp256k1 curve in affine coordinates.
type Point struct {
X *big.Int
Y *big.Int
}
// Equals returns true if the given point is the same.
func (p *Point) Equals(other *Point) bool {
return p.X.Cmp(other.X) == 0 && p.Y.Cmp(other.Y) == 0
}
// String prints the coordinates of this point.
func (p *Point) String() string {
return fmt.Sprintf("{x: %032x, y: %032x}", p.X.Bytes(), p.Y.Bytes())
}
// Read deserializes a compressed elliptic curve point from the reader.
func (p *Point) Read(r io.Reader) error {
buf := make([]byte, 32+1)
if _, err := io.ReadFull(r, buf); err != nil {
return err
}
sign := buf[0]
x := new(big.Int).SetBytes(buf[1:])
if (sign & 0xfe) != 8 {
return errors.New("point is not serialized correctly")
}
// Derive the possible y coordinates from the secp256k1 curve
// y² = x³ + 7.
x3 := new(big.Int).Mul(x, x)
x3.Mul(x3, x)
x3.Add(x3, curve.Params().B)
// y = ±sqrt(x³ + 7).
y := ModSqrtFast(x3)
// Pick which y from the sign encoded in the first byte.
if (sign & 1) != 0 {
y = new(big.Int).Sub(curve.P, y)
}
p.X = x
p.Y = y
return nil
}
// serializedPedersenCommitment is the constant that is encoded to signal that
// the encoded value is a Pedersen commitment, rather than a standard compressed
// curve point.
const serializedPedersenCommitment = byte(9)
// Bytes compresses and serializes the point.
func (p *Point) Bytes() []byte {
buff := new(bytes.Buffer)
sign := serializedPedersenCommitment
if IsQuadraticResidue(p.Y) {
sign ^= 1
}
if err := buff.WriteByte(sign); err != nil {
logrus.Fatal(err)
}
x := GetB32(p.X)
if _, err := buff.Write(x[:]); err != nil {
logrus.Fatal(err)
}
return buff.Bytes()
}
// isOdd returns true if the given integer is odd.
func isOdd(a *big.Int) bool {
return a.Bit(0) == 1
}
// ModSqrtOrig returns a value v such that v*v = x mod P.
func ModSqrtOrig(x *big.Int) *big.Int {
return new(big.Int).ModSqrt(x, curve.Params().P)
}
// ModSqrtFast returns a value v such that v*v = x mod P. This is about twice as
// fast as ModSqrtOrig. See: https://bitcointalk.org/index.php?topic=162805.msg1712294#msg1712294
func ModSqrtFast(x *big.Int) *big.Int {
return new(big.Int).Exp(x, curve.QPlus1Div4(), curve.Params().P)
}
// IsQuadraticResidue returns true if there exists some x such that
// x*x = y mod P.
func IsQuadraticResidue(y *big.Int) bool {
return big.Jacobi(y, curve.P) >= 0
}
// SerializePoints returns a byte slice containing a bit vector that indicates
// whether the points
func SerializePoints(points []*Point) []byte {
bitvec := make([]byte, (len(points)+7)/8)
// Encode whether each y value is a quadratic residue, so when we decompress
// the points we can determine the sign of the y coordinate.
for i, point := range points {
if !IsQuadraticResidue(point.Y) {
bitvec[i/8] |= 1 << (uint(i) % 8)
}
}
// Now write all the x coordinates as fixed 32-byte integers.
buff := new(bytes.Buffer)
for _, point := range points {
x := GetB32(point.X)
if _, err := buff.Write(x[:]); err != nil {
logrus.Fatal(err)
}
}
return append(bitvec, buff.Bytes()...)
}
// DeserializePoints parses num points that have been serialized using
// SerializePoints.
func DeserializePoints(buf []byte, num uint) ([]*Point, error) {
bitvecSize := (num + 7) / 8
isNonResidue := buf[0:bitvecSize]
xcoords := buf[bitvecSize:]
points := make([]*Point, num)
for i := uint(0); i < num; i++ {
x := new(big.Int).SetBytes(xcoords[i*32 : (i+1)*32])
// Derive the possible y coordinates from the secp256k1 curve
// y² = x³ + 7.
x3 := new(big.Int).Mul(x, x)
x3.Mul(x3, x)
x3.Add(x3, curve.Params().B)
// y = ±sqrt(x³ + 7).
y := ModSqrtFast(x3)
// Pick which y from the bit vector.
if isNonResidue[i/8]&(1<<(i%8)) != 0 {
y = new(big.Int).Sub(curve.P, y)
}
points[i] = new(Point)
points[i].Y = y
points[i].X = x
}
return points, nil
}
// ScalarMulPoint multiplies a point by a scalar.
func ScalarMulPoint(point *Point, scalar *big.Int) *Point {
x, y := curve.ScalarMult(point.X, point.Y, scalar.Bytes())
return &Point{x, y}
}
// ScalarMultAll multiplies all points by the given scalar and sums the results.
func ScalarMultAll(scalar *big.Int, points ...*Point) *Point {
initial := ScalarMulPoint(points[0], scalar)
sumx := new(big.Int).Set(initial.X)
sumy := new(big.Int).Set(initial.Y)
for i := 1; i < len(points); i++ {
mult := ScalarMulPoint(points[i], scalar)
sumx, sumy = curve.Add(sumx, sumy, mult.X, mult.Y)
}
return &Point{sumx, sumy}
}
// ScalarMulPoints multiplies each point with the corresponding scalar and sums
// the results. This function will panic if the number of scalars and points
// differ.
func ScalarMulPoints(scalars []*big.Int, points []*Point) *Point {
if len(scalars) != len(points) {
panic("len(scalars) != len(points)")
}
initial := ScalarMulPoint(points[0], scalars[0])
sumx := new(big.Int).Set(initial.X)
sumy := new(big.Int).Set(initial.Y)
for i := 1; i < len(points); i++ {
mult := ScalarMulPoint(points[i], scalars[i])
sumx, sumy = curve.Add(sumx, sumy, mult.X, mult.Y)
}
return &Point{sumx, sumy}
}
// ScalarMul returns the vector that is the result of the scalar multiplication
// of vector and scalar.
func ScalarMul(vector []*big.Int, scalar *big.Int) []*big.Int {
result := make([]*big.Int, len(vector))
for i := range vector {
result[i] = Mul(vector[i], scalar)
}
return result
}
// ScalarMultArray multiplies each point in the vector points by the scalar xi
// and returns them as a vector.
func ScalarMultArray(xi *big.Int, points []*Point) []*Point {
result := make([]*Point, len(points))
for i := range points {
result[i] = ScalarMulPoint(points[i], xi)
}
return result
}
// Square computes and returns z*z.
func Square(z *big.Int) *big.Int {
return Mul(z, z)
}
// AddVectors returns the vector z = a + b. This function will panic if the vectors are
// of different length.
func AddVectors(a []*big.Int, b []*big.Int) []*big.Int {
if len(a) != len(b) {
panic("vectors must be equal dimension")
}
z := make([]*big.Int, len(a))
for i := range a {
z[i] = Sum(a[i], b[i])
}
return z
}
// AddVectors3 returns the vector z = a + b + c.
func AddVectors3(a []*big.Int, b []*big.Int, c []*big.Int) []*big.Int {
if len(a) != len(b) || len(a) != len(c) {
panic("vectors must be equal dimension")
}
z := make([]*big.Int, len(a))
for i := range a {
z[i] = Sum(a[i], b[i], c[i])
}
return z
}
// Dot computes the inner product of two vectors of length n: a · b =
// a_1 * b_1 + a_2 * b_2 + ··· + a_n * b_n.
func Dot(a, b []*big.Int) *big.Int {
if len(a) != len(b) {
panic("vectors must have same length")
}
result := big.NewInt(0)
for i := 0; i < len(a); i++ {
result.Add(result, Mul(a[i], b[i]))
}
result.Mod(result, curve.N)
return result
}
// SubScalars returns the scalar a - b.
func SubScalars(a, b *big.Int) *big.Int {
aMinusB := new(big.Int).Sub(a, b)
aMinusB.Mod(aMinusB, curve.N)
return aMinusB
}
// SubVectors returns the vector a - b. This function will panic if the vectors are of
// different lengths.
func SubVectors(a, b []*big.Int) []*big.Int {
if len(a) != len(b) {
panic("vectors must have same length")
}
var result []*big.Int
for i := 0; i < len(a); i++ {
result = append(result, SubScalars(a[i], b[i]))
}
return result
}
// Ones returns a vector of length n where all elements are 1.
func Ones(n int) []*big.Int {
ones := make([]*big.Int, n)
for i := 0; i < n; i++ {
ones[i] = big.NewInt(1)
}
return ones
}
// Hadamard computes the vector given by element-wise multiplication of the two
// given vectors. a ○ b = (a_0*b_0 a_1*b_1 ... a_n*b_n). This function will
// panic if the vectors have different lengths.
func Hadamard(a, b []*big.Int) []*big.Int {
if len(a) != len(b) {
panic("vectors must be the same length")
}
result := make([]*big.Int, len(a))
for i := 0; i < len(a); i++ {
result[i] = Mul(a[i], b[i])
}
return result
}
// HadamardP computes the element-wise point addition of the two vectors. This
// function will panic if the vectors have different lengths.
func HadamardP(a []*Point, b []*Point) []*Point {
if len(a) != len(b) {
panic("vectors must be the same length")
}
result := make([]*Point, len(a))
for i := range a {
result[i] = new(Point)
result[i].X, result[i].Y = curve.Add(a[i].X, a[i].Y, b[i].X, b[i].Y)
}
return result
}
// Sum adds the given numbers and returns the total sum.
func Sum(nums ...*big.Int) *big.Int {
sum := new(big.Int).Set(nums[0])
for i := 1; i < len(nums); i++ {
sum.Add(sum, nums[i])
}
sum.Mod(sum, curve.N)
return sum
}
// SumPoints adds the given curve points and returns the total sum.
func SumPoints(points ...*Point) *Point {
sumx := new(big.Int).Set(points[0].X)
sumy := new(big.Int).Set(points[0].Y)
for i := 1; i < len(points); i++ {
sumx, sumy = curve.Add(sumx, sumy, points[i].X, points[i].Y)
}
return &Point{
X: sumx,
Y: sumy,
}
}
// Mul returns the product of the given integers.
func Mul(nums ...*big.Int) *big.Int {
prod := new(big.Int).Set(nums[0])
for i := 1; i < len(nums); i++ {
prod.Mul(prod, nums[i])
}
prod.Mod(prod, curve.N)
return prod
}
// VectorOf returns a length n vector of vs.
func VectorOf(n int, v *big.Int) []*big.Int {
vec := make([]*big.Int, n)
for i := 0; i < n; i++ {
vec[i] = new(big.Int).Set(v)
}
return vec
}
// Neg returns the additive inverse of z modulo the group order, i.e. -z such
// that z + (-z) = 0 mod N.
func Neg(z *big.Int) *big.Int {
x := new(big.Int).Sub(curve.N, z)
return x.Mod(x, curve.N)
}
// Inv returns the multiplicative inverse of z modulo the group order, i.e. z^-1
// such that z * z^-1 = 1 mod N.
func Inv(z *big.Int) *big.Int {
return new(big.Int).ModInverse(z, curve.N)
}
// GetB32 returns a fixed size 32-byte slice containing the big-endian
// representation of num. This function will panic if the given number does not
// fit into 32 bytes.
func GetB32(num *big.Int) [32]byte {
numBytes := num.Bytes()
if len(numBytes) > 32 {
panic("num doesn't fit in 32 bytes")
}
var b [32]byte
offset := 32 - len(numBytes)
for i := offset; i < 32; i++ {
b[i] = numBytes[i-offset]
}
return b
}