diff --git a/ios/zerokit-ios/zerokit-ios.xcodeproj/project.pbxproj b/ios/zerokit-ios/zerokit-ios.xcodeproj/project.pbxproj index b3fcc65..928f669 100644 --- a/ios/zerokit-ios/zerokit-ios.xcodeproj/project.pbxproj +++ b/ios/zerokit-ios/zerokit-ios.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ 7961AA77293428FD00B52AA9 /* rln.wasm in Resources */ = {isa = PBXBuildFile; fileRef = 7961AA74293428FD00B52AA9 /* rln.wasm */; }; 7961AA7929342D2C00B52AA9 /* verification_key.json in Resources */ = {isa = PBXBuildFile; fileRef = 7961AA7829342D2C00B52AA9 /* verification_key.json */; }; 799E22A12938FB5D004476D7 /* zerokit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 799E22A02938FB5D004476D7 /* zerokit.swift */; }; + 799E22A329390A44004476D7 /* CBuffer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 799E22A229390A44004476D7 /* CBuffer.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -30,6 +31,7 @@ 7961AA74293428FD00B52AA9 /* rln.wasm */ = {isa = PBXFileReference; lastKnownFileType = file; name = rln.wasm; path = ../../resources/rln.wasm; sourceTree = ""; }; 7961AA7829342D2C00B52AA9 /* verification_key.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = verification_key.json; path = ../../resources/verification_key.json; sourceTree = ""; }; 799E22A02938FB5D004476D7 /* zerokit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = zerokit.swift; sourceTree = ""; }; + 799E22A229390A44004476D7 /* CBuffer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CBuffer.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -72,6 +74,7 @@ 7961AA74293428FD00B52AA9 /* rln.wasm */, 791439182933B320003246AC /* Preview Content */, 799E22A02938FB5D004476D7 /* zerokit.swift */, + 799E22A229390A44004476D7 /* CBuffer.swift */, ); path = "zerokit-ios"; sourceTree = ""; @@ -176,6 +179,7 @@ buildActionMask = 2147483647; files = ( 799E22A12938FB5D004476D7 /* zerokit.swift in Sources */, + 799E22A329390A44004476D7 /* CBuffer.swift in Sources */, 791439152933B31F003246AC /* ContentView.swift in Sources */, 791439132933B31F003246AC /* zerokit_iosApp.swift in Sources */, ); diff --git a/ios/zerokit-ios/zerokit-ios/CBuffer.swift b/ios/zerokit-ios/zerokit-ios/CBuffer.swift new file mode 100644 index 0000000..d2c145a --- /dev/null +++ b/ios/zerokit-ios/zerokit-ios/CBuffer.swift @@ -0,0 +1,45 @@ +import Foundation + +class CBuffer { + let bufferPtr: UnsafeMutablePointer + + init(input: inout [UInt8]) { + bufferPtr = CBuffer.toPointer(input: &input) + } + + init() { + let size = MemoryLayout.stride + bufferPtr = UnsafeMutablePointer.allocate(capacity: size) + } + + deinit { + bufferPtr.deallocate() + } + + // toBuffer converts the input to a buffer object that is used to communicate data with the rln lib + private static func toBuffer(data: inout [UInt8]) -> Buffer { + let result = sliceToPtr(data: &data) + return Buffer( + ptr: result.dataPtr, + len: uintptr_t(result.dataLen) + ) + } + + private static func sliceToPtr(data: inout [UInt8]) -> (dataPtr: UnsafePointer?, dataLen: CInt) { + if data.count == 0 { + return (nil, CInt(0)) + } else { + let uint8Pointer = UnsafeMutablePointer.allocate(capacity: data.count) + uint8Pointer.initialize(from: &data, count: data.count) + return (UnsafePointer(uint8Pointer), CInt(data.count)) + } + } + + private static func toPointer(input: inout [UInt8]) -> UnsafeMutablePointer { + let buf = toBuffer(data: &input) + let size = MemoryLayout.stride(ofValue: buf) + let allocB = UnsafeMutablePointer.allocate(capacity: size) + allocB.initialize(to: buf) + return allocB + } +} diff --git a/ios/zerokit-ios/zerokit-ios/zerokit.swift b/ios/zerokit-ios/zerokit-ios/zerokit.swift index a179975..8ae1a9f 100644 --- a/ios/zerokit-ios/zerokit-ios/zerokit.swift +++ b/ios/zerokit-ios/zerokit-ios/zerokit.swift @@ -1,33 +1,5 @@ import Foundation -// toBuffer converts the input to a buffer object that is used to communicate data with the rln lib -func toBuffer(data: inout [UInt8]) -> Buffer { - let result = sliceToPtr(data: &data) - return Buffer( - ptr: result.dataPtr, - len: uintptr_t(result.dataLen) - ) -} - -func sliceToPtr(data: inout [UInt8]) -> (dataPtr: UnsafePointer?, dataLen: CInt) { - if data.count == 0 { - return (nil, CInt(0)) - } else { - let uint8Pointer = UnsafeMutablePointer.allocate(capacity: data.count) - uint8Pointer.initialize(from: &data, count: data.count) - return (UnsafePointer(uint8Pointer), CInt(data.count)) - // return (UnsafePointer(data), CInt(data.count)) - } -} - -func toCBufferPtr(input: inout [UInt8]) -> UnsafeMutablePointer { - let buf = toBuffer(data: &input) - let size = MemoryLayout.stride(ofValue: buf) - let allocB = UnsafeMutablePointer.allocate(capacity: size) - allocB.initialize(to: buf) - return allocB -} - func readFile(filename: String, filetype: String) -> [UInt8] { do { let path = Bundle.main.path(forResource: filename, ofType: filetype) @@ -40,16 +12,11 @@ func readFile(filename: String, filetype: String) -> [UInt8] { return [UInt8]() } -func createOutputBuffer() -> UnsafeMutablePointer { - let size = MemoryLayout.stride - return UnsafeMutablePointer.allocate(capacity: size) -} - func generateCredentials(ctx: OpaquePointer!) -> [UInt8] { // Generating credentials ====== - let credentialBuffer = createOutputBuffer() - if key_gen(ctx, credentialBuffer) { - let bufferPointer = UnsafeRawBufferPointer(start: credentialBuffer.pointee.ptr, count: Int(credentialBuffer.pointee.len)) + let credentialBuffer = CBuffer() + if key_gen(ctx, credentialBuffer.bufferPtr) { + let bufferPointer = UnsafeRawBufferPointer(start: credentialBuffer.bufferPtr.pointee.ptr, count: Int(credentialBuffer.bufferPtr.pointee.len)) var generatedKeyBytes = [UInt8]() bufferPointer.withUnsafeBytes { generatedKeyBytes.append(contentsOf: $0) @@ -65,9 +32,9 @@ func generateCredentials(ctx: OpaquePointer!) -> [UInt8] { } func getMerkleRoot(ctx: OpaquePointer!) -> [UInt8] { - let rootBuffer = createOutputBuffer() - if get_root(ctx, rootBuffer) { - let bufferPointer = UnsafeRawBufferPointer(start: rootBuffer.pointee.ptr, count: Int(rootBuffer.pointee.len)) + let output = CBuffer() + if get_root(ctx, output.bufferPtr) { + let bufferPointer = UnsafeRawBufferPointer(start: output.bufferPtr.pointee.ptr, count: Int(output.bufferPtr.pointee.len)) var rootBytes = [UInt8]() bufferPointer.withUnsafeBytes { rootBytes.append(contentsOf: $0) @@ -85,14 +52,14 @@ func newRLN() -> String { var zkey_bytes = readFile(filename: "rln_final", filetype: "zkey") var vk_bytes = readFile(filename: "verification_key", filetype: "json") - let circom_buffer = toCBufferPtr(input: &circom_bytes) - let zkey_buffer = toCBufferPtr(input: &zkey_bytes) - let vk_buffer = toCBufferPtr(input: &vk_bytes) + let circom_buffer = CBuffer(input: &circom_bytes) + let zkey_buffer = CBuffer(input: &zkey_bytes) + let vk_buffer = CBuffer(input: &vk_bytes) // Instantiating RLN object let objUnsafeMutablePtr = UnsafeMutablePointer.allocate(capacity: 1) var ctx : OpaquePointer! = OpaquePointer(objUnsafeMutablePtr) - if !new_with_params(20, circom_buffer, zkey_buffer, vk_buffer, &ctx) { + if !new_with_params(20, circom_buffer.bufferPtr, zkey_buffer.bufferPtr, vk_buffer.bufferPtr, &ctx) { // TODO: throw error }