-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmod.rs
More file actions
382 lines (347 loc) · 12.8 KB
/
mod.rs
File metadata and controls
382 lines (347 loc) · 12.8 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// SPDX-License-Identifier: CC0-1.0
//! The Simplicity Human-Readable Encoding
//!
//! This module provides the ability to decode and encode [`NamedCommitNode`]s
//! in a human-readable format.
//!
mod error;
mod named_node;
mod parse;
use crate::dag::{DagLike, MaxSharing};
use crate::jet::{Jet, JetEnvironment};
use crate::node::{self, CommitNode, NoWitness};
use crate::types;
use crate::{Cmr, ConstructNode, Ihr, Value};
use std::collections::HashMap;
use std::str;
use std::sync::Arc;
pub use self::error::{Error, ErrorSet};
pub use self::named_node::NamedCommitNode;
/// Line/column pair
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
pub struct Position {
line: usize,
column: usize,
}
impl Position {
/// Create a new position from line and column.
pub fn new(line: usize, column: usize) -> Self {
Position { line, column }
}
}
/// For named construct nodes, we abuse the `witness` combinator to support typed holes.
///
/// We do this because `witness` nodes have free source and target arrows, and
/// allow us to store arbitrary data in them using the generic witness type of
/// the node. Holes are characterized entirely by their source and target type,
/// just like witnesses, and are labelled by their name.
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub enum WitnessOrHole {
/// This witness is an actual witness combinator (with no witness data attached,
/// that comes later)
Witness,
/// This is a typed hole, with the given name.
TypedHole(Arc<str>),
}
impl WitnessOrHole {
pub fn shallow_clone(&self) -> Self {
match self {
WitnessOrHole::Witness => WitnessOrHole::Witness,
WitnessOrHole::TypedHole(name) => WitnessOrHole::TypedHole(Arc::clone(name)),
}
}
}
impl From<&'_ NoWitness> for WitnessOrHole {
fn from(_: &NoWitness) -> Self {
WitnessOrHole::Witness
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Forest<J: Jet> {
roots: HashMap<Arc<str>, Arc<NamedCommitNode<J>>>,
}
impl<J: Jet> Forest<J> {
/// Parses a forest from a string
pub fn parse<JE: JetEnvironment<Jet = J>>(s: &str) -> Result<Self, ErrorSet> {
parse::parse::<JE>(s).map(|roots| Forest { roots })
}
/// Parses a program from a bytestring
pub fn from_program(root: Arc<CommitNode<J>>) -> Self {
let root = NamedCommitNode::from_node(&root);
let mut roots = HashMap::new();
roots.insert("main".into(), root);
Forest { roots }
}
/// Accessor for the map of roots of this forest
pub fn roots(&self) -> &HashMap<Arc<str>, Arc<NamedCommitNode<J>>> {
&self.roots
}
/// Serialize the program in human-readable form
pub fn string_serialize(&self) -> String {
struct Print {
cmr: Cmr,
ihr: Option<Ihr>,
expr_str: String, // The X = Y part
arrow_str: String, // The :: A -> B part
}
fn print_lines(lines: &[Print], skip_before_end: bool) -> String {
let mut ret = String::new();
let expr_width = lines.iter().map(|line| line.expr_str.len()).max().unwrap();
let arrow_width = lines.iter().map(|line| line.arrow_str.len()).max().unwrap();
let last_line = lines.len();
for (n, line) in lines.iter().enumerate() {
ret += "\n";
if skip_before_end && n == last_line - 1 {
ret += "\n";
}
ret += &format!("-- CMR: {}\n", line.cmr);
if let Some(ihr) = line.ihr {
ret += &format!("-- IHR: {}\n", ihr);
} else {
ret += "-- IHR: [undetermined]\n";
}
ret += &format!(
"{0:1$} {2:3$}\n",
line.expr_str, expr_width, line.arrow_str, arrow_width,
);
}
ret
}
let mut witness_lines = vec![];
let mut const_lines = vec![];
let mut program_lines = vec![];
// Pass 1: compute string data for every node
for root in self.roots.values() {
for data in root.as_ref().post_order_iter::<MaxSharing<_>>() {
let node = data.node;
let name = node.name();
let mut expr_str = match node.inner() {
node::Inner::AssertR(cmr, _) => format!("{} := assertr #{}", name, cmr),
node::Inner::Fail(entropy) => format!("{} := fail {}", name, entropy),
node::Inner::Jet(ref j) => format!("{} := jet_{}", name, j),
node::Inner::Word(ref word) => {
format!("{} := const {}", name, word)
}
inner => format!("{} := {}", name, inner),
};
if let Some(child) = node.left_child() {
expr_str.push(' ');
expr_str.push_str(child.name());
}
if let Some(child) = node.right_child() {
expr_str.push(' ');
expr_str.push_str(child.name());
} else if let node::Inner::AssertL(_, cmr) = node.inner() {
expr_str.push_str(" #");
expr_str.push_str(&cmr.to_string());
} else if let node::Inner::Disconnect(_, hole_name) = node.inner() {
expr_str.push_str(&format!(" ?{}", hole_name));
}
let arrow = node.arrow();
let arrow_str = format!(": {} -> {}", arrow.source, arrow.target).replace('×', "*"); // for human-readable encoding stick with ASCII
let print = Print {
cmr: node.cmr(),
ihr: node.ihr(),
expr_str,
arrow_str,
};
if let node::Inner::Witness(..) = node.inner() {
witness_lines.push(print);
} else if let node::Inner::Word(..) = node.inner() {
const_lines.push(print);
} else {
program_lines.push(print);
}
}
}
// Pass 2: actually print everything
let mut ret = String::new();
if !witness_lines.is_empty() {
ret += "--------------\n-- Witnesses\n--------------\n";
ret += &print_lines(&witness_lines, false);
ret += "\n";
}
if !const_lines.is_empty() {
// FIXME detect scribes
ret += "--------------\n-- Constants\n--------------\n";
ret += &print_lines(&const_lines, false);
ret += "\n";
}
if !program_lines.is_empty() {
ret += "--------------\n-- Program code\n--------------\n";
ret += &print_lines(&program_lines, true /* add a blank line before main */);
}
ret
}
/// Convert the forest into a witness node.
///
/// Succeeds if the forest contains a "main" root and returns `None` otherwise.
pub fn to_witness_node<'brand>(
&self,
inference_context: &types::Context<'brand>,
witness: &HashMap<Arc<str>, Value>,
) -> Option<Arc<ConstructNode<'brand, J>>> {
let main = self.roots.get("main")?;
Some(main.to_construct_node(inference_context, witness, self.roots()))
}
}
#[cfg(test)]
mod tests {
use crate::human_encoding::Forest;
use crate::jet::{CoreEnv, JetEnvironment};
use crate::types;
use crate::{BitMachine, Value};
use std::collections::HashMap;
use std::sync::Arc;
fn assert_finalize_ok<JE: JetEnvironment>(
s: &str,
witness: &HashMap<Arc<str>, Value>,
env: &JE,
) {
types::Context::with_context(|ctx| {
let program = Forest::parse::<JE>(s)
.expect("Failed to parse human encoding")
.to_witness_node(&ctx, witness)
.expect("Forest is missing expected root")
.finalize_pruned(env)
.expect("Failed to finalize");
let mut mac = BitMachine::for_program(&program).expect("program has reasonable bounds");
mac.exec(&program, env).expect("Failed to run program");
});
}
fn assert_finalize_err<JE: JetEnvironment>(
s: &str,
witness: &HashMap<Arc<str>, Value>,
env: &JE,
err_msg: &'static str,
) {
types::Context::with_context(|ctx| {
let program = match Forest::<JE::Jet>::parse::<JE>(s)
.expect("Failed to parse human encoding")
.to_witness_node(&ctx, witness)
.expect("Forest is missing expected root")
.finalize_pruned(env)
{
Ok(program) => program,
Err(error) => {
assert_eq!(&error.to_string(), err_msg);
return;
}
};
let mut mac = BitMachine::for_program(&program).expect("program has reasonable bounds");
match mac.exec(&program, env) {
Ok(_) => panic!("Execution is expected to fail"),
Err(error) => assert_eq!(&error.to_string(), err_msg),
}
});
}
#[test]
fn executed_witness_with_value() {
let s = "
a := witness
b := witness
main := comp
comp
pair a b
jet_lt_8
jet_verify
";
let a_less_than_b = HashMap::from([
(Arc::from("a"), Value::u8(0x00)),
(Arc::from("b"), Value::u8(0x01)),
]);
assert_finalize_ok::<CoreEnv>(s, &a_less_than_b, &CoreEnv::new());
let b_greater_equal_a = HashMap::from([
(Arc::from("a"), Value::u8(0x01)),
(Arc::from("b"), Value::u8(0x01)),
]);
assert_finalize_err::<CoreEnv>(
s,
&b_greater_equal_a,
&CoreEnv::new(),
"Jet failed during execution",
);
}
#[test]
fn executed_witness_without_value() {
let witness = HashMap::from([(Arc::from("wit1"), Value::u32(1337))]);
assert_finalize_err::<CoreEnv>(
"
wit1 := witness : 1 -> 2^32
wit2 := witness : 1 -> 2^32
wits_are_equal := comp (pair wit1 wit2) jet_eq_32 : 1 -> 2
main := comp wits_are_equal jet_verify : 1 -> 1
",
&witness,
&CoreEnv::new(),
"Jet failed during execution",
);
}
#[test]
fn pruned_witness_without_value() {
let s = "
wit1 := witness : 1 -> 2
wit2 := witness : 1 -> 2^64
input := pair wit1 unit : 1 -> 2 * 1
process := case (drop injr unit) (drop comp wit2 jet_all_64) : 2 * 1 -> 2
main := comp input comp process jet_verify : 1 -> 1
";
let wit2_is_pruned = HashMap::from([(Arc::from("wit1"), Value::u1(0))]);
assert_finalize_ok::<CoreEnv>(s, &wit2_is_pruned, &CoreEnv::new());
let wit2_is_missing = HashMap::from([(Arc::from("wit1"), Value::u1(1))]);
assert_finalize_err::<CoreEnv>(
s,
&wit2_is_missing,
&CoreEnv::new(),
"Jet failed during execution",
);
let wit2_is_present = HashMap::from([
(Arc::from("wit1"), Value::u1(1)),
(Arc::from("wit2"), Value::u64(u64::MAX)),
]);
assert_finalize_ok::<CoreEnv>(s, &wit2_is_present, &CoreEnv::new());
}
#[test]
fn executed_hole_with_value() {
let empty = HashMap::new();
assert_finalize_ok::<CoreEnv>(
"
id1 := iden : 2^256 * 1 -> 2^256 * 1
main := comp (disconnect id1 ?hole) unit
hole := unit
",
&empty,
&CoreEnv::new(),
);
}
#[test]
fn executed_hole_without_value() {
let empty = HashMap::new();
assert_finalize_err::<CoreEnv>(
"
wit1 := witness
main := comp wit1 comp disconnect iden ?dis2 unit
",
&empty,
&CoreEnv::new(),
"disconnect node had one child (redeem time); must have two",
);
}
#[test]
fn witness_name_override() {
let s = "
wit1 := witness : 1 -> 2
wit2 := wit1 : 1 -> 2
main := comp wit2 jet_verify : 1 -> 1
";
let wit1_populated = HashMap::from([(Arc::from("wit1"), Value::u1(1))]);
assert_finalize_err::<CoreEnv>(
s,
&wit1_populated,
&CoreEnv::new(),
"Jet failed during execution",
);
let wit2_populated = HashMap::from([(Arc::from("wit2"), Value::u1(1))]);
assert_finalize_ok::<CoreEnv>(s, &wit2_populated, &CoreEnv::new());
}
}