refactor: Buffer RAII
This commit is contained in:
parent
657a53b7a4
commit
00db19a252
|
@ -16,6 +16,7 @@
|
||||||
7961AA77293428FD00B52AA9 /* rln.wasm in Resources */ = {isa = PBXBuildFile; fileRef = 7961AA74293428FD00B52AA9 /* rln.wasm */; };
|
7961AA77293428FD00B52AA9 /* rln.wasm in Resources */ = {isa = PBXBuildFile; fileRef = 7961AA74293428FD00B52AA9 /* rln.wasm */; };
|
||||||
7961AA7929342D2C00B52AA9 /* verification_key.json in Resources */ = {isa = PBXBuildFile; fileRef = 7961AA7829342D2C00B52AA9 /* verification_key.json */; };
|
7961AA7929342D2C00B52AA9 /* verification_key.json in Resources */ = {isa = PBXBuildFile; fileRef = 7961AA7829342D2C00B52AA9 /* verification_key.json */; };
|
||||||
799E22A12938FB5D004476D7 /* zerokit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 799E22A02938FB5D004476D7 /* zerokit.swift */; };
|
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 */
|
/* End PBXBuildFile section */
|
||||||
|
|
||||||
/* Begin PBXFileReference section */
|
/* Begin PBXFileReference section */
|
||||||
|
@ -30,6 +31,7 @@
|
||||||
7961AA74293428FD00B52AA9 /* rln.wasm */ = {isa = PBXFileReference; lastKnownFileType = file; name = rln.wasm; path = ../../resources/rln.wasm; sourceTree = "<group>"; };
|
7961AA74293428FD00B52AA9 /* rln.wasm */ = {isa = PBXFileReference; lastKnownFileType = file; name = rln.wasm; path = ../../resources/rln.wasm; sourceTree = "<group>"; };
|
||||||
7961AA7829342D2C00B52AA9 /* verification_key.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = verification_key.json; path = ../../resources/verification_key.json; sourceTree = "<group>"; };
|
7961AA7829342D2C00B52AA9 /* verification_key.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = verification_key.json; path = ../../resources/verification_key.json; sourceTree = "<group>"; };
|
||||||
799E22A02938FB5D004476D7 /* zerokit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = zerokit.swift; sourceTree = "<group>"; };
|
799E22A02938FB5D004476D7 /* zerokit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = zerokit.swift; sourceTree = "<group>"; };
|
||||||
|
799E22A229390A44004476D7 /* CBuffer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CBuffer.swift; sourceTree = "<group>"; };
|
||||||
/* End PBXFileReference section */
|
/* End PBXFileReference section */
|
||||||
|
|
||||||
/* Begin PBXFrameworksBuildPhase section */
|
/* Begin PBXFrameworksBuildPhase section */
|
||||||
|
@ -72,6 +74,7 @@
|
||||||
7961AA74293428FD00B52AA9 /* rln.wasm */,
|
7961AA74293428FD00B52AA9 /* rln.wasm */,
|
||||||
791439182933B320003246AC /* Preview Content */,
|
791439182933B320003246AC /* Preview Content */,
|
||||||
799E22A02938FB5D004476D7 /* zerokit.swift */,
|
799E22A02938FB5D004476D7 /* zerokit.swift */,
|
||||||
|
799E22A229390A44004476D7 /* CBuffer.swift */,
|
||||||
);
|
);
|
||||||
path = "zerokit-ios";
|
path = "zerokit-ios";
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
|
@ -176,6 +179,7 @@
|
||||||
buildActionMask = 2147483647;
|
buildActionMask = 2147483647;
|
||||||
files = (
|
files = (
|
||||||
799E22A12938FB5D004476D7 /* zerokit.swift in Sources */,
|
799E22A12938FB5D004476D7 /* zerokit.swift in Sources */,
|
||||||
|
799E22A329390A44004476D7 /* CBuffer.swift in Sources */,
|
||||||
791439152933B31F003246AC /* ContentView.swift in Sources */,
|
791439152933B31F003246AC /* ContentView.swift in Sources */,
|
||||||
791439132933B31F003246AC /* zerokit_iosApp.swift in Sources */,
|
791439132933B31F003246AC /* zerokit_iosApp.swift in Sources */,
|
||||||
);
|
);
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
class CBuffer {
|
||||||
|
let bufferPtr: UnsafeMutablePointer<Buffer>
|
||||||
|
|
||||||
|
init(input: inout [UInt8]) {
|
||||||
|
bufferPtr = CBuffer.toPointer(input: &input)
|
||||||
|
}
|
||||||
|
|
||||||
|
init() {
|
||||||
|
let size = MemoryLayout<Buffer>.stride
|
||||||
|
bufferPtr = UnsafeMutablePointer<Buffer>.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<UInt8>?, dataLen: CInt) {
|
||||||
|
if data.count == 0 {
|
||||||
|
return (nil, CInt(0))
|
||||||
|
} else {
|
||||||
|
let uint8Pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: data.count)
|
||||||
|
uint8Pointer.initialize(from: &data, count: data.count)
|
||||||
|
return (UnsafePointer<UInt8>(uint8Pointer), CInt(data.count))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func toPointer(input: inout [UInt8]) -> UnsafeMutablePointer<Buffer> {
|
||||||
|
let buf = toBuffer(data: &input)
|
||||||
|
let size = MemoryLayout.stride(ofValue: buf)
|
||||||
|
let allocB = UnsafeMutablePointer<Buffer>.allocate(capacity: size)
|
||||||
|
allocB.initialize(to: buf)
|
||||||
|
return allocB
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,33 +1,5 @@
|
||||||
import Foundation
|
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<UInt8>?, dataLen: CInt) {
|
|
||||||
if data.count == 0 {
|
|
||||||
return (nil, CInt(0))
|
|
||||||
} else {
|
|
||||||
let uint8Pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: data.count)
|
|
||||||
uint8Pointer.initialize(from: &data, count: data.count)
|
|
||||||
return (UnsafePointer<UInt8>(uint8Pointer), CInt(data.count))
|
|
||||||
// return (UnsafePointer<UInt8>(data), CInt(data.count))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func toCBufferPtr(input: inout [UInt8]) -> UnsafeMutablePointer<Buffer> {
|
|
||||||
let buf = toBuffer(data: &input)
|
|
||||||
let size = MemoryLayout.stride(ofValue: buf)
|
|
||||||
let allocB = UnsafeMutablePointer<Buffer>.allocate(capacity: size)
|
|
||||||
allocB.initialize(to: buf)
|
|
||||||
return allocB
|
|
||||||
}
|
|
||||||
|
|
||||||
func readFile(filename: String, filetype: String) -> [UInt8] {
|
func readFile(filename: String, filetype: String) -> [UInt8] {
|
||||||
do {
|
do {
|
||||||
let path = Bundle.main.path(forResource: filename, ofType: filetype)
|
let path = Bundle.main.path(forResource: filename, ofType: filetype)
|
||||||
|
@ -40,16 +12,11 @@ func readFile(filename: String, filetype: String) -> [UInt8] {
|
||||||
return [UInt8]()
|
return [UInt8]()
|
||||||
}
|
}
|
||||||
|
|
||||||
func createOutputBuffer() -> UnsafeMutablePointer<Buffer> {
|
|
||||||
let size = MemoryLayout<Buffer>.stride
|
|
||||||
return UnsafeMutablePointer<Buffer>.allocate(capacity: size)
|
|
||||||
}
|
|
||||||
|
|
||||||
func generateCredentials(ctx: OpaquePointer!) -> [UInt8] {
|
func generateCredentials(ctx: OpaquePointer!) -> [UInt8] {
|
||||||
// Generating credentials ======
|
// Generating credentials ======
|
||||||
let credentialBuffer = createOutputBuffer()
|
let credentialBuffer = CBuffer()
|
||||||
if key_gen(ctx, credentialBuffer) {
|
if key_gen(ctx, credentialBuffer.bufferPtr) {
|
||||||
let bufferPointer = UnsafeRawBufferPointer(start: credentialBuffer.pointee.ptr, count: Int(credentialBuffer.pointee.len))
|
let bufferPointer = UnsafeRawBufferPointer(start: credentialBuffer.bufferPtr.pointee.ptr, count: Int(credentialBuffer.bufferPtr.pointee.len))
|
||||||
var generatedKeyBytes = [UInt8]()
|
var generatedKeyBytes = [UInt8]()
|
||||||
bufferPointer.withUnsafeBytes {
|
bufferPointer.withUnsafeBytes {
|
||||||
generatedKeyBytes.append(contentsOf: $0)
|
generatedKeyBytes.append(contentsOf: $0)
|
||||||
|
@ -65,9 +32,9 @@ func generateCredentials(ctx: OpaquePointer!) -> [UInt8] {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMerkleRoot(ctx: OpaquePointer!) -> [UInt8] {
|
func getMerkleRoot(ctx: OpaquePointer!) -> [UInt8] {
|
||||||
let rootBuffer = createOutputBuffer()
|
let output = CBuffer()
|
||||||
if get_root(ctx, rootBuffer) {
|
if get_root(ctx, output.bufferPtr) {
|
||||||
let bufferPointer = UnsafeRawBufferPointer(start: rootBuffer.pointee.ptr, count: Int(rootBuffer.pointee.len))
|
let bufferPointer = UnsafeRawBufferPointer(start: output.bufferPtr.pointee.ptr, count: Int(output.bufferPtr.pointee.len))
|
||||||
var rootBytes = [UInt8]()
|
var rootBytes = [UInt8]()
|
||||||
bufferPointer.withUnsafeBytes {
|
bufferPointer.withUnsafeBytes {
|
||||||
rootBytes.append(contentsOf: $0)
|
rootBytes.append(contentsOf: $0)
|
||||||
|
@ -85,14 +52,14 @@ func newRLN() -> String {
|
||||||
var zkey_bytes = readFile(filename: "rln_final", filetype: "zkey")
|
var zkey_bytes = readFile(filename: "rln_final", filetype: "zkey")
|
||||||
var vk_bytes = readFile(filename: "verification_key", filetype: "json")
|
var vk_bytes = readFile(filename: "verification_key", filetype: "json")
|
||||||
|
|
||||||
let circom_buffer = toCBufferPtr(input: &circom_bytes)
|
let circom_buffer = CBuffer(input: &circom_bytes)
|
||||||
let zkey_buffer = toCBufferPtr(input: &zkey_bytes)
|
let zkey_buffer = CBuffer(input: &zkey_bytes)
|
||||||
let vk_buffer = toCBufferPtr(input: &vk_bytes)
|
let vk_buffer = CBuffer(input: &vk_bytes)
|
||||||
|
|
||||||
// Instantiating RLN object
|
// Instantiating RLN object
|
||||||
let objUnsafeMutablePtr = UnsafeMutablePointer<AnyObject>.allocate(capacity: 1)
|
let objUnsafeMutablePtr = UnsafeMutablePointer<AnyObject>.allocate(capacity: 1)
|
||||||
var ctx : OpaquePointer! = OpaquePointer(objUnsafeMutablePtr)
|
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
|
// TODO: throw error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue