snarkvm_circuit_program/data/ciphertext/
mod.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
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
34impl<A: Aleo> Inject for Ciphertext<A> {
35    type Primitive = console::Ciphertext<A::Network>;
36
37    /// Initializes a new ciphertext circuit from a primitive.
38    fn new(mode: Mode, ciphertext: Self::Primitive) -> Self {
39        // Ensure the number of field elements does not exceed the maximum allowed size.
40        match (*ciphertext).len() <= A::MAX_DATA_SIZE_IN_FIELDS as usize {
41            true => Self(Inject::new(mode, (*ciphertext).to_vec())),
42            false => A::halt("Ciphertext exceeds maximum allowed size"),
43        }
44    }
45}
46
47impl<A: Aleo> Eject for Ciphertext<A> {
48    type Primitive = console::Ciphertext<A::Network>;
49
50    /// Ejects the mode of the ciphertext.
51    fn eject_mode(&self) -> Mode {
52        self.0.eject_mode()
53    }
54
55    /// Ejects the ciphertext.
56    fn eject_value(&self) -> Self::Primitive {
57        match console::FromFields::from_fields(&self.0.eject_value()) {
58            Ok(ciphertext) => ciphertext,
59            Err(error) => A::halt(format!("Failed to eject ciphertext: {error}")),
60        }
61    }
62}
63
64impl<A: Aleo> Deref for Ciphertext<A> {
65    type Target = [Field<A>];
66
67    fn deref(&self) -> &Self::Target {
68        &self.0
69    }
70}