mirror of
https://github.com/apple/swift-crypto
synced 2026-11-26 04:47:49 +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>
103 lines
3.5 KiB
Swift
103 lines
3.5 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftCrypto open source project
|
|
//
|
|
// Copyright (c) 2025 Apple Inc. and the SwiftCrypto project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
import Benchmark
|
|
import Crypto
|
|
import Foundation
|
|
import _CryptoExtras
|
|
|
|
let benchmarks = {
|
|
let defaultMetrics: [BenchmarkMetric] = [.mallocCountTotal, .cpuTotal]
|
|
|
|
Benchmark(
|
|
"arc-issue-p384",
|
|
configuration: Benchmark.Configuration(
|
|
metrics: defaultMetrics,
|
|
scalingFactor: .kilo,
|
|
maxDuration: .seconds(10_000_000),
|
|
maxIterations: 3
|
|
)
|
|
) { benchmark in
|
|
let privateKey = P384._ARCV1.PrivateKey()
|
|
let publicKey = privateKey.publicKey
|
|
let requestContext = Data("shared request context".utf8)
|
|
let precredential = try publicKey.prepareCredentialRequest(requestContext: requestContext)
|
|
let credentialRequest = precredential.credentialRequest
|
|
|
|
benchmark.startMeasurement()
|
|
|
|
for _ in benchmark.scaledIterations {
|
|
blackHole(try privateKey.issue(credentialRequest))
|
|
}
|
|
}
|
|
|
|
Benchmark(
|
|
"arc-verify-p384",
|
|
configuration: Benchmark.Configuration(
|
|
metrics: defaultMetrics,
|
|
scalingFactor: .kilo,
|
|
maxDuration: .seconds(10_000_000),
|
|
maxIterations: 10
|
|
)
|
|
) { benchmark in
|
|
let privateKey = P384._ARCV1.PrivateKey()
|
|
let publicKey = privateKey.publicKey
|
|
let requestContext = Data("shared request context".utf8)
|
|
let (presentationContext, presentationLimit) = (Data("shared presentation context".utf8), 2)
|
|
let precredential = try publicKey.prepareCredentialRequest(requestContext: requestContext)
|
|
let credentialRequest = precredential.credentialRequest
|
|
let credentialResponse = try privateKey.issue(credentialRequest)
|
|
var credential = try publicKey.finalize(credentialResponse, for: precredential)
|
|
let (presentation, nonce) = try credential.makePresentation(
|
|
context: presentationContext,
|
|
presentationLimit: presentationLimit
|
|
)
|
|
|
|
benchmark.startMeasurement()
|
|
|
|
for _ in benchmark.scaledIterations {
|
|
blackHole(
|
|
try privateKey.verify(
|
|
presentation,
|
|
requestContext: requestContext,
|
|
presentationContext: presentationContext,
|
|
presentationLimit: presentationLimit,
|
|
nonce: nonce
|
|
)
|
|
)
|
|
}
|
|
}
|
|
|
|
Benchmark(
|
|
"voprf-evaluate-p384",
|
|
configuration: Benchmark.Configuration(
|
|
metrics: defaultMetrics,
|
|
scalingFactor: .kilo,
|
|
maxDuration: .seconds(10_000_000),
|
|
maxIterations: 3
|
|
)
|
|
) { benchmark in
|
|
let privateKey = P384._VOPRF.PrivateKey()
|
|
let publicKey = privateKey.publicKey
|
|
let privateInput = Data("This is some input data".utf8)
|
|
let blindedInput = try publicKey.blind(privateInput)
|
|
let blindedElement = blindedInput.blindedElement
|
|
|
|
benchmark.startMeasurement()
|
|
|
|
for _ in benchmark.scaledIterations {
|
|
blackHole(try privateKey.evaluate(blindedElement))
|
|
}
|
|
}
|
|
}
|