snarkvm_circuit_program/data/ciphertext/from_fields.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
18impl<A: Aleo> From<Vec<Field<A>>> for Ciphertext<A> {
19 /// Initializes a ciphertext from a list of base field elements.
20 fn from(fields: Vec<Field<A>>) -> Self {
21 // Ensure the number of field elements does not exceed the maximum allowed size.
22 match fields.len() <= A::MAX_DATA_SIZE_IN_FIELDS as usize {
23 true => Self(fields),
24 false => A::halt("Ciphertext exceeds maximum allowed size"),
25 }
26 }
27}
28
29impl<A: Aleo> From<&[Field<A>]> for Ciphertext<A> {
30 /// Initializes a ciphertext from a list of base field elements.
31 fn from(fields: &[Field<A>]) -> Self {
32 Self::from_fields(fields)
33 }
34}
35
36impl<A: Aleo> FromFields for Ciphertext<A> {
37 type Field = Field<A>;
38
39 /// Initializes a ciphertext from a list of base field elements.
40 fn from_fields(fields: &[Self::Field]) -> Self {
41 Self::from(fields.to_vec())
42 }
43}