risc0_circuit_recursion/zkr.rs
1// Copyright 2023 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// Control trees for various recursion programs
16
17use std::io::Read;
18
19use anyhow::{Context, Result};
20
21const ZKR_ZIP: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/recursion_zkr.zip"));
22
23/// Lookup and return the zkr recursion program as a vector of words.
24///
25/// ```rust
26/// let encoded_program = risc0_circuit_recursion::zkr::get_zkr("lift_20.zkr").unwrap();
27/// ```
28pub fn get_zkr(name: &str) -> Result<Vec<u32>> {
29 let mut zip = zip::ZipArchive::new(std::io::Cursor::new(ZKR_ZIP)).unwrap();
30 let mut f = zip
31 .by_name(name)
32 .with_context(|| format!("Failed to read {name}"))?;
33
34 let mut u8vec: Vec<u8> = Vec::new();
35 f.read_to_end(&mut u8vec)?;
36
37 Ok(Vec::from(bytemuck::cast_slice(u8vec.as_slice())))
38}
39
40/// Iterate over all provided zkr programs.
41///
42/// ```rust
43/// let listing = risc0_circuit_recursion::zkr::get_all_zkrs().unwrap();
44/// println!("{}", listing.into_iter().map(|(name, _)| name).collect::<Vec<_>>().join("\n"));
45/// ```
46pub fn get_all_zkrs() -> Result<Vec<(String, Vec<u32>)>> {
47 let mut zip = zip::ZipArchive::new(std::io::Cursor::new(ZKR_ZIP)).unwrap();
48 let files: Vec<String> = (0..zip.len())
49 .map(|idx| Ok(zip.by_index(idx)?.name().to_string()))
50 .collect::<Result<_>>()?;
51
52 files
53 .into_iter()
54 .map(|name| {
55 let mut f = zip.by_name(&name)?;
56
57 let mut u8vec: Vec<u8> = Vec::new();
58 f.read_to_end(&mut u8vec)?;
59
60 Ok((name, Vec::from(bytemuck::cast_slice(u8vec.as_slice()))))
61 })
62 .collect()
63}