|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftCrypto open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the SwiftCrypto project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | +import Benchmark |
| 15 | +import Crypto |
| 16 | +import Foundation |
| 17 | +import _CryptoExtras |
| 18 | + |
| 19 | +let benchmarks = { |
| 20 | + let defaultMetrics: [BenchmarkMetric] = [.mallocCountTotal, .cpuTotal] |
| 21 | + |
| 22 | + Benchmark( |
| 23 | + "arc-issue-p384", |
| 24 | + configuration: Benchmark.Configuration( |
| 25 | + metrics: defaultMetrics, |
| 26 | + scalingFactor: .kilo, |
| 27 | + maxDuration: .seconds(10_000_000), |
| 28 | + maxIterations: 3 |
| 29 | + ) |
| 30 | + ) { benchmark in |
| 31 | + let privateKey = P384._ARCV1.PrivateKey() |
| 32 | + let publicKey = privateKey.publicKey |
| 33 | + let requestContext = Data("shared request context".utf8) |
| 34 | + let precredential = try publicKey.prepareCredentialRequest(requestContext: requestContext) |
| 35 | + let credentialRequest = precredential.credentialRequest |
| 36 | + |
| 37 | + benchmark.startMeasurement() |
| 38 | + |
| 39 | + for _ in benchmark.scaledIterations { |
| 40 | + blackHole(try privateKey.issue(credentialRequest)) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + Benchmark( |
| 45 | + "arc-verify-p384", |
| 46 | + configuration: Benchmark.Configuration( |
| 47 | + metrics: defaultMetrics, |
| 48 | + scalingFactor: .kilo, |
| 49 | + maxDuration: .seconds(10_000_000), |
| 50 | + maxIterations: 10 |
| 51 | + ) |
| 52 | + ) { benchmark in |
| 53 | + let privateKey = P384._ARCV1.PrivateKey() |
| 54 | + let publicKey = privateKey.publicKey |
| 55 | + let requestContext = Data("shared request context".utf8) |
| 56 | + let (presentationContext, presentationLimit) = (Data("shared presentation context".utf8), 2) |
| 57 | + let precredential = try publicKey.prepareCredentialRequest(requestContext: requestContext) |
| 58 | + let credentialRequest = precredential.credentialRequest |
| 59 | + let credentialResponse = try privateKey.issue(credentialRequest) |
| 60 | + var credential = try publicKey.finalize(credentialResponse, for: precredential) |
| 61 | + let (presentation, nonce) = try credential.makePresentation( |
| 62 | + context: presentationContext, |
| 63 | + presentationLimit: presentationLimit |
| 64 | + ) |
| 65 | + |
| 66 | + benchmark.startMeasurement() |
| 67 | + |
| 68 | + for _ in benchmark.scaledIterations { |
| 69 | + blackHole( |
| 70 | + try privateKey.verify( |
| 71 | + presentation, |
| 72 | + requestContext: requestContext, |
| 73 | + presentationContext: presentationContext, |
| 74 | + presentationLimit: presentationLimit, |
| 75 | + nonce: nonce |
| 76 | + ) |
| 77 | + ) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + Benchmark( |
| 82 | + "voprf-evaluate-p384", |
| 83 | + configuration: Benchmark.Configuration( |
| 84 | + metrics: defaultMetrics, |
| 85 | + scalingFactor: .kilo, |
| 86 | + maxDuration: .seconds(10_000_000), |
| 87 | + maxIterations: 3 |
| 88 | + ) |
| 89 | + ) { benchmark in |
| 90 | + let privateKey = P384._VOPRF.PrivateKey() |
| 91 | + let publicKey = privateKey.publicKey |
| 92 | + let privateInput = Data("This is some input data".utf8) |
| 93 | + let blindedInput = try publicKey.blind(privateInput) |
| 94 | + let blindedElement = blindedInput.blindedElement |
| 95 | + |
| 96 | + benchmark.startMeasurement() |
| 97 | + |
| 98 | + for _ in benchmark.scaledIterations { |
| 99 | + blackHole(try privateKey.evaluate(blindedElement)) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments