diff --git a/.gitignore b/.gitignore index d792139..ca18e8c 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,6 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb -*.run \ No newline at end of file +*.run +*.a +*.so \ No newline at end of file diff --git a/README.md b/README.md index bc0e6a8..431f4f7 100644 --- a/README.md +++ b/README.md @@ -13,13 +13,13 @@ Bridge the gap between Swift and the `noir_rs` library, offering Swift developer - ๐ŸŒ‰ Effortless interfacing between Swift and Rust. - ๐Ÿ”’ Secure zkSNARK proof generation and verification. -## ๐Ÿ“ฆ Build +## ๐Ÿ“ฆ Rust Build ```bash cargo build ``` -## ๐Ÿงช Testing +## ๐Ÿงช Rust Testing Ensure the reliability and robustness of your bridge with the provided tests: @@ -27,6 +27,23 @@ Ensure the reliability and robustness of your bridge with the provided tests: cargo test ``` +## ๐Ÿ“ฆ Swift Build + +```bash +# copy barretenberg lib to the swift directory +cp target/debug/build/barretenberg-/out/lib/libbarretenberg.a swift/ +# copy noir_swift lib to the swift directory +cp target/debug/libnoir_swift.a swift/ + +cd swift + +# compile swift project +./build + +# run the program +./main.run +``` + ## ๐ŸŽฏ Quick Usage We provide two main functions for the bridge: @@ -35,8 +52,13 @@ We provide two main functions for the bridge: 2. **verify_swift**: Verifies a given zkSNARK proof against a circuit and its verification key. ```rust -fn prove_swift(circuit_bytecode: String, initial_witness_vec_raw: Vec) -> Result -fn verify_swift(circuit_bytecode: String, proof: Proof) -> Result +pub struct Proof { + pub proof: Vec, + pub verification_key: Vec, +} + +fn prove_swift(circuit_bytecode: String, initial_witness_vec_raw: Vec) -> Option; +fn verify_swift(circuit_bytecode: String, proof: Proof) -> Option; ``` ## ๐Ÿ” Example diff --git a/generated/noir_swift/noir_swift.h b/generated/noir_swift/noir_swift.h index 5755397..8f5f5f0 100644 --- a/generated/noir_swift/noir_swift.h +++ b/generated/noir_swift/noir_swift.h @@ -1,6 +1,5 @@ // File automatically generated by swift-bridge. #include -#include typedef struct Proof Proof; void __swift_bridge__$Proof$_free(void* self); @@ -14,6 +13,6 @@ uintptr_t __swift_bridge__$Vec_Proof$len(void* vec_ptr); void* __swift_bridge__$Vec_Proof$as_ptr(void* vec_ptr); void* __swift_bridge__$prove_swift(void* circuit_bytecode, void* initial_witness_vec_raw); -bool __swift_bridge__$verify_swift(void* circuit_bytecode, void* proof); +struct __private__OptionBool __swift_bridge__$verify_swift(void* circuit_bytecode, void* proof); diff --git a/generated/noir_swift/noir_swift.swift b/generated/noir_swift/noir_swift.swift index c79d12e..f200bde 100644 --- a/generated/noir_swift/noir_swift.swift +++ b/generated/noir_swift/noir_swift.swift @@ -1,8 +1,8 @@ -public func prove_swift(_ circuit_bytecode: GenericIntoRustString, _ initial_witness_vec_raw: RustVec) -> Proof { - Proof(ptr: __swift_bridge__$prove_swift({ let rustString = circuit_bytecode.intoRustString(); rustString.isOwned = false; return rustString.ptr }(), { let val = initial_witness_vec_raw; val.isOwned = false; return val.ptr }())) +public func prove_swift(_ circuit_bytecode: GenericIntoRustString, _ initial_witness_vec_raw: RustVec) -> Optional { + { let val = __swift_bridge__$prove_swift({ let rustString = circuit_bytecode.intoRustString(); rustString.isOwned = false; return rustString.ptr }(), { let val = initial_witness_vec_raw; val.isOwned = false; return val.ptr }()); if val != nil { return Proof(ptr: val!) } else { return nil } }() } -public func verify_swift(_ circuit_bytecode: GenericIntoRustString, _ proof: Proof) -> Bool { - __swift_bridge__$verify_swift({ let rustString = circuit_bytecode.intoRustString(); rustString.isOwned = false; return rustString.ptr }(), {proof.isOwned = false; return proof.ptr;}()) +public func verify_swift(_ circuit_bytecode: GenericIntoRustString, _ proof: Proof) -> Optional { + __swift_bridge__$verify_swift({ let rustString = circuit_bytecode.intoRustString(); rustString.isOwned = false; return rustString.ptr }(), {proof.isOwned = false; return proof.ptr;}()).intoSwiftRepr() } public class Proof: ProofRefMut { diff --git a/src/lib.rs b/src/lib.rs index cbe4049..fc3f61f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,8 +7,11 @@ use noir_rs::{ mod ffi { extern "Rust" { type Proof; - fn prove_swift(circuit_bytecode: String, initial_witness_vec_raw: Vec) -> Proof; - fn verify_swift(circuit_bytecode: String, proof: Proof) -> bool; + fn prove_swift( + circuit_bytecode: String, + initial_witness_vec_raw: Vec, + ) -> Option; + fn verify_swift(circuit_bytecode: String, proof: Proof) -> Option; } } @@ -17,7 +20,7 @@ pub struct Proof { pub verification_key: Vec, } -pub fn prove_swift(circuit_bytecode: String, initial_witness_vec_raw: Vec) -> Proof { +pub fn prove_swift(circuit_bytecode: String, initial_witness_vec_raw: Vec) -> Option { let initial_witness_vec: Vec = initial_witness_vec_raw .into_iter() .map(|f| f as i128) @@ -28,15 +31,15 @@ pub fn prove_swift(circuit_bytecode: String, initial_witness_vec_raw: Vec) initial_witness.insert(Witness(i as u32 + 1), witness); } - let (proof, verification_key) = prove(circuit_bytecode, initial_witness).unwrap(); - Proof { + let (proof, verification_key) = prove(circuit_bytecode, initial_witness).ok()?; + Some(Proof { proof, verification_key, - } + }) } -pub fn verify_swift(circuit_bytecode: String, proof: Proof) -> bool { - verify(circuit_bytecode, proof.proof, proof.verification_key).unwrap() +pub fn verify_swift(circuit_bytecode: String, proof: Proof) -> Option { + verify(circuit_bytecode, proof.proof, proof.verification_key).ok() } #[cfg(test)] @@ -47,8 +50,8 @@ mod tests { #[test] fn test_prove_verify() { - let proof = prove_swift(String::from(BYTECODE), vec![1_i32, 10_i32]); - let verdict = verify_swift(String::from(BYTECODE), proof); + let proof = prove_swift(String::from(BYTECODE), vec![1_i32, 10_i32]).unwrap(); + let verdict = verify_swift(String::from(BYTECODE), proof).unwrap(); assert!(verdict); } } diff --git a/swift/bridging-header.h b/swift/bridging-header.h index beb3de5..5b47c7b 100644 --- a/swift/bridging-header.h +++ b/swift/bridging-header.h @@ -1,7 +1,7 @@ #ifndef BridgingHeader_h #define BridgingHeader_h -#include "../generated/noir_swift/noir_swift.h" #include "../generated/SwiftBridgeCore.h" +#include "../generated/noir_swift/noir_swift.h" #endif /* BridgingHeader_h */ \ No newline at end of file diff --git a/swift/build.sh b/swift/build.sh index 4d32d3d..f0d91e4 100755 --- a/swift/build.sh +++ b/swift/build.sh @@ -1 +1 @@ -swiftc -L ../target/debug/ -lnoir_swift -lbarretenberg -lstdc++ -lssl -lcrypto -import-objc-header bridging-header.h main.swift ../generated/noir_swift/noir_swift.swift ../generated/SwiftBridgeCore.swift -o main.run \ No newline at end of file +swiftc -L . -lnoir_swift -lbarretenberg -lstdc++ -lssl -lcrypto -import-objc-header bridging-header.h main.swift ../generated/noir_swift/noir_swift.swift ../generated/SwiftBridgeCore.swift -o main.run \ No newline at end of file diff --git a/swift/main.swift b/swift/main.swift index ce47b77..73c9a2d 100644 --- a/swift/main.swift +++ b/swift/main.swift @@ -1 +1,15 @@ -print("hello") \ No newline at end of file +let BYTECODE = "H4sIAAAAAAAA/7WTMRLEIAhFMYkp9ywgGrHbq6yz5v5H2JkdCyaxC9LgWDw+H9gBwMM91p7fPeOzIKdYjEeMLYdGTB8MpUrCmOohJJQkfYMwN4mSSy0ZC0VudKbCZ4cthqzVrsc/yw28dMZeWmrWerfBexnsxD6hJ7jUufr4GvyZFp8xpG0C14Pd8s/q29vPCBXypvmpDx7sD8opnfqIfsM1RNtxBQAA" + +func testProveVerify() -> Bool { + let vec = RustVec() + vec.push(value: 1) + vec.push(value: 2) + + guard let proof = prove_swift(BYTECODE, vec) else { + return false + } + + return verify_swift(BYTECODE, proof) ?? false +} + +print(testProveVerify()) \ No newline at end of file