Skip to content

Format templates using gtfmt tool #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions dh/sidh/internal/templates/arith_decl.gotemp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//go:build {{if .OPT_ARM}}({{end}}amd64 && !noasm{{if .OPT_ARM}}) || (arm64 && !noasm){{end}}
// +build amd64,!noasm{{if .OPT_ARM}} arm64,!noasm{{end}}

package {{ .PACKAGE}}
package {{.PACKAGE}}

import (
. "github.com/cloudflare/circl/dh/sidh/internal/common"
Expand All @@ -14,39 +14,39 @@ import (
// If choice is neither 0 nor 1 then behaviour is undefined.
// This function executes in constant time.
//go:noescape
func cmov{{ .FIELD}}(x, y *Fp, choice uint8)
func cmov{{.FIELD}}(x, y *Fp, choice uint8)

// If choice = 0, leave x,y unchanged. If choice = 1, set x,y = y,x.
// If choice is neither 0 nor 1 then behaviour is undefined.
// This function executes in constant time.
//go:noescape
func cswap{{ .FIELD}}(x, y *Fp, choice uint8)
func cswap{{.FIELD}}(x, y *Fp, choice uint8)

// Compute z = x + y (mod p).
//go:noescape
func add{{ .FIELD}}(z, x, y *Fp)
func add{{.FIELD}}(z, x, y *Fp)

// Compute z = x - y (mod p).
//go:noescape
func sub{{ .FIELD}}(z, x, y *Fp)
func sub{{.FIELD}}(z, x, y *Fp)

// Compute z = x + y, without reducing mod p.
//go:noescape
func adl{{ .FIELD}}(z, x, y *FpX2)
func adl{{.FIELD}}(z, x, y *FpX2)

// Compute z = x - y, without reducing mod p.
//go:noescape
func sul{{ .FIELD}}(z, x, y *FpX2)
func sul{{.FIELD}}(z, x, y *FpX2)

// Reduce a field element in [0, 2*p) to one in [0,p).
//go:noescape
func mod{{ .FIELD}}(x *Fp)
func mod{{.FIELD}}(x *Fp)

// Computes z = x * y.
//go:noescape
func mul{{ .FIELD}}(z *FpX2, x, y *Fp)
func mul{{.FIELD}}(z *FpX2, x, y *Fp)

// Computes the Montgomery reduction z = x R^{-1} (mod 2*p). On return value
// of x may be changed. z=x not allowed.
//go:noescape
func rdc{{ .FIELD}}(z *Fp, x *FpX2)
func rdc{{.FIELD}}(z *Fp, x *FpX2)
44 changes: 22 additions & 22 deletions dh/sidh/internal/templates/arith_generic.gotemp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//go:build {{if .OPT_ARM}}noasm || (!amd64 && !arm64){{else}}noasm || !amd64{{end}}
// +build {{if .OPT_ARM}}noasm !amd64,!arm64{{else}}noasm !amd64{{end}}

package {{ .PACKAGE}}
package {{.PACKAGE}}

import (
"math/bits"
Expand All @@ -13,30 +13,30 @@ import (
)

// Compute z = x + y (mod p).
func add{{ .FIELD }}(z, x, y *common.Fp) {
func add{{.FIELD}}(z, x, y *common.Fp) {
var carry uint64

// z=x+y % {{ .FIELD }}
// z=x+y % {{.FIELD}}
for i := 0; i < FpWords; i++ {
z[i], carry = bits.Add64(x[i], y[i], carry)
}

// z = z - {{ .FIELD}}x2
// z = z - {{.FIELD}}x2
carry = 0
for i := 0; i < FpWords; i++ {
z[i], carry = bits.Sub64(z[i], {{ .FIELD}}x2[i], carry)
z[i], carry = bits.Sub64(z[i], {{.FIELD}}x2[i], carry)
}

// if z<0 add {{ .FIELD}}x2 back
// if z<0 add {{.FIELD}}x2 back
mask := uint64(0 - carry)
carry = 0
for i := 0; i < FpWords; i++ {
z[i], carry = bits.Add64(z[i], {{ .FIELD}}x2[i]&mask, carry)
z[i], carry = bits.Add64(z[i], {{.FIELD}}x2[i]&mask, carry)
}
}

// Compute z = x - y (mod p).
func sub{{ .FIELD }}(z, x, y *common.Fp) {
func sub{{.FIELD}}(z, x, y *common.Fp) {
var borrow uint64

for i := 0; i < FpWords; i++ {
Expand All @@ -47,14 +47,14 @@ func sub{{ .FIELD }}(z, x, y *common.Fp) {
borrow = 0

for i := 0; i < FpWords; i++ {
z[i], borrow = bits.Add64(z[i], {{ .FIELD}}x2[i]&mask, borrow)
z[i], borrow = bits.Add64(z[i], {{.FIELD}}x2[i]&mask, borrow)
}
}

// If choice = 0, leave x unchanged. If choice = 1, sets x to y.
// If choice is neither 0 nor 1 then behaviour is undefined.
// This function executes in constant time.
func cmov{{ .FIELD }}(x, y *common.Fp, choice uint8) {
func cmov{{.FIELD}}(x, y *common.Fp, choice uint8) {
mask := 0 - uint64(choice)
for i := 0; i < FpWords; i++ {
x[i] ^= mask & (x[i] ^ y[i])
Expand All @@ -66,7 +66,7 @@ func cmov{{ .FIELD }}(x, y *common.Fp, choice uint8) {
// For details see "Hackers Delight, 2.20"
//
// Implementation doesn't actually depend on a prime field.
func cswap{{ .FIELD }}(x, y *common.Fp, mask uint8) {
func cswap{{.FIELD}}(x, y *common.Fp, mask uint8) {
var tmp, mask64 uint64

mask64 = 0 - uint64(mask)
Expand All @@ -79,17 +79,17 @@ func cswap{{ .FIELD }}(x, y *common.Fp, mask uint8) {

// Perform Montgomery reduction: set z = x R^{-1} (mod 2*p)
// with R=2^(FpWords*64). Destroys the input value.
func rdc{{ .FIELD }}(z *common.Fp, x *common.FpX2) {
func rdc{{.FIELD}}(z *common.Fp, x *common.FpX2) {
var carry, t, u, v uint64
var hi, lo uint64
var count int

count = {{ .FIELD}}p1Zeros
count = {{.FIELD}}p1Zeros

for i := 0; i < FpWords; i++ {
for j := 0; j < i; j++ {
if j < (i - count + 1) {
hi, lo = bits.Mul64(z[j], {{ .FIELD }}p1[i-j])
hi, lo = bits.Mul64(z[j], {{.FIELD}}p1[i-j])
v, carry = bits.Add64(lo, v, 0)
u, carry = bits.Add64(hi, u, carry)
t += carry
Expand All @@ -111,7 +111,7 @@ func rdc{{ .FIELD }}(z *common.Fp, x *common.FpX2) {
}
for j := i - FpWords + 1; j < FpWords; j++ {
if j < (FpWords - count) {
hi, lo = bits.Mul64(z[j], {{ .FIELD }}p1[i-j])
hi, lo = bits.Mul64(z[j], {{.FIELD}}p1[i-j])
v, carry = bits.Add64(lo, v, 0)
u, carry = bits.Add64(hi, u, carry)
t += carry
Expand All @@ -131,7 +131,7 @@ func rdc{{ .FIELD }}(z *common.Fp, x *common.FpX2) {
}

// Compute z = x * y.
func mul{{ .FIELD }}(z *common.FpX2, x, y *common.Fp) {
func mul{{.FIELD}}(z *common.FpX2, x, y *common.Fp) {
var u, v, t uint64
var hi, lo uint64
var carry uint64
Expand Down Expand Up @@ -165,30 +165,30 @@ func mul{{ .FIELD }}(z *common.FpX2, x, y *common.Fp) {
}

// Compute z = x + y, without reducing mod p.
func adl{{ .FIELD }}(z, x, y *common.FpX2) {
func adl{{.FIELD}}(z, x, y *common.FpX2) {
var carry uint64
for i := 0; i < 2*FpWords; i++ {
z[i], carry = bits.Add64(x[i], y[i], carry)
}
}

// Reduce a field element in [0, 2*p) to one in [0,p).
func mod{{ .FIELD }}(x *common.Fp) {
func mod{{.FIELD}}(x *common.Fp) {
var borrow, mask uint64
for i := 0; i < FpWords; i++ {
x[i], borrow = bits.Sub64(x[i], {{ .FIELD }}[i], borrow)
x[i], borrow = bits.Sub64(x[i], {{.FIELD}}[i], borrow)
}

// Sets all bits if borrow = 1
mask = 0 - borrow
borrow = 0
for i := 0; i < FpWords; i++ {
x[i], borrow = bits.Add64(x[i], {{ .FIELD }}[i]&mask, borrow)
x[i], borrow = bits.Add64(x[i], {{.FIELD}}[i]&mask, borrow)
}
}

// Compute z = x - y, without reducing mod p.
func sul{{ .FIELD }}(z, x, y *common.FpX2) {
func sul{{.FIELD}}(z, x, y *common.FpX2) {
var borrow, mask uint64
for i := 0; i < 2*FpWords; i++ {
z[i], borrow = bits.Sub64(x[i], y[i], borrow)
Expand All @@ -198,6 +198,6 @@ func sul{{ .FIELD }}(z, x, y *common.FpX2) {
mask = 0 - borrow
borrow = 0
for i := FpWords; i < 2*FpWords; i++ {
z[i], borrow = bits.Add64(z[i], {{ .FIELD }}[i-FpWords]&mask, borrow)
z[i], borrow = bits.Add64(z[i], {{.FIELD}}[i-FpWords]&mask, borrow)
}
}
30 changes: 15 additions & 15 deletions dh/sidh/internal/templates/arith_test.gotemp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots.

package {{ .PACKAGE}}
package {{.PACKAGE}}

import (
"testing"
Expand All @@ -26,14 +26,14 @@ func TestFpCswap(t *testing.T) {
x := one
y := two

cswap{{ .FIELD}}(&x, &y, 0)
cswap{{.FIELD}}(&x, &y, 0)
for i := 0; i < FpWords; i++ {
if (x[i] != one[i]) || (y[i] != two[i]) {
t.Error("Found", x, "expected", one)
}
}

cswap{{ .FIELD}}(&x, &y, 1)
cswap{{.FIELD}}(&x, &y, 1)
for i := 0; i < FpWords; i++ {
if (x[i] != two[i]) || (y[i] != one[i]) {
t.Error("Found", x, "expected", two)
Expand All @@ -48,7 +48,7 @@ func TestFpCmov(t *testing.T) {
x := one
y := two

cmov{{ .FIELD}}(&x, &y, 0)
cmov{{.FIELD}}(&x, &y, 0)
for i := 0; i < FpWords; i++ {
if x[i] != one[i] {
t.Error("Found", x, "expected", one)
Expand All @@ -58,7 +58,7 @@ func TestFpCmov(t *testing.T) {
}
}

cmov{{ .FIELD}}(&x, &y, 1)
cmov{{.FIELD}}(&x, &y, 1)
for i := 0; i < FpWords; i++ {
if x[i] != two[i] {
t.Error("Found", x, "expected", two)
Expand All @@ -72,58 +72,58 @@ func TestFpCmov(t *testing.T) {
// Benchmarking for field arithmetic
func BenchmarkMul(b *testing.B) {
for n := 0; n < b.N; n++ {
mul{{ .FIELD}}(&benchmarkFpX2, &bench_x, &bench_y)
mul{{.FIELD}}(&benchmarkFpX2, &bench_x, &bench_y)
}
}

func BenchmarkRdc(b *testing.B) {
z := bench_z

// This benchmark actually computes garbage, because
// rdc{{ .FIELD}} mangles its input, but since it's
// rdc{{.FIELD}} mangles its input, but since it's
// constant-time that shouldn't matter for the benchmarks.
for n := 0; n < b.N; n++ {
rdc{{ .FIELD}}(&benchmarkFp, &z)
rdc{{.FIELD}}(&benchmarkFp, &z)
}
}

func BenchmarkAdd(b *testing.B) {
for n := 0; n < b.N; n++ {
add{{ .FIELD}}(&benchmarkFp, &bench_x, &bench_y)
add{{.FIELD}}(&benchmarkFp, &bench_x, &bench_y)
}
}

func BenchmarkSub(b *testing.B) {
for n := 0; n < b.N; n++ {
sub{{ .FIELD}}(&benchmarkFp, &bench_x, &bench_y)
sub{{.FIELD}}(&benchmarkFp, &bench_x, &bench_y)
}
}

func BenchmarkCswap(b *testing.B) {
x, y := bench_x, bench_y
for n := 0; n < b.N; n++ {
cswap{{ .FIELD}}(&x, &y, 1)
cswap{{ .FIELD}}(&x, &y, 0)
cswap{{.FIELD}}(&x, &y, 1)
cswap{{.FIELD}}(&x, &y, 0)
}
}

func BenchmarkMod(b *testing.B) {
x := bench_x
for n := 0; n < b.N; n++ {
mod{{ .FIELD}}(&x)
mod{{.FIELD}}(&x)
}
}

func BenchmarkX2AddLazy(b *testing.B) {
x, y, z := bench_z, bench_z, bench_z
for n := 0; n < b.N; n++ {
adl{{ .FIELD}}(&x, &y, &z)
adl{{.FIELD}}(&x, &y, &z)
}
}

func BenchmarkX2SubLazy(b *testing.B) {
x, y, z := bench_z, bench_z, bench_z
for n := 0; n < b.N; n++ {
sul{{ .FIELD}}(&x, &y, &z)
sul{{.FIELD}}(&x, &y, &z)
}
}
2 changes: 1 addition & 1 deletion dh/sidh/internal/templates/core.gotemp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots.

package {{ .PACKAGE}}
package {{.PACKAGE}}

import (
. "github.com/cloudflare/circl/dh/sidh/internal/common"
Expand Down
2 changes: 1 addition & 1 deletion dh/sidh/internal/templates/curve.gotemp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots.

package {{ .PACKAGE}}
package {{.PACKAGE}}

import (
"crypto/rand"
Expand Down
2 changes: 1 addition & 1 deletion dh/sidh/internal/templates/curve_test.gotemp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots.

package {{ .PACKAGE}}
package {{.PACKAGE}}

import (
"bytes"
Expand Down
Loading