risc0_binfmt/
lib.rs

1// Copyright 2025 RISC Zero, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Manages formatted binaries used by the RISC Zero zkVM
16
17#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
18#![deny(missing_docs)]
19#![deny(rustdoc::broken_intra_doc_links)]
20#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
21
22mod addr;
23mod elf;
24mod exit_code;
25mod hash;
26mod image;
27mod povw;
28mod sys_state;
29
30use anyhow::Result;
31use risc0_zkp::core::digest::Digest;
32
33pub use self::image::{MemoryImage, Page, KERNEL_START_ADDR};
34
35pub use crate::{
36    addr::{ByteAddr, WordAddr},
37    elf::{AbiKind, Program, ProgramBinary, ProgramBinaryHeader},
38    exit_code::{ExitCode, InvalidExitCodeError},
39    hash::{tagged_iter, tagged_list, tagged_list_cons, tagged_struct, Digestible},
40    povw::{PovwJobId, PovwLogId, PovwNonce},
41    sys_state::{read_sha_halfs, write_sha_halfs, DecodeError, SystemState},
42};
43
44pub(crate) const WORD_SIZE: usize = 4;
45const PAGE_BYTES: usize = 1024;
46pub(crate) const PAGE_WORDS: usize = PAGE_BYTES / WORD_SIZE;
47
48/// Compute and return the ImageID of the specified combined user ELF + kernel ELF binary.
49pub fn compute_image_id(blob: &[u8]) -> Result<Digest> {
50    ProgramBinary::decode(blob)?.compute_image_id()
51}