snarkvm_circuit_program/data/ciphertext/
mod.rs

1// Copyright 2024-2025 Aleo Network Foundation
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 decrypt;
17mod equal;
18mod from_bits;
19mod from_fields;
20mod num_randomizers;
21mod size_in_fields;
22mod to_bits;
23mod to_fields;
24
25use crate::{Plaintext, Visibility};
26use snarkvm_circuit_network::Aleo;
27use snarkvm_circuit_types::{Boolean, Field, environment::prelude::*};
28
29use core::ops::Deref;
30
31#[derive(Clone)]
32pub struct Ciphertext<A: Aleo>(Vec<Field<A>>);
33
34#[cfg(feature = "console")]
35impl<A: Aleo> Inject for Ciphertext<A> {
36    type Primitive = console::Ciphertext<A::Network>;
37
38    /// Initializes a new ciphertext circuit from a primitive.
39    fn new(mode: Mode, ciphertext: Self::Primitive) -> Self {
40        // Ensure the number of field elements does not exceed the maximum allowed size.
41        match (*ciphertext).len() <= A::MAX_DATA_SIZE_IN_FIELDS as usize {
42            true => Self(Inject::new(mode, (*ciphertext).to_vec())),
43            false => A::halt("Ciphertext exceeds maximum allowed size"),
44        }
45    }
46}
47
48#[cfg(feature = "console")]
49impl<A: Aleo> Eject for Ciphertext<A> {
50    type Primitive = console::Ciphertext<A::Network>;
51
52    /// Ejects the mode of the ciphertext.
53    fn eject_mode(&self) -> Mode {
54        self.0.eject_mode()
55    }
56
57    /// Ejects the ciphertext.
58    fn eject_value(&self) -> Self::Primitive {
59        match console::FromFields::from_fields(&self.0.eject_value()) {
60            Ok(ciphertext) => ciphertext,
61            Err(error) => A::halt(format!("Failed to eject ciphertext: {error}")),
62        }
63    }
64}
65
66impl<A: Aleo> Deref for Ciphertext<A> {
67    type Target = [Field<A>];
68
69    fn deref(&self) -> &Self::Target {
70        &self.0
71    }
72}