From 32046e44d0a9cdf257f6f7a35fc00506fe21d2a4 Mon Sep 17 00:00:00 2001 From: armfazh Date: Wed, 9 Oct 2024 14:06:34 -0700 Subject: [PATCH] Convert to a long test. --- sign/slhdsa/slhdsa_test.go | 53 +++++++++++++++++++++----------------- sign/slhdsa/wotsp.go | 4 ++- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/sign/slhdsa/slhdsa_test.go b/sign/slhdsa/slhdsa_test.go index 45a9b526..a592df69 100644 --- a/sign/slhdsa/slhdsa_test.go +++ b/sign/slhdsa/slhdsa_test.go @@ -2,6 +2,7 @@ package slhdsa_test import ( "crypto/rand" + "flag" "testing" "github.com/cloudflare/circl/internal/sha3" @@ -32,28 +33,34 @@ var supportedPrehashIDs = [5]slhdsa.PreHashID{ slhdsa.PreHashSHAKE256, } -func TestSlhdsa(t *testing.T) { - for i := range supportedParameters { - id := supportedParameters[i] +// Indicates whether long tests should be run +var runLongTest = flag.Bool("long", false, "runs longer tests") - t.Run(id.Name(), func(t *testing.T) { - t.Run("Keys", func(t *testing.T) { testKeys(t, id) }) +func TestSlhdsaLong(t *testing.T) { + if !*runLongTest { + t.Skip("Skipped one long test, add -long flag to run longer tests") + } - for j := range supportedPrehashIDs { - ph := supportedPrehashIDs[j] - msg := []byte("Alice and Bob") - ctx := []byte("this is a context string") - pub, priv, err := slhdsa.GenerateKey(rand.Reader, id) - test.CheckNoErr(t, err, "keygen failed") + for _, paramID := range supportedParameters { + t.Run(paramID.Name(), func(t *testing.T) { + t.Run("Keys", func(t *testing.T) { testKeys(t, paramID) }) - t.Run("Sign/"+ph.String(), func(t *testing.T) { - testSign(t, &pub, &priv, msg, ctx, ph) - }) + for _, ph := range supportedPrehashIDs { + t.Run(ph.String(), func(t *testing.T) { testSign(t, paramID, ph) }) } }) } } +func TestSlhdsa(t *testing.T) { + t.Run("Keys", func(t *testing.T) { + testKeys(t, slhdsa.ParamIDSHA2Fast128) + }) + t.Run("PreHashSHA256", func(t *testing.T) { + testSign(t, slhdsa.ParamIDSHA2Fast128, slhdsa.PreHashSHA256) + }) +} + func testKeys(t *testing.T, id slhdsa.ParamID) { reader := sha3.NewShake128() @@ -79,13 +86,13 @@ func testKeys(t *testing.T, id slhdsa.ParamID) { test.CheckOk(pub2.Equal(pub3), "public key not equal", t) } -func testSign( - t *testing.T, - pk *slhdsa.PublicKey, - sk *slhdsa.PrivateKey, - msg, ctx []byte, - ph slhdsa.PreHashID, -) { +func testSign(t *testing.T, id slhdsa.ParamID, ph slhdsa.PreHashID) { + msg := []byte("Alice and Bob") + ctx := []byte("this is a context string") + + pk, sk, err := slhdsa.GenerateKey(rand.Reader, id) + test.CheckNoErr(t, err, "keygen failed") + m, err := slhdsa.NewMessageWithPreHash(ph) test.CheckNoErr(t, err, "NewMessageWithPreHash failed") @@ -95,13 +102,13 @@ func testSign( sig, err := sk.SignRandomized(rand.Reader, &m, ctx) test.CheckNoErr(t, err, "SignRandomized failed") - valid := slhdsa.Verify(pk, &m, ctx, sig) + valid := slhdsa.Verify(&pk, &m, ctx, sig) test.CheckOk(valid, "Verify failed", t) sig, err = sk.SignDeterministic(&m, ctx) test.CheckNoErr(t, err, "SignDeterministic failed") - valid = slhdsa.Verify(pk, &m, ctx, sig) + valid = slhdsa.Verify(&pk, &m, ctx, sig) test.CheckOk(valid, "Verify failed", t) } diff --git a/sign/slhdsa/wotsp.go b/sign/slhdsa/wotsp.go index a1717604..45235482 100644 --- a/sign/slhdsa/wotsp.go +++ b/sign/slhdsa/wotsp.go @@ -22,7 +22,9 @@ func (ws *wotsSignature) fromBytes(p *params, c *cursor) { } // See FIPS 205 -- Section 5 -- Algorithm 5. -func (s *state) chain(x []byte, index, steps uint32, addr address) (out []byte) { +func (s *state) chain( + x []byte, index, steps uint32, addr address, +) (out []byte) { out = x s.F.address.Set(addr) for j := index; j < index+steps; j++ {