snarkvm_circuit_program/state_path/
mod.rs

1// Copyright 2024-2025 Aleo Network Foundation
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16mod helpers;
17pub use helpers::*;
18
19mod verify;
20
21#[cfg(test)]
22use snarkvm_circuit_types::environment::assert_scope;
23
24use snarkvm_circuit_collections::merkle_tree::MerklePath;
25use snarkvm_circuit_network::Aleo;
26use snarkvm_circuit_types::{Boolean, Field, U8, environment::prelude::*};
27
28/// The depth of the Merkle tree for the blocks.
29const BLOCKS_DEPTH: u8 = console::BLOCKS_DEPTH;
30/// The depth of the Merkle tree for the block header.
31const HEADER_DEPTH: u8 = console::HEADER_DEPTH;
32/// The depth of the Merkle tree for transactions in a block.
33const TRANSACTIONS_DEPTH: u8 = console::TRANSACTIONS_DEPTH;
34/// The depth of the Merkle tree for the transaction.
35const TRANSACTION_DEPTH: u8 = console::TRANSACTION_DEPTH;
36/// The depth of the Merkle tree for the transition.
37const TRANSITION_DEPTH: u8 = console::TRANSITION_DEPTH;
38
39type BlockPath<A> = MerklePath<A, BLOCKS_DEPTH>;
40type HeaderPath<A> = MerklePath<A, HEADER_DEPTH>;
41type TransactionsPath<A> = MerklePath<A, TRANSACTIONS_DEPTH>;
42type TransactionPath<A> = MerklePath<A, TRANSACTION_DEPTH>;
43type TransitionPath<A> = MerklePath<A, TRANSITION_DEPTH>;
44
45/// The state path proves existence of the transition leaf to either a global or local state root.
46pub struct StatePath<A: Aleo> {
47    /// The global state root (Public).
48    global_state_root: Field<A>,
49    /// The Merkle path for the block hash.
50    block_path: BlockPath<A>,
51    /// The block hash.
52    block_hash: Field<A>,
53    /// The previous block hash.
54    previous_block_hash: Field<A>,
55    /// The block header root.
56    header_root: Field<A>,
57    /// The Merkle path for the block header leaf.
58    header_path: HeaderPath<A>,
59    /// The block header leaf.
60    header_leaf: HeaderLeaf<A>,
61    /// The Merkle path for the transaction ID.
62    transactions_path: TransactionsPath<A>,
63    /// The transaction ID.
64    transaction_id: Field<A>,
65    /// The Merkle path for the transaction leaf.
66    transaction_path: TransactionPath<A>,
67    /// The transaction leaf.
68    transaction_leaf: TransactionLeaf<A>,
69    /// The transition root.
70    transition_root: Field<A>,
71    /// The transition commitment.
72    tcm: Field<A>,
73    /// The Merkle path for the transition leaf.
74    transition_path: TransitionPath<A>,
75    /// The transition leaf.
76    transition_leaf: TransitionLeaf<A>,
77}
78
79impl<A: Aleo> StatePath<A> {
80    /// Returns the transition leaf.
81    pub const fn transition_leaf(&self) -> &TransitionLeaf<A> {
82        &self.transition_leaf
83    }
84}
85
86impl<A: Aleo> Inject for StatePath<A> {
87    type Primitive = console::StatePath<A::Network>;
88
89    /// Initializes a new ciphertext circuit from a primitive.
90    fn new(mode: Mode, state_path: Self::Primitive) -> Self {
91        Self {
92            global_state_root: Field::new(Mode::Public, *state_path.global_state_root()),
93            block_path: BlockPath::new(mode, state_path.block_path().clone()),
94            block_hash: Field::new(mode, *state_path.block_hash()),
95            previous_block_hash: Field::new(mode, *state_path.previous_block_hash()),
96            header_root: Field::new(mode, *state_path.header_root()),
97            header_path: HeaderPath::new(mode, state_path.header_path().clone()),
98            header_leaf: HeaderLeaf::new(mode, *state_path.header_leaf()),
99            transactions_path: TransactionsPath::new(mode, state_path.transactions_path().clone()),
100            transaction_id: Field::new(mode, **state_path.transaction_id()),
101            transaction_path: TransactionPath::new(mode, state_path.transaction_path().clone()),
102            transaction_leaf: TransactionLeaf::new(mode, *state_path.transaction_leaf()),
103            transition_root: Field::new(mode, *state_path.transition_root()),
104            tcm: Field::new(mode, *state_path.tcm()),
105            transition_path: TransitionPath::new(mode, state_path.transition_path().clone()),
106            transition_leaf: TransitionLeaf::new(mode, *state_path.transition_leaf()),
107        }
108    }
109}
110
111impl<A: Aleo> Eject for StatePath<A> {
112    type Primitive = console::StatePath<A::Network>;
113
114    /// Ejects the mode of the state path.
115    fn eject_mode(&self) -> Mode {
116        Mode::combine(self.global_state_root.eject_mode(), [
117            self.block_path.eject_mode(),
118            self.block_hash.eject_mode(),
119            self.previous_block_hash.eject_mode(),
120            self.header_root.eject_mode(),
121            self.header_path.eject_mode(),
122            self.header_leaf.eject_mode(),
123            self.transactions_path.eject_mode(),
124            self.transaction_id.eject_mode(),
125            self.transaction_path.eject_mode(),
126            self.transaction_leaf.eject_mode(),
127            self.transition_root.eject_mode(),
128            self.tcm.eject_mode(),
129            self.transition_path.eject_mode(),
130            self.transition_leaf.eject_mode(),
131        ])
132    }
133
134    /// Ejects the state path.
135    fn eject_value(&self) -> Self::Primitive {
136        Self::Primitive::from(
137            self.global_state_root.eject_value().into(),
138            self.block_path.eject_value(),
139            self.block_hash.eject_value().into(),
140            self.previous_block_hash.eject_value().into(),
141            self.header_root.eject_value(),
142            self.header_path.eject_value(),
143            self.header_leaf.eject_value(),
144            self.transactions_path.eject_value(),
145            self.transaction_id.eject_value().into(),
146            self.transaction_path.eject_value(),
147            self.transaction_leaf.eject_value(),
148            self.transition_root.eject_value(),
149            self.tcm.eject_value(),
150            self.transition_path.eject_value(),
151            self.transition_leaf.eject_value(),
152        )
153    }
154}
155
156#[cfg(all(test, feature = "console"))]
157mod tests {
158    use super::*;
159    use crate::Circuit;
160
161    use snarkvm_utilities::TestRng;
162
163    use anyhow::Result;
164
165    type CurrentNetwork = <Circuit as Environment>::Network;
166
167    const ITERATIONS: u64 = 250;
168
169    fn check_new(
170        mode: Mode,
171        num_constants: u64,
172        num_public: u64,
173        num_private: u64,
174        num_constraints: u64,
175    ) -> Result<()> {
176        let rng = &mut TestRng::default();
177
178        for _ in 0..ITERATIONS {
179            // Sample the console state path.
180            let console_state_path =
181                console::state_path::test_helpers::sample_local_state_path::<CurrentNetwork>(None, rng).unwrap();
182
183            Circuit::scope(format!("New {mode}"), || {
184                let candidate = StatePath::<Circuit>::new(mode, console_state_path.clone());
185                assert_eq!(console_state_path, candidate.eject_value());
186                assert_scope!(num_constants, num_public, num_private, num_constraints);
187            });
188            Circuit::reset();
189        }
190        Ok(())
191    }
192
193    #[test]
194    fn test_state_path_new_constant() -> Result<()> {
195        check_new(Mode::Constant, 450, 1, 0, 0)
196    }
197
198    #[test]
199    fn test_state_path_new_public() -> Result<()> {
200        check_new(Mode::Public, 0, 451, 0, 376)
201    }
202
203    #[test]
204    fn test_state_path_new_private() -> Result<()> {
205        check_new(Mode::Private, 0, 1, 450, 376)
206    }
207}