forked from yetanotherco/zkRust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
228 lines (200 loc) · 8.65 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use aligned_sdk::core::types::ProvingSystemId;
use clap::{Args, Parser, Subcommand};
use env_logger::Env;
use log::info;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use zkRust::risc0;
use zkRust::sp1;
use zkRust::submit_proof_to_aligned;
use zkRust::utils;
#[derive(Parser)]
#[command(version, about, long_about = None)]
#[command(propagate_version = true)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
#[clap(about = "Generate a proof of execution of a program using SP1")]
ProveSp1(ProofArgs),
#[clap(about = "Generate a proof of execution of a program using RISC0")]
ProveRisc0(ProofArgs),
}
#[derive(Args, Debug)]
struct ProofArgs {
guest_path: String,
#[clap(long)]
submit_to_aligned: bool,
#[clap(long, required_if_eq("submit_to_aligned", "true"))]
keystore_path: Option<PathBuf>,
#[clap(long, default_value("https://ethereum-holesky-rpc.publicnode.com"))]
rpc_url: String,
#[clap(long, default_value("17000"))]
chain_id: u64,
#[clap(long, default_value("100000000000000"))]
max_fee: u128,
#[clap(long)]
precompiles: bool,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let cli = Cli::parse();
match &cli.command {
Commands::ProveSp1(args) => {
info!("Proving with SP1, program in: {}", args.guest_path);
// Perform sanitation checks on directory
match utils::validate_directory_structure(&args.guest_path) {
Ok(_) => {
utils::prepare_workspace(
&args.guest_path,
sp1::SP1_SRC_DIR,
sp1::SP1_GUEST_CARGO_TOML,
"./workspaces/sp1/script",
"./workspaces/sp1/script/Cargo.toml",
sp1::SP1_BASE_HOST_CARGO_TOML,
sp1::SP1_BASE_GUEST_CARGO_TOML,
)?;
let imports = utils::get_imports(sp1::SP1_GUEST_MAIN).unwrap();
let function_bodies = utils::extract_function_bodies(
sp1::SP1_GUEST_MAIN,
vec![
"fn main()".to_string(),
"fn input()".to_string(),
"fn output()".to_string(),
],
)
.unwrap();
/*
Adds header to the guest & replace I/O imports
risc0:
#![no_main]
sp1_zkvm::entrypoint!(main);
*/
utils::prepare_guest(
&imports,
&function_bodies[0],
sp1::SP1_GUEST_PROGRAM_HEADER,
sp1::SP1_IO_READ,
sp1::SP1_IO_COMMIT,
sp1::SP1_GUEST_MAIN,
)?;
sp1::prepare_host(&function_bodies[1], &function_bodies[2], &imports)?;
if args.precompiles {
let mut toml_file = OpenOptions::new()
.append(true) // Open the file in append mode
.open(sp1::SP1_GUEST_CARGO_TOML)?;
writeln!(toml_file, "{}", sp1::SP1_ACCELERATION_IMPORT)?;
}
if sp1::generate_sp1_proof()?.success() {
info!("SP1 proof and ELF generated");
utils::replace(sp1::SP1_GUEST_CARGO_TOML, sp1::SP1_ACCELERATION_IMPORT, "")
.unwrap();
// Submit to aligned
if args.submit_to_aligned {
submit_proof_to_aligned(
&args.keystore_path.as_ref().unwrap(),
sp1::SP1_PROOF_PATH,
sp1::SP1_ELF_PATH,
None,
&args.rpc_url,
&args.chain_id,
&args.max_fee,
ProvingSystemId::SP1,
)
.await
.expect("Failed to submit to Aligned");
info!("SP1 proof submitted and verified on aligned");
}
// Clear host & guest
std::fs::copy(sp1::SP1_BASE_HOST_FILE, sp1::SP1_HOST_MAIN).unwrap();
return Ok(());
}
info!("SP1 proof generation failed");
// Clear host
std::fs::copy(sp1::SP1_BASE_HOST_FILE, sp1::SP1_HOST_MAIN)?;
return Ok(());
}
Err(e) => return Err(e),
}
}
Commands::ProveRisc0(args) => {
info!("Proving with Risc0, program in: {}", args.guest_path);
// Perform sanitation checks on directory
match utils::validate_directory_structure(&args.guest_path) {
Ok(_) => {
utils::prepare_workspace(
&args.guest_path,
risc0::RISC0_SRC_DIR,
risc0::RISC0_GUEST_CARGO_TOML,
"./workspaces/risc0/host",
"./workspaces/risc0/host/Cargo.toml",
risc0::RISC0_BASE_HOST_CARGO_TOML,
risc0::RISC0_BASE_GUEST_CARGO_TOML,
)?;
let imports = utils::get_imports(risc0::RISC0_GUEST_MAIN).unwrap();
let function_bodies = utils::extract_function_bodies(
risc0::RISC0_GUEST_MAIN,
vec![
"fn main()".to_string(),
"fn input()".to_string(),
"fn output()".to_string(),
],
)
.unwrap();
/*
Adds header to the guest & replace I/O imports
risc0:
#![no_main]
risc0_zkvm::guest::entry!(main);
*/
utils::prepare_guest(
&imports,
&function_bodies[0],
risc0::RISC0_GUEST_PROGRAM_HEADER,
risc0::RISC0_IO_READ,
risc0::RISC0_IO_COMMIT,
risc0::RISC0_GUEST_MAIN,
)?;
risc0::prepare_host(&function_bodies[1], &function_bodies[2], &imports)?;
if args.precompiles {
let mut toml_file = OpenOptions::new()
.append(true)
.open(risc0::RISC0_GUEST_CARGO_TOML)?;
writeln!(toml_file, "{}", risc0::RISC0_ACCELERATION_IMPORT)?;
}
if risc0::generate_risc0_proof()?.success() {
info!("Risc0 proof and Image ID generated");
// Submit to aligned
if args.submit_to_aligned {
submit_proof_to_aligned(
&args.keystore_path.as_ref().unwrap(),
risc0::PROOF_FILE_PATH,
risc0::IMAGE_ID_FILE_PATH,
Some(risc0::PUBLIC_INPUT_FILE_PATH),
&args.rpc_url,
&args.chain_id,
&args.max_fee,
ProvingSystemId::Risc0,
)
.await
.expect("Failed to submit to Aligned");
info!("Risc0 proof submitted and verified on aligned");
}
// Clear Host file
std::fs::copy(risc0::RISC0_BASE_HOST_FILE, risc0::RISC0_HOST_MAIN).unwrap();
return Ok(());
}
info!("Risc0 proof generation failed");
// Clear Host file
std::fs::copy(risc0::RISC0_BASE_HOST_FILE, risc0::RISC0_HOST_MAIN).unwrap();
return Ok(());
}
Err(e) => return Err(e),
}
}
}
}