github.com/cloudflare/circl@v1.5.0/kem/sike/gen.go (about) 1 //go:build ignore 2 // +build ignore 3 4 // Autogenerates wrappers from templates to prevent too much duplicated code 5 // between the code for different modes. 6 package main 7 8 import ( 9 "bytes" 10 "fmt" 11 "go/format" 12 "io/ioutil" 13 "strings" 14 "text/template" 15 ) 16 17 type Instance struct { 18 Bits int 19 } 20 21 func (m Instance) Pkg() string { 22 return strings.ToLower(m.Name()) 23 } 24 25 func (m Instance) Name() string { 26 return fmt.Sprintf("SIKEp%d", m.Bits) 27 } 28 29 func (m Instance) Field() string { 30 return fmt.Sprintf("Fp%d", m.Bits) 31 } 32 33 var ( 34 Instances = []Instance{ 35 {Bits: 434}, 36 {Bits: 503}, 37 {Bits: 751}, 38 } 39 TemplateWarning = "// Code generated from" 40 ) 41 42 func main() { 43 generatePackageFiles() 44 } 45 46 // Generates instance/sike.go from templates/pkg.templ.go 47 func generatePackageFiles() { 48 tl, err := template.ParseFiles("templates/pkg.templ.go") 49 if err != nil { 50 panic(err) 51 } 52 53 for _, mode := range Instances { 54 buf := new(bytes.Buffer) 55 err := tl.Execute(buf, mode) 56 if err != nil { 57 panic(err) 58 } 59 60 // Formating output code 61 code, err := format.Source(buf.Bytes()) 62 if err != nil { 63 panic("error formating code") 64 } 65 66 res := string(code) 67 offset := strings.Index(res, TemplateWarning) 68 if offset == -1 { 69 panic("Missing template warning in pkg.templ.go") 70 } 71 err = ioutil.WriteFile(mode.Pkg()+"/sike.go", []byte(res[offset:]), 0o644) 72 if err != nil { 73 panic(err) 74 } 75 } 76 }