From 16197b4051cdd9be445994c80d43145350ba7d6a Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Thu, 24 Aug 2023 14:40:14 -0400 Subject: [PATCH] crypto/internal/edwards25519: shorten quick.Check tests in short mode 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 Auto-Submit: Bryan Mills TryBot-Result: Gopher Robot Run-TryBot: Bryan Mills Reviewed-by: Joedian Reid --- extra_test.go | 6 +++--- field/fe_alias_test.go | 4 ++-- field/fe_test.go | 22 ++++++++++++++-------- scalar_alias_test.go | 2 +- scalar_test.go | 26 ++++++++++++++++---------- scalarmult_test.go | 12 ++++-------- 6 files changed, 40 insertions(+), 32 deletions(-) diff --git a/extra_test.go b/extra_test.go index 6434776..6fb832a 100644 --- a/extra_test.go +++ b/extra_test.go @@ -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) } @@ -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) } } @@ -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) } } diff --git a/field/fe_alias_test.go b/field/fe_alias_test.go index bf1efdc..0c81239 100644 --- a/field/fe_alias_test.go +++ b/field/fe_alias_test.go @@ -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) diff --git a/field/fe_test.go b/field/fe_test.go index 945a024..a24fbfe 100644 --- a/field/fe_test.go +++ b/field/fe_test.go @@ -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 @@ -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) } } @@ -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) } } @@ -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) } @@ -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) } } @@ -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) } } diff --git a/scalar_alias_test.go b/scalar_alias_test.go index ef384bf..1bca1b0 100644 --- a/scalar_alias_test.go +++ b/scalar_alias_test.go @@ -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) } diff --git a/scalar_test.go b/scalar_test.go index 67bcdaf..05551ef 100644 --- a/scalar_test.go +++ b/scalar_test.go @@ -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[:]) @@ -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) } } @@ -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) } @@ -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) } @@ -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) } } @@ -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) } } @@ -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) } } diff --git a/scalarmult_test.go b/scalarmult_test.go index 6c92ab3..4a00c79 100644 --- a/scalarmult_test.go +++ b/scalarmult_test.go @@ -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. @@ -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) } } @@ -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) } } @@ -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) } } @@ -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) } }