diff --git a/README.md b/README.md index 431f4f7..7356185 100644 --- a/README.md +++ b/README.md @@ -1,55 +1,57 @@ -# SWIFT-BRIDGE for `noir_rs` +# Swift-Bridge for `noir_rs` ![GitHub Workflow Status Fedora](https://github.com/visoftsolutions/noir_swift/actions/workflows/build&test@fedora.yml/badge.svg) ![GitHub Workflow Status Ubuntu](https://github.com/visoftsolutions/noir_swift/actions/workflows/build&test@ubuntu.yml/badge.svg) ![GitHub Workflow Status Macos](https://github.com/visoftsolutions/noir_swift/actions/workflows/build&test@macos.yml/badge.svg) ![Version](https://img.shields.io/badge/version-0.1.0-blue) -Bridge the gap between Swift and the `noir_rs` library, offering Swift developers a seamless way to utilize the `noir_rs` library's zkSNARK proving scheme functionalities across iOS, macOS, WatchOS, and other Swift-compatible platforms. +Swift-Bridge connects the Swift ecosystem to the `noir_rs` Rust library. This provides Swift developers with a streamlined experience when integrating `noir_rs`'s zkSNARK proving functionalities on iOS, macOS, WatchOS, and other platforms supported by Swift. ## ๐Ÿš€ Features -- ๐Ÿ“ฑ Directly use `noir_rs` zkSNARK functionalities in Swift. -- ๐ŸŒ‰ Effortless interfacing between Swift and Rust. -- ๐Ÿ”’ Secure zkSNARK proof generation and verification. +- ๐Ÿ“ฑ Native access to `noir_rs` zkSNARK tools within Swift. +- ๐ŸŒ‰ Smooth integration between Swift and Rust, making the bridge almost invisible. +- ๐Ÿ”’ Ensured security with zkSNARK proof generation and verification. -## ๐Ÿ“ฆ Rust Build +## Building & Testing + +### ๐Ÿ“ฆ Building Rust ```bash cargo build ``` -## ๐Ÿงช Rust Testing +### ๐Ÿงช Testing Rust -Ensure the reliability and robustness of your bridge with the provided tests: +To maintain the utmost confidence in the bridge, leverage the provided tests: ```bash cargo test ``` -## ๐Ÿ“ฆ Swift Build +### ๐Ÿ“ฆ Building Swift ```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 +# Copy noir_swift lib to the swift directory cp target/debug/libnoir_swift.a swift/ +# Copy barretenberg lib to the swift directory +cp target/debug/build/barretenberg-/out/lib/libbarretenberg.a swift/ cd swift -# compile swift project +# Compile the Swift project ./build -# run the program +# Run the program ./main.run ``` -## ๐ŸŽฏ Quick Usage +## ๐ŸŽฏ Usage -We provide two main functions for the bridge: +The bridge offers two main functions: -1. **prove_swift**: Generates a zkSNARK proof using the given circuit bytecode and initial witness. -2. **verify_swift**: Verifies a given zkSNARK proof against a circuit and its verification key. +1. **prove_swift**: Create a zkSNARK proof using provided circuit bytecode and an initial witness. +2. **verify_swift**: Authenticate a zkSNARK proof against its respective circuit and verification key. ```rust pub struct Proof { @@ -63,4 +65,20 @@ fn verify_swift(circuit_bytecode: String, proof: Proof) -> Option; ## ๐Ÿ” Example -Refer to the `tests` module for a comprehensive usage example: +In this example, we're using a noir circuit that verifies if x is different from y: + +```swift +let BYTECODE = "H4sIAAAAAAAA/7WTMRLEIAhFMYkp9ywgGrHbq6yz5v5H2JkdCyaxC9LgWDw+H9gBwMM91p7fPeOzIKdYjEeMLYdGTB8MpUrCmOohJJQkfYMwN4mSSy0ZC0VudKbCZ4cthqzVrsc/yw28dMZeWmrWerfBexnsxD6hJ7jUufr4GvyZFp8xpG0C14Pd8s/q29vPCBXypvmpDx7sD8opnfqIfsM1RNtxBQAA" + +let witness = RustVec() +witness.push(value: 1) +witness.push(value: 2) + +guard let proof = prove_swift(BYTECODE, witness) else { + return false +} + +let verdict = verify_swift(BYTECODE, proof) ?? false + +print(verdict) // Outputs true upon successful verification +``` \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index fc3f61f..0eecf56 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,17 @@ pub struct Proof { pub verification_key: Vec, } +/// Generates a zkSNARK proof from given bytecode and witness values. +/// +/// This function converts the initial witness vector from i32 values to FieldElement values, then creates a WitnessMap +/// from these values. Using the provided circuit bytecode and this WitnessMap, it generates the zkSNARK proof. +/// +/// # Parameters +/// - `circuit_bytecode`: The bytecode of the ACIR circuit for which a proof needs to be generated. +/// - `initial_witness_vec_raw`: The initial witness values represented as a vector of i32. +/// +/// # Returns +/// - `Option`: The generated proof and its associated verification key wrapped in a `Proof` struct. Returns `None` if the proof generation fails. pub fn prove_swift(circuit_bytecode: String, initial_witness_vec_raw: Vec) -> Option { let initial_witness_vec: Vec = initial_witness_vec_raw .into_iter() @@ -38,6 +49,17 @@ pub fn prove_swift(circuit_bytecode: String, initial_witness_vec_raw: Vec) }) } +/// Verifies a given zkSNARK proof using the associated circuit bytecode and verification key. +/// +/// This function takes in the circuit bytecode, the generated proof, and its verification key, then verifies +/// the correctness of the proof. +/// +/// # Parameters +/// - `circuit_bytecode`: The bytecode of the ACIR circuit against which the proof needs to be verified. +/// - `proof`: The `Proof` struct containing the zkSNARK proof and its verification key. +/// +/// # Returns +/// - `Option`: Returns `true` if the proof is valid, `false` otherwise. Returns `None` if the verification process fails. pub fn verify_swift(circuit_bytecode: String, proof: Proof) -> Option { verify(circuit_bytecode, proof.proof, proof.verification_key).ok() } diff --git a/swift/main.swift b/swift/main.swift index 73c9a2d..27edb6c 100644 --- a/swift/main.swift +++ b/swift/main.swift @@ -1,11 +1,11 @@ let BYTECODE = "H4sIAAAAAAAA/7WTMRLEIAhFMYkp9ywgGrHbq6yz5v5H2JkdCyaxC9LgWDw+H9gBwMM91p7fPeOzIKdYjEeMLYdGTB8MpUrCmOohJJQkfYMwN4mSSy0ZC0VudKbCZ4cthqzVrsc/yw28dMZeWmrWerfBexnsxD6hJ7jUufr4GvyZFp8xpG0C14Pd8s/q29vPCBXypvmpDx7sD8opnfqIfsM1RNtxBQAA" func testProveVerify() -> Bool { - let vec = RustVec() - vec.push(value: 1) - vec.push(value: 2) + let witness = RustVec() + witness.push(value: 1) + witness.push(value: 2) - guard let proof = prove_swift(BYTECODE, vec) else { + guard let proof = prove_swift(BYTECODE, witness) else { return false }