From da283147ec0476a0b84a3cbe54a606e73fcb3151 Mon Sep 17 00:00:00 2001 From: Eugene Zagidullin Date: Wed, 1 Apr 2026 23:56:48 +0300 Subject: [PATCH] MLDSA44PublicKey.Hash() returned incorrect type leading to a wrong b58 prefix --- crypt/crypt_regression_test.go | 48 ++++++++++++++++++++++++++++++++++ key.go | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 crypt/crypt_regression_test.go diff --git a/crypt/crypt_regression_test.go b/crypt/crypt_regression_test.go new file mode 100644 index 0000000..894d164 --- /dev/null +++ b/crypt/crypt_regression_test.go @@ -0,0 +1,48 @@ +package crypt + +import ( + "crypto/ecdsa" + "crypto/ed25519" + "crypto/elliptic" + "crypto/rand" + "testing" + + "github.com/cloudflare/circl/sign/mldsa/mldsa44" + "github.com/decred/dcrd/dcrec/secp256k1/v4" + "github.com/ecadlabs/goblst/minpk" + "github.com/ecadlabs/gotez/v2" + "github.com/stretchr/testify/require" +) + +func TestPKHType(t *testing.T) { + t.Run("Ed25519", func(t *testing.T) { + p, _, _ := ed25519.GenerateKey(rand.Reader) + pub := Ed25519PublicKey(p) + _, ok := pub.Hash().(*gotez.Ed25519PublicKeyHash) + require.True(t, ok, "public key hash has type *gotez.Ed25519PublicKeyHash") + }) + t.Run("Secp256k1", func(t *testing.T) { + k, _ := ecdsa.GenerateKey(secp256k1.S256(), rand.Reader) + pub := (*ECDSAPublicKey)(&k.PublicKey) + _, ok := pub.Hash().(*gotez.Secp256k1PublicKeyHash) + require.True(t, ok, "public key hash has type *gotez.Secp256k1PublicKeyHash") + }) + t.Run("P256", func(t *testing.T) { + k, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + pub := (*ECDSAPublicKey)(&k.PublicKey) + _, ok := pub.Hash().(*gotez.P256PublicKeyHash) + require.True(t, ok, "public key hash has type *gotez.P256PublicKeyHash") + }) + t.Run("BLS", func(t *testing.T) { + k, _ := minpk.GenerateKey(rand.Reader) + pub := (*BLSPublicKey)(k.PublicKey()) + _, ok := pub.Hash().(*gotez.BLSPublicKeyHash) + require.True(t, ok, "public key hash has type *gotez.BLSPublicKeyHash") + }) + t.Run("MLDSA44", func(t *testing.T) { + p, _, _ := mldsa44.GenerateKey(rand.Reader) + pub := (*MLDSA44PublicKey)(p) + _, ok := pub.Hash().(*gotez.MLDSA44PublicKeyHash) + require.True(t, ok, "public key hash has type *gotez.MLDSA44PublicKeyHash") + }) +} diff --git a/key.go b/key.go index 6d7e6a1..c5b2ba1 100644 --- a/key.go +++ b/key.go @@ -341,7 +341,7 @@ func (pk *MLDSA44PublicKey) Hash() PublicKeyHash { panic(err) } digest.Write(pk[:]) - var out BLSPublicKeyHash + var out MLDSA44PublicKeyHash copy(out[:], digest.Sum(nil)) return &out }