Skip to content

Commit

Permalink
crypto/internal/edwards25519: shorten quick.Check tests in short mode
Browse files Browse the repository at this point in the history
The edwards25519 tests can be quite slow on platforms without a
well-optimized implementation, especially if the race detector is also
enabled. Since these tests aren't checking for specific inputs anyway,
the extra coverage of a more aggressive quick.Config does not seem
worth wasting extra time on slow CI builders and TryBots.

For #60109.

Change-Id: I530e75a0b76725585df5a2f5ded6705ab1b9da51
Reviewed-on: https://go-review.googlesource.com/c/go/+/522715
Reviewed-by: Filippo Valsorda <filippo@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Joedian Reid <joedian@golang.org>
  • Loading branch information
Bryan C. Mills authored and FiloSottile committed Dec 10, 2023
1 parent 6387a56 commit 16197b4
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 32 deletions.
6 changes: 3 additions & 3 deletions extra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestScalarInvert(t *testing.T) {
return check.Equal(scOne) == 1 && isReduced(xInv.Bytes())
}

if err := quick.Check(invertWorks, quickCheckConfig32); err != nil {
if err := quick.Check(invertWorks, quickCheckConfig(32)); err != nil {
t.Error(err)
}

Expand Down Expand Up @@ -144,7 +144,7 @@ func TestMultiScalarMultMatchesBaseMult(t *testing.T) {
return p.Equal(&check) == 1
}

if err := quick.Check(multiScalarMultMatchesBaseMult, quickCheckConfig32); err != nil {
if err := quick.Check(multiScalarMultMatchesBaseMult, quickCheckConfig(32)); err != nil {
t.Error(err)
}
}
Expand All @@ -164,7 +164,7 @@ func TestVarTimeMultiScalarMultMatchesBaseMult(t *testing.T) {
return p.Equal(&check) == 1
}

if err := quick.Check(varTimeMultiScalarMultMatchesBaseMult, quickCheckConfig32); err != nil {
if err := quick.Check(varTimeMultiScalarMultMatchesBaseMult, quickCheckConfig(32)); err != nil {
t.Error(err)
}
}
Expand Down
4 changes: 2 additions & 2 deletions field/fe_alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ func TestAliasing(t *testing.T) {
var err error
switch {
case tt.oneArgF != nil:
err = quick.Check(checkAliasingOneArg(tt.oneArgF), &quick.Config{MaxCountScale: 1 << 8})
err = quick.Check(checkAliasingOneArg(tt.oneArgF), quickCheckConfig(256))
case tt.twoArgsF != nil:
err = quick.Check(checkAliasingTwoArgs(tt.twoArgsF), &quick.Config{MaxCountScale: 1 << 8})
err = quick.Check(checkAliasingTwoArgs(tt.twoArgsF), quickCheckConfig(256))
}
if err != nil {
t.Errorf("%v: %v", tt.name, err)
Expand Down
22 changes: 14 additions & 8 deletions field/fe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ func (v Element) String() string {
return hex.EncodeToString(v.Bytes())
}

// quickCheckConfig1024 will make each quickcheck test run (1024 * -quickchecks)
// times. The default value of -quickchecks is 100.
var quickCheckConfig1024 = &quick.Config{MaxCountScale: 1 << 10}
// quickCheckConfig returns a quick.Config that scales the max count by the
// given factor if the -short flag is not set.
func quickCheckConfig(slowScale int) *quick.Config {
cfg := new(quick.Config)
if !testing.Short() {
cfg.MaxCountScale = float64(slowScale)
}
return cfg
}

func generateFieldElement(rand *mathrand.Rand) Element {
const maskLow52Bits = (1 << 52) - 1
Expand Down Expand Up @@ -114,7 +120,7 @@ func TestMultiplyDistributesOverAdd(t *testing.T) {
return t1.Equal(t2) == 1 && isInBounds(t1) && isInBounds(t2)
}

if err := quick.Check(multiplyDistributesOverAdd, quickCheckConfig1024); err != nil {
if err := quick.Check(multiplyDistributesOverAdd, quickCheckConfig(1024)); err != nil {
t.Error(err)
}
}
Expand Down Expand Up @@ -419,7 +425,7 @@ func TestMult32(t *testing.T) {
return t1.Equal(t2) == 1 && isInBounds(t1) && isInBounds(t2)
}

if err := quick.Check(mult32EquivalentToMul, quickCheckConfig1024); err != nil {
if err := quick.Check(mult32EquivalentToMul, quickCheckConfig(1024)); err != nil {
t.Error(err)
}
}
Expand Down Expand Up @@ -498,7 +504,7 @@ func TestCarryPropagate(t *testing.T) {
return *t1 == *t2 && isInBounds(t2)
}

if err := quick.Check(asmLikeGeneric, quickCheckConfig1024); err != nil {
if err := quick.Check(asmLikeGeneric, quickCheckConfig(1024)); err != nil {
t.Error(err)
}

Expand All @@ -522,7 +528,7 @@ func TestFeSquare(t *testing.T) {
return t1 == t2 && isInBounds(&t2)
}

if err := quick.Check(asmLikeGeneric, quickCheckConfig1024); err != nil {
if err := quick.Check(asmLikeGeneric, quickCheckConfig(1024)); err != nil {
t.Error(err)
}
}
Expand All @@ -546,7 +552,7 @@ func TestFeMul(t *testing.T) {
b1 == b2 && isInBounds(&b2)
}

if err := quick.Check(asmLikeGeneric, quickCheckConfig1024); err != nil {
if err := quick.Check(asmLikeGeneric, quickCheckConfig(1024)); err != nil {
t.Error(err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion scalar_alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestScalarAliasing(t *testing.T) {
}, v, x, y)
},
} {
err := quick.Check(f, &quick.Config{MaxCountScale: 1 << 5})
err := quick.Check(f, quickCheckConfig(32))
if err != nil {
t.Errorf("%v: %v", name, err)
}
Expand Down
26 changes: 16 additions & 10 deletions scalar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ import (
"testing/quick"
)

// quickCheckConfig returns a quick.Config that scales the max count by the
// given factor if the -short flag is not set.
func quickCheckConfig(slowScale int) *quick.Config {
cfg := new(quick.Config)
if !testing.Short() {
cfg.MaxCountScale = float64(slowScale)
}
return cfg
}

var scOneBytes = [32]byte{1}
var scOne, _ = new(Scalar).SetCanonicalBytes(scOneBytes[:])
var scMinusOne, _ = new(Scalar).SetCanonicalBytes(scalarMinusOneBytes[:])
Expand Down Expand Up @@ -53,15 +63,11 @@ func (Scalar) Generate(rand *mathrand.Rand, size int) reflect.Value {
return reflect.ValueOf(val)
}

// quickCheckConfig1024 will make each quickcheck test run (1024 * -quickchecks)
// times. The default value of -quickchecks is 100.
var quickCheckConfig1024 = &quick.Config{MaxCountScale: 1 << 10}

func TestScalarGenerate(t *testing.T) {
f := func(sc Scalar) bool {
return isReduced(sc.Bytes())
}
if err := quick.Check(f, quickCheckConfig1024); err != nil {
if err := quick.Check(f, quickCheckConfig(1024)); err != nil {
t.Errorf("generated unreduced scalar: %v", err)
}
}
Expand All @@ -76,7 +82,7 @@ func TestScalarSetCanonicalBytes(t *testing.T) {
repr := sc.Bytes()
return bytes.Equal(in[:], repr) && isReduced(repr)
}
if err := quick.Check(f1, quickCheckConfig1024); err != nil {
if err := quick.Check(f1, quickCheckConfig(1024)); err != nil {
t.Errorf("failed bytes->scalar->bytes round-trip: %v", err)
}

Expand All @@ -86,7 +92,7 @@ func TestScalarSetCanonicalBytes(t *testing.T) {
}
return sc1 == sc2
}
if err := quick.Check(f2, quickCheckConfig1024); err != nil {
if err := quick.Check(f2, quickCheckConfig(1024)); err != nil {
t.Errorf("failed scalar->bytes->scalar round-trip: %v", err)
}

Expand Down Expand Up @@ -115,7 +121,7 @@ func TestScalarSetUniformBytes(t *testing.T) {
inBig := bigIntFromLittleEndianBytes(in[:])
return inBig.Mod(inBig, mod).Cmp(scBig) == 0
}
if err := quick.Check(f, quickCheckConfig1024); err != nil {
if err := quick.Check(f, quickCheckConfig(1024)); err != nil {
t.Error(err)
}
}
Expand Down Expand Up @@ -175,7 +181,7 @@ func TestScalarMultiplyDistributesOverAdd(t *testing.T) {
return t1 == t2 && isReduced(reprT1) && isReduced(reprT2)
}

if err := quick.Check(multiplyDistributesOverAdd, quickCheckConfig1024); err != nil {
if err := quick.Check(multiplyDistributesOverAdd, quickCheckConfig(1024)); err != nil {
t.Error(err)
}
}
Expand All @@ -194,7 +200,7 @@ func TestScalarAddLikeSubNeg(t *testing.T) {
return t1 == t2 && isReduced(t1.Bytes())
}

if err := quick.Check(addLikeSubNeg, quickCheckConfig1024); err != nil {
if err := quick.Check(addLikeSubNeg, quickCheckConfig(1024)); err != nil {
t.Error(err)
}
}
Expand Down
12 changes: 4 additions & 8 deletions scalarmult_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import (
)

var (
// quickCheckConfig32 will make each quickcheck test run (32 * -quickchecks)
// times. The default value of -quickchecks is 100.
quickCheckConfig32 = &quick.Config{MaxCountScale: 1 << 5}

// a random scalar generated using dalek.
dalekScalar, _ = (&Scalar{}).SetCanonicalBytes([]byte{219, 106, 114, 9, 174, 249, 155, 89, 69, 203, 201, 93, 92, 116, 234, 187, 78, 115, 103, 172, 182, 98, 62, 103, 187, 136, 13, 100, 248, 110, 12, 4})
// the above, times the edwards25519 basepoint.
Expand Down Expand Up @@ -83,7 +79,7 @@ func TestScalarMultDistributesOverAdd(t *testing.T) {
return check.Equal(&r) == 1
}

if err := quick.Check(scalarMultDistributesOverAdd, quickCheckConfig32); err != nil {
if err := quick.Check(scalarMultDistributesOverAdd, quickCheckConfig(32)); err != nil {
t.Error(err)
}
}
Expand All @@ -105,7 +101,7 @@ func TestScalarMultNonIdentityPoint(t *testing.T) {
return p.Equal(&q) == 1
}

if err := quick.Check(scalarMultNonIdentityPoint, quickCheckConfig32); err != nil {
if err := quick.Check(scalarMultNonIdentityPoint, quickCheckConfig(32)); err != nil {
t.Error(err)
}
}
Expand Down Expand Up @@ -149,7 +145,7 @@ func TestScalarMultMatchesBaseMult(t *testing.T) {
return p.Equal(&q) == 1
}

if err := quick.Check(scalarMultMatchesBaseMult, quickCheckConfig32); err != nil {
if err := quick.Check(scalarMultMatchesBaseMult, quickCheckConfig(32)); err != nil {
t.Error(err)
}
}
Expand Down Expand Up @@ -177,7 +173,7 @@ func TestVarTimeDoubleBaseMultMatchesBaseMult(t *testing.T) {
return p.Equal(&check) == 1
}

if err := quick.Check(varTimeDoubleBaseMultMatchesBaseMult, quickCheckConfig32); err != nil {
if err := quick.Check(varTimeDoubleBaseMultMatchesBaseMult, quickCheckConfig(32)); err != nil {
t.Error(err)
}
}
Expand Down

0 comments on commit 16197b4

Please sign in to comment.