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
mod load;
mod store;
use crate::{CallStack, Operand, RegisterTypes, Stack};
use console::{
network::prelude::*,
program::{Entry, Literal, Plaintext, Register, Value},
types::{Address, Field},
};
use indexmap::IndexMap;
#[derive(Clone)]
pub struct Registers<N: Network, A: circuit::Aleo<Network = N>> {
call_stack: CallStack<N>,
register_types: RegisterTypes<N>,
console_registers: IndexMap<u64, Value<N>>,
circuit_registers: IndexMap<u64, circuit::Value<A>>,
caller: Option<Address<N>>,
caller_circuit: Option<circuit::Address<A>>,
tvk: Option<Field<N>>,
tvk_circuit: Option<circuit::Field<A>>,
}
impl<N: Network, A: circuit::Aleo<Network = N>> Registers<N, A> {
#[inline]
pub fn new(call_stack: CallStack<N>, register_types: RegisterTypes<N>) -> Self {
Self {
call_stack,
register_types,
console_registers: IndexMap::new(),
circuit_registers: IndexMap::new(),
caller: None,
caller_circuit: None,
tvk: None,
tvk_circuit: None,
}
}
#[inline]
pub fn call_stack(&self) -> CallStack<N> {
self.call_stack.clone()
}
#[inline]
pub fn caller(&self) -> Result<Address<N>> {
self.caller.ok_or_else(|| anyhow!("Caller address (console) is not set in the registers."))
}
#[inline]
pub fn caller_circuit(&self) -> Result<circuit::Address<A>> {
self.caller_circuit.clone().ok_or_else(|| anyhow!("Caller address (circuit) is not set in the registers."))
}
#[inline]
pub fn set_caller(&mut self, caller: Address<N>) {
self.caller = Some(caller);
}
#[inline]
pub fn set_caller_circuit(&mut self, caller_circuit: circuit::Address<A>) {
self.caller_circuit = Some(caller_circuit);
}
#[inline]
pub fn tvk(&self) -> Result<Field<N>> {
self.tvk.ok_or_else(|| anyhow!("Transition view key (console) is not set in the registers."))
}
#[inline]
pub fn tvk_circuit(&self) -> Result<circuit::Field<A>> {
self.tvk_circuit.clone().ok_or_else(|| anyhow!("Transition view key (circuit) is not set in the registers."))
}
#[inline]
pub fn set_tvk(&mut self, tvk: Field<N>) {
self.tvk = Some(tvk);
}
#[inline]
pub fn set_tvk_circuit(&mut self, tvk_circuit: circuit::Field<A>) {
self.tvk_circuit = Some(tvk_circuit);
}
#[inline]
pub fn ensure_console_and_circuit_registers_match(&self) -> Result<()> {
use circuit::Eject;
for ((console_index, console_register), (circuit_index, circuit_register)) in
self.console_registers.iter().zip_eq(&self.circuit_registers)
{
if *console_index != *circuit_index {
bail!("Console and circuit register indices are mismatching ({console_index} != {circuit_index})")
}
if console_register != &circuit_register.eject_value() {
bail!("The console and circuit register values do not match at index {console_index}")
}
}
Ok(())
}
}