github.com/amazechain/amc@v0.1.3/common/crypto/bls12381/gt.go (about)

     1  // Copyright 2023 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  //nolint:stylecheck
    18  package bls12381
    19  
    20  import (
    21  	"errors"
    22  	"math/big"
    23  )
    24  
    25  // E is type for target group element
    26  type E = fe12
    27  
    28  // GT is type for target multiplicative group GT.
    29  type GT struct {
    30  	fp12 *fp12
    31  }
    32  
    33  func (e *E) Set(e2 *E) *E {
    34  	return e.set(e2)
    35  }
    36  
    37  // One sets a new target group element to one
    38  func (e *E) One() *E {
    39  	e = new(fe12).one()
    40  	return e
    41  }
    42  
    43  // IsOne returns true if given element equals to one
    44  func (e *E) IsOne() bool {
    45  	return e.isOne()
    46  }
    47  
    48  // Equal returns true if given two element is equal, otherwise returns false
    49  func (g *E) Equal(g2 *E) bool {
    50  	return g.equal(g2)
    51  }
    52  
    53  // NewGT constructs new target group instance.
    54  func NewGT() *GT {
    55  	fp12 := newFp12(nil)
    56  	return &GT{fp12}
    57  }
    58  
    59  // Q returns group order in big.Int.
    60  func (g *GT) Q() *big.Int {
    61  	return new(big.Int).Set(q)
    62  }
    63  
    64  // FromBytes expects 576 byte input and returns target group element
    65  // FromBytes returns error if given element is not on correct subgroup.
    66  func (g *GT) FromBytes(in []byte) (*E, error) {
    67  	e, err := g.fp12.fromBytes(in)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	if !g.IsValid(e) {
    72  		return e, errors.New("invalid element")
    73  	}
    74  	return e, nil
    75  }
    76  
    77  // ToBytes serializes target group element.
    78  func (g *GT) ToBytes(e *E) []byte {
    79  	return g.fp12.toBytes(e)
    80  }
    81  
    82  // IsValid checks whether given target group element is in correct subgroup.
    83  func (g *GT) IsValid(e *E) bool {
    84  	r := g.New()
    85  	g.fp12.exp(r, e, q)
    86  	return r.isOne()
    87  }
    88  
    89  // New initializes a new target group element which is equal to one
    90  func (g *GT) New() *E {
    91  	return new(E).One()
    92  }
    93  
    94  // Add adds two field element `a` and `b` and assigns the result to the element in first argument.
    95  func (g *GT) Add(c, a, b *E) {
    96  	g.fp12.add(c, a, b)
    97  }
    98  
    99  // Sub subtracts two field element `a` and `b`, and assigns the result to the element in first argument.
   100  func (g *GT) Sub(c, a, b *E) {
   101  	g.fp12.sub(c, a, b)
   102  }
   103  
   104  // Mul multiplies two field element `a` and `b` and assigns the result to the element in first argument.
   105  func (g *GT) Mul(c, a, b *E) {
   106  	g.fp12.mul(c, a, b)
   107  }
   108  
   109  // Square squares an element `a` and assigns the result to the element in first argument.
   110  func (g *GT) Square(c, a *E) {
   111  	g.fp12.cyclotomicSquare(c, a)
   112  }
   113  
   114  // Exp exponents an element `a` by a scalar `s` and assigns the result to the element in first argument.
   115  func (g *GT) Exp(c, a *E, s *big.Int) {
   116  	g.fp12.cyclotomicExp(c, a, s)
   117  }
   118  
   119  // Inverse inverses an element `a` and assigns the result to the element in first argument.
   120  func (g *GT) Inverse(c, a *E) {
   121  	g.fp12.inverse(c, a)
   122  }