forked from cloudflare/bn256
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gfp2.go
213 lines (177 loc) · 4.13 KB
/
gfp2.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
package bn256
import (
"math/big"
)
// For details of the algorithms used, see "Multiplication and Squaring on
// Pairing-Friendly Fields, Devegili et al.
// http://eprint.iacr.org/2006/471.pdf.
// gfP2 implements a field of size p² as a quadratic extension of the base field
// where i²=-1.
type gfP2 struct {
X, Y gfP // value is xi+Y.
}
func gfP2Decode(in *gfP2) *gfP2 {
out := &gfP2{}
montDecode(&out.X, &in.X)
montDecode(&out.Y, &in.Y)
return out
}
func (e *gfP2) String() string {
return "(" + e.X.String() + ", " + e.Y.String() + ")"
}
func (e *gfP2) Set(a *gfP2) *gfP2 {
e.X.Set(&a.X)
e.Y.Set(&a.Y)
return e
}
func (e *gfP2) SetZero() *gfP2 {
e.X = gfP{0}
e.Y = gfP{0}
return e
}
func (e *gfP2) SetOne() *gfP2 {
e.X = gfP{0}
e.Y = *newGFp(1)
return e
}
func (e *gfP2) IsZero() bool {
zero := gfP{0}
return e.X == zero && e.Y == zero
}
func (e *gfP2) IsOne() bool {
zero, one := gfP{0}, *newGFp(1)
return e.X == zero && e.Y == one
}
func (e *gfP2) Conjugate(a *gfP2) *gfP2 {
e.Y.Set(&a.Y)
gfpNeg(&e.X, &a.X)
return e
}
func (e *gfP2) Neg(a *gfP2) *gfP2 {
gfpNeg(&e.X, &a.X)
gfpNeg(&e.Y, &a.Y)
return e
}
func (e *gfP2) Add(a, b *gfP2) *gfP2 {
gfpAdd(&e.X, &a.X, &b.X)
gfpAdd(&e.Y, &a.Y, &b.Y)
return e
}
func (e *gfP2) Sub(a, b *gfP2) *gfP2 {
gfpSub(&e.X, &a.X, &b.X)
gfpSub(&e.Y, &a.Y, &b.Y)
return e
}
// See "Multiplication and Squaring in Pairing-Friendly Fields",
// http://eprint.iacr.org/2006/471.pdf
func (e *gfP2) Mul(a, b *gfP2) *gfP2 {
tx, t := &gfP{}, &gfP{}
gfpMul(tx, &a.X, &b.Y)
gfpMul(t, &b.X, &a.Y)
gfpAdd(tx, tx, t)
ty := &gfP{}
gfpMul(ty, &a.Y, &b.Y)
gfpMul(t, &a.X, &b.X)
gfpSub(ty, ty, t)
e.X.Set(tx)
e.Y.Set(ty)
return e
}
func (e *gfP2) MulScalar(a *gfP2, b *gfP) *gfP2 {
gfpMul(&e.X, &a.X, b)
gfpMul(&e.Y, &a.Y, b)
return e
}
// MulXi sets e=ξa where ξ=i+3 and then returns e.
func (e *gfP2) MulXi(a *gfP2) *gfP2 {
// (xi+Y)(i+3) = (3x+Y)i+(3y-X)
tx := &gfP{}
gfpAdd(tx, &a.X, &a.X)
gfpAdd(tx, tx, &a.X)
gfpAdd(tx, tx, &a.Y)
ty := &gfP{}
gfpAdd(ty, &a.Y, &a.Y)
gfpAdd(ty, ty, &a.Y)
gfpSub(ty, ty, &a.X)
e.X.Set(tx)
e.Y.Set(ty)
return e
}
func (e *gfP2) Square(a *gfP2) *gfP2 {
// Complex squaring algorithm:
// (xi+Y)² = (X+Y)(Y-X) + 2*i*X*Y
tx, ty := &gfP{}, &gfP{}
gfpSub(tx, &a.Y, &a.X)
gfpAdd(ty, &a.X, &a.Y)
gfpMul(ty, tx, ty)
gfpMul(tx, &a.X, &a.Y)
gfpAdd(tx, tx, tx)
e.X.Set(tx)
e.Y.Set(ty)
return e
}
func (e *gfP2) Invert(a *gfP2) *gfP2 {
// See "Implementing cryptographic pairings", M. Scott, section 3.2.
// ftp://136.206.11.249/pub/crypto/pairings.pdf
t1, t2 := &gfP{}, &gfP{}
gfpMul(t1, &a.X, &a.X)
gfpMul(t2, &a.Y, &a.Y)
gfpAdd(t1, t1, t2)
inv := &gfP{}
inv.Invert(t1)
gfpNeg(t1, &a.X)
gfpMul(&e.X, t1, inv)
gfpMul(&e.Y, &a.Y, inv)
return e
}
func (e *gfP) divBy2(a *gfP) (*gfP, error) {
aInt, err := a.ToInt()
if err != nil {
return nil, err
}
if new(big.Int).Mod(aInt, big.NewInt(2)).Sign() == 0 {
return e.SetInt(new(big.Int).Div(aInt, big.NewInt(2))), nil
}
s := new(big.Int).Add(aInt, p)
return e.SetInt(new(big.Int).Div(s, big.NewInt(2))), nil
}
// Sqrt returns square root of g. Let's say g = a + b*i and tSqrt = sqrt(a^2 + b^2).
// Then Sqrt(g) = sqrt((a + tSqrt)/2) + i * b * 1 / (2*sqrt((a + tSqrt)/2)).
func (e *gfP2) Sqrt(g *gfP2) (*gfP2, error) {
yy, xx, t, tSqrt, z, newY, newYInv, xDiv2, newX := &gfP{}, &gfP{}, &gfP{}, &gfP{},
&gfP{}, &gfP{}, &gfP{}, &gfP{}, &gfP{}
gfpMul(yy, &g.Y, &g.Y)
gfpMul(xx, &g.X, &g.X)
gfpAdd(t, xx, yy)
var err error
tSqrt, err = tSqrt.Sqrt(t)
if err != nil { // g.Y^2 + g.X^2 is not QR
return e, err
}
gfpAdd(z, tSqrt, &g.Y) // Z = g.Y + sqrt(g.Y^2 + g.X^2)
z, err = z.divBy2(z) // Z = (g.Y + sqrt(g.Y^2 + g.X^2)) / 2
if err != nil {
return e, err
}
newY, err = newY.Sqrt(z)
if err != nil {
gfpSub(z, &g.Y, tSqrt) // Z = g.Y - sqrt(g.Y^2 + g.X^2)
z, err = z.divBy2(z) // Z = (g.Y - sqrt(g.Y^2 + g.X^2)) / 2
if err != nil {
return e, err
}
newY, err = newY.Sqrt(z)
if err != nil {
return e, err
}
}
newYInv.Invert(newY)
xDiv2, err = xDiv2.divBy2(&g.X)
if err != nil {
return e, err
}
gfpMul(newX, xDiv2, newYInv)
e.Y = *newY
e.X = *newX
return e, nil
}