How yoii-inc/zk-mpc works
Major Components
• Core Cryptographic Libraries (arkworks/*): These directories encapsulate the basic math, field and polynomial operations, elliptic curve arithmetic, SNARK construction (groth16, marlin, r1cs-std, poly-commit, etc.), and curve definitions.
• MPC Sub-Crates:
– mpc-algebra: Implements MPC-friendly algebra types (e.g., MpcField, MpcVar) and operations.
– mpc-net: Handles networking, communication protocols among participants.
– mpc-trait: Encapsulates shared trait definitions for MPC computations.
• Application/Client Logic (src/): Implements high-level circuit definitions (various circuits, constraint definitions in input_circuit.rs and circuits/*), and modules for encryption (she) and zk-SNARK integration (groth16.rs, marlin.rs). It also includes orchestration of the preprocessing phase (producing necessary computation artifacts) and the online phase (executing MPC protocols).
• Examples & Scripts:
– Werewolf game (both CLI and standalone game binaries) that demonstrates using the MPC, secret input specification, and online computation.
– Benchmark scripts (in benches and docs) and various run-*.zsh scripts that orchestrate the overall process.
Relationships and Data Flow
• Preprocessing Phase: The “main” binary (src/main.rs) orchestrates the initialization of the MPC protocols where circuits, constraints, and cryptographic proofs (e.g., commitment generation, ZKP proofs) are generated using the underlying arkworks libraries and mpc-algebra.
• Online Phase: The online script (run_online.zsh) triggers the network module (mpc-net) to perform live multiparty computation, using secret inputs specified via JSON (inputs folder) and validated by circuit constraints.
• Interaction between Modules:
– The application layer (src/circuits, src/she, etc.) calls functions provided by the arkworks primitives to build circuits and generate proofs.
– The mpc-algebra and mpc-trait crates serve as wrappers/adapters that combine the abstract cryptographic operations with concrete MPC protocols.
– The mpc-net crate ensures that the different parties communicate their shares and results according to the SPDZ-like protocol delineated in the README.
– The examples (e.g., Werewolf game) illustrate a complete flow: input specification, preprocessing (via groth16 or marlin), and execution of an MPC-based online phase.
Architectural Patterns and Protocols
• Layered Design: Separate low-level cryptographic computations (arkworks libraries) from higher-level MPC coordination (mpc-algebra, mpc-net).
• Modularization: Each major cryptographic primitive and protocol is isolated into its own sub-crate (e.g., poly-commit, curves, nonnative), making it easier to understand dependencies.
• Dual-Phase Execution: The system distinctly separates the offline (preprocessing) and online computation phases, a common pattern in MPC design.
• Extensibility: The design supports multiple protocols (e.g., Groth16, Marlin, SHE) and allows users to plug in custom circuits or even custom game logic (as in Werewolf).
Technology and Framework Details
• Rust-based build system (Cargo with separate Cargo.toml files for each sub-crate).
• GitHub integration for continuous integration (see .github/workflows).
• Usage of Rust macros and constraints (see ff-macros, r1cs-std) to enforce field arithmetic and circuit synthesis.
• Shell scripts (zsh files) that help trigger different phases of the system.
• JSON-based input specification (via inputs/inputs.json) for secret parameters.