Debian Codegraph @

dh/sidh/internal/templates/gen.go

raw
1 // The following directive is necessary to make the package coherent:
2
3 //go:build ignore
4 // +build ignore
5
6 // This program generates contributors.go. It can be invoked by running
7 // go generate
8 package main
9
10 import (
11 "bytes"
12 "fmt"
13 "go/format"
14 "os"
15 "strings"
16 "text/template"
17 )
18
19 var p34 = map[string]struct {
20 pow_strategy string
21 mul_strategy string
22 mul_initial int
23 }{
24 "P434": {
25 pow_strategy: "[]uint8{3, 10, 7, 5, 6, 5, 3, 8, 4, 7, 5, 6, 4, 5, 9, 6, 3, 11, 5, 5, 2, 8, 4, 7, 7, 8, 5, 6, 4, 8, 5, 2, 10, 6, 5, 4, 8, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1}",
26 mul_strategy: "[]uint8{2, 15, 9, 8, 14, 12, 2, 8, 5, 15, 8, 15, 6, 6, 3, 2, 0, 10, 9, 13, 1, 12, 3, 7, 1, 10, 8, 11, 2, 15, 14, 1, 11, 12, 14, 3, 11, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0}",
27 mul_initial: 8,
28 },
29 "P503": {
30 pow_strategy: "[]uint8{12, 5, 5, 2, 7, 11, 3, 8, 4, 11, 4, 7, 5, 6, 3, 7, 5, 7, 2, 12, 5, 6, 4, 6, 8, 6, 4, 7, 5, 5, 8, 5, 8, 5, 5, 8, 9, 3, 6, 2, 10, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3}",
31 mul_strategy: "[]uint8{12, 11, 10, 0, 1, 8, 3, 7, 1, 8, 3, 6, 7, 14, 2, 14, 14, 9, 0, 13, 9, 15, 5, 12, 7, 13, 7, 15, 6, 7, 9, 0, 5, 7, 6, 8, 8, 3, 7, 0, 10, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 3}",
32 mul_initial: 0,
33 },
34 "P751": {
35 pow_strategy: "[]uint8{5, 7, 6, 2, 10, 4, 6, 9, 8, 5, 9, 4, 7, 5, 5, 4, 8, 3, 9, 5, 5, 4, 10, 4, 6, 6, 6, 5, 8, 9, 3, 4, 9, 4, 5, 6, 6, 2, 9, 4, 5, 5, 5, 7, 7, 9, 4, 6, 4, 8, 5, 8, 6, 6, 2, 9, 7, 4, 8, 8, 8, 4, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2}",
36 mul_strategy: "[]uint8{15, 11, 10, 0, 15, 3, 3, 3, 4, 4, 9, 7, 11, 11, 5, 3, 12, 2, 10, 8, 5, 2, 8, 3, 5, 4, 11, 4, 0, 9, 2, 1, 12, 7, 5, 14, 15, 0, 14, 5, 6, 4, 5, 13, 6, 9, 7, 15, 1, 14, 11, 15, 12, 5, 0, 10, 9, 7, 7, 10, 14, 6, 11, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 1}",
37 mul_initial: 13,
38 },
39 }
40
41 // P434 optimized implementation for
42 // ARM64 is not ported yet
43 var opt_arm = map[string]bool{
44 "P434": false,
45 "P503": true,
46 "P751": true,
47 }
48
49 // Generates an 'fileNameBase.go' from 'fileNameBase.gotemp' file
50 // for a given finite 'field'. Maps placeholders to 'values'.
51 func gen(field, fileNameBase string, values interface{}) {
52 // Template files are located in ../templates and have
53 // extension .gotemp
54 templateFile := "../templates/" + fileNameBase + ".gotemp"
55 t, err := template.ParseFiles(templateFile)
56 if err != nil {
57 panic(fmt.Sprintf("Cannot open template file %s", templateFile))
58 }
59 var buf bytes.Buffer
60 err = t.Execute(&buf, values)
61 if err != nil {
62 panic("bad template execution")
63 }
64
65 // Formating output code
66 code, err := format.Source(buf.Bytes())
67 if err != nil {
68 panic("error formating code")
69 }
70
71 // name of the output .go file
72 outFileName := fileNameBase + ".go"
73 out, err := os.Create(outFileName)
74 if err != nil {
75 panic("Cannot open file")
76 }
77 _, err = out.Write(code)
78 if err != nil {
79 panic("error writing code")
80 }
81
82 err = out.Close()
83 if err != nil {
84 panic("Cant close generated file")
85 }
86 }
87
88 func main() {
89 field := os.Args[1]
90
91 s := struct {
92 FIELD string
93 PACKAGE string
94 P34_POW_STRATEGY string
95 P34_MUL_STRATEGY string
96 P34_INITIAL_MUL int
97 OPT_ARM bool
98 }{
99 FIELD: field,
100 PACKAGE: strings.ToLower(field),
101 P34_POW_STRATEGY: p34[field].pow_strategy,
102 P34_MUL_STRATEGY: p34[field].mul_strategy,
103 P34_INITIAL_MUL: p34[field].mul_initial,
104 OPT_ARM: opt_arm[field],
105 }
106
107 targets := map[string]interface{}{
108 "arith_decl": s,
109 "arith_generic": s,
110 "curve": s,
111 "fp2": s,
112 "core": s,
113
114 // tests
115 "arith_test": s,
116 "fp2_test": s,
117 "curve_test": s,
118 }
119
120 for v, s := range targets {
121 gen(field, v, s)
122 }
123 }