Skip to main content

snarkvm_circuit_program/data/future/
argument.rs

1// Copyright (c) 2019-2026 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    /// A dynamic future.
26    DynamicFuture(DynamicFuture<A>),
27}
28
29impl<A: Aleo> Inject for Argument<A> {
30    type Primitive = console::Argument<A::Network>;
31
32    /// Initializes a circuit of the given mode and argument.
33    fn new(mode: Mode, value: Self::Primitive) -> Self {
34        match value {
35            console::Argument::Plaintext(plaintext) => Self::Plaintext(Inject::new(mode, plaintext)),
36            console::Argument::Future(future) => Self::Future(Inject::new(mode, future)),
37            console::Argument::DynamicFuture(dynamic_future) => Self::DynamicFuture(Inject::new(mode, dynamic_future)),
38        }
39    }
40}
41
42impl<A: Aleo> Eject for Argument<A> {
43    type Primitive = console::Argument<A::Network>;
44
45    /// Ejects the mode of the circuit argument.
46    fn eject_mode(&self) -> Mode {
47        match self {
48            Self::Plaintext(plaintext) => plaintext.eject_mode(),
49            Self::Future(future) => future.eject_mode(),
50            Self::DynamicFuture(dynamic_future) => dynamic_future.eject_mode(),
51        }
52    }
53
54    /// Ejects the circuit argument.
55    fn eject_value(&self) -> Self::Primitive {
56        match self {
57            Self::Plaintext(plaintext) => Self::Primitive::Plaintext(plaintext.eject_value()),
58            Self::Future(future) => Self::Primitive::Future(future.eject_value()),
59            Self::DynamicFuture(dynamic_future) => Self::Primitive::DynamicFuture(dynamic_future.eject_value()),
60        }
61    }
62}
63
64impl<A: Aleo> Equal<Self> for Argument<A> {
65    type Output = Boolean<A>;
66
67    /// Returns `true` if `self` and `other` are equal.
68    fn is_equal(&self, other: &Self) -> Self::Output {
69        match (self, other) {
70            (Self::Plaintext(a), Self::Plaintext(b)) => a.is_equal(b),
71            (Self::Future(a), Self::Future(b)) => a.is_equal(b),
72            (Self::DynamicFuture(a), Self::DynamicFuture(b)) => a.is_equal(b),
73            (Self::Plaintext(..), _) | (Self::Future(..), _) | (Self::DynamicFuture(..), _) => Boolean::constant(false),
74        }
75    }
76
77    /// Returns `true` if `self` and `other` are *not* equal.
78    fn is_not_equal(&self, other: &Self) -> Self::Output {
79        match (self, other) {
80            (Self::Plaintext(a), Self::Plaintext(b)) => a.is_not_equal(b),
81            (Self::Future(a), Self::Future(b)) => a.is_not_equal(b),
82            (Self::DynamicFuture(a), Self::DynamicFuture(b)) => a.is_not_equal(b),
83            (Self::Plaintext(..), _) | (Self::Future(..), _) | (Self::DynamicFuture(..), _) => Boolean::constant(true),
84        }
85    }
86}
87
88impl<A: Aleo> ToBits for Argument<A> {
89    type Boolean = Boolean<A>;
90
91    /// Returns the argument as a list of **little-endian** bits.
92    #[inline]
93    fn write_bits_le(&self, vec: &mut Vec<Boolean<A>>) {
94        match self {
95            Self::Plaintext(plaintext) => {
96                vec.push(Boolean::constant(false));
97                plaintext.write_bits_le(vec);
98            }
99            Self::Future(future) => {
100                vec.push(Boolean::constant(true));
101                future.write_bits_le(vec);
102            }
103            Self::DynamicFuture(dynamic_future) => {
104                vec.push(Boolean::constant(true));
105                // Note. This encoding is needed to uniquely disambiguate dynamic futures from static futures.
106                // This is sound because:
107                //  - a static future expects the program ID bits after the initial tag bit
108                //  - a program ID contains two `Identifier`s
109                //  - an `Identifier` cannot lead with a zero byte, since a leading zero byte implies an empty string.
110                // The 12 bits consist of: 8 bits for disambiguation (zero byte) + 4 bits reserved for future variants.
111                vec.extend(std::iter::repeat_n(Boolean::constant(false), 12));
112                dynamic_future.write_bits_le(vec);
113            }
114        }
115    }
116
117    /// Returns the argument as a list of **big-endian** bits.
118    #[inline]
119    fn write_bits_be(&self, vec: &mut Vec<Boolean<A>>) {
120        match self {
121            Self::Plaintext(plaintext) => {
122                vec.push(Boolean::constant(false));
123                plaintext.write_bits_be(vec);
124            }
125            Self::Future(future) => {
126                vec.push(Boolean::constant(true));
127                future.write_bits_be(vec);
128            }
129            Self::DynamicFuture(dynamic_future) => {
130                vec.push(Boolean::constant(true));
131                // Note. This encoding is needed to uniquely disambiguate dynamic futures from static futures.
132                // This is sound because:
133                //  - a static future expects the program ID bits after the initial tag bit
134                //  - a program ID contains two `Identifier`s
135                //  - an `Identifier` cannot lead with a zero byte, since a leading zero byte implies an empty string.
136                // The 12 bits consist of: 8 bits for disambiguation (zero byte) + 4 bits reserved for future variants.
137                vec.extend(std::iter::repeat_n(Boolean::constant(false), 12));
138                dynamic_future.write_bits_be(vec);
139            }
140        }
141    }
142}