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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use super::{hash_op, hash_seq, BaseElement, FieldElement, OpCode, OpHint, BASE_CYCLE_LENGTH};
use core::fmt;
use winter_utils::collections::BTreeMap;
const BLOCK_SUFFIX: [u8; 1] = [OpCode::Noop as u8];
const BLOCK_SUFFIX_OFFSET: usize = BASE_CYCLE_LENGTH - 1;
const LOOP_SKIP_BLOCK: [OpCode; 15] = [
OpCode::Not,
OpCode::Assert,
OpCode::Noop,
OpCode::Noop,
OpCode::Noop,
OpCode::Noop,
OpCode::Noop,
OpCode::Noop,
OpCode::Noop,
OpCode::Noop,
OpCode::Noop,
OpCode::Noop,
OpCode::Noop,
OpCode::Noop,
OpCode::Noop,
];
const LOOP_BLOCK_SUFFIX: [u8; 16] = [
OpCode::Not as u8,
OpCode::Assert as u8,
OpCode::Noop as u8,
OpCode::Noop as u8,
OpCode::Noop as u8,
OpCode::Noop as u8,
OpCode::Noop as u8,
OpCode::Noop as u8,
OpCode::Noop as u8,
OpCode::Noop as u8,
OpCode::Noop as u8,
OpCode::Noop as u8,
OpCode::Noop as u8,
OpCode::Noop as u8,
OpCode::Noop as u8,
OpCode::Noop as u8,
];
#[derive(Clone)]
pub enum ProgramBlock {
Span(Span),
Group(Group),
Switch(Switch),
Loop(Loop),
}
#[derive(Clone)]
pub struct Span {
op_codes: Vec<OpCode>,
op_hints: BTreeMap<usize, OpHint>,
}
#[derive(Clone)]
pub struct Group {
body: Vec<ProgramBlock>,
}
#[derive(Clone)]
pub struct Switch {
t_branch: Vec<ProgramBlock>,
f_branch: Vec<ProgramBlock>,
}
#[derive(Clone)]
pub struct Loop {
body: Vec<ProgramBlock>,
skip: Vec<ProgramBlock>,
}
impl ProgramBlock {
pub fn is_span(&self) -> bool {
matches!(self, ProgramBlock::Span(_))
}
}
impl fmt::Debug for ProgramBlock {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ProgramBlock::Span(block) => write!(f, "{:?}", block)?,
ProgramBlock::Group(block) => write!(f, "{:?}", block)?,
ProgramBlock::Switch(block) => write!(f, "{:?}", block)?,
ProgramBlock::Loop(block) => write!(f, "{:?}", block)?,
}
Ok(())
}
}
impl Span {
pub fn new(instructions: Vec<OpCode>, hints: BTreeMap<usize, OpHint>) -> Span {
let alignment = instructions.len() % BASE_CYCLE_LENGTH;
assert!(
alignment == BASE_CYCLE_LENGTH - 1,
"invalid number of instructions: expected one less than a multiple of {}, but was {}",
BASE_CYCLE_LENGTH,
instructions.len()
);
for (i, &op_code) in instructions.iter().enumerate() {
if op_code == OpCode::Push {
assert!(
i % 8 == 0,
"PUSH is not allowed on step {}, must be on step which is a multiple of 8",
i
);
let hint = hints.get(&i);
assert!(
hint.is_some(),
"invalid PUSH operation on step {}: operation value is missing",
i
);
match hint.unwrap() {
OpHint::PushValue(_) => (),
_ => panic!(
"invalid PUSH operation on step {}: operation value is of wrong type",
i
),
}
}
}
for &step in hints.keys() {
assert!(
step < instructions.len(),
"hint out of bounds: step must be smaller than {} but is {}",
instructions.len(),
step
);
}
Span {
op_codes: instructions,
op_hints: hints,
}
}
pub fn new_block(instructions: Vec<OpCode>) -> ProgramBlock {
ProgramBlock::Span(Span::new(instructions, BTreeMap::new()))
}
pub fn from_instructions(instructions: Vec<OpCode>) -> Span {
Span::new(instructions, BTreeMap::new())
}
pub fn length(&self) -> usize {
self.op_codes.len()
}
pub fn starts_with(&self, instructions: &[OpCode]) -> bool {
self.op_codes.starts_with(instructions)
}
pub fn get_op(&self, step: usize) -> (OpCode, OpHint) {
(self.op_codes[step], self.get_hint(step))
}
pub fn get_hint(&self, op_index: usize) -> OpHint {
match self.op_hints.get(&op_index) {
Some(&hint) => hint,
None => OpHint::None,
}
}
pub fn hash(&self, mut state: [BaseElement; 4]) -> [BaseElement; 4] {
for (i, &op_code) in self.op_codes.iter().enumerate() {
let op_value = if op_code == OpCode::Push {
match self.get_hint(i) {
OpHint::PushValue(op_value) => op_value,
_ => panic!("value for PUSH operation is missing"),
}
} else {
BaseElement::ZERO
};
hash_op(&mut state, op_code as u8, op_value, i)
}
state
}
pub fn merge(span1: &Span, span2: &Span) -> Span {
let mut new_op_codes = span1.op_codes.clone();
new_op_codes.push(OpCode::Noop);
new_op_codes.extend_from_slice(&span2.op_codes);
let offset = span1.length() + 1;
let mut new_hints = span1.op_hints.clone();
for (step, &hint) in &span2.op_hints {
new_hints.insert(step + offset, hint);
}
Span::new(new_op_codes, new_hints)
}
}
impl fmt::Debug for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (op_code, op_hint) = self.get_op(0);
write!(f, "{}{}", op_code, op_hint)?;
for i in 1..self.length() {
let (op_code, op_hint) = self.get_op(i);
write!(f, " {}{}", op_code, op_hint)?;
}
Ok(())
}
}
impl Group {
pub fn new(body: Vec<ProgramBlock>) -> Group {
validate_block_list(&body, &[]);
Group { body }
}
pub fn new_block(body: Vec<ProgramBlock>) -> ProgramBlock {
ProgramBlock::Group(Group::new(body))
}
pub fn body(&self) -> &[ProgramBlock] {
&self.body
}
pub fn body_hash(&self) -> BaseElement {
hash_seq(&self.body, &BLOCK_SUFFIX, BLOCK_SUFFIX_OFFSET)
}
pub fn get_hash(&self) -> (BaseElement, BaseElement) {
let v0 = self.body_hash();
(v0, BaseElement::ZERO)
}
}
impl fmt::Debug for Group {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "block ")?;
for block in self.body.iter() {
write!(f, "{:?} ", block)?;
}
write!(f, "end")
}
}
impl Switch {
pub fn new(true_branch: Vec<ProgramBlock>, false_branch: Vec<ProgramBlock>) -> Switch {
validate_block_list(&true_branch, &[OpCode::Assert]);
validate_block_list(&false_branch, &[OpCode::Not, OpCode::Assert]);
Switch {
t_branch: true_branch,
f_branch: false_branch,
}
}
pub fn new_block(
true_branch: Vec<ProgramBlock>,
false_branch: Vec<ProgramBlock>,
) -> ProgramBlock {
ProgramBlock::Switch(Switch::new(true_branch, false_branch))
}
pub fn true_branch(&self) -> &[ProgramBlock] {
&self.t_branch
}
pub fn true_branch_hash(&self) -> BaseElement {
hash_seq(&self.t_branch, &BLOCK_SUFFIX, BLOCK_SUFFIX_OFFSET)
}
pub fn false_branch(&self) -> &[ProgramBlock] {
&self.f_branch
}
pub fn false_branch_hash(&self) -> BaseElement {
hash_seq(&self.f_branch, &BLOCK_SUFFIX, BLOCK_SUFFIX_OFFSET)
}
pub fn get_hash(&self) -> (BaseElement, BaseElement) {
let v0 = self.true_branch_hash();
let v1 = self.false_branch_hash();
(v0, v1)
}
}
impl fmt::Debug for Switch {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "if ")?;
for block in self.t_branch.iter() {
write!(f, "{:?} ", block)?;
}
write!(f, "else ")?;
for block in self.f_branch.iter() {
write!(f, "{:?} ", block)?;
}
write!(f, "end")
}
}
impl Loop {
pub fn new(body: Vec<ProgramBlock>) -> Loop {
validate_block_list(&body, &[OpCode::Assert]);
let skip_block = Span::from_instructions(LOOP_SKIP_BLOCK.to_vec());
let skip = vec![ProgramBlock::Span(skip_block)];
Loop { body, skip }
}
pub fn new_block(body: Vec<ProgramBlock>) -> ProgramBlock {
ProgramBlock::Loop(Loop::new(body))
}
pub fn body(&self) -> &[ProgramBlock] {
&self.body
}
pub fn image(&self) -> BaseElement {
hash_seq(&self.body, &[], 0)
}
pub fn body_hash(&self) -> BaseElement {
hash_seq(&self.body, &LOOP_BLOCK_SUFFIX, 0)
}
pub fn skip(&self) -> &[ProgramBlock] {
&self.skip
}
pub fn skip_hash(&self) -> BaseElement {
hash_seq(&self.skip, &BLOCK_SUFFIX, BLOCK_SUFFIX_OFFSET)
}
pub fn get_hash(&self) -> (BaseElement, BaseElement) {
let v0 = self.body_hash();
let v1 = self.skip_hash();
(v0, v1)
}
}
impl fmt::Debug for Loop {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "while ")?;
for block in self.body.iter() {
write!(f, "{:?} ", block)?;
}
write!(f, "end")
}
}
fn validate_block_list(blocks: &[ProgramBlock], starts_with: &[OpCode]) {
assert!(
!blocks.is_empty(),
"a sequence of blocks must contain at least one block"
);
match &blocks[0] {
ProgramBlock::Span(block) => {
if !starts_with.is_empty() {
assert!(
block.starts_with(starts_with),
"the first block does not start with a valid sequence of instructions"
);
}
}
_ => panic!("a sequence of blocks must start with a Span block"),
};
let mut was_span = true;
for block in blocks.iter().skip(1) {
match block {
ProgramBlock::Span(_) => {
assert!(
!was_span,
"a Span block cannot be followed by another Span block"
);
}
_ => was_span = false,
}
}
}