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
|
use std::io::{self, Write};
use std::process::{Command, Stdio};
use zkvms_host_io::{
benchmarkable, foreach_private_input_field, foreach_public_input_field, read_args,
PrivateInput, PublicInput,
RunType::{Execute, Prove, Verify},
RunWith,
};
static PUBLIC_INPUT_PATH: &str = "public_input.bin";
static PRIVATE_INPUT_PATH: &str = "private_input.bin";
/// Inserts array sizes before every square bracket
///
/// # Example
///
/// If `flat` is "[[0,1], [2,3,4], []]"
/// Output will be "3[2[0,1], 3[2,3,4], 0[]]"
fn get_with_sizes(flat: &str) -> String {
let mut values = flat.split('[').map(|x| x.trim()).skip(1);
let current = values.next().unwrap_or(flat);
// 1D collection or not a collection
if current != "" {
let size = 1 + current
.clone()
.to_string()
.chars()
.take_while(|x| *x != ']')
.map(|x| (x == ',') as usize)
.sum::<usize>();
(if size > 1 {
size.to_string()
} else {
String::new()
}) + "["
+ current
+ &values.map(|x| "[".to_string() + x).collect::<String>()
}
// ND collection
else {
let size: usize = values.clone().count();
let subcollections = values.map(|x| get_with_sizes(x)).collect::<String>();
size.to_string() + "[" + &subcollections
}
}
/// Creates an anonymous function which takes `run_info`, "serializes" the
/// specified input, outputs it into a file and returns a "path:<PATH>"
/// argument, ready to be passed to zkWasm.
///
/// The macro takes three arguments: run_info input expression, path for file
/// output and the name of a foreach macro.
///
/// For collection types, first the size is emitted and afterwards its actual
/// values.
macro_rules! build_input {
($input:expr , $path:ident , $type:ident) => {
|run_info: &RunWith| {
let mut ret: Vec<u64> = Vec::new();
$type! {
// Simplify input string
let flat = format!("{:?}", $input.yield)
.replace("false", "0")
.replace("true", "1")
.replace('(', "[")
.replace(')', "]")
.replace('{', "[")
.replace('}', "]");
let flat = get_with_sizes(&flat);
let values = flat
.replace('[', ",")
.replace(']', " ")
.replace(':', ",")
.split(',')
.map(|val| {
let val = val.trim();
if let Some(num) = val.parse::<u64>().ok() {
vec![num]
}
else {
let val = val.trim_matches('"');
let mut size = vec![val.len() as u64];
size.extend(val
.bytes()
.into_iter()
.map(|x| x as u64)
.collect::<Vec<u64>>());
size
}
})
.flatten()
.collect::<Vec<u64>>();
ret.extend(values);
}
let bytes = ret
.iter()
.map(|x| x.to_be_bytes())
.flatten()
.collect::<Vec<u8>>();
std::fs::write($path, bytes);
format!("{}:file", $path)
}
};
}
fn zkwasm_command(subcmd: &str) -> Command {
let mut command = Command::new("zkwasm-cli");
command
.arg("--params")
.arg("./params")
.arg("prog")
.arg(subcmd)
.arg("--wasm")
.arg(env!("GUEST_PATH"));
command
}
fn run(cmd: &mut Command) {
assert!(cmd.status().expect("couldn't execute command!").success());
}
fn main() {
let run_info = read_args();
let k = run_info.env_or("ZKWASM_K", "19");
let scheme = run_info.env_or("ZKWASM_SCHEME", "shplonk");
run(zkwasm_command("setup")
.arg("-k")
.arg(k)
.arg("--scheme")
.arg(scheme));
let public_input = build_input!(
run_info.public_input,
PUBLIC_INPUT_PATH,
foreach_public_input_field
)(&run_info);
let private_input = build_input!(
run_info.private_input,
PRIVATE_INPUT_PATH,
foreach_private_input_field
)(&run_info);
let output = run_info.env_or("ZKWASM_OUTPUT", "./output");
let params = run_info.env_or("ZKWASM_PARAMS", "./params");
match run_info.run_type {
Execute => benchmarkable! {
run(zkwasm_command("dry-run")
.arg("--public").arg(public_input.clone())
.arg("--private").arg(private_input.clone())
.arg("--output").arg(output.clone()));
},
Prove => benchmarkable! {
run(zkwasm_command("prove")
.arg("--public").arg(public_input.clone())
.arg("--private").arg(private_input.clone())
.arg("--output").arg(output.clone()));
},
Verify => {
run(zkwasm_command("prove")
.arg("--public")
.arg(public_input)
.arg("--private")
.arg(private_input)
.arg("--output")
.arg(output.clone()));
benchmarkable! {
run(Command::new("zkwasm-cli")
.arg("--params").arg(params.clone())
.arg("prog").arg("verify")
.arg("--output").arg(output.clone()));
}
}
}
}
|