-
Notifications
You must be signed in to change notification settings - Fork 151
/
ops_purego.go
95 lines (81 loc) · 1.91 KB
/
ops_purego.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
package element
const OpsNoAsm = `
import "math/bits"
{{ $mulConsts := list 3 5 13 }}
{{- range $i := $mulConsts }}
// MulBy{{$i}} x *= {{$i}} (mod q)
func MulBy{{$i}}(x *{{$.ElementName}}) {
{{- if eq 1 $.NbWords}}
var y {{$.ElementName}}
y.SetUint64({{$i}})
x.Mul(x, &y)
{{- else}}
{{- if eq $i 3}}
_x := *x
x.Double(x).Add(x, &_x)
{{- else if eq $i 5}}
_x := *x
x.Double(x).Double(x).Add(x, &_x)
{{- else if eq $i 13}}
var y = {{$.ElementName}}{
{{- range $i := $.Thirteen}}
{{$i}},{{end}}
}
x.Mul(x, &y)
{{- else }}
NOT IMPLEMENTED
{{- end}}
{{- end}}
}
{{- end}}
// Butterfly sets
// a = a + b (mod q)
// b = a - b (mod q)
func Butterfly(a, b *{{.ElementName}}) {
_butterflyGeneric(a, b)
}
func fromMont(z *{{.ElementName}} ) {
_fromMontGeneric(z)
}
func reduce(z *{{.ElementName}}) {
_reduceGeneric(z)
}
// Mul z = x * y (mod q)
{{- if $.NoCarry}}
//
// x and y must be less than q
{{- end }}
func (z *{{.ElementName}}) Mul(x, y *{{.ElementName}}) *{{.ElementName}} {
{{- if eq $.NbWords 1}}
{{ template "mul_cios_one_limb" dict "all" . "V1" "x" "V2" "y" }}
{{- else }}
{{ mul_doc $.NoCarry }}
{{- if $.NoCarry}}
{{ template "mul_nocarry" dict "all" . "V1" "x" "V2" "y"}}
{{- else}}
{{ template "mul_cios" dict "all" . "V1" "x" "V2" "y" "ReturnZ" true}}
{{- end}}
{{ template "reduce" . }}
{{- end }}
return z
}
// Square z = x * x (mod q)
{{- if $.NoCarry}}
//
// x must be less than q
{{- end }}
func (z *{{.ElementName}}) Square(x *{{.ElementName}}) *{{.ElementName}} {
// see Mul for algorithm documentation
{{- if eq $.NbWords 1}}
{{ template "mul_cios_one_limb" dict "all" . "V1" "x" "V2" "x" }}
{{- else }}
{{- if $.NoCarry}}
{{ template "mul_nocarry" dict "all" . "V1" "x" "V2" "x"}}
{{- else}}
{{ template "mul_cios" dict "all" . "V1" "x" "V2" "x" "ReturnZ" true}}
{{- end}}
{{ template "reduce" . }}
{{- end }}
return z
}
`