You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.1 KiB

  1. package bn128
  2. import (
  3. "errors"
  4. "math/big"
  5. )
  6. type Bn128 struct {
  7. Q *big.Int
  8. R *big.Int
  9. Gg1 [2]*big.Int
  10. Gg2 [2][2]*big.Int
  11. NonResidueFq2 *big.Int
  12. NonResidueFq6 [2]*big.Int
  13. Fq1 Fq
  14. Fq2 Fq2
  15. Fq6 Fq6
  16. Fq12 Fq12
  17. G1 G1
  18. G2 G2
  19. }
  20. func NewBn128() (Bn128, error) {
  21. var b Bn128
  22. q, ok := new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208583", 10) // i
  23. if !ok {
  24. return b, errors.New("err with q")
  25. }
  26. b.Q = q
  27. r, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10) // i
  28. if !ok {
  29. return b, errors.New("err with r")
  30. }
  31. b.R = r
  32. b.Gg1 = [2]*big.Int{
  33. big.NewInt(int64(1)),
  34. big.NewInt(int64(2)),
  35. }
  36. g2_00, ok := new(big.Int).SetString("10857046999023057135944570762232829481370756359578518086990519993285655852781", 10)
  37. if !ok {
  38. return b, errors.New("err with g2_00")
  39. }
  40. g2_01, ok := new(big.Int).SetString("11559732032986387107991004021392285783925812861821192530917403151452391805634", 10)
  41. if !ok {
  42. return b, errors.New("err with g2_00")
  43. }
  44. g2_10, ok := new(big.Int).SetString("8495653923123431417604973247489272438418190587263600148770280649306958101930", 10)
  45. if !ok {
  46. return b, errors.New("err with g2_00")
  47. }
  48. g2_11, ok := new(big.Int).SetString("4082367875863433681332203403145435568316851327593401208105741076214120093531", 10)
  49. if !ok {
  50. return b, errors.New("err with g2_00")
  51. }
  52. g2_0 := [2]*big.Int{
  53. g2_00,
  54. g2_01,
  55. }
  56. g2_1 := [2]*big.Int{
  57. g2_10,
  58. g2_11,
  59. }
  60. b.Gg2 = [2][2]*big.Int{
  61. g2_0,
  62. g2_1,
  63. }
  64. b.Fq1 = NewFq(q)
  65. b.NonResidueFq2, ok = new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208582", 10) // i
  66. if !ok {
  67. return b, errors.New("err with nonResidueFq2")
  68. }
  69. b.NonResidueFq6 = [2]*big.Int{
  70. big.NewInt(int64(9)),
  71. big.NewInt(int64(1)),
  72. }
  73. b.Fq2 = Fq2{b.Fq1, b.NonResidueFq2}
  74. b.Fq6 = Fq6{b.Fq2, b.NonResidueFq6}
  75. b.Fq12 = Fq12{b.Fq6, b.Fq2, b.NonResidueFq6}
  76. b.G1 = NewG1(b.Fq1, b.Gg1)
  77. b.G2 = NewG2(b.Fq2, b.Gg2)
  78. return b, nil
  79. }