snarkvm_console_program/data/dynamic/record/to_bits.rs
1// Copyright (c) 2019-2026 Provable Inc.
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<N: Network> ToBits for DynamicRecord<N> {
19 /// Returns the dynamic record as a list of **little-endian** bits.
20 fn write_bits_le(&self, vec: &mut Vec<bool>) {
21 // Construct the owner bits.
22 self.owner.write_bits_le(vec);
23
24 // Construct the root bits.
25 self.root.write_bits_le(vec);
26
27 // Construct the nonce bits.
28 self.nonce.write_bits_le(vec);
29
30 // Construct the version bits.
31 self.version.write_bits_le(vec);
32 }
33
34 /// Returns the dynamic record as a list of **big-endian** bits.
35 fn write_bits_be(&self, vec: &mut Vec<bool>) {
36 // Construct the owner bits.
37 self.owner.write_bits_be(vec);
38
39 // Construct the root bits.
40 self.root.write_bits_be(vec);
41
42 // Construct the nonce bits.
43 self.nonce.write_bits_be(vec);
44
45 // Construct the version bits.
46 self.version.write_bits_be(vec);
47 }
48}
49
50impl<N: Network> DynamicRecord<N> {
51 /// Returns the number of bits in a dynamic record.
52 #[inline]
53 pub fn size_in_bits() -> Result<usize> {
54 // Account for the owner bits.
55 let mut size = Address::<N>::size_in_bits();
56 // Account for the root bits.
57 size = size.checked_add(Field::<N>::size_in_bits()).ok_or_else(|| anyhow!("`size_in_bits` overflowed"))?;
58 // Account for the nonce bits.
59 size = size.checked_add(Group::<N>::size_in_bits()).ok_or_else(|| anyhow!("`size_in_bits` overflowed"))?;
60 // Account for the version bits.
61 size = size.checked_add(U8::<N>::size_in_bits()).ok_or_else(|| anyhow!("`size_in_bits` overflowed"))?;
62
63 Ok(size)
64 }
65
66 /// Returns the number of raw bits in a dynamic record.
67 #[inline]
68 pub fn size_in_bits_raw() -> Result<usize> {
69 Self::size_in_bits()
70 }
71}