mirror of
https://github.com/apple/swift-crypto
synced 2026-11-24 04:37:48 +00:00
### Motivation We've recently added some EC operations internally to support some schemes, including ARC and VOPRF. Some of this code is quite allocation-heavy, which could be avoided with some minimally invasive changes. ### Modifications - Add package benchmarks for ARC and VOPRF. - Make use of the `consuming` keyword in some targeted places to reduce allocations in arithmetic- and append-chains. - Use a Thread-/Task-local FiniteFieldArithmeticContext for EC operations, and thread this through as a `BN_CTX` to BoringSSL functions when available. - Replace EC static computed properties with stored properties for things that are statically knowable about the EC group. ### Result This has resulted in a 50% reduction in allocations for ARC issuance and VOPRF evaluation, and a 34% reduction in ARC verification. ```none SwiftCryptoBenchmarks ============================================================================================================================ ---------------------------------------------------------------------------------------------------------------------------- arc-issue-p384 metrics ---------------------------------------------------------------------------------------------------------------------------- ╒══════════════════════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕ │ Malloc (total) * │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │ ╞══════════════════════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡ │ alpha │ 945 │ 945 │ 945 │ 945 │ 945 │ 945 │ 945 │ 3 │ ├──────────────────────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ beta │ 464 │ 464 │ 464 │ 464 │ 464 │ 464 │ 464 │ 3 │ ├──────────────────────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Δ │ -481 │ -481 │ -481 │ -481 │ -481 │ -481 │ -481 │ 0 │ ├──────────────────────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Improvement % │ 51 │ 51 │ 51 │ 51 │ 51 │ 51 │ 51 │ 0 │ ╘══════════════════════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛ ---------------------------------------------------------------------------------------------------------------------------- arc-verify-p384 metrics ---------------------------------------------------------------------------------------------------------------------------- ╒══════════════════════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕ │ Malloc (total) * │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │ ╞══════════════════════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡ │ alpha │ 410 │ 410 │ 410 │ 415 │ 415 │ 415 │ 415 │ 10 │ ├──────────────────────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ beta │ 271 │ 271 │ 271 │ 275 │ 275 │ 275 │ 275 │ 10 │ ├──────────────────────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Δ │ -139 │ -139 │ -139 │ -140 │ -140 │ -140 │ -140 │ 0 │ ├──────────────────────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Improvement % │ 34 │ 34 │ 34 │ 34 │ 34 │ 34 │ 34 │ 0 │ ╘══════════════════════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛ ---------------------------------------------------------------------------------------------------------------------------- voprf-evaluate-p384 metrics ---------------------------------------------------------------------------------------------------------------------------- ╒══════════════════════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕ │ Malloc (total) * │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │ ╞══════════════════════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡ │ alpha │ 331 │ 331 │ 331 │ 331 │ 331 │ 331 │ 331 │ 3 │ ├──────────────────────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ beta │ 168 │ 168 │ 168 │ 168 │ 168 │ 168 │ 168 │ 3 │ ├──────────────────────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Δ │ -163 │ -163 │ -163 │ -163 │ -163 │ -163 │ -163 │ 0 │ ├──────────────────────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Improvement % │ 49 │ 49 │ 49 │ 49 │ 49 │ 49 │ 49 │ 0 │ ╘══════════════════════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛ ``` --------- Co-authored-by: Cory Benfield <lukasa@apple.com>