1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package bn256 implements a particular bilinear group at the 128-bit security level.
7 // Bilinear groups are the basis of many of the new cryptographic protocols
8 // that have been proposed over the past decade. They consist of a triplet of
9 // groups (G₁, G₂ and GT) such that there exists a function e(g₁ˣ,g₂ʸ)=gTˣʸ
10 // (where gₓ is a generator of the respective group). That function is called
11 // a pairing function.
13 // This package specifically implements the Optimal Ate pairing over a 256-bit
14 // Barreto-Naehrig curve as described in
15 // http://cryptojedi.org/papers/dclxvi-20100714.pdf. Its output is compatible
16 // with the implementation described in that paper.
17 package bn256 // import "golang.org/x/crypto/bn256"
25 // BUG(agl): this implementation is not constant time.
26 // TODO(agl): keep GF(p²) elements in Mongomery form.
28 // G1 is an abstract cyclic group. The zero value is suitable for use as the
29 // output of an operation, but cannot be used as an input.
34 // RandomG1 returns x and g₁ˣ where x is a random, non-zero number read from r.
35 func RandomG1(r io.Reader) (*big.Int, *G1, error) {
40 k, err = rand.Int(r, Order)
49 return k, new(G1).ScalarBaseMult(k), nil
52 func (g *G1) String() string {
53 return "bn256.G1" + g.p.String()
56 // ScalarBaseMult sets e to g*k where g is the generator of the group and
58 func (e *G1) ScalarBaseMult(k *big.Int) *G1 {
60 e.p = newCurvePoint(nil)
62 e.p.Mul(curveGen, k, new(bnPool))
66 // ScalarMult sets e to a*k and then returns e.
67 func (e *G1) ScalarMult(a *G1, k *big.Int) *G1 {
69 e.p = newCurvePoint(nil)
71 e.p.Mul(a.p, k, new(bnPool))
75 // Add sets e to a+b and then returns e.
76 // BUG(agl): this function is not complete: a==b fails.
77 func (e *G1) Add(a, b *G1) *G1 {
79 e.p = newCurvePoint(nil)
81 e.p.Add(a.p, b.p, new(bnPool))
85 // Neg sets e to -a and then returns e.
86 func (e *G1) Neg(a *G1) *G1 {
88 e.p = newCurvePoint(nil)
94 // Marshal converts n to a byte slice.
95 func (n *G1) Marshal() []byte {
98 xBytes := new(big.Int).Mod(n.p.x, p).Bytes()
99 yBytes := new(big.Int).Mod(n.p.y, p).Bytes()
101 // Each value is a 256-bit number.
102 const numBytes = 256 / 8
104 ret := make([]byte, numBytes*2)
105 copy(ret[1*numBytes-len(xBytes):], xBytes)
106 copy(ret[2*numBytes-len(yBytes):], yBytes)
111 // Unmarshal sets e to the result of converting the output of Marshal back into
112 // a group element and then returns e.
113 func (e *G1) Unmarshal(m []byte) (*G1, bool) {
114 // Each value is a 256-bit number.
115 const numBytes = 256 / 8
117 if len(m) != 2*numBytes {
122 e.p = newCurvePoint(nil)
125 e.p.x.SetBytes(m[0*numBytes : 1*numBytes])
126 e.p.y.SetBytes(m[1*numBytes : 2*numBytes])
128 if e.p.x.Sign() == 0 && e.p.y.Sign() == 0 {
129 // This is the point at infinity.
137 if !e.p.IsOnCurve() {
145 // G2 is an abstract cyclic group. The zero value is suitable for use as the
146 // output of an operation, but cannot be used as an input.
151 // RandomG1 returns x and g₂ˣ where x is a random, non-zero number read from r.
152 func RandomG2(r io.Reader) (*big.Int, *G2, error) {
157 k, err = rand.Int(r, Order)
166 return k, new(G2).ScalarBaseMult(k), nil
169 func (g *G2) String() string {
170 return "bn256.G2" + g.p.String()
173 // ScalarBaseMult sets e to g*k where g is the generator of the group and
175 func (e *G2) ScalarBaseMult(k *big.Int) *G2 {
177 e.p = newTwistPoint(nil)
179 e.p.Mul(twistGen, k, new(bnPool))
183 // ScalarMult sets e to a*k and then returns e.
184 func (e *G2) ScalarMult(a *G2, k *big.Int) *G2 {
186 e.p = newTwistPoint(nil)
188 e.p.Mul(a.p, k, new(bnPool))
192 // Add sets e to a+b and then returns e.
193 // BUG(agl): this function is not complete: a==b fails.
194 func (e *G2) Add(a, b *G2) *G2 {
196 e.p = newTwistPoint(nil)
198 e.p.Add(a.p, b.p, new(bnPool))
202 // Marshal converts n into a byte slice.
203 func (n *G2) Marshal() []byte {
206 xxBytes := new(big.Int).Mod(n.p.x.x, p).Bytes()
207 xyBytes := new(big.Int).Mod(n.p.x.y, p).Bytes()
208 yxBytes := new(big.Int).Mod(n.p.y.x, p).Bytes()
209 yyBytes := new(big.Int).Mod(n.p.y.y, p).Bytes()
211 // Each value is a 256-bit number.
212 const numBytes = 256 / 8
214 ret := make([]byte, numBytes*4)
215 copy(ret[1*numBytes-len(xxBytes):], xxBytes)
216 copy(ret[2*numBytes-len(xyBytes):], xyBytes)
217 copy(ret[3*numBytes-len(yxBytes):], yxBytes)
218 copy(ret[4*numBytes-len(yyBytes):], yyBytes)
223 // Unmarshal sets e to the result of converting the output of Marshal back into
224 // a group element and then returns e.
225 func (e *G2) Unmarshal(m []byte) (*G2, bool) {
226 // Each value is a 256-bit number.
227 const numBytes = 256 / 8
229 if len(m) != 4*numBytes {
234 e.p = newTwistPoint(nil)
237 e.p.x.x.SetBytes(m[0*numBytes : 1*numBytes])
238 e.p.x.y.SetBytes(m[1*numBytes : 2*numBytes])
239 e.p.y.x.SetBytes(m[2*numBytes : 3*numBytes])
240 e.p.y.y.SetBytes(m[3*numBytes : 4*numBytes])
242 if e.p.x.x.Sign() == 0 &&
243 e.p.x.y.Sign() == 0 &&
244 e.p.y.x.Sign() == 0 &&
245 e.p.y.y.Sign() == 0 {
246 // This is the point at infinity.
254 if !e.p.IsOnCurve() {
262 // GT is an abstract cyclic group. The zero value is suitable for use as the
263 // output of an operation, but cannot be used as an input.
268 func (g *GT) String() string {
269 return "bn256.GT" + g.p.String()
272 // ScalarMult sets e to a*k and then returns e.
273 func (e *GT) ScalarMult(a *GT, k *big.Int) *GT {
277 e.p.Exp(a.p, k, new(bnPool))
281 // Add sets e to a+b and then returns e.
282 func (e *GT) Add(a, b *GT) *GT {
286 e.p.Mul(a.p, b.p, new(bnPool))
290 // Neg sets e to -a and then returns e.
291 func (e *GT) Neg(a *GT) *GT {
295 e.p.Invert(a.p, new(bnPool))
299 // Marshal converts n into a byte slice.
300 func (n *GT) Marshal() []byte {
303 xxxBytes := n.p.x.x.x.Bytes()
304 xxyBytes := n.p.x.x.y.Bytes()
305 xyxBytes := n.p.x.y.x.Bytes()
306 xyyBytes := n.p.x.y.y.Bytes()
307 xzxBytes := n.p.x.z.x.Bytes()
308 xzyBytes := n.p.x.z.y.Bytes()
309 yxxBytes := n.p.y.x.x.Bytes()
310 yxyBytes := n.p.y.x.y.Bytes()
311 yyxBytes := n.p.y.y.x.Bytes()
312 yyyBytes := n.p.y.y.y.Bytes()
313 yzxBytes := n.p.y.z.x.Bytes()
314 yzyBytes := n.p.y.z.y.Bytes()
316 // Each value is a 256-bit number.
317 const numBytes = 256 / 8
319 ret := make([]byte, numBytes*12)
320 copy(ret[1*numBytes-len(xxxBytes):], xxxBytes)
321 copy(ret[2*numBytes-len(xxyBytes):], xxyBytes)
322 copy(ret[3*numBytes-len(xyxBytes):], xyxBytes)
323 copy(ret[4*numBytes-len(xyyBytes):], xyyBytes)
324 copy(ret[5*numBytes-len(xzxBytes):], xzxBytes)
325 copy(ret[6*numBytes-len(xzyBytes):], xzyBytes)
326 copy(ret[7*numBytes-len(yxxBytes):], yxxBytes)
327 copy(ret[8*numBytes-len(yxyBytes):], yxyBytes)
328 copy(ret[9*numBytes-len(yyxBytes):], yyxBytes)
329 copy(ret[10*numBytes-len(yyyBytes):], yyyBytes)
330 copy(ret[11*numBytes-len(yzxBytes):], yzxBytes)
331 copy(ret[12*numBytes-len(yzyBytes):], yzyBytes)
336 // Unmarshal sets e to the result of converting the output of Marshal back into
337 // a group element and then returns e.
338 func (e *GT) Unmarshal(m []byte) (*GT, bool) {
339 // Each value is a 256-bit number.
340 const numBytes = 256 / 8
342 if len(m) != 12*numBytes {
350 e.p.x.x.x.SetBytes(m[0*numBytes : 1*numBytes])
351 e.p.x.x.y.SetBytes(m[1*numBytes : 2*numBytes])
352 e.p.x.y.x.SetBytes(m[2*numBytes : 3*numBytes])
353 e.p.x.y.y.SetBytes(m[3*numBytes : 4*numBytes])
354 e.p.x.z.x.SetBytes(m[4*numBytes : 5*numBytes])
355 e.p.x.z.y.SetBytes(m[5*numBytes : 6*numBytes])
356 e.p.y.x.x.SetBytes(m[6*numBytes : 7*numBytes])
357 e.p.y.x.y.SetBytes(m[7*numBytes : 8*numBytes])
358 e.p.y.y.x.SetBytes(m[8*numBytes : 9*numBytes])
359 e.p.y.y.y.SetBytes(m[9*numBytes : 10*numBytes])
360 e.p.y.z.x.SetBytes(m[10*numBytes : 11*numBytes])
361 e.p.y.z.y.SetBytes(m[11*numBytes : 12*numBytes])
366 // Pair calculates an Optimal Ate pairing.
367 func Pair(g1 *G1, g2 *G2) *GT {
368 return >{optimalAte(g2.p, g1.p, new(bnPool))}
371 // bnPool implements a tiny cache of *big.Int objects that's used to reduce the
372 // number of allocations made during processing.
378 func (pool *bnPool) Get() *big.Int {
390 pool.bns = pool.bns[:l-1]
394 func (pool *bnPool) Put(bn *big.Int) {
398 pool.bns = append(pool.bns, bn)
402 func (pool *bnPool) Count() int {