github.com/consensys/gnark-crypto@v0.14.0/ecc/bw6-761/fr/fft/options.go (about)

     1  // Copyright 2020 Consensys Software Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Code generated by consensys/gnark-crypto DO NOT EDIT
    16  
    17  package fft
    18  
    19  import (
    20  	"runtime"
    21  
    22  	"github.com/consensys/gnark-crypto/ecc/bw6-761/fr"
    23  )
    24  
    25  // Option defines option for altering the behavior of FFT methods.
    26  // See the descriptions of functions returning instances of this type for
    27  // particular options.
    28  type Option func(*fftConfig)
    29  
    30  type fftConfig struct {
    31  	coset   bool
    32  	nbTasks int
    33  }
    34  
    35  // OnCoset if provided, FFT(a) returns the evaluation of a on a coset.
    36  func OnCoset() Option {
    37  	return func(opt *fftConfig) {
    38  		opt.coset = true
    39  	}
    40  }
    41  
    42  // WithNbTasks sets the max number of task (go routine) to spawn. Must be between 1 and 512.
    43  func WithNbTasks(nbTasks int) Option {
    44  	if nbTasks < 1 {
    45  		nbTasks = 1
    46  	} else if nbTasks > 512 {
    47  		nbTasks = 512
    48  	}
    49  	return func(opt *fftConfig) {
    50  		opt.nbTasks = nbTasks
    51  	}
    52  }
    53  
    54  // default options
    55  func fftOptions(opts ...Option) fftConfig {
    56  	// apply options
    57  	opt := fftConfig{
    58  		coset:   false,
    59  		nbTasks: runtime.NumCPU(),
    60  	}
    61  	for _, option := range opts {
    62  		option(&opt)
    63  	}
    64  	return opt
    65  }
    66  
    67  // DomainOption defines option for altering the definition of the FFT domain
    68  // See the descriptions of functions returning instances of this type for
    69  // particular options.
    70  type DomainOption func(*domainConfig)
    71  
    72  type domainConfig struct {
    73  	shift          *fr.Element
    74  	withPrecompute bool
    75  }
    76  
    77  // WithShift sets the FrMultiplicativeGen of the domain.
    78  // Default is generator of the largest 2-adic subgroup.
    79  func WithShift(shift fr.Element) DomainOption {
    80  	return func(opt *domainConfig) {
    81  		opt.shift = new(fr.Element).Set(&shift)
    82  	}
    83  }
    84  
    85  // WithoutPrecompute disables precomputation of twiddles in the domain.
    86  // When this option is set, FFTs will be slower, but will use less memory.
    87  func WithoutPrecompute() DomainOption {
    88  	return func(opt *domainConfig) {
    89  		opt.withPrecompute = false
    90  	}
    91  }
    92  
    93  // default options
    94  func domainOptions(opts ...DomainOption) domainConfig {
    95  	// apply options
    96  	opt := domainConfig{
    97  		withPrecompute: true,
    98  	}
    99  	for _, option := range opts {
   100  		option(&opt)
   101  	}
   102  	return opt
   103  }