/ console / program / src / data / record / num_randomizers.rs
num_randomizers.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, Private: Visibility> Record<N, Private> {
22      /// Returns the number of field elements to encode `self`.
23      pub(crate) fn num_randomizers(&self) -> Result<u16> {
24          // Initialize an tracker for the number of randomizers.
25          let mut num_randomizers: u16 = 0;
26  
27          // If the owner is private, increment the number of randomizers by 1.
28          if self.owner.is_private() {
29              num_randomizers += 1;
30          }
31  
32          // Increment the number of randomizers by the number of data randomizers.
33          for (_, entry) in self.data.iter() {
34              num_randomizers = num_randomizers
35                  .checked_add(entry.num_randomizers()?)
36                  .ok_or_else(|| anyhow!("Number of randomizers exceeds maximum allowed size."))?;
37          }
38  
39          // Ensure the number of randomizers does not exceed the maximum allowed size.
40          match num_randomizers as u32 <= N::MAX_DATA_SIZE_IN_FIELDS {
41              true => Ok(num_randomizers),
42              false => bail!("Number of randomizers exceeds the maximum allowed size."),
43          }
44      }
45  }