github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/crypto/bn256/google/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  //版权所有2012 Go作者。版权所有。
    10  //此源代码的使用受BSD样式的控制
    11  //可以在许可文件中找到的许可证。
    12  
    13  //包BN256实现特定的双线性组。
    14  //
    15  //双线性群是许多新密码协议的基础。
    16  //在过去的十年里提出的。它们由三个组成
    17  //组(g_、g嫪和gt),以便存在一个函数e(g_、g嫪)=gt_
    18  //(其中g_是相应组的生成器)。该函数被调用
    19  //配对功能。
    20  //
    21  //这个包专门实现了256位以上的最佳ATE配对
    22  //Barreto-Naehrig曲线,如中所述
    23  //http://cryptojedi.org/papers/dclxvi-20100714.pdf.它的输出是兼容的
    24  //以及本文中描述的实现。
    25  //
    26  //(此软件包以前声称在128位安全级别下运行。
    27  //然而,最近对攻击的改进意味着这不再是真的了。见
    28  //https://moderncrypto.org/mail archive/curves/2016/000740.html.网址)
    29  package bn256
    30  
    31  import (
    32  	"crypto/rand"
    33  	"errors"
    34  	"io"
    35  	"math/big"
    36  )
    37  
    38  //bug(agl):这个实现不是固定的时间。
    39  //todo(agl):保持gf(p²)元素为蒙古语形式。
    40  
    41  //g1是一个抽象的循环群。零值适合用作
    42  //操作的输出,但不能用作输入。
    43  type G1 struct {
    44  	p *curvePoint
    45  }
    46  
    47  //randomg1返回x和g_,其中x是从r读取的随机非零数字。
    48  func RandomG1(r io.Reader) (*big.Int, *G1, error) {
    49  	var k *big.Int
    50  	var err error
    51  
    52  	for {
    53  		k, err = rand.Int(r, Order)
    54  		if err != nil {
    55  			return nil, nil, err
    56  		}
    57  		if k.Sign() > 0 {
    58  			break
    59  		}
    60  	}
    61  
    62  	return k, new(G1).ScalarBaseMult(k), nil
    63  }
    64  
    65  func (e *G1) String() string {
    66  	return "bn256.G1" + e.p.String()
    67  }
    68  
    69  //curve points以大整数返回p的曲线点
    70  func (e *G1) CurvePoints() (*big.Int, *big.Int, *big.Int, *big.Int) {
    71  	return e.p.x, e.p.y, e.p.z, e.p.t
    72  }
    73  
    74  //scalarbasemult将e设置为g*k,其中g是组的生成器,并且
    75  //然后返回E。
    76  func (e *G1) ScalarBaseMult(k *big.Int) *G1 {
    77  	if e.p == nil {
    78  		e.p = newCurvePoint(nil)
    79  	}
    80  	e.p.Mul(curveGen, k, new(bnPool))
    81  	return e
    82  }
    83  
    84  //scalarmult将e设置为*k,然后返回e。
    85  func (e *G1) ScalarMult(a *G1, k *big.Int) *G1 {
    86  	if e.p == nil {
    87  		e.p = newCurvePoint(nil)
    88  	}
    89  	e.p.Mul(a.p, k, new(bnPool))
    90  	return e
    91  }
    92  
    93  //将集合e添加到a+b,然后返回e。
    94  //bug(agl):此函数不完整:a==b失败。
    95  func (e *G1) Add(a, b *G1) *G1 {
    96  	if e.p == nil {
    97  		e.p = newCurvePoint(nil)
    98  	}
    99  	e.p.Add(a.p, b.p, new(bnPool))
   100  	return e
   101  }
   102  
   103  //neg将e设置为-a,然后返回e。
   104  func (e *G1) Neg(a *G1) *G1 {
   105  	if e.p == nil {
   106  		e.p = newCurvePoint(nil)
   107  	}
   108  	e.p.Negative(a.p)
   109  	return e
   110  }
   111  
   112  //marshal将n转换为字节片。
   113  func (e *G1) Marshal() []byte {
   114  //每个值都是256位数字。
   115  	const numBytes = 256 / 8
   116  
   117  	if e.p.IsInfinity() {
   118  		return make([]byte, numBytes*2)
   119  	}
   120  
   121  	e.p.MakeAffine(nil)
   122  
   123  	xBytes := new(big.Int).Mod(e.p.x, P).Bytes()
   124  	yBytes := new(big.Int).Mod(e.p.y, P).Bytes()
   125  
   126  	ret := make([]byte, numBytes*2)
   127  	copy(ret[1*numBytes-len(xBytes):], xBytes)
   128  	copy(ret[2*numBytes-len(yBytes):], yBytes)
   129  
   130  	return ret
   131  }
   132  
   133  //unmarshal将e设置为将marshal的输出转换回
   134  //一个group元素,然后返回e。
   135  func (e *G1) Unmarshal(m []byte) ([]byte, error) {
   136  //每个值都是256位数字。
   137  	const numBytes = 256 / 8
   138  	if len(m) != 2*numBytes {
   139  		return nil, errors.New("bn256: not enough data")
   140  	}
   141  //解开这些点并检查它们的帽子
   142  	if e.p == nil {
   143  		e.p = newCurvePoint(nil)
   144  	}
   145  	e.p.x.SetBytes(m[0*numBytes : 1*numBytes])
   146  	if e.p.x.Cmp(P) >= 0 {
   147  		return nil, errors.New("bn256: coordinate exceeds modulus")
   148  	}
   149  	e.p.y.SetBytes(m[1*numBytes : 2*numBytes])
   150  	if e.p.y.Cmp(P) >= 0 {
   151  		return nil, errors.New("bn256: coordinate exceeds modulus")
   152  	}
   153  //确保点在曲线上
   154  	if e.p.x.Sign() == 0 && e.p.y.Sign() == 0 {
   155  //这是无穷远的点。
   156  		e.p.y.SetInt64(1)
   157  		e.p.z.SetInt64(0)
   158  		e.p.t.SetInt64(0)
   159  	} else {
   160  		e.p.z.SetInt64(1)
   161  		e.p.t.SetInt64(1)
   162  
   163  		if !e.p.IsOnCurve() {
   164  			return nil, errors.New("bn256: malformed point")
   165  		}
   166  	}
   167  	return m[2*numBytes:], nil
   168  }
   169  
   170  //g2是一个抽象的循环群。零值适合用作
   171  //操作的输出,但不能用作输入。
   172  type G2 struct {
   173  	p *twistPoint
   174  }
   175  
   176  //randomg1返回x和g,其中x是从r读取的随机非零数字。
   177  func RandomG2(r io.Reader) (*big.Int, *G2, error) {
   178  	var k *big.Int
   179  	var err error
   180  
   181  	for {
   182  		k, err = rand.Int(r, Order)
   183  		if err != nil {
   184  			return nil, nil, err
   185  		}
   186  		if k.Sign() > 0 {
   187  			break
   188  		}
   189  	}
   190  
   191  	return k, new(G2).ScalarBaseMult(k), nil
   192  }
   193  
   194  func (e *G2) String() string {
   195  	return "bn256.G2" + e.p.String()
   196  }
   197  
   198  //curve points返回p的曲线点,其中包括
   199  //以及曲线点的虚部。
   200  func (e *G2) CurvePoints() (*gfP2, *gfP2, *gfP2, *gfP2) {
   201  	return e.p.x, e.p.y, e.p.z, e.p.t
   202  }
   203  
   204  //scalarbasemult将e设置为g*k,其中g是组的生成器,并且
   205  //然后返回。
   206  func (e *G2) ScalarBaseMult(k *big.Int) *G2 {
   207  	if e.p == nil {
   208  		e.p = newTwistPoint(nil)
   209  	}
   210  	e.p.Mul(twistGen, k, new(bnPool))
   211  	return e
   212  }
   213  
   214  //scalarmult将e设置为*k,然后返回e。
   215  func (e *G2) ScalarMult(a *G2, k *big.Int) *G2 {
   216  	if e.p == nil {
   217  		e.p = newTwistPoint(nil)
   218  	}
   219  	e.p.Mul(a.p, k, new(bnPool))
   220  	return e
   221  }
   222  
   223  //将集合e添加到a+b,然后返回e。
   224  //bug(agl):此函数不完整:a==b失败。
   225  func (e *G2) Add(a, b *G2) *G2 {
   226  	if e.p == nil {
   227  		e.p = newTwistPoint(nil)
   228  	}
   229  	e.p.Add(a.p, b.p, new(bnPool))
   230  	return e
   231  }
   232  
   233  //marshal将n转换为字节片。
   234  func (n *G2) Marshal() []byte {
   235  //每个值都是256位数字。
   236  	const numBytes = 256 / 8
   237  
   238  	if n.p.IsInfinity() {
   239  		return make([]byte, numBytes*4)
   240  	}
   241  
   242  	n.p.MakeAffine(nil)
   243  
   244  	xxBytes := new(big.Int).Mod(n.p.x.x, P).Bytes()
   245  	xyBytes := new(big.Int).Mod(n.p.x.y, P).Bytes()
   246  	yxBytes := new(big.Int).Mod(n.p.y.x, P).Bytes()
   247  	yyBytes := new(big.Int).Mod(n.p.y.y, P).Bytes()
   248  
   249  	ret := make([]byte, numBytes*4)
   250  	copy(ret[1*numBytes-len(xxBytes):], xxBytes)
   251  	copy(ret[2*numBytes-len(xyBytes):], xyBytes)
   252  	copy(ret[3*numBytes-len(yxBytes):], yxBytes)
   253  	copy(ret[4*numBytes-len(yyBytes):], yyBytes)
   254  
   255  	return ret
   256  }
   257  
   258  //unmarshal将e设置为将marshal的输出转换回
   259  //一个group元素,然后返回e。
   260  func (e *G2) Unmarshal(m []byte) ([]byte, error) {
   261  //每个值都是256位数字。
   262  	const numBytes = 256 / 8
   263  	if len(m) != 4*numBytes {
   264  		return nil, errors.New("bn256: not enough data")
   265  	}
   266  //解开这些点并检查它们的帽子
   267  	if e.p == nil {
   268  		e.p = newTwistPoint(nil)
   269  	}
   270  	e.p.x.x.SetBytes(m[0*numBytes : 1*numBytes])
   271  	if e.p.x.x.Cmp(P) >= 0 {
   272  		return nil, errors.New("bn256: coordinate exceeds modulus")
   273  	}
   274  	e.p.x.y.SetBytes(m[1*numBytes : 2*numBytes])
   275  	if e.p.x.y.Cmp(P) >= 0 {
   276  		return nil, errors.New("bn256: coordinate exceeds modulus")
   277  	}
   278  	e.p.y.x.SetBytes(m[2*numBytes : 3*numBytes])
   279  	if e.p.y.x.Cmp(P) >= 0 {
   280  		return nil, errors.New("bn256: coordinate exceeds modulus")
   281  	}
   282  	e.p.y.y.SetBytes(m[3*numBytes : 4*numBytes])
   283  	if e.p.y.y.Cmp(P) >= 0 {
   284  		return nil, errors.New("bn256: coordinate exceeds modulus")
   285  	}
   286  //确保点在曲线上
   287  	if e.p.x.x.Sign() == 0 &&
   288  		e.p.x.y.Sign() == 0 &&
   289  		e.p.y.x.Sign() == 0 &&
   290  		e.p.y.y.Sign() == 0 {
   291  //这是无穷远的点。
   292  		e.p.y.SetOne()
   293  		e.p.z.SetZero()
   294  		e.p.t.SetZero()
   295  	} else {
   296  		e.p.z.SetOne()
   297  		e.p.t.SetOne()
   298  
   299  		if !e.p.IsOnCurve() {
   300  			return nil, errors.New("bn256: malformed point")
   301  		}
   302  	}
   303  	return m[4*numBytes:], nil
   304  }
   305  
   306  //gt是一个抽象循环群。零值适合用作
   307  //操作的输出,但不能用作输入。
   308  type GT struct {
   309  	p *gfP12
   310  }
   311  
   312  func (g *GT) String() string {
   313  	return "bn256.GT" + g.p.String()
   314  }
   315  
   316  //scalarmult将e设置为*k,然后返回e。
   317  func (e *GT) ScalarMult(a *GT, k *big.Int) *GT {
   318  	if e.p == nil {
   319  		e.p = newGFp12(nil)
   320  	}
   321  	e.p.Exp(a.p, k, new(bnPool))
   322  	return e
   323  }
   324  
   325  //将集合e添加到a+b,然后返回e。
   326  func (e *GT) Add(a, b *GT) *GT {
   327  	if e.p == nil {
   328  		e.p = newGFp12(nil)
   329  	}
   330  	e.p.Mul(a.p, b.p, new(bnPool))
   331  	return e
   332  }
   333  
   334  //neg将e设置为-a,然后返回e。
   335  func (e *GT) Neg(a *GT) *GT {
   336  	if e.p == nil {
   337  		e.p = newGFp12(nil)
   338  	}
   339  	e.p.Invert(a.p, new(bnPool))
   340  	return e
   341  }
   342  
   343  //marshal将n转换为字节片。
   344  func (n *GT) Marshal() []byte {
   345  	n.p.Minimal()
   346  
   347  	xxxBytes := n.p.x.x.x.Bytes()
   348  	xxyBytes := n.p.x.x.y.Bytes()
   349  	xyxBytes := n.p.x.y.x.Bytes()
   350  	xyyBytes := n.p.x.y.y.Bytes()
   351  	xzxBytes := n.p.x.z.x.Bytes()
   352  	xzyBytes := n.p.x.z.y.Bytes()
   353  	yxxBytes := n.p.y.x.x.Bytes()
   354  	yxyBytes := n.p.y.x.y.Bytes()
   355  	yyxBytes := n.p.y.y.x.Bytes()
   356  	yyyBytes := n.p.y.y.y.Bytes()
   357  	yzxBytes := n.p.y.z.x.Bytes()
   358  	yzyBytes := n.p.y.z.y.Bytes()
   359  
   360  //每个值都是256位数字。
   361  	const numBytes = 256 / 8
   362  
   363  	ret := make([]byte, numBytes*12)
   364  	copy(ret[1*numBytes-len(xxxBytes):], xxxBytes)
   365  	copy(ret[2*numBytes-len(xxyBytes):], xxyBytes)
   366  	copy(ret[3*numBytes-len(xyxBytes):], xyxBytes)
   367  	copy(ret[4*numBytes-len(xyyBytes):], xyyBytes)
   368  	copy(ret[5*numBytes-len(xzxBytes):], xzxBytes)
   369  	copy(ret[6*numBytes-len(xzyBytes):], xzyBytes)
   370  	copy(ret[7*numBytes-len(yxxBytes):], yxxBytes)
   371  	copy(ret[8*numBytes-len(yxyBytes):], yxyBytes)
   372  	copy(ret[9*numBytes-len(yyxBytes):], yyxBytes)
   373  	copy(ret[10*numBytes-len(yyyBytes):], yyyBytes)
   374  	copy(ret[11*numBytes-len(yzxBytes):], yzxBytes)
   375  	copy(ret[12*numBytes-len(yzyBytes):], yzyBytes)
   376  
   377  	return ret
   378  }
   379  
   380  //unmarshal将e设置为将marshal的输出转换回
   381  //一个group元素,然后返回e。
   382  func (e *GT) Unmarshal(m []byte) (*GT, bool) {
   383  //每个值都是256位数字。
   384  	const numBytes = 256 / 8
   385  
   386  	if len(m) != 12*numBytes {
   387  		return nil, false
   388  	}
   389  
   390  	if e.p == nil {
   391  		e.p = newGFp12(nil)
   392  	}
   393  
   394  	e.p.x.x.x.SetBytes(m[0*numBytes : 1*numBytes])
   395  	e.p.x.x.y.SetBytes(m[1*numBytes : 2*numBytes])
   396  	e.p.x.y.x.SetBytes(m[2*numBytes : 3*numBytes])
   397  	e.p.x.y.y.SetBytes(m[3*numBytes : 4*numBytes])
   398  	e.p.x.z.x.SetBytes(m[4*numBytes : 5*numBytes])
   399  	e.p.x.z.y.SetBytes(m[5*numBytes : 6*numBytes])
   400  	e.p.y.x.x.SetBytes(m[6*numBytes : 7*numBytes])
   401  	e.p.y.x.y.SetBytes(m[7*numBytes : 8*numBytes])
   402  	e.p.y.y.x.SetBytes(m[8*numBytes : 9*numBytes])
   403  	e.p.y.y.y.SetBytes(m[9*numBytes : 10*numBytes])
   404  	e.p.y.z.x.SetBytes(m[10*numBytes : 11*numBytes])
   405  	e.p.y.z.y.SetBytes(m[11*numBytes : 12*numBytes])
   406  
   407  	return e, true
   408  }
   409  
   410  //配对计算最佳ATE配对。
   411  func Pair(g1 *G1, g2 *G2) *GT {
   412  	return &GT{optimalAte(g2.p, g1.p, new(bnPool))}
   413  }
   414  
   415  //pairingcheck计算一组点的最佳ate对。
   416  func PairingCheck(a []*G1, b []*G2) bool {
   417  	pool := new(bnPool)
   418  
   419  	acc := newGFp12(pool)
   420  	acc.SetOne()
   421  
   422  	for i := 0; i < len(a); i++ {
   423  		if a[i].p.IsInfinity() || b[i].p.IsInfinity() {
   424  			continue
   425  		}
   426  		acc.Mul(acc, miller(b[i].p, a[i].p, pool), pool)
   427  	}
   428  	ret := finalExponentiation(acc, pool)
   429  	acc.Put(pool)
   430  
   431  	return ret.IsOne()
   432  }
   433  
   434  //bnpool实现一个*big.int对象的小缓存,用于减少
   435  //处理期间进行的分配数。
   436  type bnPool struct {
   437  	bns   []*big.Int
   438  	count int
   439  }
   440  
   441  func (pool *bnPool) Get() *big.Int {
   442  	if pool == nil {
   443  		return new(big.Int)
   444  	}
   445  
   446  	pool.count++
   447  	l := len(pool.bns)
   448  	if l == 0 {
   449  		return new(big.Int)
   450  	}
   451  
   452  	bn := pool.bns[l-1]
   453  	pool.bns = pool.bns[:l-1]
   454  	return bn
   455  }
   456  
   457  func (pool *bnPool) Put(bn *big.Int) {
   458  	if pool == nil {
   459  		return
   460  	}
   461  	pool.bns = append(pool.bns, bn)
   462  	pool.count--
   463  }
   464  
   465  func (pool *bnPool) Count() int {
   466  	return pool.count
   467  }