-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCryptoIdolModule.swift
131 lines (111 loc) · 5.61 KB
/
CryptoIdolModule.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import React
import EzklPackage // Replace with the actual name of your SPM library
@objc(NativeModelProver)
class NativeModelProver: NSObject {
@objc
func setupKeys(_ param: String, resolver: @escaping RCTPromiseResolveBlock, rejecter: @escaping RCTPromiseRejectBlock) {
DispatchQueue.global(qos: .userInitiated).async {
do {
// Parse JSON parameter to get paths
guard let paths = try JSONSerialization.jsonObject(with: param.data(using: .utf8)!, options: []) as? [String: String],
var compiledCircuitPath = paths["compiledCircuit"],
var srsPath = paths["srs"] else {
throw NSError(domain: "InvalidParameterError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Invalid paths in input parameter."])
}
// Remove "file://" prefix if present
if compiledCircuitPath.hasPrefix("file://") {
compiledCircuitPath = String(compiledCircuitPath.dropFirst(7))
}
if srsPath.hasPrefix("file://") {
srsPath = String(srsPath.dropFirst(7))
}
// Read compiledCircuit and srs files as binary data
let compiledCircuitData = try Data(contentsOf: URL(fileURLWithPath: compiledCircuitPath))
let srsData = try Data(contentsOf: URL(fileURLWithPath: srsPath))
// Generate vk and pk using the functions in EzklPackage
let vk = try EzklPackage.genVk(compiledCircuit: compiledCircuitData, srs: srsData, compressSelectors: true)
let pk = try EzklPackage.genPk(vk: vk, compiledCircuit: compiledCircuitData, srs: srsData)
// Save vk and pk to local files
let vkPath = FileManager.default.temporaryDirectory.appendingPathComponent("vk.key")
let pkPath = FileManager.default.temporaryDirectory.appendingPathComponent("pk.key")
try vk.write(to: vkPath)
try pk.write(to: pkPath)
// Prepare result dictionary to send back file paths to JavaScript
let result: [String: String] = [
"vkPath": vkPath.path,
"pkPath": pkPath.path
]
// Return to the main thread and resolve with file paths
DispatchQueue.main.async {
resolver(result)
}
} catch {
// Handle any errors and reject promise with full error description
DispatchQueue.main.async {
let errorMessage = "Error during `setupKeys` computation: \(error.localizedDescription)"
rejecter("E_COMPUTATION_ERROR", errorMessage, error)
}
}
}
}
@objc
func generateProof(_ param: String, resolver: @escaping RCTPromiseResolveBlock, rejecter: @escaping RCTPromiseRejectBlock) {
DispatchQueue.global(qos: .userInitiated).async {
do {
// Parse JSON parameter to get paths for pk, compiledCircuit, and srs
guard let paths = try JSONSerialization.jsonObject(with: param.data(using: .utf8)!, options: []) as? [String: String],
var pkPath = paths["pk"],
var compiledCircuitPath = paths["compiledCircuit"],
var srsPath = paths["srs"],
let inputDataString = paths["inputData"] else {
throw NSError(domain: "InvalidParameterError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Invalid paths in input parameter."])
}
// Strip "file://" prefix if present
if pkPath.hasPrefix("file://") {
pkPath = String(pkPath.dropFirst(7))
}
if compiledCircuitPath.hasPrefix("file://") {
compiledCircuitPath = String(compiledCircuitPath.dropFirst(7))
}
if srsPath.hasPrefix("file://") {
srsPath = String(srsPath.dropFirst(7))
}
// Read pk, compiledCircuit, and srs files as binary data
let pkData = try Data(contentsOf: URL(fileURLWithPath: pkPath))
let compiledCircuitData = try Data(contentsOf: URL(fileURLWithPath: compiledCircuitPath))
let srsData = try Data(contentsOf: URL(fileURLWithPath: srsPath))
// Convert inputData (passed as JSON string) into Data
guard let inputData = inputDataString.data(using: .utf8) else {
throw NSError(domain: "DataConversionError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Failed to convert inputData to Data."])
}
// Generate witness using genWitness
let witness = try EzklPackage.genWitness(compiledCircuit: compiledCircuitData, input: inputData)
// Generate proof using prove
let proofData = try EzklPackage.prove(witness: witness, pk: pkData, compiledCircuit: compiledCircuitData, srs: srsData)
// Convert proof Data to JSON string
guard let proofJSON = try JSONSerialization.jsonObject(with: proofData, options: []) as? [String: Any],
let proofString = String(data: try JSONSerialization.data(withJSONObject: proofJSON, options: []), encoding: .utf8) else {
throw NSError(domain: "JSONConversionError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Failed to convert proof data to JSON string."])
}
// Prepare result dictionary with proof JSON string as output
let result: [String: String] = [
"proof": proofString
]
// Return to the main thread and resolve with the proof
DispatchQueue.main.async {
resolver(result)
}
} catch {
// Handle any errors and reject promise with full error description
DispatchQueue.main.async {
let errorMessage = "Error during `generateProof` computation: \(error.localizedDescription)"
rejecter("E_COMPUTATION_ERROR", errorMessage, error)
}
}
}
}
@objc
static func requiresMainQueueSetup() -> Bool {
return false
}
}