Skip to main content

snarkvm_console_program/data/value/
to_fields.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
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    ///
23    /// Each variant encodes its data as follows:
24    /// 1. The variant's `to_bits_le()` produces a bit sequence reflecting its internal structure.
25    /// 2. A terminator bit (`true`) is appended to mark the end of the data.
26    /// 3. The bits are packed into field elements in chunks of `Field::size_in_data_bits()`.
27    ///
28    /// Cross-variant ambiguity is not a concern because the caller always knows the
29    /// expected variant via the `ValueType` or `RegisterType` from the program definition,
30    /// and only the matching variant's `to_fields()` is invoked. Within each variant,
31    /// the encoding is unambiguous:
32    /// - `Plaintext` uses a 2-bit type tag (Literal=00, Struct=01, Array=10) followed by typed data.
33    /// - `Record` starts with owner visibility, 256-bit owner, and a u32 data-length encoding.
34    /// - `Future` starts with a u16 length-prefixed program ID, then function name and arguments.
35    /// - `DynamicRecord` is fixed-size (owner + root + nonce + version, 776 bits total).
36    /// - `DynamicFuture` is fixed-size (4 field elements, 1024 bits total).
37    #[inline]
38    fn to_fields(&self) -> Result<Vec<Self::Field>> {
39        // // TODO (howardwu): Implement `Literal::to_fields()` to replace this closure.
40        // // (Optional) Closure for converting a list of literals into a list of field elements.
41        // //
42        // // If the list is comprised of `Address`, `Field`, `Group`, and/or `Scalar`, then the closure
43        // // will return the underlying field elements (instead of packing the literals from bits).
44        // // Otherwise, the list is converted into bits, and then packed into field elements.
45        // let to_field_elements = |input: &[Literal<_>]| {
46        //     // Determine whether the input is comprised of field-friendly literals.
47        //     match input.iter().all(|literal| {
48        //         matches!(literal, Literal::Address(_) | Literal::Field(_) | Literal::Group(_) | Literal::Scalar(_))
49        //     }) {
50        //         // Case 1 - Map each literal directly to its field representation.
51        //         true => input
52        //             .iter()
53        //             .map(|literal| match literal {
54        //                 Literal::Address(address) => address.to_field(),
55        //                 Literal::Field(field) => field.clone(),
56        //                 Literal::Group(group) => group.to_x_coordinate(),
57        //                 Literal::Scalar(scalar) => scalar.to_field(),
58        //                 _ => P::halt("Unreachable literal variant detected during hashing."),
59        //             })
60        //             .collect::<Vec<_>>(),
61        //         // Case 2 - Convert the literals to bits, and then pack them into field elements.
62        //         false => input
63        //             .to_bits_le()
64        //             .chunks(<P::Environment as Environment>::BaseField::size_in_data_bits())
65        //             .map(FromBits::from_bits_le)
66        //             .collect::<Vec<_>>(),
67        //     }
68        // };
69
70        match self {
71            Self::Plaintext(plaintext) => plaintext.to_fields(),
72            Self::Record(record) => record.to_fields(),
73            Self::Future(future) => future.to_fields(),
74            Self::DynamicRecord(dynamic_record) => dynamic_record.to_fields(),
75            Self::DynamicFuture(dynamic_future) => dynamic_future.to_fields(),
76        }
77    }
78}