snarkvm_ledger_puzzle_epoch/synthesis/program/
mod.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
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
// 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.

mod construct_inputs;
mod to_leaves;
mod to_r1cs;

use crate::synthesis::helpers::*;
use circuit::{Mode, environment::R1CS};
use console::{
    account::PrivateKey,
    network::Network,
    prelude::{Itertools, ToBits as TBits, Uniform, Zero},
    program::{Field, Identifier, Literal, LiteralType, Value},
};
use snarkvm_synthesizer_process::{CallStack, Process, Registers, Stack, StackProgramTypes};
use snarkvm_synthesizer_program::{Instruction, Program, RegistersStoreCircuit, StackProgram};

use aleo_std::prelude::{finish, lap, timer};
use anyhow::{Result, anyhow, bail, ensure};
use rand::Rng;
use rand_chacha::ChaChaRng;
use std::{
    fmt::{self, Debug, Formatter},
    ops::Deref,
    str::FromStr,
};

/// The arity of the Merkle tree.
const ARITY: u8 = 8;

#[derive(Clone)]
pub struct EpochProgram<N: Network> {
    /// The program stack for the epoch.
    stack: Stack<N>,
    /// The register table.
    register_table: RegisterTable,
    /// The epoch hash.
    epoch_hash: N::BlockHash,
}

impl<N: Network> Debug for EpochProgram<N> {
    /// Formats the epoch program for debugging.
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("EpochProgram")
            .field("epoch_hash", &self.epoch_hash)
            .field("program", self.stack.program())
            .finish()
    }
}

impl<N: Network> PartialEq for EpochProgram<N> {
    /// Returns `true` if the epoch programs are equal.
    fn eq(&self, other: &Self) -> bool {
        self.epoch_hash == other.epoch_hash && self.stack.program() == other.stack.program()
    }
}

impl<N: Network> Eq for EpochProgram<N> {}

impl<N: Network> EpochProgram<N> {
    /// Initializes a new epoch program, given an epoch.
    ///
    /// This method deterministically synthesizes a new program.
    pub fn new(epoch_hash: N::BlockHash) -> Result<Self> {
        // Initialize the register table.
        let mut register_table = RegisterTable::new();

        // Construct the program inputs, as a string.
        let input_string = register_table.input_block().to_string();

        // Sample the instructions from the given epoch.
        let instructions = sample_instructions::<N>(epoch_hash, &mut register_table)?;
        debug_assert!(!instructions.is_empty());
        // Construct the program instructions, as a string.
        let mut instruction_string = String::new();
        for instruction in &instructions {
            instruction_string.push_str(&format!("    {instruction}\n"));
        }

        // Construct the program string.
        let program_string = format!(
            r"program puzzle.aleo;

function synthesize:
{input_string}
{instruction_string}
"
        );

        // Construct the program.
        let program = Program::from_str(&program_string)?;

        // Initialize a new process.
        let process = Process::<N>::load()?;
        // Initialize the stack with the synthesis challenge program.
        let stack = Stack::new(&process, &program)?;

        Ok(Self { stack, register_table, epoch_hash })
    }
}

impl<N: Network> EpochProgram<N> {
    /// Returns the program stack.
    #[inline]
    pub const fn stack(&self) -> &Stack<N> {
        &self.stack
    }

    /// Returns the register table.
    #[inline]
    pub const fn register_table(&self) -> &RegisterTable {
        &self.register_table
    }

    /// Returns the epoch.
    #[inline]
    pub const fn epoch_hash(&self) -> N::BlockHash {
        self.epoch_hash
    }

    /// Returns the instructions for the program.
    #[inline]
    pub fn instructions(&self) -> Result<&[Instruction<N>]> {
        Ok(self.stack.program().get_function_ref(&Identifier::from_str("synthesize")?)?.instructions())
    }
}

impl<N: Network> Deref for EpochProgram<N> {
    type Target = Program<N>;

    fn deref(&self) -> &Self::Target {
        self.stack.program()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use console::prelude::TestRng;

    type CurrentNetwork = console::network::MainnetV0;

    #[test]
    fn test_new_is_deterministic() {
        let mut rng = TestRng::default();

        // Initialize a random epoch hash.
        let epoch_hash = rng.gen();
        // Initialize a new epoch program.
        let epoch_program_0 = EpochProgram::<CurrentNetwork>::new(epoch_hash).unwrap();
        // Initialize a new epoch program.
        let epoch_program_1 = EpochProgram::<CurrentNetwork>::new(epoch_hash).unwrap();
        // Ensure the epoch program matches.
        assert_eq!(epoch_program_0, epoch_program_1);
    }

    #[test]
    fn test_instructions_succeeds() {
        let mut rng = TestRng::default();

        // Initialize a random epoch hash.
        let epoch_hash = rng.gen();
        // Initialize a new epoch program.
        let epoch_program = EpochProgram::<CurrentNetwork>::new(epoch_hash).unwrap();
        // Retrieve the instructions.
        let instructions = epoch_program.instructions().unwrap();
        // Ensure the instructions are not empty.
        assert!(!instructions.is_empty());
    }
}