File: arith_test.gotemp

package info (click to toggle)
golang-github-cloudflare-circl 1.0.0%2B20200724-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,788 kB
  • sloc: asm: 19,418; ansic: 1,289; makefile: 54
file content (101 lines) | stat: -rw-r--r-- 3,147 bytes parent folder | download
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
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots.

package {{ .PACKAGE}}

import (
	"testing"

	"github.com/cloudflare/circl/dh/sidh/internal/common"
)

// Package-level storage for this field element is intended to deter
// compiler optimizations.
var (
	benchmarkFp   common.Fp
	benchmarkFpX2 common.FpX2
	bench_x       = common.Fp{17026702066521327207, 5108203422050077993, 10225396685796065916, 11153620995215874678, 6531160855165088358, 15302925148404145445, 1248821577836769963, 9789766903037985294, 7493111552032041328, 10838999828319306046, 18103257655515297935, 27403304611634}
	bench_y       = common.Fp{4227467157325093378, 10699492810770426363, 13500940151395637365, 12966403950118934952, 16517692605450415877, 13647111148905630666, 14223628886152717087, 7167843152346903316, 15855377759596736571, 4300673881383687338, 6635288001920617779, 30486099554235}
	bench_z       = common.FpX2{1595347748594595712, 10854920567160033970, 16877102267020034574, 12435724995376660096, 3757940912203224231, 8251999420280413600, 3648859773438820227, 17622716832674727914, 11029567000887241528, 11216190007549447055, 17606662790980286987, 4720707159513626555, 12887743598335030915, 14954645239176589309, 14178817688915225254, 1191346797768989683, 12629157932334713723, 6348851952904485603, 16444232588597434895, 7809979927681678066, 14642637672942531613, 3092657597757640067, 10160361564485285723, 240071237}
)

func TestFpCswap(t *testing.T) {
	var one = common.Fp{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
	var two = common.Fp{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}

	var x = one
	var y = two

	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", two)
		}
	}

	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)
		}
	}
}

// Benchmarking for field arithmetic
func BenchmarkMul(b *testing.B) {
	for n := 0; n < b.N; n++ {
		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
	// constant-time that shouldn't matter for the benchmarks.
	for n := 0; n < b.N; n++ {
		rdc{{ .FIELD}}(&benchmarkFp, &z)
	}
}

func BenchmarkAdd(b *testing.B) {
	for n := 0; n < b.N; n++ {
		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)
	}
}

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)
	}
}

func BenchmarkMod(b *testing.B) {
	x := bench_x
	for n := 0; n < b.N; n++ {
		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)
	}
}

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)
	}
}