snarkvm_synthesizer_program/traits/stack_and_registers.rs
1// Copyright (c) 2019-2026 Provable Inc.
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
16use std::sync::Arc;
17
18use crate::{FinalizeGlobalState, FinalizeStoreTrait, Function, Operand, Program};
19use console::{
20 account::Group,
21 network::Network,
22 prelude::{Result, bail},
23 program::{
24 Future,
25 Identifier,
26 Literal,
27 Locator,
28 Plaintext,
29 PlaintextType,
30 ProgramID,
31 Record,
32 Register,
33 RegisterType,
34 Request,
35 StructType,
36 Value,
37 ValueType,
38 },
39 types::{Address, Field, U8, U16},
40};
41use rand::{CryptoRng, Rng};
42use snarkvm_synthesizer_snark::{ProvingKey, VerifyingKey};
43
44/// This trait is intended to be implemented only by `snarkvm_synthesizer_process::Stack`.
45///
46/// We make it a trait only to avoid circular dependencies.
47pub trait StackTrait<N: Network> {
48 /// Returns `true` if the proving key for the given name exists.
49 /// The name can be a function name or a record name (for translation keys).
50 fn contains_proving_key(&self, function_or_record_name: &Identifier<N>) -> bool;
51
52 /// Returns the proving key for the given name.
53 /// The name can be a function name or a record name (for translation keys).
54 fn get_proving_key(&self, function_or_record_name: &Identifier<N>) -> Result<ProvingKey<N>>;
55
56 /// Inserts the proving key for the given name.
57 /// The name can be a function name or a record name (for translation keys).
58 fn insert_proving_key(&self, function_or_record_name: &Identifier<N>, proving_key: ProvingKey<N>) -> Result<()>;
59
60 /// Removes the proving key for the given name.
61 /// The name can be a function name or a record name (for translation keys).
62 fn remove_proving_key(&self, function_or_record_name: &Identifier<N>);
63
64 /// Returns `true` if the verifying key for the given name exists.
65 /// The name can be a function name or a record name (for translation keys).
66 fn contains_verifying_key(&self, function_or_record_name: &Identifier<N>) -> bool;
67
68 /// Returns the verifying key for the given name.
69 /// The name can be a function name or a record name (for translation keys).
70 fn get_verifying_key(&self, function_or_record_name: &Identifier<N>) -> Result<VerifyingKey<N>>;
71
72 /// Inserts the verifying key for the given name.
73 /// The name can be a function name or a record name (for translation keys).
74 fn insert_verifying_key(
75 &self,
76 function_or_record_name: &Identifier<N>,
77 verifying_key: VerifyingKey<N>,
78 ) -> Result<()>;
79
80 /// Removes the verifying key for the given name.
81 /// The name can be a function name or a record name (for translation keys).
82 fn remove_verifying_key(&self, function_or_record_name: &Identifier<N>);
83
84 /// Checks that the given value matches the layout of the value type.
85 fn matches_value_type(&self, value: &Value<N>, value_type: &ValueType<N>) -> Result<()>;
86
87 /// Checks that the given stack value matches the layout of the register type.
88 fn matches_register_type(&self, stack_value: &Value<N>, register_type: &RegisterType<N>) -> Result<()>;
89
90 /// Checks that the given record matches the layout of the external record type.
91 fn matches_external_record(&self, record: &Record<N, Plaintext<N>>, locator: &Locator<N>) -> Result<()>;
92
93 /// Checks that the given record matches the layout of the record type.
94 fn matches_record(&self, record: &Record<N, Plaintext<N>>, record_name: &Identifier<N>) -> Result<()>;
95
96 /// Checks that the given plaintext matches the layout of the plaintext type.
97 fn matches_plaintext(&self, plaintext: &Plaintext<N>, plaintext_type: &PlaintextType<N>) -> Result<()>;
98
99 /// Checks that the given future matches the layout of the future type.
100 fn matches_future(&self, future: &Future<N>, locator: &Locator<N>) -> Result<()>;
101
102 /// Returns the program.
103 fn program(&self) -> &Program<N>;
104
105 /// Returns the program ID.
106 fn program_id(&self) -> &ProgramID<N>;
107
108 /// Returns the program address.
109 fn program_address(&self) -> &Address<N>;
110
111 /// Returns the program checksum.
112 fn program_checksum(&self) -> &[U8<N>; 32];
113
114 /// Returns the program checksum as a field element.
115 fn program_checksum_as_field(&self) -> Result<Field<N>>;
116
117 /// Returns the program edition.
118 fn program_edition(&self) -> U16<N>;
119
120 /// Returns the number of amendments for the current program edition.
121 fn program_amendment_count(&self) -> u64;
122
123 /// Sets the number of amendments for the current program edition.
124 fn set_program_amendment_count(&mut self, program_amendment_count: u64);
125
126 /// Returns the program owner.
127 /// The program owner should only be set for programs that are deployed after `ConsensusVersion::V9` is active.
128 fn program_owner(&self) -> &Option<Address<N>>;
129
130 /// Sets the program owner.
131 fn set_program_owner(&mut self, program_owner: Option<Address<N>>);
132
133 /// Returns the external stack for the given program ID.
134 fn get_external_stack(&self, program_id: &ProgramID<N>) -> Result<Arc<Self>>;
135
136 /// Returns the external stack for the given program ID, without checking that:
137 ///
138 /// - The program ID is different from the current program ID.
139 /// - The program ID is imported by the current program.
140 ///
141 /// This function is only to be used for resolution during dynamic dispatch.
142 fn get_stack_global(&self, program_id: &ProgramID<N>) -> Result<Arc<Self>>;
143
144 /// Returns the function with the given function name.
145 fn get_function(&self, function_name: &Identifier<N>) -> Result<Function<N>>;
146
147 /// Returns a reference to the function with the given function name.
148 fn get_function_ref(&self, function_name: &Identifier<N>) -> Result<&Function<N>>;
149
150 /// Returns the minimum number of calls for the given function name.
151 /// Note: In a static call graph (no dynamic dispatch), the minimum is the actual count.
152 fn get_minimum_number_of_calls(&self, function_name: &Identifier<N>) -> Result<usize>;
153
154 /// Returns whether or not a function has a dynamic call in its execution.
155 fn contains_dynamic_call(&self, function_name: &Identifier<N>) -> Result<bool>;
156
157 /// Samples a value for the given value_type.
158 fn sample_value<R: Rng + CryptoRng>(
159 &self,
160 burner_address: &Address<N>,
161 value_type: &RegisterType<N>,
162 rng: &mut R,
163 ) -> Result<Value<N>>;
164
165 /// Returns a record for the given record name, with the given burner address and nonce.
166 fn sample_record<R: Rng + CryptoRng>(
167 &self,
168 burner_address: &Address<N>,
169 record_name: &Identifier<N>,
170 record_nonce: Group<N>,
171 rng: &mut R,
172 ) -> Result<Record<N, Plaintext<N>>>;
173
174 /// Returns a record for the given record name, deriving the nonce from tvk and index.
175 fn sample_record_using_tvk<R: Rng + CryptoRng>(
176 &self,
177 burner_address: &Address<N>,
178 record_name: &Identifier<N>,
179 tvk: Field<N>,
180 index: Field<N>,
181 rng: &mut R,
182 ) -> Result<Record<N, Plaintext<N>>>;
183
184 /// Evaluates a view function on this stack against the given finalize-store state.
185 ///
186 /// The caller (`Call::finalize`) loads operand values from the caller's registers and
187 /// passes them as `inputs`; this method runs the view body and returns its outputs. It
188 /// is the cross-crate hook that lets `Call::finalize` (in `snarkvm-synthesizer-program`)
189 /// dispatch view-call evaluation into `snarkvm-synthesizer-process` without depending on
190 /// concrete `Stack` / `FinalizeRegisters` types.
191 fn evaluate_view(
192 &self,
193 state: FinalizeGlobalState,
194 store: &dyn FinalizeStoreTrait<N>,
195 view_name: &Identifier<N>,
196 inputs: Vec<Value<N>>,
197 ) -> Result<Vec<Value<N>>>;
198}
199
200/// Are the two types either the same, or both structurally equivalent `PlaintextType`s?
201pub fn register_types_equivalent<N: Network>(
202 stack0: &impl StackTrait<N>,
203 type0: &RegisterType<N>,
204 stack1: &impl StackTrait<N>,
205 type1: &RegisterType<N>,
206) -> Result<bool> {
207 use RegisterType::*;
208 if let (Plaintext(plaintext0), Plaintext(plaintext1)) = (type0, type1) {
209 types_equivalent(stack0, plaintext0, stack1, plaintext1)
210 } else {
211 Ok(type0 == type1)
212 }
213}
214
215/// Determines whether two `PlaintextType` values are equivalent.
216///
217/// Equivalence of literals means they're the same type.
218///
219/// Equivalence of structs means they have the same local names (regardless of whether
220/// they're local or external), and their members have the same names and equivalent
221/// types in the same order, recursively.
222///
223/// Equivalence of arrays means they have the same length and their element types are
224/// equivalent.
225///
226/// This definition of equivalence was chosen to balance these concerns:
227///
228/// 1. All programs from before the existence of external structs will continue to work;
229/// thus it's necessary for a struct created from another program to be considered equivalent
230/// to a local one with the same name and structure, as in practice that was the behavior.
231/// 2. We don't want to allow a fork. Thus we do need to check names, not just structural
232/// equivalence - otherwise we could get a program deployable to a node which is using
233/// this check, but not deployable to a node running an earlier SnarkVM.
234///
235/// The stacks are passed because struct types need to access their stack to get their
236/// structure.
237pub fn types_equivalent<N: Network>(
238 stack0: &impl StackTrait<N>,
239 type0: &PlaintextType<N>,
240 stack1: &impl StackTrait<N>,
241 type1: &PlaintextType<N>,
242) -> Result<bool> {
243 use PlaintextType::*;
244
245 let struct_compare = |stack0, st0: &StructType<N>, stack1, st1: &StructType<N>| -> Result<bool> {
246 if st0.members().len() != st1.members().len() {
247 return Ok(false);
248 }
249
250 for ((name0, type0), (name1, type1)) in st0.members().iter().zip(st1.members()) {
251 if name0 != name1 || !types_equivalent(stack0, type0, stack1, type1)? {
252 return Ok(false);
253 }
254 }
255
256 Ok(true)
257 };
258
259 match (type0, type1) {
260 (Array(array0), Array(array1)) => Ok(array0.length() == array1.length()
261 && types_equivalent(stack0, array0.next_element_type(), stack1, array1.next_element_type())?),
262 (Literal(lit0), Literal(lit1)) => Ok(lit0 == lit1),
263 (Struct(id0), Struct(id1)) => {
264 if id0 != id1 {
265 return Ok(false);
266 }
267 let struct_type0 = stack0.program().get_struct(id0)?;
268 let struct_type1 = stack1.program().get_struct(id1)?;
269 struct_compare(stack0, struct_type0, stack1, struct_type1)
270 }
271 (ExternalStruct(loc0), ExternalStruct(loc1)) => {
272 if loc0.resource() != loc1.resource() {
273 return Ok(false);
274 }
275 let external_stack0 = stack0.get_external_stack(loc0.program_id())?;
276 let struct_type0 = external_stack0.program().get_struct(loc0.resource())?;
277 let external_stack1 = stack1.get_external_stack(loc1.program_id())?;
278 let struct_type1 = external_stack1.program().get_struct(loc1.resource())?;
279 struct_compare(&*external_stack0, struct_type0, &*external_stack1, struct_type1)
280 }
281 (ExternalStruct(loc), Struct(id)) => {
282 if loc.resource() != id {
283 return Ok(false);
284 }
285 let external_stack = stack0.get_external_stack(loc.program_id())?;
286 let struct_type0 = external_stack.program().get_struct(loc.resource())?;
287 let struct_type1 = stack1.program().get_struct(id)?;
288 struct_compare(&*external_stack, struct_type0, stack1, struct_type1)
289 }
290 (Struct(id), ExternalStruct(loc)) => {
291 if id != loc.resource() {
292 return Ok(false);
293 }
294 let struct_type0 = stack0.program().get_struct(id)?;
295 let external_stack = stack1.get_external_stack(loc.program_id())?;
296 let struct_type1 = external_stack.program().get_struct(loc.resource())?;
297 struct_compare(stack0, struct_type0, &*external_stack, struct_type1)
298 }
299 _ => Ok(false),
300 }
301}
302
303pub trait FinalizeRegistersState<N: Network>: RegistersTrait<N> {
304 /// Returns the global state for the finalize scope.
305 fn state(&self) -> &FinalizeGlobalState;
306
307 /// Returns the transition ID for the finalize scope, if one is associated with this scope.
308 /// View functions are externally-callable and have no associated transition, so this is
309 /// `None` on the view path; finalize and constructor scopes always have `Some(...)`.
310 fn transition_id(&self) -> Option<&N::TransitionID>;
311
312 /// Returns the function name for the finalize scope.
313 fn function_name(&self) -> &Identifier<N>;
314
315 /// Returns the nonce for the finalize registers, if one is associated with this scope.
316 /// `None` on the view path (no transition → no nonce); always `Some(...)` on finalize.
317 fn nonce(&self) -> Option<u64>;
318}
319
320pub trait RegistersSigner<N: Network>: RegistersTrait<N> {
321 /// Returns the transition signer.
322 fn signer(&self) -> Result<Address<N>>;
323
324 /// Sets the transition signer.
325 fn set_signer(&mut self, signer: Address<N>);
326
327 /// Returns the root transition view key.
328 fn root_tvk(&self) -> Result<Field<N>>;
329
330 /// Sets the root transition view key.
331 fn set_root_tvk(&mut self, root_tvk: Field<N>);
332
333 /// Returns the transition caller.
334 fn caller(&self) -> Result<Address<N>>;
335
336 /// Sets the transition caller.
337 fn set_caller(&mut self, caller: Address<N>);
338
339 /// Returns the transition view key.
340 fn tvk(&self) -> Result<Field<N>>;
341
342 /// Sets the transition view key.
343 fn set_tvk(&mut self, tvk: Field<N>);
344
345 /// Returns the request.
346 fn request(&self) -> Result<&Request<N>>;
347
348 /// Sets the request.
349 fn set_request(&mut self, request: Request<N>);
350}
351
352pub trait RegistersTrait<N: Network> {
353 /// Loads the value of a given operand.
354 ///
355 /// # Errors
356 /// This method should halt if the register locator is not found.
357 /// In the case of register members, this method should halt if the member is not found.
358 fn load(&self, stack: &impl StackTrait<N>, operand: &Operand<N>) -> Result<Value<N>>;
359
360 /// Loads the literal of a given operand.
361 ///
362 /// # Errors
363 /// This method should halt if the given operand is not a literal.
364 /// This method should halt if the register locator is not found.
365 /// In the case of register members, this method should halt if the member is not found.
366 fn load_literal(&self, stack: &impl StackTrait<N>, operand: &Operand<N>) -> Result<Literal<N>> {
367 match self.load(stack, operand)? {
368 Value::Plaintext(Plaintext::Literal(literal, ..)) => Ok(literal),
369 Value::Plaintext(Plaintext::Struct(..))
370 | Value::Plaintext(Plaintext::Array(..))
371 | Value::Record(..)
372 | Value::Future(..)
373 | Value::DynamicRecord(..)
374 | Value::DynamicFuture(..) => {
375 bail!("Operand must be a literal")
376 }
377 }
378 }
379
380 /// Loads the plaintext of a given operand.
381 ///
382 /// # Errors
383 /// This method should halt if the given operand is not a plaintext.
384 /// This method should halt if the register locator is not found.
385 /// In the case of register members, this method should halt if the member is not found.
386 fn load_plaintext(&self, stack: &impl StackTrait<N>, operand: &Operand<N>) -> Result<Plaintext<N>> {
387 match self.load(stack, operand)? {
388 Value::Plaintext(plaintext) => Ok(plaintext),
389 Value::Record(..) | Value::Future(..) | Value::DynamicRecord(..) | Value::DynamicFuture(..) => {
390 bail!("Operand must be a plaintext")
391 }
392 }
393 }
394
395 /// Assigns the given value to the given register, assuming the register is not already assigned.
396 ///
397 /// # Errors
398 /// This method should halt if the given register is a register member.
399 /// This method should halt if the given register is an input register.
400 /// This method should halt if the register is already used.
401 fn store(&mut self, stack: &impl StackTrait<N>, register: &Register<N>, stack_value: Value<N>) -> Result<()>;
402
403 /// Assigns the given literal to the given register, assuming the register is not already assigned.
404 ///
405 /// # Errors
406 /// This method should halt if the given register is a register member.
407 /// This method should halt if the given register is an input register.
408 /// This method should halt if the register is already used.
409 fn store_literal(&mut self, stack: &impl StackTrait<N>, register: &Register<N>, literal: Literal<N>) -> Result<()> {
410 self.store(stack, register, Value::Plaintext(Plaintext::from(literal)))
411 }
412}
413
414/// This trait is intended to be implemented only by `snarkvm_synthesizer_process::Registers`.
415///
416/// We make it a trait only to avoid circular dependencies.
417pub trait RegistersCircuit<N: Network, A: circuit::Aleo<Network = N>> {
418 /// Returns the transition signer, as a circuit.
419 fn signer_circuit(&self) -> Result<circuit::Address<A>>;
420
421 /// Sets the transition signer, as a circuit.
422 fn set_signer_circuit(&mut self, signer_circuit: circuit::Address<A>);
423
424 /// Returns the root transition view key, as a circuit.
425 fn root_tvk_circuit(&self) -> Result<circuit::Field<A>>;
426
427 /// Sets the root transition view key, as a circuit.
428 fn set_root_tvk_circuit(&mut self, root_tvk_circuit: circuit::Field<A>);
429
430 /// Returns the transition caller, as a circuit.
431 fn caller_circuit(&self) -> Result<circuit::Address<A>>;
432
433 /// Sets the transition caller, as a circuit.
434 fn set_caller_circuit(&mut self, caller_circuit: circuit::Address<A>);
435
436 /// Returns the transition view key, as a circuit.
437 fn tvk_circuit(&self) -> Result<circuit::Field<A>>;
438
439 /// Sets the transition view key, as a circuit.
440 fn set_tvk_circuit(&mut self, tvk_circuit: circuit::Field<A>);
441
442 /// Loads the value of a given operand.
443 ///
444 /// # Errors
445 /// This method should halt if the register locator is not found.
446 /// In the case of register members, this method should halt if the member is not found.
447 fn load_circuit(&self, stack: &impl StackTrait<N>, operand: &Operand<N>) -> Result<circuit::Value<A>>;
448
449 /// Loads the literal of a given operand.
450 ///
451 /// # Errors
452 /// This method should halt if the given operand is not a literal.
453 /// This method should halt if the register locator is not found.
454 /// In the case of register members, this method should halt if the member is not found.
455 fn load_literal_circuit(&self, stack: &impl StackTrait<N>, operand: &Operand<N>) -> Result<circuit::Literal<A>> {
456 match self.load_circuit(stack, operand)? {
457 circuit::Value::Plaintext(circuit::Plaintext::Literal(literal, ..)) => Ok(literal),
458 circuit::Value::Plaintext(circuit::Plaintext::Struct(..))
459 | circuit::Value::Plaintext(circuit::Plaintext::Array(..))
460 | circuit::Value::Record(..)
461 | circuit::Value::Future(..)
462 | circuit::Value::DynamicRecord(..)
463 | circuit::Value::DynamicFuture(..) => bail!("Operand must be a literal"),
464 }
465 }
466
467 /// Loads the plaintext of a given operand.
468 ///
469 /// # Errors
470 /// This method should halt if the given operand is not a plaintext.
471 /// This method should halt if the register locator is not found.
472 /// In the case of register members, this method should halt if the member is not found.
473 fn load_plaintext_circuit(
474 &self,
475 stack: &impl StackTrait<N>,
476 operand: &Operand<N>,
477 ) -> Result<circuit::Plaintext<A>> {
478 match self.load_circuit(stack, operand)? {
479 circuit::Value::Plaintext(plaintext) => Ok(plaintext),
480 circuit::Value::Record(..)
481 | circuit::Value::Future(..)
482 | circuit::Value::DynamicRecord(..)
483 | circuit::Value::DynamicFuture(..) => bail!("Operand must be a plaintext"),
484 }
485 }
486
487 /// Assigns the given value to the given register, assuming the register is not already assigned.
488 ///
489 /// # Errors
490 /// This method should halt if the given register is a register member.
491 /// This method should halt if the given register is an input register.
492 /// This method should halt if the register is already used.
493 fn store_circuit(
494 &mut self,
495 stack: &impl StackTrait<N>,
496 register: &Register<N>,
497 stack_value: circuit::Value<A>,
498 ) -> Result<()>;
499
500 /// Assigns the given literal to the given register, assuming the register is not already assigned.
501 ///
502 /// # Errors
503 /// This method should halt if the given register is a register member.
504 /// This method should halt if the given register is an input register.
505 /// This method should halt if the register is already used.
506 fn store_literal_circuit(
507 &mut self,
508 stack: &impl StackTrait<N>,
509 register: &Register<N>,
510 literal: circuit::Literal<A>,
511 ) -> Result<()> {
512 self.store_circuit(stack, register, circuit::Value::Plaintext(circuit::Plaintext::from(literal)))
513 }
514}