snarkvm-synthesizer-program 4.7.1

Program for a decentralized virtual machine
Documentation
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// Copyright (c) 2019-2026 Provable Inc.
// This file is part of the snarkVM library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::*;

impl<N: Network> Parser for ProgramCore<N> {
    /// Parses a string into a program.
    #[inline]
    fn parse(string: &str) -> ParserResult<Self> {
        // A helper to parse a program.
        enum P<N: Network> {
            Constructor(ConstructorCore<N>),
            M(Mapping<N>),
            S(StructType<N>),
            R(RecordType<N>),
            C(ClosureCore<N>),
            F(FunctionCore<N>),
            V(ViewCore<N>),
        }

        // Parse the imports from the string.
        let (string, imports) = many0(Import::parse)(string)?;
        // Parse the whitespace and comments from the string.
        let (string, _) = Sanitizer::parse(string)?;
        // Parse the 'program' keyword from the string.
        let (string, _) = tag(Self::type_name())(string)?;
        // Parse the whitespace from the string.
        let (string, _) = Sanitizer::parse_whitespaces(string)?;
        // Parse the program ID from the string.
        let (string, id) = ProgramID::parse(string)?;
        // Parse the whitespace from the string.
        let (string, _) = Sanitizer::parse_whitespaces(string)?;
        // Parse the semicolon ';' keyword from the string.
        let (string, _) = tag(";")(string)?;

        fn intermediate<N: Network>(string: &str) -> ParserResult<P<N>> {
            // Parse the whitespace and comments from the string.
            let (string, _) = Sanitizer::parse(string)?;

            if string.starts_with(ConstructorCore::<N>::type_name()) {
                map(ConstructorCore::parse, |constructor| P::<N>::Constructor(constructor))(string)
            } else if string.starts_with(Mapping::<N>::type_name()) {
                map(Mapping::parse, |mapping| P::<N>::M(mapping))(string)
            } else if string.starts_with(StructType::<N>::type_name()) {
                map(StructType::parse, |struct_| P::<N>::S(struct_))(string)
            } else if string.starts_with(RecordType::<N>::type_name()) {
                map(RecordType::parse, |record| P::<N>::R(record))(string)
            } else if string.starts_with(ClosureCore::<N>::type_name()) {
                map(ClosureCore::parse, |closure| P::<N>::C(closure))(string)
            } else if string.starts_with(FunctionCore::<N>::type_name()) {
                map(FunctionCore::parse, |function| P::<N>::F(function))(string)
            } else if string.starts_with(ViewCore::<N>::type_name()) {
                map(ViewCore::parse, |view| P::<N>::V(view))(string)
            } else {
                Err(Err::Error(make_error(string, ErrorKind::Alt)))
            }
        }

        // Parse the struct or function from the string.
        let (string, components) = many1(intermediate)(string)?;
        // Parse the whitespace and comments from the string.
        let (string, _) = Sanitizer::parse(string)?;

        // Initialize a new program.
        let mut program = match ProgramCore::<N>::new(id) {
            Ok(program) => program,
            Err(error) => {
                eprintln!("{error}");
                return map_res(take(0usize), Err)(string);
            }
        };

        // Add the imports (if any) to the program.
        for import in imports {
            match program.add_import(import) {
                Ok(_) => (),
                Err(error) => {
                    eprintln!("{error}");
                    return map_res(take(0usize), Err)(string);
                }
            }
        }

        // Construct the program with the parsed components.
        for component in components {
            let result = match component {
                P::Constructor(constructor) => program.add_constructor(constructor),
                P::M(mapping) => program.add_mapping(mapping),
                P::S(struct_) => program.add_struct(struct_),
                P::R(record) => program.add_record(record),
                P::C(closure) => program.add_closure(closure),
                P::F(function) => program.add_function(function),
                P::V(view) => program.add_view(view),
            };

            match result {
                Ok(_) => (),
                Err(error) => {
                    eprintln!("{error}");
                    return map_res(take(0usize), Err)(string);
                }
            }
        }

        Ok((string, program))
    }
}

impl<N: Network> FromStr for ProgramCore<N> {
    type Err = Error;

    /// Returns a program from a string literal.
    fn from_str(string: &str) -> Result<Self> {
        // Ensure the raw program string is less than MAX_PROGRAM_SIZE.
        ensure!(string.len() <= N::LATEST_MAX_PROGRAM_SIZE(), "Program length exceeds N::MAX_PROGRAM_SIZE.");

        match Self::parse(string) {
            Ok((remainder, object)) => {
                // Ensure the remainder is empty.
                ensure!(remainder.is_empty(), "Failed to parse string. Remaining invalid string is: \"{remainder}\"");
                // Return the object.
                Ok(object)
            }
            Err(error) => bail!("Failed to parse string. {error}"),
        }
    }
}

impl<N: Network> Debug for ProgramCore<N> {
    /// Prints the program as a string.
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        Display::fmt(self, f)
    }
}

impl<N: Network> Display for ProgramCore<N> {
    /// Prints the program as a string.
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        if !self.imports.is_empty() {
            // Print the imports.
            for import in self.imports.values() {
                writeln!(f, "{import}")?;
            }

            // Print a newline.
            writeln!(f)?;
        }

        // Print the program name.
        write!(f, "{} {};\n\n", Self::type_name(), self.id)?;

        // Write the components.
        let mut components_iter = self.components.iter().peekable();
        while let Some((label, definition)) = components_iter.next() {
            match label {
                ProgramLabel::Constructor => {
                    // Write the constructor, if it exists.
                    if let Some(constructor) = &self.constructor {
                        writeln!(f, "{constructor}")?;
                    }
                }
                ProgramLabel::Identifier(identifier) => match definition {
                    ProgramDefinition::Constructor => return Err(fmt::Error),
                    ProgramDefinition::Mapping => match self.mappings.get(identifier) {
                        Some(mapping) => writeln!(f, "{mapping}")?,
                        None => return Err(fmt::Error),
                    },
                    ProgramDefinition::Struct => match self.structs.get(identifier) {
                        Some(struct_) => writeln!(f, "{struct_}")?,
                        None => return Err(fmt::Error),
                    },
                    ProgramDefinition::Record => match self.records.get(identifier) {
                        Some(record) => writeln!(f, "{record}")?,
                        None => return Err(fmt::Error),
                    },
                    ProgramDefinition::Closure => match self.closures.get(identifier) {
                        Some(closure) => writeln!(f, "{closure}")?,
                        None => return Err(fmt::Error),
                    },
                    ProgramDefinition::Function => match self.functions.get(identifier) {
                        Some(function) => writeln!(f, "{function}")?,
                        None => return Err(fmt::Error),
                    },
                    ProgramDefinition::View => match self.views.get(identifier) {
                        Some(view) => writeln!(f, "{view}")?,
                        None => return Err(fmt::Error),
                    },
                },
            }

            // Omit the last newline.
            if components_iter.peek().is_some() {
                writeln!(f)?;
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Program;
    use console::network::MainnetV0;

    type CurrentNetwork = MainnetV0;

    #[test]
    fn test_program_parse() -> Result<()> {
        // Initialize a new program.
        let (string, program) = Program::<CurrentNetwork>::parse(
            r"
program to_parse.aleo;

struct message:
    first as field;
    second as field;

function compute:
    input r0 as message.private;
    add r0.first r0.second into r1;
    output r1 as field.private;",
        )
        .unwrap();
        assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");

        // Ensure the program contains the struct.
        assert!(program.contains_struct(&Identifier::from_str("message")?));
        // Ensure the program contains the function.
        assert!(program.contains_function(&Identifier::from_str("compute")?));

        Ok(())
    }

    #[test]
    fn test_program_parse_with_view_zero_inputs() -> Result<()> {
        let program = Program::<CurrentNetwork>::from_str(
            r"
program qy_zeroin.aleo;

function noop:
    input r0 as u64.private;
    output r0 as u64.private;

view fixed_value:
    add 0u64 1234u64 into r0;
    output r0 as u64.public;",
        )?;
        assert_eq!(program.views().len(), 1);
        Ok(())
    }

    #[test]
    fn test_program_parse_with_view() -> Result<()> {
        let (string, program) = Program::<CurrentNetwork>::parse(
            r"
program token_with_view.aleo;

mapping balances:
    key as address.public;
    value as u64.public;

function noop:
    input r0 as u64.private;
    output r0 as u64.private;

view total_balance:
    input r0 as address.public;
    get.or_use balances[r0] 0u64 into r1;
    output r1 as u64.public;",
        )
        .unwrap();
        assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");

        // The program should expose the view by name.
        let view_name = Identifier::from_str("total_balance")?;
        assert!(program.contains_view(&view_name));
        assert_eq!(1, program.views().len());
        let view = program.get_view_ref(&view_name)?;
        assert_eq!(1, view.inputs().len());
        assert_eq!(1, view.commands().len());
        assert_eq!(1, view.outputs().len());

        Ok(())
    }

    #[test]
    fn test_program_view_rejects_writes() {
        let result = Program::<CurrentNetwork>::from_str(
            r"
program bad_view.aleo;

mapping balances:
    key as address.public;
    value as u64.public;

view mutate:
    input r0 as address.public;
    set 1u64 into balances[r0];
    output r0 as address.public;",
        );
        assert!(result.is_err(), "expected program parse to fail when view contains 'set'");
    }

    #[test]
    fn test_program_view_rejects_call() {
        let result = Program::<CurrentNetwork>::from_str(
            r"
program bad_view.aleo;

closure helper:
    input r0 as field;
    output r0 as field;

view uses_call:
    input r0 as field.public;
    call helper r0 into r1;
    output r1 as field.public;",
        );
        assert!(result.is_err(), "expected program parse to fail when view contains 'call'");
    }

    #[test]
    fn test_program_rejects_duplicate_view_names() {
        let result = Program::<CurrentNetwork>::from_str(
            r"
program dup_view.aleo;

view foo:
    add 0u64 1u64 into r0;
    output r0 as u64.public;

view foo:
    add 0u64 2u64 into r0;
    output r0 as u64.public;",
        );
        assert!(result.is_err(), "expected program parse to fail with two views named 'foo'");
    }

    #[test]
    fn test_program_rejects_view_name_colliding_with_function() {
        let result = Program::<CurrentNetwork>::from_str(
            r"
program qf_collision.aleo;

function foo:
    input r0 as u64.private;
    output r0 as u64.private;

view foo:
    add 0u64 1u64 into r0;
    output r0 as u64.public;",
        );
        assert!(result.is_err(), "expected program parse to fail when a view reuses a function name");
    }

    #[test]
    fn test_program_rejects_view_name_colliding_with_closure() {
        let result = Program::<CurrentNetwork>::from_str(
            r"
program qc_collision.aleo;

closure foo:
    input r0 as field;
    output r0 as field;

function noop:
    input r0 as u64.private;
    output r0 as u64.private;

view foo:
    add 0u64 1u64 into r0;
    output r0 as u64.public;",
        );
        assert!(result.is_err(), "expected program parse to fail when a view reuses a closure name");
    }

    #[test]
    fn test_program_parse_function_zero_inputs() -> Result<()> {
        // Initialize a new program.
        let (string, program) = Program::<CurrentNetwork>::parse(
            r"
program to_parse.aleo;

function compute:
    add 1u32 2u32 into r0;
    output r0 as u32.private;",
        )
        .unwrap();
        assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");

        // Ensure the program contains the function.
        assert!(program.contains_function(&Identifier::from_str("compute")?));

        Ok(())
    }

    #[test]
    fn test_program_display() -> Result<()> {
        let expected = r"program to_parse.aleo;

struct message:
    first as field;
    second as field;

function compute:
    input r0 as message.private;
    add r0.first r0.second into r1;
    output r1 as field.private;
";
        // Parse a new program.
        let program = Program::<CurrentNetwork>::from_str(expected)?;
        // Ensure the program string matches.
        assert_eq!(expected, format!("{program}"));

        Ok(())
    }

    #[test]
    fn test_program_size() {
        let max_program_size: usize = CurrentNetwork::LATEST_MAX_PROGRAM_SIZE();

        // Define variable name for easy experimentation with program sizes.
        let var_name = "a";

        // Helper function to generate imports.
        let gen_import_string = |n: usize| -> String {
            let mut s = String::new();
            for i in 0..n {
                s.push_str(&format!("import foo{i}.aleo;\n"));
            }
            s
        };

        // Helper function to generate large structs.
        let gen_struct_string = |n: usize| -> String {
            let mut s = String::with_capacity(max_program_size);
            for i in 0..n {
                s.push_str(&format!("struct m{i}:\n"));
                for j in 0..10 {
                    s.push_str(&format!("    {var_name}{j} as u128;\n"));
                }
            }
            s
        };

        // Helper function to generate large records.
        let gen_record_string = |n: usize| -> String {
            let mut s = String::with_capacity(max_program_size);
            for i in 0..n {
                s.push_str(&format!("record r{i}:\n    owner as address.private;\n"));
                for j in 0..10 {
                    s.push_str(&format!("    {var_name}{j} as u128.private;\n"));
                }
            }
            s
        };

        // Helper function to generate large mappings.
        let gen_mapping_string = |n: usize| -> String {
            let mut s = String::with_capacity(max_program_size);
            for i in 0..n {
                s.push_str(&format!("mapping {var_name}{i}:\n    key as field.public;\n    value as field.public;\n"));
            }
            s
        };

        // Helper function to generate large closures.
        let gen_closure_string = |n: usize| -> String {
            let mut s = String::with_capacity(max_program_size);
            for i in 0..n {
                s.push_str(&format!("closure c{i}:\n    input r0 as u128;\n"));
                for j in 0..10 {
                    s.push_str(&format!("    add r0 r0 into r{j};\n"));
                }
                s.push_str(&format!("    output r{} as u128;\n", 4000));
            }
            s
        };

        // Helper function to generate large functions.
        let gen_function_string = |n: usize| -> String {
            let mut s = String::with_capacity(max_program_size);
            for i in 0..n {
                s.push_str(&format!("function f{i}:\n    add 1u128 1u128 into r0;\n"));
                for j in 0..500 {
                    s.push_str(&format!("    add r0 r0 into r{j};\n"));
                }
            }
            s
        };

        // Helper function to generate and parse a program.
        let test_parse = |imports: &str, body: &str, should_succeed: bool| {
            let program = format!("{imports}\nprogram to_parse.aleo;\n\n{body}");
            let result = Program::<CurrentNetwork>::from_str(&program);
            if result.is_ok() != should_succeed {
                println!("Program failed to parse: {program}");
            }
            assert_eq!(result.is_ok(), should_succeed);
        };

        // A program with MAX_IMPORTS should succeed.
        test_parse(&gen_import_string(CurrentNetwork::MAX_IMPORTS), &gen_struct_string(1), true);
        // A program with more than MAX_IMPORTS should fail.
        test_parse(&gen_import_string(CurrentNetwork::MAX_IMPORTS + 1), &gen_struct_string(1), false);
        // A program with MAX_STRUCTS should succeed.
        test_parse("", &gen_struct_string(CurrentNetwork::MAX_STRUCTS), true);
        // A program with more than MAX_STRUCTS should fail.
        test_parse("", &gen_struct_string(CurrentNetwork::MAX_STRUCTS + 1), false);
        // A program with MAX_RECORDS should succeed.
        test_parse("", &gen_record_string(CurrentNetwork::MAX_RECORDS), true);
        // A program with more than MAX_RECORDS should fail.
        test_parse("", &gen_record_string(CurrentNetwork::MAX_RECORDS + 1), false);
        // A program with MAX_MAPPINGS should succeed.
        test_parse("", &gen_mapping_string(CurrentNetwork::MAX_MAPPINGS), true);
        // A program with more than MAX_MAPPINGS should fail.
        test_parse("", &gen_mapping_string(CurrentNetwork::MAX_MAPPINGS + 1), false);
        // A program with MAX_CLOSURES should succeed.
        test_parse("", &gen_closure_string(CurrentNetwork::MAX_CLOSURES), true);
        // A program with more than MAX_CLOSURES should fail.
        test_parse("", &gen_closure_string(CurrentNetwork::MAX_CLOSURES + 1), false);
        // A program with MAX_FUNCTIONS should succeed.
        test_parse("", &gen_function_string(CurrentNetwork::MAX_FUNCTIONS), true);
        // A program with more than MAX_FUNCTIONS should fail.
        test_parse("", &gen_function_string(CurrentNetwork::MAX_FUNCTIONS + 1), false);

        // Initialize a program which is too big.
        let program_too_big = format!(
            "{} {} {} {} {}",
            gen_struct_string(CurrentNetwork::MAX_STRUCTS),
            gen_record_string(CurrentNetwork::MAX_RECORDS),
            gen_mapping_string(CurrentNetwork::MAX_MAPPINGS),
            gen_closure_string(CurrentNetwork::MAX_CLOSURES),
            gen_function_string(CurrentNetwork::MAX_FUNCTIONS)
        );
        // A program which is too big should fail.
        test_parse("", &program_too_big, false);
    }
}