github.com/consensys/gnark@v0.11.0/test/unsafekzg/options.go (about) 1 package unsafekzg 2 3 import ( 4 "os" 5 "path/filepath" 6 7 "github.com/consensys/gnark/logger" 8 ) 9 10 type Option func(*config) error 11 12 // WithCacheDir enables the filesystem cache and sets the cache directory 13 // to ~/.gnark/kzg by default. 14 func WithFSCache() Option { 15 return func(opt *config) error { 16 opt.fsCache = true 17 return nil 18 } 19 } 20 21 type config struct { 22 fsCache bool 23 cacheDir string 24 } 25 26 // default options 27 func options(opts ...Option) (config, error) { 28 var opt config 29 30 // apply user provided options. 31 for _, option := range opts { 32 err := option(&opt) 33 if err != nil { 34 return opt, err 35 } 36 } 37 38 // default value for cacheDir is ~/.gnark/kzg 39 if opt.fsCache { 40 if opt.cacheDir == "" { 41 homeDir, err := os.UserHomeDir() 42 if err != nil { 43 panic(err) 44 } 45 opt.cacheDir = filepath.Join(homeDir, ".gnark", "kzg") 46 } 47 initCache(opt.cacheDir) 48 } 49 50 return opt, nil 51 } 52 53 func initCache(cacheDir string) { 54 // get gnark logger 55 log := logger.Logger() 56 57 // populate cache from disk 58 log.Warn().Str("cacheDir", cacheDir).Msg("using kzg srs cache") 59 60 if _, err := os.Stat(cacheDir); os.IsNotExist(err) { 61 err := os.MkdirAll(cacheDir, 0700) 62 if err != nil { 63 panic(err) 64 } 65 } 66 }