@@ -31,8 +31,9 @@ type TestHarness[PubT crypto.PublicKey, PrivT crypto.PrivateKey] struct {
3131
3232 MultibaseCode uint64
3333
34- DefaultHash crypto.Hash
35- OtherHashes []crypto.Hash
34+ DefaultHash crypto.Hash
35+ OtherHashes []crypto.Hash
36+ SupportsPreHashed bool
3637
3738 PublicKeyBytesSize int
3839 PrivateKeyBytesSize int
@@ -330,6 +331,37 @@ func TestSuite[PubT crypto.PublicKey, PrivT crypto.PrivateKey](t *testing.T, har
330331 require .False (t , valid )
331332 })
332333 }
334+
335+ t .Run (tc .name + "-Prehashed" , func (t * testing.T ) {
336+ msg := []byte ("message" )
337+
338+ if harness .SupportsPreHashed {
339+ // Pre-hash the message with the default hash, then sign and verify the digest directly.
340+ h := tc .defaultHash .New ()
341+ h .Write (msg )
342+ digest := h .Sum (nil )
343+
344+ sig , err := tc .signer (digest , crypto .WithSigningPreHashed ())
345+ require .NoError (t , err )
346+ require .NotEmpty (t , sig )
347+
348+ valid := tc .verifier (digest , sig , crypto .WithSigningPreHashed ())
349+ require .True (t , valid )
350+
351+ // Wrong digest must not verify.
352+ wrong := make ([]byte , len (digest ))
353+ valid = tc .verifier (wrong , sig , crypto .WithSigningPreHashed ())
354+ require .False (t , valid )
355+ } else {
356+ // Key type does not support PREHASHED: sign must return an error,
357+ // verify must return false.
358+ _ , err := tc .signer (msg , crypto .WithSigningPreHashed ())
359+ require .Error (t , err )
360+
361+ valid := tc .verifier (msg , []byte ("fake" ), crypto .WithSigningPreHashed ())
362+ require .False (t , valid )
363+ }
364+ })
333365 }
334366 })
335367
0 commit comments