refactor: MembershipKey

This commit is contained in:
Richard Ramos 2022-12-01 14:58:18 -04:00
parent 00db19a252
commit 7e5a4479ce
4 changed files with 109 additions and 22 deletions

View File

@ -17,6 +17,7 @@
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 */; };
799E22A529391F6A004476D7 /* MembershipKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 799E22A429391F6A004476D7 /* MembershipKey.swift */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@ -32,6 +33,7 @@
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>"; };
799E22A229390A44004476D7 /* CBuffer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CBuffer.swift; sourceTree = "<group>"; };
799E22A429391F6A004476D7 /* MembershipKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MembershipKey.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -66,6 +68,7 @@
791439112933B31F003246AC /* zerokit-ios */ = {
isa = PBXGroup;
children = (
799E22A429391F6A004476D7 /* MembershipKey.swift */,
7961AA7829342D2C00B52AA9 /* verification_key.json */,
791439122933B31F003246AC /* zerokit_iosApp.swift */,
791439142933B31F003246AC /* ContentView.swift */,
@ -178,6 +181,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
799E22A529391F6A004476D7 /* MembershipKey.swift in Sources */,
799E22A12938FB5D004476D7 /* zerokit.swift in Sources */,
799E22A329390A44004476D7 /* CBuffer.swift in Sources */,
791439152933B31F003246AC /* ContentView.swift in Sources */,

View File

@ -3,8 +3,8 @@ import Foundation
class CBuffer {
let bufferPtr: UnsafeMutablePointer<Buffer>
init(input: inout [UInt8]) {
bufferPtr = CBuffer.toPointer(input: &input)
init(input: [UInt8]) {
bufferPtr = CBuffer.toPointer(input: input)
}
init() {
@ -17,26 +17,26 @@ class CBuffer {
}
// 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)
private static func toBuffer(data: [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) {
private static func sliceToPtr(data: [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)
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)
private static func toPointer(input: [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)

View File

@ -0,0 +1,37 @@
import Foundation
enum MembershipKeyError: Error {
case invalidInputLength
case invalidIdKey
case invalidIdCommitment
}
typealias IDKey = [UInt8]
typealias IDCommitment = [UInt8]
class MembershipKey {
let idKey: IDKey
let idCommitment: IDCommitment
init(idKey: [UInt8], idCommitment: [UInt8]) throws {
guard idKey.count == 32 else {
throw MembershipKeyError.invalidIdKey
}
guard idCommitment.count == 32 else {
throw MembershipKeyError.invalidIdCommitment
}
self.idKey = idKey
self.idCommitment = idCommitment
}
static func fromBytes(memKeys: [UInt8]) throws -> MembershipKey {
guard memKeys.count == 64 else {
throw MembershipKeyError.invalidInputLength
}
let idKey = memKeys[0..<32]
let idCommitment = memKeys[32...]
return try MembershipKey(idKey: Array(idKey), idCommitment: Array(idCommitment))
}
}

View File

@ -12,7 +12,39 @@ func readFile(filename: String, filetype: String) -> [UInt8] {
return [UInt8]()
}
func generateCredentials(ctx: OpaquePointer!) -> [UInt8] {
func insertMember(ctx: OpaquePointer!, credential: MembershipKey) throws {
let inputBuffer = CBuffer(input: credential.idCommitment)
if !set_next_leaf(ctx, inputBuffer.bufferPtr) {
throw RLNError.memberNotInserted
}
}
func insertMembers(ctx: OpaquePointer!, commitments: [IDCommitment], index: UInt) throws {
var cnt = UInt64(littleEndian: UInt64(commitments.count))
let countBytes = withUnsafeBytes(of: &cnt) { Array($0) }
var credentialsBytes = countBytes
commitments.forEach {
credentialsBytes += $0
}
let inputBuffer = CBuffer(input: credentialsBytes)
if !set_leaves_from(ctx, index, inputBuffer.bufferPtr) {
throw RLNError.memberNotInserted
}
}
let DefaultEpochUnitSeconds = 10; // the rln-relay epoch length in seconds
enum RLNError: Error {
case noCredentialsGenerated
case memberNotInserted
}
func generateCredentials(ctx: OpaquePointer!) throws -> MembershipKey {
// Generating credentials ======
let credentialBuffer = CBuffer()
if key_gen(ctx, credentialBuffer.bufferPtr) {
@ -21,14 +53,10 @@ func generateCredentials(ctx: OpaquePointer!) -> [UInt8] {
bufferPointer.withUnsafeBytes {
generatedKeyBytes.append(contentsOf: $0)
}
// TODO: generatedKeyBytes will contain 64 bytes now. Extract IDCommitment and IDKey into a specific type
return generatedKeyBytes
} else {
// TODO: throw error
return try MembershipKey.fromBytes(memKeys: generatedKeyBytes)
}
return [UInt8]()
throw RLNError.noCredentialsGenerated
}
func getMerkleRoot(ctx: OpaquePointer!) -> [UInt8] {
@ -48,13 +76,13 @@ func getMerkleRoot(ctx: OpaquePointer!) -> [UInt8] {
func newRLN() -> String {
// Reading resource files
var circom_bytes = readFile(filename: "rln", filetype: "wasm")
var zkey_bytes = readFile(filename: "rln_final", filetype: "zkey")
var vk_bytes = readFile(filename: "verification_key", filetype: "json")
let circom_bytes = readFile(filename: "rln", filetype: "wasm")
let zkey_bytes = readFile(filename: "rln_final", filetype: "zkey")
let vk_bytes = readFile(filename: "verification_key", filetype: "json")
let circom_buffer = CBuffer(input: &circom_bytes)
let zkey_buffer = CBuffer(input: &zkey_bytes)
let vk_buffer = CBuffer(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<AnyObject>.allocate(capacity: 1)
@ -63,7 +91,25 @@ func newRLN() -> String {
// TODO: throw error
}
let newCredential = generateCredentials(ctx: ctx)
do {
let newCredential = try generateCredentials(ctx: ctx)
try insertMember(ctx: ctx, credential: newCredential)
var commitmentCollection = [IDCommitment]()
for _ in 1...3 {
let currCred = try generateCredentials(ctx: ctx)
commitmentCollection.append(currCred.idCommitment)
}
try insertMembers(ctx: ctx, commitments: commitmentCollection, index: 1)
} catch {
print("Unexpected error: \(error).")
}
// TODO: Calculate Epoch
// TODO: Serialize Message
// TODO: generateRLNProof
// TODO: validateProof
let merkleRoot = getMerkleRoot(ctx: ctx)