github.com/consensys/gnark-crypto@v0.14.0/field/goff/cmd/root.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  // Package cmd is the CLI interface for goff
    16  package cmd
    17  
    18  import (
    19  	"fmt"
    20  	"math/bits"
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  
    25  	"github.com/consensys/gnark-crypto/field/generator"
    26  	field "github.com/consensys/gnark-crypto/field/generator/config"
    27  	"github.com/spf13/cobra"
    28  )
    29  
    30  var rootCmd = &cobra.Command{
    31  	Use:     "goff",
    32  	Short:   "goff generates arithmetic operations for any moduli",
    33  	Run:     cmdGenerate,
    34  	Version: Version,
    35  }
    36  
    37  // flags
    38  var (
    39  	fModulus     string
    40  	fOutputDir   string
    41  	fPackageName string
    42  	fElementName string
    43  )
    44  
    45  func init() {
    46  	cobra.OnInitialize()
    47  	rootCmd.PersistentFlags().StringVarP(&fElementName, "element", "e", "", "name of the generated struct and file")
    48  	rootCmd.PersistentFlags().StringVarP(&fModulus, "modulus", "m", "", "field modulus (base 10)")
    49  	rootCmd.PersistentFlags().StringVarP(&fOutputDir, "output", "o", "", "destination path to create output files")
    50  	rootCmd.PersistentFlags().StringVarP(&fPackageName, "package", "p", "", "package name in generated files")
    51  	if bits.UintSize != 64 {
    52  		panic("goff only supports 64bits architectures")
    53  	}
    54  }
    55  
    56  func cmdGenerate(cmd *cobra.Command, args []string) {
    57  	fmt.Println()
    58  	fmt.Println("running goff version", Version)
    59  	fmt.Println()
    60  
    61  	// parse flags
    62  	if err := parseFlags(cmd); err != nil {
    63  		_ = cmd.Usage()
    64  		fmt.Printf("\n%s\n", err.Error())
    65  		os.Exit(-1)
    66  	}
    67  
    68  	// generate code
    69  	F, err := field.NewFieldConfig(fPackageName, fElementName, fModulus, false)
    70  	if err != nil {
    71  		fmt.Printf("\n%s\n", err.Error())
    72  		os.Exit(-1)
    73  	}
    74  	if err := generator.GenerateFF(F, fOutputDir); err != nil {
    75  		fmt.Printf("\n%s\n", err.Error())
    76  		os.Exit(-1)
    77  	}
    78  }
    79  
    80  func parseFlags(cmd *cobra.Command) error {
    81  	if fModulus == "" ||
    82  		fOutputDir == "" ||
    83  		fPackageName == "" ||
    84  		fElementName == "" {
    85  		return errMissingArgument
    86  	}
    87  
    88  	// clean inputs
    89  	fOutputDir = filepath.Clean(fOutputDir)
    90  	fPackageName = strings.ToLower(fPackageName)
    91  
    92  	return nil
    93  }
    94  
    95  // Execute adds all child commands to the root command and sets flags appropriately.
    96  // This is called by main.main(). It only needs to happen once to the rootCmd.
    97  func Execute() {
    98  	if err := rootCmd.Execute(); err != nil {
    99  		fmt.Println(err)
   100  		os.Exit(1)
   101  	}
   102  }