snarkvm_console_program/data/record/
mod.rs

1// Copyright 2024 Aleo Network Foundation
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};
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}
55
56impl<N: Network, Private: Visibility> Record<N, Private> {
57    /// Initializes a new record plaintext.
58    pub fn from_plaintext(
59        owner: Owner<N, Plaintext<N>>,
60        data: IndexMap<Identifier<N>, Entry<N, Plaintext<N>>>,
61        nonce: Group<N>,
62    ) -> Result<Record<N, Plaintext<N>>> {
63        let reserved = [Identifier::from_str("owner")?];
64        // Ensure the members has no duplicate names.
65        ensure!(!has_duplicates(data.keys().chain(reserved.iter())), "Found a duplicate entry name in a record");
66        // Ensure the number of entries is within the maximum limit.
67        ensure!(data.len() <= N::MAX_DATA_ENTRIES, "Found a record that exceeds size ({})", data.len());
68        // Return the record.
69        Ok(Record { owner, data, nonce })
70    }
71
72    /// Initializes a new record ciphertext.
73    pub fn from_ciphertext(
74        owner: Owner<N, Ciphertext<N>>,
75        data: IndexMap<Identifier<N>, Entry<N, Ciphertext<N>>>,
76        nonce: Group<N>,
77    ) -> Result<Record<N, Ciphertext<N>>> {
78        let reserved = [Identifier::from_str("owner")?];
79        // Ensure the members has no duplicate names.
80        ensure!(!has_duplicates(data.keys().chain(reserved.iter())), "Found a duplicate entry name in a record");
81        // Ensure the number of entries is within the maximum limit.
82        ensure!(data.len() <= N::MAX_DATA_ENTRIES, "Found a record that exceeds size ({})", data.len());
83        // Return the record.
84        Ok(Record { owner, data, nonce })
85    }
86}
87
88impl<N: Network, Private: Visibility> Record<N, Private> {
89    /// Returns the owner of the program record.
90    pub const fn owner(&self) -> &Owner<N, Private> {
91        &self.owner
92    }
93
94    /// Returns the program data.
95    pub const fn data(&self) -> &IndexMap<Identifier<N>, Entry<N, Private>> {
96        &self.data
97    }
98
99    /// Returns the nonce of the program record.
100    pub const fn nonce(&self) -> &Group<N> {
101        &self.nonce
102    }
103}
104
105impl<N: Network, Private: Visibility> Record<N, Private> {
106    /// Returns the owner of the program record, and consumes `self`.
107    pub fn into_owner(self) -> Owner<N, Private> {
108        self.owner
109    }
110
111    /// Returns the program data, and consumes `self`.
112    pub fn into_data(self) -> IndexMap<Identifier<N>, Entry<N, Private>> {
113        self.data
114    }
115
116    /// Returns the nonce of the program record, and consumes `self`.
117    pub fn into_nonce(self) -> Group<N> {
118        self.nonce
119    }
120}