github.com/cloudflare/circl@v1.5.0/ecc/bls12381/ff/gen.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  // Code Generation using fiat-crypto
     5  //
     6  // Download and unpack `ExtractionOCaml-Ubuntu LTS` from
     7  // https://github.com/mit-plv/fiat-crypto/suites/3241842351/artifacts/75305272
     8  //
     9  // Then run this program specifying the path to the word_by_word_montgomery binary.
    10  //  $ FIAT_BINARY=<path to binary> go run gen.go
    11  //
    12  // References:
    13  // [1] Erbsen et al. "Simple High-Level Code For Cryptographic Arithmetic – With
    14  // Proofs, Without Compromises" https://github.com/mit-plv/fiat-crypto
    15  
    16  package main
    17  
    18  import (
    19  	"log"
    20  	"os"
    21  	"os/exec"
    22  	"strings"
    23  	"text/template"
    24  )
    25  
    26  const (
    27  	packName    = "ff"
    28  	headerCIRCL = "Code generated by gen.go using fiat-crypto."
    29  )
    30  
    31  var (
    32  	FIAT_BINARY = "./word_by_word_montgomery"
    33  	FIAT_PARAMS = []string{
    34  		"--output", "{{.Name}}.go",
    35  		"--lang", "Go",
    36  		"--package-name", "{{.PackageName}}",
    37  		"--doc-prepend-header", "{{.Header}}",
    38  		"--package-case", "lowerCamelCase",
    39  		"--public-function-case", "lowerCamelCase",
    40  		"--public-type-case", "lowerCamelCase",
    41  		"--doc-newline-before-package-declaration",
    42  		"--no-primitives",
    43  		"--widen-carry",
    44  		"--no-field-element-typedefs",
    45  		"--relax-primitive-carry-to-bitwidth", "64",
    46  		"{{.Prefix}}", "64", "{{.Prime}}",
    47  		"add", "sub", "mul", "square",
    48  	}
    49  )
    50  
    51  var fields = []struct{ Prefix, Name, Prime, Header, PackageName string }{
    52  	{
    53  		Prefix:      "FpMont",
    54  		Name:        "fpMont381",
    55  		Prime:       "0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab",
    56  		Header:      headerCIRCL,
    57  		PackageName: packName,
    58  	},
    59  	{
    60  		Prefix:      "ScMont",
    61  		Name:        "scMont255",
    62  		Prime:       "0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001",
    63  		Header:      headerCIRCL,
    64  		PackageName: packName,
    65  	},
    66  }
    67  
    68  func main() {
    69  	if s := os.Getenv("FIAT_BINARY"); s != "" {
    70  		FIAT_BINARY = s
    71  	}
    72  
    73  	var err error
    74  	t := template.New("params")
    75  	buf := new(strings.Builder)
    76  
    77  	for _, f := range fields {
    78  		params := []string{}
    79  
    80  		for _, p := range FIAT_PARAMS {
    81  			buf.Reset()
    82  			t = template.Must(t.Parse(p))
    83  			err = t.Execute(buf, f)
    84  			if err != nil {
    85  				log.Fatalf("executing template:", err)
    86  			}
    87  			params = append(params, buf.String())
    88  		}
    89  
    90  		cmd := exec.Command(FIAT_BINARY, params...)
    91  		out, err := cmd.CombinedOutput()
    92  		if len(out) != 0 || err != nil {
    93  			log.Fatalf("command output: %s\n command error: %v\n", out, err)
    94  		}
    95  	}
    96  }