Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(crypto): update Ed25519 tests #1483

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions crypto/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -143,7 +150,8 @@ func (addr Address) SerializeSize() int {
case AddressTypeTreasury:
return 1
case AddressTypeValidator,
AddressTypeBLSAccount:
AddressTypeBLSAccount,
AddressTypeEd25519Account:
return AddressSize
default:
return 0
Expand Down
12 changes: 6 additions & 6 deletions crypto/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func TestToString(t *testing.T) {
nil,
},
{
"pc1r0hrct7eflrpw4ccrttxzs4qud2axex4dwc9mn4",
crypto.InvalidAddressTypeError(3),
"pc1y0hrct7eflrpw4ccrttxzs4qud2axex4dksmred",
crypto.InvalidAddressTypeError(4),
nil,
},
{
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 8 additions & 8 deletions crypto/bls/bls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion crypto/bls/private_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 3 additions & 3 deletions crypto/bls/private_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion crypto/bls/public_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions crypto/bls/public_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion crypto/bls/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 10 additions & 7 deletions crypto/bls/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
Expand All @@ -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
Expand Down
Loading
Loading