File: gen.go

package info (click to toggle)
golang-github-cloudflare-circl 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,060 kB
  • sloc: asm: 20,492; ansic: 1,292; makefile: 68
file content (187 lines) | stat: -rw-r--r-- 3,756 bytes parent folder | download | duplicates (2)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//go:build ignore

package main

import (
	"bytes"
	"fmt"
	"go/format"
	"math/big"
	"os"
	"path"
	"strings"
	"text/template"
)

type Fp struct {
	Name          string
	Bits          uint
	NumRootsUnity uint
	NumInverseInt uint
	Modulus       string
	Generator     string
}

func (f Fp) NumUint64() int { return int(f.Bits) / 64 }

func (f Fp) NumUint8() int { return int(f.Bits+7) / 8 }

func (f Fp) NeedsMasking() bool {
	p := f.Prime()
	return p.BitLen() < 8*f.NumUint8()
}

func (f Fp) OrderConst() (s string) {
	d := baseTwo64(f.Prime())
	for i := f.NumUint64() - 1; i >= 0; i-- {
		s += fmt.Sprintf("orderP%v = uint64(0x%v)\n", i, d[i].Text(16))
	}
	return
}

func (f Fp) OrderVar() string {
	var s []string
	for i := f.NumUint64() - 1; i >= 0; i-- {
		s = append(s, fmt.Sprintf("orderP%v", i))
	}
	return strings.Join(s, ",")
}

func (f Fp) Prime() *big.Int {
	p, _ := new(big.Int).SetString(f.Modulus, 16)
	return p
}

func (f Fp) Half() string {
	p := f.Prime()
	half := big.NewInt(2)
	half.ModInverse(half, p)
	f.toMont(half, p)
	return printDigits(half)
}

func (f Fp) RSquare() string {
	p := f.Prime()
	R2 := big.NewInt(1)
	R2.Lsh(R2, f.Bits)
	R2.Mul(R2, R2).Mod(R2, p)
	return printDigits(R2)
}

func (f Fp) RootsOfUnity() (s string) {
	p := f.Prime()
	g, _ := new(big.Int).SetString(f.Generator, 16)
	for i := range f.NumRootsUnity + 1 {
		twoI := big.NewInt(1)
		twoI.Lsh(twoI, f.NumRootsUnity-i)
		gi := new(big.Int).Exp(g, twoI, p)
		f.toMont(gi, p)
		s += "{" + printDigits(gi) + "},\n"
	}
	return
}

func (f Fp) InverseInt() (s string) {
	p := f.Prime()
	for i := range f.NumInverseInt {
		invI := new(big.Int).ModInverse(big.NewInt(int64(i+1)), p)
		f.toMont(invI, p)
		s += "{" + printDigits(invI) + "},\n"
	}
	return
}

func (f Fp) toMont(x, p *big.Int) {
	R := big.NewInt(1)
	R.Lsh(R, f.Bits)
	x.Mul(x, R).Mod(x, p)
}

func baseTwo64(v *big.Int) (d []*big.Int) {
	n := new(big.Int).Set(v)
	two64 := new(big.Int).Lsh(big.NewInt(1), 64)
	for n.Sign() > 0 {
		x := new(big.Int).Mod(n, two64)
		n.Rsh(n, 64)
		d = append(d, x)
	}
	return
}

func printDigits(n *big.Int) (s string) {
	for _, d := range baseTwo64(n) {
		s += fmt.Sprintf("0x%016x,", d)
	}
	return
}

func main() {
	const TemplateWarning = "// Code generated from"

	fields := []Fp{
		{
			Name:          "Fp64",
			Bits:          64,
			NumRootsUnity: 32,
			NumInverseInt: 8,
			// Modulus: 2^32 * 4294967295 + 1
			Modulus: "ffffffff00000001",
			// Generator: 7^4294967295
			Generator: "185629dcda58878c",
		},
		{
			Name:          "Fp128",
			Bits:          128,
			NumRootsUnity: 66,
			NumInverseInt: 8,
			// Modulus: 2^66 * 4611686018427387897 + 1
			Modulus: "ffffffffffffffe40000000000000001",
			// Generator: 7^4611686018427387897
			Generator: "6d278fbf4f60228b1f9b2759c5109f06",
		},
	}

	for _, file := range []string{"fp_test", "fp", "vector", "poly"} {
		tName := "templates/" + file + ".go.tmpl"
		tl, err := template.
			New(path.Base(tName)).
			Funcs(template.FuncMap{"ToLower": strings.ToLower}).
			ParseFiles(tName)
		if err != nil {
			panic(err)
		}

		for _, f := range fields {
			buf := new(bytes.Buffer)
			err := tl.Execute(buf, f)
			if err != nil {
				panic(err)
			}

			code := buf.Bytes()
			code, err = format.Source(code)
			if err != nil {
				panic("error formating code")
			}

			res := string(code)
			offset := strings.Index(res, TemplateWarning)
			if offset == -1 {
				panic("Missing template warning")
			}

			folder := strings.ToLower(f.Name)
			fileName := file
			if file == "fp_test" {
				folder = "."
				fileName = strings.ToLower(f.Name) + "_test"
			}

			path := fmt.Sprintf("%v/%v.go", folder, fileName)
			err = os.WriteFile(path, []byte(res[offset:]), 0o600)
			if err != nil {
				panic(err)
			}
		}
	}
}