diff --git a/ecies_test.go b/ecies_test.go index 9c2a2e6..c0842b9 100644 --- a/ecies_test.go +++ b/ecies_test.go @@ -197,3 +197,19 @@ func TestEncryptAgainstPythonVersion(t *testing.T) { assert.Equal(t, string(plaintext), testingMessage) } + +func BenchmarkEncryptAndDecrypt(b *testing.B) { + privkey := NewPrivateKeyFromBytes(testingReceiverPrivkey) + + for i := 0; i < b.N; i++ { + ciphertext, err := Encrypt(privkey.PublicKey, []byte(testingMessage)) + if err != nil { + b.Fatal(err) + } + + _, err = Decrypt(privkey, ciphertext) + if err != nil { + b.Fatal(err) + } + } +} diff --git a/publickey.go b/publickey.go index da1ffaf..858cf9b 100644 --- a/publickey.go +++ b/publickey.go @@ -103,11 +103,7 @@ func NewPublicKeyFromBytes(b []byte) (*PublicKey, error) { // Could be optionally compressed by dropping Y part func (k *PublicKey) Bytes(compressed bool) []byte { x := k.X.Bytes() - if len(x) < 32 { - for i := 0; i < 32-len(x); i++ { - x = append([]byte{0}, x...) - } - } + x = zeroPad(x, 32) if compressed { // If odd @@ -120,11 +116,7 @@ func (k *PublicKey) Bytes(compressed bool) []byte { } y := k.Y.Bytes() - if len(y) < 32 { - for i := 0; i < 32-len(y); i++ { - y = append([]byte{0}, y...) - } - } + y = zeroPad(y, 32) return bytes.Join([][]byte{{0x04}, x, y}, nil) } diff --git a/utils.go b/utils.go index 72af677..fc1d977 100644 --- a/utils.go +++ b/utils.go @@ -18,10 +18,12 @@ func kdf(secret []byte) (key []byte, err error) { return key, nil } -func zeroPad(b []byte, leigth int) []byte { - for i := 0; i < leigth-len(b); i++ { - b = append([]byte{0x00}, b...) +func zeroPad(b []byte, length int) []byte { + if len(b) > length { + panic("bytes too long") + } + if len(b) < length { + b = append(make([]byte, length-len(b)), b...) } - return b }