From 0346070ff6a956904d011d99d31fdb9154ce3876 Mon Sep 17 00:00:00 2001 From: 19byte <87568744+19byte@users.noreply.github.com> Date: Mon, 4 Mar 2024 21:30:28 +0800 Subject: [PATCH] Improve aes_gcm performance * this modification does not change the encryption method before: ``` goos: darwin goarch: arm64 pkg: github.com/ethereum/go-ethereum/signer/storage BenchmarkEncryption BenchmarkEncryption-11 1306752 906.1 ns/op PASS ``` after: ``` goos: darwin goarch: arm64 pkg: github.com/ethereum/go-ethereum/signer/storage BenchmarkEncryption BenchmarkEncryption-11 2077677 545.8 ns/op PASS ``` --- signer/storage/aes_gcm_storage.go | 1 + signer/storage/aes_gcm_storage_test.go | 28 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/signer/storage/aes_gcm_storage.go b/signer/storage/aes_gcm_storage.go index 39eedd88fd..6c303ee6d9 100644 --- a/signer/storage/aes_gcm_storage.go +++ b/signer/storage/aes_gcm_storage.go @@ -22,6 +22,7 @@ import ( "crypto/rand" "encoding/json" "errors" + "io" "os" "github.com/ethereum/go-ethereum/log" diff --git a/signer/storage/aes_gcm_storage_test.go b/signer/storage/aes_gcm_storage_test.go index 00f2f21900..d89b4a6711 100644 --- a/signer/storage/aes_gcm_storage_test.go +++ b/signer/storage/aes_gcm_storage_test.go @@ -160,3 +160,31 @@ func TestSwappedKeys(t *testing.T) { t.Errorf("double-swapped value should work fine") } } + +func BenchmarkEncryption(b *testing.B) { + storage := NewAESEncryptedStorage("test.json", []byte("x?^0^fIhNpSkTKbN")) + + // Generate plaintext and additional data + plaintext := []byte("example plaintext") + additionalData := []byte("additional data") + + b.ResetTimer() + for i := 0; i < b.N; i++ { + // Encrypt plaintext + ciphertext, _, err := storage.encrypt(plaintext, additionalData) + if err != nil { + b.Fatalf("Encryption failed: %v", err) + } + + // Decrypt ciphertext + decryptedPlaintext, err := storage.decrypt(ciphertext, additionalData) + if err != nil { + b.Fatalf("Decryption failed: %v", err) + } + + // Verify decryption + if string(decryptedPlaintext) != string(plaintext) { + b.Fatalf("Decryption failed: plaintext mismatch") + } + } +}