github.com/consensys/gnark-crypto@v0.14.0/internal/generator/tower/generate.go (about)

     1  package tower
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/consensys/bavard"
     9  	"github.com/consensys/gnark-crypto/internal/generator/config"
    10  	"github.com/consensys/gnark-crypto/internal/generator/tower/asm/amd64"
    11  )
    12  
    13  // Generate generates a tower 2->6->12 over fp
    14  func Generate(conf config.Curve, baseDir string, bgen *bavard.BatchGenerator) error {
    15  	if conf.Equal(config.BW6_756) || conf.Equal(config.BW6_761) || conf.Equal(config.BW6_633) || conf.Equal(config.BLS24_315) || conf.Equal(config.BLS24_317) {
    16  		return nil
    17  	}
    18  
    19  	entries := []bavard.Entry{
    20  		{File: filepath.Join(baseDir, "e2_amd64.go"), Templates: []string{"amd64.fq2.go.tmpl"}},
    21  		{File: filepath.Join(baseDir, "e2_fallback.go"), Templates: []string{"fallback.fq2.go.tmpl"}, BuildTag: "!amd64"},
    22  		{File: filepath.Join(baseDir, "asm.go"), Templates: []string{"asm.go.tmpl"}, BuildTag: "!noadx"},
    23  		{File: filepath.Join(baseDir, "asm_noadx.go"), Templates: []string{"asm_noadx.go.tmpl"}, BuildTag: "noadx"},
    24  	}
    25  
    26  	if err := bgen.Generate(conf, "fptower", "./tower/template/fq12over6over2", entries...); err != nil {
    27  		return err
    28  	}
    29  
    30  	towerConfs := []towerConf{
    31  		{
    32  			Curve:           &conf,
    33  			RecursionDegree: 2,
    34  			TotalDegree:     2,
    35  			BaseName:        "fp.Element",
    36  			BaseElementName: "A",
    37  		},
    38  		{
    39  			Curve:           &conf,
    40  			RecursionDegree: 3,
    41  			TotalDegree:     6,
    42  			BaseName:        "E2",
    43  			BaseElementName: "B",
    44  		},
    45  		{
    46  			Curve:           &conf,
    47  			RecursionDegree: 2,
    48  			TotalDegree:     12,
    49  			BaseName:        "E6",
    50  			BaseElementName: "C",
    51  		},
    52  	}
    53  
    54  	for _, towerConf := range towerConfs {
    55  
    56  		entries = []bavard.Entry{
    57  			{
    58  				File:      filepath.Join(baseDir, fmt.Sprintf("e%d.go", towerConf.TotalDegree)),
    59  				Templates: []string{fmt.Sprintf("fq%d.go.tmpl", towerConf.TotalDegree), "base.go.tmpl"},
    60  			},
    61  			{
    62  				File:      filepath.Join(baseDir, fmt.Sprintf("e%d_test.go", towerConf.TotalDegree)),
    63  				Templates: []string{fmt.Sprintf("tests/fq%d.go.tmpl", towerConf.TotalDegree), "tests/base.go.tmpl"},
    64  			},
    65  		}
    66  
    67  		if err := bgen.Generate(towerConf, "fptower", "./tower/template/fq12over6over2", entries...); err != nil {
    68  			return err
    69  		}
    70  	}
    71  
    72  	{
    73  		// fq2 assembly
    74  		fName := filepath.Join(baseDir, "e2_amd64.s")
    75  		f, err := os.Create(fName)
    76  		if err != nil {
    77  			return err
    78  		}
    79  
    80  		Fq2Amd64 := amd64.NewFq2Amd64(f, conf.Fp, conf)
    81  		if err := Fq2Amd64.Generate(true); err != nil {
    82  			_ = f.Close()
    83  			return err
    84  		}
    85  		_ = f.Close()
    86  
    87  	}
    88  
    89  	if conf.Equal(config.BN254) || conf.Equal(config.BLS12_381) {
    90  		{
    91  			// fq2 assembly
    92  			fName := filepath.Join(baseDir, "e2_adx_amd64.s")
    93  			os.Remove(fName)
    94  		}
    95  	}
    96  
    97  	return nil
    98  
    99  }
   100  
   101  type towerConf struct {
   102  	Curve           *config.Curve
   103  	RecursionDegree int
   104  	TotalDegree     int
   105  	BaseName        string
   106  	BaseElementName string
   107  }