-
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.
- Loading branch information
1 parent
5380d75
commit 6a699e5
Showing
16 changed files
with
248 additions
and
1,019 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
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
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,78 @@ | ||
import Foundation | ||
import mopro | ||
|
||
// Helper function to convert bytes to bits | ||
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 | ||
} | ||
|
||
func serializeOutputs(_ stringArray: [String]) -> [UInt8] { | ||
var bytesArray: [UInt8] = [] | ||
let length = stringArray.count | ||
var littleEndianLength = length.littleEndian | ||
let targetLength = 32 | ||
withUnsafeBytes(of: &littleEndianLength) { | ||
bytesArray.append(contentsOf: $0) | ||
} | ||
for value in stringArray { | ||
// TODO: should handle 254-bit input | ||
var littleEndian = Int32(value)!.littleEndian | ||
var byteLength = 0 | ||
withUnsafeBytes(of: &littleEndian) { | ||
bytesArray.append(contentsOf: $0) | ||
byteLength = byteLength + $0.count | ||
} | ||
if byteLength < targetLength { | ||
let paddingCount = targetLength - byteLength | ||
let paddingArray = [UInt8](repeating: 0, count: paddingCount) | ||
bytesArray.append(contentsOf: paddingArray) | ||
} | ||
} | ||
return bytesArray | ||
} | ||
|
||
do { | ||
let zkeyPath = "../../../test-vectors/circom/keccak256_256_test_final.zkey" | ||
|
||
// 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) | ||
|
||
// Generate Proof | ||
let generateProofResult = try generateCircomProof(zkeyPath: zkeyPath, circuitInputs: inputs) | ||
assert(!generateProofResult.proof.isEmpty, "Proof should not be empty") | ||
|
||
// Verify Proof | ||
assert( | ||
Data(expectedOutput) == generateProofResult.inputs, | ||
"Circuit outputs mismatch the expected outputs") | ||
|
||
let isValid = try verifyCircomProof( | ||
zkeyPath: zkeyPath, proof: generateProofResult.proof, publicInput: generateProofResult.inputs) | ||
assert(isValid, "Proof verification should succeed") | ||
|
||
} catch let error as MoproError { | ||
print("MoproError: \(error)") | ||
} catch { | ||
print("Unexpected error: \(error)") | ||
} |
File renamed without changes.
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,69 @@ | ||
import Foundation | ||
import mopro | ||
|
||
func serializeOutputs(_ stringArray: [String]) -> [UInt8] { | ||
var bytesArray: [UInt8] = [] | ||
let length = stringArray.count | ||
var littleEndianLength = length.littleEndian | ||
let targetLength = 32 | ||
withUnsafeBytes(of: &littleEndianLength) { | ||
bytesArray.append(contentsOf: $0) | ||
} | ||
for value in stringArray { | ||
// TODO: should handle 254-bit input | ||
var littleEndian = Int32(value)!.littleEndian | ||
var byteLength = 0 | ||
withUnsafeBytes(of: &littleEndian) { | ||
bytesArray.append(contentsOf: $0) | ||
byteLength = byteLength + $0.count | ||
} | ||
if byteLength < targetLength { | ||
let paddingCount = targetLength - byteLength | ||
let paddingArray = [UInt8](repeating: 0, count: paddingCount) | ||
bytesArray.append(contentsOf: paddingArray) | ||
} | ||
} | ||
return bytesArray | ||
} | ||
|
||
do { | ||
let zkeyPath = "../../../test-vectors/circom/multiplier2_final.zkey" | ||
|
||
// Prepare inputs | ||
var inputs = [String: [String]]() | ||
let a = 3 | ||
let b = 5 | ||
let c = a * b | ||
inputs["a"] = [String(a)] | ||
inputs["b"] = [String(b)] | ||
|
||
// Expected outputs | ||
let outputs: [String] = [String(c), String(a)] | ||
let expectedOutput: [UInt8] = serializeOutputs(outputs) | ||
|
||
// Generate Proof | ||
let generateProofResult = try generateCircomProof(zkeyPath: zkeyPath, circuitInputs: inputs) | ||
assert(!generateProofResult.proof.isEmpty, "Proof should not be empty") | ||
|
||
// Verify Proof | ||
assert( | ||
Data(expectedOutput) == generateProofResult.inputs, | ||
"Circuit outputs mismatch the expected outputs") | ||
|
||
let isValid = try verifyCircomProof( | ||
zkeyPath: zkeyPath, proof: generateProofResult.proof, publicInput: generateProofResult.inputs) | ||
assert(isValid, "Proof verification should succeed") | ||
|
||
// Convert proof to Ethereum compatible proof | ||
let convertProofResult = toEthereumProof(proof: generateProofResult.proof) | ||
let convertInputsResult = toEthereumInputs(inputs: generateProofResult.inputs) | ||
assert(convertProofResult.a.x.count > 0, "Proof should not be empty") | ||
assert(convertInputsResult.count > 0, "Inputs should not be empty") | ||
|
||
} catch let error as MoproError { | ||
print("MoproError: \(error)") | ||
throw error | ||
} catch { | ||
print("Unexpected error: \(error)") | ||
throw error | ||
} |
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,23 @@ | ||
import uniffi.mopro.* | ||
|
||
try { | ||
var zkeyPath = "../test-vectors/circom/multiplier2_bls_final.zkey" | ||
|
||
// Prepare inputs | ||
val inputs = mutableMapOf<String, List<String>>() | ||
inputs["a"] = listOf("3") | ||
inputs["b"] = listOf("5") | ||
|
||
// Generate proof | ||
var generateProofResult = generateCircomProof(zkeyPath, inputs) | ||
assert(generateProofResult.proof.size > 0) { "Proof is empty" } | ||
|
||
// Verify proof | ||
var isValid = verifyCircomProof(zkeyPath, generateProofResult.proof, generateProofResult.inputs) | ||
assert(isValid) { "Proof is invalid" } | ||
|
||
|
||
} catch (e: Exception) { | ||
println(e) | ||
throw e | ||
} |
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,63 @@ | ||
import Foundation | ||
import mopro | ||
|
||
func serializeOutputs(_ stringArray: [String]) -> [UInt8] { | ||
var bytesArray: [UInt8] = [] | ||
let length = stringArray.count | ||
var littleEndianLength = length.littleEndian | ||
let targetLength = 32 | ||
withUnsafeBytes(of: &littleEndianLength) { | ||
bytesArray.append(contentsOf: $0) | ||
} | ||
for value in stringArray { | ||
// TODO: should handle 254-bit input | ||
var littleEndian = Int32(value)!.littleEndian | ||
var byteLength = 0 | ||
withUnsafeBytes(of: &littleEndian) { | ||
bytesArray.append(contentsOf: $0) | ||
byteLength = byteLength + $0.count | ||
} | ||
if byteLength < targetLength { | ||
let paddingCount = targetLength - byteLength | ||
let paddingArray = [UInt8](repeating: 0, count: paddingCount) | ||
bytesArray.append(contentsOf: paddingArray) | ||
} | ||
} | ||
return bytesArray | ||
} | ||
|
||
do { | ||
let zkeyPath = "../../../test-vectors/circom/multiplier2_bls_final.zkey" | ||
|
||
// Prepare inputs | ||
var inputs = [String: [String]]() | ||
let a = 3 | ||
let b = 5 | ||
let c = a * b | ||
inputs["a"] = [String(a)] | ||
inputs["b"] = [String(b)] | ||
|
||
// Expected outputs | ||
let outputs: [String] = [String(c)] | ||
let expectedOutput: [UInt8] = serializeOutputs(outputs) | ||
|
||
// Generate Proof | ||
let generateProofResult = try generateCircomProof(zkeyPath: zkeyPath, circuitInputs: inputs) | ||
assert(!generateProofResult.proof.isEmpty, "Proof should not be empty") | ||
|
||
// Verify Proof | ||
assert( | ||
Data(expectedOutput) == generateProofResult.inputs, | ||
"Circuit outputs mismatch the expected outputs") | ||
|
||
let isValid = try verifyCircomProof( | ||
zkeyPath: zkeyPath, proof: generateProofResult.proof, publicInput: generateProofResult.inputs) | ||
assert(isValid, "Proof verification should succeed") | ||
|
||
} catch let error as MoproError { | ||
print("MoproError: \(error)") | ||
throw error | ||
} catch { | ||
print("Unexpected error: \(error)") | ||
throw error | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.