Skip to content

Commit

Permalink
feat(ios): Create separate screens for examples
Browse files Browse the repository at this point in the history
Keccak (Setup) and Keccak (Zkey) now both available

Also added mock RSA screen
  • Loading branch information
oskarth committed Nov 11, 2023
1 parent 7b0c7fd commit da155b4
Show file tree
Hide file tree
Showing 6 changed files with 382 additions and 176 deletions.
12 changes: 12 additions & 0 deletions mopro-ios/MoproKit/Example/MoproKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
CEA2D1302AB96A7A00F292D2 /* multiplier2.wasm in Resources */ = {isa = PBXBuildFile; fileRef = CEA2D12E2AB96A7A00F292D2 /* multiplier2.wasm */; };
CEA2D1322AB96AB500F292D2 /* multiplier2.r1cs in Resources */ = {isa = PBXBuildFile; fileRef = CEA2D1312AB96AB500F292D2 /* multiplier2.r1cs */; };
CEA2D1332AB96AB500F292D2 /* multiplier2.r1cs in Resources */ = {isa = PBXBuildFile; fileRef = CEA2D1312AB96AB500F292D2 /* multiplier2.r1cs */; };
CEB804502AFF81960063F091 /* KeccakSetupViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEB8044F2AFF81960063F091 /* KeccakSetupViewController.swift */; };
CEB804562AFF81AF0063F091 /* KeccakZkeyViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEB804552AFF81AF0063F091 /* KeccakZkeyViewController.swift */; };
CEB804582AFF81BF0063F091 /* RSAViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEB804572AFF81BF0063F091 /* RSAViewController.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -61,6 +64,9 @@
CE5A5C082AD43A860074539D /* keccak256_256_test.r1cs */ = {isa = PBXFileReference; lastKnownFileType = file; name = keccak256_256_test.r1cs; path = "../../../../../mopro-core/examples/circom/keccak256/target/keccak256_256_test.r1cs"; sourceTree = "<group>"; };
CEA2D12E2AB96A7A00F292D2 /* multiplier2.wasm */ = {isa = PBXFileReference; lastKnownFileType = file; name = multiplier2.wasm; path = "../../../../../mopro-core/examples/circom/multiplier2/target/multiplier2_js/multiplier2.wasm"; sourceTree = "<group>"; };
CEA2D1312AB96AB500F292D2 /* multiplier2.r1cs */ = {isa = PBXFileReference; lastKnownFileType = file; name = multiplier2.r1cs; path = "../../../../../mopro-core/examples/circom/multiplier2/target/multiplier2.r1cs"; sourceTree = "<group>"; };
CEB8044F2AFF81960063F091 /* KeccakSetupViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeccakSetupViewController.swift; sourceTree = "<group>"; };
CEB804552AFF81AF0063F091 /* KeccakZkeyViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeccakZkeyViewController.swift; sourceTree = "<group>"; };
CEB804572AFF81BF0063F091 /* RSAViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RSAViewController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -116,6 +122,9 @@
607FACD21AFB9204008FA782 /* Example for MoproKit */ = {
isa = PBXGroup;
children = (
CEB804572AFF81BF0063F091 /* RSAViewController.swift */,
CEB804552AFF81AF0063F091 /* KeccakZkeyViewController.swift */,
CEB8044F2AFF81960063F091 /* KeccakSetupViewController.swift */,
CEA2D1282AB9681E00F292D2 /* Resources */,
607FACD51AFB9204008FA782 /* AppDelegate.swift */,
607FACD71AFB9204008FA782 /* ViewController.swift */,
Expand Down Expand Up @@ -387,7 +396,10 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
CEB804582AFF81BF0063F091 /* RSAViewController.swift in Sources */,
CEB804562AFF81AF0063F091 /* KeccakZkeyViewController.swift in Sources */,
607FACD81AFB9204008FA782 /* ViewController.swift in Sources */,
CEB804502AFF81960063F091 /* KeccakSetupViewController.swift in Sources */,
607FACD61AFB9204008FA782 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
167 changes: 167 additions & 0 deletions mopro-ios/MoproKit/Example/MoproKit/KeccakSetupViewController.swift
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 mopro-ios/MoproKit/Example/MoproKit/KeccakZkeyViewController.swift
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 mopro-ios/MoproKit/Example/MoproKit/RSAViewController.swift
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
}
}
Loading

0 comments on commit da155b4

Please sign in to comment.