{"id":25412272,"url":"https://github.com/datachainlab/zkdcap","last_synced_at":"2025-04-14T16:45:44.682Z","repository":{"id":276404665,"uuid":"926113448","full_name":"datachainlab/zkdcap","owner":"datachainlab","description":null,"archived":false,"fork":false,"pushed_at":"2025-04-10T03:05:07.000Z","size":7448,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-04-10T04:20:57.361Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/datachainlab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-02T15:33:58.000Z","updated_at":"2025-03-30T14:03:05.000Z","dependencies_parsed_at":"2025-02-08T02:29:57.159Z","dependency_job_id":"9234ad48-58c1-4c7d-9bc9-b9120a536d4c","html_url":"https://github.com/datachainlab/zkdcap","commit_stats":null,"previous_names":["datachainlab/zkdcap"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datachainlab%2Fzkdcap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datachainlab%2Fzkdcap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datachainlab%2Fzkdcap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datachainlab%2Fzkdcap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/datachainlab","download_url":"https://codeload.github.com/datachainlab/zkdcap/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248918713,"owners_count":21183240,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-16T11:18:34.793Z","updated_at":"2025-04-14T16:45:44.664Z","avatar_url":"https://github.com/datachainlab.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# zkDCAP\n\n[![test](https://github.com/datachainlab/zkdcap/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/datachainlab/zkdcap/actions/workflows/test.yml)\n\n## Overview\n\nzkDCAP is a guest program that runs inside the [RISC Zero zkVM](https://github.com/risc0/risc0), designed to verify Intel SGX/TDX DCAP quotes and generate a zero-knowledge proof (ZKP) of the result.\n\nSince verifying a quote directly on-chain (e.g., on Ethereum or other EVM-compatible blockchains) is extremely gas-intensive, zkDCAP significantly reduces gas costs by offloading the verification process to the zkVM and proving its validity through ZKP.\n\nThe verification logic is based on Intel's reference implementation: [SGX-TDX-DCAP-QuoteVerificationLibrary](https://github.com/intel/SGX-TDX-DCAP-QuoteVerificationLibrary/blob/812e0fa140a284b772b2d8b08583c761e23ec3b3/).\n\nzkDCAP is designed to minimize trust assumptions, requiring trust only in Intel's Root CA certificate. However, performing quote verification inside a zkVM and validating the result via ZKP introduces several challenges. This README outlines these challenges and our design solutions.\n\n## Design Considerations\n\n### Validity Period\n\n#### Background\n\nDCAP quote verification requires multiple components of collateral—including certificates, CRLs, and TCB info—to determine the platform's TCB status.\n\nEach collateral component comes with its own validity period. Verifiers must ensure that the **current time falls within all of these periods**.\n\nIf expired collateral (e.g., an outdated TCB info) is used, quotes from already compromised SGX environments may be mistakenly accepted.\n\n#### Our Approach\n\nIn a typical quote verification process, the verifier checks whether the current time is within the validity range of the collateral.  \n\nHowever, it is not practical to securely obtain a trusted current timestamp within the zkVM, as the timestamp provided by the prover may have been tampered with.\n\nTo solve this, zkDCAP adopts the following two-step approach:\n\n1. Inside the zkVM, it computes the **intersection of the validity periods** across all collateral components and includes this range in the output.\n2. The on-chain verifier (e.g., a smart contract) verifies that the **current block timestamp** falls within this range when verifying the ZKP.\n\nThis ensures that the validity check can be enforced **without requiring the verifier to trust any timestamp provided by the prover**.\n\n**Note:** Regarding collateral availability, [Intel documentation](https://cc-enabling.trustedservices.intel.com/intel-tdx-enabling-guide/02/infrastructure_setup/#collateral-caching-service) and our observation of PCS API behavior show that each collateral component typically has a validity period of ~30 days after download, which provides sufficient availability for verification.\n\n### TCB Evaluation Data Number (TCB-R Counter)\n\n#### Background\n\nIntel periodically updates TCB info for SGX/TDX platforms through **TCB Recovery** events.\n\nEach update is assigned a unique **TCB Evaluation Data Number**, also known as the **TCB-R Counter**.\n\nIntel's Provisioning Certification Service (PCS) API allows clients to retrieve TCB information corresponding to different TCB-R Counters using the `update` query parameter.  \nFor example, the `?update=standard` parameter may return an older TCB info version (commonly up to 12 months prior to the latest). This mechanism is intended to support transitional compatibility, but it introduces a potential security risk.\n\nBecause the TCB status of a platform is evaluated based on the TCB info version used during verification, using an outdated TCB-R Counter may lead to a false \"UpToDate\" result for platforms that have been deprecated or have since become vulnerable.\n\nTherefore, to ensure the correctness and freshness of the attestation, verifiers **must validate quotes against the latest available TCB-R Counter**.\n\n#### Our Approach\n\nzkDCAP includes in its output the **minimum TCB Evaluation Data Number (TCB-R Counter)** among those found in the collateral used during verification. This allows the on-chain verifier to determine which version of TCB info the TCB status is based on.\n\nBy comparing this value with the latest TCB-R Counter tracked on-chain, the verifier can ensure that the verification result reflects a sufficiently recent TCB update. This mechanism prevents acceptance of attestations based on outdated TCB data.\n\nFor example, in the LCP Client, zkDCAP is integrated with a mechanism that maintains and updates the latest TCB-R Counter.\n\nAs long as **at least one operator continuously fetches the latest TCB data from Intel PCS**, the LCP Client ensures that all operators perform verification using up-to-date TCB info.\n\nSee more at: [https://docs.lcp.network/protocol/lcp-client-zkdcap](https://docs.lcp.network/protocol/lcp-client-zkdcap)\n\n## Verifier Program\n\nThe guest program that performs DCAP quote verification inside the zkVM is implemented in [main.rs](./zkvm/risc0/guest/src/bin/main.rs).  \nIt reads a serialized quote, collateral, and the current timestamp as input, verifies the quote using the `verify_quote` function, and commits the verification result as a serialized output.\n\nThis program is compiled into a zkVM guest binary and used to generate the zero-knowledge proof (ZKP) that is verified on-chain.\n\n### Inputs\n\nzkDCAP's verifier program takes the following three inputs:\n\n1. **Quote**\n\n   - A DCAP Quote (SGX/TDX, version 3 or 4)\n   - Provided as `Quote::V3(QuoteV3)` or `Quote::V4(QuoteV4)`\n\n2. **QvCollateral**\n\n   The collateral required for DCAP quote verification, typically fetched via PCS/PCCS APIs. The following Rust struct defines the format of the `QvCollateral` input, which contains the collateral data used for verification:\n\n   ```rust\n   #[derive(Clone, Debug, Default)]\n   pub struct QvCollateral {\n       pub tcb_info_json: String,\n       pub qe_identity_json: String,\n       pub sgx_intel_root_ca_der: Vec\u003cu8\u003e,\n       pub sgx_tcb_signing_der: Vec\u003cu8\u003e,\n       pub sgx_intel_root_ca_crl_der: Vec\u003cu8\u003e,\n       pub sgx_pck_crl_der: Vec\u003cu8\u003e,\n   }\n   ```\n\n   - `tcb_info_json`: Platform TCB info (e.g., TCB levels, SVN)\n   - `qe_identity_json`: QE identity including MRSIGNER\n   - `sgx_intel_root_ca_der`: Intel SGX Root CA certificate (DER)\n   - `sgx_tcb_signing_der`: TCBInfo signing cert (DER)\n   - `sgx_intel_root_ca_crl_der` / `sgx_pck_crl_der`: CRLs for revocation checks\n\n3. **current_time**\n\n   - UNIX timestamp (in seconds), provided by the prover\n   - zkDCAP uses it only to **compute the intersection of collateral validity periods**\n   - The actual time check is performed by the verifier using **on-chain block timestamp**, without relying on the prover to supply a trusted timestamp\n\n### Output\n\n#### `QuoteVerificationOutput`\n\nThe result of the verification process, which includes all necessary data to validate the quote and the integrity of the collateral.\n\n```rust\n#[derive(Debug, Clone, PartialEq, Eq)]\npub struct QuoteVerificationOutput {\n    pub version: u16,\n    pub quote_version: u16,\n    pub tee_type: u32,\n    pub status: Status,\n    pub min_tcb_evaluation_data_number: u32,\n    pub fmspc: [u8; 6],\n    pub sgx_intel_root_ca_hash: [u8; 32],\n    pub validity: Validity,\n    pub quote_body: QuoteBody,\n    pub advisory_ids: Vec\u003cString\u003e,\n}\n```\n\nKey fields:\n\n1. **min_tcb_evaluation_data_number**  \n   Minimum TCB evaluation data number(TCB-R Counter) from the collateral used during verification.  \n   The verifier ensures that this value is greater than or equal to the on-chain configured minimum TCB-R Counter.\n\n2. **validity**  \n   Validity intersection (NotBefore / NotAfter) of all collateral.\n   The verifier checks if the current block timestamp falls within this range.\n\n3. **status** and **advisory_ids**  \n   The resulting TCB status and any associated Intel security advisory IDs (e.g., `INTEL-SA-xxxx`).\n\n4. **sgx_intel_root_ca_hash**  \n   Keccak-256 hash of the Intel SGX Root CA certificate. The verifier **MUST** ensure this matches the hash of the expected trusted root CA certificate.\n\n5. **quote_body**  \n   Includes enclave measurement (`MRENCLAVE`) and attributes from the verified quote.\n\nThis output, when verified together with the ZKP on-chain, enables gas-efficient and secure DCAP quote attestation with significantly reduced gas costs.\n\n## Security Audit\n\nzkDCAP was audited as part of its integration into the LCP Client.\n\nThe audit was conducted by [Quantstamp](https://quantstamp.com/). You can find the report here:\n[https://certificate.quantstamp.com/full/datachain-lcp-zk-dcap/15bac7cd-3b90-47c7-a25e-b0c3214c6630/index.html](https://certificate.quantstamp.com/full/datachain-lcp-zk-dcap/15bac7cd-3b90-47c7-a25e-b0c3214c6630/index.html)\n\n## Build Instructions\n\nTo build the zkDCAP guest program for RISC Zero, run the following command:\n\n```bash\nZKDCAP_RISC0_BUILD=1 cargo build -r --manifest-path=./zkvm/risc0/Cargo.toml\n```\n\nThis will generate the zkVM guest program file here: `./zkvm/risc0/artifacts/dcap-quote-verifier`\n\n**Note:** Ensure that you have the RISC Zero toolchain properly installed before building.\n\nAlso, the generated method ID is automatically written to `zkvm/risc0/src/methods.rs`, where it is embedded and used by the prover and verifier to ensure correct proof generation and verification.\n\n## License\n\nThis project is licensed under the **Apache 2.0 License**.\n\n## Contributing\n\nContributions are welcome! Please open issues or pull requests.\n\n## Acknowledgements\n\n- [RISC Zero](https://github.com/risc0/risc0): We thank the RISC Zero team for providing an excellent zkVM.\n- [dcap-rs](https://github.com/automata-network/dcap-rs): Our implementation was initially based on the dcap-rs codebase by Automata. We have since extended it significantly through architectural modifications, enhancements, and adaptations to support zkDCAP's design goals.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdatachainlab%2Fzkdcap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdatachainlab%2Fzkdcap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdatachainlab%2Fzkdcap/lists"}