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.
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.
18 // (This package previously claimed to operate at a 128-bit security level.
19 // However, recent improvements in attacks mean that is no longer true. See
20 // https://moderncrypto.org/mail-archive/curves/2016/000740.html.)
21 package bn256 // import "golang.org/x/crypto/bn256"
29 // BUG(agl): this implementation is not constant time.
30 // TODO(agl): keep GF(p²) elements in Mongomery form.
32 // G1 is an abstract cyclic group. The zero value is suitable for use as the
33 // output of an operation, but cannot be used as an input.
38 // RandomG1 returns x and g₁ˣ where x is a random, non-zero number read from r.
39 func RandomG1(r io.Reader) (*big.Int, *G1, error) {
44 k, err = rand.Int(r, Order)
53 return k, new(G1).ScalarBaseMult(k), nil
56 func (e *G1) String() string {
57 return "bn256.G1" + e.p.String()
60 // ScalarBaseMult sets e to g*k where g is the generator of the group and
62 func (e *G1) ScalarBaseMult(k *big.Int) *G1 {
64 e.p = newCurvePoint(nil)
66 e.p.Mul(curveGen, k, new(bnPool))
70 // ScalarMult sets e to a*k and then returns e.
71 func (e *G1) ScalarMult(a *G1, k *big.Int) *G1 {
73 e.p = newCurvePoint(nil)
75 e.p.Mul(a.p, k, new(bnPool))
79 // Add sets e to a+b and then returns e.
80 // BUG(agl): this function is not complete: a==b fails.
81 func (e *G1) Add(a, b *G1) *G1 {
83 e.p = newCurvePoint(nil)
85 e.p.Add(a.p, b.p, new(bnPool))
89 // Neg sets e to -a and then returns e.
90 func (e *G1) Neg(a *G1) *G1 {
92 e.p = newCurvePoint(nil)
98 // Marshal converts n to a byte slice.
99 func (e *G1) Marshal() []byte {
100 // Each value is a 256-bit number.
101 const numBytes = 256 / 8
103 if e.p.IsInfinity() {
104 return make([]byte, numBytes*2)
109 xBytes := new(big.Int).Mod(e.p.x, p).Bytes()
110 yBytes := new(big.Int).Mod(e.p.y, p).Bytes()
112 ret := make([]byte, numBytes*2)
113 copy(ret[1*numBytes-len(xBytes):], xBytes)
114 copy(ret[2*numBytes-len(yBytes):], yBytes)
119 // Unmarshal sets e to the result of converting the output of Marshal back into
120 // a group element and then returns e.
121 func (e *G1) Unmarshal(m []byte) (*G1, bool) {
122 // Each value is a 256-bit number.
123 const numBytes = 256 / 8
125 if len(m) != 2*numBytes {
130 e.p = newCurvePoint(nil)
133 e.p.x.SetBytes(m[0*numBytes : 1*numBytes])
134 e.p.y.SetBytes(m[1*numBytes : 2*numBytes])
136 if e.p.x.Sign() == 0 && e.p.y.Sign() == 0 {
137 // This is the point at infinity.
145 if !e.p.IsOnCurve() {
153 // G2 is an abstract cyclic group. The zero value is suitable for use as the
154 // output of an operation, but cannot be used as an input.
159 // RandomG1 returns x and g₂ˣ where x is a random, non-zero number read from r.
160 func RandomG2(r io.Reader) (*big.Int, *G2, error) {
165 k, err = rand.Int(r, Order)
174 return k, new(G2).ScalarBaseMult(k), nil
177 func (e *G2) String() string {
178 return "bn256.G2" + e.p.String()
181 // ScalarBaseMult sets e to g*k where g is the generator of the group and
183 func (e *G2) ScalarBaseMult(k *big.Int) *G2 {
185 e.p = newTwistPoint(nil)
187 e.p.Mul(twistGen, k, new(bnPool))
191 // ScalarMult sets e to a*k and then returns e.
192 func (e *G2) ScalarMult(a *G2, k *big.Int) *G2 {
194 e.p = newTwistPoint(nil)
196 e.p.Mul(a.p, k, new(bnPool))
200 // Add sets e to a+b and then returns e.
201 // BUG(agl): this function is not complete: a==b fails.
202 func (e *G2) Add(a, b *G2) *G2 {
204 e.p = newTwistPoint(nil)
206 e.p.Add(a.p, b.p, new(bnPool))
210 // Marshal converts n into a byte slice.
211 func (n *G2) Marshal() []byte {
212 // Each value is a 256-bit number.
213 const numBytes = 256 / 8
215 if n.p.IsInfinity() {
216 return make([]byte, numBytes*4)
221 xxBytes := new(big.Int).Mod(n.p.x.x, p).Bytes()
222 xyBytes := new(big.Int).Mod(n.p.x.y, p).Bytes()
223 yxBytes := new(big.Int).Mod(n.p.y.x, p).Bytes()
224 yyBytes := new(big.Int).Mod(n.p.y.y, p).Bytes()
226 ret := make([]byte, numBytes*4)
227 copy(ret[1*numBytes-len(xxBytes):], xxBytes)
228 copy(ret[2*numBytes-len(xyBytes):], xyBytes)
229 copy(ret[3*numBytes-len(yxBytes):], yxBytes)
230 copy(ret[4*numBytes-len(yyBytes):], yyBytes)
235 // Unmarshal sets e to the result of converting the output of Marshal back into
236 // a group element and then returns e.
237 func (e *G2) Unmarshal(m []byte) (*G2, bool) {
238 // Each value is a 256-bit number.
239 const numBytes = 256 / 8
241 if len(m) != 4*numBytes {
246 e.p = newTwistPoint(nil)
249 e.p.x.x.SetBytes(m[0*numBytes : 1*numBytes])
250 e.p.x.y.SetBytes(m[1*numBytes : 2*numBytes])
251 e.p.y.x.SetBytes(m[2*numBytes : 3*numBytes])
252 e.p.y.y.SetBytes(m[3*numBytes : 4*numBytes])
254 if e.p.x.x.Sign() == 0 &&
255 e.p.x.y.Sign() == 0 &&
256 e.p.y.x.Sign() == 0 &&
257 e.p.y.y.Sign() == 0 {
258 // This is the point at infinity.
266 if !e.p.IsOnCurve() {
274 // GT is an abstract cyclic group. The zero value is suitable for use as the
275 // output of an operation, but cannot be used as an input.
280 func (g *GT) String() string {
281 return "bn256.GT" + g.p.String()
284 // ScalarMult sets e to a*k and then returns e.
285 func (e *GT) ScalarMult(a *GT, k *big.Int) *GT {
289 e.p.Exp(a.p, k, new(bnPool))
293 // Add sets e to a+b and then returns e.
294 func (e *GT) Add(a, b *GT) *GT {
298 e.p.Mul(a.p, b.p, new(bnPool))
302 // Neg sets e to -a and then returns e.
303 func (e *GT) Neg(a *GT) *GT {
307 e.p.Invert(a.p, new(bnPool))
311 // Marshal converts n into a byte slice.
312 func (n *GT) Marshal() []byte {
315 xxxBytes := n.p.x.x.x.Bytes()
316 xxyBytes := n.p.x.x.y.Bytes()
317 xyxBytes := n.p.x.y.x.Bytes()
318 xyyBytes := n.p.x.y.y.Bytes()
319 xzxBytes := n.p.x.z.x.Bytes()
320 xzyBytes := n.p.x.z.y.Bytes()
321 yxxBytes := n.p.y.x.x.Bytes()
322 yxyBytes := n.p.y.x.y.Bytes()
323 yyxBytes := n.p.y.y.x.Bytes()
324 yyyBytes := n.p.y.y.y.Bytes()
325 yzxBytes := n.p.y.z.x.Bytes()
326 yzyBytes := n.p.y.z.y.Bytes()
328 // Each value is a 256-bit number.
329 const numBytes = 256 / 8
331 ret := make([]byte, numBytes*12)
332 copy(ret[1*numBytes-len(xxxBytes):], xxxBytes)
333 copy(ret[2*numBytes-len(xxyBytes):], xxyBytes)
334 copy(ret[3*numBytes-len(xyxBytes):], xyxBytes)
335 copy(ret[4*numBytes-len(xyyBytes):], xyyBytes)
336 copy(ret[5*numBytes-len(xzxBytes):], xzxBytes)
337 copy(ret[6*numBytes-len(xzyBytes):], xzyBytes)
338 copy(ret[7*numBytes-len(yxxBytes):], yxxBytes)
339 copy(ret[8*numBytes-len(yxyBytes):], yxyBytes)
340 copy(ret[9*numBytes-len(yyxBytes):], yyxBytes)
341 copy(ret[10*numBytes-len(yyyBytes):], yyyBytes)
342 copy(ret[11*numBytes-len(yzxBytes):], yzxBytes)
343 copy(ret[12*numBytes-len(yzyBytes):], yzyBytes)
348 // Unmarshal sets e to the result of converting the output of Marshal back into
349 // a group element and then returns e.
350 func (e *GT) Unmarshal(m []byte) (*GT, bool) {
351 // Each value is a 256-bit number.
352 const numBytes = 256 / 8
354 if len(m) != 12*numBytes {
362 e.p.x.x.x.SetBytes(m[0*numBytes : 1*numBytes])
363 e.p.x.x.y.SetBytes(m[1*numBytes : 2*numBytes])
364 e.p.x.y.x.SetBytes(m[2*numBytes : 3*numBytes])
365 e.p.x.y.y.SetBytes(m[3*numBytes : 4*numBytes])
366 e.p.x.z.x.SetBytes(m[4*numBytes : 5*numBytes])
367 e.p.x.z.y.SetBytes(m[5*numBytes : 6*numBytes])
368 e.p.y.x.x.SetBytes(m[6*numBytes : 7*numBytes])
369 e.p.y.x.y.SetBytes(m[7*numBytes : 8*numBytes])
370 e.p.y.y.x.SetBytes(m[8*numBytes : 9*numBytes])
371 e.p.y.y.y.SetBytes(m[9*numBytes : 10*numBytes])
372 e.p.y.z.x.SetBytes(m[10*numBytes : 11*numBytes])
373 e.p.y.z.y.SetBytes(m[11*numBytes : 12*numBytes])
378 // Pair calculates an Optimal Ate pairing.
379 func Pair(g1 *G1, g2 *G2) *GT {
380 return >{optimalAte(g2.p, g1.p, new(bnPool))}
383 // bnPool implements a tiny cache of *big.Int objects that's used to reduce the
384 // number of allocations made during processing.
390 func (pool *bnPool) Get() *big.Int {
402 pool.bns = pool.bns[:l-1]
406 func (pool *bnPool) Put(bn *big.Int) {
410 pool.bns = append(pool.bns, bn)
414 func (pool *bnPool) Count() int {