forked from cloudflare/circl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
point_test.go
114 lines (101 loc) · 1.98 KB
/
point_test.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package ed25519
import (
"crypto/rand"
"flag"
"testing"
"github.com/cloudflare/circl/internal/test"
)
func randomPoint(P *pointR1) {
k := make([]byte, paramB)
_, _ = rand.Read(k[:])
P.fixedMult(k)
}
func TestPoint(t *testing.T) {
const testTimes = 1 << 10
t.Run("add", func(t *testing.T) {
var P pointR1
var Q pointR1
var R pointR2
for i := 0; i < testTimes; i++ {
randomPoint(&P)
_16P := P
R.fromR1(&P)
// 16P = 2^4P
for j := 0; j < 4; j++ {
_16P.double()
}
// 16P = P+P...+P
Q.SetIdentity()
for j := 0; j < 16; j++ {
Q.add(&R)
}
got := _16P.isEqual(&Q)
want := true
if got != want {
test.ReportError(t, got, want, P)
}
}
})
t.Run("fixed", func(t *testing.T) {
var P, Q, R pointR1
k := make([]byte, paramB)
l := make([]byte, paramB)
for i := 0; i < testTimes; i++ {
randomPoint(&P)
_, _ = rand.Read(k[:])
Q.fixedMult(k[:])
R.doubleMult(&P, k[:], l[:])
got := Q.isEqual(&R)
want := true
if got != want {
test.ReportError(t, got, want, P, k)
}
}
})
}
var runLongBench = flag.Bool("long", false, "runs longer benchmark")
func BenchmarkPoint(b *testing.B) {
if !*runLongBench {
b.Log("Skipped one long bench, add -long flag to run longer bench")
b.SkipNow()
}
k := make([]byte, paramB)
l := make([]byte, paramB)
_, _ = rand.Read(k)
_, _ = rand.Read(l)
var P pointR1
var Q pointR2
var R pointR3
randomPoint(&P)
Q.fromR1(&P)
b.Run("toAffine", func(b *testing.B) {
for i := 0; i < b.N; i++ {
P.toAffine()
}
})
b.Run("double", func(b *testing.B) {
for i := 0; i < b.N; i++ {
P.double()
}
})
b.Run("mixadd", func(b *testing.B) {
for i := 0; i < b.N; i++ {
P.mixAdd(&R)
}
})
b.Run("add", func(b *testing.B) {
for i := 0; i < b.N; i++ {
P.add(&Q)
}
})
b.Run("fixedMult", func(b *testing.B) {
for i := 0; i < b.N; i++ {
P.fixedMult(k)
}
})
b.Run("doubleMult", func(b *testing.B) {
for i := 0; i < b.N; i++ {
P.doubleMult(&P, k, l)
}
})
}