Skip to content

Commit

Permalink
test: update bindings tests (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
vivianjeng authored Jul 9, 2024
1 parent 5380d75 commit 6a699e5
Show file tree
Hide file tree
Showing 16 changed files with 248 additions and 1,019 deletions.
4 changes: 4 additions & 0 deletions test-e2e/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use mopro_ffi::{app, WtnsFn};

rust_witness::witness!(multiplier2);
rust_witness::witness!(multiplier2bls);
rust_witness::witness!(keccak256256test);
rust_witness::witness!(hashbenchbls);

app!();

Expand All @@ -11,6 +13,8 @@ fn zkey_witness_map(name: &str) -> Result<WtnsFn, MoproError> {
match name {
"multiplier2_final.zkey" => Ok(multiplier2_witness),
"keccak256_256_test_final.zkey" => Ok(keccak256256test_witness),
"hashbench_bls_final.zkey" => Ok(hashbenchbls_witness),
"multiplier2_bls_final.zkey" => Ok(multiplier2bls_witness),
_ => Err(MoproError::CircomError("Unknown circuit name".to_string())),
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import uniffi.mopro.*

try {
initializeMopro()

try {
var zkeyPath = "../test-vectors/circom/keccak256_256_test_final.zkey"

val inputs = mutableMapOf<String, List<String>>()
inputs["in"] =
listOf(
Expand Down Expand Up @@ -264,9 +265,9 @@ try {
"0"
)

var generateProofResult = generateProofStatic(inputs)
var generateProofResult = generateCircomProof(zkeyPath, inputs)
assert(generateProofResult.proof.size > 0) { "Proof is empty" }
var isValid = verifyProofStatic(generateProofResult.proof, generateProofResult.inputs)
var isValid = verifyCircomProof(zkeyPath, generateProofResult.proof, generateProofResult.inputs)
assert(isValid) { "Proof is invalid" }
} catch (e: Exception) {
println(e)
Expand Down
78 changes: 78 additions & 0 deletions test-e2e/tests/bindings/test_circom_keccak.swift
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.
69 changes: 69 additions & 0 deletions test-e2e/tests/bindings/test_circom_multiplier2.swift
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
}
23 changes: 23 additions & 0 deletions test-e2e/tests/bindings/test_circom_multiplier2_bls.kts
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
}
63 changes: 63 additions & 0 deletions test-e2e/tests/bindings/test_circom_multiplier2_bls.swift
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
}
66 changes: 0 additions & 66 deletions test-e2e/tests/bindings/test_mopro.swift

This file was deleted.

42 changes: 0 additions & 42 deletions test-e2e/tests/bindings/test_mopro_gpu_benchmarks.swift

This file was deleted.

Loading

0 comments on commit 6a699e5

Please sign in to comment.