-
Notifications
You must be signed in to change notification settings - Fork 151
/
fixed_exp.go
69 lines (52 loc) · 1.75 KB
/
fixed_exp.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
package element
const FixedExp = `
{{- if .SqrtQ3Mod4}}
{{expByAddChain "SqrtExp" .SqrtQ3Mod4ExponentData .ElementName}}
{{- else if .SqrtAtkin}}
{{expByAddChain "SqrtExp" .SqrtAtkinExponentData .ElementName}}
{{- else if .SqrtTonelliShanks}}
{{expByAddChain "SqrtExp" .SqrtSMinusOneOver2Data .ElementName}}
{{- end }}
{{expByAddChain "LegendreExp" .LegendreExponentData .ElementName}}
{{define "expByAddChain name data eName"}}
// expBy{{.name}} is equivalent to z.Exp(x, {{ .data.N }})
//
// uses {{ .data.Meta.Module }} {{ .data.Meta.ReleaseTag }} to generate a shorter addition chain
func (z *{{.eName}}) expBy{{$.name}}(x {{.eName}}) *{{.eName}} {
// addition chain:
//
{{- range lines_ (format_ .data.Script) }}
// {{ . }}
{{- end }}
//
// Operations: {{ .data.Ops.Doubles }} squares {{ .data.Ops.Adds }} multiplies
// Allocate Temporaries.
var (
{{- range .data.Program.Temporaries }}
{{ . }} = new({{$.eName}})
{{- end -}}
)
// var {{range $i, $e := .data.Program.Temporaries }}{{ $e }} {{- if last_ $i $.data.Program.Temporaries}} {{$.eName}} {{- else }}, {{- end}}{{- end -}}
{{ range $i := .data.Program.Instructions }}
// {{ printf "Step %d: %s = x^%#x" $i.Output.Index $i.Output (index $.data.Chain $i.Output.Index) }}
{{- with add_ $i.Op }}
{{ $i.Output }}.Mul({{ ptr_ .X }}{{ .X }}, {{ ptr_ .Y }}{{ .Y }})
{{ end -}}
{{- with double_ $i.Op }}
{{ $i.Output }}.Square({{ ptr_ .X }}{{ .X }})
{{ end -}}
{{- with shift_ $i.Op -}}
{{- $first := 0 -}}
{{- if ne $i.Output.Identifier .X.Identifier }}
{{ $i.Output }}.Square({{ ptr_ .X }}{{ .X }})
{{- $first = 1 -}}
{{- end }}
for s := {{ $first }}; s < {{ .S }}; s++ {
{{ $i.Output }}.Square({{ ptr_ $i.Output }}{{ $i.Output }})
}
{{ end -}}
{{- end }}
return z
}
{{end}}
`