# Advancing Zero-Knowledge Proofs on iOS: Integrating Halo2 into zkMopro and Bringing EZKL to Mobile ## Introduction In the rapidly evolving field of privacy-preserving technologies, **Zero-Knowledge Proofs (ZKPs)** have emerged as a cornerstone for secure and private computations. They allow one party to prove to another that a statement is true without revealing any additional information. This capability is crucial for applications that require verification without compromising sensitive data. Our project aimed to enhance the iOS toolkit for ZKP development by integrating the **Halo2** proving system into **zkMopro** and porting the **EZKL** library to iOS devices. These efforts were geared toward enabling **Zero-Knowledge Machine Learning (ZKML)** applications directly on mobile devices, thereby expanding possibilities for data privacy and secure computing on edge devices and brining the capabilities into regular consumer hands. --- ## Part 1: Integrating Halo2 into zkMopro ### Why Halo2? Before our integration, zkMopro only supported **Circom** with the **Groth16** proving system. While effective, these systems require a **Structured Reference String (SRS)** that needs to be generated separately for each circuit—a process that can be cumbersome and limits flexibility. **Halo2**, on the other hand, utilizes a **Common Reference String (CRS)**, which can be generated once and reused across multiple circuits. This approach significantly streamlines the setup process and is more adaptable for various applications. As a result, Halo2 and similar proving systems have seen wider adoption in the industry and is the foundation for projects like **EZKL**, which lacks a direct equivalent in the Circom ecosystem. ### The Significance of Integration Integrating Halo2 into zkMopro was an important step toward making Mopro support multiple proving systems within the platform, as before zkMopro only supported Circom. This enhancement not only provided developers with more options but also ensured a more modular and maintainable codebase. By enabling the Halo2 support, we have laid the groundwork for future integrations of other proving systems, making zkMopro a more versatile tool for ZKP development. ### The Integration Process The integration spanned several weeks and coincided with a major rework of the zkMopro codebase. While this overlap initially posed challenges—requiring us to restart parts of the integration—it ultimately led to a cleaner and more modular architecture. One of the primary challenges was accommodating the different flavors of the Halo2 library. To address this, we enabled user to provide their custom configuration of the proving system while allowing zkMopro to handle the bindings generation and iOS compilation. This design decision empowers developers to write Halo2 circuits and compile them for iOS with minimal effort and changes to their original Halo2 code. The following code snippet demonstrates the simplicity of including Halo2 circuits in zkMopro: ```rust mopro_ffi::set_halo2_circuits! { ("fibonacci_pk.bin", halo2_fibonacci::prove, "fibonacci_vk.bin", halo2_fibonacci::verify), } fn get_halo2_proving_circuit(circuit: &str) -> Result<Halo2ProveFn, MoproError> { match circuit { "fibonacci_pk.bin" => Ok(halo2_fibonacci::prove), _ => Err(MoproError::CircuitNotFound), } } fn get_halo2_verifying_circuit(circuit: &str) -> Result<Halo2VerifyFn, MoproError> { match circuit { "fibonacci_vk.bin" => Ok(halo2_fibonacci::verify), _ => Err(MoproError::CircuitNotFound), } } ``` ### Contributions and Achievements - **Pull Request Submission**: We submitted a pull request to add Halo2 support to zkMopro ([PR #214](https://github.com/zkmopro/mopro/pull/214)), which has been reviewed and merged. - **Benchmarking**: Benchmarked Halo2 on iOS using three different circuits, aligning with the zkMopro team's benchmarks for Circom ([Benchmark Results](https://zkmopro.org/docs/performance#halo2)). - **Testing and Documentation**: Implemented unit and integration tests, and [enhanced documentation](https://github.com/zkmopro/mopro/pull/216) to guide developers in using Halo2 with zkMopro. - **Sample Projects**: Created sample projects to demonstrate the integration: - [RSA Full zkMopro Example](https://github.com/ElusAegis/halo2-rsa-mopro.git) - [Keccak256 zkMopro Halo2 Circuit](https://github.com/ElusAegis/halo2-keccak-stable.git) - [Fibonacci zkMopro Halo2 Circuit](https://github.com/ElusAegis/halo2-fibonacci-sample.git) Although we proposed additional improvements to simplify the `mopro` library in another pull request ([PR #201](https://github.com/zkmopro/mopro/pull/201)), it was not merged. Nonetheless, our contributions have additionally enhanced zkMopro's flexibility and ease of use. --- ## Part 2: Porting EZKL to iOS ### Introduction to EZKL **EZKL** is a Rust library that facilitates zero-knowledge proofs for machine learning models, particularly neural networks. It enables proving that a neural network computation was performed correctly without revealing the inputs or internal parameters. Integrating EZKL into iOS devices unlocks new opportunities for privacy-preserving machine learning applications on mobile platforms. ### Challenges and Solutions During the porting process, we encountered two major challenges. First, the **EZKL** library managed data through file operations, which posed limitations for mobile applications where models, proofs, and witnesses are typically transmitted over networks rather than stored locally. Second, although we had previously integrated **Halo2** into **zkMopro**, we decided not to use it for this project because zkMopro's integration only exposed the basic `prove` and `verify` functions for proving and verifying circuits. However, to successfully port EZKL we needed to port EZKL specific functions, such as `verify_network_settings`, which had not been defined in zkMopro. To address the first issue, we rewrote the core EZKL functions to operate on data objects instead of files, delegating file handling to the application layer. This approach aligns better with mobile development practices and offers developers greater flexibility in managing data, especially in environments where file access is restricted, like iOS. As for the second challenge, we opted to build the integration from scratch rather than relying on zkMopro’s Halo2 integration. This decision allowed us to create a leaner, as well as more comprehensive interface that not only exposes the essential proof and verification functions but also provides the flexibility to configure and expose additional functionalities, such as witness generation and key setup. Our solution is available in the [EZKL iOS Bindings Generator](https://github.com/ElusAegis/ezkl-ios-rust-porter/), which provides Swift-compatible bindings for EZKL, enabling seamless integration with iOS applications. We have also more recently done a direct integration of iOS bindings into the EZKL library, which allowed us to shrink the iOS package size by 300% and which is available [here](https://github.com/ElusAegis/ezkl-for-ios). ### Achievements - **Swift Package Development**: Created a Swift Package for integrating EZKL into Xcode projects via Swift Package Manager, eliminating the need to build the library from source. The package is accessible at [EZKL iOS Package](https://github.com/ElusAegis/ezkl-ios-port). - **Continuous Integration Setup**: Established a CI/CD pipeline that updates the package with each commit to the main branch of the Rust repository when bindings are updated. - **Example Application**: Developed a sample iOS application demonstrating how to use EZKL within an iOS app, which can be found in the [Example directory](https://github.com/ElusAegis/ezkl-ios-port/blob/main/Example). <img src="https://github.com/ElusAegis/ezkl-ios-port/blob/main/docs/sample-app.png?raw=true" alt="Sample App Screenshot" width="20%"/> - **Testing and Documentation**: Added unit and integration tests, and provided comprehensive documentation to assist developers in integrating EZKL into their projects. ### Installation Considerations Due to limitations with Xcode's Swift Package Manager and **Git Large File Storage (LFS)**, the package must currently be installed manually: 1. **Download the Package**: Clone the repository or download it as a ZIP file from GitHub. ```bash git clone https://github.com/ElusAegis/ezkl-ios-port.git ``` 2. **Add as a Local Package**: In Xcode, navigate to **File** > **Add Packages...**, click **Add Local...**, and select the downloaded package directory. For more details, refer to the [Installation Guide](https://github.com/ElusAegis/ezkl-ios-port#installation) in the repository. --- ## Conclusion Our project successfully achieved its objectives of integrating the Halo2 proving system into zkMopro and porting the EZKL library to iOS. These advancements significantly enhance the capabilities of developers working with zero-knowledge proofs on mobile devices, enabling more complex and privacy-preserving applications. **Future Plans**: - **Experiment with MPC-in-the-head Proving Systems** - **Explore Folding potential on Mobile Phones** - **Hand of support over the EZKL iOS Package to the EZKL team** - **Explore Metal optimisation for the EZKL library** --- *Thank you for reading. For more information or to contribute to these projects, please visit the respective GitHub repositories linked throughout this post.*