snarkvm_console_program/data/value/
to_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<N: Network> ToFields for Value<N> {
19    type Field = Field<N>;
20
21    /// Returns the stack value as a list of fields.
22    #[inline]
23    fn to_fields(&self) -> Result<Vec<Self::Field>> {
24        // // TODO (howardwu): Implement `Literal::to_fields()` to replace this closure.
25        // // (Optional) Closure for converting a list of literals into a list of field elements.
26        // //
27        // // If the list is comprised of `Address`, `Field`, `Group`, and/or `Scalar`, then the closure
28        // // will return the underlying field elements (instead of packing the literals from bits).
29        // // Otherwise, the list is converted into bits, and then packed into field elements.
30        // let to_field_elements = |input: &[Literal<_>]| {
31        //     // Determine whether the input is comprised of field-friendly literals.
32        //     match input.iter().all(|literal| {
33        //         matches!(literal, Literal::Address(_) | Literal::Field(_) | Literal::Group(_) | Literal::Scalar(_))
34        //     }) {
35        //         // Case 1 - Map each literal directly to its field representation.
36        //         true => input
37        //             .iter()
38        //             .map(|literal| match literal {
39        //                 Literal::Address(address) => address.to_field(),
40        //                 Literal::Field(field) => field.clone(),
41        //                 Literal::Group(group) => group.to_x_coordinate(),
42        //                 Literal::Scalar(scalar) => scalar.to_field(),
43        //                 _ => P::halt("Unreachable literal variant detected during hashing."),
44        //             })
45        //             .collect::<Vec<_>>(),
46        //         // Case 2 - Convert the literals to bits, and then pack them into field elements.
47        //         false => input
48        //             .to_bits_le()
49        //             .chunks(<P::Environment as Environment>::BaseField::size_in_data_bits())
50        //             .map(FromBits::from_bits_le)
51        //             .collect::<Vec<_>>(),
52        //     }
53        // };
54
55        match self {
56            Self::Plaintext(plaintext) => plaintext.to_fields(),
57            Self::Record(record) => record.to_fields(),
58            Self::Future(future) => future.to_fields(),
59        }
60    }
61}