Skip to content

Commit

Permalink
Simplifying test for prehash.
Browse files Browse the repository at this point in the history
  • Loading branch information
armfazh committed Oct 9, 2024
1 parent 092d7a3 commit 12020e3
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions sign/slhdsa/slhdsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,34 @@ var supportedPrehashIDs = [5]slhdsa.PreHashID{
slhdsa.PreHashSHAKE256,
}

func TestSlhdsa(t *testing.T) {
for i := range supportedParameters {
id := supportedParameters[i]
func TestSlhdsaNoPreHash(t *testing.T) {
for _, paramID := range supportedParameters {
testKeys(t, paramID)
t.Run(paramID.Name(), func(t *testing.T) { testSign(t, paramID, slhdsa.NoPreHash) })
}
}

t.Run(id.Name(), func(t *testing.T) {
t.Run("Keys", func(t *testing.T) { testKeys(t, id) })
func TestSlhdsaPreHashSHA256(t *testing.T) {
for _, paramID := range supportedParameters {
t.Run(paramID.Name(), func(t *testing.T) { testSign(t, paramID, slhdsa.PreHashSHA256) })
}
}

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")
func TestSlhdsaPreHashSHA512(t *testing.T) {
for _, paramID := range supportedParameters {
t.Run(paramID.Name(), func(t *testing.T) { testSign(t, paramID, slhdsa.PreHashSHA512) })
}
}

t.Run("Sign/"+ph.String(), func(t *testing.T) {
testSign(t, &pub, &priv, msg, ctx, ph)
})
}
})
func TestSlhdsaPreHashSHAKE128(t *testing.T) {
for _, paramID := range supportedParameters {
t.Run(paramID.Name(), func(t *testing.T) { testSign(t, paramID, slhdsa.PreHashSHAKE128) })
}
}

func TestSlhdsaPreHashSHAKE256(t *testing.T) {
for _, paramID := range supportedParameters {
t.Run(paramID.Name(), func(t *testing.T) { testSign(t, paramID, slhdsa.PreHashSHAKE256) })
}
}

Expand Down Expand Up @@ -79,13 +88,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")

Expand All @@ -95,13 +104,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)
}

Expand Down

0 comments on commit 12020e3

Please sign in to comment.