diff --git a/crypto/address.go b/crypto/address.go index 06c450d28..536a93505 100644 --- a/crypto/address.go +++ b/crypto/address.go @@ -14,9 +14,10 @@ import ( type AddressType byte const ( - AddressTypeTreasury AddressType = 0 - AddressTypeValidator AddressType = 1 - AddressTypeBLSAccount AddressType = 2 + AddressTypeTreasury AddressType = 0 + AddressTypeValidator AddressType = 1 + AddressTypeBLSAccount AddressType = 2 + AddressTypeEd25519Account AddressType = 3 ) const ( @@ -52,7 +53,11 @@ func AddressFromString(text string) (Address, error) { } // check type is valid - validTypes := []AddressType{AddressTypeValidator, AddressTypeBLSAccount} + validTypes := []AddressType{ + AddressTypeValidator, + AddressTypeBLSAccount, + AddressTypeEd25519Account, + } if !slices.Contains(validTypes, AddressType(typ)) { return Address{}, InvalidAddressTypeError(typ) } @@ -114,7 +119,8 @@ func (addr Address) Encode(w io.Writer) error { case AddressTypeTreasury: return encoding.WriteElement(w, uint8(0)) case AddressTypeValidator, - AddressTypeBLSAccount: + AddressTypeBLSAccount, + AddressTypeEd25519Account: return encoding.WriteElement(w, addr) default: return InvalidAddressTypeError(t) @@ -130,7 +136,8 @@ func (addr *Address) Decode(r io.Reader) error { case AddressTypeTreasury: return nil case AddressTypeValidator, - AddressTypeBLSAccount: + AddressTypeBLSAccount, + AddressTypeEd25519Account: return encoding.ReadElement(r, addr[1:]) default: return InvalidAddressTypeError(t) @@ -143,7 +150,8 @@ func (addr Address) SerializeSize() int { case AddressTypeTreasury: return 1 case AddressTypeValidator, - AddressTypeBLSAccount: + AddressTypeBLSAccount, + AddressTypeEd25519Account: return AddressSize default: return 0 diff --git a/crypto/address_test.go b/crypto/address_test.go index cdb835566..6bc28b3af 100644 --- a/crypto/address_test.go +++ b/crypto/address_test.go @@ -91,8 +91,8 @@ func TestToString(t *testing.T) { nil, }, { - "pc1r0hrct7eflrpw4ccrttxzs4qud2axex4dwc9mn4", - crypto.InvalidAddressTypeError(3), + "pc1y0hrct7eflrpw4ccrttxzs4qud2axex4dksmred", + crypto.InvalidAddressTypeError(4), nil, }, { @@ -137,13 +137,13 @@ func TestAddressEncoding(t *testing.T) { }, { 0, - "030000000000000000000000000000000000000000", - crypto.InvalidAddressTypeError(3), + "040000000000000000000000000000000000000000", + crypto.InvalidAddressTypeError(4), }, { 0, - "03000102030405060708090a0b0c0d0e0f0001020304", - crypto.InvalidAddressTypeError(3), + "04000102030405060708090a0b0c0d0e0f0001020304", + crypto.InvalidAddressTypeError(4), }, { 21, diff --git a/crypto/bls/bls_test.go b/crypto/bls/bls_test.go index ba4f9a497..9ea54f9ac 100644 --- a/crypto/bls/bls_test.go +++ b/crypto/bls/bls_test.go @@ -12,14 +12,14 @@ import ( ) func TestSigning(t *testing.T) { - msg := []byte("zarb") + msg := []byte("pactus") prv, _ := bls.PrivateKeyFromString( "SECRET1PDRWTLP5PX0FAHDX39GXZJP7FKZFALML0D5U9TT9KVQHDUC99CMGQQJVK67") pub, _ := bls.PublicKeyFromString( "public1p4u8hfytl2pj6l9rj0t54gxcdmna4hq52ncqkkqjf3arha5mlk3x4mzpyjkhmdl20jae7f65aamjr" + "vqcvf4sudcapz52ctcwc8r9wz3z2gwxs38880cgvfy49ta5ssyjut05myd4zgmjqstggmetyuyg7v5jhx47a") sig, _ := bls.SignatureFromString( - "ad0f88cec815e9b8af3f0136297cb242ed8b6369af723fbdac077fa927f5780db7df47c77fb53f3a22324673f000c792") + "923d67a8624cbb7972b29328e15ec76cc846076ccf00a9e94d991c677846f334ae4ba4551396fbcd6d1cab7593baf3b7") addr, _ := crypto.AddressFromString("pc1p5x2a0lkt5nrrdqe0rkcv6r4pfkmdhrr3xk73tq") sig1 := prv.Sign(msg) @@ -30,13 +30,13 @@ func TestSigning(t *testing.T) { } func TestSignatureAggregate(t *testing.T) { - msg := []byte("zarb") + msg := []byte("pactus") prv1, _ := bls.PrivateKeyFromString( "SECRET1PDRWTLP5PX0FAHDX39GXZJP7FKZFALML0D5U9TT9KVQHDUC99CMGQQJVK67") prv2, _ := bls.PrivateKeyFromString( "SECRET1PDUV97560CWDGW2DR453YPUT84REN04G0DZFAPJQL5DV0CKDAN75QCJEV6F") agg, _ := bls.SignatureFromString( - "a390ffec7061827b7e89193a26841dd9e3537b5db0af55661b624e8b93b855e9f65278850002ea72fb3098e674220eca") + "ad747172697127cb08dda29a386e106eb24ab0edfbc044014c3bd7a5f583cc38b3a223ff2c1df9c0b4df110630e6946b") sig1 := prv1.Sign(msg).(*bls.Signature) sig2 := prv2.Sign(msg).(*bls.Signature) @@ -51,8 +51,8 @@ func TestAggregateFailed(t *testing.T) { pub2, prv2 := ts.RandBLSKeyPair() pub3, prv3 := ts.RandBLSKeyPair() pub4, prv4 := ts.RandBLSKeyPair() - msg1 := []byte("zarb") - msg2 := []byte("zarb0") + msg1 := []byte("pactus") + msg2 := []byte("pactus0") sig1 := prv1.Sign(msg1).(*bls.Signature) sig11 := prv1.Sign(msg2).(*bls.Signature) @@ -103,7 +103,7 @@ func TestAggregateOnlyOneSignature(t *testing.T) { ts := testsuite.NewTestSuite(t) _, prv1 := ts.RandBLSKeyPair() - msg1 := []byte("zarb") + msg1 := []byte("pactus") sig1 := prv1.Sign(msg1).(*bls.Signature) agg1 := bls.SignatureAggregate(sig1) @@ -126,7 +126,7 @@ func TestDuplicatedAggregate(t *testing.T) { pub1, prv1 := ts.RandBLSKeyPair() pub2, prv2 := ts.RandBLSKeyPair() - msg1 := []byte("zarb") + msg1 := []byte("pactus") sig1 := prv1.Sign(msg1).(*bls.Signature) sig2 := prv2.Sign(msg1).(*bls.Signature) diff --git a/crypto/bls/private_key.go b/crypto/bls/private_key.go index f0f8a5680..741f1e25c 100644 --- a/crypto/bls/private_key.go +++ b/crypto/bls/private_key.go @@ -161,5 +161,10 @@ func (prv *PrivateKey) PublicKey() crypto.PublicKey { } func (prv *PrivateKey) EqualsTo(x crypto.PrivateKey) bool { - return prv.fr.Equal(&x.(*PrivateKey).fr) + xBLS, ok := x.(*PrivateKey) + if !ok { + return false + } + + return prv.fr.Equal(&xBLS.fr) } diff --git a/crypto/bls/private_key_test.go b/crypto/bls/private_key_test.go index 3d23e7c16..d58361c8e 100644 --- a/crypto/bls/private_key_test.go +++ b/crypto/bls/private_key_test.go @@ -15,14 +15,14 @@ func TestPrivateKeyEqualsTo(t *testing.T) { _, prv1 := ts.RandBLSKeyPair() _, prv2 := ts.RandBLSKeyPair() + _, prv3 := ts.RandEd25519KeyPair() assert.True(t, prv1.EqualsTo(prv1)) assert.False(t, prv1.EqualsTo(prv2)) - assert.Equal(t, prv1, prv1) - assert.NotEqual(t, prv1, prv2) + assert.False(t, prv1.EqualsTo(prv3)) } -func TestPrivateKeyToString(t *testing.T) { +func TestPrivateKeyFromString(t *testing.T) { tests := []struct { errMsg string encoded string diff --git a/crypto/bls/public_key.go b/crypto/bls/public_key.go index 63b4c0157..9d6aa40ac 100644 --- a/crypto/bls/public_key.go +++ b/crypto/bls/public_key.go @@ -141,7 +141,12 @@ func (pub *PublicKey) Verify(msg []byte, sig crypto.Signature) error { // EqualsTo checks if the current public key is equal to another public key. func (pub *PublicKey) EqualsTo(x crypto.PublicKey) bool { - return subtle.ConstantTimeCompare(pub.data, x.(*PublicKey).data) == 1 + xBLS, ok := x.(*PublicKey) + if !ok { + return false + } + + return subtle.ConstantTimeCompare(pub.data, xBLS.data) == 1 } // AccountAddress returns the account address derived from the public key. diff --git a/crypto/bls/public_key_test.go b/crypto/bls/public_key_test.go index 2c3735ba5..4994628f6 100644 --- a/crypto/bls/public_key_test.go +++ b/crypto/bls/public_key_test.go @@ -40,11 +40,11 @@ func TestPublicKeyEqualsTo(t *testing.T) { pub1, _ := ts.RandBLSKeyPair() pub2, _ := ts.RandBLSKeyPair() + pub3, _ := ts.RandEd25519KeyPair() assert.True(t, pub1.EqualsTo(pub1)) assert.False(t, pub1.EqualsTo(pub2)) - assert.Equal(t, pub1, pub1) - assert.NotEqual(t, pub1, pub2) + assert.False(t, pub1.EqualsTo(pub3)) } func TestPublicKeyEncoding(t *testing.T) { @@ -108,7 +108,7 @@ func TestNilSignature(t *testing.T) { assert.Error(t, pub.Verify(nil, &bls.Signature{})) } -func TestPublicKeyBytes(t *testing.T) { +func TestPublicKeyFromString(t *testing.T) { tests := []struct { errMsg string encoded string diff --git a/crypto/bls/signature.go b/crypto/bls/signature.go index 2cf6c1c8b..43cae9f70 100644 --- a/crypto/bls/signature.go +++ b/crypto/bls/signature.go @@ -90,7 +90,12 @@ func (sig *Signature) Decode(r io.Reader) error { // EqualsTo checks if the current signature is equal to another signature. func (sig *Signature) EqualsTo(x crypto.Signature) bool { - return subtle.ConstantTimeCompare(sig.data, x.(*Signature).data) == 1 + xBLS, ok := x.(*Signature) + if !ok { + return false + } + + return subtle.ConstantTimeCompare(sig.data, xBLS.data) == 1 } // PointG1 returns the point on G1 for the signature. diff --git a/crypto/bls/signature_test.go b/crypto/bls/signature_test.go index 7e098275b..17f2c17ee 100644 --- a/crypto/bls/signature_test.go +++ b/crypto/bls/signature_test.go @@ -39,14 +39,17 @@ func TestSignatureCBORMarshaling(t *testing.T) { func TestSignatureEqualsTo(t *testing.T) { ts := testsuite.NewTestSuite(t) - _, prv := ts.RandBLSKeyPair() - sig1 := prv.Sign([]byte("foo")) - sig2 := prv.Sign([]byte("bar")) + _, prv1 := ts.RandBLSKeyPair() + _, prv2 := ts.RandBLSKeyPair() + _, prv3 := ts.RandEd25519KeyPair() + + sig1 := prv1.Sign([]byte("foo")) + sig2 := prv2.Sign([]byte("foo")) + sig3 := prv3.Sign([]byte("foo")) assert.True(t, sig1.EqualsTo(sig1)) assert.False(t, sig1.EqualsTo(sig2)) - assert.Equal(t, sig1, sig1) - assert.NotEqual(t, sig1, sig2) + assert.False(t, sig1.EqualsTo(sig3)) } func TestSignatureEncoding(t *testing.T) { @@ -70,7 +73,7 @@ func TestSignatureEncoding(t *testing.T) { func TestVerifyingSignature(t *testing.T) { ts := testsuite.NewTestSuite(t) - msg := []byte("zarb") + msg := []byte("pactus") pb1, pv1 := ts.RandBLSKeyPair() pb2, pv2 := ts.RandBLSKeyPair() @@ -85,7 +88,7 @@ func TestVerifyingSignature(t *testing.T) { assert.ErrorIs(t, pb1.Verify(msg[1:], sig1), crypto.ErrInvalidSignature) } -func TestSignatureBytes(t *testing.T) { +func TestSignatureFromString(t *testing.T) { tests := []struct { errMsg string encoded string diff --git a/crypto/ed25519/ed25519_test.go b/crypto/ed25519/ed25519_test.go index 6a065c139..39ec879d4 100644 --- a/crypto/ed25519/ed25519_test.go +++ b/crypto/ed25519/ed25519_test.go @@ -1,186 +1,27 @@ package ed25519_test import ( - "encoding/hex" "testing" - bls12381 "github.com/kilic/bls12-381" "github.com/pactus-project/pactus/crypto" - "github.com/pactus-project/pactus/crypto/bls" - "github.com/pactus-project/pactus/util/testsuite" + "github.com/pactus-project/pactus/crypto/ed25519" "github.com/stretchr/testify/assert" ) func TestSigning(t *testing.T) { - msg := []byte("zarb") - prv, _ := bls.PrivateKeyFromString( - "SECRET1PDRWTLP5PX0FAHDX39GXZJP7FKZFALML0D5U9TT9KVQHDUC99CMGQQJVK67") - pub, _ := bls.PublicKeyFromString( - "public1p4u8hfytl2pj6l9rj0t54gxcdmna4hq52ncqkkqjf3arha5mlk3x4mzpyjkhmdl20jae7f65aamjr" + - "vqcvf4sudcapz52ctcwc8r9wz3z2gwxs38880cgvfy49ta5ssyjut05myd4zgmjqstggmetyuyg7v5jhx47a") - sig, _ := bls.SignatureFromString( - "ad0f88cec815e9b8af3f0136297cb242ed8b6369af723fbdac077fa927f5780db7df47c77fb53f3a22324673f000c792") - addr, _ := crypto.AddressFromString("pc1p5x2a0lkt5nrrdqe0rkcv6r4pfkmdhrr3xk73tq") + msg := []byte("pactus") + prv, _ := ed25519.PrivateKeyFromString( + "SECRET1RYY62A96X25ZAL4DPL5Z63G83GCSFCCQ7K0CMQD3MFNLYK3A6R26QUUK3Y0") + pub, _ := ed25519.PublicKeyFromString( + "public1rvqxnpfph8tnc3ck55z85w285t5jetylmmktr9wlzs0zvx7kx500szxfudh") + sig, _ := ed25519.SignatureFromString( + "361aaa09c408bfcf7e79dd90c583eeeaefe7c732ca5643cfb2ea7a6d22105b87" + + "4a412080525a855bbd5df94110a7d0083d6e386e016ecf8b7f522c339f79d305") + addr, _ := crypto.AddressFromString("pc1r7jkvfnegf0rf5ua05fzu9krjhjxcrrygl3v4nl") sig1 := prv.Sign(msg) assert.Equal(t, sig.Bytes(), sig1.Bytes()) assert.NoError(t, pub.Verify(msg, sig)) assert.Equal(t, pub, prv.PublicKey()) - assert.Equal(t, addr, pub.ValidatorAddress()) -} - -func TestSignatureAggregate(t *testing.T) { - msg := []byte("zarb") - prv1, _ := bls.PrivateKeyFromString( - "SECRET1PDRWTLP5PX0FAHDX39GXZJP7FKZFALML0D5U9TT9KVQHDUC99CMGQQJVK67") - prv2, _ := bls.PrivateKeyFromString( - "SECRET1PDUV97560CWDGW2DR453YPUT84REN04G0DZFAPJQL5DV0CKDAN75QCJEV6F") - agg, _ := bls.SignatureFromString( - "a390ffec7061827b7e89193a26841dd9e3537b5db0af55661b624e8b93b855e9f65278850002ea72fb3098e674220eca") - sig1 := prv1.Sign(msg).(*bls.Signature) - sig2 := prv2.Sign(msg).(*bls.Signature) - - assert.True(t, bls.SignatureAggregate(sig1, sig2).EqualsTo(agg)) - assert.False(t, prv1.EqualsTo(prv2)) -} - -func TestAggregateFailed(t *testing.T) { - ts := testsuite.NewTestSuite(t) - - pub1, prv1 := ts.RandBLSKeyPair() - pub2, prv2 := ts.RandBLSKeyPair() - pub3, prv3 := ts.RandBLSKeyPair() - pub4, prv4 := ts.RandBLSKeyPair() - msg1 := []byte("zarb") - msg2 := []byte("zarb0") - - sig1 := prv1.Sign(msg1).(*bls.Signature) - sig11 := prv1.Sign(msg2).(*bls.Signature) - sig2 := prv2.Sign(msg1).(*bls.Signature) - sig3 := prv3.Sign(msg1).(*bls.Signature) - sig4 := prv4.Sign(msg1).(*bls.Signature) - - agg1 := bls.SignatureAggregate(sig1, sig2, sig3) - agg2 := bls.SignatureAggregate(sig1, sig2, sig4) - agg3 := bls.SignatureAggregate(sig11, sig2, sig3) - agg4 := bls.SignatureAggregate(sig1, sig2) - agg5 := bls.SignatureAggregate(sig3, sig2, sig1) - - pubs1 := []*bls.PublicKey{pub1, pub2, pub3} - pubs2 := []*bls.PublicKey{pub1, pub2, pub4} - pubs3 := []*bls.PublicKey{pub1, pub2} - pubs4 := []*bls.PublicKey{pub3, pub2, pub1} - - pubAgg1 := bls.PublicKeyAggregate(pubs1...) - pubAgg2 := bls.PublicKeyAggregate(pubs2...) - pubAgg3 := bls.PublicKeyAggregate(pubs3...) - pubAgg4 := bls.PublicKeyAggregate(pubs4...) - - assert.NoError(t, pub1.Verify(msg1, sig1)) - assert.NoError(t, pub2.Verify(msg1, sig2)) - assert.NoError(t, pub3.Verify(msg1, sig3)) - assert.Error(t, pub2.Verify(msg1, sig1)) - assert.Error(t, pub3.Verify(msg1, sig1)) - assert.Error(t, pub1.Verify(msg1, agg1)) - assert.Error(t, pub2.Verify(msg1, agg1)) - assert.Error(t, pub3.Verify(msg1, agg1)) - - assert.NoError(t, pubAgg1.Verify(msg1, agg1)) - assert.Error(t, pubAgg1.Verify(msg2, agg1)) - assert.Error(t, pubAgg1.Verify(msg1, agg2)) - assert.Error(t, pubAgg2.Verify(msg1, agg1)) - assert.NoError(t, pubAgg2.Verify(msg1, agg2)) - assert.Error(t, pubAgg2.Verify(msg2, agg2)) - assert.Error(t, pubAgg1.Verify(msg1, agg3)) - assert.Error(t, pubAgg1.Verify(msg2, agg3)) - assert.Error(t, pubAgg1.Verify(msg1, agg4)) - assert.Error(t, pubAgg3.Verify(msg1, agg1)) - assert.NoError(t, pubAgg1.Verify(msg1, agg5)) - assert.NoError(t, pubAgg4.Verify(msg1, agg1)) -} - -func TestAggregateOnlyOneSignature(t *testing.T) { - ts := testsuite.NewTestSuite(t) - - _, prv1 := ts.RandBLSKeyPair() - msg1 := []byte("zarb") - sig1 := prv1.Sign(msg1).(*bls.Signature) - agg1 := bls.SignatureAggregate(sig1) - - assert.True(t, agg1.EqualsTo(sig1)) -} - -func TestAggregateOnlyOnePublicKey(t *testing.T) { - ts := testsuite.NewTestSuite(t) - - pub1, _ := ts.RandBLSKeyPair() - agg1 := bls.PublicKeyAggregate(pub1) - - assert.True(t, agg1.EqualsTo(pub1)) -} - -// TODO: should we check for duplication here? -func TestDuplicatedAggregate(t *testing.T) { - ts := testsuite.NewTestSuite(t) - - pub1, prv1 := ts.RandBLSKeyPair() - pub2, prv2 := ts.RandBLSKeyPair() - - msg1 := []byte("zarb") - - sig1 := prv1.Sign(msg1).(*bls.Signature) - sig2 := prv2.Sign(msg1).(*bls.Signature) - - agg1 := bls.SignatureAggregate(sig1, sig2, sig1) - agg2 := bls.SignatureAggregate(sig1, sig2) - assert.False(t, agg1.EqualsTo(agg2)) - - pubs1 := []*bls.PublicKey{pub1, pub2} - pubs2 := []*bls.PublicKey{pub1, pub2, pub1} - pubAgg1 := bls.PublicKeyAggregate(pubs1...) - pubAgg2 := bls.PublicKeyAggregate(pubs2...) - assert.False(t, pubAgg1.EqualsTo(pubAgg2)) -} - -// TestHashToCurve ensures that the hash-to-curve function in kilic/bls12-381 -// works as intended and is compatible with the spec. -// test vectors can be found here: -// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-16#appendix-J.9.1 -func TestHashToCurve(t *testing.T) { - domain := []byte("QUUX-V01-CS02-with-BLS12381G1_XMD:SHA-256_SSWU_RO_") - tests := []struct { - msg string - expected string - }{ - { - "", - "052926add2207b76ca4fa57a8734416c8dc95e24501772c814278700eed6d1e4e8cf62d9c09db0fac349612b759e79a1" + - "08ba738453bfed09cb546dbb0783dbb3a5f1f566ed67bb6be0e8c67e2e81a4cc68ee29813bb7994998f3eae0c9c6a265", - }, - { - "abc", - "03567bc5ef9c690c2ab2ecdf6a96ef1c139cc0b2f284dca0a9a7943388a49a3aee664ba5379a7655d3c68900be2f6903" + - "0b9c15f3fe6e5cf4211f346271d7b01c8f3b28be689c8429c85b67af215533311f0b8dfaaa154fa6b88176c229f2885d", - }, - { - "abcdef0123456789", - "11e0b079dea29a68f0383ee94fed1b940995272407e3bb916bbf268c263ddd57a6a27200a784cbc248e84f357ce82d98" + - "03a87ae2caf14e8ee52e51fa2ed8eefe80f02457004ba4d486d6aa1f517c0889501dc7413753f9599b099ebcbbd2d709", - }, - { - "q128_qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" + - "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", - "15f68eaa693b95ccb85215dc65fa81038d69629f70aeee0d0f677cf22285e7bf58d7cb86eefe8f2e9bc3f8cb84fac488" + - "1807a1d50c29f430b8cafc4f8638dfeeadf51211e1602a5f184443076715f91bb90a48ba1e370edce6ae1062f5e6dd38", - }, - } - - g1 := bls12381.NewG1() - for no, test := range tests { - mappedPoint, _ := g1.HashToCurve([]byte(test.msg), domain) - d, _ := hex.DecodeString(test.expected) - expectedPoint, _ := g1.FromBytes(d) - assert.Equal(t, expectedPoint, mappedPoint, - "test %v: not match", no) - } + assert.Equal(t, addr, pub.AccountAddress()) } diff --git a/crypto/ed25519/private_key.go b/crypto/ed25519/private_key.go index 0a0820dde..00576fd54 100644 --- a/crypto/ed25519/private_key.go +++ b/crypto/ed25519/private_key.go @@ -18,7 +18,7 @@ type PrivateKey struct { } // PrivateKeyFromString decodes the input string and returns the PrivateKey -// if the string is a valid bech32m encoding of a BLS public key. +// if the string is a valid bech32m encoding of a Ed25519 public key. func PrivateKeyFromString(text string) (*PrivateKey, error) { // Decode the bech32m encoded private key. hrp, typ, data, err := bech32m.DecodeToBase256WithTypeNoLimit(text) @@ -39,12 +39,6 @@ func PrivateKeyFromString(text string) (*PrivateKey, error) { return PrivateKeyFromBytes(data) } -func KeyGen(seed []byte) (*PrivateKey, error) { - prv := ed25519.NewKeyFromSeed(seed) - - return PrivateKeyFromBytes(prv) -} - // PrivateKeyFromBytes constructs a ED25519 private key from the raw bytes. func PrivateKeyFromBytes(data []byte) (*PrivateKey, error) { if len(data) != PrivateKeySize { diff --git a/crypto/ed25519/private_key_test.go b/crypto/ed25519/private_key_test.go index 447650688..d8a4fac2f 100644 --- a/crypto/ed25519/private_key_test.go +++ b/crypto/ed25519/private_key_test.go @@ -1,8 +1,6 @@ package ed25519_test import ( - "encoding/hex" - "fmt" "strings" "testing" @@ -14,15 +12,13 @@ import ( func TestPrivateKeyEqualsTo(t *testing.T) { ts := testsuite.NewTestSuite(t) - _, prv1 := ts.RandED25519KeyPair() - _, prv2 := ts.RandED25519KeyPair() - - fmt.Println(prv1.String()) + _, prv1 := ts.RandEd25519KeyPair() + _, prv2 := ts.RandEd25519KeyPair() + _, prv3 := ts.RandBLSKeyPair() assert.True(t, prv1.EqualsTo(prv1)) assert.False(t, prv1.EqualsTo(prv2)) - assert.Equal(t, prv1, prv1) - assert.NotEqual(t, prv1, prv2) + assert.False(t, prv1.EqualsTo(prv3)) } func TestPrivateKeyFromString(t *testing.T) { @@ -34,29 +30,27 @@ func TestPrivateKeyFromString(t *testing.T) { }{ { "invalid separator index -1", - "XXXXXXR2SYCC5TDQKMJ73J64J8GJTMTKREEQNQAS0M5SLZ9LVJV7Y940NVZQD9JUS" + - "GV2N44C9H5PVGRXARNGZ7QF3PSKH7805E5SZXPE7ZHHAGX0NFQR", + "not_proper_encoded", false, nil, }, { - "invalid checksum (expected s9c56g got czlgh0)", - "SECRET1RAC7048K666DCCYG7FJW68ZE2G6P32UAPLRLWDV3RTAR4PWZUX2CDSFAL55VM" + - "YS06CY35LA72AWZN5DY5NZA078S4S4K654UFJ0YCCZLGH0", + "invalid checksum (expected uuk3y0 got uuk30y)", + "SECRET1RYY62A96X25ZAL4DPL5Z63G83GCSFCCQ7K0CMQD3MFNLYK3A6R26QUUK30Y", false, nil, }, { - "invalid bech32 string length 0", - "", + "invalid HRP: xxx", + "XXX1RYY62A96X25ZAL4DPL5Z63G83GCSFCCQ7K0CMQD3MFNLYK3A6R26Q8JXUV6", false, nil, }, { - "invalid character not part of charset: 105", - "SECRET1IOIOOI", + "invalid private key type: 4", + "SECRET1YVKPE43FDU9TC4C8LPFD4JY9METET3GEKQE7E7ECK4EJYV20WVAPQZCU0KL", false, nil, }, { - "invalid bech32 string length 0", - "SECRET1HPZZU9", + "private key should be 32 bytes, but it is 31 bytes", + "SECRET1RDRWTLP5PX0FAHDX39GXZJP7FKZFALML0D5U9TT9KVQHDUC99CCPV3HNE", false, nil, }, { @@ -64,9 +58,8 @@ func TestPrivateKeyFromString(t *testing.T) { "SECRET1RJ6STNTA7Y3P2QLQF8A6QCX05F2H5TFNE5RSH066KZME4WVFXKE7QW097LG", true, []byte{ - 0x96, 0xa0, 0xb9, 0xaf, 0xbe, 0x24, 0x42, 0xa0, 0x7c, 0x9, 0x3f, 0x74, 0xc, 0x19, 0xf4, - 0x4a, 0xaf, 0x45, 0xa6, 0x79, 0xa0, 0xe1, 0x77, 0xeb, 0x56, 0x16, 0xf3, 0x57, 0x31, 0x26, - 0xb6, 0x7c, + 0x96, 0xa0, 0xb9, 0xaf, 0xbe, 0x24, 0x42, 0xa0, 0x7c, 0x09, 0x3f, 0x74, 0x0c, 0x19, 0xf4, 0x4a, + 0xaf, 0x45, 0xa6, 0x79, 0xa0, 0xe1, 0x77, 0xeb, 0x56, 0x16, 0xf3, 0x57, 0x31, 0x26, 0xb6, 0x7c, }, }, } @@ -82,24 +75,3 @@ func TestPrivateKeyFromString(t *testing.T) { } } } - -// TestKeyGen ensures the KeyGen function works as intended. -func TestKeyGen(t *testing.T) { - tests := []struct { - seed []byte - sk string - }{} - - for i, test := range tests { - prv, err := ed25519.KeyGen(test.seed) - if test.sk == "Err" { - assert.Error(t, err, - "test '%v' failed. no error", i) - } else { - assert.NoError(t, err, - "test'%v' failed. has error", i) - assert.Equal(t, test.sk, hex.EncodeToString(prv.Bytes()), - "test '%v' failed. not equal", i) - } - } -} diff --git a/crypto/ed25519/public_key.go b/crypto/ed25519/public_key.go index 24f0ae2c4..5978a7293 100644 --- a/crypto/ed25519/public_key.go +++ b/crypto/ed25519/public_key.go @@ -22,7 +22,7 @@ type PublicKey struct { } // PublicKeyFromString decodes the input string and returns the PublicKey -// if the string is a valid bech32m encoding of a BLS public key. +// if the string is a valid bech32m encoding of a Ed25519 public key. func PublicKeyFromString(text string) (*PublicKey, error) { // Decode the bech32m encoded public key. hrp, typ, data, err := bech32m.DecodeToBase256WithTypeNoLimit(text) @@ -42,7 +42,7 @@ func PublicKeyFromString(text string) (*PublicKey, error) { return PublicKeyFromBytes(data) } -// PublicKeyFromBytes constructs a BLS public key from the raw bytes. +// PublicKeyFromBytes constructs a Ed25519 public key from the raw bytes. func PublicKeyFromBytes(data []byte) (*PublicKey, error) { if len(data) != PublicKeySize { return nil, errors.Errorf(errors.ErrInvalidPublicKey, @@ -54,14 +54,14 @@ func PublicKeyFromBytes(data []byte) (*PublicKey, error) { // Bytes returns the raw byte representation of the public key. func (pub *PublicKey) Bytes() []byte { - return pub.inner[:PublicKeySize] + return pub.inner } -// String returns a human-readable string for the BLS public key. +// String returns a human-readable string for the Ed25519 public key. func (pub *PublicKey) String() string { str, _ := bech32m.EncodeFromBase256WithType( crypto.PublicKeyHRP, - crypto.SignatureTypeBLS, + crypto.SignatureTypeEd25519, pub.Bytes()) return str @@ -128,7 +128,7 @@ func (pub *PublicKey) EqualsTo(x crypto.PublicKey) bool { // AccountAddress returns the account address derived from the public key. func (pub *PublicKey) AccountAddress() crypto.Address { data := hash.Hash160(hash.Hash256(pub.Bytes())) - addr := crypto.NewAddress(crypto.AddressTypeBLSAccount, data) + addr := crypto.NewAddress(crypto.AddressTypeEd25519Account, data) return addr } diff --git a/crypto/ed25519/public_key_test.go b/crypto/ed25519/public_key_test.go index 2be99a142..ac3ae817f 100644 --- a/crypto/ed25519/public_key_test.go +++ b/crypto/ed25519/public_key_test.go @@ -7,18 +7,17 @@ import ( cbor "github.com/fxamacker/cbor/v2" "github.com/pactus-project/pactus/crypto" - "github.com/pactus-project/pactus/crypto/bls" + "github.com/pactus-project/pactus/crypto/ed25519" "github.com/pactus-project/pactus/util" "github.com/pactus-project/pactus/util/testsuite" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestPublicKeyCBORMarshaling(t *testing.T) { ts := testsuite.NewTestSuite(t) - pub1, _ := ts.RandBLSKeyPair() - pub2 := new(bls.PublicKey) + pub1, _ := ts.RandEd25519KeyPair() + pub2 := new(ed25519.PublicKey) bs, err := pub1.MarshalCBOR() assert.NoError(t, err) @@ -27,88 +26,77 @@ func TestPublicKeyCBORMarshaling(t *testing.T) { assert.Error(t, pub2.UnmarshalCBOR([]byte("abcd"))) - inv, _ := hex.DecodeString(strings.Repeat("ff", bls.PublicKeySize)) + inv, _ := hex.DecodeString(strings.Repeat("ff", ed25519.PublicKeySize)) data, _ := cbor.Marshal(inv) assert.NoError(t, pub2.UnmarshalCBOR(data)) - - _, err = pub2.PointG2() - assert.Error(t, err) } func TestPublicKeyEqualsTo(t *testing.T) { ts := testsuite.NewTestSuite(t) - pub1, _ := ts.RandBLSKeyPair() - pub2, _ := ts.RandBLSKeyPair() + pub1, _ := ts.RandEd25519KeyPair() + pub2, _ := ts.RandEd25519KeyPair() + pub3, _ := ts.RandBLSKeyPair() assert.True(t, pub1.EqualsTo(pub1)) assert.False(t, pub1.EqualsTo(pub2)) - assert.Equal(t, pub1, pub1) - assert.NotEqual(t, pub1, pub2) + assert.False(t, pub1.EqualsTo(pub3)) } func TestPublicKeyEncoding(t *testing.T) { ts := testsuite.NewTestSuite(t) - pub, _ := ts.RandBLSKeyPair() + pub, _ := ts.RandEd25519KeyPair() w1 := util.NewFixedWriter(20) assert.Error(t, pub.Encode(w1)) - w2 := util.NewFixedWriter(bls.PublicKeySize) + w2 := util.NewFixedWriter(ed25519.PublicKeySize) assert.NoError(t, pub.Encode(w2)) r1 := util.NewFixedReader(20, w2.Bytes()) assert.Error(t, pub.Decode(r1)) - r2 := util.NewFixedReader(bls.PublicKeySize, w2.Bytes()) + r2 := util.NewFixedReader(ed25519.PublicKeySize, w2.Bytes()) assert.NoError(t, pub.Decode(r2)) } func TestPublicKeyVerifyAddress(t *testing.T) { ts := testsuite.NewTestSuite(t) - pub1, _ := ts.RandBLSKeyPair() - pub2, _ := ts.RandBLSKeyPair() + pub1, _ := ts.RandEd25519KeyPair() + pub2, _ := ts.RandEd25519KeyPair() err := pub1.VerifyAddress(pub1.AccountAddress()) assert.NoError(t, err) - err = pub1.VerifyAddress(pub1.ValidatorAddress()) - assert.NoError(t, err) err = pub1.VerifyAddress(pub2.AccountAddress()) assert.Equal(t, crypto.AddressMismatchError{ Expected: pub1.AccountAddress(), Got: pub2.AccountAddress(), }, err) - - err = pub1.VerifyAddress(pub2.ValidatorAddress()) - assert.Equal(t, crypto.AddressMismatchError{ - Expected: pub1.ValidatorAddress(), - Got: pub2.ValidatorAddress(), - }, err) } func TestNilPublicKey(t *testing.T) { ts := testsuite.NewTestSuite(t) - pub := &bls.PublicKey{} - randSig := ts.RandBLSSignature() + pub, _ := ts.RandEd25519KeyPair() + randSig := ts.RandEd25519Signature() assert.Error(t, pub.VerifyAddress(ts.RandAccAddress())) assert.Error(t, pub.VerifyAddress(ts.RandValAddress())) assert.Error(t, pub.Verify(nil, nil)) - assert.Error(t, pub.Verify(nil, &bls.Signature{})) assert.Error(t, pub.Verify(nil, randSig)) } func TestNilSignature(t *testing.T) { ts := testsuite.NewTestSuite(t) - pub, _ := ts.RandBLSKeyPair() + pub, _ := ts.RandEd25519KeyPair() + randSig := ts.RandEd25519Signature() assert.Error(t, pub.Verify(nil, nil)) - assert.Error(t, pub.Verify(nil, &bls.Signature{})) + assert.Error(t, pub.Verify(nil, randSig)) } -func TestPublicKeyBytes(t *testing.T) { +func TestPublicKeyFromString(t *testing.T) { tests := []struct { errMsg string encoded string @@ -121,62 +109,38 @@ func TestPublicKeyBytes(t *testing.T) { false, nil, }, { - "invalid bech32 string length 0", - "", - false, nil, - }, - { - "invalid character not part of charset: 105", - "public1ioiooi", - false, nil, - }, - { - "invalid bech32 string length 0", - "public134jkgz", + "invalid checksum (expected ztd56p got ztd5p6)", + "public1rafnl324uwngqdq455ax4e52fedmfcvskkwas6wsau0u0nwj4g96qztd5p6", false, nil, }, { "invalid HRP: xxx", - "xxx1p4u8hfytl2pj6l9rj0t54gxcdmna4hq52ncqkkqjf3arha5mlk3x4mzpyjkhmdl20jae7f65aamjrvqc" + - "vf4sudcapz52ctcwc8r9wz3z2gwxs38880cgvfy49ta5ssyjut05myd4zgmjqstggmetyuyg7v5evslaq", - false, nil, - }, - { - "invalid checksum (expected jhx47a got jhx470)", - "public1p4u8hfytl2pj6l9rj0t54gxcdmna4hq52ncqkkqjf3arha5mlk3x4mzpyjkhmdl20jae7f65aamjr" + - "vqcvf4sudcapz52ctcwc8r9wz3z2gwxs38880cgvfy49ta5ssyjut05myd4zgmjqstggmetyuyg7v5jhx470", + "xxx1rafnl324uwngqdq455ax4e52fedmfcvskkwas6wsau0u0nwj4g96qvguamu", false, nil, }, { - "public key should be 96 bytes, but it is 95 bytes", - "public1p4u8hfytl2pj6l9rj0t54gxcdmna4hq52ncqkkqjf3arha5mlk3x4mzpyjkhmdl20jae7f65aamjr" + - "vqcvf4sudcapz52ctcwc8r9wz3z2gwxs38880cgvfy49ta5ssyjut05myd4zgmjqstggmetyuyg73y98kl", + "public key should be 32 bytes, but it is 31 bytes", + "public1ruwz86xyvhyehy8g7wg98jsmy07cfkjp6dy8zwxa8hqtdj99hquk7xyus", false, nil, }, { - "invalid public key type: 2", - "public1z372l5frmm5e7cn7ewfjdkx5t7y62kztqr82rtatat70cl8p8ng3rdzr02mzpwcfl6s2v26kry6mwg" + - "xpqy92ywx9wtff80mc9p3kr4cmhgekj048gavx2zdh78tsnh7eg5jzdw6s3et6c0dqyp22vslcgkukxh4l4", + "invalid public key type: 4", + "public1yafnl324uwngqdq455ax4e52fedmfcvskkwas6wsau0u0nwj4g96qdnx0mf", false, nil, }, { "", - "public1p4u8hfytl2pj6l9rj0t54gxcdmna4hq52ncqkkqjf3arha5mlk3x4mzpyjkhmdl20jae7f65aamjr" + - "vqcvf4sudcapz52ctcwc8r9wz3z2gwxs38880cgvfy49ta5ssyjut05myd4zgmjqstggmetyuyg7v5jhx47a", + "public1rafnl324uwngqdq455ax4e52fedmfcvskkwas6wsau0u0nwj4g96qztd56p", true, []byte{ - 0xaf, 0x0f, 0x74, 0x91, 0x7f, 0x50, 0x65, 0xaf, 0x94, 0x72, 0x7a, 0xe9, 0x54, 0x1b, 0x0d, 0xdc, - 0xfb, 0x5b, 0x82, 0x8a, 0x9e, 0x01, 0x6b, 0x02, 0x49, 0x8f, 0x47, 0x7e, 0xd3, 0x7f, 0xb4, 0x4d, 0x5d, - 0x88, 0x24, 0x95, 0xaf, 0xb6, 0xfd, 0x4f, 0x97, 0x73, 0xe4, 0xea, 0x9d, 0xee, 0xe4, 0x36, 0x03, 0x0c, - 0x4d, 0x61, 0xc6, 0xe3, 0xa1, 0x15, 0x15, 0x85, 0xe1, 0xd8, 0x38, 0xca, 0xe1, 0x44, 0x4a, 0x43, 0x8d, - 0x08, 0x9c, 0xe7, 0x7e, 0x10, 0xc4, 0x92, 0xa5, 0x5f, 0x69, 0x08, 0x12, 0x5c, 0x5b, 0xe9, 0xb2, 0x36, - 0xa2, 0x46, 0xe4, 0x08, 0x2d, 0x08, 0xde, 0x56, 0x4e, 0x11, 0x1e, 0x65, + 0xea, 0x67, 0xf8, 0xaa, 0xbc, 0x74, 0xd0, 0x06, 0x82, 0xb4, 0xa7, 0x4d, 0x5c, 0xd1, 0x49, 0xcb, + 0x76, 0x9c, 0x32, 0x16, 0xb3, 0xbb, 0x0d, 0x3a, 0x1d, 0xe3, 0xf8, 0xf9, 0xba, 0x55, 0x41, 0x74, }, }, } for no, test := range tests { - pub, err := bls.PublicKeyFromString(test.encoded) + pub, err := ed25519.PublicKeyFromString(test.encoded) if test.valid { assert.NoError(t, err, "test %v: unexpected error", no) assert.Equal(t, test.result, pub.Bytes(), "test %v: invalid bytes", no) @@ -186,48 +150,3 @@ func TestPublicKeyBytes(t *testing.T) { } } } - -func TestPointG2(t *testing.T) { - tests := []struct { - errMsg string - encoded string - valid bool - }{ - { - "compression flag must be set", - "public1pqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" + - "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqjzu9w8", - false, - }, - { - "input string must be zero when infinity flag is set", - "public1pllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll" + - "llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllluhpuzyf", - false, - }, - { - "public key is zero", - "public1pcqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" + - "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqglnhh9", - false, - }, - { - "", - "public1p4u8hfytl2pj6l9rj0t54gxcdmna4hq52ncqkkqjf3arha5mlk3x4mzpyjkhmdl20jae7f65aamjr" + - "vqcvf4sudcapz52ctcwc8r9wz3z2gwxs38880cgvfy49ta5ssyjut05myd4zgmjqstggmetyuyg7v5jhx47a", - true, - }, - } - - for no, test := range tests { - pub, err := bls.PublicKeyFromString(test.encoded) - require.NoError(t, err) - - _, err = pub.PointG2() - if test.valid { - assert.NoError(t, err, "test %v: unexpected error", no) - } else { - assert.Contains(t, err.Error(), test.errMsg, "test %v: error not matched", no) - } - } -} diff --git a/crypto/ed25519/signature.go b/crypto/ed25519/signature.go index 98c08d2d6..76c38b01b 100644 --- a/crypto/ed25519/signature.go +++ b/crypto/ed25519/signature.go @@ -21,7 +21,7 @@ type Signature struct { } // SignatureFromString decodes the input string and returns the Signature -// if the string is a valid hexadecimal encoding of a BLS signature. +// if the string is a valid hexadecimal encoding of a Ed25519 signature. func SignatureFromString(text string) (*Signature, error) { data, err := hex.DecodeString(text) if err != nil { @@ -31,7 +31,7 @@ func SignatureFromString(text string) (*Signature, error) { return SignatureFromBytes(data) } -// SignatureFromBytes constructs a BLS signature from the raw bytes. +// SignatureFromBytes constructs a Ed25519 signature from the raw bytes. func SignatureFromBytes(data []byte) (*Signature, error) { if len(data) != SignatureSize { return nil, errors.Errorf(errors.ErrInvalidSignature, @@ -86,6 +86,11 @@ func (sig *Signature) Decode(r io.Reader) error { } // EqualsTo checks if the current signature is equal to another signature. -func (sig *Signature) EqualsTo(right crypto.Signature) bool { - return subtle.ConstantTimeCompare(sig.data, right.(*Signature).data) == 1 +func (sig *Signature) EqualsTo(x crypto.Signature) bool { + xEd25519, ok := x.(*Signature) + if !ok { + return false + } + + return subtle.ConstantTimeCompare(sig.data, xEd25519.data) == 1 } diff --git a/crypto/ed25519/signature_test.go b/crypto/ed25519/signature_test.go index 78bc94803..1d507c08c 100644 --- a/crypto/ed25519/signature_test.go +++ b/crypto/ed25519/signature_test.go @@ -7,19 +7,18 @@ import ( cbor "github.com/fxamacker/cbor/v2" "github.com/pactus-project/pactus/crypto" - "github.com/pactus-project/pactus/crypto/bls" + "github.com/pactus-project/pactus/crypto/ed25519" "github.com/pactus-project/pactus/util" "github.com/pactus-project/pactus/util/testsuite" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestSignatureCBORMarshaling(t *testing.T) { ts := testsuite.NewTestSuite(t) - _, prv := ts.RandBLSKeyPair() + _, prv := ts.RandEd25519KeyPair() sig1 := prv.Sign(ts.RandBytes(16)) - sig2 := new(bls.Signature) + sig2 := new(ed25519.Signature) bs, err := sig1.MarshalCBOR() assert.NoError(t, err) @@ -28,52 +27,52 @@ func TestSignatureCBORMarshaling(t *testing.T) { assert.Error(t, sig2.UnmarshalCBOR([]byte("abcd"))) - inv, _ := hex.DecodeString(strings.Repeat("ff", bls.SignatureSize)) + inv, _ := hex.DecodeString(strings.Repeat("ff", ed25519.SignatureSize)) data, _ := cbor.Marshal(inv) assert.NoError(t, sig2.UnmarshalCBOR(data)) - - _, err = sig2.PointG1() - assert.Error(t, err) } func TestSignatureEqualsTo(t *testing.T) { ts := testsuite.NewTestSuite(t) - _, prv := ts.RandBLSKeyPair() - sig1 := prv.Sign([]byte("foo")) - sig2 := prv.Sign([]byte("bar")) + _, prv1 := ts.RandEd25519KeyPair() + _, prv2 := ts.RandEd25519KeyPair() + _, prv3 := ts.RandBLSKeyPair() + + sig1 := prv1.Sign([]byte("foo")) + sig2 := prv2.Sign([]byte("foo")) + sig3 := prv3.Sign([]byte("foo")) assert.True(t, sig1.EqualsTo(sig1)) assert.False(t, sig1.EqualsTo(sig2)) - assert.Equal(t, sig1, sig1) - assert.NotEqual(t, sig1, sig2) + assert.False(t, sig1.EqualsTo(sig3)) } func TestSignatureEncoding(t *testing.T) { ts := testsuite.NewTestSuite(t) - _, prv := ts.RandBLSKeyPair() + _, prv := ts.RandEd25519KeyPair() sig := prv.Sign(ts.RandBytes(16)) w1 := util.NewFixedWriter(20) assert.Error(t, sig.Encode(w1)) - w2 := util.NewFixedWriter(bls.SignatureSize) + w2 := util.NewFixedWriter(ed25519.SignatureSize) assert.NoError(t, sig.Encode(w2)) r1 := util.NewFixedReader(20, w2.Bytes()) assert.Error(t, sig.Decode(r1)) - r2 := util.NewFixedReader(bls.SignatureSize, w2.Bytes()) + r2 := util.NewFixedReader(ed25519.SignatureSize, w2.Bytes()) assert.NoError(t, sig.Decode(r2)) } func TestVerifyingSignature(t *testing.T) { ts := testsuite.NewTestSuite(t) - msg := []byte("zarb") + msg := []byte("pactus") - pb1, pv1 := ts.RandBLSKeyPair() - pb2, pv2 := ts.RandBLSKeyPair() + pb1, pv1 := ts.RandEd25519KeyPair() + pb2, pv2 := ts.RandEd25519KeyPair() sig1 := pv1.Sign(msg) sig2 := pv2.Sign(msg) @@ -85,7 +84,7 @@ func TestVerifyingSignature(t *testing.T) { assert.ErrorIs(t, pb1.Verify(msg[1:], sig1), crypto.ErrInvalidSignature) } -func TestSignatureBytes(t *testing.T) { +func TestSignatureFromString(t *testing.T) { tests := []struct { errMsg string encoded string @@ -98,34 +97,26 @@ func TestSignatureBytes(t *testing.T) { false, nil, }, { - "signature should be 48 bytes, but it is 0 bytes", + "signature should be 64 bytes, but it is 0 bytes", "", false, nil, }, - { - "encoding/hex: odd length hex string", - "0", - false, nil, - }, - { - "signature should be 48 bytes, but it is 1 bytes", - "00", - false, nil, - }, { "", - "ad0f88cec815e9b8af3f0136297cb242ed8b6369af723fbdac077fa927f5780db7df47c77fb53f3a22324673f000c792", + "7d6af02f788422319781b03d7f4ed575b78c4c4dc8060ce145624fc8dc9ad92b" + + "ae2d28c70242f03a644f313009ad9cc88b5dc37d501e43279c8fbc40b973af04", true, []byte{ - 0xad, 0x0f, 0x88, 0xce, 0xc8, 0x15, 0xe9, 0xb8, 0xaf, 0x3f, 0x01, 0x36, 0x29, 0x7c, 0xb2, 0x42, - 0xed, 0x8b, 0x63, 0x69, 0xaf, 0x72, 0x3f, 0xbd, 0xac, 0x07, 0x7f, 0xa9, 0x27, 0xf5, 0x78, 0x0d, - 0xb7, 0xdf, 0x47, 0xc7, 0x7f, 0xb5, 0x3f, 0x3a, 0x22, 0x32, 0x46, 0x73, 0xf0, 0x00, 0xc7, 0x92, + 0x7d, 0x6a, 0xf0, 0x2f, 0x78, 0x84, 0x22, 0x31, 0x97, 0x81, 0xb0, 0x3d, 0x7f, 0x4e, 0xd5, 0x75, + 0xb7, 0x8c, 0x4c, 0x4d, 0xc8, 0x06, 0x0c, 0xe1, 0x45, 0x62, 0x4f, 0xc8, 0xdc, 0x9a, 0xd9, 0x2b, + 0xae, 0x2d, 0x28, 0xc7, 0x02, 0x42, 0xf0, 0x3a, 0x64, 0x4f, 0x31, 0x30, 0x09, 0xad, 0x9c, 0xc8, + 0x8b, 0x5d, 0xc3, 0x7d, 0x50, 0x1e, 0x43, 0x27, 0x9c, 0x8f, 0xbc, 0x40, 0xb9, 0x73, 0xaf, 0x04, }, }, } for no, test := range tests { - sig, err := bls.SignatureFromString(test.encoded) + sig, err := ed25519.SignatureFromString(test.encoded) if test.valid { assert.NoError(t, err, "test %v: unexpected error", no) assert.Equal(t, test.bytes, sig.Bytes(), "test %v: invalid bytes", no) @@ -135,44 +126,3 @@ func TestSignatureBytes(t *testing.T) { } } } - -func TestPointG1(t *testing.T) { - tests := []struct { - errMsg string - encoded string - valid bool - }{ - { - "compression flag must be set", - "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", - false, - }, - { - "input string must be zero when infinity flag is set", - "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - false, - }, - { - "signature is zero", - "c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", - false, - }, - { - "", - "ad0f88cec815e9b8af3f0136297cb242ed8b6369af723fbdac077fa927f5780db7df47c77fb53f3a22324673f000c792", - true, - }, - } - - for no, test := range tests { - sig, err := bls.SignatureFromString(test.encoded) - require.NoError(t, err) - - _, err = sig.PointG1() - if test.valid { - assert.NoError(t, err, "test %v: unexpected error", no) - } else { - assert.Contains(t, err.Error(), test.errMsg, "test %v: error not matched", no) - } - } -} diff --git a/crypto/hash/hash_test.go b/crypto/hash/hash_test.go index 18f8e0b36..e45aaf3c6 100644 --- a/crypto/hash/hash_test.go +++ b/crypto/hash/hash_test.go @@ -38,16 +38,16 @@ func TestHashEmpty(t *testing.T) { } func TestHash256(t *testing.T) { - data := []byte("zarb") + data := []byte("pactus") h1 := hash.Hash256(data) - expected, _ := hex.DecodeString("12b38977f2d67f06f0c0cd54aaf7324cf4fee184398ea33d295e8d1543c2ee1a") + expected, _ := hex.DecodeString("ea020ace5c968f755dfc1b5921e574191cd9ff438639badae8a69f667e0d5970") assert.Equal(t, expected, h1) } func TestHash160(t *testing.T) { - data := []byte("zarb") + data := []byte("pactus") h := hash.Hash160(data) - expected, _ := hex.DecodeString("e93efc0c83176034cb828e39435eeecc07a29298") + expected, _ := hex.DecodeString("1e4633f850c9590a97633631eae007e18648e30e") assert.Equal(t, expected, h) } diff --git a/network/utils_test.go b/network/utils_test.go index d9d5319ee..6f03b8909 100644 --- a/network/utils_test.go +++ b/network/utils_test.go @@ -156,8 +156,11 @@ func TestSubnetsToFilters(t *testing.T) { } func TestMessageIdFunc(t *testing.T) { - m := &lp2pspb.Message{Data: []byte("zarb")} + m := &lp2pspb.Message{Data: []byte("pactus")} id := MessageIDFunc(m) - assert.Equal(t, id, "\x12\xb3\x89\x77\xf2\xd6\x7f\x06\xf0\xc0\xcd\x54\xaa\xf7\x32\x4c\xf4\xfe\xe1\x84") + assert.Equal(t, id, string([]byte{ + 0xea, 0x02, 0x0a, 0xce, 0x5c, 0x96, 0x8f, 0x75, + 0x5d, 0xfc, 0x1b, 0x59, 0x21, 0xe5, 0x74, 0x19, 0x1c, 0xd9, 0xff, 0x43, + })) } diff --git a/util/testsuite/testsuite.go b/util/testsuite/testsuite.go index 728fbbd6f..3ab7d7bfc 100644 --- a/util/testsuite/testsuite.go +++ b/util/testsuite/testsuite.go @@ -237,8 +237,8 @@ func (ts *TestSuite) RandBLSKeyPair() (*bls.PublicKey, *bls.PrivateKey) { return pub, prv } -// RandED25519KeyPair generates a random ED25519 key pair for testing purposes. -func (ts *TestSuite) RandED25519KeyPair() (*ed25519.PublicKey, *ed25519.PrivateKey) { +// RandEd25519KeyPair generates a random Ed25519 key pair for testing purposes. +func (ts *TestSuite) RandEd25519KeyPair() (*ed25519.PublicKey, *ed25519.PrivateKey) { buf := make([]byte, ed25519.PrivateKeySize) _, err := ts.Rand.Read(buf) if err != nil { @@ -265,6 +265,14 @@ func (ts *TestSuite) RandBLSSignature() *bls.Signature { return sig.(*bls.Signature) } +// RandEd25519Signature generates a random BLS signature for testing purposes. +func (ts *TestSuite) RandEd25519Signature() *ed25519.Signature { + _, prv := ts.RandEd25519KeyPair() + sig := prv.Sign(ts.RandBytes(8)) + + return sig.(*ed25519.Signature) +} + // RandHash generates a random hash for testing purposes. func (ts *TestSuite) RandHash() hash.Hash { return hash.CalcHash(util.Int64ToSlice(ts.RandInt64(util.MaxInt64))) diff --git a/wallet/manager.go b/wallet/manager.go index eef29ea88..50dca6f97 100644 --- a/wallet/manager.go +++ b/wallet/manager.go @@ -153,20 +153,23 @@ func (wm *Manager) GetNewAddress( var addressInfo *vault.AddressInfo switch addressType { - case crypto.AddressTypeBLSAccount: - info, err := wlt.NewBLSAccountAddress(label) + case crypto.AddressTypeValidator: + info, err := wlt.NewValidatorAddress(label) if err != nil { return nil, err } addressInfo = info - case crypto.AddressTypeValidator: - info, err := wlt.NewValidatorAddress(label) + case crypto.AddressTypeBLSAccount: + info, err := wlt.NewBLSAccountAddress(label) if err != nil { return nil, err } addressInfo = info + case crypto.AddressTypeEd25519Account: + return nil, status.Errorf(codes.InvalidArgument, "not supported yet") + case crypto.AddressTypeTreasury: return nil, status.Errorf(codes.InvalidArgument, "invalid address type")