aboutsummaryrefslogtreecommitdiff
path: root/zkvms/nexus/wrapper_macro/src/lib.rs
blob: 218359b09361809632fbb09bfb3e025a339a1933 (plain) (blame)
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
use proc_macro::TokenStream;

#[path = "../../../../guests_macro/src/parse_fn.rs"]
mod parse_fn;
use crate::parse_fn::FunctionDefinition;

/// Creates a body, which reads all inputs, stores them in variables, then
/// writes the ones, defined as public in `default_public_input.toml` to the
/// journal and finally executes the guest entrypoint function with those
/// arguments, committing its output.
///
/// # Usage
///
/// Inside Nexus' guest (excluding the `entrypoint_expr` call):
///
/// ```rust
/// make_wrapper!{fn main(...) -> ...}
/// ```
///
/// # Example output
///
/// ```rust
/// {
///     let ... = read_private_input::<...>().unwrap();
///     let ... = read_private_input::<...>().unwrap();
///     ...
///     write_output::<...>(&...);
///     write_output::<...>(&...);
///     ...
///     write_output::<...>(&zkp::main(..., ..., ...));
/// }
/// ```
#[proc_macro]
pub fn make_wrapper(item: TokenStream) -> TokenStream {
    let fd = FunctionDefinition::new(&item);

    let mut out = TokenStream::new();
    out.extend(format!("let {} = read_public_input::<{}>().unwrap();", fd.grouped_public_patterns(), fd.grouped_public_types()).parse::<TokenStream>());
    out.extend(format!("let {} = read_private_input::<{}>().unwrap();", fd.grouped_private_patterns(), fd.grouped_private_types()).parse::<TokenStream>());

    out.extend(
        format!(
            "write_public_output::<{}>(&zkp::{}({}));",
            fd.return_type, fd.name, fd.grouped_patterns()
        )
        .parse::<TokenStream>(),
    );

    let mut block = TokenStream::new();
    block.extend(format!("{{ {} }}", out).parse::<TokenStream>());
    block
}