-
-
Notifications
You must be signed in to change notification settings - Fork 663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Benchmark application #4101
Merged
Merged
Benchmark application #4101
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fb1d4c8
chore(core): add benchmark messages
onvej-sl e8f0c51
feat(core): implement benchmark application
onvej-sl 9d87d4d
feat(python): implement benchmark application interface
onvej-sl f160af4
build(core): hide benchmark application behind compilation flag
onvej-sl aa8b8a0
build(core): move argument parsing together
onvej-sl 35da0e7
feat(core): allow benchmark only in debug
onvej-sl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
syntax = "proto2"; | ||
package hw.trezor.messages.bitcoin; | ||
|
||
// Sugar for easier handling in Java | ||
option java_package = "com.satoshilabs.trezor.lib.protobuf"; | ||
option java_outer_classname = "TrezorMessageBenchmark"; | ||
|
||
/** | ||
* Request: Ask device for a list of names of all supported benchmarks | ||
* @start | ||
* @next Benchmarks | ||
* @next Failure | ||
*/ | ||
message BenchmarkListNames { | ||
} | ||
|
||
/** | ||
* Response: Contains the list of names of all supported benchmarks | ||
* @end | ||
*/ | ||
message BenchmarkNames { | ||
repeated string names = 1; | ||
} | ||
|
||
/** | ||
* Request: Ask device to run a benchmark | ||
* @start | ||
* @next BenchmarkResult | ||
* @next Failure | ||
*/ | ||
message BenchmarkRun { | ||
optional string name = 1; | ||
} | ||
|
||
/** | ||
* Response: Contains the result of the benchmark | ||
* @end | ||
*/ | ||
message BenchmarkResult { | ||
optional string value = 1; | ||
optional string unit = 3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added benchmark application. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
if not __debug__: | ||
from trezor import utils | ||
|
||
utils.halt("Disabled in production mode") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import utime | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from typing import Protocol | ||
|
||
from trezor.messages import BenchmarkResult | ||
|
||
class Benchmark(Protocol): | ||
def prepare(self) -> None: ... | ||
|
||
def run(self) -> None: ... | ||
|
||
def get_result(self, duration_us: int, repetitions: int) -> BenchmarkResult: ... | ||
|
||
|
||
def run_benchmark(benchmark: Benchmark) -> BenchmarkResult: | ||
minimum_duration_s = 1 | ||
minimum_duration_us = minimum_duration_s * 1000000 | ||
benchmark.prepare() | ||
start_time_us = utime.ticks_us() | ||
repetitions = 0 | ||
while True: | ||
benchmark.run() | ||
repetitions += 1 | ||
duration_us = utime.ticks_diff(utime.ticks_us(), start_time_us) | ||
if duration_us > minimum_duration_us: | ||
break | ||
return benchmark.get_result(duration_us, repetitions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
from trezor.crypto import aes, aesgcm, chacha20poly1305 | ||
from trezor.crypto.curve import curve25519, ed25519, nist256p1, secp256k1 | ||
from trezor.crypto.hashlib import ( | ||
blake2b, | ||
blake2s, | ||
blake256, | ||
groestl512, | ||
ripemd160, | ||
sha1, | ||
sha3_256, | ||
sha3_512, | ||
sha256, | ||
sha512, | ||
) | ||
|
||
from .cipher_benchmark import DecryptBenchmark, EncryptBenchmark | ||
from .common import random_bytes | ||
from .curve_benchmark import ( | ||
MultiplyBenchmark, | ||
PublickeyBenchmark, | ||
SignBenchmark, | ||
VerifyBenchmark, | ||
) | ||
from .hash_benchmark import HashBenchmark | ||
|
||
|
||
# This is a wrapper above the trezor.crypto.curve.ed25519 module that satisfies SignCurve protocol, the modules uses `message` instead of `digest` in `sign()` and `verify()` | ||
class Ed25519: | ||
def __init__(self): | ||
pass | ||
|
||
def generate_secret(self) -> bytes: | ||
return ed25519.generate_secret() | ||
|
||
def publickey(self, secret_key: bytes) -> bytes: | ||
return ed25519.publickey(secret_key) | ||
|
||
def sign(self, secret_key: bytes, digest: bytes) -> bytes: | ||
# ed25519.sign(secret_key: bytes, message: bytes, hasher: str = "") -> bytes: | ||
return ed25519.sign(secret_key, digest) | ||
|
||
def verify(self, public_key: bytes, signature: bytes, digest: bytes) -> bool: | ||
# ed25519.verify(public_key: bytes, signature: bytes, message: bytes) -> bool: | ||
return ed25519.verify(public_key, signature, digest) | ||
|
||
|
||
benchmarks = { | ||
"crypto/hash/blake2b": HashBenchmark(lambda: blake2b()), | ||
"crypto/hash/blake2s": HashBenchmark(lambda: blake2s()), | ||
"crypto/hash/blake256": HashBenchmark(lambda: blake256()), | ||
"crypto/hash/groestl512": HashBenchmark(lambda: groestl512()), | ||
"crypto/hash/ripemd160": HashBenchmark(lambda: ripemd160()), | ||
"crypto/hash/sha1": HashBenchmark(lambda: sha1()), | ||
"crypto/hash/sha3_256": HashBenchmark(lambda: sha3_256()), | ||
"crypto/hash/sha3_512": HashBenchmark(lambda: sha3_512()), | ||
"crypto/hash/sha256": HashBenchmark(lambda: sha256()), | ||
"crypto/hash/sha512": HashBenchmark(lambda: sha512()), | ||
"crypto/cipher/aes128-ecb/encrypt": EncryptBenchmark( | ||
lambda: aes(aes.ECB, random_bytes(16), random_bytes(16)), 16 | ||
), | ||
"crypto/cipher/aes128-ecb/decrypt": DecryptBenchmark( | ||
lambda: aes(aes.ECB, random_bytes(16), random_bytes(16)), 16 | ||
), | ||
"crypto/cipher/aesgcm128/encrypt": EncryptBenchmark( | ||
lambda: aesgcm(random_bytes(16), random_bytes(16)), 16 | ||
), | ||
"crypto/cipher/aesgcm128/decrypt": DecryptBenchmark( | ||
lambda: aesgcm(random_bytes(16), random_bytes(16)), 16 | ||
), | ||
"crypto/cipher/aesgcm256/encrypt": EncryptBenchmark( | ||
lambda: aesgcm(random_bytes(32), random_bytes(16)), 16 | ||
), | ||
"crypto/cipher/aesgcm256/decrypt": DecryptBenchmark( | ||
lambda: aesgcm(random_bytes(32), random_bytes(16)), 16 | ||
), | ||
"crypto/cipher/chacha20poly1305/encrypt": EncryptBenchmark( | ||
lambda: chacha20poly1305(random_bytes(32), random_bytes(12)), 64 | ||
), | ||
"crypto/cipher/chacha20poly1305/decrypt": DecryptBenchmark( | ||
lambda: chacha20poly1305(random_bytes(32), random_bytes(12)), 64 | ||
), | ||
"crypto/curve/secp256k1/sign": SignBenchmark(secp256k1), | ||
"crypto/curve/secp256k1/verify": VerifyBenchmark(secp256k1), | ||
"crypto/curve/secp256k1/publickey": PublickeyBenchmark(secp256k1), | ||
"crypto/curve/secp256k1/multiply": MultiplyBenchmark(secp256k1), | ||
"crypto/curve/nist256p1/sign": SignBenchmark(nist256p1), | ||
"crypto/curve/nist256p1/verify": VerifyBenchmark(nist256p1), | ||
"crypto/curve/nist256p1/publickey": PublickeyBenchmark(nist256p1), | ||
"crypto/curve/nist256p1/multiply": MultiplyBenchmark(nist256p1), | ||
"crypto/curve/ed25519/sign": SignBenchmark(Ed25519()), | ||
"crypto/curve/ed25519/verify": VerifyBenchmark(Ed25519()), | ||
"crypto/curve/ed25519/publickey": PublickeyBenchmark(ed25519), | ||
"crypto/curve/curve25519/publickey": PublickeyBenchmark(curve25519), | ||
"crypto/curve/curve25519/multiply": MultiplyBenchmark(curve25519), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from typing import TYPE_CHECKING, Callable | ||
|
||
from trezor.messages import BenchmarkResult | ||
|
||
from .common import format_float, maximum_used_memory_in_bytes, random_bytes | ||
|
||
if TYPE_CHECKING: | ||
from typing import Protocol | ||
|
||
class CipherCtx(Protocol): | ||
def encrypt(self, data: bytes) -> bytes: ... | ||
|
||
def decrypt(self, data: bytes) -> bytes: ... | ||
|
||
|
||
class EncryptBenchmark: | ||
def __init__( | ||
self, cipher_ctx_constructor: Callable[[], CipherCtx], block_size: int | ||
): | ||
self.cipher_ctx_constructor = cipher_ctx_constructor | ||
self.block_size = block_size | ||
|
||
def prepare(self): | ||
self.cipher_ctx = self.cipher_ctx_constructor() | ||
self.blocks_count = maximum_used_memory_in_bytes // self.block_size | ||
self.iterations_count = 100 | ||
self.data = random_bytes(self.blocks_count * self.block_size) | ||
|
||
def run(self): | ||
for _ in range(self.iterations_count): | ||
self.cipher_ctx.encrypt(self.data) | ||
|
||
def get_result(self, duration_us: int, repetitions: int) -> BenchmarkResult: | ||
value = (repetitions * self.iterations_count * len(self.data) * 1000 * 1000) / ( | ||
duration_us * 1024 * 1024 | ||
) | ||
|
||
return BenchmarkResult( | ||
value=format_float(value), | ||
unit="MB/s", | ||
) | ||
|
||
|
||
class DecryptBenchmark: | ||
def __init__( | ||
self, cipher_ctx_constructor: Callable[[], CipherCtx], block_size: int | ||
): | ||
self.cipher_ctx_constructor = cipher_ctx_constructor | ||
self.block_size = block_size | ||
|
||
def prepare(self): | ||
self.cipher_ctx = self.cipher_ctx_constructor() | ||
self.blocks_count = maximum_used_memory_in_bytes // self.block_size | ||
self.iterations_count = 100 | ||
self.data = random_bytes(self.blocks_count * self.block_size) | ||
|
||
def run(self): | ||
for _ in range(self.iterations_count): | ||
self.cipher_ctx.decrypt(self.data) | ||
|
||
def get_result(self, duration_us: int, repetitions: int) -> BenchmarkResult: | ||
value = (repetitions * self.iterations_count * len(self.data) * 1000 * 1000) / ( | ||
duration_us * 1024 * 1024 | ||
) | ||
|
||
return BenchmarkResult( | ||
value=format_float(value), | ||
unit="MB/s", | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note that there's now a common sectionoh it's not here yet, it's in #4234. not sure when that will land so you can either go ahead here or wait for itSCONS_OPTS
where this will go when you're rebasing