snarkvm_circuit_program/data/record/
encrypt.rs

1// Copyright 2024-2025 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
16use super::*;
17
18impl<A: Aleo> Record<A, Plaintext<A>> {
19    /// Encrypts `self` for the record owner under the given randomizer.
20    pub fn encrypt(&self, randomizer: &Scalar<A>) -> Record<A, Ciphertext<A>> {
21        // Ensure the randomizer corresponds to the record nonce.
22        A::assert_eq(&self.nonce, A::g_scalar_multiply(randomizer));
23        // Compute the record view key.
24        let record_view_key = ((*self.owner).to_group() * randomizer).to_x_coordinate();
25        // Encrypt the record.
26        self.encrypt_symmetric_unchecked(record_view_key)
27    }
28
29    /// Encrypts `self` under the given record view key.
30    /// Note: This method does not check that the record view key corresponds to the record owner.
31    /// Use `Self::encrypt` for the checked variant.
32    pub fn encrypt_symmetric_unchecked(&self, record_view_key: Field<A>) -> Record<A, Ciphertext<A>> {
33        // Determine the number of randomizers needed to encrypt the record.
34        let num_randomizers = self.num_randomizers();
35        // Prepare a randomizer for each field element.
36        let randomizers = A::hash_many_psd8(&[A::encryption_domain(), record_view_key], num_randomizers);
37        // Encrypt the record.
38        self.encrypt_with_randomizers(&randomizers)
39    }
40
41    /// Encrypts `self` under the given randomizers.
42    fn encrypt_with_randomizers(&self, randomizers: &[Field<A>]) -> Record<A, Ciphertext<A>> {
43        // Initialize an index to keep track of the randomizer index.
44        let mut index: usize = 0;
45
46        // Encrypt the owner.
47        let owner = match self.owner.is_public().eject_value() {
48            true => self.owner.encrypt(&[]),
49            false => self.owner.encrypt(&[randomizers[index].clone()]),
50        };
51
52        // Increment the index if the owner is private.
53        if owner.is_private().eject_value() {
54            index += 1;
55        }
56
57        // Encrypt the data.
58        let mut encrypted_data = IndexMap::with_capacity(self.data.len());
59        for (id, entry, num_randomizers) in self.data.iter().map(|(id, entry)| (id, entry, entry.num_randomizers())) {
60            // Retrieve the randomizers for this entry.
61            let randomizers = &randomizers[index..index + num_randomizers as usize];
62            // Encrypt the entry.
63            let entry = match entry {
64                // Constant entries do not need to be encrypted.
65                Entry::Constant(plaintext) => Entry::Constant(plaintext.clone()),
66                // Public entries do not need to be encrypted.
67                Entry::Public(plaintext) => Entry::Public(plaintext.clone()),
68                // Private entries are encrypted with the given randomizers.
69                Entry::Private(private) => Entry::Private(private.encrypt_with_randomizers(randomizers)),
70            };
71            // Insert the encrypted entry.
72            if encrypted_data.insert(id.clone(), entry).is_some() {
73                A::halt(format!("Duplicate identifier in record: {id}"))
74            }
75            // Increment the index.
76            index += num_randomizers as usize;
77        }
78
79        // Return the encrypted record.
80        Record { owner, data: encrypted_data, nonce: self.nonce.clone() }
81    }
82}