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
|
use proc_macro::TokenStream;
use std::{fs::File, io::Write};
mod parse_fn;
/// Create an `entrypoint_expr` macro inside the guest program. This will be
/// used inside the ZKVM guest program to call the ZKVM guest wrapper's
/// `make_wrapper` macro with entrypoint function type.
///
/// The overarching goal is to call `make_wrapper` with an input, which
/// contains the function definition. This can only happen inside the guest,
/// since that is the only place where we have the definition (syntactically).
/// But you can only pass data to macros by "inlining" it as macro arguments
/// (i.e. macros work on syntax, so creating a variable wouldn't work). Also,
/// the `make_wrapper` definition doesn't exist in the guest, but the ZKVM
/// wrapper crate. For these reasons we create a macro which invokes
/// `make_wrapper` with the proper arguments.
///
/// # Usage
///
/// Inside your guest (under guests directory) add an attribute above your main
/// (entrypoint/start) function. It takes no arguments.
///
/// ```rust
/// #[guests_macro::proving_entrypoint]
/// fn main(...) -> ... { ..... }
/// ```
///
/// # Example output
///
/// ```rust
/// #[macro_export]
/// macro_rules! entrypoint_expr {
/// () => {
/// make_wrapper!{fn main(...) -> ...}
/// };
/// }
/// ```
#[proc_macro_attribute]
pub fn proving_entrypoint(_: TokenStream, mut item: TokenStream) -> TokenStream {
let fd = parse_fn::FunctionDefinition::new(&item);
let fn_type = format!("fn {}{} -> {}", fd.name, fd.args, fd.return_type).replace('\n', " ");
// We also need to pass some type information to the host program compile-time.
// Put it in the file guests/type.txt.
let mut output = File::create("../type.txt").unwrap();
write!(output, "{fn_type}");
item.extend(
format!(
"#[macro_export]
macro_rules! entrypoint_expr {{
() => {{
make_wrapper!{{ {} }}
}};
}}",
fn_type
)
.parse::<TokenStream>(),
);
item
}
|