Skip to main content

snarkvm_circuit_program/data/value/
mod.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
16mod equal;
17mod find;
18mod to_bits;
19mod to_bits_raw;
20mod to_fields;
21mod to_fields_raw;
22
23use crate::{Access, DynamicFuture, DynamicRecord, Entry, Future, Plaintext, Record};
24use snarkvm_circuit_network::Aleo;
25use snarkvm_circuit_types::{Boolean, Field, environment::prelude::*};
26
27#[derive(Clone)]
28pub enum Value<A: Aleo> {
29    /// A plaintext value.
30    Plaintext(Plaintext<A>),
31    /// A record value.
32    Record(Record<A, Plaintext<A>>),
33    /// A future.
34    Future(Future<A>),
35    /// A dynamic record.
36    DynamicRecord(DynamicRecord<A>),
37    /// A dynamic future.
38    DynamicFuture(DynamicFuture<A>),
39}
40
41impl<A: Aleo> Inject for Value<A> {
42    type Primitive = console::Value<A::Network>;
43
44    /// Initializes a circuit of the given mode and value.
45    fn new(mode: Mode, value: Self::Primitive) -> Self {
46        match value {
47            console::Value::Plaintext(plaintext) => Value::Plaintext(Plaintext::new(mode, plaintext)),
48            console::Value::Record(record) => Value::Record(Record::new(Mode::Private, record)),
49            console::Value::Future(future) => Value::Future(Future::new(mode, future)),
50            console::Value::DynamicRecord(dynamic_record) => {
51                Value::DynamicRecord(DynamicRecord::new(Mode::Private, dynamic_record))
52            }
53            console::Value::DynamicFuture(dynamic_future) => {
54                Value::DynamicFuture(DynamicFuture::new(mode, dynamic_future))
55            }
56        }
57    }
58}
59
60impl<A: Aleo> Eject for Value<A> {
61    type Primitive = console::Value<A::Network>;
62
63    /// Ejects the mode of the circuit value.
64    fn eject_mode(&self) -> Mode {
65        match self {
66            Value::Plaintext(plaintext) => plaintext.eject_mode(),
67            Value::Record(record) => record.eject_mode(),
68            Value::Future(future) => future.eject_mode(),
69            Value::DynamicRecord(dynamic_record) => dynamic_record.eject_mode(),
70            Value::DynamicFuture(dynamic_future) => dynamic_future.eject_mode(),
71        }
72    }
73
74    /// Ejects the circuit value.
75    fn eject_value(&self) -> Self::Primitive {
76        match self {
77            Value::Plaintext(plaintext) => console::Value::Plaintext(plaintext.eject_value()),
78            Value::Record(record) => console::Value::Record(record.eject_value()),
79            Value::Future(future) => console::Value::Future(future.eject_value()),
80            Value::DynamicRecord(dynamic_record) => console::Value::DynamicRecord(dynamic_record.eject_value()),
81            Value::DynamicFuture(dynamic_future) => console::Value::DynamicFuture(dynamic_future.eject_value()),
82        }
83    }
84}
85
86impl<A: Aleo> From<Plaintext<A>> for Value<A> {
87    /// Initializes the value from a plaintext.
88    fn from(plaintext: Plaintext<A>) -> Self {
89        Self::Plaintext(plaintext)
90    }
91}
92
93impl<A: Aleo> From<Record<A, Plaintext<A>>> for Value<A> {
94    /// Initializes the value from a record.
95    fn from(record: Record<A, Plaintext<A>>) -> Self {
96        Self::Record(record)
97    }
98}
99
100impl<A: Aleo> From<Future<A>> for Value<A> {
101    /// Initializes the value from a future.
102    fn from(future: Future<A>) -> Self {
103        Self::Future(future)
104    }
105}
106
107impl<A: Aleo> From<DynamicRecord<A>> for Value<A> {
108    /// Initializes the value from a dynamic record.
109    fn from(dynamic_record: DynamicRecord<A>) -> Self {
110        Self::DynamicRecord(dynamic_record)
111    }
112}
113
114impl<A: Aleo> From<DynamicFuture<A>> for Value<A> {
115    /// Initializes the value from a dynamic future.
116    fn from(dynamic_future: DynamicFuture<A>) -> Self {
117        Self::DynamicFuture(dynamic_future)
118    }
119}