github.com/consensys/gnark-crypto@v0.14.0/internal/generator/fft/template/fr.generator.go.tmpl (about)

     1  import (
     2  	"fmt"
     3  	"math/big"
     4  	"math/bits"
     5  
     6  	"github.com/consensys/gnark-crypto/ecc"
     7  )
     8  
     9  // Generator returns a generator for Z/2^(log(m))Z
    10  // or an error if m is too big (required root of unity doesn't exist)
    11  func Generator(m uint64) (Element, error) {
    12  	x := ecc.NextPowerOfTwo(m)
    13  
    14  	var rootOfUnity Element
    15  	{{if eq .Name "bls12-378"}}
    16  		rootOfUnity.SetString("4045585818372166415418670827807793147093034396422209590578257013290761627990")
    17  		const maxOrderRoot uint64 = 42
    18  	{{else if eq .Name "bls12-377"}}
    19  		rootOfUnity.SetString("8065159656716812877374967518403273466521432693661810619979959746626482506078")
    20  		const maxOrderRoot uint64 = 47
    21  	{{else if eq .Name "bls12-381"}}
    22  		rootOfUnity.SetString("10238227357739495823651030575849232062558860180284477541189508159991286009131")
    23  		const maxOrderRoot uint64 = 32
    24  	{{else if eq .Name "bn254"}}
    25  		rootOfUnity.SetString("19103219067921713944291392827692070036145651957329286315305642004821462161904")
    26  		const maxOrderRoot uint64 = 28
    27  	{{else if eq .Name "bw6-761"}}
    28  		rootOfUnity.SetString("32863578547254505029601261939868325669770508939375122462904745766352256812585773382134936404344547323199885654433")
    29  		const maxOrderRoot uint64 = 46
    30  	{{else if eq .Name "bw6-756"}}
    31          rootOfUnity.SetString("199251335866470442271346949249090720992237796757894062992204115206570647302191425225605716521843542790404563904580")
    32          const maxOrderRoot uint64 = 41
    33      {{else if eq .Name "bw6-633"}}
    34  		rootOfUnity.SetString("4991787701895089137426454739366935169846548798279261157172811661565882460884369603588700158257")
    35  		const maxOrderRoot uint64 = 20
    36  	{{else if eq .Name "bls24-315"}}
    37  		rootOfUnity.SetString("1792993287828780812362846131493071959406149719416102105453370749552622525216")
    38         const maxOrderRoot uint64 = 22
    39  	{{else if eq .Name "bls24-317"}}
    40  		rootOfUnity.SetString("16532287748948254263922689505213135976137839535221842169193829039521719560631")
    41         const maxOrderRoot uint64 = 60
    42  	{{end}}
    43  
    44  	// find generator for Z/2^(log(m))Z
    45  	logx := uint64(bits.TrailingZeros64(x))
    46  	if logx > maxOrderRoot {
    47  		return Element{}, fmt.Errorf("m (%d) is too big: the required root of unity does not exist", m)
    48  	}
    49  
    50  	expo := uint64(1 << (maxOrderRoot - logx))
    51  	var generator Element
    52  	generator.Exp(rootOfUnity, big.NewInt(int64(expo))) // order x
    53  	return generator, nil
    54  }