//===----------------------------------------------------------------------===// // // This source file is part of the SwiftCrypto open source project // // Copyright (c) 2024 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 Foundation import Crypto @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *) struct Verifier: ProofParticipant { typealias Group = H2G.G var label: String var scalarLabels: [String] var points: [Group.Element] var pointLabels: [String] var constraints: [(PointVar, [(ScalarVar, PointVar)])] init(label: String) { self.label = label self.scalarLabels = [] self.points = [] self.pointLabels = [] self.constraints = [] } mutating func appendScalar(label: String) -> ScalarVar { self.scalarLabels.append(label) return ScalarVar(index: self.scalarLabels.count - 1) } func verify(proof: Proof) throws -> Bool { // Perform size checks on proof fields. if self.points.count != self.pointLabels.count { throw ZKPErrors.invalidProofFields } // For each constraint, recompute the blinded version of the constraint element. // Example: if the constraint is A=x*B, compute ABlind=challenge*A + xResponse*B // Example: if the constraint is A=x*B+y*C, compute ABlind=challenge*A + xResponse*B + yResponse*C var blindedPoints: [Group.Element] = [] var blindedPointsLabels: [String] = [] for (constraintPoint, linearCombination) in self.constraints { // Check that all PointVar and ScalarVar variables in the constraint have been correctly allocated. if !(0...composeChallenge(label: self.label, points: self.points, pointLabels: self.pointLabels, blindedPoints: blindedPoints, blindedPointsLabels: blindedPointsLabels, scalarLabels: self.scalarLabels) return challenge == proof.challenge } }