diff --git a/Example/Podfile.lock b/Example/Podfile.lock index fb7a542..9cb503b 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -1,8 +1,8 @@ PODS: - - rapidsnark (0.1.0): - - rapidsnark/rapidsnark (= 0.1.0) - - rapidsnark/C (0.1.0) - - rapidsnark/rapidsnark (0.1.0): + - rapidsnark (0.0.1-alpha.3): + - rapidsnark/rapidsnark (= 0.0.1-alpha.3) + - rapidsnark/C (0.0.1-alpha.3) + - rapidsnark/rapidsnark (0.0.1-alpha.3): - rapidsnark/C DEPENDENCIES: @@ -13,7 +13,7 @@ EXTERNAL SOURCES: :path: "../" SPEC CHECKSUMS: - rapidsnark: c8f4f7529dd5afd9a9856bb92d14486f990770c1 + rapidsnark: d7ec571676bc69b2dd27b00e8a20d3d3fb9b023f PODFILE CHECKSUM: 96331fd92ed9651d0ec43feadad116a15c20e7c7 diff --git a/Example/Pods/Local Podspecs/rapidsnark.podspec.json b/Example/Pods/Local Podspecs/rapidsnark.podspec.json index 280b250..7d9433c 100644 --- a/Example/Pods/Local Podspecs/rapidsnark.podspec.json +++ b/Example/Pods/Local Podspecs/rapidsnark.podspec.json @@ -1,9 +1,9 @@ { "name": "rapidsnark", - "version": "0.1.0", + "version": "0.0.1-alpha.3", "summary": "Swift wrapper for the rapidsnark proof generation library.", "description": "This library is Swift wrapper for the [Rapidsnark](https://github.com/iden3/rapidsnark). It enables the\ngeneration of proofs for specified circuits within an iOS environment.", - "homepage": "https://github.com/5eeman/rapidsnark-ios", + "homepage": "https://github.com/iden3/ios-rapidsnark", "license": { "type": "GNU", "file": "COPYING" @@ -12,8 +12,8 @@ "Yaroslav Moria": "morya.yaroslav@gmail.com" }, "source": { - "git": "https://github.com/5eeman/rapidsnark-ios.git", - "tag": "0.1.0" + "git": "https://github.com/iden3/ios-rapidsnark.git", + "tag": "0.0.1-alpha.3" }, "platforms": { "ios": "12.0" @@ -22,10 +22,12 @@ "5" ], "pod_target_xcconfig": { - "ONLY_ACTIVE_ARCH": "YES" + "ONLY_ACTIVE_ARCH": "YES", + "CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES": "YES" }, "user_target_xcconfig": { - "ONLY_ACTIVE_ARCH": "YES" + "ONLY_ACTIVE_ARCH": "YES", + "CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES": "YES" }, "default_subspecs": "rapidsnark", "subspecs": [ diff --git a/Example/Pods/Manifest.lock b/Example/Pods/Manifest.lock index fb7a542..9cb503b 100644 --- a/Example/Pods/Manifest.lock +++ b/Example/Pods/Manifest.lock @@ -1,8 +1,8 @@ PODS: - - rapidsnark (0.1.0): - - rapidsnark/rapidsnark (= 0.1.0) - - rapidsnark/C (0.1.0) - - rapidsnark/rapidsnark (0.1.0): + - rapidsnark (0.0.1-alpha.3): + - rapidsnark/rapidsnark (= 0.0.1-alpha.3) + - rapidsnark/C (0.0.1-alpha.3) + - rapidsnark/rapidsnark (0.0.1-alpha.3): - rapidsnark/C DEPENDENCIES: @@ -13,7 +13,7 @@ EXTERNAL SOURCES: :path: "../" SPEC CHECKSUMS: - rapidsnark: c8f4f7529dd5afd9a9856bb92d14486f990770c1 + rapidsnark: d7ec571676bc69b2dd27b00e8a20d3d3fb9b023f PODFILE CHECKSUM: 96331fd92ed9651d0ec43feadad116a15c20e7c7 diff --git a/Example/Pods/Target Support Files/Pods-rapidsnark_Example/Pods-rapidsnark_Example.debug.xcconfig b/Example/Pods/Target Support Files/Pods-rapidsnark_Example/Pods-rapidsnark_Example.debug.xcconfig index d82643b..bcadc13 100644 --- a/Example/Pods/Target Support Files/Pods-rapidsnark_Example/Pods-rapidsnark_Example.debug.xcconfig +++ b/Example/Pods/Target Support Files/Pods-rapidsnark_Example/Pods-rapidsnark_Example.debug.xcconfig @@ -1,4 +1,5 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES +CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES = YES CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = NO FRAMEWORK_SEARCH_PATHS = $(inherited) "${PODS_CONFIGURATION_BUILD_DIR}/rapidsnark" "${PODS_ROOT}/../../Libs" GCC_PREPROCESSOR_DEFINITIONS = $(inherited) COCOAPODS=1 diff --git a/Example/Pods/Target Support Files/Pods-rapidsnark_Example/Pods-rapidsnark_Example.release.xcconfig b/Example/Pods/Target Support Files/Pods-rapidsnark_Example/Pods-rapidsnark_Example.release.xcconfig index d82643b..bcadc13 100644 --- a/Example/Pods/Target Support Files/Pods-rapidsnark_Example/Pods-rapidsnark_Example.release.xcconfig +++ b/Example/Pods/Target Support Files/Pods-rapidsnark_Example/Pods-rapidsnark_Example.release.xcconfig @@ -1,4 +1,5 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES +CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES = YES CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = NO FRAMEWORK_SEARCH_PATHS = $(inherited) "${PODS_CONFIGURATION_BUILD_DIR}/rapidsnark" "${PODS_ROOT}/../../Libs" GCC_PREPROCESSOR_DEFINITIONS = $(inherited) COCOAPODS=1 diff --git a/Example/Pods/Target Support Files/Pods-rapidsnark_Tests/Pods-rapidsnark_Tests.debug.xcconfig b/Example/Pods/Target Support Files/Pods-rapidsnark_Tests/Pods-rapidsnark_Tests.debug.xcconfig index 886431d..c1be7c7 100644 --- a/Example/Pods/Target Support Files/Pods-rapidsnark_Tests/Pods-rapidsnark_Tests.debug.xcconfig +++ b/Example/Pods/Target Support Files/Pods-rapidsnark_Tests/Pods-rapidsnark_Tests.debug.xcconfig @@ -1,3 +1,4 @@ +CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES = YES CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = NO FRAMEWORK_SEARCH_PATHS = $(inherited) "${PODS_CONFIGURATION_BUILD_DIR}/rapidsnark" "${PODS_ROOT}/../../Libs" GCC_PREPROCESSOR_DEFINITIONS = $(inherited) COCOAPODS=1 diff --git a/Example/Pods/Target Support Files/Pods-rapidsnark_Tests/Pods-rapidsnark_Tests.release.xcconfig b/Example/Pods/Target Support Files/Pods-rapidsnark_Tests/Pods-rapidsnark_Tests.release.xcconfig index 886431d..c1be7c7 100644 --- a/Example/Pods/Target Support Files/Pods-rapidsnark_Tests/Pods-rapidsnark_Tests.release.xcconfig +++ b/Example/Pods/Target Support Files/Pods-rapidsnark_Tests/Pods-rapidsnark_Tests.release.xcconfig @@ -1,3 +1,4 @@ +CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES = YES CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = NO FRAMEWORK_SEARCH_PATHS = $(inherited) "${PODS_CONFIGURATION_BUILD_DIR}/rapidsnark" "${PODS_ROOT}/../../Libs" GCC_PREPROCESSOR_DEFINITIONS = $(inherited) COCOAPODS=1 diff --git a/Example/Pods/Target Support Files/rapidsnark/rapidsnark-Info.plist b/Example/Pods/Target Support Files/rapidsnark/rapidsnark-Info.plist index 434e06a..8928a8d 100644 --- a/Example/Pods/Target Support Files/rapidsnark/rapidsnark-Info.plist +++ b/Example/Pods/Target Support Files/rapidsnark/rapidsnark-Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.1.0 + 0.0.1 CFBundleSignature ???? CFBundleVersion diff --git a/Example/Pods/Target Support Files/rapidsnark/rapidsnark.debug.xcconfig b/Example/Pods/Target Support Files/rapidsnark/rapidsnark.debug.xcconfig index 621d6e2..0879436 100644 --- a/Example/Pods/Target Support Files/rapidsnark/rapidsnark.debug.xcconfig +++ b/Example/Pods/Target Support Files/rapidsnark/rapidsnark.debug.xcconfig @@ -1,3 +1,4 @@ +CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES = YES CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = NO CONFIGURATION_BUILD_DIR = ${PODS_CONFIGURATION_BUILD_DIR}/rapidsnark FRAMEWORK_SEARCH_PATHS = $(inherited) "${PODS_ROOT}/../../Libs" diff --git a/Example/Pods/Target Support Files/rapidsnark/rapidsnark.release.xcconfig b/Example/Pods/Target Support Files/rapidsnark/rapidsnark.release.xcconfig index 621d6e2..0879436 100644 --- a/Example/Pods/Target Support Files/rapidsnark/rapidsnark.release.xcconfig +++ b/Example/Pods/Target Support Files/rapidsnark/rapidsnark.release.xcconfig @@ -1,3 +1,4 @@ +CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES = YES CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = NO CONFIGURATION_BUILD_DIR = ${PODS_CONFIGURATION_BUILD_DIR}/rapidsnark FRAMEWORK_SEARCH_PATHS = $(inherited) "${PODS_ROOT}/../../Libs" diff --git a/Sources/rapidsnark/rapidsnark.swift b/Sources/rapidsnark/rapidsnark.swift index f5a0adf..42ee098 100644 --- a/Sources/rapidsnark/rapidsnark.swift +++ b/Sources/rapidsnark/rapidsnark.swift @@ -1,5 +1,5 @@ #if canImport(C) - import C +import C #endif import Darwin.C.string @@ -10,19 +10,19 @@ public let defaultErrorBufferSize = 256 /** Performs cryptographic proofs using the Groth16 proving scheme - + - Parameters: - - zkey: The zkey data used to generate the proof. - - witness: The witness data used to generate the proof. - - proofBufferSize: The size of the buffer to store the proof. Defaults to a predefined value. - - publicBufferSize: The size for the public signal buffer. If not provided, it's calculated dynamically. - - errorBufferSize: The size of the buffer for error messages. Defaults to a predefined value. - + - zkey: The zkey data used to generate the proof. + - witness: The witness data used to generate the proof. + - proofBufferSize: The size of the buffer to store the proof. Defaults to a predefined value. + - publicBufferSize: The size for the public signal buffer. If not provided, it's calculated dynamically. + - errorBufferSize: The size of the buffer for error messages. Defaults to a predefined value. + - Throws: `RapidsnarkProverError` child classes - if the proof generation fails, with the error message indicating the reason for failure. - + if the proof generation fails, with the error message indicating the reason for failure. + - Returns: A tuple containing the generated proof as a string and the public signals as a string. -*/ + */ public func groth16Prove( zkey: Data, witness: Data, @@ -32,7 +32,7 @@ public func groth16Prove( ) throws -> (proof: String, publicSignals: String) { let zkeyBuf = NSData(data: zkey).bytes let witnessBuf = NSData(data: witness).bytes - + // calculate the size for the public signal buffer if not provided var currentPublicBufferSize : Int; if let publicBufferSize { @@ -40,15 +40,15 @@ public func groth16Prove( } else { currentPublicBufferSize = try groth16PublicSizeForZkeyBuf(zkey: zkey, errorBufferSize: errorBufferSize); } - + var proofBuffer = Array(repeating: 0, count: proofBufferSize); var publicBuffer = Array(repeating: 0, count: currentPublicBufferSize) var errorMessageBuffer: [CChar] = Array(repeating: 0, count: errorBufferSize) - + var proofBufferSizeUInt = UInt(proofBufferSize) var currentPublicBufferSizeUInt = UInt(currentPublicBufferSize) let errorBufferSizeUInt = UInt(errorBufferSize) - + // Call the rapidsnark C++ library function to perform the Groth16 proof let statusCode = groth16_prover( zkeyBuf, UInt(zkey.count), @@ -57,29 +57,29 @@ public func groth16Prove( &publicBuffer, ¤tPublicBufferSizeUInt, &errorMessageBuffer, errorBufferSizeUInt ); - + if (statusCode == PROVER_OK) { return (String(cString: proofBuffer), String(cString: publicBuffer)) } - + throw groth16proverStatusCodeErrors(statusCode, message: String(cString: errorMessageBuffer)) } /** Performs cryptographic proofs using the Groth16 proving scheme. - + - Parameters: - - zkeyPath: The path to the .zkey file. - - witness: The witness data used to generate the proof. - - proofBufferSize: The size of the buffer to store the proof. Defaults to a predefined value. - - publicBufferSize: The size for the public signal buffer. If not provided, it's calculated dynamically. - - errorBufferSize: The size of the buffer for error messages. Defaults to a predefined value. - + - zkeyPath: The path to the .zkey file. + - witness: The witness data used to generate the proof. + - proofBufferSize: The size of the buffer to store the proof. Defaults to a predefined value. + - publicBufferSize: The size for the public signal buffer. If not provided, it's calculated dynamically. + - errorBufferSize: The size of the buffer for error messages. Defaults to a predefined value. + - Throws: `RapidsnarkProverError` child classes - if the proof generation fails, with the error message indicating the reason for failure. - + if the proof generation fails, with the error message indicating the reason for failure. + - Returns: A tuple containing the generated proof as a string and the public signals as a string. -*/ + */ public func groth16ProveWithZKeyFilePath( zkeyPath: String, witness: Data, @@ -88,7 +88,7 @@ public func groth16ProveWithZKeyFilePath( errorBufferSize: Int = defaultErrorBufferSize ) throws -> (proof: String, publicSignals: String) { let witnessBuf = NSData(data: witness).bytes - + // calculate the size for the public signal buffer if not provided var currentPublicBufferSize : Int; if let publicBufferSize { @@ -96,15 +96,15 @@ public func groth16ProveWithZKeyFilePath( } else { currentPublicBufferSize = try groth16PublicSizeForZkeyFile(zkeyPath: zkeyPath, errorBufferSize: errorBufferSize); } - + var proofBuffer = Array(repeating: 0, count: proofBufferSize); var publicBuffer = Array(repeating: 0, count: currentPublicBufferSize) var errorMessageBuffer: [CChar] = Array(repeating: 0, count: errorBufferSize) - + var proofBufferSizeUInt = UInt(proofBufferSize) var currentPublicBufferSizeUInt = UInt(currentPublicBufferSize) let errorBufferSizeUInt = UInt(errorBufferSize) - + // Call the rapidsnark C++ library function to perform the Groth16 proof let statusCode = groth16_prover_zkey_file( zkeyPath, @@ -113,28 +113,28 @@ public func groth16ProveWithZKeyFilePath( &publicBuffer, ¤tPublicBufferSizeUInt, &errorMessageBuffer, errorBufferSizeUInt ); - + if (statusCode == PROVER_OK) { return (String(cString: proofBuffer), String(cString: publicBuffer)) } - + throw groth16proverStatusCodeErrors(statusCode, message: String(cString: errorMessageBuffer)) } /** Verifying proofs using the Groth16 scheme. - + - Parameters: - - proof: The proof data to be verified. - - inputs: The input data used for verification. - - verificationKey: The verification key data used for verification. - - errorBufferSize: The size of the buffer for error messages. Defaults to a predefined value. - + - proof: The proof data to be verified. + - inputs: The input data used for verification. + - verificationKey: The verification key data used for verification. + - errorBufferSize: The size of the buffer for error messages. Defaults to a predefined value. + - Throws: `RapidsnarkVerifierError` child classes - if the proof verification fails, with the error message indicating the reason for failure. - + if the proof verification fails, with the error message indicating the reason for failure. + - Returns: A boolean value indicating whether the proof is valid (`true`) or not (`false`). -*/ + */ public func groth16Verify( proof: Data, inputs: Data, @@ -144,9 +144,9 @@ public func groth16Verify( let proofBuf = NSData(data: proof).bytes let inputsBuf = NSData(data: inputs).bytes let verificationKeyBuf = NSData(data: verificationKey).bytes - + var errorMessageBuffer: [CChar] = Array(repeating: 0, count: errorBufferSize) - + // Call the rapidsnark C++ library function to perform the Groth16 Verification let statusCode = groth16_verify( proofBuf, @@ -155,37 +155,41 @@ public func groth16Verify( &errorMessageBuffer, UInt(errorBufferSize) ); - + if (statusCode == VERIFIER_VALID_PROOF) { return true; + } else if (statusCode == VERIFIER_INVALID_PROOF) { + throw RapidsnarkVerifierError.invalidProof(message: String(cString: errorMessageBuffer)) + } else if (statusCode == VERIFIER_ERROR) { + throw RapidsnarkVerifierError.error(message: String(cString: errorMessageBuffer)) + } else { + throw RapidsnarkUnknownStatusError(message: String(cString: errorMessageBuffer)) } - - throw groth16proverStatusCodeErrors(statusCode, message: String(cString: errorMessageBuffer)) } /** Calculates the size of the public signal buffer based on the provided zkey file data. - + - Parameters: - - zkey: The zkey data used to calculate the public signal buffer size. - - errorBufferSize: The size of the buffer for error messages. Defaults to a predefined value. - + - zkey: The zkey data used to calculate the public signal buffer size. + - errorBufferSize: The size of the buffer for error messages. Defaults to a predefined value. + - Throws: `RapidsnarkProverError` child classes - if the calculation of the public signal buffer size fails, with the error message indicating the reason for failure. - + if the calculation of the public signal buffer size fails, with the error message indicating the reason for failure. + - Returns: An integer value representing the calculated size of the public signal buffer. -*/ + */ public func groth16PublicSizeForZkeyBuf( zkey: Data, errorBufferSize: Int = defaultErrorBufferSize ) throws -> Int { let zkeyBuffer = NSData(data: zkey).bytes - + var errorMessageBuffer: [CChar] = Array(repeating: 0, count: errorBufferSize) - + var size = 0 - + let statusCode = groth16_public_size_for_zkey_buf( zkeyBuffer, UInt(zkey.count), @@ -193,46 +197,46 @@ public func groth16PublicSizeForZkeyBuf( &errorMessageBuffer, UInt(errorBufferSize) ); - + if (statusCode == PROVER_OK) { return size } - + throw RapidsnarkProverError.error(message: String(cString: errorMessageBuffer)) } /** Determine the necessary buffer size for storing public signals based on a provided .zkey file - + - Parameters: - - zkeyPath: The path to the .zkey file used to calculate the public signal buffer size. - - errorBufferSize: The size of the buffer for error messages. Defaults to a predefined value. - + - zkeyPath: The path to the .zkey file used to calculate the public signal buffer size. + - errorBufferSize: The size of the buffer for error messages. Defaults to a predefined value. + - Throws: `RapidsnarkProverError` child classes - if the calculation of the public signal buffer size fails, with the error message indicating the reason for failure. - + if the calculation of the public signal buffer size fails, with the error message indicating the reason for failure. + - Returns: An integer value representing the calculated size of the public signal buffer. -*/ + */ public func groth16PublicSizeForZkeyFile( zkeyPath: String, errorBufferSize: Int = defaultErrorBufferSize ) throws -> Int { var errorMessageBuffer: [CChar] = Array(repeating: 0, count: errorBufferSize) - + var size = 0 - + let statusCode = groth16_public_size_for_zkey_file( zkeyPath, &size, &errorMessageBuffer, UInt(errorBufferSize) ); - + if (statusCode == PROVER_OK) { return size } - + throw RapidsnarkProverError.error(message: String(cString: errorMessageBuffer)) } @@ -245,20 +249,22 @@ private func groth16proverStatusCodeErrors(_ statusCode: Int32, message: String) return RapidsnarkProverError.shortBuffer(message: message) case PROVER_INVALID_WITNESS_LENGTH: return RapidsnarkProverError.invalidWitnessLength(message: message) - case VERIFIER_INVALID_PROOF: - return RapidsnarkVerifierError.invalidProof(message: message) - case VERIFIER_ERROR: - return RapidsnarkVerifierError.error(message: message) default: return RapidsnarkUnknownStatusError(message: message) } } -public protocol RapidsnarkError : Error { +public protocol RapidsnarkError : CustomNSError { var message: String { get } } +public extension RapidsnarkError { + var errorUserInfo: [String : Any] { + return ["message": message] + } +} + public enum RapidsnarkProverError : RapidsnarkError { case error(message: String) case shortBuffer(message: String) @@ -274,6 +280,17 @@ public enum RapidsnarkProverError : RapidsnarkError { return message } } + + public var errorCode: Int { + switch self { + case .error: + return NSNumber(value: PROVER_ERROR).intValue + case .shortBuffer: + return NSNumber(value: PROVER_ERROR_SHORT_BUFFER).intValue + case .invalidWitnessLength: + return NSNumber(value: PROVER_INVALID_WITNESS_LENGTH).intValue + } + } } public enum RapidsnarkVerifierError : RapidsnarkError { @@ -288,6 +305,15 @@ public enum RapidsnarkVerifierError : RapidsnarkError { return message } } + + public var errorCode: Int { + switch self { + case .error: + return NSNumber(value: VERIFIER_ERROR).intValue + case .invalidProof: + return NSNumber(value: VERIFIER_INVALID_PROOF).intValue + } + } } public class RapidsnarkUnknownStatusError : RapidsnarkError { @@ -296,4 +322,8 @@ public class RapidsnarkUnknownStatusError : RapidsnarkError { init(message: String) { self.message = message } + + public var errorCode: Int { + return -1 + } } diff --git a/rapidsnark.podspec b/rapidsnark.podspec index c154b30..22dc7ac 100644 --- a/rapidsnark.podspec +++ b/rapidsnark.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'rapidsnark' - s.version = '0.0.1-alpha.3' + s.version = '0.0.1-alpha.4' s.summary = 'Swift wrapper for the rapidsnark proof generation library.' # This description is used to generate tags and improve search results.