gt.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2020 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package bls12381
  17. import (
  18. "errors"
  19. "math/big"
  20. )
  21. // E is type for target group element
  22. type E = fe12
  23. // GT is type for target multiplicative group GT.
  24. type GT struct {
  25. fp12 *fp12
  26. }
  27. func (e *E) Set(e2 *E) *E {
  28. return e.set(e2)
  29. }
  30. // One sets a new target group element to one
  31. func (e *E) One() *E {
  32. e = new(fe12).one()
  33. return e
  34. }
  35. // IsOne returns true if given element equals to one
  36. func (e *E) IsOne() bool {
  37. return e.isOne()
  38. }
  39. // Equal returns true if given two element is equal, otherwise returns false
  40. func (g *E) Equal(g2 *E) bool {
  41. return g.equal(g2)
  42. }
  43. // NewGT constructs new target group instance.
  44. func NewGT() *GT {
  45. fp12 := newFp12(nil)
  46. return &GT{fp12}
  47. }
  48. // Q returns group order in big.Int.
  49. func (g *GT) Q() *big.Int {
  50. return new(big.Int).Set(q)
  51. }
  52. // FromBytes expects 576 byte input and returns target group element
  53. // FromBytes returns error if given element is not on correct subgroup.
  54. func (g *GT) FromBytes(in []byte) (*E, error) {
  55. e, err := g.fp12.fromBytes(in)
  56. if err != nil {
  57. return nil, err
  58. }
  59. if !g.IsValid(e) {
  60. return e, errors.New("invalid element")
  61. }
  62. return e, nil
  63. }
  64. // ToBytes serializes target group element.
  65. func (g *GT) ToBytes(e *E) []byte {
  66. return g.fp12.toBytes(e)
  67. }
  68. // IsValid checks whether given target group element is in correct subgroup.
  69. func (g *GT) IsValid(e *E) bool {
  70. r := g.New()
  71. g.fp12.exp(r, e, q)
  72. return r.isOne()
  73. }
  74. // New initializes a new target group element which is equal to one
  75. func (g *GT) New() *E {
  76. return new(E).One()
  77. }
  78. // Add adds two field element `a` and `b` and assigns the result to the element in first argument.
  79. func (g *GT) Add(c, a, b *E) {
  80. g.fp12.add(c, a, b)
  81. }
  82. // Sub subtracts two field element `a` and `b`, and assigns the result to the element in first argument.
  83. func (g *GT) Sub(c, a, b *E) {
  84. g.fp12.sub(c, a, b)
  85. }
  86. // Mul multiplies two field element `a` and `b` and assigns the result to the element in first argument.
  87. func (g *GT) Mul(c, a, b *E) {
  88. g.fp12.mul(c, a, b)
  89. }
  90. // Square squares an element `a` and assigns the result to the element in first argument.
  91. func (g *GT) Square(c, a *E) {
  92. g.fp12.cyclotomicSquare(c, a)
  93. }
  94. // Exp exponents an element `a` by a scalar `s` and assigns the result to the element in first argument.
  95. func (g *GT) Exp(c, a *E, s *big.Int) {
  96. g.fp12.cyclotomicExp(c, a, s)
  97. }
  98. // Inverse inverses an element `a` and assigns the result to the element in first argument.
  99. func (g *GT) Inverse(c, a *E) {
  100. g.fp12.inverse(c, a)
  101. }