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

     1  import (
     2  	"runtime"
     3  	{{ template "import_fr" . }}
     4  )
     5  
     6  // Option defines option for altering the behavior of FFT methods.
     7  // See the descriptions of functions returning instances of this type for
     8  // particular options.
     9  type Option func(*fftConfig)
    10  
    11  type fftConfig struct {
    12  	coset   bool
    13  	nbTasks int
    14  }
    15  
    16  // OnCoset if provided, FFT(a) returns the evaluation of a on a coset.
    17  func OnCoset() Option {
    18  	return func(opt *fftConfig) {
    19  		opt.coset = true
    20  	}
    21  }
    22  
    23  // WithNbTasks sets the max number of task (go routine) to spawn. Must be between 1 and 512.
    24  func WithNbTasks(nbTasks int) Option {
    25  	if nbTasks < 1 {
    26  		nbTasks = 1
    27  	} else if nbTasks > 512 {
    28  		nbTasks = 512
    29  	}
    30  	return func(opt *fftConfig) {
    31  		opt.nbTasks = nbTasks
    32  	}
    33  }
    34  
    35  // default options
    36  func fftOptions(opts ...Option) fftConfig {
    37  	// apply options
    38  	opt := fftConfig{
    39  		coset:   false,
    40  		nbTasks: runtime.NumCPU(),
    41  	}
    42  	for _, option := range opts {
    43  		option(&opt)
    44  	}
    45  	return opt
    46  }
    47  
    48  // DomainOption defines option for altering the definition of the FFT domain
    49  // See the descriptions of functions returning instances of this type for
    50  // particular options.
    51  type DomainOption func(*domainConfig)
    52  
    53  type domainConfig struct {
    54  	shift    *fr.Element
    55  	withPrecompute bool
    56  }
    57  
    58  // WithShift sets the FrMultiplicativeGen of the domain.
    59  // Default is generator of the largest 2-adic subgroup.
    60  func WithShift(shift fr.Element) DomainOption {
    61  	return func(opt *domainConfig) {
    62  		opt.shift = new(fr.Element).Set(&shift)
    63  	}
    64  }
    65  
    66  // WithoutPrecompute disables precomputation of twiddles in the domain.
    67  // When this option is set, FFTs will be slower, but will use less memory.
    68  func WithoutPrecompute() DomainOption {
    69  	return func(opt *domainConfig) {
    70  		opt.withPrecompute = false
    71  	}
    72  }
    73  
    74  // default options
    75  func domainOptions(opts ...DomainOption) domainConfig {
    76  	// apply options
    77  	opt := domainConfig{
    78  		withPrecompute: true, 
    79  	}
    80  	for _, option := range opts {
    81  		option(&opt)
    82  	}
    83  	return opt
    84  }