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