//===----------------------------------------------------------------------===// // // 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 Prover: ProofParticipant { typealias Group = H2G.G var label: String var scalars: [Group.Scalar] var scalarLabels: [String] var points: [Group.Element] var pointLabels: [String] var constraints: [(PointVar, [(ScalarVar, PointVar)])] init(label: String) { self.label = label self.scalars = [] self.scalarLabels = [] self.points = [] self.pointLabels = [] self.constraints = [] } mutating func appendScalar(label: String, assignment: Group.Scalar) -> ScalarVar { self.scalarLabels.append(label) self.scalars.append(assignment) return ScalarVar(index: self.scalars.count - 1) } func prove() throws -> Proof { // Create a blinding scalar for each scalar variable. let blindings = (0.. Proof { // Perform size checks on proof fields. if (self.scalars.count != self.scalarLabels.count) || (self.points.count != self.pointLabels.count) { throw ZKPErrors.invalidProofFields } // Check that there is one blinding scalar for each allocated scalar variable. if (blindings.count != self.scalars.count) { throw ZKPErrors.invalidInputLength } // For each constraint, compute the blinded version of the constraint element. // Example: if the constraint is A=x*B, compute ABlind=xBlind*B for blinding scalar xBlind. // Example: if the constraint is A=x*B+y*C, compute ABlind=xBlind*B + yBlind*C for blinding scalars xBlind, yBlind. 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) // Compute response scalars from the challenge, scalars, and blindings. // Example: if the scalar is m, compute mResponse = mBlind - challenge * m for blinding scalar xBlind. var responses: [Group.Scalar] = [] for (index, scalar) in self.scalars.enumerated() { let blinding = blindings[index] responses.append(blinding - challenge * scalar) } return Proof(challenge: challenge, responses: responses) } }