Skip to content

Commit

Permalink
documentation@readme updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartosz Nowak committed Oct 25, 2023
1 parent 4b5805a commit 49df762
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 23 deletions.
56 changes: 37 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -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&[email protected]/badge.svg)
![GitHub Workflow Status Ubuntu](https://github.com/visoftsolutions/noir_swift/actions/workflows/build&[email protected]/badge.svg)
![GitHub Workflow Status Macos](https://github.com/visoftsolutions/noir_swift/actions/workflows/build&[email protected]/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-<hash>/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-<hash>/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 {
Expand All @@ -63,4 +65,20 @@ fn verify_swift(circuit_bytecode: String, proof: Proof) -> Option<bool>;

## 🔍 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<Int32>()
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
```
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ pub struct Proof {
pub verification_key: Vec<u8>,
}

/// 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<Proof>`: 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<i32>) -> Option<Proof> {
let initial_witness_vec: Vec<FieldElement> = initial_witness_vec_raw
.into_iter()
Expand All @@ -38,6 +49,17 @@ pub fn prove_swift(circuit_bytecode: String, initial_witness_vec_raw: Vec<i32>)
})
}

/// 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<bool>`: 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<bool> {
verify(circuit_bytecode, proof.proof, proof.verification_key).ok()
}
Expand Down
8 changes: 4 additions & 4 deletions swift/main.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
let BYTECODE = "H4sIAAAAAAAA/7WTMRLEIAhFMYkp9ywgGrHbq6yz5v5H2JkdCyaxC9LgWDw+H9gBwMM91p7fPeOzIKdYjEeMLYdGTB8MpUrCmOohJJQkfYMwN4mSSy0ZC0VudKbCZ4cthqzVrsc/yw28dMZeWmrWerfBexnsxD6hJ7jUufr4GvyZFp8xpG0C14Pd8s/q29vPCBXypvmpDx7sD8opnfqIfsM1RNtxBQAA"

func testProveVerify() -> Bool {
let vec = RustVec<Int32>()
vec.push(value: 1)
vec.push(value: 2)
let witness = RustVec<Int32>()
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
}

Expand Down

0 comments on commit 49df762

Please sign in to comment.