neoneo-go/pkg/crypto/keys/publickey_test.go
Roman Khimov f0fbe9f6c9 crypto: drop home-grown elliptic crypto, use crypto/elliptic
As NEO uses P256 we can use standard crypto/elliptic library for almost
everything, the only exception being decompression of the Y coordinate. For
some reason the standard library only supports uncompressed format in its
Marshal()/Unmarshal() functions. elliptic.P256() is known to have
constant-time implementation, so it fixes #245 (and the decompression using
big.Int operates on public key, so nobody really cares about that part being
constant-time).

New decompress function is inspired by
https://stackoverflow.com/questions/46283760, even though the previous one
really did the same thing just in a little less obvious way.
2019-09-05 12:33:27 +03:00

50 lines
1.2 KiB
Go

package keys
import (
"bytes"
"encoding/hex"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEncodeDecodeInfinity(t *testing.T) {
key := &PublicKey{}
buf := new(bytes.Buffer)
assert.Nil(t, key.EncodeBinary(buf))
assert.Equal(t, 1, buf.Len())
keyDecode := &PublicKey{}
assert.Nil(t, keyDecode.DecodeBinary(buf))
assert.Equal(t, []byte{0x00}, keyDecode.Bytes())
}
func TestEncodeDecodePublicKey(t *testing.T) {
for i := 0; i < 4; i++ {
k, err := NewPrivateKey()
assert.Nil(t, err)
p, err := k.PublicKey()
assert.Nil(t, err)
buf := new(bytes.Buffer)
assert.Nil(t, p.EncodeBinary(buf))
pDecode := &PublicKey{}
assert.Nil(t, pDecode.DecodeBinary(buf))
assert.Equal(t, p.X, pDecode.X)
}
}
func TestDecodeFromString(t *testing.T) {
str := "03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c"
pubKey, err := NewPublicKeyFromString(str)
assert.Nil(t, err)
assert.Equal(t, str, hex.EncodeToString(pubKey.Bytes()))
}
func TestPubkeyToAddress(t *testing.T) {
pubKey, err := NewPublicKeyFromString("031ee4e73a17d8f76dc02532e2620bcb12425b33c0c9f9694cc2caa8226b68cad4")
assert.Nil(t, err)
actual := pubKey.Address()
expected := "AUpGsNCHzSimeMRVPQfhwrVdiUp8Q2N2Qx"
assert.Equal(t, expected, actual)
}