epoch and message serialization

This commit is contained in:
Richard Ramos 2022-12-02 09:17:21 -04:00
parent 31bad1963b
commit 30d41de747
4 changed files with 51 additions and 7 deletions

View File

@ -19,6 +19,7 @@
799E22A329390A44004476D7 /* CBuffer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 799E22A229390A44004476D7 /* CBuffer.swift */; };
799E22A529391F6A004476D7 /* MembershipKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 799E22A429391F6A004476D7 /* MembershipKey.swift */; };
799E22A7293A2865004476D7 /* RLN.swift in Sources */ = {isa = PBXBuildFile; fileRef = 799E22A6293A2865004476D7 /* RLN.swift */; };
799E22A9293A2FAF004476D7 /* Epoch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 799E22A8293A2FAF004476D7 /* Epoch.swift */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@ -36,6 +37,7 @@
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>"; };
799E22A6293A2865004476D7 /* RLN.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RLN.swift; sourceTree = "<group>"; };
799E22A8293A2FAF004476D7 /* Epoch.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Epoch.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -81,6 +83,7 @@
799E22A02938FB5D004476D7 /* zerokit.swift */,
799E22A229390A44004476D7 /* CBuffer.swift */,
799E22A6293A2865004476D7 /* RLN.swift */,
799E22A8293A2FAF004476D7 /* Epoch.swift */,
);
path = "zerokit-ios";
sourceTree = "<group>";
@ -190,6 +193,7 @@
799E22A329390A44004476D7 /* CBuffer.swift in Sources */,
791439152933B31F003246AC /* ContentView.swift in Sources */,
791439132933B31F003246AC /* zerokit_iosApp.swift in Sources */,
799E22A9293A2FAF004476D7 /* Epoch.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -0,0 +1,23 @@
import Foundation
typealias Epoch = [UInt8]
let DefaultEpochUnitSeconds = 10; // the rln-relay epoch length in seconds
func epochIntToBytes(epoch: Int) -> Epoch {
var epoch64 = UInt64(littleEndian: UInt64(epoch))
let epochBytes = withUnsafeBytes(of: &epoch64) { Array($0) }
let padLen = 32 - (epochBytes.count % 32)
if padLen > 0 {
return epochBytes + Array<UInt8>(repeating: 0, count: padLen)
}
return epochBytes
}
func dateToEpoch(timestamp: Date, epochUnitSeconds: Int = DefaultEpochUnitSeconds) -> Epoch {
let unixTimestamp = Int(timestamp.timeIntervalSince1970)
let epoch = unixTimestamp / epochUnitSeconds
return epochIntToBytes(epoch: epoch)
}

View File

@ -80,4 +80,16 @@ class RLN {
throw RLNError.couldNotObtainMerkleRoot
}
func serializeMsg(uint8Msg: [UInt8], memIndex: Int, epoch: Epoch, idKey: IDKey) -> [UInt8] {
// calculate message length
var msgLen64 = UInt64(littleEndian: UInt64(uint8Msg.count))
let msgLenBytes = withUnsafeBytes(of: &msgLen64) { Array($0) }
// Converting index to LE bytes
var memIndex64 = UInt64(littleEndian: UInt64(memIndex))
let memIndexBytes = withUnsafeBytes(of: &memIndex64) { Array($0) }
// [ id_key<32> | id_index<8> | epoch<32> | signal_len<8> | signal<var> ]
return idKey + memIndexBytes + epoch + msgLenBytes + uint8Msg
}
}

View File

@ -13,24 +13,23 @@ func readFile(filename: String, filetype: String) -> [UInt8] {
}
let DefaultEpochUnitSeconds = 10; // the rln-relay epoch length in seconds
func test() -> String {
// Reading resource files
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")
do {
// Instantiating RLN object
let rlnObj = try RLN(circomBytes: circom_bytes, zkeyBytes: zkey_bytes, vkBytes: vk_bytes)
// Generating a credential
let newCredential = try rlnObj.generateCredentials()
// Inserting a single credential
try rlnObj.insertMember(credential: newCredential)
// Inserting multiple credentials
var commitmentCollection = [IDCommitment]()
for _ in 1...3 {
let currCred = try rlnObj.generateCredentials()
@ -38,10 +37,16 @@ func test() -> String {
}
try rlnObj.insertMembers(commitments: commitmentCollection, index: 1)
// Obtaining the current merkle root
let merkleRoot = try rlnObj.getMerkleRoot()
// TODO: Calculate Epoch
// TODO: Serialize Message
// Date to epoch conversion
let epoch = dateToEpoch(timestamp: Date())
// Serialize Message
let msg: [UInt8] = [1,2,3,4,5,6,7,8,9,10]
let serializedMessage = rlnObj.serializeMsg(uint8Msg: msg, memIndex: 0, epoch: epoch, idKey: newCredential.idKey)
// TODO: generateRLNProof
// TODO: validateProof
} catch {