gitee.com/lh-her-team/common@v1.5.1/crypto/hibe/hibe_amd64/hibe/bn256/bn256.go (about)

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