github.com/amazechain/amc@v0.1.3/common/crypto/bn256/cloudflare/gfp12.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  package bn256
    18  
    19  // For details of the algorithms used, see "Multiplication and Squaring on
    20  // Pairing-Friendly Fields, Devegili et al.
    21  // http://eprint.iacr.org/2006/471.pdf.
    22  
    23  import (
    24  	"math/big"
    25  )
    26  
    27  // gfP12 implements the field of size p¹² as a quadratic extension of gfP6
    28  // where ω²=τ.
    29  type gfP12 struct {
    30  	x, y gfP6 // value is xω + y
    31  }
    32  
    33  func (e *gfP12) String() string {
    34  	return "(" + e.x.String() + "," + e.y.String() + ")"
    35  }
    36  
    37  func (e *gfP12) Set(a *gfP12) *gfP12 {
    38  	e.x.Set(&a.x)
    39  	e.y.Set(&a.y)
    40  	return e
    41  }
    42  
    43  func (e *gfP12) SetZero() *gfP12 {
    44  	e.x.SetZero()
    45  	e.y.SetZero()
    46  	return e
    47  }
    48  
    49  func (e *gfP12) SetOne() *gfP12 {
    50  	e.x.SetZero()
    51  	e.y.SetOne()
    52  	return e
    53  }
    54  
    55  func (e *gfP12) IsZero() bool {
    56  	return e.x.IsZero() && e.y.IsZero()
    57  }
    58  
    59  func (e *gfP12) IsOne() bool {
    60  	return e.x.IsZero() && e.y.IsOne()
    61  }
    62  
    63  func (e *gfP12) Conjugate(a *gfP12) *gfP12 {
    64  	e.x.Neg(&a.x)
    65  	e.y.Set(&a.y)
    66  	return e
    67  }
    68  
    69  func (e *gfP12) Neg(a *gfP12) *gfP12 {
    70  	e.x.Neg(&a.x)
    71  	e.y.Neg(&a.y)
    72  	return e
    73  }
    74  
    75  // Frobenius computes (xω+y)^p = x^p ω·ξ^((p-1)/6) + y^p
    76  func (e *gfP12) Frobenius(a *gfP12) *gfP12 {
    77  	e.x.Frobenius(&a.x)
    78  	e.y.Frobenius(&a.y)
    79  	e.x.MulScalar(&e.x, xiToPMinus1Over6)
    80  	return e
    81  }
    82  
    83  // FrobeniusP2 computes (xω+y)^p² = x^p² ω·ξ^((p²-1)/6) + y^p²
    84  func (e *gfP12) FrobeniusP2(a *gfP12) *gfP12 {
    85  	e.x.FrobeniusP2(&a.x)
    86  	e.x.MulGFP(&e.x, xiToPSquaredMinus1Over6)
    87  	e.y.FrobeniusP2(&a.y)
    88  	return e
    89  }
    90  
    91  func (e *gfP12) FrobeniusP4(a *gfP12) *gfP12 {
    92  	e.x.FrobeniusP4(&a.x)
    93  	e.x.MulGFP(&e.x, xiToPSquaredMinus1Over3)
    94  	e.y.FrobeniusP4(&a.y)
    95  	return e
    96  }
    97  
    98  func (e *gfP12) Add(a, b *gfP12) *gfP12 {
    99  	e.x.Add(&a.x, &b.x)
   100  	e.y.Add(&a.y, &b.y)
   101  	return e
   102  }
   103  
   104  func (e *gfP12) Sub(a, b *gfP12) *gfP12 {
   105  	e.x.Sub(&a.x, &b.x)
   106  	e.y.Sub(&a.y, &b.y)
   107  	return e
   108  }
   109  
   110  func (e *gfP12) Mul(a, b *gfP12) *gfP12 {
   111  	tx := (&gfP6{}).Mul(&a.x, &b.y)
   112  	t := (&gfP6{}).Mul(&b.x, &a.y)
   113  	tx.Add(tx, t)
   114  
   115  	ty := (&gfP6{}).Mul(&a.y, &b.y)
   116  	t.Mul(&a.x, &b.x).MulTau(t)
   117  
   118  	e.x.Set(tx)
   119  	e.y.Add(ty, t)
   120  	return e
   121  }
   122  
   123  func (e *gfP12) MulScalar(a *gfP12, b *gfP6) *gfP12 {
   124  	e.x.Mul(&e.x, b)
   125  	e.y.Mul(&e.y, b)
   126  	return e
   127  }
   128  
   129  func (c *gfP12) Exp(a *gfP12, power *big.Int) *gfP12 {
   130  	sum := (&gfP12{}).SetOne()
   131  	t := &gfP12{}
   132  
   133  	for i := power.BitLen() - 1; i >= 0; i-- {
   134  		t.Square(sum)
   135  		if power.Bit(i) != 0 {
   136  			sum.Mul(t, a)
   137  		} else {
   138  			sum.Set(t)
   139  		}
   140  	}
   141  
   142  	c.Set(sum)
   143  	return c
   144  }
   145  
   146  func (e *gfP12) Square(a *gfP12) *gfP12 {
   147  	// Complex squaring algorithm
   148  	v0 := (&gfP6{}).Mul(&a.x, &a.y)
   149  
   150  	t := (&gfP6{}).MulTau(&a.x)
   151  	t.Add(&a.y, t)
   152  	ty := (&gfP6{}).Add(&a.x, &a.y)
   153  	ty.Mul(ty, t).Sub(ty, v0)
   154  	t.MulTau(v0)
   155  	ty.Sub(ty, t)
   156  
   157  	e.x.Add(v0, v0)
   158  	e.y.Set(ty)
   159  	return e
   160  }
   161  
   162  func (e *gfP12) Invert(a *gfP12) *gfP12 {
   163  	// See "Implementing cryptographic pairings", M. Scott, section 3.2.
   164  	// ftp://136.206.11.249/pub/crypto/pairings.pdf
   165  	t1, t2 := &gfP6{}, &gfP6{}
   166  
   167  	t1.Square(&a.x)
   168  	t2.Square(&a.y)
   169  	t1.MulTau(t1).Sub(t2, t1)
   170  	t2.Invert(t1)
   171  
   172  	e.x.Neg(&a.x)
   173  	e.y.Set(&a.y)
   174  	e.MulScalar(e, t2)
   175  	return e
   176  }