-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmain.rs
More file actions
296 lines (269 loc) · 11.6 KB
/
main.rs
File metadata and controls
296 lines (269 loc) · 11.6 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use aligned_sdk::core::types::ProvingSystemId;
use clap::{Parser, Subcommand};
use env_logger::Env;
use log::error;
use log::info;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use tokio::io;
use zkRust::{risc0, sp1, submit_proof_to_aligned, utils, ProofArgs};
#[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),
}
#[tokio::main]
async fn main() -> io::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
let proof_data_dir = PathBuf::from(&args.proof_data_directory_path);
if !proof_data_dir.exists() {
std::fs::create_dir_all(proof_data_dir).unwrap_or(info!(
"Saving Proofs to: {:?}",
&args.proof_data_directory_path
));
}
if utils::validate_directory_structure(&args.guest_path) {
let Some(home_dir) = dirs::home_dir() else {
error!("Failed to locate home directory");
return Ok(());
};
let Ok(current_dir) = std::env::current_dir() else {
error!("Failed to locate current directory");
return Ok(());
};
let home_dir = home_dir.join(".zkRust");
utils::prepare_workspace(
&PathBuf::from(&args.guest_path),
&home_dir.join(sp1::SP1_SRC_DIR),
&home_dir.join(sp1::SP1_GUEST_CARGO_TOML),
&home_dir.join("workspaces/sp1/script"),
&home_dir.join("workspaces/sp1/script/Cargo.toml"),
&home_dir.join(sp1::SP1_BASE_HOST_CARGO_TOML),
&home_dir.join(sp1::SP1_BASE_GUEST_CARGO_TOML),
)?;
let Ok(imports) = utils::get_imports(&home_dir.join(sp1::SP1_GUEST_MAIN)) else {
error!("Failed to extract imports");
return Ok(());
};
let main_path = home_dir.join(sp1::SP1_GUEST_MAIN);
let Ok(function_bodies) = utils::extract_function_bodies(
&main_path,
vec![
"fn main()".to_string(),
"fn input()".to_string(),
"fn output()".to_string(),
],
) else {
error!("Failed to extract function bodies");
return Ok(());
};
/*
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,
&home_dir.join(sp1::SP1_GUEST_MAIN),
)?;
sp1::prepare_host(
&function_bodies[1],
&function_bodies[2],
&imports,
&home_dir.join(sp1::SP1_BASE_HOST),
&home_dir.join(sp1::SP1_HOST_MAIN),
)?;
if args.precompiles {
let mut toml_file = OpenOptions::new()
.append(true) // Open the file in append mode
.open(home_dir.join(sp1::SP1_GUEST_CARGO_TOML))?;
writeln!(toml_file, "{}", sp1::SP1_ACCELERATION_IMPORT)?;
}
let script_dir = home_dir.join(sp1::SP1_SCRIPT_DIR);
if sp1::generate_sp1_proof(&script_dir, ¤t_dir)?.success() {
info!("SP1 proof and ELF generated");
utils::replace(
&home_dir.join(sp1::SP1_GUEST_CARGO_TOML),
sp1::SP1_ACCELERATION_IMPORT,
"",
)?;
// Submit to aligned
if args.submit_to_aligned {
submit_proof_to_aligned(
sp1::SP1_PROOF_PATH,
sp1::SP1_ELF_PATH,
Some(sp1::SP1_PUB_INPUT_PATH),
args,
ProvingSystemId::SP1,
)
.await
.map_err(|e| {
error!("Proof not submitted to Aligned");
io::Error::other(e.to_string())
})?;
info!("SP1 proof submitted and verified on Aligned");
}
std::fs::copy(
home_dir.join(sp1::SP1_BASE_HOST_FILE),
home_dir.join(sp1::SP1_HOST_MAIN),
)
.map_err(|e| {
error!("Failed to clear SP1 host file");
e
})?;
return Ok(());
}
info!("SP1 proof generation failed");
// Clear host
std::fs::copy(
home_dir.join(sp1::SP1_BASE_HOST_FILE),
home_dir.join(sp1::SP1_HOST_MAIN),
)?;
return Ok(());
} else {
error!("zkRust directory structure invalid please consult the README",);
return Ok(());
}
}
Commands::ProveRisc0(args) => {
info!("Proving with Risc0, program in: {}", args.guest_path);
// Perform sanitation checks on directory
if utils::validate_directory_structure(&args.guest_path) {
let proof_data_dir = PathBuf::from(&args.proof_data_directory_path);
if !proof_data_dir.exists() {
std::fs::create_dir_all(proof_data_dir).unwrap_or(info!(
"Saving generated proofs to: {:?}",
&args.proof_data_directory_path
));
}
let Some(home_dir) = dirs::home_dir() else {
error!("Failed to locate home directory");
return Ok(());
};
let Ok(current_dir) = std::env::current_dir() else {
error!("Failed to locate current directory");
return Ok(());
};
let home_dir = home_dir.join(".zkRust");
utils::prepare_workspace(
&PathBuf::from(&args.guest_path),
&home_dir.join(risc0::RISC0_SRC_DIR),
&home_dir.join(risc0::RISC0_GUEST_CARGO_TOML),
&home_dir.join("workspaces/risc0/host"),
&home_dir.join("workspaces/risc0/host/Cargo.toml"),
&home_dir.join(risc0::RISC0_BASE_HOST_CARGO_TOML),
&home_dir.join(risc0::RISC0_BASE_GUEST_CARGO_TOML),
)?;
let Ok(imports) = utils::get_imports(&home_dir.join(risc0::RISC0_GUEST_MAIN))
else {
error!("Failed to extract imports");
return Ok(());
};
let main_path = home_dir.join(risc0::RISC0_GUEST_MAIN);
let Ok(function_bodies) = utils::extract_function_bodies(
&main_path,
vec![
"fn main()".to_string(),
"fn input()".to_string(),
"fn output()".to_string(),
],
) else {
error!("Failed to extract function bodies");
return Ok(());
};
/*
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,
&home_dir.join(risc0::RISC0_GUEST_MAIN),
)?;
risc0::prepare_host(
&function_bodies[1],
&function_bodies[2],
&imports,
&home_dir.join(risc0::RISC0_BASE_HOST),
&home_dir.join(risc0::RISC0_HOST_MAIN),
)?;
if args.precompiles {
let mut toml_file = OpenOptions::new()
.append(true)
.open(home_dir.join(risc0::RISC0_GUEST_CARGO_TOML))?;
writeln!(toml_file, "{}", risc0::RISC0_ACCELERATION_IMPORT)?;
}
let workspace_dir = home_dir.join(risc0::RISC0_WORKSPACE_DIR);
if risc0::generate_risc0_proof(&workspace_dir, ¤t_dir)?.success() {
info!("Risc0 proof and Image ID generated");
utils::replace(
&home_dir.join(risc0::RISC0_GUEST_CARGO_TOML),
risc0::RISC0_ACCELERATION_IMPORT,
"",
)?;
// Submit to aligned
if args.submit_to_aligned {
submit_proof_to_aligned(
risc0::PROOF_FILE_PATH,
risc0::IMAGE_ID_FILE_PATH,
Some(risc0::PUBLIC_INPUT_FILE_PATH),
args,
ProvingSystemId::Risc0,
)
.await
.map_err(|e| {
error!("Error submitting proofs to Aligned: {:?}", e);
io::Error::other(e.to_string())
})?;
info!("Risc0 proof submitted and verified on Aligned");
}
// Clear Host file
std::fs::copy(
home_dir.join(risc0::RISC0_BASE_HOST_FILE),
home_dir.join(risc0::RISC0_HOST_MAIN),
)
.map_err(|e| {
error!("Failed to clear Risc0 host file");
e
})?;
return Ok(());
}
info!("Risc0 proof generation failed");
// Clear Host file
std::fs::copy(
home_dir.join(risc0::RISC0_BASE_HOST_FILE),
home_dir.join(risc0::RISC0_HOST_MAIN),
)?;
return Ok(());
} else {
error!("zkRust directory structure incorrect please consult the README",);
return Ok(());
}
}
}
}