github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/crypto/bn256/cloudflare/bn256.go (about)

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