Skip to content

Commit

Permalink
Merge pull request #28 from ecies/cgo-support
Browse files Browse the repository at this point in the history
Decrypt benchmark added and README updated
  • Loading branch information
savely-krasovsky authored Feb 2, 2022
2 parents 11dcd55 + 9ddce1d commit 306c655
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,24 @@ func main() {
log.Printf("ciphertext decrypted: %s\n", string(plaintext))
}
```

## Benchmarks
With CGO:
```
goos: linux
goarch: amd64
pkg: github.com/ecies/go/v2
cpu: AMD Ryzen 7 5700G with Radeon Graphics
BenchmarkEncrypt-16 12250 98122 ns/op 5185 B/op 61 allocs/op
BenchmarkDecrypt-16 23934 50046 ns/op 4097 B/op 46 allocs/op
```

Without CGO:
```
goos: linux
goarch: amd64
pkg: github.com/ecies/go/v2
cpu: AMD Ryzen 7 5700G with Radeon Graphics
BenchmarkEncrypt-16 10000 112632 ns/op 5655 B/op 68 allocs/op
BenchmarkDecrypt-16 14038 85641 ns/op 4725 B/op 56 allocs/op
```
23 changes: 19 additions & 4 deletions ecies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

const testingMessage = "helloworld"
const testingJsonMessage = `{"code":0,"msg":"ok","data":{"pageNumber":1,"pageSize":10,"total":0,"list":[],"realTotal":0}}{"code":0,"msg":"ok","data":{"pageNumber":1,"pageSize":10,"total":0,"list":[],"realTotal":0}}{"code":0,"msg":"ok","data":{"pageNumber":1,"pageSize":10,"total":0,"list":[],"realTotal":0}}`
const testingReceiverPubkeyHex = "0498afe4f150642cd05cc9d2fa36458ce0a58567daeaf5fde7333ba9b403011140a4e28911fcf83ab1f457a30b4959efc4b9306f514a4c3711a16a80e3b47eb58b"
const testingReceiverPrivkeyHex = "95d3c5e483e9b1d4f5fc8e79b2deaf51362980de62dbb082a9a4257eef653d7d"
const pythonBackend = "https://eciespy.herokuapp.com/"
Expand All @@ -26,13 +27,10 @@ func TestGenerateKey(t *testing.T) {
assert.NoError(t, err)
}

// go test -benchmem -run=^$ -bench ^BenchmarkEncrypt$ github.com/ecies/go/v2
// MacBook Pro (15-inch, 2017)
// BenchmarkEncrypt-8 286 4316673 ns/op 1616494 B/op 18813 allocs/op
func BenchmarkEncrypt(b *testing.B) {
privkey := NewPrivateKeyFromBytes(testingReceiverPrivkey)

msg := []byte(`{"code":0,"msg":"ok","data":{"pageNumber":1,"pageSize":10,"total":0,"list":[],"realTotal":0}}{"code":0,"msg":"ok","data":{"pageNumber":1,"pageSize":10,"total":0,"list":[],"realTotal":0}}{"code":0,"msg":"ok","data":{"pageNumber":1,"pageSize":10,"total":0,"list":[],"realTotal":0}}`)
msg := []byte(testingJsonMessage)
for i := 0; i < b.N; i++ {
_, err := Encrypt(privkey.PublicKey, msg)
if err != nil {
Expand All @@ -41,6 +39,23 @@ func BenchmarkEncrypt(b *testing.B) {
}
}

func BenchmarkDecrypt(b *testing.B) {
privkey := NewPrivateKeyFromBytes(testingReceiverPrivkey)
msg := []byte(testingJsonMessage)

ciphertext, err := Encrypt(privkey.PublicKey, msg)
if err != nil {
b.Fail()
}

for i := 0; i < b.N; i++ {
_, err := Decrypt(privkey, ciphertext)
if err != nil {
b.Fail()
}
}
}

func TestEncryptAndDecrypt(t *testing.T) {
privkey := NewPrivateKeyFromBytes(testingReceiverPrivkey)

Expand Down

0 comments on commit 306c655

Please sign in to comment.