num_randomizers.rs
1 // Copyright (c) 2019-2025 Alpha-Delta Network Inc. 2 // This file is part of the alphavm 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 16 use super::*; 17 18 impl<N: Network, Private: Visibility> Record<N, Private> { 19 /// Returns the number of field elements to encode `self`. 20 pub(crate) fn num_randomizers(&self) -> Result<u16> { 21 // Initialize an tracker for the number of randomizers. 22 let mut num_randomizers: u16 = 0; 23 24 // If the owner is private, increment the number of randomizers by 1. 25 if self.owner.is_private() { 26 num_randomizers += 1; 27 } 28 29 // Increment the number of randomizers by the number of data randomizers. 30 for (_, entry) in self.data.iter() { 31 num_randomizers = num_randomizers 32 .checked_add(entry.num_randomizers()?) 33 .ok_or_else(|| anyhow!("Number of randomizers exceeds maximum allowed size."))?; 34 } 35 36 // Ensure the number of randomizers does not exceed the maximum allowed size. 37 match num_randomizers as u32 <= N::MAX_DATA_SIZE_IN_FIELDS { 38 true => Ok(num_randomizers), 39 false => bail!("Number of randomizers exceeds the maximum allowed size."), 40 } 41 } 42 }