MCPcopy Index your code
hub / github.com/ChainSafe/Spectre

github.com/ChainSafe/Spectre @v1.0.4

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.0.4 ↗ · + Follow
280 symbols 683 edges 50 files 62 documented · 22%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Spectre

Spectre is a Zero-Knowledge (ZK) coprocessor designed to offload intensive computations from the resource-limited execution layer of target chains. Iit offers a trust-minimized method for verifying block headers, adhering to the consensus rules of the originating chain.

The type of outsourced computation is specific to the arithmetic circuits. For Spectre, its primary function is to verify the Ethereum LightClient protocol introduced in the Altair hardfork.

Requirements

  • Rust 1.73.0-nightly
  • Packages build-essential clang pkg-config libssl-dev
  • Foundry
  • Just

Technical details

Spectre prover utilizes the Halo2 proving stack (privacy-scaling-explorations/halo2 fork).

Circuits are implemented with the halo2-lib circuit development framework. This library contains a number of non-trivial optimization tricks, while its readable SDK prevents most of the soundness bugs and improves auditability. Our team has contributed a number of features back to the halo2-lib repository, containing some foundational cryptographic primitives powering Ethereum consensus.

Verifier contracts for consensus proofs are auto-generated via the privacy-scaling-explorations/snark-verifier. We aslo support privacy-scaling-explorations/halo2-solidity-verifier behind experimental flag. Supplemental contract logic has been introduced exclusively to manage intermediary states during proof verifications.

Usage

Before build

Spectre uses abigen macro to generate smart contract types and rstest for consensus spec tests. To ensure that all the necessary data for macros expansion is present during first build or testing, please run:

git submodule update --init --recursive
just build-contracts
just download-spec-tests

Setup circuits

Step circuit

cargo run -r -p spectre-prover -- circuit sync-step-compressed -k 20 -p ./build/sync_step_20.pkey -K 23 -P ./build/sync_step_verifier_23.pkey -L 19 setup

Flags -k and -K are circuit degrees for first and aggregation (compression) stage respectively. -L is the number lookup bits used in aggregation stage.

Committee update circuit

cargo run -r -p spectre-prover -- circuit committee-update -k 20 -p ./build/committee_update_20.pkey -K 24 -P ./build/committee_update_verifier_20.pkey setup

Alternatively, you can use just recipes as shown below.

just setup-step-compressed testnet
just setup-committee-update testnet

Generates verifier contracts

Step proof

cargo run -r -p spectre-prover -- circuit sync-step-compressed -p ./build/sync_step_20.pkey -P ./build/sync_step_verifier_23.pkey gen-verifier -o ./contracts/snark-verifiers/sync_step_verifier.sol

Committee update proof

cargo run -r -p spectre-prover -- circuit committee-update -p ./build/committee_update_20.pkey -P ./build/committee_update_verifier_24.pkey gen-verifier -o ./contracts/snark-verifiers/committee_update_verifier.sol

Or use just recipes as shown below.

just gen-verifier-step-compressed testnet
just gen-verifier-committee-update testnet

Deploying contracts

Just scripts are provided to deploy the contracts either to a local testnet, or public networks.

For either make a copy of the .env.example file called .env. Set the INITIAL_SYNC_PERIOD, INITIAL_COMMITTEE_POSEIDON and SLOTS_PER_PERIOD variables according to the network you want Spectre to act as a light-client for and the starting point.

To get the INITIAL_COMMITTEE_POSEIDON value, run:

cargo run -r -p spectre-prover -- utils committee-poseidon --beacon-api https://lodestar-sepolia.chainsafe.io

--beacon-api is a URL of the RPC of the targeted Beacon chain.

Deploying locally

  1. Start a local anvil instance with:
anvil
  1. Copy one of the private key strings printed into the DEPLOYER_PRIVATE_KEY in the .env file then run
just deploy-contracts-local

Deploying to a public network

  1. Obtain the required gas token and obtain the private key for the deployer account. Set the DEPLOYER_PRIVATE_KEY in the .env file.
  2. Obtain a public RPC URL for the network and set the variable <NETWORK>_RPC_URL in the .env file (If using Infura this will require an API key)
  3. Run
just deploy-contracts <NETWORK>

where <NETWORK> is one of ["GOERLI", "SEPOLIA", "MAINNET"].

Running the prover

Prover is accessible via JSON RPC interface. To start it, run:

cargo run -r -p spectre-prover -- rpc --port 3000 --spec testnet

where --spec is one of ["testnet", "mainnet"].

Extension points exported contracts — how you extend this code

Spec (Interface)
Beacon chain specification. [3 implementers]
eth-types/src/spec.rs
GateBuilderConfig (Interface)
Custom config for a custom gate builder. [2 implementers]
lightclient-circuits/src/util/gates.rs
CommonGateManager (Interface)
Thin abstraction over a gate a `VirtualRegionManager`. [2 implementers]
lightclient-circuits/src/util/gates.rs
Halo2ConfigPinning (Interface)
Halo2 circuit configuration parameters. [2 implementers]
lightclient-circuits/src/util/circuit.rs
AppCircuit (Interface)
(no doc) [3 implementers]
lightclient-circuits/src/util/circuit.rs
WitnessFrom (Interface)
(no doc) [3 implementers]
lightclient-circuits/src/util/conversion.rs

Core symbols most depended-on inside this repo

main
called by 119
lightclient-circuits/src/gadget/crypto/builder.rs
map
called by 100
lightclient-circuits/src/witness/hashing.rs
to_vec
called by 48
lightclient-circuits/src/witness/hashing.rs
into_iter
called by 37
lightclient-circuits/src/witness/hashing.rs
into_witness
called by 19
lightclient-circuits/src/util/conversion.rs
spread
called by 18
lightclient-circuits/src/gadget/crypto/sha256_flex/spread.rs
instances
called by 13
lightclient-circuits/src/gadget/crypto/builder.rs
state_to_spread_u32
called by 10
lightclient-circuits/src/gadget/crypto/sha256_flex/compression.rs

Shape

Method 115
Function 109
Class 38
Interface 10
Enum 8

Languages

Rust100%

Modules by API surface

lightclient-circuits/src/gadget/crypto/builder.rs37 symbols
lightclient-circuits/src/util/circuit.rs20 symbols
lightclient-circuits/src/witness/multiproof.rs16 symbols
lightclient-circuits/src/gadget/crypto/sha256_wide/gate.rs13 symbols
lightclient-circuits/src/sync_step_circuit.rs12 symbols
lightclient-circuits/src/gadget/crypto/sha256_flex/compression.rs11 symbols
lightclient-circuits/src/committee_update_circuit.rs11 symbols
lightclient-circuits/src/witness/hashing.rs10 symbols
lightclient-circuits/src/gadget/crypto/sha256_flex/spread.rs10 symbols
lightclient-circuits/src/gadget/crypto/sha256_flex/gate.rs10 symbols
preprocessor/src/lib.rs8 symbols
prover/src/rpc_client.rs7 symbols

For agents

$ claude mcp add Spectre \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact