snarkvm_circuit_program/data/future/
argument.rs

1// Copyright (c) 2019-2025 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 super::*;
17
18/// An argument passed into a future.
19#[derive(Clone)]
20pub enum Argument<A: Aleo> {
21    /// A plaintext value.
22    Plaintext(Plaintext<A>),
23    /// A future.
24    Future(Future<A>),
25}
26
27impl<A: Aleo> Inject for Argument<A> {
28    type Primitive = console::Argument<A::Network>;
29
30    /// Initializes a circuit of the given mode and argument.
31    fn new(mode: Mode, value: Self::Primitive) -> Self {
32        match value {
33            console::Argument::Plaintext(plaintext) => Self::Plaintext(Inject::new(mode, plaintext)),
34            console::Argument::Future(future) => Self::Future(Inject::new(mode, future)),
35        }
36    }
37}
38
39impl<A: Aleo> Eject for Argument<A> {
40    type Primitive = console::Argument<A::Network>;
41
42    /// Ejects the mode of the circuit argument.
43    fn eject_mode(&self) -> Mode {
44        match self {
45            Self::Plaintext(plaintext) => plaintext.eject_mode(),
46            Self::Future(future) => future.eject_mode(),
47        }
48    }
49
50    /// Ejects the circuit argument.
51    fn eject_value(&self) -> Self::Primitive {
52        match self {
53            Self::Plaintext(plaintext) => Self::Primitive::Plaintext(plaintext.eject_value()),
54            Self::Future(future) => Self::Primitive::Future(future.eject_value()),
55        }
56    }
57}
58
59impl<A: Aleo> Equal<Self> for Argument<A> {
60    type Output = Boolean<A>;
61
62    /// Returns `true` if `self` and `other` are equal.
63    fn is_equal(&self, other: &Self) -> Self::Output {
64        match (self, other) {
65            (Self::Plaintext(plaintext_a), Self::Plaintext(plaintext_b)) => plaintext_a.is_equal(plaintext_b),
66            (Self::Future(future_a), Self::Future(future_b)) => future_a.is_equal(future_b),
67            (Self::Plaintext(..), _) | (Self::Future(..), _) => Boolean::constant(false),
68        }
69    }
70
71    /// Returns `true` if `self` and `other` are *not* equal.
72    fn is_not_equal(&self, other: &Self) -> Self::Output {
73        match (self, other) {
74            (Self::Plaintext(plaintext_a), Self::Plaintext(plaintext_b)) => plaintext_a.is_not_equal(plaintext_b),
75            (Self::Future(future_a), Self::Future(future_b)) => future_a.is_not_equal(future_b),
76            (Self::Plaintext(..), _) | (Self::Future(..), _) => Boolean::constant(true),
77        }
78    }
79}
80
81impl<A: Aleo> ToBits for Argument<A> {
82    type Boolean = Boolean<A>;
83
84    /// Returns the argument as a list of **little-endian** bits.
85    #[inline]
86    fn write_bits_le(&self, vec: &mut Vec<Boolean<A>>) {
87        match self {
88            Self::Plaintext(plaintext) => {
89                vec.push(Boolean::constant(false));
90                plaintext.write_bits_le(vec);
91            }
92            Self::Future(future) => {
93                vec.push(Boolean::constant(true));
94                future.write_bits_le(vec);
95            }
96        }
97    }
98
99    /// Returns the argument as a list of **big-endian** bits.
100    #[inline]
101    fn write_bits_be(&self, vec: &mut Vec<Boolean<A>>) {
102        match self {
103            Self::Plaintext(plaintext) => {
104                vec.push(Boolean::constant(false));
105                plaintext.write_bits_be(vec);
106            }
107            Self::Future(future) => {
108                vec.push(Boolean::constant(true));
109                future.write_bits_be(vec);
110            }
111        }
112    }
113}