|
| 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 | + "go/format" |
| 11 | + "io/ioutil" |
| 12 | + "strings" |
| 13 | + "text/template" |
| 14 | +) |
| 15 | + |
| 16 | +type Param struct { |
| 17 | + Gf string |
| 18 | + PublicKeySize uint |
| 19 | + PrivateKeySize uint |
| 20 | + CiphertextSize uint |
| 21 | + SysN uint |
| 22 | + SysT uint |
| 23 | +} |
| 24 | + |
| 25 | +type Instance struct { |
| 26 | + Name string |
| 27 | + Param Param |
| 28 | +} |
| 29 | + |
| 30 | +func (m Instance) Pkg() string { |
| 31 | + return strings.ToLower(m.Name) |
| 32 | +} |
| 33 | + |
| 34 | +func (m Instance) IsSemiSystematic() bool { |
| 35 | + return strings.HasSuffix(m.Name, "f") |
| 36 | +} |
| 37 | + |
| 38 | +func (m Instance) Is348864() bool { |
| 39 | + return strings.Contains(m.Name, "348864") |
| 40 | +} |
| 41 | + |
| 42 | +func (m Instance) Is460896() bool { |
| 43 | + return strings.Contains(m.Name, "460896") |
| 44 | +} |
| 45 | + |
| 46 | +func (m Instance) Is6688128() bool { |
| 47 | + return strings.Contains(m.Name, "6688128") |
| 48 | +} |
| 49 | + |
| 50 | +func (m Instance) Is6960119() bool { |
| 51 | + return strings.Contains(m.Name, "6960119") |
| 52 | +} |
| 53 | + |
| 54 | +func (m Instance) Is8192128() bool { |
| 55 | + return strings.Contains(m.Name, "8192128") |
| 56 | +} |
| 57 | + |
| 58 | +var ( |
| 59 | + McElieceParam348864 = Param{ |
| 60 | + Gf: "gf4096", |
| 61 | + PublicKeySize: 261120, |
| 62 | + PrivateKeySize: 6492, |
| 63 | + CiphertextSize: 128, |
| 64 | + SysN: 3488, |
| 65 | + SysT: 64, |
| 66 | + } |
| 67 | + McElieceParam460896 = Param{ |
| 68 | + Gf: "gf8192", |
| 69 | + PublicKeySize: 524160, |
| 70 | + PrivateKeySize: 13608, |
| 71 | + CiphertextSize: 188, |
| 72 | + SysN: 4608, |
| 73 | + SysT: 96, |
| 74 | + } |
| 75 | + McElieceParam6688128 = Param{ |
| 76 | + Gf: "gf8192", |
| 77 | + PublicKeySize: 1044992, |
| 78 | + PrivateKeySize: 13932, |
| 79 | + CiphertextSize: 240, |
| 80 | + SysN: 6688, |
| 81 | + SysT: 128, |
| 82 | + } |
| 83 | + McElieceParam6960119 = Param{ |
| 84 | + Gf: "gf8192", |
| 85 | + PublicKeySize: 1047319, |
| 86 | + PrivateKeySize: 13948, |
| 87 | + CiphertextSize: 226, |
| 88 | + SysN: 6960, |
| 89 | + SysT: 119, |
| 90 | + } |
| 91 | + McElieceParam8192128 = Param{ |
| 92 | + Gf: "gf8192", |
| 93 | + PublicKeySize: 1357824, |
| 94 | + PrivateKeySize: 14120, |
| 95 | + CiphertextSize: 240, |
| 96 | + SysN: 8192, |
| 97 | + SysT: 128, |
| 98 | + } |
| 99 | + Instances = []Instance{ |
| 100 | + {Name: "mceliece348864", Param: McElieceParam348864}, |
| 101 | + {Name: "mceliece348864f", Param: McElieceParam348864}, |
| 102 | + {Name: "mceliece460896", Param: McElieceParam460896}, |
| 103 | + {Name: "mceliece460896f", Param: McElieceParam460896}, |
| 104 | + {Name: "mceliece6688128", Param: McElieceParam6688128}, |
| 105 | + {Name: "mceliece6688128f", Param: McElieceParam6688128}, |
| 106 | + {Name: "mceliece6960119", Param: McElieceParam6960119}, |
| 107 | + {Name: "mceliece6960119f", Param: McElieceParam6960119}, |
| 108 | + {Name: "mceliece8192128", Param: McElieceParam8192128}, |
| 109 | + {Name: "mceliece8192128f", Param: McElieceParam8192128}, |
| 110 | + } |
| 111 | + |
| 112 | + TemplateWarning = "// Code generated from" |
| 113 | +) |
| 114 | + |
| 115 | +func main() { |
| 116 | + generateTemplateFilesIf("templates/benes_348864.templ.go", "benes", func(m Instance) bool { return m.Is348864() }) |
| 117 | + generateTemplateFilesIf("templates/benes_other.templ.go", "benes", func(m Instance) bool { return !m.Is348864() }) |
| 118 | + generateTemplateFilesIf("templates/operations_6960119.templ.go", "operations", func(m Instance) bool { return m.Is6960119() }) |
| 119 | + generateTemplateFiles("templates/mceliece.templ.go", "mceliece") |
| 120 | + generateTemplateFiles("templates/pk_gen.templ.go", "pk_gen") |
| 121 | +} |
| 122 | + |
| 123 | +func generateTemplateFiles(templatePath, outputName string) { |
| 124 | + generateTemplateFilesIf(templatePath, outputName, func(instance Instance) bool { return true }) |
| 125 | +} |
| 126 | + |
| 127 | +func generateTemplateFilesIf(templatePath, outputName string, predicate func(Instance) bool) { |
| 128 | + tl, err := template.ParseFiles(templatePath) |
| 129 | + if err != nil { |
| 130 | + panic(err) |
| 131 | + } |
| 132 | + |
| 133 | + for _, mode := range Instances { |
| 134 | + if !predicate(mode) { |
| 135 | + continue |
| 136 | + } |
| 137 | + buf := new(bytes.Buffer) |
| 138 | + err := tl.Execute(buf, mode) |
| 139 | + if err != nil { |
| 140 | + panic(err) |
| 141 | + } |
| 142 | + |
| 143 | + // Formating output code |
| 144 | + code, err := format.Source(buf.Bytes()) |
| 145 | + if err != nil { |
| 146 | + panic("error formating code") |
| 147 | + } |
| 148 | + |
| 149 | + res := string(code) |
| 150 | + offset := strings.Index(res, TemplateWarning) |
| 151 | + if offset == -1 { |
| 152 | + panic("Missing template warning in pkg.templ.go") |
| 153 | + } |
| 154 | + err = ioutil.WriteFile(mode.Pkg()+"/"+outputName+".go", []byte(res[offset:]), 0o644) |
| 155 | + if err != nil { |
| 156 | + panic(err) |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments