# zkaa-onchain/zkaa-recursive architecture

> Architecture diagram of the GitHub repository zkaa-onchain/zkaa-recursive, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/zkaa-onchain/zkaa-recursive
- Repository: https://github.com/zkaa-onchain/zkaa-recursive

## Overview

Based on our analysis of the file tree and README, this project is a Rust-based command line toolkit called “plonkit” that serves as a zk-SNARK workflow engine for circuits written in the circom DSL. Its purpose is to provide an end‐to‐end solution for generating proofs, verifying them, aggregating recursive proofs, and generating a Solidity verifier smart contract using the plonk proof system. The project integrates with external tools like circom and snarkit2 for circuit compilation and witness generation, and it offers a complete workflow from key setup (trusted SRS generation) to smart contract verification.

##### Main Components
- CLI Entry Point: Represented by src/bin/main.rs, this is the command line interface that parses user commands and delegates tasks (subcommands such as “prove”, “recursive-prove”, “setup”, etc.).
- Core Engine & Modules: Divide the engine into logical Rust modules such as:
- Circuit Processing: Modules like circom_circuit.rs and r1cs_file.rs that handle circuit representation, loading, and analysis.
- Proof Generation: Modules (e.g., plonk.rs and transpile.rs) that implement proof generation logic.
- Recursive Proof Handling: The “recursive” module manages aggregation of multiple proofs for recursive verification.
- Utility Functions: Module utils.rs, reader.rs, and tests.rs support common functionality and testing.
- Key and Setup Management: Represent the “keys/setup” directory and associated logic for generating/using the SRS (Structured Reference String) for Plonk.
- Test & Validation Infrastructure:
- Test circuits folder and associated shell scripts (e.g., test_poseidon_plonk.sh) that illustrate end-to-end flows.
- Contract Integration: The “test/contract” folder highlighting the Hardhat configuration and Solidity verifier generation.
- External Tools and Dependencies: Include circom and snarkit2 as external executables (with prerequisites noted in the README), as well as the Rust toolchain and dependencies defined in Cargo.toml.

##### Relationships and Data Flow
- From the main CLI, commands are routed to the Proof Generation Module which:
1. Loads a circuit file (using circom_circuit.rs or r1cs_file.rs).
2. Possibly calls out to external tools to generate a witness (via snarkit2/circom externally).
3. Uses proof generation logic in plonk.rs to create a SNARK proof.
4. Optionally aggregates proofs using the recursive proof module.
- Once a proof is generated, the system exports a verification key (and a Solidity verifier may be generated), with a clear flow from input (circuit and SRS/key files) to output (proof.bin, vk.bin, verifier.sol).

##### Architectural Patterns and Design Principles
- CLI-driven Workflow: The design is centered around a command-line interface that controls the execution of various subcommands.
- Extensibility: Separate concerns for proof generation, recursive aggregation, and verifier generation indicate a design that allows new subcommands or algorithm enhancements.
- Integration with External Systems: The integration with external circuit compilers (circom) and blockchain contracts (via Hardhat and Solidity) should be depicted.

##### Detailed Component Relationships
- CLI Layer: Show how commands (prove, recursive-prove, setup, export-verification-key, generate-verifier) are received and routed.
- Data Flow: From user-provided circuit and input files → processed by circuit modules → passed to the proof generation module → outputting proof binaries and verification keys.
- Recursive Component: Clearly mark its role in aggregating individual proofs for more complex verification strategies.
- Testing & Verification: Illustrate the integration tests that verify the entire circuit-to-verifier workflow, connecting test folders with actual build artifacts.
- External Interaction: Mark dependencies on external executables (circom and snarkit2) which are prerequisites before the Rust toolchain kicks in.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% CLI Layer
    A["CLI (main.rs)"]:::cli

    %% Core Engine Modules
    subgraph "Core Engine Modules"
        B1["Circuit Processing: circom_circuit.rs"]:::core
        B2["Circuit Processing: r1cs_file.rs"]:::core
        C1["Proof Generation: plonk.rs"]:::core
        C2["Proof Generation: transpile.rs"]:::core
        D["Recursive Proof Handling (mod.rs)"]:::core
        E1["Reader (reader.rs)"]:::utility
        E2["Utils (utils.rs)"]:::utility
        E3["Tests (tests.rs)"]:::utility
    end

    %% Key and Setup Management
    F["SRS Key Setup (keys/setup)"]:::setup

    %% External Dependencies
    subgraph "External Dependencies"
        G["circom"]:::external
        H["snarkit2"]:::external
        I["Rust Toolchain (Cargo.toml)"]:::external
    end

    %% Outputs Section
    subgraph "Outputs"
        O1["SNARK Proof (proof.bin)"]:::output
        O2["Verification Key (vk.bin)"]:::output
        O3["Solidity Verifier (verifier.sol)"]:::output
    end

    %% Test & Validation Infrastructure
    subgraph "Test & Validation"
        T1["Test Circuits"]:::test
        T2["Smart Contract Integration"]:::test
    end

    %% Data flow connections
    I -->|"builds"| A
    A -->|"routes"| B1
    A -->|"routes"| B2

    B1 -->|"analyzes"| C1
    B2 -->|"analyzes"| C2

    %% External tools for witness generation
    B1 -->|"calls"| G
    B1 -->|"calls"| H
    B2 -->|"calls"| G
    B2 -->|"calls"| H
    G -->|"witness-output"| C1
    H -->|"witness-output"| C1

    %% Key setup integration
    F -->|"provides SRS key"| C1

    %% Proof generation flow to recursive handling
    C1 -->|"generates proof"| D
    C2 -->|"generates proof"| D

    %% Direct output from proof generation (if not aggregated)
    C1 -->|"exports"| O1
    C1 -->|"exports"| O2
    C1 -->|"exports"| O3

    %% Aggregated output from recursive proof handling
    D -->|"aggregates proof"| O1
    D -->|"aggregates vk"| O2
    D -->|"generates verifier"| O3

    %% Utility functions support
    C1 -->|"logs/util"| E1
    D -->|"logs/util"| E2
    D -->|"test support"| E3

    %% Testing and validation integration
    O1 -->|"validated by"| T1
    O2 -->|"validated by"| T1
    O3 -->|"integrated with"| T2

    %% Click Events
    click A "https://github.com/zkaa-onchain/zkaa-recursive/blob/zkaa/src/bin/main.rs"
    click B1 "https://github.com/zkaa-onchain/zkaa-recursive/blob/zkaa/src/circom_circuit.rs"
    click B2 "https://github.com/zkaa-onchain/zkaa-recursive/blob/zkaa/src/r1cs_file.rs"
    click C1 "https://github.com/zkaa-onchain/zkaa-recursive/blob/zkaa/src/plonk.rs"
    click C2 "https://github.com/zkaa-onchain/zkaa-recursive/blob/zkaa/src/transpile.rs"
    click D "https://github.com/zkaa-onchain/zkaa-recursive/blob/zkaa/src/recursive/mod.rs"
    click E1 "https://github.com/zkaa-onchain/zkaa-recursive/blob/zkaa/src/reader.rs"
    click E2 "https://github.com/zkaa-onchain/zkaa-recursive/blob/zkaa/src/utils.rs"
    click E3 "https://github.com/zkaa-onchain/zkaa-recursive/blob/zkaa/src/tests.rs"
    click F "https://github.com/zkaa-onchain/zkaa-recursive/tree/zkaa/keys/setup"
    click T1 "https://github.com/zkaa-onchain/zkaa-recursive/tree/zkaa/test/circuits"
    click T2 "https://github.com/zkaa-onchain/zkaa-recursive/tree/zkaa/test/contract"

    %% Styles
    classDef cli fill:#B3CDE0,stroke:#6497B1,stroke-width:2px;
    classDef core fill:#CDEAC0,stroke:#6BBE44,stroke-width:2px;
    classDef utility fill:#FDFD97,stroke:#E2C044,stroke-width:2px;
    classDef setup fill:#FFDF91,stroke:#E59866,stroke-width:2px;
    classDef external fill:#FF9AA2,stroke:#E27D60,stroke-width:2px;
    classDef output fill:#D5E1DF,stroke:#88BDBC,stroke-width:2px;
    classDef test fill:#E0BBE4,stroke:#957DAD,stroke-width:2px;
```
