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
```
This commit is contained in:
19byte 2024-03-04 21:30:28 +08:00
parent 248155eea0
commit 0346070ff6
2 changed files with 29 additions and 0 deletions

View file

@ -22,6 +22,7 @@ import (
"crypto/rand" "crypto/rand"
"encoding/json" "encoding/json"
"errors" "errors"
"io"
"os" "os"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"

View file

@ -160,3 +160,31 @@ func TestSwappedKeys(t *testing.T) {
t.Errorf("double-swapped value should work fine") 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")
}
}
}