snarkvm_circuit_program/response/
mod.rs1#[cfg(test)]
17use snarkvm_circuit_types::environment::assert_scope;
18
19mod from_outputs;
20mod process_outputs_from_callback;
21
22use crate::{Identifier, ProgramID, Value, compute_function_id};
23use snarkvm_circuit_network::Aleo;
24use snarkvm_circuit_types::{Address, Field, U16, environment::prelude::*};
25
26pub enum OutputID<A: Aleo> {
27 Constant(Field<A>),
29 Public(Field<A>),
31 Private(Field<A>),
33 Record(Field<A>, Field<A>, Field<A>),
35 ExternalRecord(Field<A>),
37 Future(Field<A>),
39}
40
41impl<A: Aleo> Inject for OutputID<A> {
42 type Primitive = console::OutputID<A::Network>;
43
44 fn new(_: Mode, output: Self::Primitive) -> Self {
46 match output {
47 console::OutputID::Constant(field) => Self::Constant(Field::new(Mode::Public, field)),
49 console::OutputID::Public(field) => Self::Public(Field::new(Mode::Public, field)),
51 console::OutputID::Private(field) => Self::Private(Field::new(Mode::Public, field)),
53 console::OutputID::Record(commitment, checksum, sender_ciphertext) => Self::Record(
55 Field::new(Mode::Public, commitment),
56 Field::new(Mode::Public, checksum),
57 Field::new(Mode::Public, sender_ciphertext),
58 ),
59 console::OutputID::ExternalRecord(hash) => Self::ExternalRecord(Field::new(Mode::Public, hash)),
61 console::OutputID::Future(hash) => Self::Future(Field::new(Mode::Public, hash)),
63 }
64 }
65}
66
67impl<A: Aleo> OutputID<A> {
68 fn constant(expected_hash: Field<A>) -> Self {
70 let output_hash = Field::new(Mode::Public, expected_hash.eject_value());
72 A::assert_eq(&output_hash, expected_hash);
74 Self::Constant(output_hash)
76 }
77
78 fn public(expected_hash: Field<A>) -> Self {
80 let output_hash = Field::new(Mode::Public, expected_hash.eject_value());
82 A::assert_eq(&output_hash, expected_hash);
84 Self::Public(output_hash)
86 }
87
88 fn private(expected_hash: Field<A>) -> Self {
90 let output_hash = Field::new(Mode::Public, expected_hash.eject_value());
92 A::assert_eq(&output_hash, expected_hash);
94 Self::Private(output_hash)
96 }
97
98 fn record(
100 expected_commitment: Field<A>,
101 expected_checksum: Field<A>,
102 expected_sender_ciphertext: Field<A>,
103 ) -> Self {
104 let output_commitment = Field::new(Mode::Public, expected_commitment.eject_value());
106 let output_checksum = Field::new(Mode::Public, expected_checksum.eject_value());
107 let output_sender_ciphertext = Field::new(Mode::Public, expected_sender_ciphertext.eject_value()); A::assert_eq(&output_commitment, expected_commitment);
110 A::assert_eq(&output_checksum, expected_checksum);
111 let is_sender_ciphertext_equal = output_sender_ciphertext.is_equal(&expected_sender_ciphertext);
115 let is_sender_ciphertext_zero = output_sender_ciphertext.is_zero();
116 A::assert(is_sender_ciphertext_equal | is_sender_ciphertext_zero);
117 Self::Record(output_commitment, output_checksum, output_sender_ciphertext)
119 }
120
121 fn external_record(expected_hash: Field<A>) -> Self {
123 let output_hash = Field::new(Mode::Public, expected_hash.eject_value());
125 A::assert_eq(&output_hash, expected_hash);
127 Self::ExternalRecord(output_hash)
129 }
130
131 fn future(expected_hash: Field<A>) -> Self {
133 let output_hash = Field::new(Mode::Public, expected_hash.eject_value());
135 A::assert_eq(&output_hash, expected_hash);
137 Self::Future(output_hash)
139 }
140}
141
142impl<A: Aleo> Eject for OutputID<A> {
143 type Primitive = console::OutputID<A::Network>;
144
145 fn eject_mode(&self) -> Mode {
147 match self {
148 Self::Constant(field) => field.eject_mode(),
149 Self::Public(field) => field.eject_mode(),
150 Self::Private(field) => field.eject_mode(),
151 Self::Record(commitment, checksum, sender_ciphertext) => {
152 Mode::combine(commitment.eject_mode(), [checksum.eject_mode(), sender_ciphertext.eject_mode()])
153 }
154 Self::ExternalRecord(hash) => hash.eject_mode(),
155 Self::Future(hash) => hash.eject_mode(),
156 }
157 }
158
159 fn eject_value(&self) -> Self::Primitive {
161 match self {
162 Self::Constant(field) => console::OutputID::Constant(field.eject_value()),
163 Self::Public(field) => console::OutputID::Public(field.eject_value()),
164 Self::Private(field) => console::OutputID::Private(field.eject_value()),
165 Self::Record(commitment, checksum, sender_ciphertext) => console::OutputID::Record(
166 commitment.eject_value(),
167 checksum.eject_value(),
168 sender_ciphertext.eject_value(),
169 ),
170 Self::ExternalRecord(hash) => console::OutputID::ExternalRecord(hash.eject_value()),
171 Self::Future(hash) => console::OutputID::Future(hash.eject_value()),
172 }
173 }
174}
175
176pub struct Response<A: Aleo> {
177 output_ids: Vec<OutputID<A>>,
179 outputs: Vec<Value<A>>,
181}
182
183impl<A: Aleo> Response<A> {
184 pub fn output_ids(&self) -> &[OutputID<A>] {
186 &self.output_ids
187 }
188
189 pub fn outputs(&self) -> &[Value<A>] {
191 &self.outputs
192 }
193}
194
195impl<A: Aleo> Eject for Response<A> {
196 type Primitive = console::Response<A::Network>;
197
198 fn eject_mode(&self) -> Mode {
200 Mode::combine(self.output_ids.eject_mode(), [self.outputs.eject_mode()])
201 }
202
203 fn eject_value(&self) -> Self::Primitive {
205 Self::Primitive::from((
206 self.output_ids.iter().map(|output_id| output_id.eject_value()).collect(),
207 self.outputs.eject_value(),
208 ))
209 }
210}