github.com/hellobchain/newcryptosm@v0.0.0-20221019060107-edb949a317e9/sm9/sm9.go (about) 1 // Package sm9 implements a particular bilinear group at the 128-bit security 2 // level. 3 // 4 // Bilinear groups are the basis of many of the new cryptographic protocols that 5 // have been proposed over the past decade. They consist of a triplet of groups 6 // (G₁, G₂ and GT) such that there exists a function e(g₁ˣ,g₂ʸ)=gTˣʸ (where gₓ 7 // is a generator of the respective group). That function is called a pairing 8 // function. 9 // 10 // This package specifically implements the Optimal Ate pairing over a 256-bit 11 // Barreto-Naehrig curve as described in 12 // http://cryptojedi.org/papers/dclxvi-20100714.pdf. Its output is compatible 13 // with the implementation described in that paper. 14 package sm9 15 16 import ( 17 "crypto/rand" 18 "errors" 19 "io" 20 "math/big" 21 ) 22 23 func randomK(r io.Reader) (k *big.Int, err error) { 24 for { 25 k, err = rand.Int(r, Order) 26 if k.Sign() > 0 || err != nil { 27 return 28 } 29 } 30 31 return 32 } 33 34 // G1 is an abstract cyclic group. The zero value is suitable for use as the 35 // output of an operation, but cannot be used as an input. 36 type G1 struct { 37 p *curvePoint 38 } 39 40 // RandomG1 returns x and g₁ˣ where x is a random, non-zero number read from r. 41 func RandomG1(r io.Reader) (*big.Int, *G1, error) { 42 k, err := randomK(r) 43 if err != nil { 44 return nil, nil, err 45 } 46 47 return k, new(G1).ScalarBaseMult(k), nil 48 } 49 50 func (g *G1) String() string { 51 return "sm9.G1" + g.p.String() 52 } 53 54 func (e *G1) CombinedMult(H *G1, baseScalar, scalar *big.Int) *G1 { 55 if e.p == nil { 56 e.p = &curvePoint{} 57 } 58 e.p.sm9CombinedMult(H.p, baseScalar, scalar) 59 return e 60 } 61 62 // ScalarBaseMult sets e to g*k where g is the generator of the group and then 63 // returns e. 64 func (e *G1) ScalarBaseMult(k *big.Int) *G1 { 65 if e.p == nil { 66 e.p = &curvePoint{} 67 } 68 e.p.Mul(curveGen, k) 69 return e 70 } 71 72 // ScalarMult sets e to a*k and then returns e. 73 func (e *G1) ScalarMult(a *G1, k *big.Int) *G1 { 74 if e.p == nil { 75 e.p = &curvePoint{} 76 } 77 e.p.Mul(a.p, k) 78 return e 79 } 80 81 /******************simon add 1220 for homo******************** 82 //copy from zhang 83 /* 84 func Test_g2() { 85 e1, e2 := &G2{}, &G2{} 86 lambda, _ := new(big.Int).SetString("b640000002a3a6eff003ab4ff0477961e1edaee07e84c2d0b978eb1109153e3f", 16) 87 88 if e1.p == nil { 89 e1.p = &twistPoint{} 90 } 91 92 if e2.p == nil { 93 e2.p = &twistPoint{} 94 } 95 e1.p.Mul2(twistGen, lambda) 96 y := e1.Marshal() 97 fmt.Printf("G2 lambda: %x\n", y) 98 99 e2.p.x.MulScalar(&twistGen.x, beta) 100 e2.p.y.Set(&twistGen.y) 101 e2.p.z.Set(&twistGen.z) 102 e2.p.t.Set(&twistGen.t) 103 y2 := e2.Marshal() 104 fmt.Printf("G2 Beta: %x\n", y2) 105 106 } 107 108 func (e *G2) ScalarBaseMult2(k *big.Int) *G2 { 109 if e.p == nil { 110 e.p = &twistPoint{} 111 } 112 e.p.Mul2(twistGen, k) 113 return e 114 } 115 */ 116 /* 117 **************************************************************/ 118 119 // Add sets e to a+b and then returns e. 120 func (e *G1) Add(a, b *G1) *G1 { 121 if e.p == nil { 122 e.p = &curvePoint{} 123 } 124 e.p.Add(a.p, b.p) 125 return e 126 } 127 128 // Neg sets e to -a and then returns e. 129 func (e *G1) Neg(a *G1) *G1 { 130 if e.p == nil { 131 e.p = &curvePoint{} 132 } 133 e.p.Neg(a.p) 134 return e 135 } 136 137 // Set sets e to a and then returns e. 138 func (e *G1) Set(a *G1) *G1 { 139 if e.p == nil { 140 e.p = &curvePoint{} 141 } 142 e.p.Set(a.p) 143 return e 144 } 145 146 // Marshal converts e to a byte slice. 147 func (e *G1) Marshal() []byte { 148 // Each value is a 256-bit number. 149 const numBytes = 256 / 8 150 151 if e.p == nil { 152 e.p = &curvePoint{} 153 } 154 155 e.p.MakeAffine() 156 ret := make([]byte, numBytes*2) 157 if e.p.IsInfinity() { 158 return ret 159 } 160 temp := &gfP{} 161 162 montDecode(temp, &e.p.x) 163 temp.Marshal(ret) 164 montDecode(temp, &e.p.y) 165 temp.Marshal(ret[numBytes:]) 166 167 return ret 168 } 169 170 // Unmarshal sets e to the result of converting the output of Marshal back into 171 // a group element and then returns e. 172 func (e *G1) Unmarshal(m []byte) ([]byte, error) { 173 // Each value is a 256-bit number. 174 const numBytes = 256 / 8 175 176 if len(m) < 2*numBytes { 177 return nil, errors.New("sm9: not enough data") 178 } 179 180 if e.p == nil { 181 e.p = &curvePoint{} 182 } else { 183 e.p.x, e.p.y = gfP{0}, gfP{0} 184 } 185 186 e.p.x.Unmarshal(m) 187 e.p.y.Unmarshal(m[numBytes:]) 188 montEncode(&e.p.x, &e.p.x) 189 montEncode(&e.p.y, &e.p.y) 190 191 zero := gfP{0} 192 if e.p.x == zero && e.p.y == zero { 193 // This is the point at infinity. 194 e.p.y = *newGFp(1) 195 e.p.z = gfP{0} 196 e.p.t = gfP{0} 197 } else { 198 e.p.z = *newGFp(1) 199 e.p.t = *newGFp(1) 200 201 if !e.p.IsOnCurve() { 202 return nil, errors.New("sm9: malformed point") 203 } 204 } 205 206 return m[2*numBytes:], nil 207 } 208 209 // G2 is an abstract cyclic group. The zero value is suitable for use as the 210 // output of an operation, but cannot be used as an input. 211 type G2 struct { 212 p *twistPoint 213 } 214 215 // RandomG2 returns x and g₂ˣ where x is a random, non-zero number read from r. 216 func RandomG2(r io.Reader) (*big.Int, *G2, error) { 217 k, err := randomK(r) 218 if err != nil { 219 return nil, nil, err 220 } 221 222 return k, new(G2).ScalarBaseMult(k), nil 223 } 224 225 func (e *G2) String() string { 226 return "sm9.G2" + e.p.String() 227 } 228 229 // ScalarBaseMult sets e to g*k where g is the generator of the group and then 230 // returns out. 231 func (e *G2) ScalarBaseMult(k *big.Int) *G2 { 232 if e.p == nil { 233 e.p = &twistPoint{} 234 } 235 e.p.Mul(twistGen, k) 236 return e 237 } 238 239 // ScalarMult sets e to a*k and then returns e. 240 func (e *G2) ScalarMult(a *G2, k *big.Int) *G2 { 241 if e.p == nil { 242 e.p = &twistPoint{} 243 } 244 e.p.Mul(a.p, k) 245 return e 246 } 247 248 // Add sets e to a+b and then returns e. 249 func (e *G2) Add(a, b *G2) *G2 { 250 if e.p == nil { 251 e.p = &twistPoint{} 252 } 253 e.p.Add(a.p, b.p) 254 return e 255 } 256 257 // Neg sets e to -a and then returns e. 258 func (e *G2) Neg(a *G2) *G2 { 259 if e.p == nil { 260 e.p = &twistPoint{} 261 } 262 e.p.Neg(a.p) 263 return e 264 } 265 266 // Set sets e to a and then returns e. 267 func (e *G2) Set(a *G2) *G2 { 268 if e.p == nil { 269 e.p = &twistPoint{} 270 } 271 e.p.Set(a.p) 272 return e 273 } 274 275 // Marshal converts e into a byte slice. 276 func (e *G2) Marshal() []byte { 277 // Each value is a 256-bit number. 278 const numBytes = 256 / 8 279 280 if e.p == nil { 281 e.p = &twistPoint{} 282 } 283 284 e.p.MakeAffine() 285 if e.p.IsInfinity() { 286 return make([]byte, 1) 287 } 288 289 ret := make([]byte, 1+numBytes*4) 290 ret[0] = 0x01 291 temp := &gfP{} 292 293 montDecode(temp, &e.p.x.x) 294 temp.Marshal(ret[1:]) 295 montDecode(temp, &e.p.x.y) 296 temp.Marshal(ret[1+numBytes:]) 297 montDecode(temp, &e.p.y.x) 298 temp.Marshal(ret[1+2*numBytes:]) 299 montDecode(temp, &e.p.y.y) 300 temp.Marshal(ret[1+3*numBytes:]) 301 302 return ret 303 } 304 305 // Unmarshal sets e to the result of converting the output of Marshal back into 306 // a group element and then returns e. 307 func (e *G2) Unmarshal(m []byte) ([]byte, error) { 308 // Each value is a 256-bit number. 309 const numBytes = 256 / 8 310 311 if e.p == nil { 312 e.p = &twistPoint{} 313 } 314 315 if len(m) > 0 && m[0] == 0x00 { 316 e.p.SetInfinity() 317 return m[1:], nil 318 } else if len(m) > 0 && m[0] != 0x01 { 319 return nil, errors.New("sm9: malformed point") 320 } else if len(m) < 1+4*numBytes { 321 return nil, errors.New("sm9: not enough data") 322 } 323 324 e.p.x.x.Unmarshal(m[1:]) 325 e.p.x.y.Unmarshal(m[1+numBytes:]) 326 e.p.y.x.Unmarshal(m[1+2*numBytes:]) 327 e.p.y.y.Unmarshal(m[1+3*numBytes:]) 328 montEncode(&e.p.x.x, &e.p.x.x) 329 montEncode(&e.p.x.y, &e.p.x.y) 330 montEncode(&e.p.y.x, &e.p.y.x) 331 montEncode(&e.p.y.y, &e.p.y.y) 332 333 if e.p.x.IsZero() && e.p.y.IsZero() { 334 // This is the point at infinity. 335 e.p.y.SetOne() 336 e.p.z.SetZero() 337 e.p.t.SetZero() 338 } else { 339 e.p.z.SetOne() 340 e.p.t.SetOne() 341 342 if !e.p.IsOnCurve() { 343 return nil, errors.New("sm9: malformed point") 344 } 345 } 346 347 return m[1+4*numBytes:], nil 348 } 349 350 // GT is an abstract cyclic group. The zero value is suitable for use as the 351 // output of an operation, but cannot be used as an input. 352 type GT struct { 353 p *gfP12 354 } 355 356 // RandomGT returns x and e(g₁, g₂)ˣ where x is a random, non-zero number read 357 // from r. 358 func RandomGT(r io.Reader) (*big.Int, *GT, error) { 359 k, err := randomK(r) 360 if err != nil { 361 return nil, nil, err 362 } 363 364 return k, new(GT).ScalarBaseMult(k), nil 365 } 366 367 // Pair calculates an Optimal Ate pairing. 368 func Pair(g1 *G1, g2 *G2) *GT { 369 return >{optimalAte(g2.p, g1.p)} 370 } 371 372 // Miller applies Miller's algorithm, which is a bilinear function from the 373 // source groups to F_p^12. Miller(g1, g2).Finalize() is equivalent to Pair(g1, 374 // g2). 375 func Miller(g1 *G1, g2 *G2) *GT { 376 return >{miller(g2.p, g1.p)} 377 } 378 379 func (g *GT) String() string { 380 return "sm9.GT" + g.p.String() 381 } 382 383 // ScalarBaseMult sets e to g*k where g is the generator of the group and then 384 // returns out. 385 func (e *GT) ScalarBaseMult(k *big.Int) *GT { 386 if e.p == nil { 387 e.p = &gfP12{} 388 } 389 e.p.Exp(gfP12Gen, k) 390 return e 391 } 392 393 // ScalarMult sets e to a*k and then returns e. 394 func (e *GT) ScalarMult(a *GT, k *big.Int) *GT { 395 if e.p == nil { 396 e.p = &gfP12{} 397 } 398 e.p.Exp(a.p, k) 399 return e 400 } 401 402 // Add sets e to a+b and then returns e. 403 func (e *GT) Add(a, b *GT) *GT { 404 if e.p == nil { 405 e.p = &gfP12{} 406 } 407 e.p.Mul(a.p, b.p) 408 return e 409 } 410 411 // Neg sets e to -a and then returns e. 412 func (e *GT) Neg(a *GT) *GT { 413 if e.p == nil { 414 e.p = &gfP12{} 415 } 416 e.p.Conjugate(a.p) 417 return e 418 } 419 420 // Set sets e to a and then returns e. 421 func (e *GT) Set(a *GT) *GT { 422 if e.p == nil { 423 e.p = &gfP12{} 424 } 425 e.p.Set(a.p) 426 return e 427 } 428 429 // Finalize is a linear function from F_p^12 to GT. 430 func (e *GT) Finalize() *GT { 431 ret := finalExponentiation(e.p) 432 e.p.Set(ret) 433 return e 434 } 435 436 // Marshal converts e into a byte slice. 437 func (e *GT) Marshal() []byte { 438 // Each value is a 256-bit number. 439 const numBytes = 256 / 8 440 441 if e.p == nil { 442 e.p = &gfP12{} 443 e.p.SetOne() 444 } 445 446 ret := make([]byte, numBytes*12) 447 temp := &gfP{} 448 449 montDecode(temp, &e.p.x.x.x) 450 temp.Marshal(ret) 451 montDecode(temp, &e.p.x.x.y) 452 temp.Marshal(ret[numBytes:]) 453 montDecode(temp, &e.p.x.y.x) 454 temp.Marshal(ret[2*numBytes:]) 455 montDecode(temp, &e.p.x.y.y) 456 temp.Marshal(ret[3*numBytes:]) 457 montDecode(temp, &e.p.x.z.x) 458 temp.Marshal(ret[4*numBytes:]) 459 montDecode(temp, &e.p.x.z.y) 460 temp.Marshal(ret[5*numBytes:]) 461 montDecode(temp, &e.p.y.x.x) 462 temp.Marshal(ret[6*numBytes:]) 463 montDecode(temp, &e.p.y.x.y) 464 temp.Marshal(ret[7*numBytes:]) 465 montDecode(temp, &e.p.y.y.x) 466 temp.Marshal(ret[8*numBytes:]) 467 montDecode(temp, &e.p.y.y.y) 468 temp.Marshal(ret[9*numBytes:]) 469 montDecode(temp, &e.p.y.z.x) 470 temp.Marshal(ret[10*numBytes:]) 471 montDecode(temp, &e.p.y.z.y) 472 temp.Marshal(ret[11*numBytes:]) 473 474 return ret 475 } 476 477 // Unmarshal sets e to the result of converting the output of Marshal back into 478 // a group element and then returns e. 479 func (e *GT) Unmarshal(m []byte) ([]byte, error) { 480 // Each value is a 256-bit number. 481 const numBytes = 256 / 8 482 483 if len(m) < 12*numBytes { 484 return nil, errors.New("sm9: not enough data") 485 } 486 487 if e.p == nil { 488 e.p = &gfP12{} 489 } 490 491 e.p.x.x.x.Unmarshal(m) 492 e.p.x.x.y.Unmarshal(m[numBytes:]) 493 e.p.x.y.x.Unmarshal(m[2*numBytes:]) 494 e.p.x.y.y.Unmarshal(m[3*numBytes:]) 495 e.p.x.z.x.Unmarshal(m[4*numBytes:]) 496 e.p.x.z.y.Unmarshal(m[5*numBytes:]) 497 e.p.y.x.x.Unmarshal(m[6*numBytes:]) 498 e.p.y.x.y.Unmarshal(m[7*numBytes:]) 499 e.p.y.y.x.Unmarshal(m[8*numBytes:]) 500 e.p.y.y.y.Unmarshal(m[9*numBytes:]) 501 e.p.y.z.x.Unmarshal(m[10*numBytes:]) 502 e.p.y.z.y.Unmarshal(m[11*numBytes:]) 503 montEncode(&e.p.x.x.x, &e.p.x.x.x) 504 montEncode(&e.p.x.x.y, &e.p.x.x.y) 505 montEncode(&e.p.x.y.x, &e.p.x.y.x) 506 montEncode(&e.p.x.y.y, &e.p.x.y.y) 507 montEncode(&e.p.x.z.x, &e.p.x.z.x) 508 montEncode(&e.p.x.z.y, &e.p.x.z.y) 509 montEncode(&e.p.y.x.x, &e.p.y.x.x) 510 montEncode(&e.p.y.x.y, &e.p.y.x.y) 511 montEncode(&e.p.y.y.x, &e.p.y.y.x) 512 montEncode(&e.p.y.y.y, &e.p.y.y.y) 513 montEncode(&e.p.y.z.x, &e.p.y.z.x) 514 montEncode(&e.p.y.z.y, &e.p.y.z.y) 515 516 return m[12*numBytes:], nil 517 }