Files
+6 431013afaf Update to WWDC25 release (#417)
This change updates Crypto to WWDC25 equivalent code.

Each of these changes were individually merged to the WWDC25 branch.
This includes:

- #359
- #363
- #365
- #367
- #368
- #397
- #403
- #410
- #407
- #401
- #415

---------

Co-authored-by: YourMJK <37852512+YourMJK@users.noreply.github.com>
Co-authored-by: Si Beaumont <beaumont@apple.com>
Co-authored-by: Rick Newton-Rogers <rnro@apple.com>
Co-authored-by: Raphael <josephnoir@users.noreply.github.com>
Co-authored-by: Evan Wilde <ewilde@apple.com>
Co-authored-by: Tim Condon <0xTim@users.noreply.github.com>
Co-authored-by: Gus Cairo <gus.cairo@apple.com>
Co-authored-by: George Barnett <gbarnett@apple.com>
Co-authored-by: Fabrice de Gans <Steelskin@users.noreply.github.com>
Co-authored-by: Wojciech Nagrodzki <278594+wnagrodzki@users.noreply.github.com>
Co-authored-by: Jesse L. Zamora <xtremekforever@gmail.com>
Co-authored-by: aryan-25 <aryan_shah@apple.com>
Co-authored-by: Gwynne Raskind <gwynne@vapor.codes>
Co-authored-by: 3405691582 <dsk@google.com>
Co-authored-by: Paul Toffoloni <69189821+ptoffy@users.noreply.github.com>
2025-09-26 11:55:11 +01:00

83 lines
3.2 KiB
Swift

//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif
import Crypto
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
struct Verifier<H2G: HashToGroup>: 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<H2G>) 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..<self.points.count).contains(constraintPoint.index) {
throw ZKPErrors.invalidVariableAllocation
}
for (_, pointVar) in linearCombination {
if !(0..<self.points.count).contains(pointVar.index) {
throw ZKPErrors.invalidVariableAllocation
}
}
// challenge * constraintPoint
let challengePoint = proof.challenge * self.points[constraintPoint.index]
let blindedPoint = (linearCombination).map { (scalar, point) in
proof.responses[scalar.index] * self.points[point.index]
}.reduce(challengePoint, +)
blindedPoints.append(blindedPoint)
blindedPointsLabels.append(self.pointLabels[constraintPoint.index] + "-blind")
}
// Obtain a scalar challenge.
let challenge = try Proof<H2G>.composeChallenge(label: self.label, points: self.points, pointLabels: self.pointLabels, blindedPoints: blindedPoints, blindedPointsLabels: blindedPointsLabels, scalarLabels: self.scalarLabels)
return challenge == proof.challenge
}
}