开源代码见:
risc0-build-kernel模块:
依赖库有:
risc0-build-kernel模块,提供了接口函数:
pub fn compile(&mut self, output: &str) {
for src in self.files.iter() {
println!("cargo:rerun-if-changed={}", src.display());
}
for dep in self.deps.iter() {
println!("cargo:rerun-if-changed={}", dep.display());
}
match &self.kernel_type {
KernelType::Cpp => self.compile_cpp(output), //直接使用cc::Build
KernelType::Cuda => self.compile_cuda(output), //使用nvcc
KernelType::Metal => self.compile_metal(output), //使用xcrun
}
}
risc0-core模块:
依赖库有:
risc0-circuit-rv32im-sys模块:
针对不同的模式,会编译生成不同的库文件:
依赖库有:
risc0-circuit-rv32im-sys模块内的cxx目录、kernels/cuda、kernels/metal目录下的rv32im ZK电路文件,均由Zirgen编译生成。目前Zirgen未开源。
risc0-circuit-recursion-sys模块:
针对不同的模式,会编译生成不同的库文件:
依赖库有:
risc0-circuit-recursion-sys模块内的cxx目录、kernels/cuda、kernels/metal目录下的ZK递归电路文件,均由Zirgen编译生成。目前Zirgen未开源。
risc0-sys模块:
依赖库有:
risc0-zkvm-platform模块:
/// Size of a zkVM machine word in bytes.
/// 4 bytes (i.e. 32 bits) as the zkVM is an implementation of the rv32im ISA.
pub const WORD_SIZE: usize = core::mem::size_of::<u32>();
/// Size of a zkVM memory page.
pub const PAGE_SIZE: usize = 1024;
/// Standard IO file descriptors for use with sys_read and sys_write.
pub mod fileno {
pub const STDIN: u32 = 0;
pub const STDOUT: u32 = 1;
pub const STDERR: u32 = 2;
pub const JOURNAL: u32 = 3;
}
依赖的库有:
risc0-zkvm-receipts模块:
risc0-zkp模块:
risc0-zkvm-methods模块:
有2大类methods:【未包含risc0-zkvm-methods-rand?】
均会调用risc0-build模块的embed_methods_with_options
函数,来生成methods.rs文件——嵌入为RISC-V构建的methods,供host端依赖项使用。
let map = HashMap::from([
(
"risc0-zkvm-methods-guest",
GuestOptions {
features: vec![],
use_docker,
},
),
(
"risc0-zkvm-methods-std",
GuestOptions {
features: vec!["test_feature1".to_string(), "test_feature2".to_string()],
use_docker: None,
},
),
]);
embed_methods_with_options(map);
依赖库有:
risc0-binfmt模块:
/// Represents the public state of a segment, needed for continuations and
/// receipt verification.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct SystemState {
/// The program counter.
pub pc: u32,
/// The root hash of a merkle tree which confirms the
/// integrity of the memory image.
pub merkle_root: Digest,
}
依赖库有:
RISC Zero program结构为:【对应load_elf函数为:从ELF文件加载初始化RISC Zero program。】
/// A RISC Zero program
pub struct Program {
/// The entrypoint of the program
pub entry: u32,
/// The initial memory image
pub image: BTreeMap<u32, u32>,
}
zkVM guest程序的memory image定义为:
/// An image of a zkVM guest's memory
///
/// This is an image of the full memory state of the zkVM, including the data,
/// text, inputs, page table, and system memory. In addition to the memory image
/// proper, this includes some metadata about the page table.
#[derive(Clone, Serialize, Deserialize)]
pub struct MemoryImage {
/// Sparse memory memory image as a map from page index to page.
pub pages: BTreeMap<u32, Vec<u8>>,
/// Metadata about the structure of the page table
pub info: PageTableInfo,
/// Program Counter from [Program] entry point
pub pc: u32,
}
相应的ImageID计算方式为:
/// Compute and return the ImageID of the specified ELF binary.
#[cfg(not(target_os = "zkvm"))]
pub fn compute_image_id(elf: &[u8]) -> anyhow::Result<risc0_zkp::core::digest::Digest> {
use risc0_zkvm_platform::{memory::GUEST_MAX_MEM, PAGE_SIZE};
let program = Program::load_elf(elf, GUEST_MAX_MEM as u32)?;
let image = MemoryImage::new(&program, PAGE_SIZE as u32)?;
image.compute_id()
}
/// Compute and return the root merkle entry of this image.
pub fn compute_root_hash(&self) -> Result<Digest> {
let root_page = self
.pages
.get(&self.info.root_idx)
.expect("Missing root page?");
hash_page_bytes(&root_page[..(self.info.root_addr - self.info.root_page_addr) as usize])
}
/// Compute and return the ImageID of this image.
pub fn compute_id(&self) -> Result<Digest> {
Ok(compute_image_id(&self.compute_root_hash()?, self.pc))
}
/// Compute and return the ImageID of the given `(merkle_root, pc)` pair.
fn compute_image_id(merkle_root: &Digest, pc: u32) -> Digest {
SystemState {
merkle_root: *merkle_root,
pc,
}
.digest::<Impl>()
}
risc0-build模块:
const RUSTUP_TOOLCHAIN_NAME: &str = "risc0";
#[derive(Debug, Deserialize)]
struct Risc0Metadata {
methods: Vec<String>,
}
struct Risc0Method {
name: String,
elf_path: PathBuf,
}
依赖库有:
cargo metadata
的输出。cargo metadata
:以 JSON 格式输出包结构和依赖关系信息。risc0-circuit-rv32im模块:
依赖库有:
risc0-circuit-recursion模块:
RECURSION_SRC_PATH
环境变量,下载链接为“https://risc0-artifacts.s3.us-west-2.amazonaws.com/zkr/{SHA256_HASH}.zip”。依赖库有:
risc0-zkvm模块:
cargo run --example fib --features="prove" -- --iterations 10
。risc0-zkvm支持如下features:【注意,若想在guest中使用risc0-zkvm,需通过设置default-features = false
来禁用"prove" feature。】
Feature | Target(s) | Implies | Description |
---|---|---|---|
client | all except rv32im | std | Enables the client API. |
cuda | prove, std | Enables CUDA GPU acceleration for the prover. Requires CUDA toolkit to be installed. | |
disable-dev-mode | all except rv32im | Disables dev mode so that proving and verifying may not be faked. Used to prevent a misplaced RISC0_DEV_MODE from breaking security in production systems. |
|
metal | macos | prove, std | Enables Metal GPU acceleration for the prover. |
prove | all except rv32im | std | Enables the prover, incompatible within the zkvm guest. |
std | all | Support for the Rust stdlib. |
依赖库有:
gimli
用Rust编写的跨平台符号库。