verity_dp_zk_host/builder.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
15use std::{collections::HashMap, env};
16
17use risc0_build::{embed_methods_with_options, DockerOptions, GuestOptions};
18use risc0_build_ethereum::generate_solidity_files;
19
20use dotenv;
21
22/// Parameters for generating Solidity code.
23pub struct SolidityBuildParams {
24 /// Path to save the generated ImageID.sol file after building.
25 pub solidity_image_id_path: String,
26 /// Path to save the ELF.sol file after building.
27 pub solidity_elf_path: String,
28}
29
30impl Default for SolidityBuildParams {
31 fn default() -> Self {
32 Self {
33 solidity_image_id_path: String::from("../contracts/ImageID.sol"),
34 solidity_elf_path: String::from("../tests/Elf.sol"),
35 }
36 }
37}
38
39/// Executes as a build hook in the guest repository.
40/// This function builds the necessary Solidity files to identify the circuit.
41/// Ensure it is added as a build dependency.
42pub fn build_for_evm(build_params: SolidityBuildParams) {
43 dotenv::dotenv().ok();
44
45 // Builds can be made deterministic and reproducible by using Docker to build the
46 // guest. Check the RISC0_USE_DOCKER variable and use Docker to build the guest if set.
47 let use_docker = env::var("RISC0_USE_DOCKER").ok().map(|_| DockerOptions {
48 root_dir: Some("../".into()),
49 });
50
51 // Generate Rust source files for the methods crate.
52 let guests = embed_methods_with_options(HashMap::from([(
53 "guests",
54 GuestOptions {
55 features: Vec::new(),
56 use_docker,
57 },
58 )]));
59
60 // Generate Solidity source files for use with Forge.
61 let solidity_opts = risc0_build_ethereum::Options::default()
62 .with_image_id_sol_path(build_params.solidity_image_id_path)
63 .with_elf_sol_path(build_params.solidity_elf_path);
64
65 generate_solidity_files(guests.as_slice(), &solidity_opts).unwrap();
66}