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