github.com/gobitfly/go-ethereum@v1.8.12/whisper/whisperv6/benchmarks_test.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package whisperv6 18 19 import ( 20 "crypto/sha256" 21 "testing" 22 23 "github.com/ethereum/go-ethereum/crypto" 24 "golang.org/x/crypto/pbkdf2" 25 ) 26 27 func BenchmarkDeriveKeyMaterial(b *testing.B) { 28 for i := 0; i < b.N; i++ { 29 pbkdf2.Key([]byte("test"), nil, 65356, aesKeyLength, sha256.New) 30 } 31 } 32 33 func BenchmarkEncryptionSym(b *testing.B) { 34 InitSingleTest() 35 36 params, err := generateMessageParams() 37 if err != nil { 38 b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 39 } 40 41 for i := 0; i < b.N; i++ { 42 msg, _ := NewSentMessage(params) 43 _, err := msg.Wrap(params) 44 if err != nil { 45 b.Errorf("failed Wrap with seed %d: %s.", seed, err) 46 b.Errorf("i = %d, len(msg.Raw) = %d, params.Payload = %d.", i, len(msg.Raw), len(params.Payload)) 47 return 48 } 49 } 50 } 51 52 func BenchmarkEncryptionAsym(b *testing.B) { 53 InitSingleTest() 54 55 params, err := generateMessageParams() 56 if err != nil { 57 b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 58 } 59 key, err := crypto.GenerateKey() 60 if err != nil { 61 b.Fatalf("failed GenerateKey with seed %d: %s.", seed, err) 62 } 63 params.KeySym = nil 64 params.Dst = &key.PublicKey 65 66 for i := 0; i < b.N; i++ { 67 msg, _ := NewSentMessage(params) 68 _, err := msg.Wrap(params) 69 if err != nil { 70 b.Fatalf("failed Wrap with seed %d: %s.", seed, err) 71 } 72 } 73 } 74 75 func BenchmarkDecryptionSymValid(b *testing.B) { 76 InitSingleTest() 77 78 params, err := generateMessageParams() 79 if err != nil { 80 b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 81 } 82 msg, _ := NewSentMessage(params) 83 env, err := msg.Wrap(params) 84 if err != nil { 85 b.Fatalf("failed Wrap with seed %d: %s.", seed, err) 86 } 87 f := Filter{KeySym: params.KeySym} 88 89 for i := 0; i < b.N; i++ { 90 msg := env.Open(&f) 91 if msg == nil { 92 b.Fatalf("failed to open with seed %d.", seed) 93 } 94 } 95 } 96 97 func BenchmarkDecryptionSymInvalid(b *testing.B) { 98 InitSingleTest() 99 100 params, err := generateMessageParams() 101 if err != nil { 102 b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 103 } 104 msg, _ := NewSentMessage(params) 105 env, err := msg.Wrap(params) 106 if err != nil { 107 b.Fatalf("failed Wrap with seed %d: %s.", seed, err) 108 } 109 f := Filter{KeySym: []byte("arbitrary stuff here")} 110 111 for i := 0; i < b.N; i++ { 112 msg := env.Open(&f) 113 if msg != nil { 114 b.Fatalf("opened envelope with invalid key, seed: %d.", seed) 115 } 116 } 117 } 118 119 func BenchmarkDecryptionAsymValid(b *testing.B) { 120 InitSingleTest() 121 122 params, err := generateMessageParams() 123 if err != nil { 124 b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 125 } 126 key, err := crypto.GenerateKey() 127 if err != nil { 128 b.Fatalf("failed GenerateKey with seed %d: %s.", seed, err) 129 } 130 f := Filter{KeyAsym: key} 131 params.KeySym = nil 132 params.Dst = &key.PublicKey 133 msg, _ := NewSentMessage(params) 134 env, err := msg.Wrap(params) 135 if err != nil { 136 b.Fatalf("failed Wrap with seed %d: %s.", seed, err) 137 } 138 139 for i := 0; i < b.N; i++ { 140 msg := env.Open(&f) 141 if msg == nil { 142 b.Fatalf("fail to open, seed: %d.", seed) 143 } 144 } 145 } 146 147 func BenchmarkDecryptionAsymInvalid(b *testing.B) { 148 InitSingleTest() 149 150 params, err := generateMessageParams() 151 if err != nil { 152 b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 153 } 154 key, err := crypto.GenerateKey() 155 if err != nil { 156 b.Fatalf("failed GenerateKey with seed %d: %s.", seed, err) 157 } 158 params.KeySym = nil 159 params.Dst = &key.PublicKey 160 msg, _ := NewSentMessage(params) 161 env, err := msg.Wrap(params) 162 if err != nil { 163 b.Fatalf("failed Wrap with seed %d: %s.", seed, err) 164 } 165 166 key, err = crypto.GenerateKey() 167 if err != nil { 168 b.Fatalf("failed GenerateKey with seed %d: %s.", seed, err) 169 } 170 f := Filter{KeyAsym: key} 171 172 for i := 0; i < b.N; i++ { 173 msg := env.Open(&f) 174 if msg != nil { 175 b.Fatalf("opened envelope with invalid key, seed: %d.", seed) 176 } 177 } 178 } 179 180 func increment(x []byte) { 181 for i := 0; i < len(x); i++ { 182 x[i]++ 183 if x[i] != 0 { 184 break 185 } 186 } 187 } 188 189 func BenchmarkPoW(b *testing.B) { 190 InitSingleTest() 191 192 params, err := generateMessageParams() 193 if err != nil { 194 b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) 195 } 196 params.Payload = make([]byte, 32) 197 params.PoW = 10.0 198 params.TTL = 1 199 200 for i := 0; i < b.N; i++ { 201 increment(params.Payload) 202 msg, _ := NewSentMessage(params) 203 _, err := msg.Wrap(params) 204 if err != nil { 205 b.Fatalf("failed Wrap with seed %d: %s.", seed, err) 206 } 207 } 208 }