-
Notifications
You must be signed in to change notification settings - Fork 162
/
generator.go
107 lines (91 loc) · 2.42 KB
/
generator.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
package tower
import (
"path/filepath"
"strings"
"github.com/consensys/bavard"
"github.com/consensys/gurvy/internal/generators/tower/templates/fp12"
"github.com/consensys/gurvy/internal/generators/tower/templates/fp2"
"github.com/consensys/gurvy/internal/generators/tower/templates/fp6"
)
// Data data used to generate the templates
type Data struct {
Fpackage string
Fp2NonResidue string
Fp6NonResidue string
EmbeddingDegree int
// data needed in the template, always set to constants
Fp2Name string
Fp6Name string
Fp12Name string
// these members are computed as needed
TwoInv []uint64 // fp.Element, used only when Fp2NonResidue==-1 and Fp6NonResidue==(1,1). TODO there must be a better way to do this.
}
// Generate generates pairing
func Generate(d Data, outputDir string) error {
rootPath := filepath.Join(outputDir, d.Fpackage)
// inverse of 2 in fp is used by some curves
// TODO this sucks, generalize it
if d.Fp2NonResidue == "-1" && d.Fp6NonResidue == "1,1" {
d.InitTwoInv()
}
// fp2
if d.EmbeddingDegree >= 2 {
src := []string{
fp2.Base,
fp2.Inline,
fp2.Mul,
}
if err := bavard.Generate(filepath.Join(rootPath, strings.ToLower(d.Fp2Name)+".go"), src, d,
bavard.Package(d.Fpackage),
bavard.Apache2("ConsenSys AG", 2020),
bavard.GeneratedBy("gurvy/internal/generators"),
); err != nil {
return err
}
}
// fp6
if d.EmbeddingDegree >= 6 {
src := []string{
fp6.Base,
fp2.Inline,
fp6.Inline,
fp6.Mul,
}
if err := bavard.Generate(filepath.Join(rootPath, strings.ToLower(d.Fp6Name)+".go"), src, d,
bavard.Package(d.Fpackage),
bavard.Apache2("ConsenSys AG", 2020),
bavard.GeneratedBy("gurvy/internal/generators"),
); err != nil {
return err
}
}
// fp12
if d.EmbeddingDegree >= 12 {
src := []string{
fp12.Base,
fp2.Inline,
fp6.Inline,
fp12.Inline,
fp12.Mul,
}
if err := bavard.Generate(filepath.Join(rootPath, strings.ToLower(d.Fp12Name)+".go"), src, d,
bavard.Package(d.Fpackage),
bavard.Apache2("ConsenSys AG", 2020),
bavard.GeneratedBy("gurvy/internal/generators"),
); err != nil {
return err
}
}
return nil
}
// InitTwoInv set z.TwoInv to the inverse of 2 as an fp.Element
func (z *Data) InitTwoInv() *Data {
var twoInv fpElement
twoInv.SetUint64(2).Inverse(&twoInv)
z.TwoInv = twoInv[:]
return z
}
const TwoInvTemplate = `
import "github.com/consensys/gurvy/{{$.Fpackage}}/fp"
type fpElement = fp.Element
`