github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/crypto/bn256/cloudflare/bn256.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:36</date>
    10  //</624342624433606656>
    11  
    12  //包BN256以128位安全性实现特定的双线性组
    13  //水平。
    14  //
    15  //双线性群是许多新的密码协议的基础
    16  //在过去的十年中被提议。它们由三组组成
    17  //(g_、g櫒和gt)这样就存在一个函数e(g_,g櫒)=gt_(其中g_
    18  //是相应组的生成器)。这个函数称为配对
    19  //功能。
    20  //
    21  //这个包专门实现了256位以上的最佳ATE配对
    22  //Barreto-Naehrig曲线,如中所述
    23  //http://cryptojedi.org/papers/dclxvi-20100714.pdf.它的输出是兼容的
    24  //以及本文中描述的实现。
    25  package bn256
    26  
    27  import (
    28  	"crypto/rand"
    29  	"errors"
    30  	"io"
    31  	"math/big"
    32  )
    33  
    34  func randomK(r io.Reader) (k *big.Int, err error) {
    35  	for {
    36  		k, err = rand.Int(r, Order)
    37  		if k.Sign() > 0 || err != nil {
    38  			return
    39  		}
    40  	}
    41  }
    42  
    43  //g1是一个抽象的循环群。零值适合用作
    44  //操作的输出,但不能用作输入。
    45  type G1 struct {
    46  	p *curvePoint
    47  }
    48  
    49  //randomg1返回x和g_,其中x是从r读取的随机非零数字。
    50  func RandomG1(r io.Reader) (*big.Int, *G1, error) {
    51  	k, err := randomK(r)
    52  	if err != nil {
    53  		return nil, nil, err
    54  	}
    55  
    56  	return k, new(G1).ScalarBaseMult(k), nil
    57  }
    58  
    59  func (g *G1) String() string {
    60  	return "bn256.G1" + g.p.String()
    61  }
    62  
    63  //scalarbasemult将e设置为g*k,其中g是组的生成器,然后
    64  //返回E
    65  func (e *G1) ScalarBaseMult(k *big.Int) *G1 {
    66  	if e.p == nil {
    67  		e.p = &curvePoint{}
    68  	}
    69  	e.p.Mul(curveGen, k)
    70  	return e
    71  }
    72  
    73  //scalarmult将e设置为*k,然后返回e。
    74  func (e *G1) ScalarMult(a *G1, k *big.Int) *G1 {
    75  	if e.p == nil {
    76  		e.p = &curvePoint{}
    77  	}
    78  	e.p.Mul(a.p, k)
    79  	return e
    80  }
    81  
    82  //将集合e添加到a+b,然后返回e。
    83  func (e *G1) Add(a, b *G1) *G1 {
    84  	if e.p == nil {
    85  		e.p = &curvePoint{}
    86  	}
    87  	e.p.Add(a.p, b.p)
    88  	return e
    89  }
    90  
    91  //neg将e设置为-a,然后返回e。
    92  func (e *G1) Neg(a *G1) *G1 {
    93  	if e.p == nil {
    94  		e.p = &curvePoint{}
    95  	}
    96  	e.p.Neg(a.p)
    97  	return e
    98  }
    99  
   100  //将e设置为a,然后返回e。
   101  func (e *G1) Set(a *G1) *G1 {
   102  	if e.p == nil {
   103  		e.p = &curvePoint{}
   104  	}
   105  	e.p.Set(a.p)
   106  	return e
   107  }
   108  
   109  //封送将E转换为字节片。
   110  func (e *G1) Marshal() []byte {
   111  //每个值都是256位数字。
   112  	const numBytes = 256 / 8
   113  
   114  	e.p.MakeAffine()
   115  	ret := make([]byte, numBytes*2)
   116  	if e.p.IsInfinity() {
   117  		return ret
   118  	}
   119  	temp := &gfP{}
   120  
   121  	montDecode(temp, &e.p.x)
   122  	temp.Marshal(ret)
   123  	montDecode(temp, &e.p.y)
   124  	temp.Marshal(ret[numBytes:])
   125  
   126  	return ret
   127  }
   128  
   129  //unmarshal将e设置为将marshal的输出转换回
   130  //一个group元素,然后返回e。
   131  func (e *G1) Unmarshal(m []byte) ([]byte, error) {
   132  //每个值都是256位数字。
   133  	const numBytes = 256 / 8
   134  	if len(m) < 2*numBytes {
   135  		return nil, errors.New("bn256: not enough data")
   136  	}
   137  //解开这些点并检查它们的帽子
   138  	if e.p == nil {
   139  		e.p = &curvePoint{}
   140  	} else {
   141  		e.p.x, e.p.y = gfP{0}, gfP{0}
   142  	}
   143  	var err error
   144  	if err = e.p.x.Unmarshal(m); err != nil {
   145  		return nil, err
   146  	}
   147  	if err = e.p.y.Unmarshal(m[numBytes:]); err != nil {
   148  		return nil, err
   149  	}
   150  //编码成蒙哥马利形式并确保它在曲线上
   151  	montEncode(&e.p.x, &e.p.x)
   152  	montEncode(&e.p.y, &e.p.y)
   153  
   154  	zero := gfP{0}
   155  	if e.p.x == zero && e.p.y == zero {
   156  //这是无穷远的点。
   157  		e.p.y = *newGFp(1)
   158  		e.p.z = gfP{0}
   159  		e.p.t = gfP{0}
   160  	} else {
   161  		e.p.z = *newGFp(1)
   162  		e.p.t = *newGFp(1)
   163  
   164  		if !e.p.IsOnCurve() {
   165  			return nil, errors.New("bn256: malformed point")
   166  		}
   167  	}
   168  	return m[2*numBytes:], nil
   169  }
   170  
   171  //g2是一个抽象的循环群。零值适合用作
   172  //操作的输出,但不能用作输入。
   173  type G2 struct {
   174  	p *twistPoint
   175  }
   176  
   177  //randomg2返回x和g,其中x是从r读取的随机非零数字。
   178  func RandomG2(r io.Reader) (*big.Int, *G2, error) {
   179  	k, err := randomK(r)
   180  	if err != nil {
   181  		return nil, nil, err
   182  	}
   183  
   184  	return k, new(G2).ScalarBaseMult(k), nil
   185  }
   186  
   187  func (e *G2) String() string {
   188  	return "bn256.G2" + e.p.String()
   189  }
   190  
   191  //scalarbasemult将e设置为g*k,其中g是组的生成器,然后
   192  //退回。
   193  func (e *G2) ScalarBaseMult(k *big.Int) *G2 {
   194  	if e.p == nil {
   195  		e.p = &twistPoint{}
   196  	}
   197  	e.p.Mul(twistGen, k)
   198  	return e
   199  }
   200  
   201  //scalarmult将e设置为*k,然后返回e。
   202  func (e *G2) ScalarMult(a *G2, k *big.Int) *G2 {
   203  	if e.p == nil {
   204  		e.p = &twistPoint{}
   205  	}
   206  	e.p.Mul(a.p, k)
   207  	return e
   208  }
   209  
   210  //将集合e添加到a+b,然后返回e。
   211  func (e *G2) Add(a, b *G2) *G2 {
   212  	if e.p == nil {
   213  		e.p = &twistPoint{}
   214  	}
   215  	e.p.Add(a.p, b.p)
   216  	return e
   217  }
   218  
   219  //neg将e设置为-a,然后返回e。
   220  func (e *G2) Neg(a *G2) *G2 {
   221  	if e.p == nil {
   222  		e.p = &twistPoint{}
   223  	}
   224  	e.p.Neg(a.p)
   225  	return e
   226  }
   227  
   228  //将e设置为a,然后返回e。
   229  func (e *G2) Set(a *G2) *G2 {
   230  	if e.p == nil {
   231  		e.p = &twistPoint{}
   232  	}
   233  	e.p.Set(a.p)
   234  	return e
   235  }
   236  
   237  //封送将E转换为字节片。
   238  func (e *G2) Marshal() []byte {
   239  //每个值都是256位数字。
   240  	const numBytes = 256 / 8
   241  
   242  	if e.p == nil {
   243  		e.p = &twistPoint{}
   244  	}
   245  
   246  	e.p.MakeAffine()
   247  	ret := make([]byte, numBytes*4)
   248  	if e.p.IsInfinity() {
   249  		return ret
   250  	}
   251  	temp := &gfP{}
   252  
   253  	montDecode(temp, &e.p.x.x)
   254  	temp.Marshal(ret)
   255  	montDecode(temp, &e.p.x.y)
   256  	temp.Marshal(ret[numBytes:])
   257  	montDecode(temp, &e.p.y.x)
   258  	temp.Marshal(ret[2*numBytes:])
   259  	montDecode(temp, &e.p.y.y)
   260  	temp.Marshal(ret[3*numBytes:])
   261  
   262  	return ret
   263  }
   264  
   265  //unmarshal将e设置为将marshal的输出转换回
   266  //一个group元素,然后返回e。
   267  func (e *G2) Unmarshal(m []byte) ([]byte, error) {
   268  //每个值都是256位数字。
   269  	const numBytes = 256 / 8
   270  	if len(m) < 4*numBytes {
   271  		return nil, errors.New("bn256: not enough data")
   272  	}
   273  //解开这些点并检查它们的帽子
   274  	if e.p == nil {
   275  		e.p = &twistPoint{}
   276  	}
   277  	var err error
   278  	if err = e.p.x.x.Unmarshal(m); err != nil {
   279  		return nil, err
   280  	}
   281  	if err = e.p.x.y.Unmarshal(m[numBytes:]); err != nil {
   282  		return nil, err
   283  	}
   284  	if err = e.p.y.x.Unmarshal(m[2*numBytes:]); err != nil {
   285  		return nil, err
   286  	}
   287  	if err = e.p.y.y.Unmarshal(m[3*numBytes:]); err != nil {
   288  		return nil, err
   289  	}
   290  //编码成蒙哥马利形式并确保它在曲线上
   291  	montEncode(&e.p.x.x, &e.p.x.x)
   292  	montEncode(&e.p.x.y, &e.p.x.y)
   293  	montEncode(&e.p.y.x, &e.p.y.x)
   294  	montEncode(&e.p.y.y, &e.p.y.y)
   295  
   296  	if e.p.x.IsZero() && e.p.y.IsZero() {
   297  //这是无穷远的点。
   298  		e.p.y.SetOne()
   299  		e.p.z.SetZero()
   300  		e.p.t.SetZero()
   301  	} else {
   302  		e.p.z.SetOne()
   303  		e.p.t.SetOne()
   304  
   305  		if !e.p.IsOnCurve() {
   306  			return nil, errors.New("bn256: malformed point")
   307  		}
   308  	}
   309  	return m[4*numBytes:], nil
   310  }
   311  
   312  //gt是一个抽象循环群。零值适合用作
   313  //操作的输出,但不能用作输入。
   314  type GT struct {
   315  	p *gfP12
   316  }
   317  
   318  //配对计算最佳ATE配对。
   319  func Pair(g1 *G1, g2 *G2) *GT {
   320  	return &GT{optimalAte(g2.p, g1.p)}
   321  }
   322  
   323  //pairingcheck计算一组点的最佳ate对。
   324  func PairingCheck(a []*G1, b []*G2) bool {
   325  	acc := new(gfP12)
   326  	acc.SetOne()
   327  
   328  	for i := 0; i < len(a); i++ {
   329  		if a[i].p.IsInfinity() || b[i].p.IsInfinity() {
   330  			continue
   331  		}
   332  		acc.Mul(acc, miller(b[i].p, a[i].p))
   333  	}
   334  	return finalExponentiation(acc).IsOne()
   335  }
   336  
   337  //Miller应用Miller算法,它是
   338  //源组到f_p^12。Miller(g1,g2).Finalize()等价于pair(g1,g2)。
   339  //G2)。
   340  func Miller(g1 *G1, g2 *G2) *GT {
   341  	return &GT{miller(g2.p, g1.p)}
   342  }
   343  
   344  func (g *GT) String() string {
   345  	return "bn256.GT" + g.p.String()
   346  }
   347  
   348  //scalarmult将e设置为*k,然后返回e。
   349  func (e *GT) ScalarMult(a *GT, k *big.Int) *GT {
   350  	if e.p == nil {
   351  		e.p = &gfP12{}
   352  	}
   353  	e.p.Exp(a.p, k)
   354  	return e
   355  }
   356  
   357  //将集合e添加到a+b,然后返回e。
   358  func (e *GT) Add(a, b *GT) *GT {
   359  	if e.p == nil {
   360  		e.p = &gfP12{}
   361  	}
   362  	e.p.Mul(a.p, b.p)
   363  	return e
   364  }
   365  
   366  //neg将e设置为-a,然后返回e。
   367  func (e *GT) Neg(a *GT) *GT {
   368  	if e.p == nil {
   369  		e.p = &gfP12{}
   370  	}
   371  	e.p.Conjugate(a.p)
   372  	return e
   373  }
   374  
   375  //将e设置为a,然后返回e。
   376  func (e *GT) Set(a *GT) *GT {
   377  	if e.p == nil {
   378  		e.p = &gfP12{}
   379  	}
   380  	e.p.Set(a.p)
   381  	return e
   382  }
   383  
   384  //Finalize是一个从f_p^12到gt的线性函数。
   385  func (e *GT) Finalize() *GT {
   386  	ret := finalExponentiation(e.p)
   387  	e.p.Set(ret)
   388  	return e
   389  }
   390  
   391  //封送将E转换为字节片。
   392  func (e *GT) Marshal() []byte {
   393  //每个值都是256位数字。
   394  	const numBytes = 256 / 8
   395  
   396  	ret := make([]byte, numBytes*12)
   397  	temp := &gfP{}
   398  
   399  	montDecode(temp, &e.p.x.x.x)
   400  	temp.Marshal(ret)
   401  	montDecode(temp, &e.p.x.x.y)
   402  	temp.Marshal(ret[numBytes:])
   403  	montDecode(temp, &e.p.x.y.x)
   404  	temp.Marshal(ret[2*numBytes:])
   405  	montDecode(temp, &e.p.x.y.y)
   406  	temp.Marshal(ret[3*numBytes:])
   407  	montDecode(temp, &e.p.x.z.x)
   408  	temp.Marshal(ret[4*numBytes:])
   409  	montDecode(temp, &e.p.x.z.y)
   410  	temp.Marshal(ret[5*numBytes:])
   411  	montDecode(temp, &e.p.y.x.x)
   412  	temp.Marshal(ret[6*numBytes:])
   413  	montDecode(temp, &e.p.y.x.y)
   414  	temp.Marshal(ret[7*numBytes:])
   415  	montDecode(temp, &e.p.y.y.x)
   416  	temp.Marshal(ret[8*numBytes:])
   417  	montDecode(temp, &e.p.y.y.y)
   418  	temp.Marshal(ret[9*numBytes:])
   419  	montDecode(temp, &e.p.y.z.x)
   420  	temp.Marshal(ret[10*numBytes:])
   421  	montDecode(temp, &e.p.y.z.y)
   422  	temp.Marshal(ret[11*numBytes:])
   423  
   424  	return ret
   425  }
   426  
   427  //unmarshal将e设置为将marshal的输出转换回
   428  //一个group元素,然后返回e。
   429  func (e *GT) Unmarshal(m []byte) ([]byte, error) {
   430  //每个值都是256位数字。
   431  	const numBytes = 256 / 8
   432  
   433  	if len(m) < 12*numBytes {
   434  		return nil, errors.New("bn256: not enough data")
   435  	}
   436  
   437  	if e.p == nil {
   438  		e.p = &gfP12{}
   439  	}
   440  
   441  	var err error
   442  	if err = e.p.x.x.x.Unmarshal(m); err != nil {
   443  		return nil, err
   444  	}
   445  	if err = e.p.x.x.y.Unmarshal(m[numBytes:]); err != nil {
   446  		return nil, err
   447  	}
   448  	if err = e.p.x.y.x.Unmarshal(m[2*numBytes:]); err != nil {
   449  		return nil, err
   450  	}
   451  	if err = e.p.x.y.y.Unmarshal(m[3*numBytes:]); err != nil {
   452  		return nil, err
   453  	}
   454  	if err = e.p.x.z.x.Unmarshal(m[4*numBytes:]); err != nil {
   455  		return nil, err
   456  	}
   457  	if err = e.p.x.z.y.Unmarshal(m[5*numBytes:]); err != nil {
   458  		return nil, err
   459  	}
   460  	if err = e.p.y.x.x.Unmarshal(m[6*numBytes:]); err != nil {
   461  		return nil, err
   462  	}
   463  	if err = e.p.y.x.y.Unmarshal(m[7*numBytes:]); err != nil {
   464  		return nil, err
   465  	}
   466  	if err = e.p.y.y.x.Unmarshal(m[8*numBytes:]); err != nil {
   467  		return nil, err
   468  	}
   469  	if err = e.p.y.y.y.Unmarshal(m[9*numBytes:]); err != nil {
   470  		return nil, err
   471  	}
   472  	if err = e.p.y.z.x.Unmarshal(m[10*numBytes:]); err != nil {
   473  		return nil, err
   474  	}
   475  	if err = e.p.y.z.y.Unmarshal(m[11*numBytes:]); err != nil {
   476  		return nil, err
   477  	}
   478  	montEncode(&e.p.x.x.x, &e.p.x.x.x)
   479  	montEncode(&e.p.x.x.y, &e.p.x.x.y)
   480  	montEncode(&e.p.x.y.x, &e.p.x.y.x)
   481  	montEncode(&e.p.x.y.y, &e.p.x.y.y)
   482  	montEncode(&e.p.x.z.x, &e.p.x.z.x)
   483  	montEncode(&e.p.x.z.y, &e.p.x.z.y)
   484  	montEncode(&e.p.y.x.x, &e.p.y.x.x)
   485  	montEncode(&e.p.y.x.y, &e.p.y.x.y)
   486  	montEncode(&e.p.y.y.x, &e.p.y.y.x)
   487  	montEncode(&e.p.y.y.y, &e.p.y.y.y)
   488  	montEncode(&e.p.y.z.x, &e.p.y.z.x)
   489  	montEncode(&e.p.y.z.y, &e.p.y.z.y)
   490  
   491  	return m[12*numBytes:], nil
   492  }
   493