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