eid-circuits 0.8.3

The identity layer for noir-zk pipelines: eid-circuits' frozen DSC, SOD and document steps as a layered registry (families eid/dsc, eid/sod, eid/document with their roots and typed bindings), proven with noir-zk's generic kernels
//! The identity layer for noir-zk pipelines: eid-circuits' frozen circuits
//! as a *layered registry* (noir-zk 0.3), with the three families a
//! combining registry names in its pipelines.
//!
//! [`circuits`] is generated by `noir-zk-codegen` from `circuits/manifest.toml`
//! and `resources/` (written by `noir-zk freeze`, see `mise run freeze`; the
//! families are hand-written in the manifest). Every active circuit gets
//! typed `Inputs` / `Outputs` and a marker type implementing `Circuit`,
//! `CircuitId` (its Chonk key embedded) and `App`, plus [`circuits::REGISTRY`],
//! [`circuits::FAMILIES`] (with each family's root) and one marker type per
//! family:
//!
//! | family | circuits | record | link in | link out | public |
//! |---|---|---|---|---|---|
//! | `eid/dsc` (`KernelStepDsc`) | 124 DSC steps | `[registry_root, c_A]` | none | `CA` | `registry_root` |
//! | `eid/sod` (`KernelStepSod`) | 124 SOD steps | `[c_A, c_B]` | `CA` | `CB` | nothing |
//! | `eid/document` (`KernelStepDocument`) | 48 document steps | `[c_B, payload_commitment, date, scope, nullifier]` | `CB` | `PayloadCommitment` | `date`, `scope`, `nullifier` |
//!
//! `KernelStepDsc::select(label, toml)` dispatches a label `eid_prover::select`
//! chose to its circuit and refuses labels outside the family. There are no
//! kernels and no pipelines here: noir-zk's generic kernels
//! (`noir_zk_backend::kernels`) fold any pipeline over these families, and a
//! combining registry declares the pipelines (which envelope app opens the
//! document's payload commitment, what else is folded) and their deployment
//! root. `tests/fold.rs` folds the test-only pipeline `[dsc, sod, document]`
//! for every synthetic chain.
//!
//! Bytecode isn't embedded (about 760 MB of base64 release assets, 535 MB
//! as gzipped packs): [`artifacts`] serves it from a store and rejects it
//! unless it hashes to the pinned value.

use noir_zk_backend::{ArtifactStore, Frozen};

pub use noir_zk_backend::DirStore;

/// Where releases publish the circuit packs: `<pack>@<version>.tar.gz`,
/// `catalog@<version>.json`, and `catalog.json` (the latest release's
/// catalog). Every asset in a pack is checked against its pin, so this host
/// is a mirror, not a trust anchor.
pub const PACKS_URL: &str = "https://circuits.zk-eid.dev";
#[cfg(feature = "http")]
pub use noir_zk_backend::HttpStore;

/// Generated from the frozen registry (see the crate docs).
#[allow(missing_docs, clippy::all)]
pub mod circuits {
    include!(concat!(env!("OUT_DIR"), "/circuits.rs"));
}

/// Circuits compiled and embedded at build time (feature `bundled`).
#[cfg(feature = "bundled")]
pub mod bundled {
    use noir_zk_backend::ArtifactStore;
    use noir_zk_core::Error;

    include!(concat!(env!("OUT_DIR"), "/bundled.rs"));

    /// The embedded assets as a store; [`artifacts`](crate::artifacts)
    /// still checks each against its pin.
    pub struct BundledStore;

    impl ArtifactStore for BundledStore {
        fn fetch(&self, asset: &str) -> Result<Vec<u8>, Error> {
            ASSETS
                .iter()
                .find(|(name, _)| *name == asset)
                .map(|(_, bytes)| bytes.to_vec())
                .ok_or_else(|| Error::Artifact(format!("{asset} is not bundled")))
        }
    }
}

/// The frozen circuits and their families with bytecode from `store`: one
/// of the artifact stores a pipeline fold merges (with the other layers and
/// `noir_zk_backend::kernels::Kernels`).
pub fn artifacts<S: ArtifactStore>(store: S) -> Frozen<S> {
    Frozen::layered(circuits::REGISTRY, circuits::FAMILIES, store)
}

/// A family of this layer by name (`dsc`, `sod`, `document`).
pub fn family(name: &str) -> Option<&'static noir_zk_core::FamilyEntry> {
    circuits::FAMILIES
        .iter()
        .find(|f| f.id.layer == "eid" && f.id.family == name)
}

#[cfg(test)]
mod tests {
    use super::*;
    use noir_zk_core::registry::Status;
    use noir_zk_core::{ChonkRole, ProofSystem};

    /// Every bundled asset passes the pin check (feature `bundled`).
    #[cfg(feature = "bundled")]
    #[test]
    fn bundled_assets_match_their_pins() {
        use noir_zk_core::Artifacts;
        let frozen = artifacts(bundled::BundledStore);
        for (asset, _) in bundled::ASSETS {
            let label = asset.split('@').next().unwrap();
            assert!(frozen.bytecode_b64(label).is_ok(), "{asset}");
        }
    }

    /// 296 apps, no kernels; the families are exactly the three steps' variants.
    #[test]
    fn registry_is_the_three_families() {
        let active: Vec<_> = circuits::REGISTRY
            .iter()
            .filter(|e| e.status == Status::Active)
            .collect();
        assert_eq!(active.len(), 296);
        assert!(active
            .iter()
            .all(|e| e.system == ProofSystem::Chonk(ChonkRole::App)));
        let members = |name: &str| family(name).unwrap().members.len();
        assert_eq!(
            (members("dsc"), members("sod"), members("document")),
            (124, 124, 48)
        );
        assert_eq!(
            (circuits::LIBRARY.name, circuits::LIBRARY.version),
            ("eid-circuits", "0.8.2")
        );
        for f in circuits::FAMILIES {
            for (label, _) in f.members {
                assert!(
                    label.starts_with(&format!("{}_", f.id.family)),
                    "{label} in {}",
                    f.id
                );
            }
        }
        let d = family("document").unwrap();
        assert_eq!(d.link_in.unwrap().link, "CB");
        assert_eq!(d.link_out.unwrap().link, "PayloadCommitment");
        assert_eq!(d.slots, ["date", "scope", "nullifier"]);
    }
}