-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ios): Create separate screens for examples
Keccak (Setup) and Keccak (Zkey) now both available Also added mock RSA screen
- Loading branch information
Showing
6 changed files
with
382 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
mopro-ios/MoproKit/Example/MoproKit/KeccakSetupViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
// | ||
// ViewController.swift | ||
// MoproKit | ||
// | ||
// Created by 1552237 on 09/16/2023. | ||
// Copyright (c) 2023 1552237. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import MoproKit | ||
|
||
class KeccakSetupViewController: UIViewController { | ||
|
||
var setupButton = 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 | ||
setupUI() | ||
} | ||
|
||
func setupUI() { | ||
setupButton.setTitle("Setup", for: .normal) | ||
proveButton.setTitle("Prove", for: .normal) | ||
verifyButton.setTitle("Verify", for: .normal) | ||
|
||
textView.isEditable = false | ||
|
||
self.title = "Keccak256 (setup)" | ||
//view.backgroundColor = .black | ||
|
||
// Setup actions for buttons | ||
setupButton.addTarget(self, action: #selector(runSetupAction), for: .touchUpInside) | ||
proveButton.addTarget(self, action: #selector(runProveAction), for: .touchUpInside) | ||
verifyButton.addTarget(self, action: #selector(runVerifyAction), for: .touchUpInside) | ||
|
||
let stackView = UIStackView(arrangedSubviews: [setupButton, 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), | ||
stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20), | ||
stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20) | ||
]) | ||
} | ||
|
||
@objc func runSetupAction() { | ||
// Logic for setup | ||
if let wasmPath = Bundle.main.path(forResource: "keccak256_256_test", ofType: "wasm"), | ||
let r1csPath = Bundle.main.path(forResource: "keccak256_256_test", ofType: "r1cs") { | ||
|
||
// Multiplier example | ||
// if let wasmPath = Bundle.main.path(forResource: "multiplier2", ofType: "wasm"), | ||
// let r1csPath = Bundle.main.path(forResource: "multiplier2", ofType: "r1cs") { | ||
|
||
do { | ||
setupResult = try moproCircom.setup(wasmPath: wasmPath, r1csPath: r1csPath) | ||
proveButton.isEnabled = true // Enable the Prove button upon successful setup | ||
} catch let error as MoproError { | ||
print("MoproError: \(error)") | ||
} catch { | ||
print("Unexpected error: \(error)") | ||
} | ||
} else { | ||
print("Error getting paths for resources") | ||
} | ||
} | ||
|
||
@objc func runProveAction() { | ||
// Logic for prove | ||
guard let setupResult = setupResult else { | ||
print("Setup is not completed yet.") | ||
return | ||
} | ||
do { | ||
// Prepare inputs | ||
let inputVec: [UInt8] = [ | ||
116, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, | ||
] | ||
let bits = bytesToBits(bytes: inputVec) | ||
var inputs = [String: [String]]() | ||
inputs["in"] = bits | ||
|
||
// Multiplier example | ||
// var inputs = [String: [String]]() | ||
// let a = 3 | ||
// let b = 5 | ||
// inputs["a"] = [String(a)] | ||
// inputs["b"] = [String(b)] | ||
|
||
// Record start time | ||
let start = CFAbsoluteTimeGetCurrent() | ||
|
||
// Generate Proof | ||
let generateProofResult = try moproCircom.generateProof(circuitInputs: inputs) | ||
assert(!generateProofResult.proof.isEmpty, "Proof should not be empty") | ||
|
||
// Record end time and compute duration | ||
let end = CFAbsoluteTimeGetCurrent() | ||
let timeTaken = end - start | ||
|
||
// Store the generated proof and public inputs for later verification | ||
generatedProof = generateProofResult.proof | ||
publicInputs = generateProofResult.inputs | ||
|
||
textView.text += "Proof generation took \(timeTaken) seconds.\n" | ||
verifyButton.isEnabled = true // Enable the Verify button once proof has been generated | ||
} catch let error as MoproError { | ||
print("MoproError: \(error)") | ||
} catch { | ||
print("Unexpected error: \(error)") | ||
} | ||
} | ||
|
||
@objc func runVerifyAction() { | ||
// Logic for verify | ||
guard let setupResult = setupResult, | ||
let proof = generatedProof, | ||
let publicInputs = publicInputs else { | ||
print("Setup is not completed or proof has not been generated yet.") | ||
return | ||
} | ||
do { | ||
// Verify Proof | ||
let isValid = try moproCircom.verifyProof(proof: proof, publicInput: publicInputs) | ||
assert(isValid, "Proof verification should succeed") | ||
|
||
textView.text += "Proof verification succeeded.\n" | ||
} catch let error as MoproError { | ||
print("MoproError: \(error)") | ||
} catch { | ||
print("Unexpected error: \(error)") | ||
} | ||
} | ||
|
||
override func didReceiveMemoryWarning() { | ||
super.didReceiveMemoryWarning() | ||
// Dispose of any resources that can be recreated. | ||
} | ||
} | ||
|
||
func bytesToBits(bytes: [UInt8]) -> [String] { | ||
var bits = [String]() | ||
for byte in bytes { | ||
for j in 0..<8 { | ||
let bit = (byte >> j) & 1 | ||
bits.append(String(bit)) | ||
} | ||
} | ||
return bits | ||
} |
119 changes: 119 additions & 0 deletions
119
mopro-ios/MoproKit/Example/MoproKit/KeccakZkeyViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// | ||
// ViewController.swift | ||
// MoproKit | ||
// | ||
// Created by 1552237 on 09/16/2023. | ||
// Copyright (c) 2023 1552237. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import MoproKit | ||
|
||
class KeccakZkeyViewController: UIViewController { | ||
|
||
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 | ||
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), | ||
stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20), | ||
stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20) | ||
]) | ||
} | ||
|
||
@objc func runInitAction() { | ||
// Logic for init | ||
// TODO: Add this and call init for zkey | ||
textView.text += "NYI: Init\n" | ||
} | ||
|
||
@objc func runProveAction() { | ||
// Logic for prove (generate_proof2) | ||
do { | ||
// Prepare inputs | ||
let inputVec: [UInt8] = [ | ||
116, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, | ||
] | ||
let bits = bytesToBits(bytes: inputVec) | ||
var inputs = [String: [String]]() | ||
inputs["in"] = bits | ||
|
||
// Expected outputs | ||
let outputVec: [UInt8] = [ | ||
37, 17, 98, 135, 161, 178, 88, 97, 125, 150, 143, 65, 228, 211, 170, 133, 153, 9, 88, | ||
212, 4, 212, 175, 238, 249, 210, 214, 116, 170, 85, 45, 21, | ||
] | ||
let outputBits: [String] = bytesToBits(bytes: outputVec) | ||
// let expectedOutput: [UInt8] = serializeOutputs(outputBits) | ||
|
||
// Record start time | ||
let start = CFAbsoluteTimeGetCurrent() | ||
|
||
// Generate Proof | ||
let generateProofResult = try generateProof2(circuitInputs: inputs) | ||
assert(!generateProofResult.proof.isEmpty, "Proof should not be empty") | ||
//assert(Data(expectedOutput) == generateProofResult.inputs, "Circuit outputs mismatch the expected outputs") | ||
|
||
// Record end time and compute duration | ||
let end = CFAbsoluteTimeGetCurrent() | ||
let timeTaken = end - start | ||
|
||
// Store the generated proof and public inputs for later verification | ||
generatedProof = generateProofResult.proof | ||
publicInputs = generateProofResult.inputs | ||
|
||
textView.text += "Proof generation took \(timeTaken) seconds.\n" | ||
verifyButton.isEnabled = true // Enable the Verify button once proof has been generated | ||
} catch let error as MoproError { | ||
print("MoproError: \(error)") | ||
} catch { | ||
print("Unexpected error: \(error)") | ||
} | ||
} | ||
|
||
@objc func runVerifyAction() { | ||
// Logic for verify | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
mopro-ios/MoproKit/Example/MoproKit/RSAViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// ViewController.swift | ||
// MoproKit | ||
// | ||
// Created by 1552237 on 09/16/2023. | ||
// Copyright (c) 2023 1552237. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import MoproKit | ||
|
||
class RSAViewController: UIViewController { | ||
|
||
var proveButton = UIButton(type: .system) | ||
var verifyButton = UIButton(type: .system) | ||
|
||
func setupUI() { | ||
proveButton.setTitle("Prove", for: .normal) | ||
verifyButton.setTitle("Verify", for: .normal) | ||
|
||
proveButton.isEnabled = false | ||
verifyButton.isEnabled = false | ||
|
||
let stackView = UIStackView(arrangedSubviews: [proveButton, verifyButton]) | ||
stackView.axis = .vertical | ||
stackView.spacing = 10 | ||
stackView.translatesAutoresizingMaskIntoConstraints = false | ||
view.addSubview(stackView) | ||
|
||
NSLayoutConstraint.activate([ | ||
stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor), | ||
stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor), | ||
stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20), | ||
stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20) | ||
]) | ||
} | ||
|
||
@objc func runProveAction() { | ||
// Logic for prove | ||
} | ||
|
||
@objc func runVerifyAction() { | ||
// Logic for verify | ||
} | ||
} |
Oops, something went wrong.