snarkvm_ledger_puzzle_epoch/synthesis/program/
construct_inputs.rs

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
// Copyright 2024 Aleo Network Foundation
// 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> EpochProgram<N> {
    /// Construct the inputs for the given epoch program.
    pub fn construct_inputs(&self, rng: &mut ChaChaRng) -> Result<Vec<Value<N>>> {
        // Prepare the inputs.
        let mut inputs = Vec::new();

        // Iterate over the input registers.
        for literal_type in self.register_table().input_register_types() {
            // Initialize the literal.
            let literal = match literal_type {
                // If the literal type is a `boolean`, then generate a random `boolean` element.
                LiteralType::Boolean => Literal::<N>::Boolean(rng.gen()),
                // If the literal type is a `field`, then generate a random non-zero `field` element.
                LiteralType::Field => {
                    let field = Field::<N>::rand(rng);
                    match field.is_zero() {
                        true => return Err(anyhow!("Invalid input, zero field element found")),
                        false => Literal::<N>::Field(field),
                    }
                }
                // If the literal type is a `i8`, then generate a random `i8`.
                LiteralType::I8 => Literal::<N>::I8(rng.gen()),
                // If the literal type is a `i16`, then generate a random `i16`.
                LiteralType::I16 => Literal::<N>::I16(rng.gen()),
                // If the literal type is a `i32`, then generate a random `i32`.
                LiteralType::I32 => Literal::<N>::I32(rng.gen()),
                // If the literal type is a `i64`, then generate a random `i64`.
                LiteralType::I64 => Literal::<N>::I64(rng.gen()),
                // If the literal type is a `i128`, then generate a random `i128`.
                LiteralType::I128 => Literal::<N>::I128(rng.gen()),
                // Unreachable.
                LiteralType::Address
                | LiteralType::Group
                | LiteralType::U8
                | LiteralType::U16
                | LiteralType::U32
                | LiteralType::U64
                | LiteralType::U128
                | LiteralType::Scalar
                | LiteralType::Signature
                | LiteralType::String => {
                    unreachable!("Invalid input literal type, malformed program");
                }
            };

            // Store the input.
            inputs.push(Value::from(literal));
        }

        Ok(inputs)
    }
}