-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathprogram.rs
More file actions
183 lines (172 loc) · 7.88 KB
/
program.rs
File metadata and controls
183 lines (172 loc) · 7.88 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
// SPDX-License-Identifier: CC0-1.0
use std::sync::Arc;
use super::Extractor;
use simplicity::node::{
CoreConstructible as _, DisconnectConstructible as _, JetConstructible as _,
WitnessConstructible as _,
};
use simplicity::types;
use simplicity::{jet::Core, Cmr, ConstructNode, FailEntropy};
/// Structure controlling the kind of programs generated by the fuzzer.
pub struct ProgramControl {
/// Whether to attempt to insert "type bombs" into the generated program.
///
/// Importantly, type bombs may have 2^n nodes for fairly large n, and will
/// not respect `max_nodes`. So if you are trying to generate small programs
/// you should not enable this.
pub enable_type_bomb: bool,
/// Whether to attempt to insert disconnect nodes into the generated program.
pub enable_disconnect: bool,
/// Whether to attempt to insert witness nodes into the generated program.
pub enable_witness: bool,
/// Whether to attempt to insert fail nodes into the generated program.
pub enable_fail: bool,
/// Whether to attempt to insert assertl and assertr nodes into the generated program.
pub enable_asserts: bool,
/// Maximum number of nodes a generated program may have. This limit may not
/// be exactly enforced. If it is `None`, no limit is enforced.
pub max_nodes: Option<usize>,
}
impl ProgramControl {
fn from_u16(u: u16) -> Self {
ProgramControl {
enable_type_bomb: u & 0x8000 == 0x8000,
enable_disconnect: u & 0x4000 == 0x4000,
enable_witness: u & 0x2000 == 0x2000,
enable_fail: u & 0x1000 == 0x1000,
enable_asserts: u & 0x0800 == 0x0800,
max_nodes: Some(5 * usize::from(u & 0x07ff)),
}
}
}
impl Extractor<'_> {
pub fn extract_core_construct_node(
&mut self,
force_control: Option<ProgramControl>,
) -> Option<Arc<ConstructNode<Core>>> {
type ArcNode = Arc<ConstructNode<Core>>;
let ctx = types::Context::new();
let mut stack: Vec<ArcNode> = vec![];
let program_control =
force_control.unwrap_or(ProgramControl::from_u16(self.extract_u16()?));
let mut count = 0usize;
for _ in 0..program_control.max_nodes.unwrap_or(usize::MAX) {
let control = self.extract_u8()?;
if program_control.enable_type_bomb && control & 0x80 == 0x80 {
let mut ret = stack.pop()?;
// Special-case: type bomb. Iterate x -> pair(x, x) on the top stack
// item up to 128 times. Its CPU cost and target type will blow up
// by a factor 2^128. If its target type has nonzero size this should
// fail to construct; if it's 0 we should be able to construct it but
// the bit machine should reject it.
for _ in 0..control & 0x7f {
// FIXME should we refuse to make the type-bomb if `ret` contains any
// witness or disconnect nodes? In this case the encoding of our
// CommitNode won't round-trip, since we're force-sharing both children
// of this `pair` but when decoding `CommitNode` we reject anything that
// shares witnesses or disconnects, which at commit-time we treat as
// being unique and never shared.
ret = ArcNode::pair(&ret, &ret).unwrap();
}
stack.push(ret);
} else {
match control {
// Return whatever we've got (note that this will "waste" everything else
// on the stack)
0 => {
if stack.len() == 1 {
return stack.pop();
} else {
return None;
}
}
// 1 through 63
1 => stack.push(ArcNode::unit(&ctx)),
2 => stack.push(ArcNode::iden(&ctx)),
3 => {
use simplicity::dag::DagLike as _;
let val = self.extract_value_direct()?;
if program_control.max_nodes.is_some() {
for _ in val.as_ref().pre_order_iter::<simplicity::dag::NoSharing>() {
count = count.checked_add(1)?;
}
}
if let Some(max) = program_control.max_nodes {
if val.compact_len() > max {
return None;
}
}
stack.push(ArcNode::scribe(&ctx, &val));
}
4 if program_control.enable_witness => stack.push(ArcNode::witness(&ctx, None)),
5 => {
let child = stack.pop()?;
stack.push(ArcNode::injl(&child));
}
6 => {
let child = stack.pop()?;
stack.push(ArcNode::injr(&child));
}
7 => {
let child = stack.pop()?;
stack.push(ArcNode::drop_(&child));
}
8 => {
let child = stack.pop()?;
stack.push(ArcNode::take(&child));
}
9 => {
let child = stack.pop()?;
let cmr_u8 = self.extract_u8()?;
let cmr = Cmr::from_byte_array([cmr_u8; 32]);
stack.push(ArcNode::assertl(&child, cmr).ok()?);
}
10 => {
let child = stack.pop()?;
let cmr_u8 = self.extract_u8()?;
let cmr = Cmr::from_byte_array([cmr_u8; 32]);
stack.push(ArcNode::assertr(cmr, &child).ok()?);
}
11 if program_control.enable_fail => {
let fail_u8 = self.extract_u8()?;
let fail = FailEntropy::from_byte_array([fail_u8; 64]);
stack.push(ArcNode::fail(&ctx, fail));
}
12 => {
let rchild = stack.pop()?;
let lchild = stack.pop()?;
stack.push(ArcNode::pair(&lchild, &rchild).ok()?);
}
13 => {
let rchild = stack.pop()?;
let lchild = stack.pop()?;
stack.push(ArcNode::case(&lchild, &rchild).ok()?);
}
14 => {
let rchild = stack.pop()?;
let lchild = stack.pop()?;
stack.push(ArcNode::comp(&lchild, &rchild).ok()?);
}
15 if program_control.enable_disconnect => {
let child = stack.pop()?;
stack.push(ArcNode::disconnect(&child, &None).ok()?);
}
// We assume that the above cases did not cover 64-255, so that if we
// right-shift by 6 we can get all 4 values.
_ => {
let extra_bits = usize::from(control >> 6);
let idx = (extra_bits << 8) + usize::from(self.extract_u8()?);
stack.push(ArcNode::jet(&ctx, Core::ALL[idx % Core::ALL.len()]));
}
}
}
if let Some(max) = program_control.max_nodes {
count = count.checked_add(1)?;
if count > max {
return None;
}
}
}
None
}
}