snarkvm_circuit_program/request/
mod.rs

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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright 2024 Aleo Network Foundation
// This file is part of the snarkVM library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[cfg(test)]
use snarkvm_circuit_types::environment::assert_scope;

mod to_tpk;
mod verify;

use crate::{Identifier, Plaintext, ProgramID, Record, Value, compute_function_id};
use snarkvm_circuit_account::Signature;
use snarkvm_circuit_network::Aleo;
use snarkvm_circuit_types::{Address, Boolean, Field, Group, U16, environment::prelude::*};

pub enum InputID<A: Aleo> {
    /// The hash of the constant input.
    Constant(Field<A>),
    /// The hash of the public input.
    Public(Field<A>),
    /// The ciphertext hash of the private input.
    Private(Field<A>),
    /// The `(commitment, gamma, serial_number, tag)` tuple of the record input.
    Record(Field<A>, Box<Group<A>>, Field<A>, Field<A>),
    /// The hash of the external record input.
    ExternalRecord(Field<A>),
}

#[cfg(feature = "console")]
impl<A: Aleo> Inject for InputID<A> {
    type Primitive = console::InputID<A::Network>;

    /// Initializes the input ID from the given mode and console input ID.
    fn new(_: Mode, input: Self::Primitive) -> Self {
        match input {
            // Inject the expected hash as `Mode::Public`.
            console::InputID::Constant(field) => Self::Constant(Field::new(Mode::Public, field)),
            // Inject the expected hash as `Mode::Public`.
            console::InputID::Public(field) => Self::Public(Field::new(Mode::Public, field)),
            // Inject the ciphertext hash as `Mode::Public`.
            console::InputID::Private(field) => Self::Private(Field::new(Mode::Public, field)),
            // Inject commitment and gamma as `Mode::Private`, and the expected serial number and tag as `Mode::Public`.
            console::InputID::Record(commitment, gamma, serial_number, tag) => Self::Record(
                Field::new(Mode::Private, commitment),
                Box::new(Group::new(Mode::Private, gamma)),
                Field::new(Mode::Public, serial_number),
                Field::new(Mode::Public, tag),
            ),
            // Inject the commitment as `Mode::Public`.
            console::InputID::ExternalRecord(field) => Self::ExternalRecord(Field::new(Mode::Public, field)),
        }
    }
}

#[cfg(feature = "console")]
impl<A: Aleo> Eject for InputID<A> {
    type Primitive = console::InputID<A::Network>;

    /// Ejects the mode of the input ID.
    fn eject_mode(&self) -> Mode {
        match self {
            Self::Constant(field) => field.eject_mode(),
            Self::Public(field) => field.eject_mode(),
            Self::Private(field) => field.eject_mode(),
            Self::Record(commitment, gamma, serial_number, tag) => Mode::combine(commitment.eject_mode(), [
                gamma.eject_mode(),
                serial_number.eject_mode(),
                tag.eject_mode(),
            ]),
            Self::ExternalRecord(field) => field.eject_mode(),
        }
    }

    /// Ejects the input ID as a primitive.
    fn eject_value(&self) -> Self::Primitive {
        match self {
            Self::Constant(field) => console::InputID::Constant(field.eject_value()),
            Self::Public(field) => console::InputID::Public(field.eject_value()),
            Self::Private(field) => console::InputID::Private(field.eject_value()),
            Self::Record(commitment, gamma, serial_number, tag) => console::InputID::Record(
                commitment.eject_value(),
                gamma.eject_value(),
                serial_number.eject_value(),
                tag.eject_value(),
            ),
            Self::ExternalRecord(field) => console::InputID::ExternalRecord(field.eject_value()),
        }
    }
}

impl<A: Aleo> ToFields for InputID<A> {
    type Field = Field<A>;

    /// Returns the input as a list of field elements.
    fn to_fields(&self) -> Vec<Self::Field> {
        match self {
            InputID::Constant(field) => vec![field.clone()],
            InputID::Public(field) => vec![field.clone()],
            InputID::Private(field) => vec![field.clone()],
            InputID::Record(commitment, gamma, serial_number, tag) => {
                vec![commitment.clone(), gamma.to_x_coordinate(), serial_number.clone(), tag.clone()]
            }
            InputID::ExternalRecord(field) => vec![field.clone()],
        }
    }
}

pub struct Request<A: Aleo> {
    /// The request signer.
    signer: Address<A>,
    /// The network ID.
    network_id: U16<A>,
    /// The program ID.
    program_id: ProgramID<A>,
    /// The function name.
    function_name: Identifier<A>,
    /// The function input IDs.
    input_ids: Vec<InputID<A>>,
    /// The function inputs.
    inputs: Vec<Value<A>>,
    /// The signature for the transition.
    signature: Signature<A>,
    /// The tag secret key.
    sk_tag: Field<A>,
    /// The transition view key.
    tvk: Field<A>,
    /// The transition commitment.
    tcm: Field<A>,
    /// The signer commitment.
    scm: Field<A>,
}

#[cfg(feature = "console")]
impl<A: Aleo> Inject for Request<A> {
    type Primitive = console::Request<A::Network>;

    /// Initializes the request from the given mode and console request.
    fn new(mode: Mode, request: Self::Primitive) -> Self {
        // Inject the transition commitment `tcm` as `Mode::Public`.
        let tcm = Field::new(Mode::Public, *request.tcm());

        // Inject the signer commitment `scm` as `Mode::Public`.
        let scm = Field::new(Mode::Public, *request.scm());

        // Inject the inputs.
        let inputs = match request
            .input_ids()
            .iter()
            .zip_eq(request.inputs())
            .map(|(input_id, input)| {
                match input_id {
                    // A constant input is injected as `Mode::Constant`.
                    console::InputID::Constant(..) => {
                        // Inject the input as `Mode::Constant`.
                        let input = Value::new(Mode::Constant, input.clone());
                        // Ensure the input is a plaintext.
                        ensure!(matches!(input, Value::Plaintext(..)), "Expected a plaintext input");
                        // Return the input.
                        Ok(input)
                    }
                    // A public input is injected as `Mode::Private`.
                    console::InputID::Public(..) => {
                        // Inject the input as `Mode::Private`.
                        let input = Value::new(Mode::Private, input.clone());
                        // Ensure the input is a plaintext.
                        ensure!(matches!(input, Value::Plaintext(..)), "Expected a plaintext input");
                        // Return the input.
                        Ok(input)
                    }
                    // A private input is injected as `Mode::Private`.
                    console::InputID::Private(..) => {
                        // Inject the input as `Mode::Private`.
                        let input = Value::new(Mode::Private, input.clone());
                        // Ensure the input is a plaintext.
                        ensure!(matches!(input, Value::Plaintext(..)), "Expected a plaintext input");
                        // Return the input.
                        Ok(input)
                    }
                    // A record input is injected as `Mode::Private`.
                    console::InputID::Record(..) => {
                        // Inject the input as `Mode::Private`.
                        let input = Value::new(Mode::Private, input.clone());
                        // Ensure the input is a record.
                        ensure!(matches!(input, Value::Record(..)), "Expected a record input");
                        // Return the input.
                        Ok(input)
                    }
                    // An external record input is injected as `Mode::Private`.
                    console::InputID::ExternalRecord(..) => {
                        // Inject the input as `Mode::Private`.
                        let input = Value::new(Mode::Private, input.clone());
                        // Ensure the input is a record.
                        ensure!(matches!(input, Value::Record(..)), "Expected an external record input");
                        // Return the input.
                        Ok(input)
                    }
                }
            })
            .collect::<Result<Vec<_>, _>>()
        {
            Ok(inputs) => inputs,
            Err(error) => A::halt(format!("{error}")),
        };

        Self {
            signer: Address::new(mode, *request.signer()),
            network_id: U16::new(Mode::Constant, *request.network_id()),
            program_id: ProgramID::new(Mode::Constant, *request.program_id()),
            function_name: Identifier::new(Mode::Constant, *request.function_name()),
            input_ids: request.input_ids().iter().map(|input_id| InputID::new(Mode::Public, *input_id)).collect(),
            inputs,
            signature: Signature::new(mode, *request.signature()),
            sk_tag: Field::new(mode, *request.sk_tag()),
            tvk: Field::new(mode, *request.tvk()),
            tcm,
            scm,
        }
    }
}

impl<A: Aleo> Request<A> {
    /// Returns the request signer.
    pub const fn signer(&self) -> &Address<A> {
        &self.signer
    }

    /// Returns the network ID.
    pub const fn network_id(&self) -> &U16<A> {
        &self.network_id
    }

    /// Returns the program ID.
    pub const fn program_id(&self) -> &ProgramID<A> {
        &self.program_id
    }

    /// Returns the function name.
    pub const fn function_name(&self) -> &Identifier<A> {
        &self.function_name
    }

    /// Returns the input IDs for the transition.
    pub fn input_ids(&self) -> &[InputID<A>] {
        &self.input_ids
    }

    /// Returns the function inputs.
    pub fn inputs(&self) -> &[Value<A>] {
        &self.inputs
    }

    /// Returns the signature for the transition.
    pub const fn signature(&self) -> &Signature<A> {
        &self.signature
    }

    /// Returns the tag secret key.
    pub const fn sk_tag(&self) -> &Field<A> {
        &self.sk_tag
    }

    /// Returns the transition view key.
    pub const fn tvk(&self) -> &Field<A> {
        &self.tvk
    }

    /// Returns the transition commitment.
    pub const fn tcm(&self) -> &Field<A> {
        &self.tcm
    }

    /// Returns the signer commitment.
    pub const fn scm(&self) -> &Field<A> {
        &self.scm
    }
}

#[cfg(feature = "console")]
impl<A: Aleo> Eject for Request<A> {
    type Primitive = console::Request<A::Network>;

    /// Ejects the mode of the request.
    fn eject_mode(&self) -> Mode {
        Mode::combine(self.signer.eject_mode(), [
            self.network_id.eject_mode(),
            self.program_id.eject_mode(),
            self.function_name.eject_mode(),
            self.input_ids.eject_mode(),
            self.inputs.eject_mode(),
            self.signature.eject_mode(),
            self.sk_tag.eject_mode(),
            self.tvk.eject_mode(),
            self.tcm.eject_mode(),
            self.scm.eject_mode(),
        ])
    }

    /// Ejects the request as a primitive.
    fn eject_value(&self) -> Self::Primitive {
        Self::Primitive::from((
            self.signer.eject_value(),
            self.network_id.eject_value(),
            self.program_id.eject_value(),
            self.function_name.eject_value(),
            self.input_ids.iter().map(|input_id| input_id.eject_value()).collect(),
            self.inputs.eject_value(),
            self.signature.eject_value(),
            self.sk_tag.eject_value(),
            self.tvk.eject_value(),
            self.tcm.eject_value(),
            self.scm.eject_value(),
        ))
    }
}