|
| 1 | +# OpenACSwift |
| 2 | + |
| 3 | +Swift bindings for the OpenAC zero-knowledge proof system, enabling RS256 circuit proof generation and verification on iOS. |
| 4 | + |
| 5 | +The prebuilt binaries are distributed via the [zkID latest release](https://github.com/zkmopro/zkID/releases/tag/latest). |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +- iOS 16+ |
| 10 | +- Xcode 15+ |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +### Swift Package Manager |
| 15 | + |
| 16 | +Add OpenACSwift to your `Package.swift`: |
| 17 | + |
| 18 | +```swift |
| 19 | +dependencies: [ |
| 20 | + .package(url: "https://github.com/zkmopro/OpenACSwift", from: "1.0.0"), |
| 21 | +], |
| 22 | +targets: [ |
| 23 | + .target( |
| 24 | + name: "YourTarget", |
| 25 | + dependencies: ["OpenACSwift"] |
| 26 | + ), |
| 27 | +] |
| 28 | +``` |
| 29 | + |
| 30 | +Or in Xcode: **File → Add Package Dependencies**, enter the repository URL. |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +Import the library and call the three main functions in order: `setupKeys` → `prove` → `verify`. |
| 35 | + |
| 36 | +```swift |
| 37 | +import OpenACSwift |
| 38 | +``` |
| 39 | + |
| 40 | +### 1. Setup Keys |
| 41 | + |
| 42 | +Generates the proving and verifying keys for the RS256 circuit. This is a one-time setup step that writes key files to `documentsPath`. Run this before the first call to `prove`. |
| 43 | + |
| 44 | +```swift |
| 45 | +let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].path |
| 46 | +let inputPath = Bundle.main.path(forResource: "input", ofType: "json") |
| 47 | + |
| 48 | +do { |
| 49 | + let message = try setupKeys(documentsPath: documentsPath, inputPath: inputPath) |
| 50 | + print("Setup complete:", message) |
| 51 | +} catch let error as ZkProofError { |
| 52 | + print("Setup failed:", error) |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +**Parameters:** |
| 57 | +- `documentsPath` — directory where key files are written and read |
| 58 | +- `inputPath` — optional path to a JSON file with circuit inputs; pass `nil` to use defaults |
| 59 | + |
| 60 | +**Returns:** a status string confirming completion. |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +### 2. Prove |
| 65 | + |
| 66 | +Generates a zero-knowledge proof using the keys created by `setupKeys`. |
| 67 | + |
| 68 | +```swift |
| 69 | +do { |
| 70 | + let result: ProofResult = try prove(documentsPath: documentsPath, inputPath: inputPath) |
| 71 | + print("Proof generated in \(result.proveMs) ms, size: \(result.proofSizeBytes) bytes") |
| 72 | +} catch let error as ZkProofError { |
| 73 | + print("Proving failed:", error) |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +**Parameters:** |
| 78 | +- `documentsPath` — same directory passed to `setupKeys` |
| 79 | +- `inputPath` — optional path to the circuit input JSON |
| 80 | + |
| 81 | +**Returns:** `ProofResult` with: |
| 82 | +- `proveMs: UInt64` — time taken to generate the proof in milliseconds |
| 83 | +- `proofSizeBytes: UInt64` — size of the generated proof in bytes |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +### 3. Verify |
| 88 | + |
| 89 | +Verifies the proof produced by `prove`. |
| 90 | + |
| 91 | +```swift |
| 92 | +do { |
| 93 | + let valid = try verify(documentsPath: documentsPath) |
| 94 | + print(valid ? "Proof is valid" : "Proof is invalid") |
| 95 | +} catch let error as ZkProofError { |
| 96 | + print("Verification failed:", error) |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +**Parameters:** |
| 101 | +- `documentsPath` — same directory passed to `setupKeys` and `prove` |
| 102 | + |
| 103 | +**Returns:** `true` if the proof is valid, `false` otherwise. |
| 104 | + |
| 105 | +--- |
| 106 | + |
| 107 | +### Full Example |
| 108 | + |
| 109 | +```swift |
| 110 | +import OpenACSwift |
| 111 | + |
| 112 | +func runZKProof() { |
| 113 | + let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].path |
| 114 | + let inputPath = Bundle.main.path(forResource: "input", ofType: "json") |
| 115 | + |
| 116 | + do { |
| 117 | + // 1. Generate keys (run once; skip if keys already exist) |
| 118 | + let status = try setupKeys(documentsPath: documentsPath, inputPath: inputPath) |
| 119 | + print("Keys ready:", status) |
| 120 | + |
| 121 | + // 2. Generate proof |
| 122 | + let proof = try prove(documentsPath: documentsPath, inputPath: inputPath) |
| 123 | + print("Proved in \(proof.proveMs) ms (\(proof.proofSizeBytes) bytes)") |
| 124 | + |
| 125 | + // 3. Verify proof |
| 126 | + let valid = try verify(documentsPath: documentsPath) |
| 127 | + print("Valid:", valid) |
| 128 | + } catch let error as ZkProofError { |
| 129 | + switch error { |
| 130 | + case .SetupRequired(let msg): print("Run setupKeys first:", msg) |
| 131 | + case .FileNotFound(let msg): print("Missing file:", msg) |
| 132 | + case .InvalidInput(let msg): print("Bad input:", msg) |
| 133 | + case .ProofGenerationFailed(let msg): print("Prove error:", msg) |
| 134 | + case .VerificationFailed(let msg): print("Verify error:", msg) |
| 135 | + case .IoError(let msg): print("IO error:", msg) |
| 136 | + } |
| 137 | + } catch { |
| 138 | + print("Unexpected error:", error) |
| 139 | + } |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +## Error Handling |
| 144 | + |
| 145 | +All three functions throw `ZkProofError`: |
| 146 | + |
| 147 | +| Case | Description | |
| 148 | +|------|-------------| |
| 149 | +| `SetupRequired` | `prove` or `verify` called before `setupKeys` | |
| 150 | +| `FileNotFound` | A required file is missing from `documentsPath` | |
| 151 | +| `InvalidInput` | The input JSON is malformed or missing required fields | |
| 152 | +| `ProofGenerationFailed` | An error occurred during proof generation | |
| 153 | +| `VerificationFailed` | The proof failed verification | |
| 154 | +| `IoError` | A filesystem read/write error occurred | |
0 commit comments