Skip to content

Commit

Permalink
WIP: Delegate async comms
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarth committed Nov 11, 2023
1 parent 4997421 commit ce19c0a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 31 deletions.
60 changes: 43 additions & 17 deletions mopro-ios/MoproKit/Example/MoproKit/KeccakZkeyViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,59 @@ import UIKit
import MoproKit

class KeccakZkeyViewController: UIViewController {
weak var delegate: KeccakZkeyViewControllerDelegate?

var initButton = UIButton(type: .system)
var proveButton = UIButton(type: .system)
var verifyButton = UIButton(type: .system)

var textView = UITextView()

let moproCircom = MoproKit.MoproCircom()
//var setupResult: SetupResult?
var generatedProof: Data?
var publicInputs: Data?

override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white

if UserDefaults.standard.object(forKey: "timeToInitialize") != nil {
let timeTakenInitialize = UserDefaults.standard.double(forKey: "timeToInitialize")
textView.text += "Initializing arkzkey took \(timeTakenInitialize) seconds.\n"
}

// if UserDefaults.standard.object(forKey: "timeToInitialize") != nil {
// let timeTakenInitialize = UserDefaults.standard.double(forKey: "timeToInitialize")
// textView.text += "Initializing arkzkey took \(timeTakenInitialize) seconds.\n"
// }
setupUI()
}


func setupUI() {
initButton.setTitle("Init", for: .normal)
proveButton.setTitle("Prove", for: .normal)
verifyButton.setTitle("Verify", for: .normal)

// Uncomment once init separate
//proveButton.isEnabled = false
proveButton.isEnabled = true
verifyButton.isEnabled = false
textView.isEditable = false

self.title = "Keccak256 (zkey)"

// Setup actions for buttons
initButton.addTarget(self, action: #selector(runInitAction), for: .touchUpInside)
proveButton.addTarget(self, action: #selector(runProveAction), for: .touchUpInside)
verifyButton.addTarget(self, action: #selector(runVerifyAction), for: .touchUpInside)

let stackView = UIStackView(arrangedSubviews: [initButton, proveButton, verifyButton, textView])
stackView.axis = .vertical
stackView.spacing = 10
stackView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(stackView)

// Make text view visible
textView.heightAnchor.constraint(equalToConstant: 200).isActive = true

NSLayoutConstraint.activate([
stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
Expand All @@ -74,9 +77,9 @@ class KeccakZkeyViewController: UIViewController {
textView.text += "Initializing library\n"
// Record start time
let start = CFAbsoluteTimeGetCurrent()

try initializeMopro()

// Record end time and compute duration
let end = CFAbsoluteTimeGetCurrent()
let timeTaken = end - start
Expand All @@ -90,8 +93,14 @@ class KeccakZkeyViewController: UIViewController {
}
}

// func updateInitializationTime(_ timeTaken: Double) {
// textView.text += "Initializing arkzkey already done, took \(timeTaken) seconds.\n"
// }

func updateInitializationTime(_ timeTaken: Double) {
textView.text += "Initializing arkzkey took \(timeTaken) seconds.\n"
DispatchQueue.main.async {
self.textView.text += "Initializing arkzkey already done, took \(timeTaken) seconds.\n"
}
}

@objc func runProveAction() {
Expand Down Expand Up @@ -138,8 +147,25 @@ class KeccakZkeyViewController: UIViewController {
print("Unexpected error: \(error)")
}
}

@objc func runVerifyAction() {
// Logic for verify
}
}

// // Conforming to the delegate protocol
// extension KeccakZkeyViewController: KeccakZkeyViewControllerDelegate {
// func updateInitializationTime(_ timeTaken: Double) {
// DispatchQueue.main.async {
// self.textView.text += "Initializing arkzkey already done, took \(timeTaken) seconds.\n"
// }
// }
// }

protocol KeccakZkeyViewControllerDelegate: AnyObject {
func didUpdateInitializationTime(_ timeTaken: Double)
}

// protocol KeccakZkeyViewControllerDelegate: AnyObject {
// func didUpdateInitializationMessage(_ message: String)
// }
52 changes: 38 additions & 14 deletions mopro-ios/MoproKit/Example/MoproKit/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

import UIKit
import MoproKit
//import KeccakZkeyViewController


// Main ViewController
class ViewController: UIViewController {
var keccakZkeyViewController: KeccakZkeyViewController?

let keccakSetupButton = UIButton(type: .system)
let keccakZkeyButton = UIButton(type: .system)
Expand All @@ -22,19 +24,29 @@ class ViewController: UIViewController {

// Initialize Mopro as early as possible
// NOTE: This is for Keccak (zkey) specifically
// Move the initialization to a background thread
DispatchQueue.global(qos: .userInitiated).async {
initializeMoproAndMeasureTime()

// Maybe black nice, need more style tweaks though
view.backgroundColor = .white
setupMainUI()
}

// Move the initialization to a background thread
private func initializeMoproAndMeasureTime() {
let start = CFAbsoluteTimeGetCurrent()

DispatchQueue.global(qos: .background).async {
do {
let start = CFAbsoluteTimeGetCurrent()
try initializeMopro()
let end = CFAbsoluteTimeGetCurrent()
let timeTaken = end - start

UserDefaults.standard.set(timeTaken, forKey: "timeToInitialize")
let timeTaken = CFAbsoluteTimeGetCurrent() - start

DispatchQueue.main.async {
// If you need to update the UI based on the result
// Do it here on the main thread
// Assuming you have a reference to KeccakZkeyViewController
let keccakZkeyVC = KeccakZkeyViewController()
keccakZkeyVC.delegate = self
self.present(keccakZkeyVC, animated: true, completion: nil)

keccakZkeyVC.delegate?.didUpdateInitializationTime(timeTaken)
}
} catch let error as MoproError {
DispatchQueue.main.async {
Expand All @@ -43,15 +55,11 @@ class ViewController: UIViewController {
}
} catch {
DispatchQueue.main.async {
print("Unexpected error: \(error)")
print("Unexpected error: \(error)")
// Optionally update the UI to show error
}
}
}

// Maybe black nice, need more style tweaks though
view.backgroundColor = .white
setupMainUI()
}

func setupMainUI() {
Expand Down Expand Up @@ -85,6 +93,7 @@ class ViewController: UIViewController {

@objc func openKeccakZkey() {
let keccakZkeyVC = KeccakZkeyViewController()
keccakZkeyVC.delegate = self
navigationController?.pushViewController(keccakZkeyVC, animated: true)
}

Expand All @@ -94,6 +103,21 @@ class ViewController: UIViewController {
}
}

extension ViewController: KeccakZkeyViewControllerDelegate {
func didUpdateInitializationTime(_ timeTaken: Double) {
// This will call the updateInitializationTime method of KeccakZkeyViewController
keccakZkeyViewController?.updateInitializationTime(timeTaken)
}
}

// extension ViewController: KeccakZkeyViewControllerDelegate {
// func didUpdateInitializationTime(_ timeTaken: Double) {
// DispatchQueue.main.async {
// self.textView.text += "Initializing arkzkey already done, took \(timeTaken) seconds.\n"
// }
// }
// }

// // Make buttons bigger
// proveButton.contentEdgeInsets = UIEdgeInsets(top: 12, left: 16, bottom: 12, right: 16)
// verifyButton.contentEdgeInsets = UIEdgeInsets(top: 12, left: 16, bottom: 12, right: 16)
Expand Down

0 comments on commit ce19c0a

Please sign in to comment.