How scipr-lab/libsnark works
Project type and purpose
• This is a C++ cryptographic library (libsnark) implementing zero-knowledge SNARK proof systems.
• It is a reusable, research‐quality toolkit, not a standalone application; client code (examples or user applications) link against it.
High‐level components
A. External Dependencies (“depends” submodule)
• libff (finite fields & elliptic curves)
• libfqfft (FFT over finite fields)
• ate-pairing, xbyak (BN128 curve support)
• libsnark-supercop (crypto primitives for ADSNARK)
• GTest (unit tests)
B. Build & Config
• CMakeLists.txt, Dockerfile, Travis CI config
• Transition‐to‐CMake.md, doxygen.conf
C. Core Library (libsnark/ directory)
1. common
– Utilities, data structures (Merkle trees, sparse vectors, integer permutation)
2. gadgetlib1
– Low‐level, template‐based gadget API for building R1CS constraints
– Basic gadgets, CPU checkers, curve gadgets, hash gadgets, pairing gadgets, routing gadgets, etc.
3. gadgetlib2
– Higher‐level, non‐template gadget API; examples & tutorial
4. relations
– Formal definitions of NP languages: R1CS, USCS, BACS, TBCS, SAP, QAP, SSP, RAM computations
5. reductions
– Conversions between NP languages (e.g. BACS→R1CS, R1CS→QAP, RAM→R1CS, TBCS→USCS)
6. zk_proof_systems
– Implementations of proof systems (ppzkSNARKs, ADSNARK, PCD) parameterized over R1CS, TBCS, USCS, etc.
7. examples & tests
– In‐tree examples for each module and unit tests (GTest)
Relationships and interactions
• External Dependencies → built by CMake → linked into libsnark core
• common → foundational data types → used by gadgetlib1, gadgetlib2, relations, reductions, zk_proof_systems
• relations → defines NP languages → consumed by gadgetlib1/gadgetlib2 (to build instances) and by zk_proof_systems (to implement prove/verify)
• reductions → use relations to translate languages → feed into zk_proof_systems backends
• gadgetlib1 & gadgetlib2 → produce R1CS instances (by pulling in common and relations) → passed to zk_proof_systems R1CS backends
• zk_proof_systems → core generator/prover/verifier algorithms → depend on libff and libfqfft for field arithmetic & FFT, and on pairing libraries (ate-pairing)
• examples → demonstrate usage of gadget libraries and proof system APIs
• tests → validate individual modules
Architectural patterns & design principles
• Layered modular architecture:
• Base layer: common utilities & external libraries
• Middle layer: constraint-building (gadgets & relations)
• Upper layer: proof system implementations
• Consumer layer: examples, applications, tests
• Separation of concerns: data structures vs. language definitions vs. proof algorithms
• Template‐based vs. object‐oriented APIs (gadgetlib1 vs. gadgetlib2)
• Use of CMake for multi‐platform build configuration and dependency management
• Optional features toggled via CMake flags (curve choice, multicore, profiling, etc.)
Detailed capture of essential elements
• Do not omit the “common” layer—it underpins everything.
• Both gadgetlib1 and gadgetlib2, since they represent alternative APIs.
• Reductions module to illustrate language‐translation pathways.
• Tie in build scripts/CMake as the orchestrator for fetching dependencies, configuring flags, and building each module.