go-snark/bn128/g1.go
bn128/g1.goRaw
1package bn128
2
3import (
4 "math/big"
5)
6
7type G1 struct {
8 F Fq
9 G [3]*big.Int
10}
11
12func NewG1(f Fq, g [2]*big.Int) G1 {
13 var g1 G1
14 g1.F = f
15 g1.G = [3]*big.Int{
16 g[0],
17 g[1],
18 g1.F.One(),
19 }
20 return g1
21}
22
23func (g1 G1) Zero() [2]*big.Int {
24 return [2]*big.Int{g1.F.Zero(), g1.F.Zero()}
25}
26func (g1 G1) IsZero(p [3]*big.Int) bool {
27 return g1.F.IsZero(p[2])
28}
29
30func (g1 G1) Add(p1, p2 [3]*big.Int) [3]*big.Int {
31
32 // https://en.wikibooks.org/wiki/Cryptography/Prime_Curve/Jacobian_Coordinates
33 // https://github.com/zcash/zcash/blob/master/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g1.cpp#L208
34 // http://hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian-0/addition/add-2007-bl.op3
35
36 if g1.IsZero(p1) {
37 return p2
38 }
39 if g1.IsZero(p2) {
40 return p1
41 }
42
43 x1 := p1[0]
44 y1 := p1[1]
45 z1 := p1[2]
46 x2 := p2[0]
47 y2 := p2[1]
48 z2 := p2[2]
49
50 z1z1 := g1.F.Square(z1)
51 z2z2 := g1.F.Square(z2)
52
53 u1 := g1.F.Mul(x1, z2z2)
54 u2 := g1.F.Mul(x2, z1z1)
55
56 t0 := g1.F.Mul(z2, z2z2)
57 s1 := g1.F.Mul(y1, t0)
58
59 t1 := g1.F.Mul(z1, z1z1)
60 s2 := g1.F.Mul(y2, t1)
61
62 h := g1.F.Sub(u2, u1)
63 t2 := g1.F.Add(h, h)
64 i := g1.F.Square(t2)
65 j := g1.F.Mul(h, i)
66 t3 := g1.F.Sub(s2, s1)
67 r := g1.F.Add(t3, t3)
68 v := g1.F.Mul(u1, i)
69 t4 := g1.F.Square(r)
70 t5 := g1.F.Add(v, v)
71 t6 := g1.F.Sub(t4, j)
72 x3 := g1.F.Sub(t6, t5)
73 t7 := g1.F.Sub(v, x3)
74 t8 := g1.F.Mul(s1, j)
75 t9 := g1.F.Add(t8, t8)
76 t10 := g1.F.Mul(r, t7)
77
78 y3 := g1.F.Sub(t10, t9)
79
80 t11 := g1.F.Add(z1, z2)
81 t12 := g1.F.Square(t11)
82 t13 := g1.F.Sub(t12, z1z1)
83 t14 := g1.F.Sub(t13, z2z2)
84 z3 := g1.F.Mul(t14, h)
85
86 return [3]*big.Int{x3, y3, z3}
87}
88
89func (g1 G1) Neg(p [3]*big.Int) [3]*big.Int {
90 return [3]*big.Int{
91 p[0],
92 g1.F.Neg(p[1]),
93 p[2],
94 }
95}
96func (g1 G1) Sub(a, b [3]*big.Int) [3]*big.Int {
97 return g1.Add(a, g1.Neg(b))
98}
99func (g1 G1) Double(p [3]*big.Int) [3]*big.Int {
100
101 // https://en.wikibooks.org/wiki/Cryptography/Prime_Curve/Jacobian_Coordinates
102 // http://hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian-0/doubling/dbl-2009-l.op3
103 // https://github.com/zcash/zcash/blob/master/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g1.cpp#L325
104
105 if g1.IsZero(p) {
106 return p
107 }
108
109 a := g1.F.Square(p[0])
110 b := g1.F.Square(p[1])
111 c := g1.F.Square(b)
112
113 t0 := g1.F.Add(p[0], b)
114 t1 := g1.F.Square(t0)
115 t2 := g1.F.Sub(t1, a)
116 t3 := g1.F.Sub(t2, c)
117
118 d := g1.F.Double(t3)
119 e := g1.F.Add(g1.F.Add(a, a), a)
120 f := g1.F.Square(e)
121
122 t4 := g1.F.Double(d)
123 x3 := g1.F.Sub(f, t4)
124
125 t5 := g1.F.Sub(d, x3)
126 twoC := g1.F.Add(c, c)
127 fourC := g1.F.Add(twoC, twoC)
128 t6 := g1.F.Add(fourC, fourC)
129 t7 := g1.F.Mul(e, t5)
130 y3 := g1.F.Sub(t7, t6)
131
132 t8 := g1.F.Mul(p[1], p[2])
133 z3 := g1.F.Double(t8)
134
135 return [3]*big.Int{x3, y3, z3}
136}
137
138func (g1 G1) MulScalar(p [3]*big.Int, e *big.Int) [3]*big.Int {
139 // https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Double-and-add
140 // for more possible implementations see g2.go file, at the function g2.MulScalar()
141
142 q := [3]*big.Int{g1.F.Zero(), g1.F.Zero(), g1.F.Zero()}
143 d := g1.F.Copy(e)
144 r := p
145 for i := d.BitLen() - 1; i >= 0; i-- {
146 q = g1.Double(q)
147 if d.Bit(i) == 1 {
148 q = g1.Add(q, r)
149 }
150 }
151
152 return q
153}
154
155func (g1 G1) Affine(p [3]*big.Int) [2]*big.Int {
156 if g1.IsZero(p) {
157 return g1.Zero()
158 }
159
160 zinv := g1.F.Inverse(p[2])
161 zinv2 := g1.F.Square(zinv)
162 x := g1.F.Mul(p[0], zinv2)
163
164 zinv3 := g1.F.Mul(zinv2, zinv)
165 y := g1.F.Mul(p[1], zinv3)
166
167 return [2]*big.Int{x, y}
168}
169
170func (g1 G1) Equal(p1, p2 [3]*big.Int) bool {
171 if g1.IsZero(p1) {
172 return g1.IsZero(p2)
173 }
174 if g1.IsZero(p2) {
175 return g1.IsZero(p1)
176 }
177
178 z1z1 := g1.F.Square(p1[2])
179 z2z2 := g1.F.Square(p2[2])
180
181 u1 := g1.F.Mul(p1[0], z2z2)
182 u2 := g1.F.Mul(p2[0], z1z1)
183
184 z1cub := g1.F.Mul(p1[2], z1z1)
185 z2cub := g1.F.Mul(p2[2], z2z2)
186
187 s1 := g1.F.Mul(p1[1], z2cub)
188 s2 := g1.F.Mul(p2[1], z1cub)
189
190 return g1.F.Equal(u1, u2) && g1.F.Equal(s1, s2)
191}