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
use super::*;
impl<N: Network> Stack<N> {
#[inline]
pub fn deploy<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(&self, rng: &mut R) -> Result<Deployment<N>> {
ensure!(!self.program.functions().is_empty(), "Program '{}' has no functions", self.program.id());
let mut bundle = IndexMap::with_capacity(self.program.functions().len());
for function_name in self.program.functions().keys() {
self.synthesize_key::<A, R>(function_name, rng)?;
let proving_key = self.get_proving_key(function_name)?;
let verifying_key = self.get_verifying_key(function_name)?;
let certificate = Certificate::certify(function_name, &proving_key, &verifying_key)?;
bundle.insert(*function_name, (verifying_key, certificate));
}
Deployment::new(N::EDITION, self.program.clone(), bundle)
}
#[inline]
pub fn verify_deployment<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(
&self,
deployment: &Deployment<N>,
rng: &mut R,
) -> Result<()> {
let edition = deployment.edition();
let program = &self.program;
let program_id = program.id();
let verifying_keys = deployment.verifying_keys();
ensure!(edition == N::EDITION, "Deployed the wrong edition (expected '{}', found '{edition}').", N::EDITION);
ensure!(program == deployment.program(), "The stack program does not match the deployment program");
ensure!(program_id.is_aleo(), "Program '{program_id}' has an incorrect network-level domain (NLD)");
ensure!(!program.functions().is_empty(), "No functions present in the deployment for program '{program_id}'");
ensure!(!verifying_keys.is_empty(), "No verifying keys present in the deployment for program '{program_id}'");
if verifying_keys.len() != program.functions().len() {
bail!("The number of verifying keys does not match the number of program functions");
}
for ((function_name, function), candidate_name) in program.functions().iter().zip_eq(verifying_keys.keys()) {
if function_name != function.name() {
bail!("The function key is '{function_name}', but the function name is '{}'", function.name())
}
if candidate_name != function.name() {
bail!("The verifier key is '{candidate_name}', but the function name is '{}'", function.name())
}
}
for (function, (verifying_key, certificate)) in program.functions().values().zip_eq(verifying_keys.values()) {
let burner_private_key = PrivateKey::new(rng)?;
let burner_address = Address::try_from(&burner_private_key)?;
let input_types = function.input_types();
let inputs = input_types
.iter()
.map(|input_type| match input_type {
ValueType::ExternalRecord(locator) => {
let stack = self.get_external_stack(locator.program_id())?;
stack.sample_value(&burner_address, &ValueType::Record(*locator.resource()), rng)
}
_ => self.sample_value(&burner_address, input_type, rng),
})
.collect::<Result<Vec<_>>>()?;
let request =
Request::sign(&burner_private_key, *program_id, *function.name(), &inputs, &input_types, rng)?;
let assignments = Assignments::<N>::default();
let call_stack = CallStack::CheckDeployment(vec![request], burner_private_key, assignments.clone());
let _response = self.execute_function::<A, R>(call_stack, rng)?;
match assignments.read().last() {
None => bail!("The assignment for function '{}' is missing in '{program_id}'", function.name()),
Some(assignment) => {
if !certificate.verify(function.name(), assignment, verifying_key) {
bail!("The certificate for function '{}' is invalid in '{program_id}'", function.name())
}
}
};
}
Ok(())
}
}