4
4
//go:build {{if .OPT_ARM}}noasm || (!amd64 && !arm64){{else}}noasm || !amd64{{end}}
5
5
// +build {{if .OPT_ARM}}noasm !amd64,!arm64{{else}}noasm !amd64{{end}}
6
6
7
- package {{ .PACKAGE}}
7
+ package {{.PACKAGE}}
8
8
9
9
import (
10
10
"math/bits"
@@ -13,30 +13,30 @@ import (
13
13
)
14
14
15
15
// Compute z = x + y (mod p).
16
- func add{{ .FIELD }}(z, x, y *common.Fp) {
16
+ func add{{.FIELD}}(z, x, y *common.Fp) {
17
17
var carry uint64
18
18
19
- // z=x+y % {{ .FIELD }}
19
+ // z=x+y % {{.FIELD}}
20
20
for i := 0; i < FpWords; i++ {
21
21
z[i], carry = bits.Add64(x[i], y[i], carry)
22
22
}
23
23
24
- // z = z - {{ .FIELD}}x2
24
+ // z = z - {{.FIELD}}x2
25
25
carry = 0
26
26
for i := 0; i < FpWords; i++ {
27
- z[i], carry = bits.Sub64(z[i], {{ .FIELD}}x2[i], carry)
27
+ z[i], carry = bits.Sub64(z[i], {{.FIELD}}x2[i], carry)
28
28
}
29
29
30
- // if z<0 add {{ .FIELD}}x2 back
30
+ // if z<0 add {{.FIELD}}x2 back
31
31
mask := uint64(0 - carry)
32
32
carry = 0
33
33
for i := 0; i < FpWords; i++ {
34
- z[i], carry = bits.Add64(z[i], {{ .FIELD}}x2[i]&mask, carry)
34
+ z[i], carry = bits.Add64(z[i], {{.FIELD}}x2[i]&mask, carry)
35
35
}
36
36
}
37
37
38
38
// Compute z = x - y (mod p).
39
- func sub{{ .FIELD }}(z, x, y *common.Fp) {
39
+ func sub{{.FIELD}}(z, x, y *common.Fp) {
40
40
var borrow uint64
41
41
42
42
for i := 0; i < FpWords; i++ {
@@ -47,14 +47,14 @@ func sub{{ .FIELD }}(z, x, y *common.Fp) {
47
47
borrow = 0
48
48
49
49
for i := 0; i < FpWords; i++ {
50
- z[i], borrow = bits.Add64(z[i], {{ .FIELD}}x2[i]&mask, borrow)
50
+ z[i], borrow = bits.Add64(z[i], {{.FIELD}}x2[i]&mask, borrow)
51
51
}
52
52
}
53
53
54
54
// If choice = 0, leave x unchanged. If choice = 1, sets x to y.
55
55
// If choice is neither 0 nor 1 then behaviour is undefined.
56
56
// This function executes in constant time.
57
- func cmov{{ .FIELD }}(x, y *common.Fp, choice uint8) {
57
+ func cmov{{.FIELD}}(x, y *common.Fp, choice uint8) {
58
58
mask := 0 - uint64(choice)
59
59
for i := 0; i < FpWords; i++ {
60
60
x[i] ^= mask & (x[i] ^ y[i])
@@ -66,7 +66,7 @@ func cmov{{ .FIELD }}(x, y *common.Fp, choice uint8) {
66
66
// For details see "Hackers Delight, 2.20"
67
67
//
68
68
// Implementation doesn't actually depend on a prime field.
69
- func cswap{{ .FIELD }}(x, y *common.Fp, mask uint8) {
69
+ func cswap{{.FIELD}}(x, y *common.Fp, mask uint8) {
70
70
var tmp, mask64 uint64
71
71
72
72
mask64 = 0 - uint64(mask)
@@ -79,17 +79,17 @@ func cswap{{ .FIELD }}(x, y *common.Fp, mask uint8) {
79
79
80
80
// Perform Montgomery reduction: set z = x R^{-1} (mod 2*p)
81
81
// with R=2^(FpWords*64). Destroys the input value.
82
- func rdc{{ .FIELD }}(z *common.Fp, x *common.FpX2) {
82
+ func rdc{{.FIELD}}(z *common.Fp, x *common.FpX2) {
83
83
var carry, t, u, v uint64
84
84
var hi, lo uint64
85
85
var count int
86
86
87
- count = {{ .FIELD}}p1Zeros
87
+ count = {{.FIELD}}p1Zeros
88
88
89
89
for i := 0; i < FpWords; i++ {
90
90
for j := 0; j < i; j++ {
91
91
if j < (i - count + 1) {
92
- hi, lo = bits.Mul64(z[j], {{ .FIELD }}p1[i-j])
92
+ hi, lo = bits.Mul64(z[j], {{.FIELD}}p1[i-j])
93
93
v, carry = bits.Add64(lo, v, 0)
94
94
u, carry = bits.Add64(hi, u, carry)
95
95
t += carry
@@ -111,7 +111,7 @@ func rdc{{ .FIELD }}(z *common.Fp, x *common.FpX2) {
111
111
}
112
112
for j := i - FpWords + 1; j < FpWords; j++ {
113
113
if j < (FpWords - count) {
114
- hi, lo = bits.Mul64(z[j], {{ .FIELD }}p1[i-j])
114
+ hi, lo = bits.Mul64(z[j], {{.FIELD}}p1[i-j])
115
115
v, carry = bits.Add64(lo, v, 0)
116
116
u, carry = bits.Add64(hi, u, carry)
117
117
t += carry
@@ -131,7 +131,7 @@ func rdc{{ .FIELD }}(z *common.Fp, x *common.FpX2) {
131
131
}
132
132
133
133
// Compute z = x * y.
134
- func mul{{ .FIELD }}(z *common.FpX2, x, y *common.Fp) {
134
+ func mul{{.FIELD}}(z *common.FpX2, x, y *common.Fp) {
135
135
var u, v, t uint64
136
136
var hi, lo uint64
137
137
var carry uint64
@@ -165,30 +165,30 @@ func mul{{ .FIELD }}(z *common.FpX2, x, y *common.Fp) {
165
165
}
166
166
167
167
// Compute z = x + y, without reducing mod p.
168
- func adl{{ .FIELD }}(z, x, y *common.FpX2) {
168
+ func adl{{.FIELD}}(z, x, y *common.FpX2) {
169
169
var carry uint64
170
170
for i := 0; i < 2*FpWords; i++ {
171
171
z[i], carry = bits.Add64(x[i], y[i], carry)
172
172
}
173
173
}
174
174
175
175
// Reduce a field element in [0, 2*p) to one in [0,p).
176
- func mod{{ .FIELD }}(x *common.Fp) {
176
+ func mod{{.FIELD}}(x *common.Fp) {
177
177
var borrow, mask uint64
178
178
for i := 0; i < FpWords; i++ {
179
- x[i], borrow = bits.Sub64(x[i], {{ .FIELD }}[i], borrow)
179
+ x[i], borrow = bits.Sub64(x[i], {{.FIELD}}[i], borrow)
180
180
}
181
181
182
182
// Sets all bits if borrow = 1
183
183
mask = 0 - borrow
184
184
borrow = 0
185
185
for i := 0; i < FpWords; i++ {
186
- x[i], borrow = bits.Add64(x[i], {{ .FIELD }}[i]&mask, borrow)
186
+ x[i], borrow = bits.Add64(x[i], {{.FIELD}}[i]&mask, borrow)
187
187
}
188
188
}
189
189
190
190
// Compute z = x - y, without reducing mod p.
191
- func sul{{ .FIELD }}(z, x, y *common.FpX2) {
191
+ func sul{{.FIELD}}(z, x, y *common.FpX2) {
192
192
var borrow, mask uint64
193
193
for i := 0; i < 2*FpWords; i++ {
194
194
z[i], borrow = bits.Sub64(x[i], y[i], borrow)
@@ -198,6 +198,6 @@ func sul{{ .FIELD }}(z, x, y *common.FpX2) {
198
198
mask = 0 - borrow
199
199
borrow = 0
200
200
for i := FpWords; i < 2*FpWords; i++ {
201
- z[i], borrow = bits.Add64(z[i], {{ .FIELD }}[i-FpWords]&mask, borrow)
201
+ z[i], borrow = bits.Add64(z[i], {{.FIELD}}[i-FpWords]&mask, borrow)
202
202
}
203
203
}
0 commit comments