Skip to content

Commit

Permalink
test(ffi): add .kts tests (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
vivianjeng authored Dec 7, 2023
1 parent 6bb9bb6 commit 107b973
Show file tree
Hide file tree
Showing 6 changed files with 305 additions and 38 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ jobs:
- name: Run core tests
run: cd mopro-core && cargo test -- --nocapture
- name: Run ffi tests
run: cd mopro-ffi/ && cargo test -- --nocapture
run: |
cd mopro-ffi/
curl -L https://repo1.maven.org/maven2/net/java/dev/jna/jna/5.13.0/jna-5.13.0.jar -o target/jna-5.13.0.jar
CLASSPATH=target/jna-5.13.0.jar cargo test -- --nocapture
- name: Run ios tests
run: |
cd mopro-ios/MoproKit/Example
Expand Down
6 changes: 3 additions & 3 deletions mopro-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ wasmer = { git = "https://github.com/oskarth/wasmer.git", rev = "09c7070" }

[dependencies]
mopro-core = { path = "../mopro-core" }
uniffi = { version = "0.24", features = ["cli"] }
uniffi = { version = "0.25", features = ["cli"] }
serde = { version = "1", features = ["derive"] }
bincode = "1"
ark-serialize = { version = "=0.4.1", features = ["derive"] }
Expand All @@ -34,8 +34,8 @@ color-eyre = "=0.6.2"
criterion = "=0.3.6"

[build-dependencies]
uniffi = { version = "0.24", features = ["build"] }
uniffi = { version = "0.25", features = ["build"] }

[dev-dependencies]
uniffi = { version = "0.24", features = ["bindgen-tests"] }
uniffi = { version = "0.25", features = ["bindgen-tests"] }
ark-bn254 = { version = "=0.4.0" }
10 changes: 10 additions & 0 deletions mopro-ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ TBD.
1. Ensure you have Rust installed
2. Add platform targets `rustup target add x86_64-apple-ios aarch64-apple-ios aarch64-apple-ios-sim`
3. Install `uniffi-bindgen` locally with `cargo install --bin uniffi-bindgen --path .`
4. In order to locally run the bindings tests, you will need
* Kotlin:
* `kotlinc`, the [Kotlin command-line compiler](https://kotlinlang.org/docs/command-line.html).
* `ktlint`, the [Kotlin linter used to format the generated bindings](https://ktlint.github.io/).
* The [Java Native Access](https://github.com/java-native-access/jna#download) JAR downloaded and its path
added to your `$CLASSPATH` environment variable.
* Swift:
* `swift` and `swiftc`, the [Swift command-line tools](https://swift.org/download/).
* The Swift `Foundation` package.

### Platforms supported

Currently iOS is the main target, but Android will soon follow. PRs welcome.
Expand Down
253 changes: 253 additions & 0 deletions mopro-ffi/tests/bindings/test_mopro_keccak2.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
import uniffi.mopro.*;

try {
initializeMopro()

val inputs = mutableMapOf<String, List<String>>()
inputs["in"] =
listOf(
"0",
"0",
"1",
"0",
"1",
"1",
"1",
"0",
"1",
"0",
"1",
"0",
"0",
"1",
"1",
"0",
"1",
"1",
"0",
"0",
"1",
"1",
"1",
"0",
"0",
"0",
"1",
"0",
"1",
"1",
"1",
"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",
"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",
"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",
"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",
"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",
"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",
"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",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0"
)

var generateProofResult = generateProof2(inputs)
assert(generateProofResult.proof.size > 0) { "Proof is empty"}
var isValid = verifyProof2(generateProofResult.proof, generateProofResult.inputs)
// FIXME: assert(isValid) { "Proof is invalid"}
println("Proof is invalid: $isValid")
} catch (e: Exception) {
println(e);
}
68 changes: 34 additions & 34 deletions mopro-ffi/tests/bindings/test_mopro_keccak2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,30 @@ func bytesToBits(bytes: [UInt8]) -> [String] {
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
// }
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 {
// // Setup
Expand All @@ -59,25 +59,25 @@ do {
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)
// 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 generateProof2(circuitInputs: inputs)
// let generateProofResult = try moproCircom.generateProof(circuitInputs: inputs)
// assert(!generateProofResult.proof.isEmpty, "Proof should not be empty")
assert(!generateProofResult.proof.isEmpty, "Proof should not be empty")

// // Verify Proof
// assert(Data(expectedOutput) == generateProofResult.inputs, "Circuit outputs mismatch the expected outputs")
assert(Data(expectedOutput) == generateProofResult.inputs, "Circuit outputs mismatch the expected outputs")

let isValid = try verifyProof2(
proof: generateProofResult.proof, publicInput: generateProofResult.inputs)
// assert(isValid, "Proof verification should succeed")
assert(isValid, "Proof verification should succeed")

} catch let error as MoproError {
print("MoproError: \(error)")
Expand Down
Loading

0 comments on commit 107b973

Please sign in to comment.