mopro_ffi/
lib.rs

1#![allow(unexpected_cfgs)]
2
3#[cfg(feature = "build")]
4pub mod app_config;
5
6// UniFFI re-export
7//
8// Uniffi macros use fully qualified paths (`::uniffi::*`) internally.
9// To allow downstream crates to transparently resolve these macros to `mopro_ffi`,
10// users must alias it (`extern crate mopro_ffi as uniffi;`, automated via `app!` macro).
11//
12// However, for this alias to work correctly, `mopro_ffi` must provide the exact same
13// exported items as the original `uniffi`. Hence, we re-export all individual items.
14#[cfg(feature = "uniffi")]
15pub use uniffi::*;
16
17#[cfg(feature = "uniffi")]
18#[macro_export]
19macro_rules! uniffi_setup {
20    () => {
21        // `::uniffi` must be available in the caller’s extern-prelude.
22        extern crate mopro_ffi as uniffi;
23        uniffi::setup_scaffolding!();
24    };
25}
26
27#[cfg(not(feature = "uniffi"))]
28#[macro_export]
29macro_rules! uniffi_setup {
30    () => {
31        // No-op when `uniffi` feature isn't enabled in `mopro_ffi`.
32    };
33}
34
35#[cfg(feature = "flutter")]
36pub use flutter_rust_bridge::*;
37
38#[cfg(feature = "flutter")]
39#[macro_export]
40macro_rules! flutter_setup {
41    () => {
42        // ::uniffi must be available in the caller’s extern-prelude.
43        extern crate mopro_ffi as flutter_rust_bridge;
44        pub fn init_app() {
45            // Default utilities - feel free to customize
46            flutter_rust_bridge::setup_default_user_utils();
47        }
48    };
49}
50
51#[cfg(not(feature = "flutter"))]
52#[macro_export]
53macro_rules! flutter_setup {
54    () => {};
55}
56
57#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
58pub use console_error_panic_hook;
59#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
60pub use getrandom;
61#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
62pub use serde_wasm_bindgen;
63#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
64pub use wasm_bindgen::*;
65#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
66pub use wasm_bindgen_console_logger;
67#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
68pub use wasm_bindgen_futures;
69#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
70pub use wasm_bindgen_rayon::*;
71
72#[macro_export]
73macro_rules! wasm_setup {
74    () => {
75        extern crate mopro_ffi as wasm_bindgen;
76        extern crate mopro_ffi as wasm_bindgen_rayon;
77        use wasm_bindgen::prelude::*;
78        use wasm_bindgen_rayon::init_thread_pool;
79    };
80}
81
82/// This macro is used to setup the Mopro FFI library
83/// It should be included in the `lib.rs` file of the project
84///
85/// This should be used with the adapter-specific macros, such as `set_circom_circuits!(...)`
86/// and `set_halo2_circuits!(...)`, etc.
87///
88/// # Circom Example
89/// ```ignore
90/// // Setup the Mopro FFI library
91/// mopro_ffi::app!();
92///
93/// // Generate a Witness Generation function for the `multiplier2` circom circuit
94/// rust_witness::witness!(multiplier2);
95///
96/// // Add `multiplier2` circom circuit to be exposed to the FFI
97/// mopro_ffi::set_circom_circuits!(
98///     "multiplier2_final.zkey",
99///     WitnessFn::RustWitness(multiplier2_witness),
100/// )
101/// ```
102///
103/// # Halo2 Example
104/// ```ignore
105/// // Setup the Mopro FFI library
106/// mopro_ffi::app!();
107///
108/// // Add `Fibonacci` circuit to generate proofs and verify proofs
109/// mopro_ffi::set_halo2_circuits!(
110///     "plonk_fibonacci_pk.bin",
111///     plonk_fibonacci::prove,
112///     "plonk_fibonacci_vk.bin",
113///     plonk_fibonacci::verify
114/// );
115/// ```
116///
117/// # Noir Example
118///
119/// Noir integration supports two hash functions for different use cases:
120/// - **Poseidon hash**: Default choice, optimized for performance and off-chain verification
121/// - **Keccak256 hash**: Required for Solidity verifier compatibility and on-chain verification
122///
123/// The hash function is automatically selected based on the `on_chain` parameter:
124/// - `on_chain = false` → Uses Poseidon (better performance)
125/// - `on_chain = true` → Uses Keccak256 (Solidity compatible)
126///
127/// Reference: https://noir-lang.org/docs/how_to/how-to-solidity-verifier
128///
129/// You don't need to generate Witness Generation functions first, like `Circom` or `Halo2` does.
130/// All you need to do is to setup the Mopro FFI library as below.
131///
132/// ```ignore
133/// // Setup the Mopro FFI library
134/// mopro_ffi::app!();
135///
136/// ```
137///
138#[macro_export]
139macro_rules! app {
140    () => {
141        mopro_ffi::uniffi_setup!();
142        mopro_ffi::flutter_setup!();
143        #[cfg(all(feature = "wasm", target_arch = "wasm32"))]
144        mopro_ffi::wasm_setup!();
145    };
146}