snarkvm_console_program/data/record/
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 entry;
17pub use entry::Entry;
18
19mod helpers;
20pub use helpers::Owner;
21
22mod bytes;
23mod decrypt;
24mod encrypt;
25mod equal;
26mod find;
27mod is_owner;
28mod num_randomizers;
29mod parse_ciphertext;
30mod parse_plaintext;
31mod serial_number;
32mod serialize;
33mod tag;
34mod to_bits;
35mod to_commitment;
36mod to_fields;
37
38use crate::{Access, Ciphertext, Identifier, Literal, Plaintext, ProgramID};
39use snarkvm_console_account::{Address, PrivateKey, ViewKey};
40use snarkvm_console_network::prelude::*;
41use snarkvm_console_types::{Boolean, Field, Group, Scalar, U8};
42
43use indexmap::IndexMap;
44
45/// A value stored in program record.
46#[derive(Clone)]
47pub struct Record<N: Network, Private: Visibility> {
48    /// The owner of the program record.
49    owner: Owner<N, Private>,
50    /// The program data.
51    data: IndexMap<Identifier<N>, Entry<N, Private>>,
52    /// The nonce of the program record.
53    nonce: Group<N>,
54    /// The version of the program record.
55    ///   - Version 0 uses a BHP hash to derive the record commitment.
56    ///   - Version 1 uses a BHP commitment to derive the record commitment.
57    version: U8<N>,
58}
59
60impl<N: Network, Private: Visibility> Record<N, Private> {
61    /// Initializes a new record plaintext.
62    pub fn from_plaintext(
63        owner: Owner<N, Plaintext<N>>,
64        data: IndexMap<Identifier<N>, Entry<N, Plaintext<N>>>,
65        nonce: Group<N>,
66        version: U8<N>,
67    ) -> Result<Record<N, Plaintext<N>>> {
68        let reserved = [Identifier::from_str("owner")?];
69        // Ensure the members has no duplicate names.
70        ensure!(!has_duplicates(data.keys().chain(reserved.iter())), "Found a duplicate entry name in a record");
71        // Ensure the number of entries is within the maximum limit.
72        ensure!(data.len() <= N::MAX_DATA_ENTRIES, "Found a record that exceeds size ({})", data.len());
73        // Return the record.
74        Ok(Record { owner, data, nonce, version })
75    }
76
77    /// Initializes a new record ciphertext.
78    pub fn from_ciphertext(
79        owner: Owner<N, Ciphertext<N>>,
80        data: IndexMap<Identifier<N>, Entry<N, Ciphertext<N>>>,
81        nonce: Group<N>,
82        version: U8<N>,
83    ) -> Result<Record<N, Ciphertext<N>>> {
84        let reserved = [Identifier::from_str("owner")?];
85        // Ensure the members has no duplicate names.
86        ensure!(!has_duplicates(data.keys().chain(reserved.iter())), "Found a duplicate entry name in a record");
87        // Ensure the number of entries is within the maximum limit.
88        ensure!(data.len() <= N::MAX_DATA_ENTRIES, "Found a record that exceeds size ({})", data.len());
89        // Return the record.
90        Ok(Record { owner, data, nonce, version })
91    }
92}
93
94impl<N: Network, Private: Visibility> Record<N, Private> {
95    /// Returns the owner of the program record.
96    pub const fn owner(&self) -> &Owner<N, Private> {
97        &self.owner
98    }
99
100    /// Returns the program data.
101    pub const fn data(&self) -> &IndexMap<Identifier<N>, Entry<N, Private>> {
102        &self.data
103    }
104
105    /// Returns the nonce of the program record.
106    pub const fn nonce(&self) -> &Group<N> {
107        &self.nonce
108    }
109
110    /// Returns the version of the program record.
111    pub const fn version(&self) -> &U8<N> {
112        &self.version
113    }
114
115    /// Returns `true` if the program record is a hiding variant.
116    pub fn is_hiding(&self) -> bool {
117        !self.version.is_zero()
118    }
119}
120
121impl<N: Network, Private: Visibility> Record<N, Private> {
122    /// Returns the owner of the program record, and consumes `self`.
123    pub fn into_owner(self) -> Owner<N, Private> {
124        self.owner
125    }
126
127    /// Returns the program data, and consumes `self`.
128    pub fn into_data(self) -> IndexMap<Identifier<N>, Entry<N, Private>> {
129        self.data
130    }
131
132    /// Returns the nonce of the program record, and consumes `self`.
133    pub fn into_nonce(self) -> Group<N> {
134        self.nonce
135    }
136}