cher-nov / cryptg

Official Telethon extension to provide much faster cryptography for Telegram API requests.
https://pypi.org/project/cryptg/
Creative Commons Zero v1.0 Universal
75 stars 24 forks source link

Different implementation #1

Closed Lonami closed 3 years ago

Lonami commented 5 years ago

I made a benchmark with the following results:

Doing 1 iteration(s) with 512.0 KB of data for ed_pyaes
Encrypt took 3.01s (avg 3.01s)
Decrypt took 3.08s (avg 3.08s)
Doing 100 iteration(s) with 512.0 KB of data for ed_cryptg
Encrypt took 1.89s (avg 0.02s)
Decrypt took 3.61s (avg 0.04s)
Doing 100 iteration(s) with 512.0 KB of data for ed_tgcrypto
Encrypt took 0.39s (avg 0.00s)
Decrypt took 0.39s (avg 0.00s)
Doing 20 iteration(s) with 512.0 KB of data for ed_ssl
Encrypt took 2.61s (avg 0.13s)
Decrypt took 2.63s (avg 0.13s)

And from faster to slower, we have:

  1. tgcrypto
  2. cryptg
  3. openssl
  4. pyaes

So, a different implementation may be worth it. Perhaps we can pull-request AES-IGE to rustls and use that.

See also the conversation that inspired this issue.

cher-nov commented 3 years ago

I would ask you to measure things again after #8 will be merged and close this issue if we'll become OK enough.

Lonami commented 3 years ago

pip install cryptg (0.2 from PyPi):

Doing 1 iteration(s) with 512.0 KB of data for ed_pyaes
Encrypt took 1.37s (avg 1.37s)
Decrypt took 1.44s (avg 1.44s)
Doing 100 iteration(s) with 512.0 KB of data for ed_cryptg
Encrypt took 12.05s (avg 0.12s)
Decrypt took 12.09s (avg 0.12s)
Doing 100 iteration(s) with 512.0 KB of data for ed_tgcrypto
Encrypt took 0.22s (avg 0.00s)
Decrypt took 0.21s (avg 0.00s)
Doing 20 iteration(s) with 512.0 KB of data for ed_ssl
Encrypt took 1.03s (avg 0.05s)
Decrypt took 1.03s (avg 0.05s)

pip install . (this branch):

Doing 100 iteration(s) with 512.0 KB of data for ed_cryptg
Encrypt took 11.59s (avg 0.12s)
Decrypt took 11.68s (avg 0.12s)

Honestly this makes zero sense. If I run cargo bench on grammers-crypto, the story is very different:

cargo bench (aesni) ``` IGE encryption (≤1KB)/encrypt/16 time: [135.61 ns 135.80 ns 136.00 ns] thrpt: [112.20 MiB/s 112.36 MiB/s 112.52 MiB/s] Found 5 outliers among 100 measurements (5.00%) 4 (4.00%) high mild 1 (1.00%) high severe IGE encryption (≤1KB)/encrypt/256 time: [835.36 ns 837.35 ns 839.28 ns] thrpt: [290.89 MiB/s 291.56 MiB/s 292.26 MiB/s] IGE encryption (≤1KB)/encrypt/512 time: [1.5693 us 1.5724 us 1.5754 us] thrpt: [309.93 MiB/s 310.54 MiB/s 311.14 MiB/s] Found 2 outliers among 100 measurements (2.00%) 2 (2.00%) high mild IGE encryption (≤1KB)/encrypt/1024 time: [3.0513 us 3.0556 us 3.0601 us] thrpt: [319.13 MiB/s 319.60 MiB/s 320.05 MiB/s] Found 5 outliers among 100 measurements (5.00%) 3 (3.00%) high mild 2 (2.00%) high severe IGE encryption (>1KB)/encrypt/16384 time: [47.993 us 48.141 us 48.415 us] thrpt: [322.73 MiB/s 324.56 MiB/s 325.57 MiB/s] IGE encryption (>1KB)/encrypt/131072 time: [383.11 us 383.97 us 384.45 us] thrpt: [325.14 MiB/s 325.55 MiB/s 326.27 MiB/s] Found 1 outliers among 10 measurements (10.00%) 1 (10.00%) high severe IGE encryption (>1KB)/encrypt/524288 time: [1.5381 ms 1.5512 ms 1.5645 ms] thrpt: [319.60 MiB/s 322.34 MiB/s 325.07 MiB/s] Found 1 outliers among 10 measurements (10.00%) 1 (10.00%) high mild IGE decryption (≤1KB)/decrypt/16 time: [135.33 ns 135.54 ns 135.78 ns] thrpt: [112.38 MiB/s 112.58 MiB/s 112.76 MiB/s] Found 1 outliers among 100 measurements (1.00%) 1 (1.00%) high mild IGE decryption (≤1KB)/decrypt/256 time: [814.68 ns 817.96 ns 821.98 ns] thrpt: [297.02 MiB/s 298.47 MiB/s 299.68 MiB/s] Found 6 outliers among 100 measurements (6.00%) 1 (1.00%) low mild 2 (2.00%) high mild 3 (3.00%) high severe IGE decryption (≤1KB)/decrypt/512 time: [1.5440 us 1.5463 us 1.5488 us] thrpt: [315.26 MiB/s 315.78 MiB/s 316.24 MiB/s] Found 6 outliers among 100 measurements (6.00%) 1 (1.00%) low mild 2 (2.00%) high mild 3 (3.00%) high severe IGE decryption (≤1KB)/decrypt/1024 time: [2.9692 us 2.9752 us 2.9816 us] thrpt: [327.53 MiB/s 328.23 MiB/s 328.90 MiB/s] Found 8 outliers among 100 measurements (8.00%) 1 (1.00%) low mild 5 (5.00%) high mild 2 (2.00%) high severe IGE decryption (>1KB)/decrypt/16384 time: [47.002 us 47.115 us 47.293 us] thrpt: [330.39 MiB/s 331.64 MiB/s 332.43 MiB/s] Found 4 outliers among 10 measurements (40.00%) 1 (10.00%) low severe 1 (10.00%) low mild 1 (10.00%) high mild 1 (10.00%) high severe IGE decryption (>1KB)/decrypt/131072 time: [373.99 us 374.60 us 375.28 us] thrpt: [333.09 MiB/s 333.69 MiB/s 334.23 MiB/s] Found 1 outliers among 10 measurements (10.00%) 1 (10.00%) high severe IGE decryption (>1KB)/decrypt/524288 time: [1.5127 ms 1.5151 ms 1.5171 ms] thrpt: [329.59 MiB/s 330.02 MiB/s 330.53 MiB/s] ```
cargo bench (aes_soft) ``` IGE encryption (≤1KB)/encrypt/16 time: [870.92 ns 872.75 ns 874.61 ns] thrpt: [17.446 MiB/s 17.484 MiB/s 17.520 MiB/s] Found 8 outliers among 100 measurements (8.00%) 2 (2.00%) low mild 5 (5.00%) high mild 1 (1.00%) high severe IGE encryption (≤1KB)/encrypt/256 time: [7.2808 us 7.2907 us 7.3011 us] thrpt: [33.439 MiB/s 33.487 MiB/s 33.532 MiB/s] Found 2 outliers among 100 measurements (2.00%) 2 (2.00%) high mild IGE encryption (≤1KB)/encrypt/512 time: [13.994 us 14.057 us 14.138 us] thrpt: [34.538 MiB/s 34.735 MiB/s 34.892 MiB/s] Found 8 outliers among 100 measurements (8.00%) 6 (6.00%) high mild 2 (2.00%) high severe IGE encryption (≤1KB)/encrypt/1024 time: [27.567 us 27.607 us 27.643 us] thrpt: [35.327 MiB/s 35.374 MiB/s 35.425 MiB/s] Found 2 outliers among 100 measurements (2.00%) 1 (1.00%) low mild 1 (1.00%) high mild IGE encryption (>1KB)/encrypt/16384 time: [431.16 us 431.90 us 432.73 us] thrpt: [36.108 MiB/s 36.177 MiB/s 36.239 MiB/s] IGE encryption (>1KB)/encrypt/131072 time: [3.4453 ms 3.4509 ms 3.4574 ms] thrpt: [36.155 MiB/s 36.223 MiB/s 36.281 MiB/s] Found 1 outliers among 10 measurements (10.00%) 1 (10.00%) high mild IGE encryption (>1KB)/encrypt/524288 time: [13.827 ms 13.855 ms 13.906 ms] thrpt: [35.957 MiB/s 36.088 MiB/s 36.162 MiB/s] IGE decryption (≤1KB)/decrypt/16 time: [911.02 ns 912.68 ns 914.28 ns] thrpt: [16.689 MiB/s 16.719 MiB/s 16.749 MiB/s] IGE decryption (≤1KB)/decrypt/256 time: [7.8308 us 7.8428 us 7.8558 us] thrpt: [31.078 MiB/s 31.129 MiB/s 31.177 MiB/s] Found 5 outliers among 100 measurements (5.00%) 5 (5.00%) high mild IGE decryption (≤1KB)/decrypt/512 time: [15.125 us 15.146 us 15.170 us] thrpt: [32.187 MiB/s 32.237 MiB/s 32.284 MiB/s] Found 4 outliers among 100 measurements (4.00%) 1 (1.00%) low mild 3 (3.00%) high mild IGE decryption (≤1KB)/decrypt/1024 time: [29.991 us 30.048 us 30.109 us] thrpt: [32.434 MiB/s 32.500 MiB/s 32.562 MiB/s] Found 5 outliers among 100 measurements (5.00%) 1 (1.00%) low mild 3 (3.00%) high mild 1 (1.00%) high severe IGE decryption (>1KB)/decrypt/16384 time: [471.70 us 472.82 us 474.68 us] thrpt: [32.917 MiB/s 33.046 MiB/s 33.125 MiB/s] IGE decryption (>1KB)/decrypt/131072 time: [3.7720 ms 3.7890 ms 3.8060 ms] thrpt: [32.843 MiB/s 32.991 MiB/s 33.139 MiB/s] IGE decryption (>1KB)/decrypt/524288 time: [15.026 ms 15.049 ms 15.091 ms] thrpt: [33.131 MiB/s 33.224 MiB/s 33.275 MiB/s] Found 1 outliers among 10 measurements (10.00%) 1 (10.00%) high severe ```

This is nowhere near the Python throughput:

>>> size = 100 * 512
>>> print('encrypt', size / 11.59, 'kb/s')
encrypt 4417.601380500431 kb/s
>>> print('decrypt', size / 11.68, 'kb/s')
decrypt 4383.561643835616 kb/s

What? I could expect a small hit from allocations and type conversions Rust-Python, but with only 100 iterations, the fact it's like 8 times slower makes no sense.

(Note there's also https://github.com/Wirtos/libaesni which is using assembly).

Lonami commented 3 years ago

Okay I don't know why pip install -e . is installing an unoptimized build because according to:

By default, develop will create a debug build, while install will create a release build.

But anyway, adding the following to the Cargo.toml:

[profile.dev]
opt-level = 3

Yields:

Doing 100 iteration(s) with 512.0 KB of data for ed_cryptg
Encrypt took 0.18s (avg 0.00s)
Decrypt took 0.16s (avg 0.00s)

Success I guess.

Lonami commented 3 years ago

Closed by #8.