-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathcurve_test.go
113 lines (98 loc) · 2.45 KB
/
curve_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
package goldilocks_test
import (
"crypto/rand"
"testing"
"github.com/cloudflare/circl/ecc/goldilocks"
"github.com/cloudflare/circl/internal/test"
)
func TestScalarMult(t *testing.T) {
const testTimes = 1 << 8
var e goldilocks.Curve
k := &goldilocks.Scalar{}
zero := &goldilocks.Scalar{}
t.Run("rG=0", func(t *testing.T) {
order := e.Order()
for i := 0; i < testTimes; i++ {
got := e.ScalarBaseMult(&order)
got.ToAffine()
want := e.Identity()
if !e.IsOnCurve(got) || !e.IsOnCurve(want) || !got.IsEqual(want) {
want.ToAffine()
test.ReportError(t, got, want)
}
}
})
t.Run("rP=0", func(t *testing.T) {
order := e.Order()
for i := 0; i < testTimes; i++ {
P := randomPoint()
got := e.ScalarMult(&order, P)
got.ToAffine()
want := e.Identity()
if !e.IsOnCurve(got) || !e.IsOnCurve(want) || !got.IsEqual(want) {
want.ToAffine()
test.ReportError(t, got, want, P, order)
}
}
})
t.Run("kG", func(t *testing.T) {
I := e.Identity()
for i := 0; i < testTimes; i++ {
_, _ = rand.Read(k[:])
got := e.ScalarBaseMult(k)
want := e.CombinedMult(k, zero, I) // k*G + 0*I
if !e.IsOnCurve(got) || !e.IsOnCurve(want) || !got.IsEqual(want) {
test.ReportError(t, got, want, k)
}
}
})
t.Run("kP", func(t *testing.T) {
for i := 0; i < testTimes; i++ {
P := randomPoint()
_, _ = rand.Read(k[:])
got := e.ScalarMult(k, P)
want := e.CombinedMult(zero, k, P)
if !e.IsOnCurve(got) || !e.IsOnCurve(want) || !got.IsEqual(want) {
test.ReportError(t, got, want, P, k)
}
}
})
t.Run("kG+lP", func(t *testing.T) {
G := e.Generator()
l := &goldilocks.Scalar{}
for i := 0; i < testTimes; i++ {
P := randomPoint()
_, _ = rand.Read(k[:])
_, _ = rand.Read(l[:])
kG := e.ScalarMult(k, G)
lP := e.ScalarMult(l, P)
got := e.Add(kG, lP)
want := e.CombinedMult(k, l, P)
if !e.IsOnCurve(got) || !e.IsOnCurve(want) || !got.IsEqual(want) {
test.ReportError(t, got, want, P, k, l)
}
}
})
}
func BenchmarkCurve(b *testing.B) {
var e goldilocks.Curve
var k, l goldilocks.Scalar
_, _ = rand.Read(k[:])
_, _ = rand.Read(l[:])
P := randomPoint()
b.Run("ScalarMult", func(b *testing.B) {
for i := 0; i < b.N; i++ {
P = e.ScalarMult(&k, P)
}
})
b.Run("ScalarBaseMult", func(b *testing.B) {
for i := 0; i < b.N; i++ {
e.ScalarBaseMult(&k)
}
})
b.Run("CombinedMult", func(b *testing.B) {
for i := 0; i < b.N; i++ {
P = e.CombinedMult(&k, &l, P)
}
})
}