snarkvm_circuit_program/state_path/
mod.rs1mod 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
28const BLOCKS_DEPTH: u8 = console::BLOCKS_DEPTH;
30const HEADER_DEPTH: u8 = console::HEADER_DEPTH;
32const TRANSACTIONS_DEPTH: u8 = console::TRANSACTIONS_DEPTH;
34const TRANSACTION_DEPTH: u8 = console::TRANSACTION_DEPTH;
36const 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
45pub struct StatePath<A: Aleo> {
47 global_state_root: Field<A>,
49 block_path: BlockPath<A>,
51 block_hash: Field<A>,
53 previous_block_hash: Field<A>,
55 header_root: Field<A>,
57 header_path: HeaderPath<A>,
59 header_leaf: HeaderLeaf<A>,
61 transactions_path: TransactionsPath<A>,
63 transaction_id: Field<A>,
65 transaction_path: TransactionPath<A>,
67 transaction_leaf: TransactionLeaf<A>,
69 transition_root: Field<A>,
71 tcm: Field<A>,
73 transition_path: TransitionPath<A>,
75 transition_leaf: TransitionLeaf<A>,
77}
78
79impl<A: Aleo> StatePath<A> {
80 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 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 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 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 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}