github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/crypto/bn256/cloudflare/bn256.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  // Package bn256 implements a particular bilinear group at the 128-bit security
    19  // level.
    20  //
    21  // Bilinear groups are the basis of many of the new cryptographic protocols that
    22  // have been proposed over the past decade. They consist of a triplet of groups
    23  // (G₁, G₂ and GT) such that there exists a function e(g₁ˣ,g₂ʸ)=gTˣʸ (where gₓ
    24  // is a generator of the respective group). That function is called a pairing
    25  // function.
    26  //
    27  // This package specifically implements the Optimal Ate pairing over a 256-bit
    28  // Barreto-Naehrig curve as described in
    29  // http://cryptojedi.org/papers/dclxvi-20100714.pdf. Its output is compatible
    30  // with the implementation described in that paper.
    31  package bn256
    32  
    33  import (
    34  	"crypto/rand"
    35  	"errors"
    36  	"io"
    37  	"math/big"
    38  )
    39  
    40  func randomK(r io.Reader) (k *big.Int, err error) {
    41  	for {
    42  		k, err = rand.Int(r, Order)
    43  		if k.Sign() > 0 || err != nil {
    44  			return
    45  		}
    46  	}
    47  }
    48  
    49  // G1 is an abstract cyclic group. The zero value is suitable for use as the
    50  // output of an operation, but cannot be used as an input.
    51  type G1 struct {
    52  	p *curvePoint
    53  }
    54  
    55  // RandomG1 returns x and g₁ˣ where x is a random, non-zero number read from r.
    56  func RandomG1(r io.Reader) (*big.Int, *G1, error) {
    57  	k, err := randomK(r)
    58  	if err != nil {
    59  		return nil, nil, err
    60  	}
    61  
    62  	return k, new(G1).ScalarBaseMult(k), nil
    63  }
    64  
    65  func (g *G1) String() string {
    66  	return "bn256.G1" + g.p.String()
    67  }
    68  
    69  // ScalarBaseMult sets e to g*k where g is the generator of the group and then
    70  // returns e.
    71  func (e *G1) ScalarBaseMult(k *big.Int) *G1 {
    72  	if e.p == nil {
    73  		e.p = &curvePoint{}
    74  	}
    75  	e.p.Mul(curveGen, k)
    76  	return e
    77  }
    78  
    79  // ScalarMult sets e to a*k and then returns e.
    80  func (e *G1) ScalarMult(a *G1, k *big.Int) *G1 {
    81  	if e.p == nil {
    82  		e.p = &curvePoint{}
    83  	}
    84  	e.p.Mul(a.p, k)
    85  	return e
    86  }
    87  
    88  // Add sets e to a+b and then returns e.
    89  func (e *G1) Add(a, b *G1) *G1 {
    90  	if e.p == nil {
    91  		e.p = &curvePoint{}
    92  	}
    93  	e.p.Add(a.p, b.p)
    94  	return e
    95  }
    96  
    97  // Neg sets e to -a and then returns e.
    98  func (e *G1) Neg(a *G1) *G1 {
    99  	if e.p == nil {
   100  		e.p = &curvePoint{}
   101  	}
   102  	e.p.Neg(a.p)
   103  	return e
   104  }
   105  
   106  // Set sets e to a and then returns e.
   107  func (e *G1) Set(a *G1) *G1 {
   108  	if e.p == nil {
   109  		e.p = &curvePoint{}
   110  	}
   111  	e.p.Set(a.p)
   112  	return e
   113  }
   114  
   115  // Marshal converts e to a byte slice.
   116  func (e *G1) Marshal() []byte {
   117  	// Each value is a 256-bit number.
   118  	const numBytes = 256 / 8
   119  
   120  	if e.p == nil {
   121  		e.p = &curvePoint{}
   122  	}
   123  
   124  	e.p.MakeAffine()
   125  	ret := make([]byte, numBytes*2)
   126  	if e.p.IsInfinity() {
   127  		return ret
   128  	}
   129  	temp := &gfP{}
   130  
   131  	montDecode(temp, &e.p.x)
   132  	temp.Marshal(ret)
   133  	montDecode(temp, &e.p.y)
   134  	temp.Marshal(ret[numBytes:])
   135  
   136  	return ret
   137  }
   138  
   139  // Unmarshal sets e to the result of converting the output of Marshal back into
   140  // a group element and then returns e.
   141  func (e *G1) Unmarshal(m []byte) ([]byte, error) {
   142  	// Each value is a 256-bit number.
   143  	const numBytes = 256 / 8
   144  	if len(m) < 2*numBytes {
   145  		return nil, errors.New("bn256: not enough data")
   146  	}
   147  	// Unmarshal the points and check their caps
   148  	if e.p == nil {
   149  		e.p = &curvePoint{}
   150  	} else {
   151  		e.p.x, e.p.y = gfP{0}, gfP{0}
   152  	}
   153  	var err error
   154  	if err = e.p.x.Unmarshal(m); err != nil {
   155  		return nil, err
   156  	}
   157  	if err = e.p.y.Unmarshal(m[numBytes:]); err != nil {
   158  		return nil, err
   159  	}
   160  	// Encode into Montgomery form and ensure it's on the curve
   161  	montEncode(&e.p.x, &e.p.x)
   162  	montEncode(&e.p.y, &e.p.y)
   163  
   164  	zero := gfP{0}
   165  	if e.p.x == zero && e.p.y == zero {
   166  		// This is the point at infinity.
   167  		e.p.y = *newGFp(1)
   168  		e.p.z = gfP{0}
   169  		e.p.t = gfP{0}
   170  	} else {
   171  		e.p.z = *newGFp(1)
   172  		e.p.t = *newGFp(1)
   173  
   174  		if !e.p.IsOnCurve() {
   175  			return nil, errors.New("bn256: malformed point")
   176  		}
   177  	}
   178  	return m[2*numBytes:], nil
   179  }
   180  
   181  // G2 is an abstract cyclic group. The zero value is suitable for use as the
   182  // output of an operation, but cannot be used as an input.
   183  type G2 struct {
   184  	p *twistPoint
   185  }
   186  
   187  // RandomG2 returns x and g₂ˣ where x is a random, non-zero number read from r.
   188  func RandomG2(r io.Reader) (*big.Int, *G2, error) {
   189  	k, err := randomK(r)
   190  	if err != nil {
   191  		return nil, nil, err
   192  	}
   193  
   194  	return k, new(G2).ScalarBaseMult(k), nil
   195  }
   196  
   197  func (e *G2) String() string {
   198  	return "bn256.G2" + e.p.String()
   199  }
   200  
   201  // ScalarBaseMult sets e to g*k where g is the generator of the group and then
   202  // returns out.
   203  func (e *G2) ScalarBaseMult(k *big.Int) *G2 {
   204  	if e.p == nil {
   205  		e.p = &twistPoint{}
   206  	}
   207  	e.p.Mul(twistGen, k)
   208  	return e
   209  }
   210  
   211  // ScalarMult sets e to a*k and then returns e.
   212  func (e *G2) ScalarMult(a *G2, k *big.Int) *G2 {
   213  	if e.p == nil {
   214  		e.p = &twistPoint{}
   215  	}
   216  	e.p.Mul(a.p, k)
   217  	return e
   218  }
   219  
   220  // Add sets e to a+b and then returns e.
   221  func (e *G2) Add(a, b *G2) *G2 {
   222  	if e.p == nil {
   223  		e.p = &twistPoint{}
   224  	}
   225  	e.p.Add(a.p, b.p)
   226  	return e
   227  }
   228  
   229  // Neg sets e to -a and then returns e.
   230  func (e *G2) Neg(a *G2) *G2 {
   231  	if e.p == nil {
   232  		e.p = &twistPoint{}
   233  	}
   234  	e.p.Neg(a.p)
   235  	return e
   236  }
   237  
   238  // Set sets e to a and then returns e.
   239  func (e *G2) Set(a *G2) *G2 {
   240  	if e.p == nil {
   241  		e.p = &twistPoint{}
   242  	}
   243  	e.p.Set(a.p)
   244  	return e
   245  }
   246  
   247  // Marshal converts e into a byte slice.
   248  func (e *G2) Marshal() []byte {
   249  	// Each value is a 256-bit number.
   250  	const numBytes = 256 / 8
   251  
   252  	if e.p == nil {
   253  		e.p = &twistPoint{}
   254  	}
   255  
   256  	e.p.MakeAffine()
   257  	ret := make([]byte, numBytes*4)
   258  	if e.p.IsInfinity() {
   259  		return ret
   260  	}
   261  	temp := &gfP{}
   262  
   263  	montDecode(temp, &e.p.x.x)
   264  	temp.Marshal(ret)
   265  	montDecode(temp, &e.p.x.y)
   266  	temp.Marshal(ret[numBytes:])
   267  	montDecode(temp, &e.p.y.x)
   268  	temp.Marshal(ret[2*numBytes:])
   269  	montDecode(temp, &e.p.y.y)
   270  	temp.Marshal(ret[3*numBytes:])
   271  
   272  	return ret
   273  }
   274  
   275  // Unmarshal sets e to the result of converting the output of Marshal back into
   276  // a group element and then returns e.
   277  func (e *G2) Unmarshal(m []byte) ([]byte, error) {
   278  	// Each value is a 256-bit number.
   279  	const numBytes = 256 / 8
   280  	if len(m) < 4*numBytes {
   281  		return nil, errors.New("bn256: not enough data")
   282  	}
   283  	// Unmarshal the points and check their caps
   284  	if e.p == nil {
   285  		e.p = &twistPoint{}
   286  	}
   287  	var err error
   288  	if err = e.p.x.x.Unmarshal(m); err != nil {
   289  		return nil, err
   290  	}
   291  	if err = e.p.x.y.Unmarshal(m[numBytes:]); err != nil {
   292  		return nil, err
   293  	}
   294  	if err = e.p.y.x.Unmarshal(m[2*numBytes:]); err != nil {
   295  		return nil, err
   296  	}
   297  	if err = e.p.y.y.Unmarshal(m[3*numBytes:]); err != nil {
   298  		return nil, err
   299  	}
   300  	// Encode into Montgomery form and ensure it's on the curve
   301  	montEncode(&e.p.x.x, &e.p.x.x)
   302  	montEncode(&e.p.x.y, &e.p.x.y)
   303  	montEncode(&e.p.y.x, &e.p.y.x)
   304  	montEncode(&e.p.y.y, &e.p.y.y)
   305  
   306  	if e.p.x.IsZero() && e.p.y.IsZero() {
   307  		// This is the point at infinity.
   308  		e.p.y.SetOne()
   309  		e.p.z.SetZero()
   310  		e.p.t.SetZero()
   311  	} else {
   312  		e.p.z.SetOne()
   313  		e.p.t.SetOne()
   314  
   315  		if !e.p.IsOnCurve() {
   316  			return nil, errors.New("bn256: malformed point")
   317  		}
   318  	}
   319  	return m[4*numBytes:], nil
   320  }
   321  
   322  // GT is an abstract cyclic group. The zero value is suitable for use as the
   323  // output of an operation, but cannot be used as an input.
   324  type GT struct {
   325  	p *gfP12
   326  }
   327  
   328  // Pair calculates an Optimal Ate pairing.
   329  func Pair(g1 *G1, g2 *G2) *GT {
   330  	return &GT{optimalAte(g2.p, g1.p)}
   331  }
   332  
   333  // PairingCheck calculates the Optimal Ate pairing for a set of points.
   334  func PairingCheck(a []*G1, b []*G2) bool {
   335  	acc := new(gfP12)
   336  	acc.SetOne()
   337  
   338  	for i := 0; i < len(a); i++ {
   339  		if a[i].p.IsInfinity() || b[i].p.IsInfinity() {
   340  			continue
   341  		}
   342  		acc.Mul(acc, miller(b[i].p, a[i].p))
   343  	}
   344  	return finalExponentiation(acc).IsOne()
   345  }
   346  
   347  // Miller applies Miller's algorithm, which is a bilinear function from the
   348  // source groups to F_p^12. Miller(g1, g2).Finalize() is equivalent to Pair(g1,
   349  // g2).
   350  func Miller(g1 *G1, g2 *G2) *GT {
   351  	return &GT{miller(g2.p, g1.p)}
   352  }
   353  
   354  func (g *GT) String() string {
   355  	return "bn256.GT" + g.p.String()
   356  }
   357  
   358  // ScalarMult sets e to a*k and then returns e.
   359  func (e *GT) ScalarMult(a *GT, k *big.Int) *GT {
   360  	if e.p == nil {
   361  		e.p = &gfP12{}
   362  	}
   363  	e.p.Exp(a.p, k)
   364  	return e
   365  }
   366  
   367  // Add sets e to a+b and then returns e.
   368  func (e *GT) Add(a, b *GT) *GT {
   369  	if e.p == nil {
   370  		e.p = &gfP12{}
   371  	}
   372  	e.p.Mul(a.p, b.p)
   373  	return e
   374  }
   375  
   376  // Neg sets e to -a and then returns e.
   377  func (e *GT) Neg(a *GT) *GT {
   378  	if e.p == nil {
   379  		e.p = &gfP12{}
   380  	}
   381  	e.p.Conjugate(a.p)
   382  	return e
   383  }
   384  
   385  // Set sets e to a and then returns e.
   386  func (e *GT) Set(a *GT) *GT {
   387  	if e.p == nil {
   388  		e.p = &gfP12{}
   389  	}
   390  	e.p.Set(a.p)
   391  	return e
   392  }
   393  
   394  // Finalize is a linear function from F_p^12 to GT.
   395  func (e *GT) Finalize() *GT {
   396  	ret := finalExponentiation(e.p)
   397  	e.p.Set(ret)
   398  	return e
   399  }
   400  
   401  // Marshal converts e into a byte slice.
   402  func (e *GT) Marshal() []byte {
   403  	// Each value is a 256-bit number.
   404  	const numBytes = 256 / 8
   405  
   406  	if e.p == nil {
   407  		e.p = &gfP12{}
   408  		e.p.SetOne()
   409  	}
   410  
   411  	ret := make([]byte, numBytes*12)
   412  	temp := &gfP{}
   413  
   414  	montDecode(temp, &e.p.x.x.x)
   415  	temp.Marshal(ret)
   416  	montDecode(temp, &e.p.x.x.y)
   417  	temp.Marshal(ret[numBytes:])
   418  	montDecode(temp, &e.p.x.y.x)
   419  	temp.Marshal(ret[2*numBytes:])
   420  	montDecode(temp, &e.p.x.y.y)
   421  	temp.Marshal(ret[3*numBytes:])
   422  	montDecode(temp, &e.p.x.z.x)
   423  	temp.Marshal(ret[4*numBytes:])
   424  	montDecode(temp, &e.p.x.z.y)
   425  	temp.Marshal(ret[5*numBytes:])
   426  	montDecode(temp, &e.p.y.x.x)
   427  	temp.Marshal(ret[6*numBytes:])
   428  	montDecode(temp, &e.p.y.x.y)
   429  	temp.Marshal(ret[7*numBytes:])
   430  	montDecode(temp, &e.p.y.y.x)
   431  	temp.Marshal(ret[8*numBytes:])
   432  	montDecode(temp, &e.p.y.y.y)
   433  	temp.Marshal(ret[9*numBytes:])
   434  	montDecode(temp, &e.p.y.z.x)
   435  	temp.Marshal(ret[10*numBytes:])
   436  	montDecode(temp, &e.p.y.z.y)
   437  	temp.Marshal(ret[11*numBytes:])
   438  
   439  	return ret
   440  }
   441  
   442  // Unmarshal sets e to the result of converting the output of Marshal back into
   443  // a group element and then returns e.
   444  func (e *GT) Unmarshal(m []byte) ([]byte, error) {
   445  	// Each value is a 256-bit number.
   446  	const numBytes = 256 / 8
   447  
   448  	if len(m) < 12*numBytes {
   449  		return nil, errors.New("bn256: not enough data")
   450  	}
   451  
   452  	if e.p == nil {
   453  		e.p = &gfP12{}
   454  	}
   455  
   456  	var err error
   457  	if err = e.p.x.x.x.Unmarshal(m); err != nil {
   458  		return nil, err
   459  	}
   460  	if err = e.p.x.x.y.Unmarshal(m[numBytes:]); err != nil {
   461  		return nil, err
   462  	}
   463  	if err = e.p.x.y.x.Unmarshal(m[2*numBytes:]); err != nil {
   464  		return nil, err
   465  	}
   466  	if err = e.p.x.y.y.Unmarshal(m[3*numBytes:]); err != nil {
   467  		return nil, err
   468  	}
   469  	if err = e.p.x.z.x.Unmarshal(m[4*numBytes:]); err != nil {
   470  		return nil, err
   471  	}
   472  	if err = e.p.x.z.y.Unmarshal(m[5*numBytes:]); err != nil {
   473  		return nil, err
   474  	}
   475  	if err = e.p.y.x.x.Unmarshal(m[6*numBytes:]); err != nil {
   476  		return nil, err
   477  	}
   478  	if err = e.p.y.x.y.Unmarshal(m[7*numBytes:]); err != nil {
   479  		return nil, err
   480  	}
   481  	if err = e.p.y.y.x.Unmarshal(m[8*numBytes:]); err != nil {
   482  		return nil, err
   483  	}
   484  	if err = e.p.y.y.y.Unmarshal(m[9*numBytes:]); err != nil {
   485  		return nil, err
   486  	}
   487  	if err = e.p.y.z.x.Unmarshal(m[10*numBytes:]); err != nil {
   488  		return nil, err
   489  	}
   490  	if err = e.p.y.z.y.Unmarshal(m[11*numBytes:]); err != nil {
   491  		return nil, err
   492  	}
   493  	montEncode(&e.p.x.x.x, &e.p.x.x.x)
   494  	montEncode(&e.p.x.x.y, &e.p.x.x.y)
   495  	montEncode(&e.p.x.y.x, &e.p.x.y.x)
   496  	montEncode(&e.p.x.y.y, &e.p.x.y.y)
   497  	montEncode(&e.p.x.z.x, &e.p.x.z.x)
   498  	montEncode(&e.p.x.z.y, &e.p.x.z.y)
   499  	montEncode(&e.p.y.x.x, &e.p.y.x.x)
   500  	montEncode(&e.p.y.x.y, &e.p.y.x.y)
   501  	montEncode(&e.p.y.y.x, &e.p.y.y.x)
   502  	montEncode(&e.p.y.y.y, &e.p.y.y.y)
   503  	montEncode(&e.p.y.z.x, &e.p.y.z.x)
   504  	montEncode(&e.p.y.z.y, &e.p.y.z.y)
   505  
   506  	return m[12*numBytes:], nil
   507  }