aboutsummaryrefslogtreecommitdiff
path: root/zkvms/risc0/wrapper_macro/src/lib.rs
blob: 42b8ca09828637f2cc45f47b037721c532d48f35 (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
53
54
55
56
57
58
59
60
61
use proc_macro::TokenStream;

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

/// Create a body, which reads all inputs, stores them in variables, then
/// commits 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 RISC0's guest (excluding the `entrypoint_expr` call):
///
/// ```rust
/// make_wrapper!{fn main(...) -> ...}
/// ```
///
/// # Example output
///
/// ```rust
/// {
///     let ... : ... = read();
///     let ... : ... = read();
///     ...
///     commit(&...);
///     commit(&...);
///     ...
///     commit(&zkp::main(..., ..., ...));
/// }
/// ```
#[proc_macro]
pub fn make_wrapper(item: TokenStream) -> TokenStream {
    let fd = FunctionDefinition::new(&item);

    let args = fd
        .arguments()
        .into_iter()
        .map(|x| format!("let {x} = read();"))
        .collect::<String>();

    let mut out = TokenStream::new();
    out.extend(args.parse::<TokenStream>());

    let commits = fd
        .public_patterns()
        .clone()
        .into_iter()
        .map(|x| format!("commit(&{x});"))
        .collect::<String>();
    out.extend(commits.parse::<TokenStream>());

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

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