/ console / program / src / data / plaintext / encrypt.rs
encrypt.rs
 1  // Copyright (c) 2025-2026 ACDC Network
 2  // This file is part of the alphavm library.
 3  //
 4  // Alpha Chain | Delta Chain Protocol
 5  // International Monetary Graphite.
 6  //
 7  // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com).
 8  // They built world-class ZK infrastructure. We installed the EASY button.
 9  // Their cryptography: elegant. Our modifications: bureaucracy-compatible.
10  // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours.
11  //
12  // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0
13  // All modifications and new work: CC0 1.0 Universal Public Domain Dedication.
14  // No rights reserved. No permission required. No warranty. No refunds.
15  //
16  // https://creativecommons.org/publicdomain/zero/1.0/
17  // SPDX-License-Identifier: CC0-1.0
18  
19  use super::*;
20  
21  impl<N: Network> Plaintext<N> {
22      /// Encrypts `self` to the given address under the given randomizer.
23      pub fn encrypt(&self, address: &Address<N>, randomizer: Scalar<N>) -> Result<Ciphertext<N>> {
24          // Compute the plaintext view key.
25          let plaintext_view_key = (**address * randomizer).to_x_coordinate();
26          // Encrypt the plaintext.
27          self.encrypt_symmetric(plaintext_view_key)
28      }
29  
30      /// Encrypts `self` under the given plaintext view key.
31      pub fn encrypt_symmetric(&self, plaintext_view_key: Field<N>) -> Result<Ciphertext<N>> {
32          // Determine the number of randomizers needed to encrypt the plaintext.
33          let num_randomizers = self.num_randomizers()?;
34          // Prepare a randomizer for each field element.
35          let randomizers = N::hash_many_psd8(&[N::encryption_domain(), plaintext_view_key], num_randomizers);
36          // Encrypt the plaintext.
37          self.encrypt_with_randomizers(&randomizers)
38      }
39  
40      /// Encrypts `self` under the given randomizers.
41      pub(crate) fn encrypt_with_randomizers(&self, randomizers: &[Field<N>]) -> Result<Ciphertext<N>> {
42          // Encrypt the plaintext.
43          Ciphertext::from_fields(
44              &self
45                  .to_fields()?
46                  .into_iter()
47                  .zip_eq(randomizers)
48                  .map(|(plaintext, randomizer)| plaintext + randomizer)
49                  .collect::<Vec<_>>(),
50          )
51      }
52  }