136 lines
3 KiB
Go
136 lines
3 KiB
Go
package edwards25519
|
|
|
|
import (
|
|
"testing"
|
|
"testing/quick"
|
|
)
|
|
|
|
func TestUnsafeVarTimeScalarMultMatchesScalarBaseMult(t *testing.T) {
|
|
unsafeVarTimeScalarMultMatchesScalarMult := func(x Scalar) bool {
|
|
var p, check Point
|
|
|
|
p.UnsafeVarTimeScalarMult(&x, B)
|
|
|
|
check.ScalarBaseMult(&x)
|
|
|
|
checkOnCurve(t, &p, &check)
|
|
return p.Equal(&check) == 1
|
|
}
|
|
|
|
if err := quick.Check(unsafeVarTimeScalarMultMatchesScalarMult, quickCheckConfig(32)); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestUnsafeVarTimeScalarBaseMultMatchesScalarBaseMult(t *testing.T) {
|
|
unsafeVarTimeScalarBaseMultMatchesScalarMult := func(x Scalar) bool {
|
|
var p, check Point
|
|
|
|
p.UnsafeVarTimeScalarBaseMult(&x)
|
|
|
|
check.ScalarBaseMult(&x)
|
|
|
|
checkOnCurve(t, &p, &check)
|
|
return p.Equal(&check) == 1
|
|
}
|
|
|
|
if err := quick.Check(unsafeVarTimeScalarBaseMultMatchesScalarMult, quickCheckConfig(32)); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestUnsafeVarTimeScalarMultNonIdentityPoint(t *testing.T) {
|
|
// Check whether p.ScalarMult and q.ScalaBaseMult give the same,
|
|
// when p and q are originally set to the base point.
|
|
|
|
unsafeVarTimeScalarMultNonIdentityPoint := func(x Scalar) bool {
|
|
var p, q Point
|
|
p.Set(B)
|
|
q.Set(B)
|
|
|
|
p.UnsafeVarTimeScalarMult(&x, B)
|
|
q.ScalarBaseMult(&x)
|
|
|
|
checkOnCurve(t, &p, &q)
|
|
|
|
return p.Equal(&q) == 1
|
|
}
|
|
|
|
if err := quick.Check(unsafeVarTimeScalarMultNonIdentityPoint, quickCheckConfig(32)); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestUnsafeVarTimeScalarMultPrecomputedNonIdentityPoint(t *testing.T) {
|
|
// Check whether p.ScalarMult and q.ScalaBaseMult give the same,
|
|
// when p and q are originally set to the base point.
|
|
|
|
table := PointTablePrecompute(B)
|
|
|
|
unsafeVarTimeScalarMultPrecomputedNonIdentityPoint := func(x Scalar) bool {
|
|
var p, q Point
|
|
q.Set(B)
|
|
|
|
p.UnsafeVarTimeScalarMultPrecomputed(&x, table)
|
|
q.ScalarBaseMult(&x)
|
|
|
|
checkOnCurve(t, &p, &q)
|
|
|
|
return p.Equal(&q) == 1
|
|
}
|
|
|
|
if err := quick.Check(unsafeVarTimeScalarMultPrecomputedNonIdentityPoint, quickCheckConfig(32)); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestUnsafeVarTimeScalarBaseMultNonIdentityPoint(t *testing.T) {
|
|
// Check whether p.ScalarMult and q.ScalaBaseMult give the same,
|
|
// when p and q are originally set to the base point.
|
|
|
|
unsafeVarTimeScalarBaseMultNonIdentityPoint := func(x Scalar) bool {
|
|
var p, q Point
|
|
p.Set(B)
|
|
q.Set(B)
|
|
|
|
p.UnsafeVarTimeScalarBaseMult(&x)
|
|
q.ScalarBaseMult(&x)
|
|
|
|
checkOnCurve(t, &p, &q)
|
|
|
|
return p.Equal(&q) == 1
|
|
}
|
|
|
|
if err := quick.Check(unsafeVarTimeScalarBaseMultNonIdentityPoint, quickCheckConfig(32)); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func BenchmarkUnsafeVarTimeScalarMult(b *testing.B) {
|
|
var p Point
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
p.UnsafeVarTimeScalarMult(dalekScalar, B)
|
|
}
|
|
}
|
|
|
|
func BenchmarkUnsafeVarTimeScalarBaseMult(b *testing.B) {
|
|
var p Point
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
p.UnsafeVarTimeScalarBaseMult(dalekScalar)
|
|
}
|
|
}
|
|
|
|
func BenchmarkUnsafeVarTimeScalarMultPrecomputed(b *testing.B) {
|
|
var p Point
|
|
|
|
table := PointTablePrecompute(B)
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
p.UnsafeVarTimeScalarMultPrecomputed(dalekScalar, table)
|
|
}
|
|
}
|