367c329b88
* whisper: fixes warnings from the code linter * whisper: more non-API-breaking changes The remaining lint errors are because of auto-generated files and one is because an exported function has a non- exported return type. Changing this would break the API, and will be part of another commit for easier reversal. * whisper: un-export NewSentMessage to please the linter This is an API change, which is why it's in its own commit. This change was initiated after the linter complained that the returned type wasn't exported. I chose to un-export the function instead of exporting the type, because that type is an implementation detail that I would like to change in the near future to make the code more readable and with an increased coverage. * whisper: update gencodec output after upgrading it to new lint standards
209 lines
5.0 KiB
Go
209 lines
5.0 KiB
Go
// Copyright 2016 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package whisperv6
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"testing"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
"golang.org/x/crypto/pbkdf2"
|
|
)
|
|
|
|
func BenchmarkDeriveKeyMaterial(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
pbkdf2.Key([]byte("test"), nil, 65356, aesKeyLength, sha256.New)
|
|
}
|
|
}
|
|
|
|
func BenchmarkEncryptionSym(b *testing.B) {
|
|
InitSingleTest()
|
|
|
|
params, err := generateMessageParams()
|
|
if err != nil {
|
|
b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
|
}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
msg, _ := newSentMessage(params)
|
|
_, err := msg.Wrap(params)
|
|
if err != nil {
|
|
b.Errorf("failed Wrap with seed %d: %s.", seed, err)
|
|
b.Errorf("i = %d, len(msg.Raw) = %d, params.Payload = %d.", i, len(msg.Raw), len(params.Payload))
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkEncryptionAsym(b *testing.B) {
|
|
InitSingleTest()
|
|
|
|
params, err := generateMessageParams()
|
|
if err != nil {
|
|
b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
|
}
|
|
key, err := crypto.GenerateKey()
|
|
if err != nil {
|
|
b.Fatalf("failed GenerateKey with seed %d: %s.", seed, err)
|
|
}
|
|
params.KeySym = nil
|
|
params.Dst = &key.PublicKey
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
msg, _ := newSentMessage(params)
|
|
_, err := msg.Wrap(params)
|
|
if err != nil {
|
|
b.Fatalf("failed Wrap with seed %d: %s.", seed, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkDecryptionSymValid(b *testing.B) {
|
|
InitSingleTest()
|
|
|
|
params, err := generateMessageParams()
|
|
if err != nil {
|
|
b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
|
}
|
|
msg, _ := newSentMessage(params)
|
|
env, err := msg.Wrap(params)
|
|
if err != nil {
|
|
b.Fatalf("failed Wrap with seed %d: %s.", seed, err)
|
|
}
|
|
f := Filter{KeySym: params.KeySym}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
msg := env.Open(&f)
|
|
if msg == nil {
|
|
b.Fatalf("failed to open with seed %d.", seed)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkDecryptionSymInvalid(b *testing.B) {
|
|
InitSingleTest()
|
|
|
|
params, err := generateMessageParams()
|
|
if err != nil {
|
|
b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
|
}
|
|
msg, _ := newSentMessage(params)
|
|
env, err := msg.Wrap(params)
|
|
if err != nil {
|
|
b.Fatalf("failed Wrap with seed %d: %s.", seed, err)
|
|
}
|
|
f := Filter{KeySym: []byte("arbitrary stuff here")}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
msg := env.Open(&f)
|
|
if msg != nil {
|
|
b.Fatalf("opened envelope with invalid key, seed: %d.", seed)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkDecryptionAsymValid(b *testing.B) {
|
|
InitSingleTest()
|
|
|
|
params, err := generateMessageParams()
|
|
if err != nil {
|
|
b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
|
}
|
|
key, err := crypto.GenerateKey()
|
|
if err != nil {
|
|
b.Fatalf("failed GenerateKey with seed %d: %s.", seed, err)
|
|
}
|
|
f := Filter{KeyAsym: key}
|
|
params.KeySym = nil
|
|
params.Dst = &key.PublicKey
|
|
msg, _ := newSentMessage(params)
|
|
env, err := msg.Wrap(params)
|
|
if err != nil {
|
|
b.Fatalf("failed Wrap with seed %d: %s.", seed, err)
|
|
}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
msg := env.Open(&f)
|
|
if msg == nil {
|
|
b.Fatalf("fail to open, seed: %d.", seed)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkDecryptionAsymInvalid(b *testing.B) {
|
|
InitSingleTest()
|
|
|
|
params, err := generateMessageParams()
|
|
if err != nil {
|
|
b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
|
}
|
|
key, err := crypto.GenerateKey()
|
|
if err != nil {
|
|
b.Fatalf("failed GenerateKey with seed %d: %s.", seed, err)
|
|
}
|
|
params.KeySym = nil
|
|
params.Dst = &key.PublicKey
|
|
msg, _ := newSentMessage(params)
|
|
env, err := msg.Wrap(params)
|
|
if err != nil {
|
|
b.Fatalf("failed Wrap with seed %d: %s.", seed, err)
|
|
}
|
|
|
|
key, err = crypto.GenerateKey()
|
|
if err != nil {
|
|
b.Fatalf("failed GenerateKey with seed %d: %s.", seed, err)
|
|
}
|
|
f := Filter{KeyAsym: key}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
msg := env.Open(&f)
|
|
if msg != nil {
|
|
b.Fatalf("opened envelope with invalid key, seed: %d.", seed)
|
|
}
|
|
}
|
|
}
|
|
|
|
func increment(x []byte) {
|
|
for i := 0; i < len(x); i++ {
|
|
x[i]++
|
|
if x[i] != 0 {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkPoW(b *testing.B) {
|
|
InitSingleTest()
|
|
|
|
params, err := generateMessageParams()
|
|
if err != nil {
|
|
b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
|
}
|
|
params.Payload = make([]byte, 32)
|
|
params.PoW = 10.0
|
|
params.TTL = 1
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
increment(params.Payload)
|
|
msg, _ := newSentMessage(params)
|
|
_, err := msg.Wrap(params)
|
|
if err != nil {
|
|
b.Fatalf("failed Wrap with seed %d: %s.", seed, err)
|
|
}
|
|
}
|
|
}
|